API

For each chart, there are a set of global prototype methods on the shared chart type which you may find useful. These are available on all charts created with Chart.js, but for the examples, let’s use a line chart we’ve made.

  1. // For example:
  2. var myLineChart = new Chart(ctx, config);

.destroy()

Use this to destroy any chart instances that are created. This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js. This must be called before the canvas is reused for a new chart.

  1. // Destroys a specific chart instance
  2. myLineChart.destroy();

.update(mode?)

Triggers an update of the chart. This can be safely called after updating the data object. This will update all scales, legends, and then re-render the chart.

  1. myLineChart.data.datasets[0].data[2] = 50; // Would update the first dataset's value of 'March' to be 50
  2. myLineChart.update(); // Calling update now animates the position of March from 90 to 50.

A mode string can be provided to indicate transition configuration should be used. Core calls this method using any of 'active', 'hide', 'reset', 'resize', 'show' or undefined. 'none' is also a supported mode for skipping animations for single update. Please see animations docs for more details.

Example:

  1. myChart.update('active');

See Updating Charts for more details.

.reset()

Reset the chart to its state before the initial animation. A new animation can then be triggered using update.

  1. myLineChart.reset();

.render()

Triggers a redraw of all chart elements. Note, this does not update elements for new data. Use .update() in that case.

.stop()

Use this to stop any current animation. This will pause the chart during any current animation frame. Call .render() to re-animate.

  1. // Stops the charts animation loop at its current frame
  2. myLineChart.stop();
  3. // => returns 'this' for chainability

.resize(width?, height?)

Use this to manually resize the canvas element. This is run each time the canvas container is resized, but you can call this method manually if you change the size of the canvas nodes container element.

You can call .resize() with no parameters to have the chart take the size of its container element, or you can pass explicit dimensions (e.g., for printing).

  1. // Resizes & redraws to fill its container element
  2. myLineChart.resize();
  3. // => returns 'this' for chainability
  4. // With an explicit size:
  5. myLineChart.resize(width, height);

.clear()

Will clear the chart canvas. Used extensively internally between animation frames, but you might find it useful.

  1. // Will clear the canvas that myLineChart is drawn on
  2. myLineChart.clear();
  3. // => returns 'this' for chainability

.toBase64Image(type?, quality?)

This returns a base 64 encoded string of the chart in its current state.

  1. myLineChart.toBase64Image();
  2. // => returns png data url of the image on the canvas
  3. myLineChart.toBase64Image('image/jpeg', 1)
  4. // => returns a jpeg data url in the highest quality of the canvas

.getElementsAtEventForMode(e, mode, options, useFinalPosition)

Calling getElementsAtEventForMode(e, mode, options, useFinalPosition) on your Chart instance passing an event and a mode will return the elements that are found. The options and useFinalPosition arguments are passed through to the handlers.

To get an item that was clicked on, getElementsAtEventForMode can be used.

  1. function clickHandler(evt) {
  2. const points = myChart.getElementAtEventForMode(evt, 'nearest', { intersect: true }, true);
  3. if (points.length) {
  4. const firstPoint = points[0];
  5. var label = myChart.data.labels[firstPoint.index];
  6. var value = myChart.data.datasets[firstPoint.datasetIndex].data[firstPoint.index];
  7. }
  8. }

.getDatasetMeta(index)

Looks for the dataset that matches the current index and returns that metadata. This returned data has all of the metadata that is used to construct the chart.

The data property of the metadata will contain information about each point, bar, etc. depending on the chart type.

Extensive examples of usage are available in the Chart.js testsAPI - 图1 (opens new window).

  1. var meta = myChart.getDatasetMeta(0);
  2. var x = meta.data[0].x;

setDatasetVisibility(datasetIndex, visibility)

Sets the visibility for a given dataset. This can be used to build a chart legend in HTML. During click on one of the HTML items, you can call setDatasetVisibility to change the appropriate dataset.

  1. chart.setDatasetVisibility(1, false); // hides dataset at index 1
  2. chart.update(); // chart now renders with dataset hidden

toggleDataVisibility(index)

Toggles the visibility of an item in all datasets. A dataset needs to explicitly support this feature for it to have an effect. From internal chart types, doughnut / pie, polar area, and bar use this.

  1. chart.toggleDataVisibility(2); // toggles the item in all datasets, at index 2
  2. chart.update(); // chart now renders with item hidden

getDataVisibility(index)

Returns the stored visibility state of an data index for all datasets. Set by toggleDataVisibility. A dataset controller should use this method to determine if an item should not be visible.

  1. var visible = chart.getDataVisibility(2);

hide(datasetIndex)

Sets the visibility for the given dataset to false. Updates the chart and animates the dataset with 'hide' mode. This animation can be configured under the hide key in animation options. Please see animations docs for more details.

  1. chart.hide(1); // hides dataset at index 1 and does 'hide' animation.

show(datasetIndex)

Sets the visibility for the given dataset to true. Updates the chart and animates the dataset with 'show' mode. This animation can be configured under the show key in animation options. Please see animations docs for more details.

  1. chart.show(1); // shows dataset at index 1 and does 'show' animation.

setActiveElements(activeElements)

Sets the active (hovered) elements for the chart. See the “Programmatic Events” sample file to see this in action.

  1. chart.setActiveElements([
  2. {datasetIndex: 0, index: 1},
  3. ]);

Static: getChart(key)

Finds the chart instance from the given key. If the key is a string, it is interpreted as the ID of the Canvas node for the Chart. The key can also be a CanvasRenderingContext2D or an HTMLDOMElement. This will return undefined if no Chart is found. To be found, the chart must have previously been created.

  1. const chart = Chart.getChart("canvas-id");