Parallel Coordinates · How it works & code
Multi-dimensional data fold + pointer filter, counting selected bars.
Runtime logic
Visiting `/dataviz/parallel`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'parallel' })`. mount resolves the variant to family `parallel`; `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. 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.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.lineTo() — Adds a straight line to a point; ctx.moveTo() — Moves the pen without drawing; ctx.stroke() — Strokes the current path; ctx.fill() — Fills the current path; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.parallel (L430-L462)
Code walkthrough
L431-431: Control definitions (colour/speed/size/count/extras): Selected Line Color, Line Thickness, Number of Axes, Number of Data Lines, Selection Range Height, Show Only Selected. L432-432: Preset buttons: name → parameter overrides (6). L433-441: Info-panel content: live rows(v), formula, notes, params, steps. L442-461: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `lineTo`, `moveTo`, `stroke`, `fill`; math via `Math.round`, `Math.sin`; returns readouts `z`, `ph`, `k`, `n`, `ax`, `rg`, `sel` … to the info panel.
Principles
One vertical axis per dimension: y = Axis Bottom − (值 − min) / (max − min) × Axis height A data = a line that connects the points on each axis in turn Snippet: Saves the line on the selected axis for the value ∈ [bottom, upper] When the data has many dimensions (price, continuation, weight)…), the normal coordinate system is only two. Parallel Coordinates Make every dimension one Straight axis , One data is a line through all the axes. Look at the line between the adjacent two axes: most parallels indicate two dimensions Positive , Mostly cross-cut X Description negative correlation 。 Which axis is the pointer?" Selection "一段区间,只有穿过这段区间的折线会被点亮,右侧显示选中了多少条以及它们在各轴上的平均值。