Skip to main content

ChartData Class

Contains cells with chart data.

Declaration

public class ChartData extends OfficeObject

Remarks

Call the Chart.getData() or ChartEx.getData() method to access chart data. Chart data stores cell values on sheets identified by zero-based indexes. Methods without a sheet index parameter use the first sheet (index 0).

Call get methods to access cells (ChartCellValue objects). If a cell does not exist, a get method creates the cell. To bind series to cells, pass a ChartDataReference object to a series method (for example, DataSeriesExBase.setValues(value)).

Refer to the following help topic for additional information: Load Data into Charts — Presentation API for Java.

Example

Create a Bar Chart with a Title

The following code snippet adds a bar chart to a slide, binds the series to chart data, and adds a chart title:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;
import com.devexpress.system.drawing.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (Presentation presentation = new Presentation()) {
            // Add a blank slide.
            presentation.getSlides().clear();
            Slide slide = new Slide(SlideLayoutType.BLANK);
            presentation.getSlides().add(slide);

            // Insert a chart that fills the slide.
            Chart chart = new Chart();
            chart.setWidth(presentation.getSlideSize().getWidth());
            chart.setHeight(presentation.getSlideSize().getHeight());
            slide.getShapes().add(chart);

            // Populate chart data.
            ChartData data = chart.getData();
            data.get(0, "A1").setNumericValue(120);
            data.get(0, "A2").setNumericValue(95);
            data.get(0, "A3").setNumericValue(140);
            data.get(0, "B1").setTextValue("Q1");
            data.get(0, "B2").setTextValue("Q2");
            data.get(0, "B3").setTextValue("Q3");

            // Bind series values and arguments to chart data ranges.
            BarSeries series = new BarSeries();
            series.setValues(new ChartDataReference(0, "A1", "A3"));
            series.setArguments(new ChartDataReference(0, "B1", "B3"));
            chart.getSeries().add(series);

            // Add a chart title.
            TextProperties titleTextProperties = new TextProperties();
            titleTextProperties.setFontSize(30f);
            titleTextProperties.setFill(new SolidFill(Color.getDarkBlue()));
            chart.setTitle(new ChartTitle(
                    new TextArea("Quarterly Sales", new TextParagraphProperties(titleTextProperties))));

            // Save the presentation.
            Files.createDirectories(Path.of("output"));
            try (OutputStream output = Files.newOutputStream(Path.of("output", "Chart.pptx"))) {
                presentation.save(output);
            }
        }
    }
}

The following screenshot illustrates the result:

 Column chart with the Quarterly Sales title created with the DevExpress Presentation API for Java

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
JavaObject
OfficeObject
ChartData
See Also

ChartData()

Initializes a new instance of the ChartData class.

Declaration

public ChartData()

Methods

deepClone() Method

Creates a copy of the chart data.

Declaration

public ChartData deepClone()

Returns

Type Description
ChartData

A copy of the chart data.

Remarks

The copy contains copies of all cells.

get(ChartDataReference reference) Method

Returns chart data cells in the specified data reference.

Declaration

public List<ChartCellValue> get(ChartDataReference reference)

Parameters

Name Type Description
reference ChartDataReference

A cell, range, or union of ranges.

Returns

Type Description
java.util.List<ChartCellValue>

Cells in the data reference.

Remarks

The get method returns cells row by row. If the data reference is a union of ranges, the get method returns cells range by range. If a cell does not exist, the get method creates the cell.

If the reference parameter is null, the get method returns an empty list.

get(int sheetIndex, String cellReference) Method

Returns the chart data cell at the specified address on the specified sheet.

Declaration

public ChartCellValue get(int sheetIndex, String cellReference)

Parameters

Name Type Description
sheetIndex int

The zero-based index of the chart data sheet.

cellReference String

The cell address in A1 notation (for example, B2).

Returns

Type Description
ChartCellValue

The chart data cell.

Exceptions

Type Description
ArgumentException

Thrown if the cellReference value is not a valid cell address in A1 notation or is outside the A1:XFD1048576 range.

ArgumentOutOfRangeException

Thrown if the chart is on a slide and the sheetIndex value is negative or chart data does not contain a sheet with this index (for example, a new chart contains a single sheet with index 0).

NullPointerException

Thrown if the cellReference value is null.

Remarks

If the cell does not exist, the get method creates the cell. Cell addresses are case-insensitive and can contain absolute reference characters (for example, $A$1).

get(String cellReference) Method

Returns the chart data cell at the specified address on the first sheet.

Declaration

public ChartCellValue get(String cellReference)

Parameters

Name Type Description
cellReference String

The cell address in A1 notation (for example, B2).

Returns

Type Description
ChartCellValue

The chart data cell.

Exceptions

Type Description
ArgumentException

Thrown if the cellReference value is not a valid cell address in A1 notation or is outside the A1:XFD1048576 range.

NullPointerException

Thrown if the cellReference value is null.

Remarks

If the cell does not exist, the get method creates the cell. Cell addresses are case-insensitive and can contain absolute reference characters (for example, $A$1).