简体   繁体   English

多个工具提示在反应中未正确显示

[英]Multiple tooltips not showing correctly in react

I am working with react D3 charts, and I have created charts and it is working fine.我正在使用 React D3 图表,并且我已经创建了图表并且工作正常。

What I have done我做了什么

  • I have several charts which are updating with in some time intervals, something like live data我有几个图表在某些时间间隔内更新,比如实时数据
  • So here to achieve this I out use effect and updating my charts every second, and my data in charts updates correctly.因此,为了实现这一点,我每秒都会使用效果并更新图表,并且图表中的数据会正确更新。
  • I have given one tooltip on hover over the the bar, so that user can check the data for each bar or line.我在柱上给出了一个关于 hover 的工具提示,以便用户可以检查每个柱或行的数据。

Using below code to show the tooltip使用以下代码显示工具提示

.on("mousemove", function (event, d) {
    // this whole code is when I hover that perticular bar
    d3.select(this)
      .transition()
      .duration("50")
      .attr("opacity", 0.6)
      .attr("x", (a) => xScaleBars(a.timeline) - 3)
      .attr("width", xScaleBars.bandwidth() + 6)
      .style("filter", "url(#glow)");

    div.transition().duration(50).style("opacity", 1);
    div
      .html(
        `</text><text"></br> value :  ${d.dataToShow}
        <br/>
        </text><text"></br> Month :  ${d.month}
        `
      )

      .style("left", event.pageX - 58 + "px")
      .style("top", event.pageY - 140 + "px");
  })
  .on("mouseout", function (d, i) {
    // this is when I move cursor out of that bar
    d3.select(this)
      .transition()
      .duration("50")
      .attr("width", xScaleBars.bandwidth())
      .attr("x", (a) => xScaleBars(a.timeline))
      .style("filter", "none")
      .attr("opacity", "1");
    div.transition().duration("50").style("opacity", 0);
  })

Issue I am facing我面临的问题

  • The issue is when I hover over one chart component it shows the tooltip, and than when I hover over the other both shows at the same time.问题是当我在一个图表组件上 hover 时它显示工具提示,而当我在另一个图表组件上 hover 同时显示时。 它显示像这样。

  • What I am trying to do is to show the tooltip when I hover the one bar of any chart and than hide it,I tried below code我想要做的是在我 hover 任何图表的一个条形图时显示工具提示而不是隐藏它,我尝试了下面的代码

d3.select("svg").selectAll(".tooltipCHart").remove();

But it doesn't resolve my issue, I think I am missing some small part但这并不能解决我的问题,我想我遗漏了一些小部分

here is my code sandbox which I tried这是我尝试过的代码沙箱

The problem is that you're creating a new tooltip div every time you re-render the chart.问题是每次重新渲染图表时都会创建一个新的工具提示div

A better approach is to have a hidden tooltip div in the beginning (in the render / return from your function component) and then just modify its contents and style (opacity: 1) on mouseover and mouseout .更好的方法是在开始时有一个隐藏的工具提示 div(在您的 function 组件的渲染/返回中),然后在mouseovermouseout上修改其内容和样式(不透明度:1)。 Keep track of the tooltip div using a ref .使用ref跟踪工具提示 div。

See workingcodesandbox (I only modified chart1, you can make similar changes to chart2)查看工作代码和框(我只修改了chart1 ,您可以对chart2进行类似的更改)

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

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