简体   繁体   English

我如何在鼠标悬停时有一条线跟随鼠标,但又有放大图表?

[英]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: 但是,当与我的图表结合时,存在以下问题:

  • the line flickers or fades while moving the mouse 移动鼠标时,线条闪烁或褪色
  • the zoom does not work with the mouse wheel (only works when the mouse is in motion) 缩放不适用于鼠标滚轮(仅在鼠标移动时有效)

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.

相关问题 d3.js v4,我如何在鼠标悬停时有一条线跟随鼠标,但又有一条圆跟随路径? - d3.js v4, how do I have a line follow the mouse on hover, but also have a circle follow the path? 如何在hover上让线跟随鼠标,但也有一个圆圈跟随D3.js v6的路径? - How to make the line follow the mouse on hover, but also have a circle follow the path with D3.js v6? 我如何在没有jQuery的情况下跟随鼠标 - How do I have an element follow the mouse without jQuery 如何用图像替换表单按钮,并在悬停时更改图像? 还精灵与单独的图像? - How do I replace a form button with an image, and have the image change on hover? Also sprites vs separate images? 如何在鼠标悬停时更改颜色的同时逐步放大以及在鼠标室上逐渐缩小和褪色的颜色 - How do I Gradual Zoom In while changing color on Mouse Hover and Gradual Zoom Out and fading color on mouse house 如何将文本悬停在自定义形状上? - How do I have text hover over a custom shape? 如何让对象面对 p5 中的鼠标? - How do I have an object face the mouse in p5? 折线图中的鼠标悬停或单击事件 - Mouse Hover or Click Event in Line Chart 如何检测鼠标的下一个悬停? - How do i detect the next hover for mouse? 如何调整图像大小并保留在悬停时更改图像的能力 - How do I have images resize and retain the ability to have them change on hover
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM