【问题标题】:How to change properties of elements in a SVG when I mouseover an element of a second SVG using D3当我使用 D3 将鼠标悬停在第二个 SVG 的元素上时如何更改 SVG 中元素的属性
【发布时间】:2015-08-28 13:33:55
【问题描述】:

我有两个 SVG,当我 mouseover 另一个 SVG 的一个元素时,我想更改一个 SVG 的元素的属性。目前,我正在努力选择适当的元素(在代码下面更详细地解释)。这是它的 jsfiddle:jsfiddle,这是整个代码:

<!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>

首先我创建一个名为mySvg 的SVG,并分别使用x1Val、x2Val、y1Val 和y2Val 中提供的一些坐标添加线条。此 SVG 进入名为 lines 的 div。对于每一行,还有一个tooltip,它在我mouseover 时显示行的名称。一切正常。

然后我创建了第二个名为mySvg2 的SVG,它只包含一个圆圈并进入名为chart 的div。当我mouseover这个圈子时,我想将mySvg中的线条颜色更改为红色,但是我无法正确选择这些线条。我尝试了几个版本:

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

但我所有的方法都失败了。

我的问题是:当我在mouseovermySvg2 中的圆圈时,如何修改我的代码以更改mySvg 中一行或所有行的颜色?

【问题讨论】:

    标签: javascript select d3.js


    【解决方案1】:

    只需选择#lines内的线条元素:

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

    我已经更新了你的JSFiddle。

    【讨论】:

    • 太棒了!这就是工作!我现在赞成并稍后接受。只是出于兴趣:其他代码“正确”吗?我不是 D3 专家,我只是想知道是否有更聪明的方法来做某些事情(例如避免 for 循环)。
    • 在纯 D3 代码中你很少遇到 for 循环;这总是引起怀疑。要走的路是通过数据绑定和选择。有很多关于这些主题的教程。 How Selections Work 上的文章以及其中链接的参考资料将提供对此的透彻理解。我已经更新了您的 JSFiddle 以进行更 D3ish 的线条插入。如果您遇到更多问题,请发布另一个问题以获得帮助。
    • 太好了,谢谢,这对我帮助很大,因为我还在学习 D3!是的,在接下来的几周里可能会有更多的问题,所以我希望你能经常上网;)
    • 正如宣布的那样,我现在发布了一个新问题,这是该问题的后续问题。如果你也能看看这个就好了!这是链接:new question
    猜你喜欢
    • 2015-11-28
    • 1970-01-01
    • 2018-03-12
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2021-10-03
    • 2020-07-06
    相关资源
    最近更新 更多