Creating and Customizing Charts

Creating Charts

It is possible to add a variety of charts to spreadsheets with Aspose.Cells. Aspose.Cells provides many flexible charting objects. This topic discusses Aspose.Cells’ charting objects.

Things to Know Before Creating a Chart

Before creating charts it’s important to understand some basic concepts that are helpful when creating charts using Aspose.Cells.

Charting Objects

Aspose.Cells provides a special set of classes used to create all kinds of charts. These classes are used to create charting objects, which act as the chart building blocks. The charting objects are listed below:

Using Charting Objects

As mentioned above, all charting objects are instances of their respective classes and provide specific properties and methods to perform specific tasks. Use charting objects to create charts.

Add any type of chart to a worksheet using the ChartCollection collection. Each item in the ChartCollection collection represents a Chart object. A Chart object encapsulates all Charting Objects required to customize the appearance of the chart. The next section shows how to use a few basic charting objects to create a simple chart.

Creating a Simple Chart

It is possible to create many different types of charts with Aspose.Cells. All standard charts supported by Aspose.Cells are pre-defined in an enumeration named ChartType. The pre-defined chart types are:

Chart TypesDescription
ColumnRepresents the Clustered Column Chart
ColumnStackedRepresents Stacked Column Chart
Column100PercentStackedRepresents 100% Stacked Column Chart
Column3DClusteredRepresents 3D Clustered Column Chart
Column3DStackedRepresents 3D Stacked Column Chart
Column3D100PercentStackedRepresents 3D 100% Stacked Column Chart
Column3DRepresents 3D Column Chart
BarRepresents Clustered Bar Chart
BarStackedRepresents Stacked Bar Chart
Bar100PercentStackedRepresents 100% Stacked Bar Chart
Bar3DClusteredRepresents 3D Clustered Bar Chart
Bar3DStackedRepresents 3D Stacked Bar Chart
Bar3D100PercentStackedRepresents 3D 100% Stacked Bar Chart
LineRepresents Line Chart
LineStackedRepresents Stacked Line Chart
Line100PercentStackedRepresents 100% Stacked Line Chart
LineWithDataMarkersRepresents Line Chart with data markers
LineStackedWithDataMarkersRepresents Stacked Line Chart with data markers
Line100PercentStackedWithDataMarkersRepresents 100% Stacked Line Chart with data markers
Line3DRepresents 3D Line Chart
PieRepresents Pie Chart
Pie3DRepresents 3D Pie Chart
PiePieRepresents Pie of Pie Chart
PieExplodedRepresents Exploded Pie Chart
Pie3DExplodedRepresents 3D Exploded Pie Chart
PieBarRepresents Bar of Pie Chart
ScatterRepresents the Scatter Chart
ScatterConnectedByCurvesWithDataMarkerRepresents the Scatter Chart connected by curves, with data markers
ScatterConnectedByCurvesWithoutDataMarkerRepresents the Scatter Chart connected by curves, without data markers
ScatterConnectedByLinesWithDataMarkerRepresents the Scatter Chart connected by lines, with data markers
ScatterConnectedByLinesWithoutDataMarkerRepresents the Scatter Chart connected by lines, without data markers
AreaRepresents Area Chart
AreaStackedRepresents Stacked Area Chart
Area100PercentStackedRepresents 100% Stacked Area Chart
Area3DRepresents 3D Area Chart
Area3DStackedRepresents 3D Stacked Area Chart
Area3D100PercentStackedRepresents 3D 100% Stacked Area Chart
DoughnutRepresents Doughnut Chart
DoughnutExplodedRepresents Exploded Doughnut Chart
RadarRepresents the Radar Chart
RadarWithDataMarkersRepresents the Radar Chart with data markers
RadarFilledRepresents Filled Radar Chart
Surface3DRepresents 3D Surface Chart
SurfaceWireframe3DRepresents the Wireframe 3D Surface Chart
SurfaceContourRepresents Contour Chart
SurfaceContourWireframeRepresents Wireframe Contour Chart
BubbleRepresents Bubble Chart
Bubble3DRepresents 3D Bubble Chart
CylinderRepresents Cylinder Chart
CylinderStackedRepresents Stacked Cylinder Chart
Cylinder100PercentStackedRepresents 100% Stacked Cylinder Chart
CylindricalBarRepresents Cylindrical Bar Chart.
CylindricalBarStackedRepresents Stacked Cylindrical Bar Chart
CylindricalBar100PercentStackedRepresents 100% Stacked Cylindrical Bar Chart
CylindricalColumn3DRepresents 3D Cylindrical Column Chart
ConeRepresents Cone Chart
ConeStackedRepresents Stacked Cone Chart
Cone100PercentStackedRepresents 100% Stacked Cone Chart
ConicalBarRepresents Conical Bar Chart
ConicalBarStackedRepresents Stacked Conical Bar Chart
ConicalBar100PercentStackedRepresents 100% Stacked Conical Bar Chart
ConicalColumn3DRepresents 3D Conical Column Chart
PyramidRepresents Pyramid Chart
PyramidStackedRepresents Stacked Pyramid Chart
Pyramid100PercentStackedRepresents 100% Stacked Pyramid Chart
PyramidBarRepresents the Pyramid Bar Chart
PyramidBarStackedRepresents Stacked Pyramid Bar Chart
PyramidBar100PercentStackedRepresents 100% Stacked Pyramid Bar Chart
PyramidColumn3DRepresents 3D Pyramid Column Chart
To create a chart using Aspose.Cells:
  1. Add some data to worksheet cells with the Cell object’s setValue method. This will be used as the data source for the chart.
  2. Add a chart to the worksheet by calling the ChartCollection collection’s add method, encapsulated in the Worksheet object.
  3. Specify the type of chart with the ChartType enumeration. For example, the example uses the ChartType.PYRAMID value as the chart type.
  4. Access the new Chart object from the ChartCollection collection by passing its index.
  5. Use any of the charting objects encapsulated in the Chart object to manage the chart. The example below uses the SeriesCollection charting object to specify the chart’s data source.

When adding source data to chart, the data source can be a range of cells (such as “A1:C3”), or a sequence of non-contiguous cells (such as “A1, A3, A5”), or a sequence of values (such as “1,2,3”).

These general steps allow you to create any type of chart. Use different charting objects to create different charts.

When the example code is executed, a pyramid chart is added to the worksheet as shown below.

Pyramid chart with its data source

todo:image_alt_text

To create a bubble chart, the ChartType has to be set to ChartType.BUBBLE and few extra properties such as BubbleSizes, Values & XValues need to be set accordingly. Upon executing the following code, a bubble chart is added to the worksheet as shown below.

Bubble chart with its data source

todo:image_alt_text

Line with Data Marker Chart

To create a line with a data marker chart, the ChartType has to be set to ChartType.LINE_WITH_DATA_MARKERS and few extra properties such as background area,Series Markers , Values & XValues need to be set accordingly. Upon executing the following code, a line with a data marker chart is added to the worksheet.

Creating Custom Charts

So far, when we’ve discussed charts, we’ve looked at standard charts that have their standard formatting settings. We only define the data source, set a few properties and the chart is created with its default format settings. But Aspose.Cells also supports creating custom charts that allows developers to create charts with their own format settings.

Creating Custom Charts

Developers can create custom charts at runtime using Aspose.Cells simple API.

A chart is composed of a data series. Each data series in Aspose.Cells is represented by a Series object whereas the SeriesCollection object serves as a collection of Series objects. When creating a custom chart, developers have the freedom to use different types of charts for different data series (collected in a SeriesCollection object).

The example code below demonstrates how to create custom charts. In this example, we are going to use a column chart for the first data series and a line chart for the second series. The result is that we add a column chart, combined with a line chart, to the worksheet.

Custom chart combining column and line charts

todo:image_alt_text

Programming Example