| name | d3-force-simulation |
| description | D3.js force simulation for creating interactive bubble charts with clustering, collision detection, and physics-based positioning |
D3.js Force Simulation
Overview
Force simulation in D3 uses physics-based algorithms to position nodes (bubbles) based on forces. Perfect for creating bubble charts with natural-looking clusters.
Core Concepts
1. Creating a Simulation
const simulation = d3.forceSimulation(nodes)
.force("name", forceFunction)
.on("tick", updatePositions);
simulation.alpha(1);
2. Common Forces
forceX / forceY
Position bubbles toward target x/y coordinates (creates clustering):
const sectors = Array.from(new Set(data.map(d => d.sector)));
const sectorX = d3.scalePoint()
.domain(sectors)
.range([0, width]);
const simulation = d3.forceSimulation(nodes)
.force("x", d3.forceX()
.x(d => sectorX(d.sector))
.strength(0.05)
)
.force("y", d3.forceY()
.y(height / 2)
.strength(0.03)
);
forceCollide
Prevents bubbles from overlapping:
.force("collide", d3.forceCollide()
.radius(d => radiusScale(d.marketCap) + 2)
.strength(0.5)
)
forceManyBody
Repulsive or attractive force between all nodes:
.force("charge", d3.forceManyBody()
.strength(-50)
)
3. Tick Events
Update positions on each simulation frame:
simulation.on("tick", () => {
circles
.attr("cx", d => d.x)
.attr("cy", d => d.y);
labels
.attr("x", d => d.x)
.attr("y", d => d.y);
});
4. Preventing Bubbles from Moving Off-Screen
Fix nodes to boundaries:
simulation.on("tick", () => {
nodes.forEach(d => {
d.x = Math.max(d.radius, Math.min(width - d.radius, d.x));
d.y = Math.max(d.radius, Math.min(height - d.radius, d.y));
});
circles.attr("cx", d => d.x).attr("cy", d => d.y);
});
5. Interactive Dragging
Allow users to drag nodes:
function drag(simulation) {
return d3.drag()
.on("start", (event, d) => {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
})
.on("drag", (event, d) => {
d.fx = event.x;
d.fy = event.y;
})
.on("end", (event, d) => {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
});
}
circles.call(drag(simulation));
Complete Example: Bubble Cluster Chart
const nodes = data.map(d => ({
id: d.ticker,
sector: d.sector,
value: d.marketCap,
radius: Math.sqrt(d.marketCap / Math.PI)
}));
const width = 800, height = 600;
const sectorX = d3.scalePoint()
.domain(Array.from(new Set(data.map(d => d.sector))))
.range([100, width - 100]);
const radiusScale = d3.scaleSqrt()
.domain([0, d3.max(nodes, d => d.value)])
.range([5, 50]);
const colorScale = d3.scaleOrdinal()
.domain(Array.from(new Set(data.( d.))))
.(d3.);
simulation = d3.(nodes)
.(, d3.().( (d.)).())
.(, d3.().(height / ).())
.(, d3.().( (d.) + ))
.(, d3.().(-))
.(, {
circles
.(, d.)
.(, d.);
});
svg = d3.().()
.(, width).(, height);
circles = svg.()
.(nodes)
.()
.(, (d.))
.(, (d.));
Performance Tips
- Use
simulation.alphaMin(0.001) to stop simulation faster
- Reduce
strength values for smoother settling
- Use
radius in forceCollide matching actual visual radius
- Call
simulation.stop() when not needed to save CPU