Skip to content

Instantly share code, notes, and snippets.

@wimdows
Forked from anonymous/index.html
Created December 14, 2011 07:25
Show Gist options
  • Save wimdows/1475611 to your computer and use it in GitHub Desktop.
Save wimdows/1475611 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 = [{"name": "Germany"}, {"name": "Netherlands"}, {"name": "Denmark"}, {"name": "Poland"}];
var links = [
{"source": 0, "target": 1, "value": 2},
{"source": 1, "target": 2, "value": 3},
{"source": 2, "target": 3, "value": 1},
{"source": 0, "target": 2, "value": 2}
];
canvas = d3.select("body").append("svg:svg")
.attr("width", width)
.attr("height", height);
force = d3.layout.force()
.nodes(nodes)
.links(links)
.size([width, height])
.charge(-120)
.linkDistance(30)
.start();
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");
link = canvas.selectAll("line")
.data(links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return d.value; })
.style("stroke", "green")
.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