简体   繁体   English

如何在一个图表上 hover 以在 D3.js 的所有图表上显示垂直线和工具提示?

[英]How to hover over one chart to display a vertical line and tooltip on all charts in D3.js?

I'm using D3.js with ReactJS I want to hover over one chart and display a vertical line and tooltip on all the other charts.我正在使用 D3.js 和 ReactJS 我想在一个图表上使用 hover 并在所有其他图表上显示一条垂直线和工具提示。

I'm able to get vertical line in only one chart at a time.我一次只能在一张图表中获得垂直线。 The expected result is to display a vertical line and tooltip across all charts.预期结果是在所有图表中显示一条垂直线和工具提示。

this what I tried:这是我试过的:

  React.useEffect(() => {
    d3.select(anchorEl)
      .on("mouseout.tooltip", () => {
        d3.select(ref.current).attr("opacity", 0);
      })
      .on("mouseover.tooltip", () => {
        d3.select(ref.current).attr("opacity", 1);
      })
      .on("mousemove.tooltip", (e) => {
        d3.select(ref.current)
          .selectAll(".tooltipLinePoint")
          .attr("opacity", 1);
        followPoints(e);
      });
  }, [anchorEl, followPoints]);

This is the actual code result multiline-chart-tooltip-acoss-all-charts这是实际的代码结果multiline-chart-tooltip-acoss-all-charts

The solution:解决方案:

Draw the line for the current hovered chart and then draw the line for the others charts and the key is to use d3.select() and d3.selectAll().为当前悬停的图表画线,然后为其他图表画线,关键是使用 d3.select() 和 d3.selectAll()。

  d3.select(ref.current)
    .select(".tooltipLine")
    .attr("x1", x)
    .attr("x2", x)
    .attr("y1", -margin.top)
    .attr("y2", height);

  d3.selectAll(".tooltip")
    .select(".tooltipLine")
    .attr("x1", x)
    .attr("x2", x)
    .attr("y1", -margin.top)
    .attr("y2", height);

  drawLine(baseXPos);
  drawContent(baseXPos);
  drawBackground();

  drawLineForOthers(baseXPos);
  drawContentForOthers(baseXPos);
  drawBackgroundForOthers();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM