onHover event is not triggering in chart.js
Asked Answered
H

1

3

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";
    */
  }
 }
 }
  });
Hospital answered 29/11, 2017 at 5:51 Comment(0)
A
7

From v2.5 onwards parameters of onHover callback has been changed. See PR#3669

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) {

}

Previously, activeEvents was the first argument, and event was not passed.

You can check release notes of v2.5

So you've to change your onHover callback as follows:

  onHover: function(event,elements) {
    $("#canvas1").css("cursor", elements[0] ? "pointer" : "default");
  }

updated jsFiddle

Abscind answered 29/11, 2017 at 7:45 Comment(1)
Is there any event which remove onHover event on remove the hover.Grisette

© 2022 - 2024 — McMap. All rights reserved.