officefileapi-120427-spreadsheet-document-api-charts-and-graphics-charting.md
The Spreadsheet Document API allows you to create charts. Use charts to analyze large amounts of data and show relationships between different data sets.
The Spreadsheet Document API supports the following chart types:
|
Chart Type
|
Chart Subtypes
| | --- | --- | |
Column Charts
|
2-D Column Charts:
3-D Column Charts:
Cylinder Charts:
Cone Charts:
Pyramid Charts:
| |
Line Charts
|
2-D Line Charts:
3-D Line Charts:
| |
Pie Charts
|
2-D Pie Charts:
3-D Pie Charts:
| |
Doughnut Charts
|
| |
Bar Charts
|
2-D Bar Charts:
3-D Bar Charts:
Cylinder Charts:
Cone Charts:
Pyramid Charts:
| |
Area Charts
|
2-D Area Charts:
3-D Area Charts:
| |
Scatter Charts
|
| |
Bubble Charts
|
| |
Stock Charts
|
| |
Radar Charts
|
| |
Excel 2016 Charts
|
|
The Spreadsheet Document API also provides limited support for the charts listed below. You can load/save these charts, create and modify them in the API, but you cannot print or export these charts to PDF.
The following image displays chart elements. You can add or remove these elements to customize a chart layout.
When you create a chart, the Spreadsheet plots data on the chart based on the number of rows and columns in the source range:
If the source range has more rows than columns, the first column provides labels for the category axis (X-axis), and the first row contains series names. Series values are arranged in columns.
If the source range has more columns than rows or the number of rows and columns is the same, the first row provides labels for the category axis (X-axis), and the first column contains series names. Series values are arranged in rows.
You can use the ChartObject.SwitchRowColumn method to change the default direction used to plot data series on a chart (whether the series values are obtained from columns or rows), or call the ChartObject.SelectData method with the direction parameter to specify how to retrieve series values.
The table below lists how to arrange data on a worksheet to create a specific chart.
|
Chart type
|
Data arrangement
| | --- | --- | |
Line, column, bar, area, surface, or radar chart
|
In columns or rows:
| |
Pie chart
|
Uses a single series of values arranged in one column or row. Another column or row contains data labels.
| |
Doughnut chart
|
In one or multiple columns or rows, and one column or row contains data labels.
| |
Scatter or bubble chart
|
In columns or rows. Place x values in the first column or row, and the corresponding y values in the adjacent columns or rows.
For a bubble chart, add columns or rows specifying the bubble size.
| |
Stock chart
|
In columns or rows in the same order as the stock chart name.
|
The chart collection (ChartCollection) stores charts in a worksheet. You can use the Worksheet.Charts property to access a worksheet’s chart collection, and the collection’s ChartCollection.Add method to create a chart. The ChartType enumeration value specifies the chart type.
Use one of the following approaches to specify the chart’s data:
Pass a cell range containing chart data to the ChartCollection.Add method. Refer to the following section for information on how to organize data in the source range to create a specific chart: Arrange Worksheet Data to Create a Chart.
// Create a column chart using a cell range as a data source.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:F6"]);
' Create a column chart using a cell range as a data source.
Dim chart As Chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet("B2:F6"))
Use the following methods to import and apply chart settings from Chart Template files (.CRTX):
ChartCollection.AddFromTemplate(Stream)Loads a chart template from the stream and adds the chart to the collection.ChartCollection.AddFromTemplate(Stream, CellRange)Loads a chart template from the stream, binds it to data from the specified range, and adds the chart to the collection.
Pass a cell range containing chart data to the ChartObject.SelectData method. This method also allows you to specify the data direction: whether the series values are arranged in columns (ChartDataDirection.Column) or rows (ChartDataDirection.Row).
// Create a column chart.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered);
// Select data for the chart and specify the data direction.
chart.SelectData(worksheet["B2:F6"], ChartDataDirection.Row);
' Create a column chart.
Dim chart As Chart = worksheet.Charts.Add(ChartType.ColumnClustered)
' Select data for the chart and specify the data direction.
chart.SelectData(worksheet("B2:F6"), ChartDataDirection.Row)
Use the SeriesCollection.Add method overloads to add data series to the chart’s series collection (ChartObject.Series). These methods enable you to plot data contained in the noncontiguous cell ranges. You can explicitly specify a name, arguments, and values for each series.
// Create a column chart.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered);
// Add two data series to the chart.
chart.Series.Add(worksheet["D2"], worksheet["B3:B6"], worksheet["D3:D6"]);
chart.Series.Add(worksheet["F2"], worksheet["B3:B6"], worksheet["F3:F6"]);
' Create a column chart.
Dim chart As Chart = worksheet.Charts.Add(ChartType.ColumnClustered)
' Add two data series to the chart.
chart.Series.Add(worksheet("D2"), worksheet("B3:B6"), worksheet("D3:D6"))
chart.Series.Add(worksheet("F2"), worksheet("B3:B6"), worksheet("F3:F6"))
The ChartData object specifies data plotted on a chart. You can also use an array of numeric, string or Boolean literals as the chart’s data source. ChartData supports implicit conversion from an array of CellValue objects. You can use these objects to specify numeric, string or Boolean values without casting. Use the ChartData.FromArray method to convert the CellValue type arrays to chart data, or assign these arrays to the series’s arguments and values directly.
// Create a column chart.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered);
// Add a series bound to a set of literal data.
Series literalSeries = chart.Series.Add(
new CellValue[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun" },
new CellValue[] { 50, 100, 30, 104, 87, 150 });
' Create a column chart.
Dim chart As Chart = worksheet.Charts.Add(ChartType.ColumnClustered)
' Add a series bound to a set of literal data.
Dim literalSeries As Series = chart.Series.Add(
New CellValue() { "Jan", "Feb", "Mar", "Apr", "May", "Jun" },
New CellValue() { 50, 100, 30, 104, 87, 150 })
The following topics contain more examples on how to create charts in code:
Each chart has an associated ChartView. This object is created automatically when you create a chart. It consolidates data series of the same type (ChartView.Series) and contains customization options. A ChartView object is a member of the Chart.Views collection. A chart can have several ChartView objects (a combination chart). To create a combination chart, use the Series.ChangeType method to change an existing series’s type. If the specified type is incompatible with the other series types, the Spreadsheet clears ChartViewCollection and creates a new ChartView object to display all chart series. This object has the same type as the modified data series. The following topic contains a list of compatible chart types and describes how to create a combination chart: How to: Create and Modify a Chart.
After you create a chart, you can use the Spreadsheet Chart API members to change the chart’s layout and appearance.
The table below lists the actions you can perform on a chart.
|
To execute this task
|
Use these API members
|
Example
| | --- | --- | --- | |
Change the source data for a chart
|
|
How to: Create and Modify a Chart
| |
Add or remove data series
|
|
How to: Create and Modify a Chart
| |
Change the data direction on a chart
|
|
How to: Create and Modify a Chart
| |
Change the data series’ order
|
|
How to: Create and Modify a Chart
| |
Change a chart type
|
|
How to: Create and Modify a Chart
| |
Change a series type
|
|
How to: Create and Modify a Chart
| |
Create a chart on a separate sheet ( chart sheet ),
or move an existing chart to a new chart sheet
|
|
How to: Move a Chart to a Chart Sheet
| |
Display or hide primary axes ,
adjust their position and orientation
|
|
How to: Change the Display of Chart Axes
| |
Add axis titles
|
|
How to: Change the Display of Chart Axes
| |
Change the axis scaling , and
format numbers on the axis
|
|
How to: Change the Display of Chart Axes
| |
Display secondary axes
|
|
How to: Change the Display of Chart Axes
| |
Display or hide gridlines
|
|
How to: Change the Display of Chart Axes
| |
Add a chart title
|
|
How to: Display the Chart Title
| |
Display or hide a chart legend , specify its position,
customize a legend entry
|
|
How to: Show or Hide the Chart Legend
| |
Display data labels for the entire chart
or individual series/data points
|
|
How to: Display and Format Data Labels
| |
Apply a number format to data labels
|
ChartView.DataLabels.NumberFormat
|
How to: Display and Format Data Labels
| |
Apply a chart style
|
|
| |
Fill a chart element
|
ShapeOutlineFill.SetGradientFill
|
How to: Change the Appearance of Series Points
| |
Format a chart element’s outline
|
|
| |
Format a chart element’s text
|
|
|
See Also
How to: Create Excel 2016 Charts in the Spreadsheet Document API