当我使用D3鼠标悬停第二个SVG的元素时,如何更改SVG中元素的属性

时间:2015-08-28 13:33:55

标签: javascript select d3.js

我有两个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,然后分别使用x1Valx2Valy1Valy2Val中提供的坐标添加这些行。此SVG进入名为div的{​​{1}}。对于每一行,还有一个lines,它显示我tooltip时的行名称。一切正常。

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

mouseover

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

我的问题是:当我mySvg中的d3.select('#lines').select(whatGoesInHere?).attr("class", "sweepline").style("stroke", "red"); 圈时,如何修改我的代码以更改mySvg中一行或所有行的颜色?

1 个答案:

答案 0 :(得分:2)

只需选择#lines内的行元素:

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

我已更新了您的JSFiddle