[英]Display all stacked area data in popup on mouseover in d3.js
I want to show stacked area data on mouseover in a way it's implemented in the nvd3 example: http://nvd3.org/examples/stackedArea.html but in pure d3. 我想在鼠标悬停时以nvd3示例中实现的方式显示堆叠区域数据: http ://nvd3.org/examples/stackedArea.html,但使用纯d3。 Currently I'm displaying popup and vertical line on mouse over event, but wasn't able to display all data for stacks within the popup.
目前,我在鼠标悬停事件上显示弹出窗口和垂直线,但无法显示弹出窗口中堆栈的所有数据。 Coffescript is below.
咖啡如下。
verticalLine = svg.append('line')
.attr({
'x1': 0,
'y1': 0,
'x2': 0,
'y2': height
})
.attr("stroke", "steelblue")
.attr('class', 'verticalLine')
svg.on "mousemove", () ->
xPos = d3.mouse(this)[0]
svg.select(".verticalLine").attr("transform", () ->
"translate(" + xPos + ",0)")
tooltip.transition()
.duration(200)
.style("font-size", "12px")
.style("opacity", .9)
tooltip.html("Info")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px")
svg.on "mouseout", () ->
tooltip.transition()
.duration(500)
.style "opacity", 0
Looks like not only me struggled with the problem, so I'm posting my solution below. 看起来不仅我在这个问题上苦苦挣扎,所以我在下面发布了我的解决方案。 The idea is to get an intersection of the vertical line with x axis, ie find a target date, which will then allow us to grab all other fields related to that date.
这个想法是获得垂直线与x轴的交点,即找到一个目标日期,这将使我们能够获取与该日期相关的所有其他字段。 I used d3.bisector to find an index of the target date.
我使用d3.bisector查找目标日期的索引。
xPos = d3.mouse(this)[0]
bisectDate = d3.bisector((d) -> d.date).left
date = x.invert(xPos)
currentDateIndex = bisectDate(browsers[0].values, date)
Working code is here https://jsfiddle.net/ovvn/t44qovhg/ 工作代码在这里https://jsfiddle.net/ovvn/t44qovhg/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.