javascript - 折线图 JS 中的工具提示回调不起作用

标签 javascript callback chart.js

我正在使用折线图 js 来显示图表,并尝试计算所选点与前一个点之间的差异并将其呈现在工具提示中。

我读过应该使用回调函数,但是当我尝试它时没有任何反应。

即使我只是尝试更改标题 如果有人可以提供帮助,这是我的折线图选项:) 图表js版本:3.6.1

  plugins: {
    legend: {
      display: false
    },
    title: {
      display: true,
      font: {
        size: 18,
      },
      color: "white"
    },
    zoom: {
      zoom: {
        drag: {
          enabled: true
        },
        mode: 'xy',
      }
    },
    tooltip: {
      callbacks: {
        title: function () {
          return "my tittle";
         }
      }
    }
}

最佳答案

这是因为您在 V3 中使用了 V2 语法,您可以读取 options 中的命名空间.

工具提示已移至插件部分

对于所有更改,请阅读 migration guide

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'orange'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'pink'
      }
    ]
  },
  options: {
    plugins: {
      tooltip: {
        callbacks: {
          title: () => ('Title')
        }
      }
    }
  }
}

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.6.2/chart.js"></script>
</body>

关于javascript - 折线图 JS 中的工具提示回调不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70465626/

相关文章:

javascript - ChartJS 2.0 - 饼图上的 Huddle 标签

javascript - 有没有办法找到两个字符串之间哪些部分相同?

javascript - 正则表达式字符串比较忽略符号

javascript - 无效的非字符串/缓冲区 block Node.js

amazon-web-services - 如何使用一个TaskToken 两次回调同一个step 函数?

javascript - 在回调中使用 this.setState

javascript - 如何限制滚动到鼠标当前悬停的 div?

php - JSON 和我的 PHP 页面

charts - ChartJS 注释隐藏/显示

javascript - "Process is not defined"与react-chartjs-2 CDN