Skip to content

Instantly share code, notes, and snippets.



Last active Mar 11, 2020
What would you like to do?
Build Your Own Graph!
license: gpl-3.0

Click to add nodes! Nodes near the cursor will be linked to the new node.

D3's force layout uses the Barnes–Hut approximation to compute repulsive charge forces between all nodes efficiently. Links are implemented as geometric constraints on top of position Verlet integration, offering greater stability. A virtual spring between each node and the center of the chart prevents nodes from drifting into space.

<!DOCTYPE html>
<meta charset="utf-8">
rect {
fill: none;
pointer-events: all;
.node {
fill: #000;
.cursor {
fill: none;
stroke: brown;
pointer-events: none;
.link {
stroke: #999;
<script src="//"></script>
var width = 960,
height = 500;
var fill = d3.scale.category20();
var force = d3.layout.force()
.size([width, height])
.nodes([{}]) // initialize with a single node
.on("tick", tick);
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height)
.on("mousemove", mousemove)
.on("mousedown", mousedown);
.attr("width", width)
.attr("height", height);
var nodes = force.nodes(),
links = force.links(),
node = svg.selectAll(".node"),
link = svg.selectAll(".link");
var cursor = svg.append("circle")
.attr("r", 30)
.attr("transform", "translate(-100,-100)")
.attr("class", "cursor");
function mousemove() {
cursor.attr("transform", "translate(" + d3.mouse(this) + ")");
function mousedown() {
var point = d3.mouse(this),
node = {x: point[0], y: point[1]},
n = nodes.push(node);
// add links to any nearby nodes
nodes.forEach(function(target) {
var x = target.x - node.x,
y = target.y - node.y;
if (Math.sqrt(x * x + y * y) < 30) {
links.push({source: node, target: target});
function tick() {
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; });
function restart() {
link =;
link.enter().insert("line", ".node")
.attr("class", "link");
node =;
node.enter().insert("circle", ".cursor")
.attr("class", "node")
.attr("r", 5)
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.