Skip to main content

Create Waterfall, Funnel, Treemap, and Other Office 2016 Charts — Presentation API for Java

  • 6 minutes to read

The Presentation API allows you to create chart types introduced in Microsoft Excel 2016 (for example, waterfall, funnel, and treemap charts) and display them on slides. The ChartEx class implements these charts.

To create a standard chart (for example, a bar, line, or pie chart), use the Chart class.

Create a ChartEx Chart

Follow the steps below to add a chart to a slide:

  1. Create a ChartEx object and add it to the SlideBase.getShapes() collection.
  2. Call ChartEx.setWidth(value) and ChartEx.setHeight(value) methods to specify the chart size.
  3. Create a series object of one of the supported chart types.
  4. Call DataSeriesExBase.setArguments(value) and DataSeriesExBase.setValues(value) methods to specify series data.
  5. Add the series to the ChartEx.getSeries() collection.

The following code snippet creates a waterfall chart, displays the first and last data points as totals, displays connector lines, and adds a chart title:

import java.io.*;
import java.nio.file.*;
import java.util.List;
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.
            ChartEx chart = new ChartEx();
            chart.setWidth(presentation.getSlideSize().getWidth());
            chart.setHeight(presentation.getSlideSize().getHeight());
            slide.getShapes().add(chart);

            // Create a waterfall series and specify data.
            WaterfallSeries series = new WaterfallSeries();
            series.setText("Cash Flow");
            series.setArguments(new ChartStringData(List.of("Start", "Jan", "Feb", "Mar", "End")));
            series.setValues(new ChartNumericData(List.of(10.0, 12.0, -5.0, 8.0, 25.0)));

            // Display the first and last points as totals and connect the columns.
            series.getSubtotalDataPoints().add(0);
            series.getSubtotalDataPoints().add(4);
            series.setShowConnectorLines(true);
            chart.getSeries().add(series);

            // Add a chart title.
            TextProperties titleTextProperties = new TextProperties();
            titleTextProperties.setFontSize(30f);
            titleTextProperties.setFill(new SolidFill(Color.getDarkBlue()));
            chart.setTitle(new ChartTitleEx(
                    new TextArea("Cash Flow", 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:

 Waterfall chart with total columns and connector lines created with the DevExpress Presentation API for Java

Supported Chart Types

The series type determines the chart type. Add an object of one of the following series classes to the ChartEx.getSeries() collection:

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

The ChartExType enumeration lists chart types. Call the SeriesExBase.getChartType() method to get the chart type of a series.

A Pareto chart contains two series. Add a HistogramSeries object to the collection; then, pass the histogram series to the ParetoSeries(histogramSeries) constructor, and add the created ParetoSeries object to the collection.

Specify Series Data

Pass one of the following objects to the setArguments(value) or setValues(value) method of a series:

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.
ChartHierarchicalStringData Multi-level string chart data specified in code.

ChartStringData and ChartHierarchicalStringData objects specify arguments only.

Sunburst and treemap charts display hierarchical categories. To specify category levels, pass a ChartDataReference object that references adjacent columns of chart data cells to the setArguments(value) method. The first column contains top-level categories. To specify category levels in code, pass a ChartHierarchicalStringData object.

Call the ChartEx.getData() method to access chart data 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 Appearance

Call the following methods to customize chart appearance:

Method Description
setColorPalette(ChartColorPalette value) Sets the chart color palette.
setStyle(ChartExStyle value) Sets the chart style.
setTitle(ChartTitleEx value) Sets the chart title.
setLegend(LegendEx value) Sets the chart legend.
ChartEx.getPlotArea() Returns plot area settings.

The following code snippet creates a waterfall chart, applies the Monochromatic 1 color palette and a predefined style, and fills the plot area with a solid color:

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 {
        String[] months = { "Start", "Jan", "Feb", "Mar", "End" };
        double[] amounts = { 10, 12, -5, 8, 25 };

        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.
            ChartEx chart = new ChartEx();
            chart.setWidth(presentation.getSlideSize().getWidth());
            chart.setHeight(presentation.getSlideSize().getHeight());
            slide.getShapes().add(chart);

            // Add months and amounts to chart data cells.
            ChartData data = chart.getData();
            for (int i = 0; i < months.length; i++) {
                data.get(0, "A" + (i + 1)).setTextValue(months[i]);
                data.get(0, "B" + (i + 1)).setNumericValue(amounts[i]);
            }

            // Create a waterfall series and display the first and last points as totals.
            WaterfallSeries series = new WaterfallSeries();
            series.setText("Cash Flow");
            series.setArguments(new ChartDataReference(0, "A1", "A5"));
            series.setValues(new ChartDataReference(0, "B1", "B5"));
            series.getSubtotalDataPoints().add(0);
            series.getSubtotalDataPoints().add(4);
            chart.getSeries().add(series);

            // Apply a color palette and a chart style, and fill the plot area.
            chart.setColorPalette(ChartColorPalette.MONOCHROMATIC_1);
            chart.setStyle(ChartExStyle.STYLE_3);
            chart.getPlotArea().setFill(new SolidFill(Color.getAliceBlue()));

            // 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:

 Waterfall chart with the Monochromatic 1 color palette and a filled plot area created with the DevExpress Presentation API for Java

Customize Individual Data Points

Call the SeriesExBase.getCustomDataPoints() method to access custom settings for individual data points. The returned dictionary maps zero-based data point indexes to DataPointEx objects.

The following code snippet creates a funnel chart and fills the last data point with red color:

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 {
        String[] stages = { "Leads", "Qualified", "Proposals", "Negotiations", "Deals" };
        double[] counts = { 1200, 640, 310, 150, 70 };

        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.
            ChartEx chart = new ChartEx();
            chart.setWidth(presentation.getSlideSize().getWidth());
            chart.setHeight(presentation.getSlideSize().getHeight());
            slide.getShapes().add(chart);

            // Add stages and counts to chart data cells.
            ChartData data = chart.getData();
            for (int i = 0; i < stages.length; i++) {
                data.get(0, "A" + (i + 1)).setTextValue(stages[i]);
                data.get(0, "B" + (i + 1)).setNumericValue(counts[i]);
            }

            // Create a funnel series.
            FunnelSeries series = new FunnelSeries();
            series.setText("Sales Pipeline");
            series.setArguments(new ChartDataReference(0, "A1", "A5"));
            series.setValues(new ChartDataReference(0, "B1", "B5"));
            chart.getSeries().add(series);

            // Fill the last data point with red.
            DataPointEx point = new DataPointEx();
            point.setFill(new SolidFill(Color.getRed()));
            series.getCustomDataPoints().put(4, point);

            // 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:

 Funnel chart with the last data point filled with red created with the DevExpress Presentation API for Java

Customize Axes, Data Labels, and Legend

Refer to the following help topics for additional information: