import { fmtValue } from '@/lib/format'; export interface BarDatum { label: string; value: number; href?: string; muted?: boolean; } /** * Horizontal bar chart in pure SVG (no chart library). Single hue; labels carry the meaning. * Values are formatted with the metric unit; bars never start anywhere but zero. */ export function BarChart({ data, unit, maxBars = 15, ariaLabel }: { data: BarDatum[]; unit?: string | null; maxBars?: number; ariaLabel: string }) { const rows = data.slice(0, maxBars); if (rows.length === 0) return null; const max = Math.max(...rows.map((r) => Math.abs(r.value)), Number.EPSILON); const rowH = 22; const labelW = 190; const valueW = 74; const width = 640; const barW = width - labelW - valueW - 8; const height = rows.length * rowH + 4; return (
{ariaLabel} {rows.map((r, i) => { const y = i * rowH + 2; const w = Math.max(1, (Math.abs(r.value) / max) * barW); const fill = r.muted ? 'var(--color-ink-4)' : 'var(--color-accent)'; return ( {r.label.length > 30 ? `${r.label.slice(0, 29)}…` : r.label} {fmtValue(r.value, unit)} ); })}
); }