// Zero-dependency SVG charts — bar and multi-series line. No charting library; // this app ships zero npm deps on the frontend by design (see CONTRIBUTING.md). const CHART_PALETTE = ['var(--primary)', 'var(--accent)', 'var(--success)', 'var(--gold)', 'var(--sky)']; function niceMax(n) { if (n <= 0) return 1; const magnitude = Math.pow(10, Math.floor(Math.log10(n))); const steps = [1, 2, 2.5, 5, 10]; for (const s of steps) { if (n <= s * magnitude) return s * magnitude; } return 10 * magnitude; } function LineChart({ series, labels, width = 640, height = 260, yMin = 0, yMax, yFormat, ariaLabel }) { const padding = { top: 16, right: 16, bottom: 32, left: 40 }; const plotW = width - padding.left - padding.right; const plotH = height - padding.top - padding.bottom; const allValues = series.flatMap(s => s.values).filter(v => v != null); const computedMax = yMax ?? niceMax(Math.max(...allValues, 1)); const fmt = yFormat || (v => v); const xFor = (i) => padding.left + (labels.length > 1 ? (i / (labels.length - 1)) * plotW : plotW / 2); const yFor = (v) => padding.top + plotH - ((v - yMin) / (computedMax - yMin)) * plotH; const gridLines = 4; const gridValues = Array.from({ length: gridLines + 1 }, (_, i) => yMin + (i / gridLines) * (computedMax - yMin)); return ( {gridValues.map((v, i) => ( {fmt(v)} ))} {labels.map((l, i) => ( (labels.length <= 12 || i % Math.ceil(labels.length / 12) === 0) && ( {l} ) ))} {series.map((s, si) => { const color = s.color || CHART_PALETTE[si % CHART_PALETTE.length]; const points = s.values.map((v, i) => (v == null ? null : [xFor(i), yFor(v)])).filter(Boolean); const path = points.map((p, i) => `${i === 0 ? 'M' : 'L'}${p[0]},${p[1]}`).join(' '); return ( {points.map((p, i) => )} ); })} ); } function BarChart({ categories, values, width = 640, height = 260, color = 'var(--primary)', yFormat, ariaLabel }) { const padding = { top: 16, right: 16, bottom: 32, left: 40 }; const plotW = width - padding.left - padding.right; const plotH = height - padding.top - padding.bottom; const computedMax = niceMax(Math.max(...values, 1)); const fmt = yFormat || (v => v); const gap = 0.35; const slot = plotW / values.length; const barW = slot * (1 - gap); const gridLines = 4; const gridValues = Array.from({ length: gridLines + 1 }, (_, i) => (i / gridLines) * computedMax); return ( {gridValues.map((v, i) => { const y = padding.top + plotH - (v / computedMax) * plotH; return ( {fmt(v)} ); })} {values.map((v, i) => { const x = padding.left + i * slot + (slot - barW) / 2; const barH = (v / computedMax) * plotH; const y = padding.top + plotH - barH; return ( {categories[i]} ); })} ); } function ChartLegend({ series }) { return (
{series.map((s, i) => (
{s.name}
))}
); } Object.assign(window, { LineChart, BarChart, ChartLegend });