Chart API

In addition to users creating their own charts from the grid, charts can also be generated by using the Chart API.

Range Charts

Charts can be created programmatically from a range via the grid's createRangeChart() API. The interface is as follows:

function createRangeChart(params: CreateRangeChartParams): ChartRef | undefined;

createRangeChart params

params: { cellRange?: CellRangeParams; chartType?: ChartType; chartThemeName?: string; chartContainer?: HTMLElement; suppressChartRanges?: boolean; // default: false aggFunc?: Function; chartThemeOverrides?: AgChartThemeOverrides; }
cellRangeDefines the range of cells to be charted. A range is normally defined with start and end rows and a list of columns. If the start and end rows are omitted, the range covers all rows (i.e. entire columns are selected). The columns can either be defined using a start and end column (the range will cover the start and end columns and all columns in between), or columns can be supplied specifically in cases where the required columns are not adjacent to each other. See Add Cell Range for more information. See cellRange for more details about this configuration object.
chartTypeThe type of chart to create.
Options: 'groupedColumn', 'stackedColumn', 'normalizedColumn', 'groupedBar', 'stackedBar', 'normalizedBar', 'line', 'scatter', 'bubble', 'pie', 'doughnut', 'area', 'stackedArea', 'normalizedArea'
chartThemeNameThe default theme to use for the created chart. In addition to the default options you listed, you can also provide your own custom chart themes.
Options: 'ag-default', 'ag-default-dark', 'ag-material', 'ag-material-dark', 'ag-pastel', 'ag-pastel-dark', 'ag-vivid', 'ag-vivid-dark', 'ag-solar', 'ag-solar-dark'
chartContainerIf the chart is to be displayed outside of the grid then a chart container should be provided. If the chart is to be displayed using the grid's popup window mechanism then leave as undefined.
suppressChartRangesBy default, when a chart is displayed using the grid, the grid will highlight the range the chart is charting when the chart gets focus. To suppress this behaviour, set suppressChartRanges=true.
Default: false
aggFuncThe aggregation function that should be applied to all series data. The built-in aggregation functions are 'sum', 'min', 'max', 'count', 'avg', 'first', 'last'. Alternatively, custom aggregation functions can be provided if they conform to the IAggFunc interface shown here.function (params: IParams): any; interface IParams { values: any[]; column: Column; colDef: ColDef; rowNode: RowNode; data: any; api: GridApi; columnApi: ColumnApi; context: any; }
chartThemeOverridesAllows specific chart options in the current theme to be overridden. See Overriding Existing Themes.

cellRange

params > cellRange

Defines the range of cells to be charted. A range is normally defined with start and end rows and a list of columns. If the start and end rows are omitted, the range covers all rows (i.e. entire columns are selected). The columns can either be defined using a start and end column (the range will cover the start and end columns and all columns in between), or columns can be supplied specifically in cases where the required columns are not adjacent to each other. See Add Cell Range for more information.

params: { ... cellRange: { rowStartIndex?: number; rowStartPinned?: string; rowEndIndex?: number; rowEndPinned?: string; columnStart?: string | Column; columnEnd?: string | Column; columns?: (string | Column)[]; } }
rowStartIndexThe index of the first row in the range.
rowStartPinnedSets whether the first row is pinned.
rowEndIndexThe index of the last row in the range.
rowEndPinnedSets whether the last row is pinned.
columnStartThe first column of the range.
columnEndThe last column of the range.
columnsThe columns in the range. Used instead of columnStart and columnEnd, particularly when you want non-sequential columns.

The API returns a ChartRef object when a chartContainer is provided. This is the same structure that is provided to the createChartContainer() callback. The ChartRef provides the application with the destroyChart() method that is required when the application wants to dispose the chart.

Example: Charts in Grid Popup Window

This example shows how charts can be created in the grid's provided popup window. The following can be noted:

  • Clicking 'Gold & Silver, 5 Rows' will chart the first five rows of Gold and Silver by Country.
  • Clicking 'Bronze, All Rows' will chart Bronze by Country using all rows (the provided cell range does not specify rows).

Example: Charts in Dashboard

This example passes a chartContainer to the API to place the chart in a location other than the grid's popup window. The following can be noted:

  • The charts are placed in div elements outside of the grid.
  • The two pie charts are showing aggregations rather than charting individual rows.
  • Clicking on a chart highlights the range in the grid for which the chart is based.
  • The bar chart is sensitive to changes in the rows. For example if you sort, the chart updates to always chart the first five rows.
  • All data is editable in the grid. Changes to the grid data is reflected in the charts.
  • The two pie charts have legends beneath. This is configured in the chartThemeOverrides.

Pivot Charts

You can also use the API to create a pivot chart. There are fewer parameters available as the pivot chart is always generated from all data in the grid:

function createPivotChart(params: CreatePivotChartParams): ChartRef | undefined; interface CreatePivotChartParams { chartType: ChartType; chartThemeName?: string; chartContainer?: HTMLElement; chartThemeOverrides?: AgChartTheme; }

The attributes have the same behaviour as described earlier.

Example: Pivot Chart

This is an example showing the pivot chart API in action, using a specified chart container.

Saving and Restoring Charts

You can access models that represent rendered charts using the getChartModels() API. The models are returned in a format that can be easily used with the other API methods to later recreate the chart. You can also retrieve a base64 encoded image rendered from the chart in the model.

function getChartModels(): ChartModel[]; interface ChartModel { chartId: string; cellRange: CellRangeParams; chartType: ChartType; chartThemeName: string; chartOptions: ChartOptions; chart: any; getChartImageDataURL: (params: GetChartImageDataUrlParams) => string; } interface GetChartImageDataUrlParams { type?: string; }

Example: Saving and Restoring Charts

The example below demonstrates how you can save and then later restore a chart. You can make changes to the chart type, theme, data and formatting options and note how the restored chart looks the same as the chart that was saved.

It also shows how you can retrieve images rendered from the chart in multiple formats.

  • Create a range chart from the grid, which will be shown in a container below the grid.
  • Change the chart type, theme, data and/or formatting in order to see the changes restored later.
  • Click "Save chart" to persist a model of the visible chart into a local variable. An alert will be shown to confirm that this has happened.
  • Click "Clear chart" to destroy the existing chart.
  • Click "Restore chart" to restore the previously saved chart.
  • Click "Open PNG" to open a PNG format image of the chart in a new window.
  • Click "Open JPG" to open a JPG format image of the chart in a new window.

Next Up

Continue to the next section to learn about: Chart Events.