简体   繁体   English

D3jS显示将鼠标悬停在圆圈上的信息

[英]D3jS show info on hover over circle

I have a line graph that adds circles on the line for each data point. 我有一个折线图,可以在每个数据点的线上添加圆圈。 On hover, I would like to display the "Date" and "Close" of the data point. 悬停时,我想显示数据点的“日期”和“关闭”。

Here is the jsfiddle 这是jsfiddle

Here is what I've tried: 这是我尝试过的:

.on("mouseover", function(d) {      
        svg.transition()        
            .duration(100)      
            .style("opacity", 1);       
        svg .html(d.date + "<br/>"  + d.close)  
            .style("left", (d3.event.pageX) + "px")     
            .style("top", (d3.event.pageY - 28) + "px");    
        })                  
    .on("mouseout", function(d) {       
        svg.transition()        
            .duration(100)      
            .style("opacity", 1);   
    })

Something is happening with the code but it is very slow and doesnt show the "Date" and "Close" on mouseover. 该代码正在发生某些事情,但是它非常慢,并且鼠标悬停时未显示“日期”和“关闭”。

How can I make the "Date" and "Close" show/hide for each data point circle when hovered over? 悬停在上方时,如何显示/隐藏每个数据点圈的“日期”和“关闭”?

I think the best way to do it will be to include d3.tip for your solution. 我认为最好的方法是为您的解决方案包括d3.tip。 I use d3.tip.v0.6.3.js. 我使用d3.tip.v0.6.3.js。

what you'll have to do first is to create the tool tip view in a var: 您首先要做的是在var中创建工具提示视图:

 var toolTip = d3.tip() .attr('class', 'd3-tip') .offset([-10, 0]) .html(function(d) { return "<div style='margin-bottom: -12px; color:"+d.data.color+"'>"+d.name+"</div></br><div style='margin-bottom: -5px'>"+ d.data.label+"</div></br>"; }); 

The 'function(d)' after the '.html' returns html piece for the tooltip, just don't forget the inverted commas. “ .html”后面的“ function(d)”返回html的工具提示,只是不要忘记逗号之间的反斜杠。

right after that function you should add: 在该功能之后,您应该添加:

 svg.call(toolTip); 

that way your svg will know this tooltip var you created, you can create this var right after you svg. 这样,您的svg就会知道您创建的此工具提示var,可以在svg之后立即创建此var。

last part will be to connect this tooltip to 2 events on the d3 elements: mouseover&mouseout: 最后一部分将将此工具提示与d3元素上的2个事件相关联:mouseover&mouseout:

 .on("mouseover", toolTip.show ) .on("mouseout", toolTip.hide); 

Hope it will help 希望对你有帮助

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

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