Skip to main content

IChartDataStorage Interface

Saves chart data to external workbooks.

Declaration

public interface IChartDataStorage

Remarks

The API embeds chart data workbooks in a saved presentation. Implement the IChartDataStorage interface to save these workbooks to external storage (for example, a file system or a database) and link charts to the saved workbooks. Pass the implementation to the SaveOptions.setChartDataStorage(value) method.

IChartDataStorage is a functional interface. You can pass a lambda expression to the setChartDataStorage(value) method.

To load linked workbooks when the API loads a presentation, implement the IChartDataProvider interface.

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

Example: Save Chart Data to External Workbooks and Load the Workbooks

The following code snippet saves chart data workbooks to separate files, links charts to these files, and loads the workbooks when it opens the saved presentation:

import java.io.*;
import java.nio.channels.Channels;
import java.nio.file.*;
import java.util.List;
import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Path outputFolder = Files.createDirectories(Path.of("output")).toAbsolutePath();

        try (Presentation presentation = new Presentation()) {
            // Add a blank slide with a bar chart.
            presentation.getSlides().clear();
            Slide slide = new Slide(SlideLayoutType.BLANK);
            presentation.getSlides().add(slide);

            Chart chart = new Chart();
            chart.setName("Sales Chart");
            chart.setWidth(600);
            chart.setHeight(400);
            slide.getShapes().add(chart);

            BarSeries series = new BarSeries();
            series.setText("Sales");
            series.setArguments(new ChartStringData(List.of("Q1", "Q2", "Q3", "Q4")));
            series.setValues(new ChartNumericData(List.of(120.0, 150.0, 170.0, 210.0)));
            chart.getSeries().add(series);

            // Save each chart data workbook to a file and link the chart to the file.
            SaveOptions saveOptions = new SaveOptions();
            saveOptions.setChartDataStorage((context, dataChannel) -> {
                Path workbook = outputFolder.resolve(context.getChartName() + ".xlsx");
                try (InputStream data = Channels.newInputStream(dataChannel)) {
                    Files.copy(data, workbook, StandardCopyOption.REPLACE_EXISTING);
                }
                return workbook.toUri();
            });
            try (OutputStream output = Files.newOutputStream(outputFolder.resolve("Document.pptx"))) {
                presentation.save(output, saveOptions);
            }
        }

        // Load linked chart data workbooks from files.
        LoadOptions loadOptions = new LoadOptions();
        loadOptions.setChartDataProvider(dataUri -> Files.newByteChannel(Path.of(dataUri)));
        try (InputStream input = Files.newInputStream(outputFolder.resolve("Document.pptx"));
             Presentation presentation = new Presentation(input, loadOptions)) {
            Chart chart = (Chart) presentation.getSlides().get(0).getShapes().get(0);
            System.out.println(chart.getName() + ": " + chart.getSeries().size() + " series");
        }
    }
}

Methods

save(ChartDataContext context, ReadableByteChannel dataChannel) Method

Saves a chart data workbook and returns the workbook URI.

Declaration

public abstract URI save(ChartDataContext context, ReadableByteChannel dataChannel)

Parameters

Name Type Description
context ChartDataContext

Information about the chart (the chart name and the URI of the linked workbook).

dataChannel ReadableByteChannel

A channel from which you can read the workbook content.

Returns

Type Description
URI

The URI of the saved workbook, or null to embed the workbook in the presentation.

Exceptions

Type
IOException

Remarks

The API calls the save method for each chart that has a data workbook.

If the save method returns a URI, the saved presentation links the chart to the workbook at this URI. The API writes the URI to the presentation without changes. You can return an absolute or relative URI.

If the save method returns null, the API embeds the workbook in the presentation. The API embeds the workbook even if you close the dataChannel channel.