javascript-将mouseout事件处理程序添加到Chart.js中的图例
作者:互联网
在图例的onHover事件中,我想将鼠标光标的样式更改为指针(是的,可以按照下面显示的代码来完成).但是,如果鼠标移出图例,我想将鼠标光标更改回默认值. Chart.js中可能吗?谢谢.
注意:在Chart.js documentation中,它提供onHover回调,但没有mouseout.
HTML:
<canvas id="canvas1" height="150"></canvas>
JavaScript:
var ctx = document.getElementById("canvas1").getContext("2d");
var mychart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['uno', 'dos', 'tres', 'cuatro'],
datasets: [{
data: [1, 2, 3, 4],
backgroundColor: ["#BDC3C7","#9B59B6","#E74C3C","#26B99A"]
}]
},
options: {
legend: {
position: 'bottom',
onHover: function(c) {
console.log(c);
$("#canvas1").css("cursor", "pointer");
}
},
}
});
这是the example in jsfiddle的链接.
解决方法:
加上这个
hover: {
onHover: function(e) {
$("#canvas1").css("cursor", "auto");
}
选项:
像这样:
var ctx = document.getElementById("canvas1").getContext("2d");
var mychart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['uno', 'dos', 'tres', 'cuatro'],
datasets: [{
data: [1, 2, 3, 4],
backgroundColor: ["#BDC3C7","#9B59B6","#E74C3C","#26B99A"]
}]
},
options: {
hover: {
onHover: function(e) {
$("#canvas1").css("cursor", "auto");
}
},
legend: {
position: 'bottom',
onHover: function(c) {
console.log(c);
$("#canvas1").css("cursor", "pointer");
}
}
}
});
https://jsfiddle.net/v77twcww/
标签:chart-js2,chart-js,charts,javascript 来源: https://codeday.me/bug/20191025/1932415.html