Creating and Customizing Charts

Creating Charts

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 in the Aspose.Cells.Charts namespace used to create the charts supported by Aspose.Cells. These classes are used to create charting objects, which act as the chart building blocks. The charting objects are listed below:

  • Series, a single data series in a chart.
  • Axis, a chart’s axis.
  • Chart, a single Excel chart.
  • ChartArea, the chart area in the worksheet.
  • ChartDataTable, a chart data table.
  • ChartFrame, the frame object in a chart.
  • ChartPoint, a single point in a series in a chart.
  • ChartPointCollection, a collection that contains all the points in one series.
  • Charts, a collection of Chart objects.
  • DataLabels, a collection of all the DataLabel objects for the specified series.
  • FillFormat, fill format for a shape.
  • Floor, the floor of a 3D chart.
  • Legend, the chart legend.
  • Line, the chart line.
  • SeriesCollection, a collection of Series objects.
  • TickLabels, the tick mark labels associated with tick marks on a chart axis.
  • Title, the title of a chart or axis.
  • Trendline, a trendline in a chart.
  • TrendlineCollection, a collection of all Trendline objects for the specified data series.
  • Walls, the walls of a 3D chart.

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 Charts collection. Each item in the Charts collection represents a Chart object. A Chart object encapsulates all other 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.

Create Chart Using Aspose.Cells

Steps:

  1. Add some data to worksheet cells with the Cell object’s PutValue method. This will be used as the data source for the chart.
  2. Add a chart to the worksheet by calling the Charts collection’s Add method, encapsulated in the Worksheet object.
  3. Specify the type of chart with the ChartType enumeration. For example, the example below uses the ChartType.Pyramid value as the chart type.
  4. Access the new Chart object from the Charts 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.

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 Aspose.Cells.Charts.ChartType.

The pre-defined chart types are:

Chart TypesDescription
ColumnRepresents 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 Scatter Chart
ScatterConnectedByCurvesWithDataMarkerRepresents Scatter Chart connected by curves, with data markers
ScatterConnectedByCurvesWithoutDataMarkerRepresents Scatter Chart connected by curves, without data markers
ScatterConnectedByLinesWithDataMarkerRepresents Scatter Chart connected by lines, with data markers
ScatterConnectedByLinesWithoutDataMarkerRepresents 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 Radar Chart
RadarWithDataMarkersRepresents Radar Chart with data markers
RadarFilledRepresents Filled Radar Chart
Surface3DRepresents 3D Surface Chart
SurfaceWireframe3DRepresents 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
CylindericalBarRepresents Cylindrical Bar Chart.
CylindericalBarStackedRepresents Stacked Cylindrical Bar Chart
CylindericalBar100PercentStackedRepresents 100% Stacked Cylindrical Bar Chart
CylindericalColumn3DRepresents 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 Pyramid Bar Chart
PyramidBarStackedRepresents Stacked Pyramid Bar Chart
PyramidBar100PercentStackedRepresents 100% Stacked Pyramid Bar Chart
PyramidColumn3DRepresents 3D Pyramid Column Chart

Pyramid Chart

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

Line Chart

In the above example, simply changing the ChartType to Line creates a line chart. The complete source is provided below. when the code is executed, a line chart is added to the worksheet.

Bubble Chart

In order 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.

Line with Data Marker Chart

In order to create a line with the data marker chart, ChartType has to be set to ChartType.LineWithDataMarkers 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 the 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 APIs also supports creating custom charts that allows developers to create charts with their own format settings.

Developers can create custom charts at run-time using Aspose.Cells.

A chart is composed of a data series. Each data series in Aspose.Cells is represented by a Series object whereas 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 the 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.