Search code examples
jquerycursorchart.jsonhover

onHover event is not triggering in chart.js


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";
    */
  }
 }
 }
  });

Solution

  • 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