chart.js - ChartJS - 将图例标题添加到工具提示标题中

标签 chart.js tooltip legend

您能告诉我要在 chartJS 选项中添加什么以使图例标题也包含在工具提示标题中吗? 在随附的屏幕截图中,您可以看到我想在工具提示的标题中添加年份提醒。

                 var chartdata = {
                 labels: ['Jan','Fev','Mar','Avr','Mai','Jui','Jui','Aou','Sep','Oct','Nov','Dec'],
                 datasets: JSON.parse('<?php echo $datasets ?>')
             };

               var graphTarget = $("#graphCanvas");

               var barGraph = new Chart(graphTarget, {
                   type: 'bar',
                   data: chartdata,
                   options: {
                            responsive: true,
                            legend: {
                                position: 'right',
                            },
                            title: {
                                display: true,
                                text: 'Consommation électrique'
                            },
                    tooltips: {
                      enabled: true,
                      mode: 'single',
                      callbacks: {
                          label: function(tooltipItems, data) {
                              return tooltipItems.yLabel + ' kW';
                          }
                      }
                    },
                    scales: {
                      yAxes: [{
                          ticks: {
                              // Includes 'kW' after the value
                              callback: function(value, index, values) {
                                  return value + ' kW';
                              }
                          }
                      }]
                    }
                        }
               });

谢谢!

graph look

最佳答案

你可以定义一个tooltips title callback如下:

tooltips: {
  callbacks: {
    title: (tooltipItems, data) => tooltipItems[0].xLabel + ' ' +  data.datasets[tooltipItems[0].datasetIndex].label,
    ...
  }

请查看您修改后的代码,看看它是如何工作的。

var barGraph = new Chart('chart', {
  type: 'bar',
  data: {
    labels: ['Jan', 'Fev', 'Mar', 'Avr', 'Mai', 'Jui', 'Jui', 'Aou', 'Sep', 'Oct', 'Nov', 'Dec'],
    datasets: [{
        label: '2019',
        data: [3, 4, 5, 2, 3, 2, 3, 4, 1, 2, 4, 5],
        backgroundColor: 'blue'
      },
      {
        label: '2020',
        data: [3, 4, 1, 2, 4, 5, 3, 4, 5, 2, 3, 2],
        backgroundColor: 'green'
      }
    ]
  },
  options: {
    responsive: true,
    legend: {
      position: 'right',
    },
    title: {
      display: true,
      text: 'Consommation électrique'
    },
    tooltips: {
      enabled: true,
      mode: 'single',
      callbacks: {
        title: (tooltipItems, data) => tooltipItems[0].xLabel + ' ' +  data.datasets[tooltipItems[0].datasetIndex].label,
        label: tooltipItems => tooltipItems.yLabel + ' kW'
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true, 
          callback: value => value + ' kW'
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="chart" height="100"></canvas>

关于chart.js - ChartJS - 将图例标题添加到工具提示标题中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64466920/

相关文章:

javascript - extjs4 在按钮单击而不是鼠标悬停时显示工具提示

excel - powershell 无法向 Excel 图表添加多个图例条目(系列)

text - 是否可以设置标签相对于 gnuplot 中的键的位置?

python - Matplotlib 1.3.0,图例行和文本不匹配

javascript - Ionic 3 + Angular 4 + chart.js - 从数组加载数据

javascript - Chart.js 左右相同的 Y 轴

javascript - 醉酒的工具提示未附加到 D3 元素

chart.js - 如何将自定义文本添加到 Charts.js 中的工具提示中

jquery - 如何在 Chart.js 中循环工具提示附加数据

javascript - 定位工具提示