The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | /** |
| 2 | * Server-rendered SVG line charts for docs: thin lines, ringed markers with a |
| 3 | * distinct shape per series, a legend, hairline grid, and a native tooltip on |
| 4 | * every point. Colours come from CSS classes (`.lc-s1`…`.lc-s3`, `.lc-text`, |
| 5 | * `.lc-muted`, `.lc-grid`, `.lc-surface`), so the host page decides the theme. |
| 6 | */ |
| 7 | |
| 8 | export interface Series { |
| 9 | readonly name: string; |
| 10 | readonly points: ReadonlyArray<readonly [x: number, y: number]>; |
| 11 | } |
| 12 | |
| 13 | export interface LineChartSpec { |
| 14 | readonly title: string; |
| 15 | readonly x: Axis; |
| 16 | readonly y: Axis; |
| 17 | readonly series: ReadonlyArray<Series>; |
| 18 | /** Written after a value in its label and tooltip (`min`), or nothing. */ |
| 19 | readonly unit: string; |
| 20 | /** Written before a value (`$`). */ |
| 21 | readonly prefix?: string; |
| 22 | } |
| 23 | |
| 24 | export interface Axis { |
| 25 | readonly label: string; |
| 26 | readonly ticks: ReadonlyArray<number>; |
| 27 | readonly log?: boolean; |
| 28 | } |
| 29 | |
| 30 | const W = 480; |
| 31 | const H = 320; |
| 32 | const M = { top: 84, right: 28, bottom: 48, left: 52 }; |
| 33 | |
| 34 | const fmt = (n: number) => |
| 35 | n >= 1000 ? `${n / 1000 >= 1000 ? `${n / 1_000_000}M` : `${n / 1000}k`}` : `${n}`; |
| 36 | |
| 37 | const scale = (axis: Axis, from: number, to: number) => { |
| 38 | const lo = axis.ticks[0]!; |
| 39 | const hi = axis.ticks[axis.ticks.length - 1]!; |
| 40 | const t = axis.log ? (v: number) => Math.log10(v) : (v: number) => v; |
| 41 | return (v: number) => from + ((t(v) - t(lo)) / (t(hi) - t(lo))) * (to - from); |
| 42 | }; |
| 43 | |
| 44 | const esc = (s: string) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); |
| 45 | |
| 46 | /** A marker per series: circle, square, diamond. */ |
| 47 | const marker = (i: number, x: number, y: number) => { |
| 48 | const r = 4.5; |
| 49 | if (i % 3 === 0) return `<circle cx="${x}" cy="${y}" r="${r}"/>`; |
| 50 | if (i % 3 === 1) |
| 51 | return `<rect x="${x - r + 0.5}" y="${y - r + 0.5}" width="${2 * r - 1}" height="${2 * r - 1}" rx="1"/>`; |
| 52 | const d = r + 1; |
| 53 | return `<path d="M${x} ${y - d}L${x + d} ${y}L${x} ${y + d}L${x - d} ${y}Z"/>`; |
| 54 | }; |
| 55 | |
| 56 | export const renderLineChart = (spec: LineChartSpec): string => { |
| 57 | const sx = scale(spec.x, M.left, W - M.right); |
| 58 | const sy = scale(spec.y, H - M.bottom, M.top); |
| 59 | const out: Array<string> = []; |
| 60 | out.push( |
| 61 | `<svg viewBox="0 0 ${W} ${H}" role="img" aria-label="${esc(spec.title)}" xmlns="http://www.w3.org/2000/svg" class="lc">`, |
| 62 | `<rect class="lc-surface" width="${W}" height="${H}" rx="8"/>`, |
| 63 | `<text class="lc-text lc-title" x="${M.left - 36}" y="24">${esc(spec.title)}</text>`, |
| 64 | ); |
| 65 | // Legend: shape and colour key, text in ink. |
| 66 | let lx = M.left - 36; |
| 67 | spec.series.forEach((s, i) => { |
| 68 | out.push( |
| 69 | `<g class="lc-s${i + 1}"><line x1="${lx}" x2="${lx + 16}" y1="46" y2="46"/>${marker(i, lx + 8, 46)}</g>`, |
| 70 | `<text class="lc-muted lc-small" x="${lx + 22}" y="50">${esc(s.name)}</text>`, |
| 71 | ); |
| 72 | lx += 22 + s.name.length * 6 + 20; |
| 73 | }); |
| 74 | // Grid and axes. |
| 75 | for (const t of spec.y.ticks) { |
| 76 | const y = sy(t); |
| 77 | out.push( |
| 78 | `<line class="lc-grid" x1="${M.left}" x2="${W - M.right}" y1="${y}" y2="${y}"/>`, |
| 79 | `<text class="lc-muted lc-small" x="${M.left - 8}" y="${y + 4}" text-anchor="end">${fmt(t)}</text>`, |
| 80 | ); |
| 81 | } |
| 82 | for (const t of spec.x.ticks) { |
| 83 | out.push( |
| 84 | `<text class="lc-muted lc-small" x="${sx(t)}" y="${H - M.bottom + 18}" text-anchor="middle">${fmt(t)}</text>`, |
| 85 | ); |
| 86 | } |
| 87 | out.push( |
| 88 | `<text class="lc-muted lc-small" x="${(M.left + W - M.right) / 2}" y="${H - 10}" text-anchor="middle">${esc(spec.x.label)}</text>`, |
| 89 | `<text class="lc-muted lc-small" x="${M.left - 36}" y="${M.top - 16}">${esc(spec.y.label)}</text>`, |
| 90 | ); |
| 91 | // Lines, then ringed markers with tooltips, then end values that don't collide. |
| 92 | const ends: Array<{ x: number; y: number; text: string }> = []; |
| 93 | spec.series.forEach((s, i) => { |
| 94 | const pts = s.points.map(([x, y]) => [sx(x), sy(y)] as const); |
| 95 | out.push( |
| 96 | `<g class="lc-s${i + 1}"><path fill="none" d="${pts.map(([x, y], j) => `${j ? "L" : "M"}${x} ${y}`).join("")}"/>`, |
| 97 | ); |
| 98 | s.points.forEach(([x, y], j) => { |
| 99 | const [px, py] = pts[j]!; |
| 100 | out.push( |
| 101 | `<g class="lc-pt">${marker(i, px, py)}<title>${esc(`${s.name}: ${fmt(x)} ${spec.x.label.toLowerCase()} → ${spec.prefix ?? ""}${y}${spec.unit ? ` ${spec.unit}` : ""}`)}</title></g>`, |
| 102 | ); |
| 103 | }); |
| 104 | out.push(`</g>`); |
| 105 | const [lastX, lastY] = s.points[s.points.length - 1]!; |
| 106 | const [ex, ey] = pts[pts.length - 1]!; |
| 107 | ends.push({ |
| 108 | x: ex, |
| 109 | y: ey, |
| 110 | text: `${spec.prefix ?? ""}${lastY}${spec.unit ? ` ${spec.unit}` : ""}`, |
| 111 | }); |
| 112 | void lastX; |
| 113 | }); |
| 114 | for (const end of ends) { |
| 115 | const clash = ends.some( |
| 116 | (o) => o !== end && Math.abs(o.x - end.x) < 40 && Math.abs(o.y - end.y) < 14, |
| 117 | ); |
| 118 | if (clash) continue; |
| 119 | // Near the right edge the label sits above and to the left of its point. |
| 120 | const right = end.x > W - M.right - 60; |
| 121 | out.push( |
| 122 | `<text class="lc-text lc-small lc-label" x="${right ? end.x - 8 : end.x + 8}" y="${end.y - 10}" text-anchor="${right ? "end" : "start"}">${esc(end.text)}</text>`, |
| 123 | ); |
| 124 | } |
| 125 | out.push(`</svg>`); |
| 126 | return out.join(""); |
| 127 | }; |
| 128 | |
| 129 | /** Colours by role; the host maps these to its own theme. */ |
| 130 | export const chartCss = (vars: { |
| 131 | readonly surface: string; |
| 132 | readonly text: string; |
| 133 | readonly muted: string; |
| 134 | readonly grid: string; |
| 135 | readonly series: readonly [string, string, string]; |
| 136 | }) => ` |
| 137 | .lc{display:block;width:100%;height:auto;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif} |
| 138 | .lc-surface{fill:${vars.surface}} |
| 139 | .lc-text{fill:${vars.text}} |
| 140 | .lc-muted{fill:${vars.muted}} |
| 141 | .lc-title{font-size:14px;font-weight:600} |
| 142 | .lc-small{font-size:11px} |
| 143 | .lc-grid{stroke:${vars.grid};stroke-width:1} |
| 144 | .lc-s1,.lc-s2,.lc-s3{stroke-width:2;stroke-linejoin:round;stroke-linecap:round} |
| 145 | .lc-s1{stroke:${vars.series[0]};fill:${vars.series[0]}} |
| 146 | .lc-s2{stroke:${vars.series[1]};fill:${vars.series[1]}} |
| 147 | .lc-s3{stroke:${vars.series[2]};fill:${vars.series[2]}} |
| 148 | .lc-s1 path[fill=none],.lc-s2 path[fill=none],.lc-s3 path[fill=none]{fill:none} |
| 149 | .lc-pt>*{stroke:${vars.surface};stroke-width:2} |
| 150 | .lc-label{stroke:${vars.surface};stroke-width:3;paint-order:stroke;stroke-linejoin:round} |
| 151 | `; |