加载应用…
云澜 Clyune Clyune Component Gallery · 中文

Bubble Scatter · How it works & code

Scatter plot with bubble size representing 3D data, real-time Pearson r and regression line.

Runtime logic

Visiting `/dataviz/bubbles`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'bubbles' })`. mount resolves the variant to family `bubbles`; `withControls` builds the control panel (5 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.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.setLineDash() — Sets the dash pattern for strokes; 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.bubbles (L216-L252)

Code walkthrough

L218-218: Control definitions (colour/speed/size/count/extras): Bubble Color, bubble scaling, number of sample points, correlation strength ρ, Number of Groups, regression line. L219-219: Preset buttons: name → parameter overrides (5). L220-228: Info-panel content: live rows(v), formula, notes, params, steps. L229-251: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `stroke`, `arc`, `fill` …; math via `Math.exp`, `Math.round`, `Math.sin`, `Math.cos`, `Math.sqrt`, `Math.min` …; returns readouts `x`, `y`, `v`, `g`, `n`, `r`, `a` … to the info panel.

Principles

r = Σ(x−x̄)(y−ȳ) / √(Σ(x−x̄)² · Σ(y−ȳ)²) Minimum 2x slope b = Σ(x−x̄)(y−ȳ) / Σ(x−x̄)², Intercept a = ȳ − b·x̄ Bubble Radius ∝ √Value (to allow area instead of radius to be positive) Scatter Plot 每个点代表一个样本,横坐标和纵坐标是它的两个属性,比如"Advertising fees"和"Sales volume"。Point as a whole leans to the right, indicating both Positive , It's negative on the right. It's okay to mess up. Bubble Chart And a third dimension: bubble size. Keep your eyes open Area is equal to the value (the radius takes square root), otherwise the value is exaggerated. Related coefficient r 在 −1 ~ 1 The closer you get ±1 越像一条直线;回归直线是让所有点到直线竖直距离平方和最小的那条线。数据点在轻微漂移,r It changes in real time.

Open the interactive demo · GitHub