Skip to main content

Create and Customize Charts — Presentation API for Java

  • 5 minutes to read

The Presentation API creates charts on slides, reads charts in existing presentations, and customizes chart elements in code. The API supports standard charts (for example, bar, line, and pie charts) and chart types introduced in Microsoft Excel 2016 (for example, waterfall, funnel, and treemap charts). A standard chart can also combine multiple chart types (a combo chart).

Chart Types

The series type specifies the chart type. For example, a BarSeries object creates a bar chart, and a WaterfallSeries object creates a waterfall chart.

Standard Charts

 Bar, line, pie, area, doughnut, and 3D column charts created with the DevExpress Presentation API for Java

A standard chart is a Chart object. To create a standard chart, add series of the following classes to the Chart.getSeries() collection:

Chart Type Series Class
Area AreaSeries
3D area Area3DSeries
Bar and column BarSeries
3D bar and column Bar3DSeries
Bubble BubbleSeries
Doughnut DoughnutSeries
Line LineSeries
3D line Line3DSeries
Pie PieSeries
3D pie Pie3DSeries
Pie of pie and bar of pie ChartOfPieSeries
Radar RadarSeries
Scatter ScatterSeries
Stock StockSeries
Surface SurfaceSeries
3D surface Surface3DSeries

Refer to the following help topic for additional information: Create Standard Charts — Presentation API for Java.

Microsoft Excel 2016 Charts

 Waterfall, funnel, histogram, box and whisker, sunburst, and treemap charts created with the DevExpress Presentation API for Java

To create a chart introduced in Microsoft Excel 2016, create a ChartEx object and add series of the following classes to the ChartEx.getSeries() collection:

Chart Type Series Class
Box and whisker BoxAndWhiskerSeries
Funnel FunnelSeries
Histogram HistogramSeries
Map MapSeries
Pareto HistogramSeries and ParetoSeries
Sunburst SunburstSeries
Treemap TreemapSeries
Waterfall WaterfallSeries

Refer to the following help topic for additional information: Create Waterfall, Funnel, Treemap, and Other Office 2016 Charts — Presentation API for Java.

Create a Chart

To add a chart to a slide, create a Chart or ChartEx object, add series to the chart, and add the chart to the SlideBase.getShapes() collection.

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

Access Charts on a Slide

A chart is a shape. To find charts on a slide, iterate through the SlideBase.getShapes() collection and check the shape type.

A standard chart displays series in chart views (getViews()). A combo chart contains multiple views. Call the ChartViewBase.getChartType() method to get the chart type of a view.

In a ChartEx chart, call the SeriesExBase.getChartType() method to get the chart type of a series.

The following code snippet loads a presentation and prints chart types of all charts on each slide:

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

public class Main {
    public static void main(String[] args) throws Exception {
        try (InputStream input = Files.newInputStream(Path.of("Document.pptx"));
             Presentation presentation = new Presentation(input)) {
            for (int i = 0; i < presentation.getSlides().size(); i++) {
                for (ShapeBase shape : presentation.getSlides().get(i).getShapes()) {
                    if (shape instanceof Chart chart) {
                        for (ChartViewBase view : chart.getViews())
                            System.out.println("Slide " + (i + 1) + ": " + view.getChartType());
                    } else if (shape instanceof ChartEx chartEx) {
                        for (SeriesExBase series : chartEx.getSeries())
                            System.out.println("Slide " + (i + 1) + ": " + series.getChartType());
                    }
                }
            }
        }
    }
}

Load Data to a Chart

Series in a standard chart get data from SeriesBase.setValues(value) and SeriesBase.setArguments(value) methods. Series in a ChartEx chart get data from DataSeriesExBase.setValues(value) and DataSeriesExBase.setArguments(value) methods.

To specify series data, pass one of the following objects to the setValues(value) or setArguments(value) method:

Class Description
ChartDataReference A cell, range, or union of ranges that contains chart data.
ChartNumericData Numeric chart data specified in code.
ChartStringData String chart data specified in code.

Call the Chart.getData() or ChartEx.getData() method to assign values to cells that ChartDataReference objects refer to.

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

Customize Chart Elements

Call the following methods to customize chart elements:

Chart Element Chart Methods ChartEx Methods
Title setTitle(ChartTitle value) setTitle(ChartTitleEx value)
Legend setLegend(Legend value) setLegend(LegendEx value)
Axes Chart.getArgumentAxis(), Chart.getValueAxis() ChartEx.getArgumentAxis(), ChartEx.getValueAxis()
Chart style setStyle(ChartStyle value) setStyle(ChartExStyle value)
Color palette Not available setColorPalette(ChartColorPalette value)
Background Chart.setFill(value) ChartEx.setFill(value)
Outline Chart.setOutlineStyle(value) ChartEx.setOutlineStyle(value)
Plot area Chart.setPlotArea(value) ChartEx.setPlotArea(value)

Refer to the following help topics for additional information: