我找到了一种不同的方法来做这件事。对我来说,这更贴近字面意义。也许因为在最初回答这个问题之后框架已经更新了。
// This event fires anytime you mouse over the chart canvas.
onHover: function (event) {
event.target.style.cursor = 'default';
},
responsive: true,
maintainAspectRatio: false,
legend: {
display: true,
position: 'bottom',
onHover: function (event, legendItem) {
// There is only a legendItem when your mouse is positioned over one
if (legendItem) {
event.target.style.cursor = 'pointer';
}
}
}
pointer
)和自定义工具提示配置选项(将指针样式更改回default
)来实现所需的行为。
以下是一个演示可行性的示例配置。我不确定您是否使用 jQuery,因此我决定不使用它。随意进行相应更改。
options: {
legend: {
position: 'top',
labels: {
fontColor: 'rgb(255, 99, 132)'
},
onHover: function(event, legendItem) {
document.getElementById("canvas").style.cursor = 'pointer';
}
},
tooltips: {
custom: function(tooltip) {
if (!tooltip.opacity) {
document.getElementById("canvas").style.cursor = 'default';
return;
}
}
}
}
.generateLegend()
原型方法创建自己的自定义图例。在SO上搜索,有很多问题演示了如何完成这个过程。 - jordanwillisoptions: {
plugins : {
legend: {
labels: {
onHover: function (e) {
e.native.target.style.cursor = 'pointer';
},
onLeave: function (e) {
e.native.target.style.cursor = 'default';
}
}
}
}
}
被接受的答案对我没有用,因为我的工具提示始终可见(position: "nearest"
)。所以我使用超时来更改和恢复光标样式:
onHover: function(event, legendItem) {
document.getElementById("canvas").style.cursor = "pointer";
clearTimeout(hoverTimeout); // global var
hoverTimeout = setTimeout(function() {
document.getElementById("canvas").style.cursor = "default";
}, 100);
}
这是一个卑鄙的诀窍,但它奏效了。