Force layout + tuning
The default layout is a physics simulation: nodes repel each other, links act like springs, and a gentle gravity keeps everything centered. Three knobs shape the result — tune them to go from a tight knot to an airy, readable map.
d3ManyBodyStrength(default-150) — how hard nodes push apart. More negative spreads the graph out.d3LinkDistance(default30) — the resting length of each link.d3GravityStrengthConnected(default0.001) — the centering pull on linked nodes. Higher gathers the graph; the near-zero default lets links and repulsion settle it instead. (Lone, edge-less nodes used3GravityStrength, default0.1, so they can't drift away on their own.)
Drag a node and the simulation re-settles around it.
js
const options = {
layout: { type: 'force' },
simulation: {
// Repulsion between every pair of nodes (negative = push apart).
// Default -150. A stronger push spreads the graph out and untangles
// dense clusters — raise it when nodes overlap.
d3ManyBodyStrength: -360,
// Resting length of each link. Default 30. Longer links give connected
// nodes more breathing room.
d3LinkDistance: 70,
// Centering pull for connected nodes (the ones with edges). Defaults to a
// tiny 0.001 so links + repulsion find their own balance; raise it to gather
// the graph toward the middle instead of letting it sprawl. Lower = looser
// and airier; higher = a tighter ball.
d3GravityStrengthConnected: 0.05,
// The pull for isolated, edge-less nodes (default 0.1) is a separate knob —
// d3GravityStrength — so a lone node still can't drift off on its own.
d3GravityStrength: 0.1
}
}js
const data = {
nodes: [
{ id: 'core' },
{ id: 'api' },
{ id: 'web' },
{ id: 'mobile' },
{ id: 'auth' },
{ id: 'billing' },
{ id: 'search' },
{ id: 'index' },
{ id: 'queue' },
{ id: 'worker' },
{ id: 'db' },
{ id: 'cache' },
{ id: 'cdn' },
{ id: 'logs' }
],
edges: [
{ from: 'web', to: 'api' },
{ from: 'mobile', to: 'api' },
{ from: 'api', to: 'core' },
{ from: 'api', to: 'auth' },
{ from: 'api', to: 'billing' },
{ from: 'api', to: 'search' },
{ from: 'search', to: 'index' },
{ from: 'core', to: 'queue' },
{ from: 'queue', to: 'worker' },
{ from: 'worker', to: 'db' },
{ from: 'core', to: 'db' },
{ from: 'api', to: 'cache' },
{ from: 'web', to: 'cdn' },
{ from: 'worker', to: 'logs' },
{ from: 'auth', to: 'db' },
{ from: 'billing', to: 'db' }
]
}