score:5

Accepted answer

no...

there's nothing in the chartjs api to override or extend the tooltip,

but, a workaround...

you can modify the draw method of the chart.tooltip class. this would allow you to "do something else" when the tooltip would normally be rendered by chartjs.

the draw method you want to tie into starts at line 1351 of the source here:

https://github.com/nnnick/chart.js/blob/master/src/chart.core.js

score:2

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.

score:4

update 2020 : for chartjs 3.5

here is a quick fix that i used with chartjs 3.5 to trigger events on hover over doughnut or pie parts. it relies on using the existing onhover option :

onhover : (event, activeelements) => {

    if (activeelements.length > 0) {

            // get active chart element
            let elt = activeelements[0];

            // retrieve element label
            let lbl = elt._model.label;

            // get element value
            let elt_index = elt._chart.tooltip._data.labels.indexof(lbl);
            let val = elt._chart.tooltip._data.datasets[0].data[elt_index];

           // trigger your event here :
           // ex for vuejs : this.$emit('element-hovered', { label : lbl, value : val });

       }
       else {
           // no active elements
       }

   }

working fine so far :)

score:5

for v2.0 version:

chart.defaults.global.hover.onhover = function(x) {
  if(x[0]) {
    var index = x[0]._index;
    // place here your code
  }
};

score:6

the way i'm doing it is slightly more simple: assuming you already have some code defining the canvas like canvas = document.getelementbyid("chart"); and your pie chart is window.mypie. you can use the onmousemove javascript event, or jquery hover

canvas.onmousemove = function(evt) {
    var el = window.mypie.getelementsatevent(evt);
    //do something with the el object to display other information
    //elsewhere on the page
}

in my case highlight a table row based on the value of el[0]._index

score:17

i know this has already been given an accepted answer, and im not sure if this does satisfy your use case but chart js released an update (maybe a month ago or so) that allowed for custom tooltips. this allows for a custom function to run when a tooltip would normally have been drawn. they have an example in the samples section of git hub

in short you define a custom function

 chart.defaults.global.customtooltips = function(tooltip){//do what you want}

here is the example they give in the samples with an extra bit of text added to an html tooltip. the only annoying thing i see is that don't provide the segment/point/bar that triggered this tooltip which would be really handy as then you could do some thing to the graph knowing this information but you are given the tooltip data which means you can do something with that instead.

chart.defaults.global.customtooltips = function (tooltip) {
      // tooltip element
      var tooltipel = $('#chartjs-tooltip');
      // hide if no tooltip
      if (!tooltip) {
          tooltipel.css({
              opacity: 0
          });
          return;
      }
      // set caret position
      tooltipel.removeclass('above below');
      tooltipel.addclass(tooltip.yalign);
      // set text
      tooltipel.html(tooltip.text+ " anythign custom you want");
      // find y location on page
      var top;
      if (tooltip.yalign == 'above') {
          top = tooltip.y - tooltip.caretheight - tooltip.caretpadding;
      } else {
          top = tooltip.y + tooltip.caretheight + tooltip.caretpadding;
      }
      // display, position, and set styles for font
      tooltipel.css({
          opacity: 1,
          left: tooltip.chart.canvas.offsetleft + tooltip.x + 'px',
          top: tooltip.chart.canvas.offsettop + top + 'px',
          fontfamily: tooltip.fontfamily,
          fontsize: tooltip.fontsize,
          fontstyle: tooltip.fontstyle,
      });
  };
  var piedata = [{
      value: 300,
      color: "#f7464a",
      highlight: "#ff5a5e",
      label: "red"
  }, {
      value: 50,
      color: "#46bfbd",
      highlight: "#5ad3d1",
      label: "green"
  }, {
      value: 100,
      color: "#fdb45c",
      highlight: "#ffc870",
      label: "yellow"
  }, {
      value: 40,
      color: "#949fb1",
      highlight: "#a8b3c5",
      label: "grey"
  }, {
      value: 120,
      color: "#4d5360",
      highlight: "#616774",
      label: "dark grey"
  }];

  var ctx1 = document.getelementbyid("chart-area1").getcontext("2d");
  window.mypie = new chart(ctx1).pie(piedata);
  var ctx2 = document.getelementbyid("chart-area2").getcontext("2d");
  window.mypie = new chart(ctx2).pie(piedata);
#canvas-holder {
       width: 100%;
       margin-top: 50px;
       text-align: center;
   }
   #chartjs-tooltip {
       opacity: 1;
       position: absolute;
       background: rgba(0, 0, 0, .7);
       color: white;
       padding: 3px;
       border-radius: 3px;
       -webkit-transition: all .1s ease;
       transition: all .1s ease;
       pointer-events: none;
       -webkit-transform: translate(-50%, 0);
       transform: translate(-50%, 0);
   }
   #chartjs-tooltip.below {
       -webkit-transform: translate(-50%, 0);
       transform: translate(-50%, 0);
   }
   #chartjs-tooltip.below:before {
       border: solid;
       border-color: #111 transparent;
       border-color: rgba(0, 0, 0, .8) transparent;
       border-width: 0 8px 8px 8px;
       bottom: 1em;
       content:"";
       display: block;
       left: 50%;
       position: absolute;
       z-index: 99;
       -webkit-transform: translate(-50%, -100%);
       transform: translate(-50%, -100%);
   }
   #chartjs-tooltip.above {
       -webkit-transform: translate(-50%, -100%);
       transform: translate(-50%, -100%);
   }
   #chartjs-tooltip.above:before {
       border: solid;
       border-color: #111 transparent;
       border-color: rgba(0, 0, 0, .8) transparent;
       border-width: 8px 8px 0 8px;
       bottom: 1em;
       content:"";
       display: block;
       left: 50%;
       top: 100%;
       position: absolute;
       z-index: 99;
       -webkit-transform: translate(-50%, 0);
       transform: translate(-50%, 0);
   }
<script src="https://raw.githack.com/chartjs/chart.js/v1.1.1/chart.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="canvas-holder">
    <canvas id="chart-area1" width="50" height="50" />
</div>
<div id="canvas-holder">
    <canvas id="chart-area2" width="300" height="300" />
</div>
<div id="chartjs-tooltip"></div>


Related Query

More Query from same tag