gpt4 book ai didi

javascript - 如何在 Chart.js v2.0 中的标签上添加 OnClick 事件?

转载 作者:太空狗 更新时间:2023-10-29 15:26:44 26 4
gpt4 key购买 nike

寻找一种在 chartjs 2.0 中的“label”元素上添加 onClick 句柄的方法使用下面的方法,每当点击 Char.js V2.0 RadarChart 中的任何一个标签属性时,都会在 console.log 中返回“undifined”。

var data = {
// below line is the labels
labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
datasets: [
{
label: "My First dataset", //this only shows as legend, not label.

backgroundColor: "rgba(179,181,198,0.2)",
borderColor: "rgba(179,181,198,1)",
pointBackgroundColor: "rgba(179,181,198,1)",
pointBorderColor: "#fff",
pointHoverBackgroundColor: "#fff",
pointHoverBorderColor: "rgba(179,181,198,1)",
data: [65, 59, 90, 81, 56, 55, 40]
},
....

//Below is how to OnClick on chart points in chart.js V2, 
//However, it didn't apply to labels, will return "undifined" .
$('#ChartV2').click(function(e) {
var activePoints = myRadarChart.getElementsAtEvent(e);
var firstPoint = activePoints[0];
console.log(firstPoint);
if (firstPoint !== undefined){
alert(firstPoint._index);
}
});

最佳答案

在 chart.js 2.5(甚至更早)中,您可以在选项中放置一个 onClick:

'legend' : {
'onClick' : function (evt, item) {
console.log ('legend onClick', evt, item);
},
'display' : true,
'labels' : ...

关于javascript - 如何在 Chart.js v2.0 中的标签上添加 OnClick 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37028924/

26 4 0