// Note: changes to the plugin code is not reflected to the chart, because the plugin is loaded at chart construction time and editor changes only trigger an chart.update().
const plugin = {
id: 'custom_canvas_background_color',
- beforeDraw: (chart) => {
+ beforeDraw: (chart, args, options) => {
const {ctx} = chart;
ctx.save();
ctx.globalCompositeOperation = 'destination-over';
- ctx.fillStyle = 'lightGreen';
+ ctx.fillStyle = options.color || '#99ffff';
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
const config = {
type: 'doughnut',
data: data,
+ options: {
+ plugins: {
+ color: 'lightGreen',
+ }
+ },
plugins: [plugin],
};
// </block:config>
If you want your new chart type to be statically typed, you must provide a `.d.ts` TypeScript declaration file. Chart.js provides a way to augment built-in types with user-defined ones, by using the concept of "declaration merging".
-When adding a new chart type, `ChartTypeRegistry` must contains the declarations for the new type, either by extending an existing entry in `ChartTypeRegistry` or by creating a new one.
+When adding a new chart type, `ChartTypeRegistry` must contain the declarations for the new type, either by extending an existing entry in `ChartTypeRegistry` or by creating a new one.
For example, to provide typings for a new chart type that extends from a bubble chart, you would add a `.d.ts` containing:
The `destroy` hook has been deprecated since Chart.js version 3.7.0, use the `afterDestroy` hook instead.

+
+## TypeScript Typings
+
+If you want your plugin to be statically typed, you must provide a `.d.ts` TypeScript declaration file. Chart.js provides a way to augment built-in types with user-defined ones, by using the concept of "declaration merging".
+
+When adding a plugin, `PluginOptionsByType` must contain the declarations for the plugin.
+
+For example, to provide typings for the [`canvas backgroundColor plugin`](../configuration/canvas-background.md), you would add a `.d.ts` containing:
+
+```ts
+import { ChartType, Plugin } from 'chart.js'
+
+declare module 'chart.js' {
+ interface PluginOptionsByType<TType extends ChartType> {
+ custom_canvas_background_color?: {
+ color?: string
+ }
+ }
+}
+```