将mouseout事件处理程序添加到Chart.js

时间:2017-06-15 16:22:02

标签: javascript charts chart.js chart.js2

如果是onHover的传说,我想将鼠标光标的样式更改为指针(是的,这可以按照下面的代码完成)。但是如果鼠标没有传说,我想将鼠标光标改回默认值。 Chart.js有可能吗?感谢。

注意:在Chart.js documentation中它提供了onHover回调,但没有鼠标输出。

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的链接。

1 个答案:

答案 0 :(得分:4)

添加此

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

options:

像这样:

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/