ChartDataLabelCollection Class

Represents a collection of ChartDataLabel.

ExpandedInheritance Hierarchy

SystemObject
  Aspose.Words.Drawing.ChartsChartDataLabelCollection

Namespace:  Aspose.Words.Drawing.Charts
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 22.1.0

ExpandedSyntax

public class ChartDataLabelCollection : IEnumerable<ChartDataLabel>, 
	IEnumerable

The ChartDataLabelCollection type exposes the following members.

ExpandedProperties

  NameDescription
Public propertyCode exampleCount
Returns the number of ChartDataLabel in this collection.
Public propertyCode exampleItem
Returns ChartDataLabel for the specified index.
Public propertyCode exampleNumberFormat
Gets an ChartNumberFormat instance allowing to set number format for the data labels of the entire series.
Public propertyCode exampleSeparator
Gets or sets string separator used for the data labels of the entire series. The default is a comma, except for pie charts showing only category name and percentage, when a line break shall be used instead.
Public propertyCode exampleShowBubbleSize
Allows to specify whether bubble size is to be displayed for the data labels of the entire series. Applies only to Bubble charts. Default value is false.
Public propertyCode exampleShowCategoryName
Allows to specify whether category name is to be displayed for the data labels of the entire series. Default value is false.
Public propertyShowDataLabelsRange
Allows to specify whether values from data labels range to be displayed in the data labels of the entire series. Default value is false.
Public propertyCode exampleShowLeaderLines
Allows to specify whether data label leader lines need be shown for the data labels of the entire series. Default value is false.
Public propertyCode exampleShowLegendKey
Allows to specify whether legend key is to be displayed for the data labels of the entire series. Default value is false.
Public propertyCode exampleShowPercentage
Allows to specify whether percentage value is to be displayed for the data labels of the entire series. Default value is false. Applies only to Pie charts.
Public propertyCode exampleShowSeriesName
Returns or sets a Boolean to indicate the series name display behavior for the data labels of the entire series. True to show the series name. False to hide. By default false.
Public propertyCode exampleShowValue
Allows to specify whether values are to be displayed in the data labels of the entire series. Default value is false.

ExpandedMethods

  NameDescription
Public methodCode exampleAdd Obsolete.
Adds new ChartDataLabel at the specified index.
Public methodCode exampleClear Obsolete.
Clears format of all ChartDataLabel in this collection.
Public methodClearFormat
Clears format of all ChartDataLabel in this collection.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodCode exampleGetEnumerator
Returns an enumerator object.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodCode exampleRemoveAt Obsolete.
Clears format of a ChartDataLabel at the specified index.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)

ExpandedExamples

Shows how to apply labels to data points in a line chart.
public void DataLabels()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    Shape chartShape = builder.InsertChart(ChartType.Line, 400, 300);
    Chart chart = chartShape.Chart;

    Assert.AreEqual(3, chart.Series.Count);
    Assert.AreEqual("Series 1", chart.Series[0].Name);
    Assert.AreEqual("Series 2", chart.Series[1].Name);
    Assert.AreEqual("Series 3", chart.Series[2].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.
    foreach (ChartSeries series in chart.Series)
    {
        ApplyDataLabels(series, 4, "000.0", ", ");
        Assert.AreEqual(4, series.DataLabels.Count);
    }

    // Change the separator string for every data label in a series.
    using (IEnumerator<ChartDataLabel> enumerator = chart.Series[0].DataLabels.GetEnumerator())
    {
        while (enumerator.MoveNext())
        {
            Assert.AreEqual(", ", enumerator.Current.Separator);
            enumerator.Current.Separator = " & ";
        }
    }

    // For a cleaner looking graph, we can remove data labels individually.
    chart.Series[1].DataLabels[2].ClearFormat();

    // We can also strip an entire series of its data labels at once.
    chart.Series[2].DataLabels.ClearFormat();

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

/// <summary>
/// Apply data labels with custom number format and separator to several data points in a series.
/// </summary>
private static void ApplyDataLabels(ChartSeries series, int labelsCount, string numberFormat, string separator)
{
    for (int i = 0; i < labelsCount; i++)
    {
        series.HasDataLabels = true;

        Assert.False(series.DataLabels[i].IsVisible);

        series.DataLabels[i].ShowCategoryName = true;
        series.DataLabels[i].ShowSeriesName = true;
        series.DataLabels[i].ShowValue = true;
        series.DataLabels[i].ShowLeaderLines = true;
        series.DataLabels[i].ShowLegendKey = true;
        series.DataLabels[i].ShowPercentage = false;
        series.DataLabels[i].IsHidden = false;
        Assert.False(series.DataLabels[i].ShowDataLabelsRange);

        series.DataLabels[i].NumberFormat.FormatCode = numberFormat;
        series.DataLabels[i].Separator = separator;

        Assert.False(series.DataLabels[i].ShowDataLabelsRange);
        Assert.True(series.DataLabels[i].IsVisible);
        Assert.False(series.DataLabels[i].IsHidden);
    }
}

ExpandedSee Also