Sunburst · How it works & code
Layered hierarchy with proportional share per ring, sector angle ∝ value
Runtime logic
Visiting `/dataviz/sunburst`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'sunburst' })`. mount resolves the variant to family `sunburst`; `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.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.arc() — Adds a circular arc (center, radius, start/end angle); ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.sunburst (L296-L341)
Code walkthrough
L298-298: Control definitions (colour/speed/size/count/extras): Starting hue, Central void, Level depth, Branch count, Sector gap °, Monochrome. L299-299: Preset buttons: name → parameter overrides (6). L300-308: Info-panel content: live rows(v), formula, notes, params, steps. L309-340: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `beginPath`, `arc`, `closePath`, `fill`, `stroke`; math via `Math.hypot`, `Math.round`, `Math.min`, `Math.max`, `Math.atan2`, `Math.cos` …; returns readouts `depth`, `leaves`, `path`, `val`, `pv`, `sp`, `sa` … to the info panel.
Principles
Sector angle = Node Value / Parent Node Value × Parent sector angle d Layer radius = [r0 + (d−1)) · Δr, r₀ + d · Δr] Parent Node Value = Σ Subpoint Value Sunburst Yes"A circle of pies":最里圈是第一层分类,往外每一圈是下一层的细分。每个扇区的 Angle is exactly the same as it is , And it must be within the perspective of the parent sector. 这样既能看出每一层的占比,又能看出层级关系,比如"Frontend → Components → A"。 Pointer suspends any sector to see the full path and ratio; Click A sector will be expanded into a full circle (down drill) and click on the centre to return to the upper level.