Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Ten bars never leave the DOM. Their rank sets a translateY, so a transition slides each one to its row as the values change.
import 'nuclo' const W = 320 const ROW = 24 const TOP = 6 const LABEL = 68 const H = TOP * ROW + 6 const KEYS = 13 const FIRST = 2012 const names = ['Atlas', 'Borealis', 'Cobalt', 'Delta', 'Ember', 'Fjord', 'Garnet', 'Helix', 'Indigo', 'Juno'] // Seeded random numbers, so the first frame is always the same. let seed = 23 const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647 // One value per name per year. Each name grows at its own pace. const series = names.map(() => { let v = 20 + random() * 60 const g = -0.04 + random() * 0.34 return Array.from({ length: KEYS }, () => { const out = v v *= 1 + g + (random() - 0.5) * 0.3 return out }) }) let t = 0 // 0 to 12, with decimals: how far between two years let playing = true let value: number[] = [] let rank: number[] = [] let peak = 1 // Interpolate the years, then rank the interpolated values. function measure() { const k = Math.min(KEYS - 2, Math.floor(t)) value = series.map((s) => s[k] + (s[k + 1] - s[k]) * (t - k)) const order = value.map((_, i) => i).sort((p, q) => value[q] - value[p]) order.forEach((item, r) => (rank[item] = r)) peak = value[order[0]] } measure() const year = () => FIRST + Math.floor(t + 0.001) const barWidth = (i: number) => Math.max(2, (value[i] / peak) * (W - LABEL - 44)) // The list never reorders: every bar keeps its place in the DOM // and moves with translateY(rank), so a CSS transition slides it // to its new row. Names below the top six fade out below the chart. // The bar's width is a plain function of the value, so it follows // every frame. The frame loop updates only the 'race' scope. export const Race = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px', width: '100%', maxWidth: '380px' } }, scope('race'), svgSvg( { viewBox: '0 0 320 150', role: 'img', 'aria-label': 'Bar chart race', style: { width: '100%', height: 'auto', overflow: 'visible' }, onMount: () => { let id = requestAnimationFrame(function frame() { if (playing) { t = Math.min(KEYS - 1, t + 0.012) if (t >= KEYS - 1) playing = false measure() update('race') } id = requestAnimationFrame(frame) }) return () => cancelAnimationFrame(id) }, }, // The big year sits behind the bars. textSvg( { x: W - 4, y: H - 8, 'text-anchor': 'end', 'font-size': 54, 'font-weight': 800, 'fill-opacity': 0.12, 'pointer-events': 'none' }, () => year(), ), ...names.map((name, i) => gSvg( { opacity: () => (rank[i] < TOP ? 1 : 0), style: () => ({ transition: 'transform 0.45s ease, opacity 0.3s ease', transform: 'translateY(' + Math.min(rank[i], TOP) * ROW + 'px)', }), }, textSvg({ x: LABEL - 6, y: 15, 'text-anchor': 'end', 'font-size': 11, 'font-weight': 600 }, name), rectSvg({ x: LABEL, y: 3, height: ROW - 6, rx: 4, fill: 'hsl(' + ((i * 47 + 12) % 360) + ' 72% 52%)', width: () => barWidth(i), }), textSvg( { y: 15, 'font-family': 'ui-monospace, monospace', 'font-size': 11, fill: '#8a8a8a', x: () => LABEL + barWidth(i) + 5 }, () => Math.round(value[i]), ), ), ), ), div( { style: { display: 'flex', flexWrap: 'wrap', alignItems: 'center', justifyContent: 'center', gap: '8px' } }, button(() => (playing ? 'Pause' : t >= KEYS - 1 ? 'Replay' : 'Play'), { onClick: () => { if (t >= KEYS - 1) { t = 0; measure() } playing = !playing update('race') }, }), input({ type: 'range', min: '0', max: String((KEYS - 1) * 100), step: '1', 'aria-label': 'Year', style: { width: '100%', maxWidth: '320px' }, value: () => String(Math.round(t * 100)), onInput: (e) => { t = Number(e.currentTarget.value) / 100 playing = false measure() update('race') }, }), ), div( { style: { fontFamily: 'ui-monospace, monospace', fontSize: '12px', color: '#8a8a8a', textAlign: 'center' } }, () => year() + ' · ' + names[rank.indexOf(0)] + ' leads · sample data', ), )