com.aspose.words

Class ChartDataLabel

  • java.lang.Object
    • com.aspose.words.ChartDataLabel
  • All Implemented Interfaces:
    java.lang.Cloneable
    public class ChartDataLabel 
    extends java.lang.Object

Represents data label on a chart point or trendline.
On a series, the ChartDataLabel object is a member of the ChartDataLabelCollection. The ChartDataLabelCollection contains a ChartDataLabel object for each point.

Example:

Shows how to apply labels to data points in a chart.
public void chartDataLabels() throws Exception {
    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.0, 300.0);

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

    // The chart already contains demo data comprised of 3 series each with 4 categories
    Assert.assertEquals(chart.getSeries().getCount(), 3);
    Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");

    // Apply data labels to every series in the graph
    for (ChartSeries series : chart.getSeries()) {
        applyDataLabels(series, 4, "000.0", ", ");
        Assert.assertEquals(series.getDataLabels().getCount(), 4);
    }

    // Get the enumerator for a data label collection
    Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();

    // And use it to go over all the data labels in one series and change their separator
    while (enumerator.hasNext()) {
        Assert.assertEquals(enumerator.next().getSeparator(), ", ");
        enumerator.next().setSeparator(" & ");
    }

    // If the chart looks too busy, we can remove data labels one by one
    chart.getSeries().get(1).getDataLabels().removeAt(2);

    // We can also clear an entire data label collection for one whole series
    chart.getSeries().get(2).getDataLabels().clear();

    doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
        Assert.assertFalse(label.isVisible());

        // Edit the appearance of the new data label
        label.setShowCategoryName(true);
        label.setShowSeriesName(true);
        label.setShowValue(true);
        label.setShowLeaderLines(true);
        label.setShowLegendKey(true);
        label.setShowPercentage(false);
        Assert.assertFalse(label.getShowDataLabelsRange());

        // Apply number format and separator
        label.getNumberFormat().setFormatCode(numberFormat);
        label.setSeparator(separator);

        // The label automatically becomes visible
        Assert.assertTrue(label.isVisible());
    }
}

Property Getters/Setters Summary
intgetIndex()
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.
booleanisVisible()
Returns true if this data label has something to display.
ChartNumberFormatgetNumberFormat()
Returns number format of the parent element.
java.lang.StringgetSeparator()
void
setSeparator(java.lang.Stringvalue)
           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.
booleangetShowBubbleSize()
void
setShowBubbleSize(booleanvalue)
           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.
booleangetShowCategoryName()
void
setShowCategoryName(booleanvalue)
           Allows to specify if category name is to be displayed for the data labels on a chart. Default value is false.
booleangetShowDataLabelsRange()
void
           Allows to specify if values from data labels range to be displayed in the data labels. Default value is false.
booleangetShowLeaderLines()
void
setShowLeaderLines(booleanvalue)
           Allows to specify if data label leader lines need be shown. Default value is false.
booleangetShowLegendKey()
void
setShowLegendKey(booleanvalue)
           Allows to specify if legend key is to be displayed for the data labels on a chart. Default value is false.
booleangetShowPercentage()
void
setShowPercentage(booleanvalue)
           Allows to specify if percentage value is to be displayed for the data labels on a chart. Default value is false.
booleangetShowSeriesName()
void
setShowSeriesName(booleanvalue)
           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.
booleangetShowValue()
void
setShowValue(booleanvalue)
           Allows to specify if values are to be displayed in the data labels. Default value is false.
 

    • Property Getters/Setters Detail

      • getIndex

        public int getIndex()
        
        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.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • isVisible

        public boolean isVisible()
        
        Returns true if this data label has something to display.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getNumberFormat

        public ChartNumberFormat getNumberFormat()
        
        Returns number format of the parent element.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getSeparator/setSeparator

        public java.lang.String getSeparator() / public void setSeparator(java.lang.String value)
        
        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.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getShowBubbleSize/setShowBubbleSize

        public boolean getShowBubbleSize() / public void setShowBubbleSize(boolean value)
        
        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.

        Example:

        Demonstrates bubble chart-exclusive features.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Insert a bubble chart with 3D effects on each bubble
        Shape shape = builder.insertChart(ChartType.BUBBLE_3_D, 500.0, 350.0);
        Chart chart = shape.getChart();
        
        Assert.assertTrue(chart.getSeries().get(0).getBubble3D());
        
        // Apply a data label to each bubble that displays the size of its bubble
        for (int i = 0; i < 3; i++) {
            ChartDataLabel cdl = chart.getSeries().get(0).getDataLabels().add(i);
            cdl.setShowBubbleSize(true);
        }
        
        doc.save(getArtifactsDir() + "Charts.Bubble3D.docx");
      • getShowCategoryName/setShowCategoryName

        public boolean getShowCategoryName() / public void setShowCategoryName(boolean value)
        
        Allows to specify if category name is to be displayed for the data labels on a chart. Default value is false.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getShowDataLabelsRange/setShowDataLabelsRange

        public boolean getShowDataLabelsRange() / public void setShowDataLabelsRange(boolean value)
        
        Allows to specify if values from data labels range to be displayed in the data labels. Default value is false.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getShowLeaderLines/setShowLeaderLines

        public boolean getShowLeaderLines() / public void setShowLeaderLines(boolean value)
        
        Allows to specify if data label leader lines need be shown. Default value is false. Applies to Pie charts only. Leader lines create a visual connection between a data label and its corresponding data point.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getShowLegendKey/setShowLegendKey

        public boolean getShowLegendKey() / public void setShowLegendKey(boolean value)
        
        Allows to specify if legend key is to be displayed for the data labels on a chart. Default value is false.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getShowPercentage/setShowPercentage

        public boolean getShowPercentage() / public void setShowPercentage(boolean value)
        
        Allows to specify if percentage value is to be displayed for the data labels on a chart. Default value is false.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getShowSeriesName/setShowSeriesName

        public boolean getShowSeriesName() / public void setShowSeriesName(boolean value)
        
        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.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }
      • getShowValue/setShowValue

        public boolean getShowValue() / public void setShowValue(boolean value)
        
        Allows to specify if values are to be displayed in the data labels. Default value is false.

        Example:

        Shows how to apply labels to data points in a chart.
        public void chartDataLabels() throws Exception {
            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.0, 300.0);
        
            // Get the chart object from the containing shape
            Chart chart = chartShape.getChart();
        
            // The chart already contains demo data comprised of 3 series each with 4 categories
            Assert.assertEquals(chart.getSeries().getCount(), 3);
            Assert.assertEquals(chart.getSeries().get(0).getName(), "Series 1");
        
            // Apply data labels to every series in the graph
            for (ChartSeries series : chart.getSeries()) {
                applyDataLabels(series, 4, "000.0", ", ");
                Assert.assertEquals(series.getDataLabels().getCount(), 4);
            }
        
            // Get the enumerator for a data label collection
            Iterator<ChartDataLabel> enumerator = chart.getSeries().get(0).getDataLabels().iterator();
        
            // And use it to go over all the data labels in one series and change their separator
            while (enumerator.hasNext()) {
                Assert.assertEquals(enumerator.next().getSeparator(), ", ");
                enumerator.next().setSeparator(" & ");
            }
        
            // If the chart looks too busy, we can remove data labels one by one
            chart.getSeries().get(1).getDataLabels().removeAt(2);
        
            // We can also clear an entire data label collection for one whole series
            chart.getSeries().get(2).getDataLabels().clear();
        
            doc.save(getArtifactsDir() + "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.getDataLabels().add(i);
                Assert.assertFalse(label.isVisible());
        
                // Edit the appearance of the new data label
                label.setShowCategoryName(true);
                label.setShowSeriesName(true);
                label.setShowValue(true);
                label.setShowLeaderLines(true);
                label.setShowLegendKey(true);
                label.setShowPercentage(false);
                Assert.assertFalse(label.getShowDataLabelsRange());
        
                // Apply number format and separator
                label.getNumberFormat().setFormatCode(numberFormat);
                label.setSeparator(separator);
        
                // The label automatically becomes visible
                Assert.assertTrue(label.isVisible());
            }
        }