[英]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我做了什么
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但这并不能解决我的问题,我想我遗漏了一些小部分
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 组件的渲染/返回中),然后在
mouseover
和mouseout
上修改其内容和样式(不透明度: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.