ChartDataLabelCollection Class
Represents a collection of ChartDataLabel.
Inheritance Hierarchy
SystemObject
  Aspose.Words.Drawing.ChartsChartDataLabelCollection

Namespace: Aspose.Words.Drawing.Charts
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
public class ChartDataLabelCollection : IEnumerable<ChartDataLabel>, 
	IEnumerable

The ChartDataLabelCollection type exposes the following members.

Properties
  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.
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.
Methods
  NameDescription
Public methodCode exampleAdd
Adds new ChartDataLabel at the specified index.
Public methodCode exampleClear
Removes all ChartDataLabel from this collection.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodCode exampleGetEnumerator
Returns an enumerator object.
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodCode exampleRemoveAt
Removes a ChartDataLabel at the specified index.
Public methodToString (Inherited from Object.)
Examples
Shows how to apply labels to data points in a chart.
public void ChartDataLabels()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Use a document builder to insert a bar chart
    Shape chartShape = builder.InsertChart(ChartType.Line, 400, 300);

    // Get the chart object from the containing shape
    Chart chart = chartShape.Chart;

    // The chart already contains demo data comprised of 3 series each with 4 categories
    Assert.AreEqual(3, chart.Series.Count);
    Assert.AreEqual("Series 1", chart.Series[0].Name);

    // Apply data labels to every series in the graph
    foreach (ChartSeries series in chart.Series)
    {
        ApplyDataLabels(series, 4, "000.0", ", ");
        Assert.AreEqual(4, series.DataLabels.Count);
    }

    // Get the enumerator for a data label collection
    using (IEnumerator<ChartDataLabel> enumerator = chart.Series[0].DataLabels.GetEnumerator())
    {
        // And use it to go over all the data labels in one series and change their separator
        while (enumerator.MoveNext())
        {
            Assert.AreEqual(", ", enumerator.Current.Separator);
            enumerator.Current.Separator = " & ";
        }
    }

    // If the chart looks too busy, we can remove data labels one by one
    chart.Series[1].DataLabels.RemoveAt(2);

    // We can also clear an entire data label collection for one whole series
    chart.Series[2].DataLabels.Clear();

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

/// <summary>
/// Apply uniform data labels with custom number format and separator to a number (determined by labelsCount) of data points in a series
/// </summary>
private void ApplyDataLabels(ChartSeries series, int labelsCount, string numberFormat, string separator)
{
    for (int i = 0; i < labelsCount; i++)
    {
        ChartDataLabel label = series.DataLabels.Add(i);
        Assert.False(label.IsVisible);

        // Edit the appearance of the new data label
        label.ShowCategoryName = true;
        label.ShowSeriesName = true;
        label.ShowValue = true;
        label.ShowLeaderLines = true;
        label.ShowLegendKey = true;
        label.ShowPercentage = false;
        Assert.False(label.ShowDataLabelsRange);

        // Apply number format and separator
        label.NumberFormat.FormatCode = numberFormat;
        label.Separator = separator;

        // The label automatically becomes visible
        Assert.True(label.IsVisible);
    }
}
See Also