const plugin = {
id: 'after-render',
afterRender: (c) => {
const ctx = document.getElementById('containerId').getContext('2d');
ctx.save();
// The next line is essential to prevent unusual behavior.
// As it causes the whole chart to go blank, when removed
// Technique is taken from:
// https://stackoverflow.com/a/50126796/165164
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, c.width, c.height);
ctx.restore();
}
};
1条答案
按热度按时间wz1wpwve1#
这可以通过使用Canvas和afterRender钩子来完成。使用
afterRender
钩子创建你的插件,当调用它时,它会获取chart.js元素的画布,并更改它的fillStyle
。插件:
将
containerId
更改为您在html canvas标记中传递的id。然后,在Chart构造函数的
plugins
选项中传递这个插件: