Chart Prototype Methods

    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(config)

    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. // duration is the time for the animation of the redraw in milliseconds
    2. // lazy is a boolean. if true, the animation can be interrupted by other animations
    3. myLineChart.update(); // Calling update now animates the position of March from 90 to 50.

    A config object can be provided with additional configuration for the update process. This is useful when update is manually called inside an event handler and some different animation is desired.

    The following properties are supported:

    • duration (number): Time for the animation of the redraw in milliseconds
    • lazy (boolean): If true, the animation can be interrupted by other animations
    • easing (string): The animation easing function. See for possible values.
      Example:
    1. myChart.update({
    2. duration: 800,
    3. easing: 'easeOutBounce'
    4. })

    See Updating Charts for more details.

    .reset()

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

    1. myLineChart.reset();

    .render(config)

    See .update(config) for more details on the config object.

    Use this to stop any current animation loop. 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()

    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.

    1. // Resizes & redraws to fill its container element
    2. myLineChart.resize();
    3. // => returns 'this' for chainability

    .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();

    .toBase64Image()

    This returns a base 64 encoded string of the chart in it's current state.

    1. myLineChart.toBase64Image();
    2. // => returns png data url of the image on the canvas

    Returns an HTML string of a legend for that chart. The legend is generated from the legendCallback in the options.

    .getElementAtEvent(e)

    1. // => returns the first element at the event point.

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

    1. function clickHandler(evt) {
    2. var item = myChart.getElementAtEvent(evt)[0];
    3. if (item) {
    4. var label = myChart.data.labels[firstPoint._index];
    5. var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
    6. }
    7. }

    .getElementsAtEvent(e)

    Looks for the element under the event point, then returns all elements at the same data index. This is used internally for 'label' mode highlighting.

    Calling getElementsAtEvent(event) on your Chart instance passing an argument of an event, or jQuery event, will return the point elements that are at that the same position of that event.

    1. canvas.onclick = function(evt){
    2. var activePoints = myLineChart.getElementsAtEvent(evt);
    3. // => activePoints is an array of points on the canvas that are at the same position as the click event.
    4. };

    This functionality may be useful for implementing DOM based tooltips, or triggering custom behaviour in your application.

    .getDatasetAtEvent(e)

    Looks for the element under the event point, then returns all elements from that dataset. This is used internally for 'dataset' mode highlighting

    1. myLineChart.getDatasetAtEvent(e);

    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.

    Extensive examples of usage are available in the .