如何在工具提示 Chart JS 中显示 xAxes 和 yAxes 数据?

How can I display the xAxes and yAxes data in the tooltip, Chart JS?

我正在尝试实现下面第一张图片中显示的工具提示。在工具提示中,我需要显示 yAxesxAxes 数据。我使用的 chart.js 版本是 3.7.0

我的工具提示如下所示:

我正在尝试复制的工具提示:

chart.js 文档对我来说很难理解。有没有哪位大神可以解释一下。

问题:为什么我的工具提示 return 正在 yAxes 数据,我 return 作为变量(label) 未定义?

是否有任何其他选项可以使我的图表看起来像第二张图片中的图表?

我的代码:

 tooltip: {
   displayColors: false,
   backgroundColor: 'rgba(45,132,180,0.8)',
   bodyFontColor: 'rgb(255,255,255)',
   callbacks: {
     title: function(item, everything){
       return;
     },
     label: function(item, everything){
       //console.log(item);
       //console.log(everything);

       let first = item.yLabel;
       let second = item.xLabel;

       let label = first + ' ppm';
       return label;
     }
   }
 }

预先感谢您的时间和努力,请帮我弄清楚我做错了什么!

yLabelxLabel 不再出现在工具提示中,它们是 V2 语法。 您可以在解析部分中轴化 y 对象以获取 y 值。然后你可以使用 afterBody 回调来显示 x 标签,如下所示:

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderColor: 'pink'
    }]
  },
  options: {
    plugins: {
      tooltip: {
        displayColors: false,
        backgroundColor: 'rgba(45,132,180,0.8)',
        bodyFontColor: 'rgb(255,255,255)',
        callbacks: {
          title: () => {
            return
          },
          label: (ttItem) => (`${ttItem.parsed.y} ppm`),
          afterBody: (ttItems) => (ttItems[0].label)
        }
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.0/chart.js"></script>
</body>