javascript - Chart.js - 鼠标悬停时自定义 css 到数据集

标签 javascript charts

我的饼图选项中有以下代码,以便将鼠标光标更改为悬停时的指针:

hover: {
      onHover: (e, el) => {
        const section = el[0];
        const currentStyle = e.target.style;
        currentStyle.cursor = section ? 'pointer' : 'normal';
      }
    }

问题

  1. 即使光标指向饼图外部(或内部),光标也会变为指针。我希望它仅在悬停数据集时发生变化。可能吗?

  2. 悬停方法即使在光标停留在元素内时也会执行,这可能会导致性能问题,是否可以在 chart.js 上使用与 onmouseenter 相同的功能?

谢谢

enter image description here

最佳答案

你唯一的问题是你为光标使用了无效值,没有“正常”,它应该是“默认”。

试试这个:

hover: {
  onHover: (e, el) => {
    const section = el[0];
    const currentStyle = e.target.style;
    currentStyle.cursor = section ? 'pointer' : 'default'; // <-- default here
  }
}

要限制触发 onHover 方法的有用功能的次数,您可以对先前的目标进行简单检查:

onHover: (() => {
  let prevTarget;

  return (e, el) => {
    const section = el[0];

    if (prevTarget !== section) {
      // Code in this if-block is called only when necessary

      const currentStyle = e.target.style;
      currentStyle.cursor = section ? 'pointer' : 'default';

      // .........
    }

    prevTarget = section;
  }
})()

关于javascript - Chart.js - 鼠标悬停时自定义 css 到数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49134444/

相关文章:

javascript - 如何使用 JavaScript 为特殊过滤器在 html Canvas 中绘制图像

javascript - 无法在 Express 生成器上执行 npm start

javascript - 谷歌图表 slider 和过滤器

android - 有没有办法在 iOS 版本的 MPAndroidChart 框架中处理手势结束(触摸)?

charts - (ApexCharts) 如何使饼图大小相同?

javascript - Laravel 5 中未捕获的 ReferenceError : Chart is not defined - Chart. js

javascript - jQuery:仅在悬停时加载内容

javascript - setup() 在 JS 中使用 PI 的 GPIO 引脚做什么

javascript - IE9 : Access is denied when set up document. 域

javascript - 具有三重 y 轴的 Google 折线图