我使用d3绘制线图。我想要做的是找到一个垂直移动线与两个线图的交点。d3.js:垂直移动线与两个线图的交点
现在,小提琴中的代码只能找到一个图形的交点。
rect.on('mousemove', function() {
var xPos = d3.mouse(this)[0];
d3.select(".verticalLine").attr("transform", function() {
return "translate(" + xPos + ",0)";
});
var pathLength = mainLine.node().getTotalLength();
var x = xPos;
var beginning = x,
end = pathLength,
target;
while (true) {
target = Math.floor((beginning + end)/2);
pos = mainLine.node().getPointAtLength(target);
if ((target === end || target === beginning) && pos.x !== x) {
break;
}
if (pos.x > x) end = target;
else if (pos.x < x) beginning = target;
else break; //position found
}
circle.attr("opacity", 1)
.attr("cx", x)
.attr("cy", pos.y);
console.log("x and y coordinate where vertical line intersects graph: " + [pos.x, pos.y]);
console.log("data where vertical line intersects graph: " + [xScale.invert(pos.x), yScale.invert(pos.y)]);
});
鉴于您有一行代码,您应该能够弄清楚如何为另一行执行此操作。 –
http://jsfiddle.net/2SURM/1/我已经做到了,但图2上的点并不完全在同一行上,它是在垂直线前面的一些像素,并且在第二张图上的鼠标移动不起作用,而我已经写入整个矩形的代码。 – asdfdefsad