onHover 事件未在 chart.js 中触发 [英] onHover event is not triggering in chart.js
问题描述
我想在鼠标在图表上移动时更改光标,例如 this fiddle.这适用于 chart.js v2.4
但不适用于 v2.6
&v2.7
有什么想法吗?
I want to change the cursor when the mouse moves on the chart, something like this fiddle.
This works with chart.js v2.4
but not works with v2.6
& v2.7
any idea?
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", e[0] ? "pointer" : "default");
/* without jquery it can be like this:
var el = document.getElementById("canvas1");
el.style.cursor = e[0] ? "pointer" : "default";
*/
}
}
}
});
推荐答案
从v2.5
起,onHover 回调的参数已更改.请参阅 PR#3669
From v2.5
onwards parameters of onHover callback has been changed. See PR#3669
onHover 回调现在有 3 个参数.第二个参数是事件触发了悬停.这样做是为了与onClick 回调.
onHover callback now has 3 parameters. The 2nd parameter is the event that triggered the hover. This was done for consistency with the onClick callback.
function(event, activeElements) {
}
以前,activeEvents 是第一个参数,并且没有传递事件.
Previously, activeEvents was the first argument, and event was not passed.
您可以查看v2.5的发行说明
所以你必须改变你的 onHover 回调
如下:
So you've to change your onHover callback
as follows:
onHover: function(event,elements) {
$("#canvas1").css("cursor", elements[0] ? "pointer" : "default");
}
这篇关于onHover 事件未在 chart.js 中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!