Detecting hover events over parts of a chart using Chart.js

后端 未结 6 1216
野性不改
野性不改 2020-12-15 11:39

I\'ve made a pie chart using Chart.js, and I\'d like to detect when a segment is hovered over. I\'ve found plenty of documentation regarding manipulating the tooltips that a

6条回答
  •  佛祖请我去吃肉
    2020-12-15 11:55

    If found a small trick using customTooltip, too. I searched for a solution to get an Event if the user moves with the mouse over a Value and a Tooltip is shown. Mainly i liked to present in a different frame some detail informations besides the raw Plot values.

    var options = {            
      customTooltips: function (tooltip)
      {
        if (!tooltip) return;
    
        tooltip.custom = false;
        tooltip.draw();
        OnEntrySelected(tooltip.title);
        tooltip.custom = this;
      }
    }
    

    The custom tooltip is drawn using tooltip.draw(), but this calls the custom method. I set it to false to avoid a recursive loop, call the default behavior and take the data i need for my callback (OnEntrySelected) which is in this case the string of the x-Axis label. This event is triggered whenever the tooltip is shown.

提交回复
热议问题