Last active
February 9, 2016 01:32
-
-
Save mbostock/3311124 to your computer and use it in GitHub Desktop.
Force-Directed Graph
This file contains 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
license: gpl-3.0 |
This file contains 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> | |
<meta charset="utf-8"> | |
<style> | |
.link { | |
stroke: #000; | |
} | |
.node { | |
fill: #ccc; | |
stroke: #000; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var graph = { | |
nodes: d3.range(13).map(Object), | |
links: [ | |
{source: 0, target: 1}, | |
{source: 1, target: 2}, | |
{source: 2, target: 0}, | |
{source: 1, target: 3}, | |
{source: 3, target: 2}, | |
{source: 3, target: 4}, | |
{source: 4, target: 5}, | |
{source: 5, target: 6}, | |
{source: 5, target: 7}, | |
{source: 6, target: 7}, | |
{source: 6, target: 8}, | |
{source: 7, target: 8}, | |
{source: 9, target: 4}, | |
{source: 9, target: 11}, | |
{source: 9, target: 10}, | |
{source: 10, target: 11}, | |
{source: 11, target: 12}, | |
{source: 12, target: 10} | |
] | |
}; | |
var width = 960, | |
height = 500; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var force = d3.layout.force() | |
.nodes(graph.nodes) | |
.links(graph.links) | |
.size([width, height]) | |
.charge(-200) | |
.on("tick", tick) | |
.start(); | |
var link = svg.selectAll(".link") | |
.data(graph.links) | |
.enter().append("line") | |
.attr("class", "link"); | |
var node = svg.selectAll(".node") | |
.data(graph.nodes) | |
.enter().append("circle") | |
.attr("class", "node") | |
.attr("r", 4.5); | |
function tick() { | |
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; }); | |
node.attr("cx", function(d) { return d.x; }) | |
.attr("cy", function(d) { return d.y; }); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment