Skip to content

Instantly share code, notes, and snippets.

@mikong mikong/gist:1947753
Created Mar 1, 2012

What would you like to do?
D3 Force Test
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src=""></script>
<div id="chart"></div>
<script type="text/javascript">
var width = 400,
height = 300;
var color = d3.scale.category20();
var force = d3.layout.force()
.size([width, height]);
var svg ="#chart").append("svg")
.attr("width", width)
.attr("height", height);
var json = {"nodes":
var link = svg.selectAll("")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll("circle.node")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(; })
.text(function(d) { return; });
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; })
.attr("y2", function(d) { return; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.