标记弹出图表js

问题描述 投票:0回答:1

我想像这样callback那样在弹出窗口(照片)上更改标签

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

pop up

而且我找不到此设置。

对不起,我的英格兰不好。爱y

javascript chart.js
1个回答
0
投票

这是更改标签文本的方法(按label callback)。https://www.chartjs.org/docs/latest/configuration/tooltip.html#label-callback

相同的方法也可以更改title afterTitle,依此类推-完整列出她:https://www.chartjs.org/docs/latest/configuration/tooltip.html#tooltip-callbacks

“ Hello world”示例:

2000更改为==>Average wage: 2000$***

*****对图表数据的访问取决于您的特定数据结构

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>
© www.soinside.com 2019 - 2024. All rights reserved.