悬停后更新图表轴标签 - Chart.js

发布于 2025-01-13 21:29:51 字数 302 浏览 5 评论 0 原文

我正在使用 Chart.js 版本 3.7.1。我想在悬停后替换 y 轴标签。悬停事件工作正常,它可以很好地识别 y 轴,但标签不会替换。我尝试使用 Chart API 中的 update() 方法,但没有帮助。

我还准备了一些快速游乐场,我在其中放置了一个解决此问题的自定义图表。 https://stackblitz.com/edit/angular-ivy-x3sncg

I'm working with chart.js version 3.7.1. I want to replace y axis labels after hovering it. Hover event works fine and it recognizes y axis quite good but labels don't replace. I tried using update() method from Chart API but it didn't help.

I also prepared some quick playground where I placed a custom chart with this problem.
https://stackblitz.com/edit/angular-ivy-x3sncg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2025-01-20 21:29:51

您可以将 afterEvent 插件与 ticks.callback函数如下所示:

options: {
  scales: {
    y: {
      beginAtZero: true,
      ticks: {
        callback: value => value + (this.isHoveringYAxe ? '

请看一下你修改后的StackBlitz 看看它是如何工作的。

: '') } } } }, plugins: [{ id: 'customHover', afterEvent: (chart, args) => { const event = args.event; if (event.type == 'mousemove') { if (event.x < chart.scales.y.width && !this.isHoveringYAxe) { this.isHoveringYAxe = true; chart.update(); } else if (event.x >= chart.scales.y.width && this.isHoveringYAxe) { this.isHoveringYAxe = false; chart.update(); } } } ]

请看一下你修改后的StackBlitz 看看它是如何工作的。

You could combine your afterEvent plugin with a ticks.callback function as shown below:

options: {
  scales: {
    y: {
      beginAtZero: true,
      ticks: {
        callback: value => value + (this.isHoveringYAxe ? '

Please take a look at your amended StackBlitz and see how it works.

: '') } } } }, plugins: [{ id: 'customHover', afterEvent: (chart, args) => { const event = args.event; if (event.type == 'mousemove') { if (event.x < chart.scales.y.width && !this.isHoveringYAxe) { this.isHoveringYAxe = true; chart.update(); } else if (event.x >= chart.scales.y.width && this.isHoveringYAxe) { this.isHoveringYAxe = false; chart.update(); } } } ]

Please take a look at your amended StackBlitz and see how it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文