user10268561
user10268561

Reputation:

labelling the pop up Chart js

I want to change label on the pop up(look photo) like callback like this

ticks: {
  callback: function (index) {
    return dir[index];
  }
}

pop up

and i can't find this setting.

Sorry for my bad england. love y

Upvotes: 1

Views: 1911

Answers (1)

Ezra Siton
Ezra Siton

Reputation: 7741

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/[email protected]"></script>

Upvotes: 2

Related Questions