Plugins

Plugins are the most efficient way to customize or change the default behavior of a chart. They have been introduced at version 2.1.0Plugins - 图1 (opens new window) (global plugins only) and extended at version 2.5.0Plugins - 图2 (opens new window) (per chart plugins and options).

Using plugins

Plugins can be shared between chart instances:

  1. const plugin = { /* plugin implementation */ };
  2. // chart1 and chart2 use "plugin"
  3. const chart1 = new Chart(ctx, {
  4. plugins: [plugin]
  5. });
  6. const chart2 = new Chart(ctx, {
  7. plugins: [plugin]
  8. });
  9. // chart3 doesn't use "plugin"
  10. const chart3 = new Chart(ctx, {});

Plugins can also be defined directly in the chart plugins config (a.k.a. inline plugins):

WARNING

inline plugins are not registered. Some plugins require registering, i.e. can’t be used inline.

  1. const chart = new Chart(ctx, {
  2. plugins: [{
  3. beforeInit: function(chart, args, options) {
  4. //..
  5. }
  6. }]
  7. });

However, this approach is not ideal when the customization needs to apply to many charts.

Global plugins

Plugins can be registered globally to be applied on all charts (a.k.a. global plugins):

  1. Chart.register({
  2. // plugin implementation
  3. });

WARNING

inline plugins can’t be registered globally.

Configuration

Plugin ID

Plugins must define a unique id in order to be configurable.

This id should follow the npm package name conventionPlugins - 图3 (opens new window):

  • can’t start with a dot or an underscore
  • can’t contain any non-URL-safe characters
  • can’t contain uppercase letters
  • should be something short, but also reasonably descriptive

If a plugin is intended to be released publicly, you may want to check the registryPlugins - 图4 (opens new window) to see if there’s something by that name already. Note that in this case, the package name should be prefixed by chartjs-plugin- to appear in Chart.js plugin registry.

Plugin options

Plugin options are located under the options.plugins config and are scoped by the plugin ID: options.plugins.{plugin-id}.

  1. const chart = new Chart(ctx, {
  2. options: {
  3. foo: { ... }, // chart 'foo' option
  4. plugins: {
  5. p1: {
  6. foo: { ... }, // p1 plugin 'foo' option
  7. bar: { ... }
  8. },
  9. p2: {
  10. foo: { ... }, // p2 plugin 'foo' option
  11. bla: { ... }
  12. }
  13. }
  14. }
  15. });

Disable plugins

To disable a global plugin for a specific chart instance, the plugin options must be set to false:

  1. Chart.register({
  2. id: 'p1',
  3. // ...
  4. });
  5. const chart = new Chart(ctx, {
  6. options: {
  7. plugins: {
  8. p1: false // disable plugin 'p1' for this instance
  9. }
  10. }
  11. });

To disable all plugins for a specific chart instance, set options.plugins to false:

  1. const chart = new Chart(ctx, {
  2. options: {
  3. plugins: false // all plugins are disabled for this instance
  4. }
  5. });

Plugin defaults

You can set default values for your plugin options in the defaults entry of your plugin object. In the example below the canvas will always have a lightgreen backgroundColor unless the user overrides this option in options.plugins.custom_canvas_background_color.color.

  1. const plugin = {
  2. id: 'custom_canvas_background_color',
  3. beforeDraw: (chart, args, options) => {
  4. const {ctx} = chart;
  5. ctx.save();
  6. ctx.globalCompositeOperation = 'destination-over';
  7. ctx.fillStyle = options.color;
  8. ctx.fillRect(0, 0, chart.width, chart.height);
  9. ctx.restore();
  10. },
  11. defaults: {
  12. color: 'lightGreen'
  13. }
  14. }

Plugin Core API

Read more about the existing plugin extension hooks.

Chart Initialization

Plugins are notified during the initialization process. These hooks can be used to setup data needed for the plugin to operate.

Chart.js init flowchart

Chart Update

Plugins are notified throughout the update process.

Chart.js update flowchart

Scale Update

Plugins are notified throughout the scale update process.

Chart.js scale update flowchart

Rendering

Plugins can interact with the chart throughout the render process. The rendering process is documented in the flowchart below. Each of the green processes is a plugin notification. The red lines indicate how cancelling part of the render process can occur when a plugin returns false from a hook. Not all hooks are cancelable, however, in general most before* hooks can be cancelled.

Chart.js render pipeline flowchart

Event Handling

Plugins can interact with the chart during the event handling process. The event handling flow is documented in the flowchart below. Each of the green processes is a plugin notification. If a plugin makes changes that require a re-render, the plugin can set args.changed to true to indicate that a render is needed. The built-in tooltip plugin uses this method to indicate when the tooltip has changed.

Chart.js event handling flowchart

Chart destroy

Plugins are notified during the destroy process. These hooks can be used to destroy things that the plugin made and used during its life. The destroy hook has been deprecated since Chart.js version 3.7.0, use the afterDestroy hook instead.

Chart.js destroy flowchart