Public
Edited
Oct 24, 2023
6 forks
51 stars
Insert cell
Insert cell
Insert cell
chart = {
// the default phyllotaxis arrangement is centered on <0,0> with a distance between nodes of ~10 pixels
// we will scale & translate it to fit the canvas
const scale = 1.7;
const center = [width / 2, height / 2];
const rescale = isNaN(nodes[0].x);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const node = svg.append("g")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 4)
.attr("fill", d => d.color);

const simulation = d3.forceSimulation(nodes)
.on("tick", tick)
.force("collide", d3.forceCollide().radius(d => 1 + d.r))
.force("x", d3.forceX(center[0]).strength(0.001))
.force("y", d3.forceY(center[1]).strength(0.001))
.stop();

// differ application of the forces
setTimeout(() => {
simulation.restart();
node.transition().attr("r", d => d.r);
}, 2000);

// once the arrangement is initialized, scale and translate it
if (rescale) {
for (const node of nodes) {
node.x = node.x * scale + center[0];
node.y = node.y * scale + center[1];
}
}

function tick() {
node.attr("cx", d => d.x).attr("cy", d => d.y);
}

// show the initial arrangement
tick();

return svg.node();
}
Insert cell
height = 600
Insert cell
n = 200
Insert cell
nodes = replay, Array.from({length: n}, (_, i) => ({
r: 2 * (4 + 9 * Math.random() ** 2),
color: color(i)
}))
Insert cell
color = d3.scaleSequential(d3.interpolateTurbo).domain([0, n])
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more