Network Graph · How it works & code
Technology stack relationship network: Coulomb repulsion + spring tension in force-directed layout
Runtime logic
Visiting `/dataviz/graph`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'graph' })`. mount resolves the variant to family `graph`; `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.createRadialGradient() — Creates a radial gradient; ctx.fillRect() — Fills a rectangle; ctx.save() — Saves the drawing state (transform, styles, clip); ctx.translate() — Translates the coordinate system; ctx.scale() — Scales the coordinate system; ctx.setLineDash() — Sets the dash pattern for strokes; 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
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.graph (L797-L844)
Code walkthrough
L798-798: Control definitions (colour/speed/size/count/extras): Starting hue, Node size, Number per category, Repulsion strength, Link length, Centripetal force. L799-799: Preset buttons: name → parameter overrides (6). L800-808: Info-panel content: live rows(v), formula, notes, params, steps. L809-843: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `createRadialGradient`, `fillRect`, `save`, `translate`, `scale`, `setLineDash` …; math via `Math.exp`, `Math.max`, `Math.round`, `Math.min`, `Math.sin`, `Math.sqrt`; returns readouts `n`, `m`, `node`, `cat`, `deg`, `nb`, `ke` … to the info panel.
Principles
Expulsion (Kulen): F = k / d², All the nodes against each other Spring (huk): F = s · (d - L) Connected nodes to each other length L Heart: F = g · (Center − Position); Speed per step × Damping 0.86 Network Graph (The point is"East and west",The line is"Relation"。This is the front-end technology vault: Web Front end, connected 6 个分类,每个分类连着具体技术,虚线是技术之间的依赖。 It's not hand-made, it's Power Guide Layout Figured out: imagine each node as a small ball with the same charge (excluded from each other), and each link is a spring (close) and a little gravitational towards the center. Repeated simulations until stress is balanced and closely related nodes naturally converge. A pointer is suspended to highlight a node and its neighbours; Hold the drag Nodes, other nodes will be run with springs; Wheel Scale.