Force-directed Graph · How it works & code
Force-directed simulation for group clustering: charge, link, and collision forces with temperature cooling.
Runtime logic
Visiting `/dataviz/d3-force`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'd3-force' })`. mount resolves the variant to family `d3-force`; `withControls` builds the control panel (6 presets + sliders). `createCanvasStage` creates the `<canvas>` (HiDPI ≤ 2×, ResizeObserver) and the info panel. The family’s `build(S)` initialises state and returns the per-frame callback. Every `requestAnimationFrame`: read controls, advance a speed-scaled clock (dt ≤ 50 ms), run the frame callback; returned readouts go to the info panel. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. Changing parameters: colour/speed/size apply live; structural ones remount; leaving the page cancels rAF, removes listeners and frees resources.
Environment
Canvas 2D: 2D canvas drawing; requestAnimationFrame: Display-synced animation loop; ResizeObserver: Canvas follows container size; Pointer Events: Unified mouse/touch/pen events; devicePixelRatio: Crisp HiDPI rendering (capped at 2×) · react 18.3.1, react-router-dom 6.30.6, vite 5.4.21
Dependencies
react 18.3.1
Imported APIs
withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; createCanvasStage — Shared Canvas 2D stage: canvas + info panel, HiDPI, one rAF loop, pointer state, idle auto-pointer; rng — Seeded deterministic PRNG (same seed, same result); hueOf — Hue of a colour (0–360°); clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.onDown — Pointer down; S.onUp — Pointer up; S.clear — Clears with the background colour (translucent = trails); S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.fillRect() — Fills a rectangle; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fillText() — Draws filled text
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.d3-force (L904-L939)
Code walkthrough
L906-906: Control definitions (colour/speed/size/count/extras): Starting hue, Node Radius, Node count, Number of Groups, Charge Strength, Link Distance. L907-907: Preset buttons: name → parameter overrides (6). L908-916: Info-panel content: live rows(v), formula, notes, params, steps. L917-938: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `stroke`, `arc`, `fill` …; math via `Math.max`, `Math.round`, `Math.floor`, `Math.min`, `Math.sqrt`, `Math.cos` …; returns readouts `n`, `m`, `g`, `alpha`, `at`, `node`, `grp` … to the info panel.
Principles
Multiple strength: Fᵢⱼ = −Electric charge · α / d² (The same symbol) Connectivity: pull both ends to distance L Group location (forceX 58.0.) / Forcey: Pull the node to the center of the group Cool:α ← α + (αTarget − α) × 0.02, Speed per step × α Zoom This is it d3-force Approach: Three forces per node——Between All Nodes Power charge , Connected Spring pull , Prevention of duplication Collision Force . One more to pull the node Unit Centre Positioning Power(s) d3 Lee forceX / forceY),同组节点之间连线又更多,所以会聚成一团一团。 d3 One more" Temperature α ": At first α = 1, High-power, nodal flying; later α Gradually Cool , Node moves slowly until it stabilizes. When you drag a node, you start again"Heat", Let the layout adjust. The pointer suspends to see the number of groups and links to which the node is located and can be held towed.