Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我使用D3将鼠标移到第二个SVG的元素上时,如何更改SVG中元素的属性_Javascript_Select_D3.js - Fatal编程技术网

Javascript 当我使用D3将鼠标移到第二个SVG的元素上时,如何更改SVG中元素的属性

Javascript 当我使用D3将鼠标移到第二个SVG的元素上时,如何更改SVG中元素的属性,javascript,select,d3.js,Javascript,Select,D3.js,我有两个SVG,当我将鼠标移到另一个SVG的元素上时,我想更改其中一个SVG元素的属性。目前,我很难选择合适的元素(在下面的代码中有更详细的解释)。下面是它的JSFIDLE:下面是完整的代码: <!DOCTYPE html> <html> <head> <title>two svgs</title> <style> .sweepline{ stroke:blue

我有两个SVG,当我将鼠标移到另一个SVG的元素上时,我想更改其中一个SVG元素的属性。目前,我很难选择合适的元素(在下面的代码中有更详细的解释)。下面是它的JSFIDLE:下面是完整的代码:

<!DOCTYPE html>
<html>
  <head>
      <title>two svgs</title>
    <style>
        .sweepline{
            stroke:blue;
            stroke-width:3;
        }
        #tooltip {
        position: absolute;
        width: 200px;
        height: auto;
        padding: 10px;
        background-color: white;
        -webkit-border-radius: 10px;
        -moz-border-radius: 10px;
        border-radius: 10px;
        -webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
        -moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
        box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
        pointer-events: none;
        }

        #tooltip.hidden {
                display: none;
        }

        #tooltip p {
                margin: 0;
                font-family: sans-serif;
                font-size: 16px;
                line-height: 20px;
        }
    </style>
  </head>
  <body>
      <div id = 'lines'></div>
      <div id = 'chart'></div>
      <div id="tooltip" class="hidden">
        <p><strong>Name of line</strong></p>
        <p>that work's: <span id="nameLine">100</span></p>
      </div>
      <script src="http://d3js.org/d3.v3.min.js"></script>
      <script>
        var width = 200
        var height = 200
        //names of the lines
        var names = ['aaa', 'bbb', 'ccc']
        //coordinates of the lines
        var x1Val = [5,10,25]
        var x2Val = [50,40,90]
        var y1Val = [5,25,150]
        var y2Val = [5,100,150]
        //create SVG
        var mySvg = d3.select("#lines").append("svg")
                                     .attr("width", width)
                                     .attr("height", height);
        //add all the lines to the svg
        for (i=0; i < x1Val.length; i++){
            mySvg.append("line")
                             .attr("x1", x1Val[i])
                             .attr("y1", y1Val[i])
                             .attr("x2", x2Val[i])
                             .attr("y2", y2Val[i])
                             .attr("id", names[i])
                             .attr("class","sweepline")
                             //when 'touched', change color of line and add tooltip with name of line
                             .on("mouseover", function(d) {
                                d3.select(this).attr("class","sweepline").style("stroke", "red");
                                var xPosition = parseFloat(d3.select(this).attr("x1")) + 100;
                                var yPosition = parseFloat(d3.select(this).attr("y1")) + 50;

                                //Update the tooltip position and value
                                d3.select("#tooltip")
                                  .style("left", xPosition + "px")
                                  .style("top", yPosition + "px")
                                  .select("#nameLine")
                                  .text(d3.select(this).attr("id"));

                                //Show the tooltip
                                d3.select("#tooltip").classed("hidden", false);
                              })
                            //change the color back and hide tooltip     
                             .on("mouseout", function() {

                                d3.select(this).attr("class","sweepline").style("stroke", "blue");
                                d3.select("#tooltip").classed("hidden", true);
                             })
        }
        //create second tooltip
        var mySvg2 = d3.select("#chart").append("svg")
                                     .attr("width", width)
                                     .attr("height", height);
        mySvg2.append('circle')
                .attr("cx", 30)
                .attr("cy", 30)
                .attr("r", 20)
                .on("mouseover", function(d) {
                    d3.select(this).style("fill", "blue");
                    //d3.select('#lines').select(whatGoesInHere?).attr("class", "sweepline").style("stroke", "red");
                });

      </script>   
  </body>
</html>
但我所有的方法都失败了


我的问题是:当我将鼠标滑过
mySvg2
中的圆圈时,我必须如何修改代码才能更改
mySvg
中一行或所有行的颜色?

只需选择
中的行元素即可:

d3.select('#lines').selectAll("line").attr("class", "sweepline").style("stroke", "red");

我已经更新了您的。

太好了!那就行了!我现在投赞成票,以后再接受。只是出于兴趣:其他代码“正确”吗?我不是D3专家,只是想知道是否有更聪明的方法来做某些事情(例如,避免for循环);这总是引起怀疑。方法是通过数据绑定和选择。有很多关于这些主题的教程。关于的文章以及与之相关的参考资料将提供对此的透彻理解。我已经更新了你的,做了更多的D3ish插入线。如果你遇到进一步的问题,请发布另一个问题以获得帮助。太好了,谢谢你,这对我帮助很大,因为我还在学习D3!是的,未来几周可能会有更多的问题,所以我希望你经常上网;)如前所述,我现在发布了一个新问题,这是这一问题的后续问题。如果你也能看看这个就太好了!以下是链接:
d3.select('#lines').selectAll("line").attr("class", "sweepline").style("stroke", "red");