简体   繁体   English

将mouseout事件处理程序添加到Chart.js中的图例

[英]Add mouseout event handler to legends in Chart.js

In the event of onHover of legends, I would like to change the style of the mouse cursor to pointer (yes, this can be done as the code shown below). 在onHover of legends的情况下,我想将鼠标光标的样式更改为指针(是的,这可以按照下面显示的代码完成)。 But in the event of mouse out of legends, I would like to change the mouse cursor back to default. 但是如果鼠标没有传说,我想将鼠标光标改回默认值。 Is it possible in Chart.js? Chart.js有可能吗? Thanks. 谢谢。

Note: in the Chart.js documentation it provides onHover callback, but no mouseout. 注意:在Chart.js文档中,它提供onHover回调,但没有mouseout。

HTML: HTML:

<canvas id="canvas1" height="150"></canvas>

JavaScript: 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");          
        }
   },
  }
});

Here is the link to the example in jsfiddle . 这是jsfiddle中示例的链接。

Add this 加上这个

hover: {
    onHover: function(e) {
    $("#canvas1").css("cursor", "auto");
  }

to options: options:


Like this: 像这样:

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/ https://jsfiddle.net/v77twcww/

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

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