liveBook · Manning

Force simulation

As explained in D3.js in Action, Third Edition

A force simulation is an iterative physical simulation that updates graph-node positions and velocities under applied forces.

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

How D3.js in Action, Third Edition covers it

How deep it goes
Works through ticks, node coordinates, force combinations, and convergence tuning without deriving the simulation’s physics.
Examples
Builds a beeswarm of sampled values and clusters painting circles at the tips of month axes.
What it assumes
Assumes JavaScript node arrays and SVG circles while introducing simulation setup and tick callbacks through working code.
What you can do after
Apply it via D3 to update SVG nodes on each tick and combine forces for clustered or network layouts.
How it connects
Connects positioning forces, collision detection, SVG rendering, network diagrams, and Svelte reactivity.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.