Treemap · How it works & code
Squarify algorithm: Area represents size, compare with slice method on aspect ratio.
Runtime logic
Visiting `/dataviz/treemap`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'treemap' })`. mount resolves the variant to family `treemap`; `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.fillRect() — Fills a rectangle; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; 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.treemap (L858-L902)
Code walkthrough
L859-859: Control definitions (colour/speed/size/count/extras): Starting hue, Inner margin px, Leaf count, Value fluctuation amplitude, Square algorithm, Monochrome. L860-860: Preset buttons: name → parameter overrides (6). L861-869: Info-panel content: live rows(v), formula, notes, params, steps. L870-901: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `fillRect`, `fill`, `stroke`; math via `Math.round`, `Math.exp`, `Math.sin`, `Math.max`, `Math.min`; returns readouts `n`, `cats`, `name`, `val`, `share`, `ar`, `lvl` … to the info panel.
Principles
Rectangle area = 值 / Total value × Area of canvas Long width ratio = max remuneration (w) / h, h / (w) Closer to 1 as compared Squarify: a row from big to small in value, if you add "worst-long-wire ratio" after adding a new block, or another line Treemap (Treemap)把层级数据画成嵌套的矩形:大矩形是分类,里面的小矩形是子项, Area Positive to Value 。在有限空间里能同时展示几十上百个项目的大小关系,常用于磁盘占用、预算、市值等。 How to cut the rectan Manual is the key: the simplest"Slicing method"Cutting a side on a proportional basis makes it easier to have long bars; Squarify Square algorithm 会尽量让每个矩形接近正方形,更容易比较。打开/Close"Square algorithm"Look at the change in the average width ratio on the right. Pointer suspension values; Click A catalog goes in, a little bit back.