JSFiddle - React, Tailwind, and code Playground
by bmdhacks
JavaScript
var w = 500,
h = 600,
fill = d3.scale.category10(),
nodes = d3.range(100).map(Object);
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h);
var force = d3.layout.force()
.nodes(nodes)
.links([])
.size([w, h])
.start();
var node = svg.selectAll("circle.node")
.data(nodes)
.enter().append("svg:circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 8)
.style("fill", function(d, i) { return fill(i*255/100,(100-i)*255/100,0); })
.style("stroke", function(d, i) { return d3.rgb(fill(i*255/100,(100-i)*255/100,0)).darker(2); })
.style("stroke-width", 1.5)
.call(force.drag);
force.on("tick", function(e) {
// Push different nodes in different directions for clustering.
var k = 4 * e.alpha;
// var k = 0.5;
nodes.forEach(function(o, i) {
o.y += (k * (-.7+ 0.1*i));
o.x += i & 2 ? k : -k;
});
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
d3.select("body").on("click", function() {
nodes.forEach(function(o, i) {
o.x += (Math.random() - .5) * 40;
o.y += (Math.random() - .5) * 40;
});
force.resume();
});