score:0

after changing the tooltip options on an existing chart, you have to invoke chart.update().

if for example you let the user switch between default and custom tooltip mode, you could define an onclick event handler as follows.

<input type="checkbox" onclick='enablecustomtooltip(this.checked)'> custom tooltip
...
const enablecustomtooltip = function(enabled) {
  if (enabled) {    
    chart.options.tooltips = { enabled: false, custom: (tooltipmodel) => customtooltip(tooltipmodel) };
  } else {
    chart.options.tooltips = { enabled: true };
  }
  chart.update();
};

please take a look at the following chart based on code from the chart.js documentation and see how it works.

const customtooltip = function(tooltipmodel) {
  var tooltipel = document.getelementbyid('chartjs-tooltip');
  if (!tooltipel) {
    tooltipel = document.createelement('div');
    tooltipel.id = 'chartjs-tooltip';
    tooltipel.innerhtml = '<table></table>';
    document.body.appendchild(tooltipel);
  }
  if (tooltipmodel.opacity === 0) {
    tooltipel.style.opacity = 0;
    return;
  }
  tooltipel.classlist.remove('above', 'below', 'no-transform');
  if (tooltipmodel.yalign) {
    tooltipel.classlist.add(tooltipmodel.yalign);
  } else {
    tooltipel.classlist.add('no-transform');
  }

  function getbody(bodyitem) {
    return bodyitem.lines;
  }

  if (tooltipmodel.body) {
    var titlelines = tooltipmodel.title || [];
    var bodylines = tooltipmodel.body.map(getbody);
    var innerhtml = '<thead>';
    titlelines.foreach(function(title) {
      innerhtml += '<tr><th>' + title + '</th></tr>';
    });
    innerhtml += '</thead><tbody>';
    bodylines.foreach(function(body, i) {
      var colors = tooltipmodel.labelcolors[i];
      var style = 'background:' + colors.backgroundcolor;
      style += '; border-color:' + colors.bordercolor;
      style += '; border-width: 2px';
      var span = '<span style="' + style + '"></span>';
      innerhtml += '<tr><td>' + span + body + '</td></tr>';
    });
    innerhtml += '</tbody>';
    var tableroot = tooltipel.queryselector('table');
    tableroot.innerhtml = innerhtml;
  }

  var position = chart.canvas.getboundingclientrect();
  tooltipel.style.opacity = 1;
  tooltipel.style.position = 'absolute';
  tooltipel.style.left = position.left + window.pagexoffset + tooltipmodel.caretx + 'px';
  tooltipel.style.top = position.top + window.pageyoffset + tooltipmodel.carety + 'px';
  tooltipel.style.fontfamily = tooltipmodel._bodyfontfamily;
  tooltipel.style.fontsize = tooltipmodel.bodyfontsize + 'px';
  tooltipel.style.fontstyle = tooltipmodel._bodyfontstyle;
  tooltipel.style.padding = tooltipmodel.ypadding + 'px ' + tooltipmodel.xpadding + 'px';
  tooltipel.style.pointerevents = 'none';
};

const enablecustomtooltip = function(enabled) {
  if (enabled) {    
    chart.options.tooltips = { enabled: false, custom: (tooltipmodel) => customtooltip(tooltipmodel) };
  } else {
    chart.options.tooltips = { enabled: true };
  }
  chart.update();
};

var chart = new chart('mychart', {
  type: 'pie',
  data: {
    labels: ['january', 'february', 'march'],
    datasets: [{
      data: [50445, 33655, 15900],
      backgroundcolor: ['#ff6384', '#36a2eb', '#ffce56']
    }]
  },
  options: {
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.4/chart.min.js"></script>
<input type="checkbox" onclick='enablecustomtooltip(this.checked)'> custom tooltip
<canvas id="mychart" height="90"></canvas>


Related Query

More Query from same tag