Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
force layout with d3.timer instead of tick loop
var force = d3.layout.force()
.size([width, height]);
d3.json("assets/500nodes.json", function(error, graph) {
if (error) throw error;
// Task 2:
// Connect the force layout to the nodes and links in our dataset
// and start the simulation
// Task 3:
// Render the links connecting the nodes. They should be 'line' elements
// that have a stroke-width that is the square root of the link 'value'
// attribute, representing the strength of the relationship. They should bind
// to the graph edges array.
var link = svg.selectAll(".link")
.attr("class", "link")
.style("stroke-width", function(d) {
return Math.sqrt(d.value);
// Create node elements for each node. They are circle elements with a fixed
// radius of 5. They are colored by their group association and have
// drag enabled by default.
var node = svg.selectAll(".node")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(; })
// with reqAnimframe:
function animate() {
.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; });
.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