JavaScript ChartsCreate/Update

Version 14.2.0

Learn about creating and updating charts in more detail.

Creating and Updating Charts Copy Link

AgCharts exposes a static create() method to perform chart initialisation, and the resulting AgChartInstance has methods such as AgChartInstance.update() to allow updating configuration.

The AgChartOptions type defines the configuration structure. See the Options Reference for more details.

Mutations to the previously used options object are not automatically picked up by the chart implementation. AgChartInstance.update() or AgChartInstance.updateDelta() should be called to apply changes.

We expect the options supplied to AgChartInstance.update() to be the full configuration state for the chart, not a partial configuration. Use AgChartInstance.updateDelta() to apply partial updates.

We expect immutable data for data elements and theme options, as this enables efficient change detection. If data elements or theme options are mutated in-place, we cannot guarantee to detect the changes.

create Function
Create a new `AgChartInstance` based upon the given configuration options.
createFinancialChart Function
Create a new `AgChartInstance` based upon the given configuration options.
createGauge Function
Create a new `AgChartInstance` based upon the given configuration options.
createQuadrantChart Function
Create a new `AgChartInstance` based upon the given configuration options.
update Function
Update an existing `AgChartInstance`. Options provided should be complete and not partial. Returns a `Promise` that resolves once the requested change has been rendered. __Note:__ As each call could trigger a chart redraw, multiple calls in quick succession could result in undesirable flickering. Callers should batch up and/or debounce changes to avoid unintended partial update renderings.
updateDelta Function
Update an existing `AgChartInstance` by applying a partial set of option changes. Returns a `Promise` that resolves once the requested change has been rendered. __Note:__ As each call could trigger a chart redraw, each individual delta options update should leave the chart in a valid options state. Also, multiple calls in quick succession could result in undesirable flickering. Callers should batch up and/or debounce changes to avoid unintended partial update renderings.
getOptions Function
Get the `AgChartOptions` representing the current chart configuration.
applyTransaction Function
Apply a transaction to incrementally update the chart data without replacing the entire dataset. Returns a `Promise` that resolves once the transaction has been applied and rendered
waitForUpdate Function
Returns a `Promise` that resolves once any pending changes have been rendered.
download Function
Starts a browser-based image download for the given `AgChartInstance`. Returns a `Promise` that resolves once the download has been initiated.
getImageDataURL Function
Returns a base64-encoded image data URL for the given `AgChartInstance`.
getState Function
Returns a representation of the current state of the given `AgChartInstance`.
setState Function
Sets the state of the given `AgChartInstance` to the state provided.
getSelection Function
Retrieve the current selection. An error may be thrown if the chart state mutates whilst the selection items are being iterated. Returns An iterable of all selected items.
setSelection Function
Replaces the current selection.
clearSelection Function
Clear the entire selection state of all items on all series.
isModuleRegistered Function
Returns whether a module is available to this chart, either registered globally or passed to it via `AgCharts.create(options, { modules })`. Param moduleId the exported name of a module, such as `'LineSeriesModule'`. Bundles such as `AllCommunityModule` are not modules; `'QuadrantChartModule'` is accepted because the quadrant preset is only exported as a bundle.
destroy Function
Destroy the chart instance and any allocated resources supporting its rendering.

The following example demonstrates both create and update cases:

  • Definition of an options object used to create the initial chart state.
  • Buttons that invoke mutations of the options and trigger update of the chart state.

Delta Options Update Copy Link

AgChartInstance exposes the updateDelta() method to allow partial updates to a charts options.

To assist with state management, the complete applied options state can be retrieved by calling the getOptions() method on the AgChartInstance.

When updating series or axes options, the complete array must be supplied with all the properties for each item.

updateDelta Function
Update an existing `AgChartInstance` by applying a partial set of option changes. Returns a `Promise` that resolves once the requested change has been rendered. __Note:__ As each call could trigger a chart redraw, each individual delta options update should leave the chart in a valid options state. Also, multiple calls in quick succession could result in undesirable flickering. Callers should batch up and/or debounce changes to avoid unintended partial update renderings.
getOptions Function
Get the `AgChartOptions` representing the current chart configuration.

The following example demonstrates:

  • Retrieving current Chart configuration via getOptions().
  • Mutation of the Chart configuration via updateDelta().

Waiting for Options Update Copy Link

Creation and updates happen asynchronously, but in some situations it may be useful to know when an update has been rendered.

To assist with this, AgChartInstance.update() and AgChartInstance.updateDelta() return Promises that resolve once rendering is complete.

Additionally AgChartsInstance.waitForUpdate() can be used after initial creation to understand when the first rendering of the newly created chart is complete.

Although rendering may be complete, browsers may not repaint until Javascript execution pauses.

Promises do not take animations into account, they resolve after the first rendering in an animation sequence.

This example demonstrates how these APIs can be used to continuously update a chart, with each update only being applied once the previous update has been rendered.

Destroying Charts Copy Link

Charts can be destroyed by using the AgChartInstance.destroy() method.

destroy Function
Destroy the chart instance and any allocated resources supporting its rendering.