Working with Charts

The new insertChart method was added into the DocumentBuilder class. So, let’s see how to insert a simple column chart into the document using insertChart method.

How to Insert a Chart from Scratch using Aspose.Words

In this section we will learn how to insert a chart into a document.

Insert Column Chart

The following code example shows how to insert column chart:

The code produces the following result:

column-chart-aspose-words-java-1

There are four different overloads for the series Add method, which were exposed to cover all possible variants of data sources for all chart types:

The code produces the following result:

column-chart-aspose-words-java-2

Insert Scatter Chart

The following code example shows how to insert a scatter chart:

The code produces the following result:

scutter-chart-aspose-words-java

Insert Area Chart

The following code example shows how to insert an area chart:

The code produces the following result:

area-chart-aspose-words-java

Insert Bubble Chart

The following code example shows how to insert a bubble chart:

The code produces the following result:

bubble-chart-aspose-words-java

Working with Charts through Shape.Chart Object

Once the chart was inserted and filled with data, you are able to change its appearance. Shape.Chart property contains all chart related options available through the public API.

For example, let’s change Chart title or legend behavior:

The code generates the followings results:

line-chart-aspose-words-java-1

How to Work with ChartSeriesCollection of Chart

Let’s look into ChartSeries collection. All chart series are available through the chart.getSeries() collection, which is Iterable:

You can remove series one by one or clear all of them as well as add a new one if needed. The newly inserted chart has some default series added to this collection. To remove them you need to call the chart.getSeries().clear() method.

Working with Single ChartSeries Class

Here is how to work with a particular series.

Please see the result below:

line-chart-aspose-words-java-2

All single ChartSeries have default ChartDataPoint options, please try using the following code to change them:

line-chart-aspose-words-java3

How to Work with Single ChartDataPoint of a ChartSeries

Using ChartDataPoint you are able to customize the formatting of a single data point of the chart series:

Please see the result below:

line-chart-aspose-words-java-4

How to Work with ChartDataLabel of a Single ChartSeries

Using ChartDataLabel you are able to specify the formatting of a single data label of the chart series, like show/hide LegendKey, CategoryName, SeriesName, Value etc:

Please see the result below:

bar-chart-aspose-words-java

How to Define Default Options for ChartDataLabels of ChartSeries

The ChartDataLabelCollection class defines properties which can be used to set default options for ChartDataLabels for Chart Series. These properties include setShowCategoryName, setShowBubbleSize, setShowPercentage, setShowSeriesName, setShowValue etc:

Please see the result below:

pie-chart-aspose-words-java

How to Format Number of Chart Data Label

Using the NumberFormat property, you can specify the number formatting of a single data label of the chart.

The following code example shows how to format a number of the data label:

How to Set Chart Axis Properties

If you want to work with chart axis, scaling, and display units for the value axis, please use ChartAxis, AxisDisplayUnit, and AxisScaling classes.

The following code example shows how to define X and Y-axis properties:

How to Set DateTime Value of Axis

The following code example shows how to set date/time values to axis properties:

How to Format Number Value of Axis

The following code example shows how to change the format of numbers on the value axis:

How to Set Bounds of Axis

The AxisBound class represents a minimum or maximum bound of axis values. Bound can be specified as a numeric, date-time or a special “auto” value.

The following code example shows how to set the bounds of an axis:

How to Set Interval Unit Between Labels

The following code example shows how to set the interval unit between labels on an axis:

How to Hide Chart Axis

If you want to show or hide the chart axis, you can simply achieve this by setting the value of ChartAxis.Hidden property.

The following code example shows how to hide the Y-axis of the chart:

How to Align Chart Label

If you want to set a text alignment for multi-line labels, you can simply achieve this by setting the value of setTickLabelAlignment() property.

The following code example shows how to tick label alignment:

How to Set Fill and Stroke Formatting

Fill and stroke formatting can be set for chart series, data points, and markers. To do this, you need to use the properties of the ChartFormat type in the ChartSeries, ChartDataPoint, and ChartMarker classes, as well as aliases for some properties, such as ForeColor, BackColor, Visible, and Transparency in the Stroke class.

The following code example shows how to set series color:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape shape = builder.InsertChart(ChartType.Column, 432, 252);

Chart chart = shape.Chart;
ChartSeriesCollection seriesColl = chart.Series;

// Delete default generated series.
seriesColl.Clear();

// Create category names array.
string[] categories = new string[] { "AW Category 1", "AW Category 2" };

// Adding new series. Value and category arrays must be the same size.
ChartSeries series1 = seriesColl.Add("AW Series 1", categories, new double[] { 1, 2 });
ChartSeries series2 = seriesColl.Add("AW Series 2", categories, new double[] { 3, 4 });
ChartSeries series3 = seriesColl.Add("AW Series 3", categories, new double[] { 5, 6 });

// Set series color.
series1.Format.Fill.ForeColor = Color.Red;
series2.Format.Fill.ForeColor = Color.Yellow;
series3.Format.Fill.ForeColor = Color.Blue;

doc.Save("ColumnColor.docx");

The following code example shows how to set line color and weight:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Shape shape = builder.InsertChart(ChartType.Line, 432, 252);

Chart chart = shape.Chart;
ChartSeriesCollection seriesColl = chart.Series;

// Delete default generated series.
seriesColl.Clear();

// Adding new series.
ChartSeries series1 = seriesColl.Add("AW Series 1", new double[] { 0.7, 1.8, 2.6 }, new double[] { 2.7, 3.2, 0.8 });
ChartSeries series2 = seriesColl.Add("AW Series 2", new double[] { 0.5, 1.5, 2.5 }, new double[] { 3, 1, 2 });

// Set series color.
series1.Format.Stroke.ForeColor = Color.Red;
series1.Format.Stroke.Weight = 5;
series2.Format.Stroke.ForeColor = Color.LightGreen;
series2.Format.Stroke.Weight = 5;

doc.Save("LineColorAndWeight.docx");