Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2011 21:33
Show Gist options
  • Save anonymous/1473987 to your computer and use it in GitHub Desktop.
Save anonymous/1473987 to your computer and use it in GitHub Desktop.
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js"></script>
</head>
<body>
<script type="text/javascript">
var height = window.innerHeight;
var width = window.innerWidth;
var nodes = ["Germany", "Netherlands", "Denmark", "Poland"];
var links = [
{"source": "Germany", "target": "Denmark"},
{"source": "Netherlands", "target": "Denmark"},
{"source": "Poland", "target": "Germany"},
{"source": "Germany", "target": "Poland"}
];
var canvas = d3.select("body").append("svg:svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.nodes(nodes)
.links(links)
.size([width, height])
.charge(-120)
.linkDistance(30)
.start();
var node = canvas.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 10)
.style("fill", "#800000");
var link = canvas.selectAll("line")
.data(links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", 2)
.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; });
force.on("tick", function() {
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>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment