简体   繁体   中英

How to change properties of elements in a SVG when I mouseover an element of a second SVG using D3

I have two SVGs and would like to change properties of elements of one SVG when I mouseover an element of the other SVG. Currently, I struggle with the appropriate selection of the elements (explained in more detail below the code). Here is the jsfiddle for it: jsfiddle and here is the entire code:

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

So first I create a SVG called mySvg and add the lines using some coordinates provided in x1Val , x2Val , y1Val and y2Val , respectively. This SVG goes into the div called lines . For each of the lines, there is also a tooltip which displays the name of the line when I mouseover . All that works fine.

Then I create a second SVG called mySvg2 which contains only a circle and goes into the div called chart . When I mouseover this circle, I would like to change the color of the lines in mySvg to red, however, I fail to select these lines correctly. I tried several versions of this:

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

but all my approaches failed.

My question is: How do I have to modify my code in order to change the colors of one or all lines in mySvg when I mouseover the circle in mySvg2 ?

Just select the line elements which are inside #lines :

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

I have updated your JSFiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM