[英]how do I have a line follow the mouse on hover, but also have zoom in chart?
I have a chart with zoom. 我有一张放大的图表。 I want to add a vertical line that will follow the mouse along the graph, and display the values at the point for all the lines of the graph.
我想添加一条垂直线,将沿着鼠标沿图形移动,并在图形的所有线的点处显示值。 Found an example d3.js v4, how do I have a line follow the mouse on hover, but also have a circle follow the path?
找到了一个示例d3.js v4,我如何在鼠标悬停时有一条线跟随鼠标,但又有一条圆跟随路径?
But when combining with my chart the following problems: 但是,当与我的图表结合时,存在以下问题:
I understand that the problem is likely that when the cursor moves, it pulls a line, and is called mouseleave
event for the zoom
element. 我知道问题很可能是当光标移动时,它拉了一条线,并被称为
zoom
元素的mouseleave
事件。 I tried to move the line several pixels to the left or to the right, but this is not what I want, and it still does not work correctly. 我试图将线向左或向右移动几个像素,但这不是我想要的,并且仍然无法正常工作。
I tried to create a line not in the mouseG
element, as in the example, but on my own zoom
element. 我尝试不在示例中的
mouseG
元素中而是在自己的zoom
元素上创建一条线。 The line is no longer displayed at all. 该行不再显示。
Here is my fiddle https://jsfiddle.net/zkdxrtuc/8/ 这是我的小提琴https://jsfiddle.net/zkdxrtuc/8/
Put the line
group below the zoom
rect
. 将
line
组放在zoom
rect
下方。
Add second mouse event handlers to the zoom
rect
. 将第二个鼠标事件处理程序添加到
zoom
rect
。
To show a line set opacity to 1, to hide set opacity to 0. 要显示将不透明度设置为1的线,请隐藏将不透明度设置为0的线。
var mouseG = svg.append("g")
.attr("class", "mouse-over-effects");
svg.append("rect")
.attr("class", "zoom")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr('pointer-events', 'all')
.call(zoom);
function brushed() {
//...
}
function zoomed() {
//...
}
mouseG.append("path") // this is the black vertical line to follow mouse
.attr("class", "mouse-line")
.style("stroke", "black")
.style("stroke-width", "1px")
.style("opacity", "0");
// var lines = focus.selectAll('path');
// var mousePerLine = mouseG.selectAll('.mouse-per-line')
// .data(d3.range(lines.length))
// .enter()
// .append("g")
// .attr("class", "mouse-per-line")
// .attr('pointer-events', 'none');
// // the circle
// mousePerLine.append("circle")
// .attr("r", 7)
// .style("stroke", function(d) { return 'red'; })
// .style("fill", "none")
// .style("stroke-width", "1px")
// .style("opacity", "0");
function showLine(){
d3.select(".mouse-line").style("opacity", "1");
}
function hideLine(){
d3.select(".mouse-line").style("opacity", "0");
}
svg.select(".zoom")
.on('mouseenter.line', showLine)
.on('mouseleave.line', hideLine)
.on('mousemove.line', function() { // mouse moving over canvas
var mouse = d3.mouse(this);
//showLine();
// move the vertical line
d3.select(".mouse-line")
.attr("d", function() {
var d = "M" + (mouse[0] + margin.left) + "," + (height + margin.top);
d += " " + (mouse[0] + margin.left) + "," + margin.top;
return d;
});
// position the circle and text
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.