/** * Server-rendered SVG line charts for docs: thin lines, ringed markers with a * distinct shape per series, a legend, hairline grid, and a native tooltip on * every point. Colours come from CSS classes (`.lc-s1`…`.lc-s3`, `.lc-text`, * `.lc-muted`, `.lc-grid`, `.lc-surface`), so the host page decides the theme. */ export interface Series { readonly name: string; readonly points: ReadonlyArray; } export interface LineChartSpec { readonly title: string; readonly x: Axis; readonly y: Axis; readonly series: ReadonlyArray; /** Written after a value in its label and tooltip (`min`), or nothing. */ readonly unit: string; /** Written before a value (`$`). */ readonly prefix?: string; } export interface Axis { readonly label: string; readonly ticks: ReadonlyArray; readonly log?: boolean; } const W = 480; const H = 320; const M = { top: 84, right: 28, bottom: 48, left: 52 }; const fmt = (n: number) => n >= 1000 ? `${n / 1000 >= 1000 ? `${n / 1_000_000}M` : `${n / 1000}k`}` : `${n}`; const scale = (axis: Axis, from: number, to: number) => { const lo = axis.ticks[0]!; const hi = axis.ticks[axis.ticks.length - 1]!; const t = axis.log ? (v: number) => Math.log10(v) : (v: number) => v; return (v: number) => from + ((t(v) - t(lo)) / (t(hi) - t(lo))) * (to - from); }; const esc = (s: string) => s.replace(/&/g, "&").replace(//g, ">"); /** A marker per series: circle, square, diamond. */ const marker = (i: number, x: number, y: number) => { const r = 4.5; if (i % 3 === 0) return ``; if (i % 3 === 1) return ``; const d = r + 1; return ``; }; export const renderLineChart = (spec: LineChartSpec): string => { const sx = scale(spec.x, M.left, W - M.right); const sy = scale(spec.y, H - M.bottom, M.top); const out: Array = []; out.push( ``, ``, `${esc(spec.title)}`, ); // Legend: shape and colour key, text in ink. let lx = M.left - 36; spec.series.forEach((s, i) => { out.push( `${marker(i, lx + 8, 46)}`, `${esc(s.name)}`, ); lx += 22 + s.name.length * 6 + 20; }); // Grid and axes. for (const t of spec.y.ticks) { const y = sy(t); out.push( ``, `${fmt(t)}`, ); } for (const t of spec.x.ticks) { out.push( `${fmt(t)}`, ); } out.push( `${esc(spec.x.label)}`, `${esc(spec.y.label)}`, ); // Lines, then ringed markers with tooltips, then end values that don't collide. const ends: Array<{ x: number; y: number; text: string }> = []; spec.series.forEach((s, i) => { const pts = s.points.map(([x, y]) => [sx(x), sy(y)] as const); out.push( ``, ); s.points.forEach(([x, y], j) => { const [px, py] = pts[j]!; out.push( `${marker(i, px, py)}${esc(`${s.name}: ${fmt(x)} ${spec.x.label.toLowerCase()} → ${spec.prefix ?? ""}${y}${spec.unit ? ` ${spec.unit}` : ""}`)}`, ); }); out.push(``); const [lastX, lastY] = s.points[s.points.length - 1]!; const [ex, ey] = pts[pts.length - 1]!; ends.push({ x: ex, y: ey, text: `${spec.prefix ?? ""}${lastY}${spec.unit ? ` ${spec.unit}` : ""}`, }); void lastX; }); for (const end of ends) { const clash = ends.some( (o) => o !== end && Math.abs(o.x - end.x) < 40 && Math.abs(o.y - end.y) < 14, ); if (clash) continue; // Near the right edge the label sits above and to the left of its point. const right = end.x > W - M.right - 60; out.push( `${esc(end.text)}`, ); } out.push(``); return out.join(""); }; /** Colours by role; the host maps these to its own theme. */ export const chartCss = (vars: { readonly surface: string; readonly text: string; readonly muted: string; readonly grid: string; readonly series: readonly [string, string, string]; }) => ` .lc{display:block;width:100%;height:auto;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif} .lc-surface{fill:${vars.surface}} .lc-text{fill:${vars.text}} .lc-muted{fill:${vars.muted}} .lc-title{font-size:14px;font-weight:600} .lc-small{font-size:11px} .lc-grid{stroke:${vars.grid};stroke-width:1} .lc-s1,.lc-s2,.lc-s3{stroke-width:2;stroke-linejoin:round;stroke-linecap:round} .lc-s1{stroke:${vars.series[0]};fill:${vars.series[0]}} .lc-s2{stroke:${vars.series[1]};fill:${vars.series[1]}} .lc-s3{stroke:${vars.series[2]};fill:${vars.series[2]}} .lc-s1 path[fill=none],.lc-s2 path[fill=none],.lc-s3 path[fill=none]{fill:none} .lc-pt>*{stroke:${vars.surface};stroke-width:2} .lc-label{stroke:${vars.surface};stroke-width:3;paint-order:stroke;stroke-linejoin:round} `;