Created
April 20, 2012 06:31
-
-
Save DarkSector/2426603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.1.3"></script> | |
| <style type="text/css"> | |
| .link { | |
| stroke: #23ff23; | |
| } | |
| .nodetext { | |
| pointer-events: none; | |
| font: 10px sans-serif; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <script type="text/javascript"> | |
| var w = 960, | |
| h = 500; | |
| var force = d3.layout.force() | |
| .gravity(.05) | |
| .distance(100) | |
| .charge(-100) | |
| .size([w, h]); | |
| var nodes = force.nodes(), | |
| links = force.links(); | |
| var vis = d3.select("body").append("svg:svg") | |
| .attr("width", w) | |
| .attr("height", h); | |
| force.on("tick", function() { | |
| vis.selectAll("g.node") | |
| .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
| vis.selectAll("line.link") | |
| .attr("x1", function(d) { return d.source.x; }) | |
| .attr("y1", function(d) { return d.source.y; }) | |
| .attr("x2", function(d) { return d.target.x; }) | |
| .attr("y2", function(d) { return d.target.y; }); | |
| }); | |
| function restart() { | |
| var link = vis.selectAll("line.link") | |
| .data(links, function(d) { return d.source.id + "-" + d.target.id; }); | |
| link.enter().insert("svg:line", "g.node") | |
| .attr("class", "link"); | |
| link.exit().remove(); | |
| var node = vis.selectAll("g.node") | |
| .data(nodes, function(d) { return d.id;}); | |
| var nodeEnter = node.enter().append("svg:g") | |
| .attr("class", "node") | |
| .call(force.drag); | |
| nodeEnter.append("svg:image") | |
| .attr("class", "circle") | |
| .attr("xlink:href", "https://d3nwyuy0nl342s.cloudfront.net/images/icons/public.png") | |
| .attr("x", "-8px") | |
| .attr("y", "-8px") | |
| .attr("width", "16px") | |
| .attr("height", "16px"); | |
| nodeEnter.append("svg:text") | |
| .attr("class", "nodetext") | |
| .attr("dx", 12) | |
| .attr("dy", ".35em") | |
| .text(function(d) { return d.id }); | |
| node.exit().remove(); | |
| force.start(); | |
| } | |
| // Add three nodes and three links. | |
| function step1() { | |
| var a = {id: "aaa"}, b = {id: "bbb"}, c = {id: "ccc"}; | |
| nodes.push(a, b, c); | |
| links.push({source: a, target: b}, {source: a, target: c}, {source: b, target: c}); | |
| restart(); | |
| } | |
| // Remove node bbb and associated links. | |
| function step2() { | |
| nodes.splice(1, 1); // remove b | |
| links.shift(); // remove a-b | |
| links.pop(); // remove b-c | |
| restart(); | |
| } | |
| // Add node bbb back. | |
| function step3() { | |
| var a = nodes[0], b = {id: "bbb"}, c = nodes[1]; | |
| nodes.push(b); | |
| links.push({source: a, target: b}, {source: b, target: c}); | |
| restart(); | |
| } | |
| restart(); | |
| setTimeout(step1, 2000); | |
| setTimeout(step2, 4000); | |
| setTimeout(step3, 6000); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment