简体   繁体   English

如何更改chart.js甜甜圈图上的鼠标光标?

[英]How to to change mouse cursor on chart.js doughnut graph?

When using the Chart.js library, I want to change cursor when hovering the mouse over the doughnut.使用 Chart.js 库时,我想在将鼠标悬停在甜甜圈上时更改光标。

I do this :我这样做:

$("#dc_LoadTime").mouseleave(function(){
    $("#dc_LoadTime").css("cursor", "default");
}); 
$("#dc_LoadTime").mouseenter(function(){
    $("#dc_LoadTime").css("cursor", "pointer");
});

With this in html page在 html 页面中使用这个

<canvas id="dc_LoadTime"></canvas>

But this change cursor when mouse enter or leave canvas not on doughnut chart.但是当鼠标进入或离开画布而不在圆环图上时,这会改变光标。 I cannot find a way to do this.我找不到办法做到这一点。 Does anybody know if this is possible?有谁知道这是否可能?

In chartjs 2.0 < 2.5 I use hover in option section of the chart, like this: 在chartjs 2.0 <2.5中,我使用悬停在图表的选项部分,如下所示:

options: {
  hover: {
    onHover: function(e) {
      $("#id-of-your-canvas").css("cursor", e[0] ? "pointer" : "default");
    }
  }
}

...and here is a complete example: https://jsfiddle.net/su5fgb5h/5/ ......这是一个完整的例子: https//jsfiddle.net/su5fgb5h/5/

In version 2.5 onhover callback has changed and we need second parameter to change cursor: 在版本2.5 onhover回调已更改,我们需要第二个参数来更改游标:

options: {
  hover: {
    onHover: function(e, el) {
      $("#id-of-your-canvas").css("cursor", el[0] ? "pointer" : "default");
    }
  }
}

You can piggy back on the showTooltip method, like so 您可以像这样捎带showTooltip方法

...
var myDoughnutChart = new Chart(ctx).Doughnut(data);

var originalShowTooltip = myDoughnutChart.showTooltip;
myDoughnutChart.showTooltip = function (activeElements) {
    $("#dc_LoadTime").css("cursor", activeElements.length ? "pointer" : "default");
    originalShowTooltip.apply(this, arguments);
}

For me this worked对我来说这有效

onHover: (event, chartElement) => {
    event.target.style.cursor = chartElement[0] ? 'pointer' : 'default';
}

Add this inside your options like将此添加到您的选项中,例如

 options = {
  responsive: true,
  maintainAspectRatio: false,
  cutoutPercentage: 75,
  aspectRatio: 1.5,
  // show pointer while hovering
  onHover: (event, chartElement) => {
    event.target.style.cursor = chartElement[0] ? 'pointer' : 'default';
   },
 };`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM