A force simulation lays out nodes by repeatedly adjusting their positions and velocities. In D3, `forceSimulation()` adds `x`, `y`, `vx`, and `vy` properties to each node. Each iteration is a tick: forces update the nodes, then a tick callback can copy their `x` and `y` values into SVG circles’ `cx` and `cy` attributes. The process continues until the layout converges. section 12.4.1
Combine forces to shape the visible result:
- `forceX()` and `forceY()` pull nodes toward horizontal or vertical positions. Their `strength()` setting changes how strongly each direction pulls; scale values can give categories separate targets. section 12.4.1
- `forceCollide()` keeps circles from overlapping. Pair it with a positioning force to form a beeswarm rather than a compact cluster. section 12.4.1
- For a network, centering, many-body, and link forces can shape the layout. A custom force can change node velocities when nodes reach the chart boundary. section 12.4.1
Lay out a beeswarm
Create one SVG circle per data node, but leave `cx` and `cy` for the simulation to set. A simulation can pull each node toward `xScale(d.value)`, pull all nodes toward `y = 0`, and use `forceCollide().radius(7)` to separate them. Register an `updateNetwork` tick callback that sets each circle’s position from its node’s `x` and `y`. To change the shape, adjust the targets, strengths, or collision radius. section 12.4.1
Cluster circles around month axes
When painting circles all start at the tips of their month axes, they overlap. Give each painting a simulation node; use `forceX()` and `forceY()` to target its month-axis tip and `forceCollide()` to separate nearby circles. In a Svelte reactive block, recalculate the forces when related values change. After each tick, expose the simulation’s nodes and draw circles at their updated `x` and `y`. Alpha and alpha decay can be adjusted by trial and error to make the layout converge faster. section 14.5.2
forceSimulation()
.force("x", forceX(d => xScale(d.value)) )
.force("y", forceY(0) )
.force("collide", forceCollide().radius(7) )
.nodes(sampleNodes)
.on("tick", updateNetwork);
See the compact beeswarm setup: an x target derived from a scale, a shared y target, collision spacing, nodes, and a tick callback. section 12.4.1
