2014-09-01 204 views
0

我有麻煩路徑添加到由節點與全選創建AG元素:將數據添加到選擇

// define the nodes 
var node = svg.selectAll(".node").data(force.nodes()).enter() 
     .append("g").attr("id", function(d) { 
      return d.id; 
     }).attr("class", "node").style("fill", function(d) { 
      return color(d.group); 
     }).call(force.drag); 

// add the nodes 
... 

// add the links and the arrows 
var path = node.selectAll("g").data(force.links(), function(d) { 
    return d.id 
}).append("path") 
// .attr("class", function(d) { return "link " + d.type; }) 
.attr("class", "link").attr("marker-end", "url(#end)"); 

輸入的數據是這樣的:

{ 
    "nodes": [ 
    { 
     "id": 0, 
     "name": "N1", 
     "group": 4 
    }, 
    { 
     "id": 1, 
     "name": "N2", 
     "group": 1 
    }, 
    { 
     "id": 2, 
     "name": "N3", 
     "group": 1 
    } 
    ], 
    "links": [ 
    { 
     "id": 0, 
     "source": 0, 
     "target": 1 
    }, 
    { 
     "id": 0, 
     "source": 0, 
     "target": 2 
    } 
    ] 
} 

我想修改this example

我的目標是一個節點有一個鼠標懸停(.node:hover),包括所有出站鏈接。使用我的簡單數據,看起來節點N1的鼠標將包含兩個鏈接。

感謝您的幫助!

這裏是所有代碼:

<!DOCTYPE html> 
<meta charset="utf-8"> 
<script src="http://d3js.org/d3.v3.js"></script> 
<style> 
.node { 
    opacity: 0.8; 
    stroke: #fff; 
    stroke-width: 1.5px; 
} 

.node text { 
    pointer-events: none; 
    font: 10px sans-serif; 
    stroke-width: 0px; 
} 

.node:hover { 
    opacity: 1; 
} 

path.link { 
    fill: none; 
    stroke: #666; 
    stroke-width: 1.5px; 
} 
</style> 
<body> 
    <script> 
     // get the data 
     d3.json("data2.json", function(error, graph) { 

      var color = d3.scale.category20(); 
      var width = 960, height = 500; 

      var force = d3.layout.force().nodes(graph.nodes).links(graph.links) 
        .size([ width, height ]).linkDistance(300).charge(-300).on(
          "tick", tick).start(); 

      var svg = d3.select("body").append("svg").attr("width", width) 
        .attr("height", height); 

      // build the arrow. 
      svg.append("svg:defs").selectAll("marker").data([ "end" ]) // Different link/path types can be defined here 
      .enter().append("svg:marker") // This section adds in the arrows 
      .attr("id", String).attr("viewBox", "0 -5 10 10").attr("refX", 15) 
        .attr("refY", -1.5).attr("markerWidth", 6).attr(
          "markerHeight", 6).attr("orient", "auto").append(
          "svg:path").attr("d", "M0,-5L10,0L0,5"); 

      // define the nodes 
      var node = svg.selectAll(".node").data(force.nodes()).enter() 
        .append("g").attr("id", function(d) { 
         return d.id; 
        }).attr("class", "node").style("fill", function(d) { 
         return color(d.group); 
        }).call(force.drag); 

      // add the nodes 
      node.append("circle").attr("r", function(d) { 
       return 3 * d.group 
      }); 

      // add the text 
      node.append("text").attr("x", 12).attr("dy", ".35em").style(
        "color", "black").text(function(d) { 
       return d.name; 
      }); 

      // add the links and the arrows 
      var path = node.selectAll("g").data(force.links(), function(d) { 
       return d.id 
      }).append("path") 
      // .attr("class", function(d) { return "link " + d.type; }) 
      .attr("class", "link").attr("marker-end", "url(#end)"); 

      // add the curvy lines 
      function tick() { 
       path.attr("d", function(d) { 
        var dx = d.target.x - d.source.x, dy = d.target.y 
          - d.source.y, dr = Math.sqrt(dx * dx + dy * dy); 
        return "M" + d.source.x + "," + d.source.y + "A" + dr + "," 
          + dr + " 0 0,1 " + d.target.x + "," + d.target.y; 
       }); 

       node.attr("transform", function(d) { 
        return "translate(" + d.x + "," + d.y + ")"; 
       }); 
      } 

     }); 
    </script> 
</body> 
</html> 
+0

這我不清楚你問什麼問題。發生了什麼,你不想發生?你不想發生什麼事情? (我可能無法回答這個問題 - 我不太清楚 - 但我認爲澄清問題將有助於那些能夠回答的人。) – Mars 2014-09-01 15:27:03

+0

我想將所有外出鏈接附加到其節點。在svg中:路徑需要位於g元素中。 g擁有圓圈(節點)和文本。如果一切都在g中,我可以添加一個css-node:hover – myborobudur 2014-09-01 15:42:29

+0

我是一個d3新手,完全不瞭解代碼,但這裏有一個想法。不'selectAll(「g」)。data(force.links()...)'爲每個鏈接創建一個新的空的'g'元素?或者說,如果在該表達式之後添加了'.enter()',那將會是。你需要類似'.selectAll(「g」)。selectAll(「link」)。data(force.links(),...)。enter。()。append(...)'? – Mars 2014-09-01 16:50:52

回答

0

我解決我的問題與鼠標懸停及移出:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <meta name="author" content="myborobudur"> 
     <meta name="date" content="2014-09-02T00:00:00+01:00"> 
     <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
     <title>Job Recommender: d3js Graph Study</title> 
     <script src="http://d3js.org/d3.v3.js"></script> 
     <style> 
     .node { 
     opacity: 0.8; 
     stroke: #fff; 
     stroke-width: 1.5px; 
     } 
     .text { 
     pointer-events: none; 
     font: 12px sans-serif; 
     stroke-width: 0px; 
     color: #000; 
     } 
     .node:hover { 
     opacity: 1; 
     } 
     path.link { 
     fill: none; 
     stroke: #666; 
     stroke-width: 1.5px; 
     } 
     </style> 
    </head> 
    <body> 
     <h2>Job Recommender: d3js Graph Study</h2> 
     <script> 
     function selectLinks(thisObject) { 
      changeLinkStyle(thisObject, function(node, link) { 
       link.style("stroke", node.attr("fillColor")); 
       link.style("stroke-width", "10px"); 
       link.style("opacity", "0.5"); 
       link.attr("marker-end", ""); 
      }); 
     } 

     function deSelectLinks(thisObject) { 
      changeLinkStyle(thisObject, function(node, link) { 
       link.style("stroke", "#666"); 
       link.style("stroke-width", "1.5px"); 
       link.style("opacity", "1"); 
       link.attr("marker-end", "url(#end)"); 
      }); 
     } 

     function changeLinkStyle(thisObject, changeStyle) { 
      var source = d3.select(thisObject); 
      var sourceId = d3.select(thisObject).attr('id'); 
      d3.selectAll('.link').each(function(d, i) { 
       var link = d3.select(this); 
       var linkSourceId = link.attr('source'); 
       if (linkSourceId === sourceId) { 
        changeStyle.call(undefined, source, link); 
       } 
      }); 
     } 

     // get the data 
     d3.json("data.json", function(error, graph) { 

      var color = d3.scale.category20(); 
      var width = 1200, height = 800; 

      var force = d3.layout.force().nodes(graph.nodes).links(graph.links) 
          .size([ width, height ]) 
          .linkDistance(300) 
          .charge(-300) 
          .on("tick", tick) 
          .start(); 

      var svg = d3.select("body").append("svg") 
          .attr("width", width) 
          .attr("height", height); 

      // build the arrow. 
      svg.append("svg:defs").selectAll("marker") 
          .data([ "end" ]) // Different link/path types can be defined here 
          .enter().append("svg:marker") // This section adds in the arrows 
          .attr("id", String) 
          .attr("viewBox", "0 -5 10 10") 
          .attr("refX", 15) 
          .attr("refY", -1.5) 
          .attr("markerWidth", 6) 
          .attr("markerHeight", 6) 
          .attr("orient", "auto") 
          .append("svg:path") 
          .attr("d", "M0,-5L10,0L0,5"); 

      // add the links and the arrows 
      var path = svg.append("svg:g").selectAll("path") 
          .data(force.links()) 
          .enter().append("svg:path")     
          .attr("class", "link") 
          .attr("source", function(d) { return d.source.id }) 
          .attr("marker-end", "url(#end)"); 

      // define the nodes 
      var node = svg.selectAll(".node").data(force.nodes()) 
          .enter().append("g").call(force.drag); 

      // add the nodes 
      node.append("circle").attr("id", function(d) { return d.id }) 
          .attr("fillColor", function(d) { return color(d.group); }) 
          .attr("onmouseover", "selectLinks(this)") 
          .attr("onmouseout", "deSelectLinks(this)") 
          .attr("class", "node") 
          .attr("r", function(d) { return 3 * d.group }) 
          .style("fill", function(d) { return color(d.group); }); 

      // add the text 
      node.append("text").attr("x", 12) 
          .attr("dy", ".35em") 
          .attr("class", "text") 
          .text(function(d) { return d.name; }); 

      // add the curvy lines 
      function tick() { 
       path.attr("d", function(d) { 
        var dx = d.target.x - d.source.x, dy = d.target.y - d.source.y, dr = Math.sqrt(dx * dx + dy * dy); 
        return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y; 
       }); 

       node.attr("transform", function(d) { 
        return "translate(" + d.x + "," + d.y + ")"; 
       }); 
      } 

     }); 
     </script> 
    </body> 
</html>