Aspose::Words::Drawing::Charts::ChartDataLabel class

ChartDataLabel class

Represents data label on a chart point or trendline. To learn more, visit the Working with Charts documentation article.

class ChartDataLabel : public Aspose::Words::Drawing::Charts::Core::INumberFormatProvider,
                       public Aspose::Words::Drawing::Charts::Core::IChartItemTextProperties,
                       public Aspose::Words::Drawing::Charts::Core::IChartFormatSource

Methods

MethodDescription
ClearFormat()Clears format of this data label. The properties are set to the default values defined in the parent data label collection.
get_Font()Provides access to the font formatting of this data label.
get_Format()Provides access to fill and line formatting of the data label.
get_Index()Specifies the index of the containing element. This index shall determine which of the parent’s children collection this element applies to. Default value is 0.
get_IsHidden()Gets/sets a flag indicating whether this label is hidden. The default value is false.
get_IsVisible()Returns true if this data label has something to display.
get_Left()Gets or sets the distance of the data label in points from the left edge of the chart or from the position specified by its Position property, depending on the value of the LeftMode property.
get_LeftMode()Gets the interpretation mode of the Left property value: whether it sets the location of the data label from the left edge of the chart of from the position specified by its Position property.
get_NumberFormat()Returns number format of the parent element.
get_Orientation()Gets the orientation of the label text.
get_Position()Gets or sets the position of the data label.
get_Rotation()Gets or sets the rotation of the label in degrees.
get_Separator()Gets or sets string separator used for the data labels on a chart. The default is a comma, except for pie charts showing only category name and percentage, when a line break shall be used instead.
get_ShowBubbleSize()Allows to specify if bubble size is to be displayed for the data labels on a chart. Applies only to Bubble charts. Default value is false.
get_ShowCategoryName()Allows to specify if category name is to be displayed for the data labels on a chart. Default value is false.
get_ShowDataLabelsRange()Allows to specify if values from data labels range to be displayed in the data labels. Default value is false.
get_ShowLeaderLines()Allows to specify if data label leader lines need be shown. Default value is false.
get_ShowLegendKey()Allows to specify if legend key is to be displayed for the data labels on a chart. Default value is false.
get_ShowPercentage()Allows to specify if percentage value is to be displayed for the data labels on a chart. Default value is false.
get_ShowSeriesName()Returns or sets a Boolean to indicate the series name display behavior for the data labels on a chart. true to show the series name; false to hide. By default false.
get_ShowValue()Allows to specify if values are to be displayed in the data labels. Default value is false.
get_Top()Gets or sets the distance of the data label in points from the top edge of the chart or from the position specified by its Position property, depending on the value of the TopMode property.
get_TopMode()Gets the interpretation mode of the Top property value: whether it sets the location of the data label from the top edge of the chart of from the position specified by its Position property.
GetType() const override
Is(const System::TypeInfo&) const override
set_IsHidden(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_IsHidden.
set_Left(double)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_Left.
set_LeftMode(Aspose::Words::Drawing::Charts::ChartDataLabelLocationMode)Sets the interpretation mode of the Left property value: whether it sets the location of the data label from the left edge of the chart of from the position specified by its Position property.
set_Orientation(Aspose::Words::Drawing::ShapeTextOrientation)Sets the orientation of the label text.
set_Position(Aspose::Words::Drawing::Charts::ChartDataLabelPosition)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_Position.
set_Rotation(int32_t)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_Rotation.
set_Separator(const System::String&)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_Separator.
set_ShowBubbleSize(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowBubbleSize.
set_ShowCategoryName(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowCategoryName.
set_ShowDataLabelsRange(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowDataLabelsRange.
set_ShowLeaderLines(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowLeaderLines.
set_ShowLegendKey(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowLegendKey.
set_ShowPercentage(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowPercentage.
set_ShowSeriesName(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowSeriesName.
set_ShowValue(bool)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_ShowValue.
set_Top(double)Setter for Aspose::Words::Drawing::Charts::ChartDataLabel::get_Top.
set_TopMode(Aspose::Words::Drawing::Charts::ChartDataLabelLocationMode)Sets the interpretation mode of the Top property value: whether it sets the location of the data label from the top edge of the chart of from the position specified by its Position property.
static Type()

Examples

Shows how to apply labels to data points in a line chart.

void DataLabels()
{
    auto doc = MakeObject<Document>();
    auto builder = MakeObject<DocumentBuilder>(doc);

    SharedPtr<Shape> chartShape = builder->InsertChart(ChartType::Line, 400, 300);
    SharedPtr<Chart> chart = chartShape->get_Chart();

    ASSERT_EQ(3, chart->get_Series()->get_Count());
    ASSERT_EQ(u"Series 1", chart->get_Series()->idx_get(0)->get_Name());
    ASSERT_EQ(u"Series 2", chart->get_Series()->idx_get(1)->get_Name());
    ASSERT_EQ(u"Series 3", chart->get_Series()->idx_get(2)->get_Name());

    // Apply data labels to every series in the chart.
    // These labels will appear next to each data point in the graph and display its value.
    for (const auto& series : System::IterateOver(chart->get_Series()))
    {
        ApplyDataLabels(series, 4, u"000.0", u", ");
        ASSERT_EQ(4, series->get_DataLabels()->get_Count());
    }

    // Change the separator string for every data label in a series.
    {
        SharedPtr<System::Collections::Generic::IEnumerator<SharedPtr<ChartDataLabel>>> enumerator =
            chart->get_Series()->idx_get(0)->get_DataLabels()->GetEnumerator();
        while (enumerator->MoveNext())
        {
            ASSERT_EQ(u", ", enumerator->get_Current()->get_Separator());
            enumerator->get_Current()->set_Separator(u" & ");
        }
    }

    // For a cleaner looking graph, we can remove data labels individually.
    chart->get_Series()->idx_get(1)->get_DataLabels()->idx_get(2)->ClearFormat();

    // We can also strip an entire series of its data labels at once.
    chart->get_Series()->idx_get(2)->get_DataLabels()->ClearFormat();

    doc->Save(ArtifactsDir + u"Charts.DataLabels.docx");
}

static void ApplyDataLabels(SharedPtr<ChartSeries> series, int labelsCount, String numberFormat, String separator)
{
    for (int i = 0; i < labelsCount; i++)
    {
        series->set_HasDataLabels(true);

        ASSERT_FALSE(series->get_DataLabels()->idx_get(i)->get_IsVisible());

        series->get_DataLabels()->idx_get(i)->set_ShowCategoryName(true);
        series->get_DataLabels()->idx_get(i)->set_ShowSeriesName(true);
        series->get_DataLabels()->idx_get(i)->set_ShowValue(true);
        series->get_DataLabels()->idx_get(i)->set_ShowLeaderLines(true);
        series->get_DataLabels()->idx_get(i)->set_ShowLegendKey(true);
        series->get_DataLabels()->idx_get(i)->set_ShowPercentage(false);
        series->get_DataLabels()->idx_get(i)->set_IsHidden(false);
        ASSERT_FALSE(series->get_DataLabels()->idx_get(i)->get_ShowDataLabelsRange());

        series->get_DataLabels()->idx_get(i)->get_NumberFormat()->set_FormatCode(numberFormat);
        series->get_DataLabels()->idx_get(i)->set_Separator(separator);

        ASSERT_FALSE(series->get_DataLabels()->idx_get(i)->get_ShowDataLabelsRange());
        ASSERT_TRUE(series->get_DataLabels()->idx_get(i)->get_IsVisible());
        ASSERT_FALSE(series->get_DataLabels()->idx_get(i)->get_IsHidden());
    }
}

See Also