根据图表。
new Chart(document.getElementById(chartID), {
type: 'pie',
responsive: true,
maintainAspectRatio: false,
data: {
labels: graph.globals.labels,
datasets: [{
backgroundColor: pieOptions.backgrounds,
data: pieOptions.objToArr(graph.meetingsData),
hoverBorderWidth: 5,
borderColor: 'transparent',
}]
},
options: {
title: {
display: true,
text: graph.globals.title,
},
legend: {
display: true,
position: 'bottom',
fullWidth: false,
onClick: () => {},
labels: {
generateLabels: (chart) => {
return pieOptions.legendLeft(chart);
}
}
},
plugins: [{
beforeInit: function(chart, options) {
console.log('yolo');
}
}]
rotation: 3.9,
}
});
但是,当我使用变量创建插件或在上面创建插件时,它没有记录任何内容。我可以使用插件的唯一方法是在全球注册时。
有人可以向我解释为什么它不起作用?
plugins
应放在其自己的配置部分中,而不嵌套在options
中。
而不是:
options: {
title: {
display: true,
text: graph.globals.title,
},
legend: {
display: true,
position: 'bottom',
fullWidth: false,
onClick: () => {},
labels: {
generateLabels: (chart) => {
return pieOptions.legendLeft(chart);
}
}
},
plugins: [{
beforeInit: function(chart, options) {
console.log('yolo');
}
}]
rotation: 3.9,
}
您的代码应该看起来像:
options: {
title: {
display: true,
text: graph.globals.title,
},
legend: {
display: true,
position: 'bottom',
fullWidth: false,
onClick: () => {},
labels: {
// generateLabels: (chart) => {
// return pieOptions.legendLeft(chart);
// }
}
},
rotation: 3.9,
},
plugins: [{
beforeInit: function(chart, options) {
console.log('yolo');
}
}]
工作jsfiddle:https://jsfiddle.net/r1x63b8v/