Chartjs-How do I customize the chart.js tooltip? Two labels have the same data, which I want to show you with each data

0👍

Create a canvas for your chart:

<canvas id="myChart"></canvas>

This is the required javascript code:

var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
  type: 'pie',
  data: {
    labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red", "Black"],
    datasets: [{
      backgroundColor: [
        "#2ecc71",
        "#3498db",
        "#95a5a6",
        "#9b59b6",
        "#f1c40f",
        "#e74c3c",
        "#34495e"
      ],
      data: [12, 19, 3, 17, 28, 24, 7]
    }]
  },
  options: {
      tooltips : {

      callbacks : { 
        label : function(tooltipItem, data) {
          var dataset = data.datasets[tooltipItem.datasetIndex];
                  return  dataset.data[tooltipItem.index] + ' %';
                },
        },
      },
  }
});

And this is the Charts.js cdn that i have used.

Here is the result:

https://imgur.com/gallery/3KszJQY

This is a working js fiddle.

Hope it helps.

Leave a comment