IChartDataProvider Interface
Loads external workbooks that contain data for presentation charts.
Declaration
public interface IChartDataProvider
Remarks
A chart in a PowerPoint presentation can link to an external workbook that contains chart data. Implement the IChartDataProvider interface to load linked workbooks when the API loads a presentation. Pass the implementation to the LoadOptions.setChartDataProvider(value) method.
IChartDataProvider is a functional interface. You can pass a lambda expression to the setChartDataProvider(value) method.
To save chart data to external workbooks, implement the IChartDataStorage 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
load(URI dataUri) Method
Loads the external workbook at the specified URI.
Declaration
public abstract ReadableByteChannel load(URI dataUri)
Parameters
| Name | Type | Description |
|---|---|---|
| dataUri | URI | The URI of the external workbook that contains chart data. |
Returns
| Type | Description |
|---|---|
| ReadableByteChannel | A channel from which the API reads the workbook content, or |
Exceptions
| Type |
|---|
| IOException |
Remarks
The API calls the load method for each chart that links to an external workbook if the presentation does not contain an embedded copy of the chart data workbook. The dataUri value is the link target stored in the presentation. The link target can be an absolute or relative URI.
The API reads the returned channel to the end and closes the channel.
If the load method returns null, the API does not load the workbook.