2014-09-01 167 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>