Examples
Practical examples। Live demos।
कोई example चुनिए और फ़ुल-स्क्रीन में खोलिए: live preview और source code साथ-साथ।
कोई example चुनिए और फ़ुल-स्क्रीन में खोलिए: live preview और source code साथ-साथ।
चार stacked bands, stream और percent layout के बीच अपना आकार बदलते हैं। series छुपाने पर उसके band की thickness बस zero हो जाती है, इसलिए हर path के commands का structure वही रहता है।
import 'nuclo' type Mode = 'stacked' | 'stream' | 'percent' const W = 320 const H = 170 const PT = 6 // plot top const PH = 142 // plot height const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] const names = ['Core', 'Router', 'Docs', 'Tools'] const colors = ['#ff3f00', '#2563eb', '#ff8a00', '#6b7280'] // A seeded random walk per series, so every reload draws the same chart. let seed = 3 const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647 const clamp = (v: number, min: number, max: number) => Math.max(min, Math.min(max, v)) const data = names.map((_, k) => { let v = 16 + k * 6 + random() * 8 return months.map(() => Math.round((v = clamp(v + (random() - 0.45) * 10, 6, 52)))) }) const on = names.map(() => true) // which series are shown let mode: Mode = 'stacked' let hover: number | null = null // the month under the pointer const x = (i: number) => 28 + (i / (months.length - 1)) * (W - 34) // Every series is a band between a low edge and a high edge. // A hidden series only gets zero thickness, so every path keeps // the same shape and the CSS transition on d can morph it. // stacked: the edges pile up from zero // stream: the pile is centered on zero // percent: each month is divided by its total function layout() { const rows = months.map((_, i) => data.map((s, k) => (on[k] ? s[i] : 0))) const totals = rows.map((r) => r.reduce((a, b) => a + b, 0)) const peak = Math.max(1, ...totals) const lo = names.map((): number[] => []) const hi = names.map((): number[] => []) rows.forEach((row, i) => { const unit = mode === 'percent' ? totals[i] || 1 : 1 let acc = mode === 'stream' ? -totals[i] / 2 : 0 row.forEach((v, k) => { lo[k][i] = acc / unit acc += v hi[k][i] = acc / unit }) }) const [d0, d1] = mode === 'percent' ? [0, 1] : mode === 'stream' ? [-peak / 2, peak / 2] : [0, peak] return { lo, hi, d0, d1 } } const py = (v: number, d0: number, d1: number) => PT + (1 - (v - d0) / (d1 - d0)) * PH // Curves with horizontal tangents never overshoot the data. function smooth(pts: number[][], from: 'M' | 'L') { let d = from + pts[0][0].toFixed(1) + ',' + pts[0][1].toFixed(1) for (let i = 1; i < pts.length; i++) { const mid = ((pts[i - 1][0] + pts[i][0]) / 2).toFixed(1) d += ' C' + mid + ',' + pts[i - 1][1].toFixed(1) + ' ' + mid + ',' + pts[i][1].toFixed(1) + ' ' + pts[i][0].toFixed(1) + ',' + pts[i][1].toFixed(1) } return d } // The high edge forward, the low edge back, then close. function band(k: number) { const { lo, hi, d0, d1 } = layout() const top = hi[k].map((v, i) => [x(i), py(v, d0, d1)]) const bottom = lo[k].map((v, i) => [x(i), py(v, d0, d1)]).reverse() return smooth(top, 'M') + ' ' + smooth(bottom, 'L') + ' Z' } // The y of a grid line, and the label that goes with it. function level(f: number) { const { d0, d1 } = layout() return py(d0 + (d1 - d0) * f, d0, d1) } function label(f: number) { const { d0, d1 } = layout() if (mode === 'stream') return '' return mode === 'percent' ? Math.round(f * 100) + '%' : String(Math.round(d0 + (d1 - d0) * f)) } const caption = () => hover === null ? 'Hover a month. Click a name to hide it.' : months[hover] + ' · ' + names.map((n, k) => (on[k] ? n + ' ' + data[k][hover!] : '')).filter(Boolean).join(' · ') const row = { display: 'flex', flexWrap: 'wrap', justifyContent: 'center', gap: '6px' } export const StackedArea = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px', width: '100%', maxWidth: '380px' } }, svgSvg( { viewBox: '0 0 320 170', role: 'img', 'aria-label': 'Stacked area chart', style: { width: '100%', overflow: 'visible' }, onPointerLeave: () => { hover = null; update() }, }, // grid lines and their labels follow the layout ...[0, 0.25, 0.5, 0.75, 1].map((f) => gSvg( lineSvg({ x1: 24, x2: W, stroke: '#ddd', 'stroke-dasharray': '2 4', y1: () => level(f) + 0.5, y2: () => level(f) + 0.5 }), textSvg({ x: 20, 'text-anchor': 'end', 'font-size': 11, 'fill-opacity': 0.6, y: () => level(f) + 4 }, () => label(f)), ), ), ...names.map((_, k) => pathSvg({ fill: colors[k], 'fill-opacity': 0.88, stroke: '#fff', 'stroke-width': 1, style: { transition: 'd 0.5s cubic-bezier(0.22, 1, 0.36, 1)' }, d: () => band(k), }), ), // a dashed line that follows the pointer lineSvg({ x1: 0, x2: 0, y1: PT, y2: PT + PH, stroke: '#888', 'stroke-dasharray': '3 3', opacity: () => (hover === null ? 0 : 1), style: () => ({ pointerEvents: 'none', transition: 'transform 0.12s ease-out', transform: 'translateX(' + x(hover ?? 0) + 'px)' }), }), ...months.map((m, i) => textSvg({ x: x(i), y: H - 4, 'text-anchor': 'middle', 'font-size': 11, 'fill-opacity': 0.6, opacity: i % 2 ? 0 : 1 }, m), ), // a transparent rect over the plot reads the pointer and picks the month rectSvg({ x: 0, y: 0, width: W, height: PT + PH, fill: 'transparent', style: { cursor: 'crosshair', touchAction: 'none' }, onPointerMove: (e) => { const box = (e.currentTarget as Element).getBoundingClientRect() hover = Math.round(clamp((e.clientX - box.left) / box.width, 0, 1) * (months.length - 1)) update() }, }), ), div( { style: { fontFamily: 'ui-monospace, monospace', fontSize: '12px', textAlign: 'center', minHeight: '36px', opacity: '0.7' } }, caption, ), // one chip per series: click to hide or show it div( { style: row }, ...names.map((name, k) => button( { 'aria-pressed': () => String(on[k]), style: () => ({ display: 'inline-flex', alignItems: 'center', gap: '6px', borderRadius: '15px', opacity: on[k] ? '1' : '0.45' }), onClick: () => { on[k] = !on[k]; update() }, }, span({ style: { width: '9px', height: '9px', borderRadius: '50%', backgroundColor: colors[k] } }), name, ), ), ), // the layout buttons: the pressed one gets an outline div( { style: row }, ...(['stacked', 'stream', 'percent'] as const).map((m) => button(m, { 'aria-pressed': () => String(mode === m), style: () => ({ outline: mode === m ? '2px solid currentColor' : 'none', outlineOffset: '2px' }), onClick: () => { mode = m; update() }, }), ), ), )