Charts
Overview
This guide shows how to insert a chart into a Word document and read it back with Aspose.Words FOSS for Java. A chart in a Word document is a Shape that has a Chart. DocumentBuilder.insertChart inserts a chart of a given ChartType at a size in points and returns the shape; Shape.getChart gives access to the chart properties, Chart.getSeries to its ChartSeriesCollection and Chart.getTitle to its title. The example inserts a column chart, replaces its sample data with one series of three quarters, sets a title, saves the document and reads the chart back. It was compiled and run against the published package on Java 8, 17 and 21.
Prerequisites
| Requirement | Detail |
|---|---|
| Java | 8 or later |
| Dependency | The package from Installation |
| Import | import com.aspose.words.*; |
| Code | The examples are statements. Put them in a method that declares throws Exception. |
Insert a Column Chart With One Series
insertChart(ChartType.COLUMN, 432, 252) inserts the chart. getSeries().clear() removes the sample series, add(name, categories, values) adds a series and getTitle().setText sets the title. After saving and reloading, the chart was found by checking Shape.hasChart: its title was Revenue by quarter and it had 1 series named Revenue.
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252);
Chart chart = shape.getChart();
chart.getSeries().clear();
chart.getSeries().add("Revenue", new String[] {"Q1", "Q2", "Q3"}, new double[] {120, 150, 180});
chart.getTitle().setText("Revenue by quarter");
doc.save("chart.docx");
Document reloaded = new Document("chart.docx");
Shape saved = null;
NodeCollection shapes = reloaded.getChildNodes(NodeType.SHAPE, true);
for (int i = 0; i < shapes.getCount(); i++) {
Shape candidate = (Shape) shapes.get(i);
if (candidate.hasChart()) {
saved = candidate;
}
}
Chart savedChart = saved.getChart();
System.out.println("Chart title: " + savedChart.getTitle().getText());
System.out.println("Series: " + savedChart.getSeries().getCount());
System.out.println("Series name: " + savedChart.getSeries().get(0).getName());The program printed:
Chart title: Revenue by quarter
Series: 1
Series name: RevenueTips and Best Practices
- Use
ChartSeriesCollection.addwith an array of category names and an array of values for one series. Shape.hasCharttells a chart shape from other shapes when you loop over the shapes of a document.- Chart rendering is not part of this edition: saving to PDF, PNG, SVG or TIFF is rejected, so a chart cannot be turned into an image with this library.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| The compiler reports cannot find symbol for Document. | The com.aspose.words package is not imported. | Add import com.aspose.words.*;. |
| The compiler reports an unreported exception. | The Document constructors, Document.save and some other methods declare throws Exception. | Declare it on the calling method or catch it. |
Chart.setStyle throws a NullPointerException. | In a test on Java 8, 17 and 21 setStyle threw on a chart created with insertChart. | Do not call setStyle on such a chart. |
FAQ
Which chart types exist?
ChartType lists them; the example uses ChartType.COLUMN, a column chart.
Can I turn a chart into an image?
No. Saving to the image and fixed-page formats is rejected with an UnsupportedOperationException whose message starts with FOSS.
API Reference Summary
| Class / Method | Description |
|---|---|
Chart | Provides access to the chart shape properties. |
ChartSeriesCollection | Represents collection of a ChartSeries. |
ChartType | Specifies type of a chart. |
DocumentBuilder.insertChart(type, width, height) | Inserts a chart object into the document and scales it to the specified size. |
Shape.hasChart() | Returns true if this Shape has a Chart. |
Related Guides
- Working with Tables
- Lists
- Working with Documents
- How to Insert a Chart Into a Word Document in Java (knowledge base)