ChartJS无法注册全局插件



在ChartJS从v2迁移到v3之后,我无法注册全局插件,通过遵循迁移指南,我在Chart.register中更改了Chart.plugins.register,但我得到了这个:

类没有id:函数(图表(

我认为问题在afterDraw函数中,但在迁移后应该如何修复?

代码

Chart.register({
afterDraw: function (chart) {
var data = chart.data.datasets[0].data;
if (data.length === 0) {
// No data is present
var ctx = chart.chart.ctx;
var width = chart.chart.width;
var height = chart.chart.height
chart.clear();
ctx.save();
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = "16px normal 'Helvetica Nueue'";
ctx.fillText('Nessun dato disponibile', width / 2, height / 2);
ctx.fontColor = "#828282";
ctx.restore();
}
}
});

通过更好地阅读文档,必须为每个注册的插件配置一个唯一的id。

插件必须定义一个唯一的id才能进行配置。

所以我只是在我的代码中添加了id

Chart.register({
id: 'no_data_label',
afterDraw: (chart, args, options) => {
const data = chart.data.datasets[0].data;
if (data.length === 0) {
// No data is present
console.log(chart)
const current = chart.ctx;
const width = chart.width;
const height = chart.height
chart.clear();
current.save();
current.textAlign = 'center';
current.textBaseline = 'middle';
current.font = "16px normal 'Helvetica Nueue'";
current.fillText('Nessun dato disponibile', width / 2, height / 2);
current.fontColor = "#828282";
current.restore();
}
}
});

最新更新