ChartJS Chart JS(React实现)中的自定义工具提示在交互相交为假时无法正常工作

vdgimpew  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(213)

因此,在ChartJS中使用自定义工具提示与React交互时会出现问题。
Example of current behaviour of custom tooltip
所以最初我用交互与相交:false对于我的图表,我希望在鼠标悬停在图表上时显示工具提示。但正如上面的视频所示,只有当我与数据集线及其下方的区域进行交互时,它才起作用,但即使我将鼠标悬停在这些线上,我也应该能够看到工具提示。
我的交互配置(这是最后一个,我已经尝试了很多组合,但真实的上它们对我来说并不像预期的那样工作):

  1. interaction: {
  2. intersect: false,
  3. includeInvisible: true,
  4. mode: 'nearest',
  5. axis: 'x',
  6. },

我还注意到,我的工具提示不透明度0,当我把鼠标放在数据集线和它上面的区域时,我不知道为什么。用原来的工具提示它工作得很好。下面是我的自定义工具提示的代码:

  1. tooltip: {
  2. enabled: false,
  3. external: ({ chart, tooltip }) => {
  4. if (!chart) {
  5. return;
  6. }
  7. if (tooltip.opacity === 0) {
  8. setTooltipData(prevValue => ({ ...prevValue, visible: false }));
  9. return;
  10. }
  11. // get coordinates of 2 points and get maximum of them to display tooltip according to it
  12. const position = chart.canvas.getBoundingClientRect();
  13. const maxX = Math.max(XusersPointPosition, XsubscribersPointPosition);
  14. const maxY = Math.min(YusersPointPosition, YsubscribersPointPosition);
  15. ....................................................................................
  16. setTooltipData({ top: maxY + offsetY, left: maxX + offsetX, label, data, visible: true });
  17. }

这也是我的插件绘制虚线的X坐标的数据集点(我也认为这条线不工作,因为工具提示似乎是不透明度0,这就是为什么也不活跃,所以我们也看不到线)

  1. export const plugins = [{
  2. beforeDatasetsDraw: chart => {
  3. // eslint-disable-next-line no-underscore-dangle
  4. if (chart.tooltip._active && chart.tooltip._active.length) {
  5. // find coordinates of tooltip
  6. const [activePoint] = chart.tooltip._active;
  7. const { ctx } = chart;
  8. const { x, y } = activePoint.element;
  9. const bottomY = chart.scales.y.bottom;
  10. // draw vertical line
  11. ctx.save();
  12. ctx.beginPath();
  13. ctx.moveTo(x, y);
  14. ctx.lineTo(x, bottomY);
  15. ctx.lineWidth = 1;
  16. ctx.strokeStyle = '#718086';
  17. ctx.setLineDash([4, 2]);
  18. ctx.stroke();
  19. ctx.restore();
  20. }
  21. },
  22. }];

最后是工具提示本身的渲染

  1. const NotificationStatisticsChart = ({ statistics }) => {
  2. const [tooltipData, setTooltipData] = useState({
  3. top: 0,
  4. left: 0,
  5. label: '',
  6. data: null,
  7. visible: false,
  8. });
  9. const data = useMemo(() => createDataObjectForChart(statistics), [statistics]);
  10. const options = useMemo(() => createOptions(statistics, setTooltipData), [statistics]);
  11. return (
  12. <div className={styles.chart}>
  13. <Line options={options} data={data} plugins={plugins} />
  14. {visible ? (
  15. <div className={styles.tooltip} style={{ top: top, left: left }}>
  16. <div className={styles.date}>{tooltipLabel}</div>
  17. {data.map(item => (
  18. <div key={item.type} className={styles.amount}>
  19. <span>{item.total}</span>
  20. <span className={styles.count}>{item.delta}</span>
  21. <span>{item.type}</span>
  22. </div>
  23. ))}
  24. </div>
  25. ) : null}
  26. </div>
  27. );
  28. };
lfapxunr

lfapxunr1#

我终于解决了这个问题!所以问题是在悬停自定义工具提示本身,它导致了一些奇怪的行为。要解决这个问题,你只需要从你的工具提示中删除指针事件

  1. tooltip.style.pointerEvents = 'none';

相关问题