[Chartjs]-Labelling the pop up Chart js

2đź‘Ť

âś…

This is the way to change the label text (By label callback).
https://www.chartjs.org/docs/latest/configuration/tooltip.html#label-callback

The same method can also change title afterTitle and so on – full list her:
https://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip-callbacks

“Hello world” example:

Change 2000 To ==> Average wage: 2000$***

*****Access to chart data depends on your specific data structure

enter image description here

var data = {
  labels: ["Africa", "Asia", "Europe", "America"],
  datasets: [{
    /* data */
    label: "Population (millions)",
    backgroundColor: ["#490A3D", "#BD1550","#E97F02", '#F8CA00'],
    data: [1000,1500,2000, 2200]
  }]
};

var options = {
  responsive: true,
  title: {
    text: 'Custom tooltip label',
    display: true
  },
  tooltips: {
    borderWidth: 1,
    borderColor: "white",
    /* https://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip-callbacks */
    callbacks: {
      label: function(tooltipItem, data) {
        /* get chart.js data  */ 
        var dataItem = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
        var labelItem = data.labels[tooltipItem.index];      
        return "Average wage: " + dataItem +"$";
      }
    }
  },
  scales: {
    xAxes: [{
      stacked: true
    }],
    yAxes: [{
      stacked: true
    }]
  }
};

var myChart = new Chart(document.getElementById("chart"), {
  type: 'bar',
  data: data,
  options: options
});
<canvas id="chart" width="800" height="350"></canvas>
<br>
<br>
<hr>
<a target="_blank" href="https://www.chartjs.org/docs/latest/configuration/tooltip.html">https://www.chartjs.org/docs/latest/configuration/tooltip.html</a>

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>

Leave a comment