¥In some use cases you would want a background image or color over the whole canvas. There is no built-in support for this, the way you can achieve this is by writing a custom plugin.
在下面的两个示例插件中,你可以看到如何将颜色或图片绘制到画布上作为背景。这种为图表提供背景的方法只有在你想要导出具有该特定背景的图表时才有必要。对于正常使用,你可以使用 CSS(opens new window) 更轻松地设置背景。
¥In the two example plugins underneath here you can see how you can draw a color or image to the canvas as background. This way of giving the chart a background is only necessary if you want to export the chart with that specific background.
For normal use you can set the background more easily with CSS(opens new window).
const data ={labels:['Red','Blue','Yellow'],datasets:[{label:'My First Dataset',data:[300,50,100],backgroundColor:['rgb(255, 99, 132)','rgb(54, 162, 235)','rgb(255, 205, 86)'],hoverOffset:4}]};
// 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:'customCanvasBackgroundColor',beforeDraw:(chart, args, options)=>{const{ctx}= chart;
ctx.save();
ctx.globalCompositeOperation ='destination-over';
ctx.fillStyle = options.color ||'#99ffff';
ctx.fillRect(0,0, chart.width, chart.height);
ctx.restore();}};
const data ={labels:['Red','Blue','Yellow'],datasets:[{label:'My First Dataset',data:[300,50,100],backgroundColor:['rgb(255, 99, 132)','rgb(54, 162, 235)','rgb(255, 205, 86)'],hoverOffset:4}]};
// 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 image =newImage();
image.src ='https://chart.nodejs.cn/img/chartjs-logo.svg';const plugin ={id:'customCanvasBackgroundImage',beforeDraw:(chart)=>{if(image.complete){const ctx = chart.ctx;const{top, left, width, height}= chart.chartArea;const x = left + width /2- image.width /2;const y = top + height /2- image.height /2;
ctx.drawImage(image, x, y);}else{
image.onload=()=> chart.draw();}}};