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

RequirementDetail
Java8 or later
DependencyThe package from Installation
Importimport com.aspose.words.*;
CodeThe 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: Revenue

Tips and Best Practices

  • Use ChartSeriesCollection.add with an array of category names and an array of values for one series.
  • Shape.hasChart tells 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

IssueCauseFix
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 / MethodDescription
ChartProvides access to the chart shape properties.
ChartSeriesCollectionRepresents collection of a ChartSeries.
ChartTypeSpecifies 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

See Also