/**
* 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<readonly [x: number, y: number]>;
}
export interface LineChartSpec {
readonly title: string;
readonly x: Axis;
readonly y: Axis;
readonly series: ReadonlyArray<Series>;
/** Written after a value in its label and tooltip (`min`), or nothing. */
readonly unit: string;
/** Written before a value (`
.repos/alchemy-effect/website/src/components/charts/lineChart.ts at main · sailfork/t3code-github · Sailfork
). */
readonly prefix?: string;
}
export interface Axis {
readonly label: string;
readonly ticks: ReadonlyArray<number>;
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, "<").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 `<circle cx="${x}" cy="${y}" r="${r}"/>`;
if (i % 3 === 1)
return `<rect x="${x - r + 0.5}" y="${y - r + 0.5}" width="${2 * r - 1}" height="${2 * r - 1}" rx="1"/>`;
const d = r + 1;
return `<path d="M${x} ${y - d}L${x + d} ${y}L${x} ${y + d}L${x - d} ${y}Z"/>`;
};
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<string> = [];
out.push(
`<svg viewBox="0 0 ${W} ${H}" role="img" aria-label="${esc(spec.title)}" xmlns="http://www.w3.org/2000/svg" class="lc">`,
`<rect class="lc-surface" width="${W}" height="${H}" rx="8"/>`,
`<text class="lc-text lc-title" x="${M.left - 36}" y="24">${esc(spec.title)}</text>`,
);
// Legend: shape and colour key, text in ink.
let lx = M.left - 36;
spec.series.forEach((s, i) => {
out.push(
`<g class="lc-s${i + 1}"><line x1="${lx}" x2="${lx + 16}" y1="46" y2="46"/>${marker(i, lx + 8, 46)}</g>`,
`<text class="lc-muted lc-small" x="${lx + 22}" y="50">${esc(s.name)}</text>`,
);
lx += 22 + s.name.length * 6 + 20;
});
// Grid and axes.
for (const t of spec.y.ticks) {
const y = sy(t);
out.push(
`<line class="lc-grid" x1="${M.left}" x2="${W - M.right}" y1="${y}" y2="${y}"/>`,
`<text class="lc-muted lc-small" x="${M.left - 8}" y="${y + 4}" text-anchor="end">${fmt(t)}</text>`,
);
}
for (const t of spec.x.ticks) {
out.push(
`<text class="lc-muted lc-small" x="${sx(t)}" y="${H - M.bottom + 18}" text-anchor="middle">${fmt(t)}</text>`,
);
}
out.push(
`<text class="lc-muted lc-small" x="${(M.left + W - M.right) / 2}" y="${H - 10}" text-anchor="middle">${esc(spec.x.label)}</text>`,
`<text class="lc-muted lc-small" x="${M.left - 36}" y="${M.top - 16}">${esc(spec.y.label)}</text>`,
);
// 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(
`<g class="lc-s${i + 1}"><path fill="none" d="${pts.map(([x, y], j) => `${j ? "L" : "M"}${x} ${y}`).join("")}"/>`,
);
s.points.forEach(([x, y], j) => {
const [px, py] = pts[j]!;
out.push(
`<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>`,
);
});
out.push(`</g>`);
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(
`<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>`,
);
}
out.push(`</svg>`);
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}
`;
.repos/alchemy-effect/website/src/components/charts/lineChart.ts at main · sailfork/t3code-github · Sailfork
). */\n readonly prefix?: string;\n}\n\nexport interface Axis {\n readonly label: string;\n readonly ticks: ReadonlyArray\u003cnumber>;\n readonly log?: boolean;\n}\n\nconst W = 480;\nconst H = 320;\nconst M = { top: 84, right: 28, bottom: 48, left: 52 };\n\nconst fmt = (n: number) =>\n n >= 1000 ? `${n / 1000 >= 1000 ? `${n / 1_000_000}M` : `${n / 1000}k`}` : `${n}`;\n\nconst scale = (axis: Axis, from: number, to: number) => {\n const lo = axis.ticks[0]!;\n const hi = axis.ticks[axis.ticks.length - 1]!;\n const t = axis.log ? (v: number) => Math.log10(v) : (v: number) => v;\n return (v: number) => from + ((t(v) - t(lo)) / (t(hi) - t(lo))) * (to - from);\n};\n\nconst esc = (s: string) => s.replace(/&/g, \"&\").replace(/\u003c/g, \"<\").replace(/>/g, \">\");\n\n/** A marker per series: circle, square, diamond. */\nconst marker = (i: number, x: number, y: number) => {\n const r = 4.5;\n if (i % 3 === 0) return `\u003ccircle cx=\"${x}\" cy=\"${y}\" r=\"${r}\"/>`;\n if (i % 3 === 1)\n return `\u003crect x=\"${x - r + 0.5}\" y=\"${y - r + 0.5}\" width=\"${2 * r - 1}\" height=\"${2 * r - 1}\" rx=\"1\"/>`;\n const d = r + 1;\n return `\u003cpath d=\"M${x} ${y - d}L${x + d} ${y}L${x} ${y + d}L${x - d} ${y}Z\"/>`;\n};\n\nexport const renderLineChart = (spec: LineChartSpec): string => {\n const sx = scale(spec.x, M.left, W - M.right);\n const sy = scale(spec.y, H - M.bottom, M.top);\n const out: Array\u003cstring> = [];\n out.push(\n `\u003csvg viewBox=\"0 0 ${W} ${H}\" role=\"img\" aria-label=\"${esc(spec.title)}\" xmlns=\"http://www.w3.org/2000/svg\" class=\"lc\">`,\n `\u003crect class=\"lc-surface\" width=\"${W}\" height=\"${H}\" rx=\"8\"/>`,\n `\u003ctext class=\"lc-text lc-title\" x=\"${M.left - 36}\" y=\"24\">${esc(spec.title)}\u003c/text>`,\n );\n // Legend: shape and colour key, text in ink.\n let lx = M.left - 36;\n spec.series.forEach((s, i) => {\n out.push(\n `\u003cg class=\"lc-s${i + 1}\">\u003cline x1=\"${lx}\" x2=\"${lx + 16}\" y1=\"46\" y2=\"46\"/>${marker(i, lx + 8, 46)}\u003c/g>`,\n `\u003ctext class=\"lc-muted lc-small\" x=\"${lx + 22}\" y=\"50\">${esc(s.name)}\u003c/text>`,\n );\n lx += 22 + s.name.length * 6 + 20;\n });\n // Grid and axes.\n for (const t of spec.y.ticks) {\n const y = sy(t);\n out.push(\n `\u003cline class=\"lc-grid\" x1=\"${M.left}\" x2=\"${W - M.right}\" y1=\"${y}\" y2=\"${y}\"/>`,\n `\u003ctext class=\"lc-muted lc-small\" x=\"${M.left - 8}\" y=\"${y + 4}\" text-anchor=\"end\">${fmt(t)}\u003c/text>`,\n );\n }\n for (const t of spec.x.ticks) {\n out.push(\n `\u003ctext class=\"lc-muted lc-small\" x=\"${sx(t)}\" y=\"${H - M.bottom + 18}\" text-anchor=\"middle\">${fmt(t)}\u003c/text>`,\n );\n }\n out.push(\n `\u003ctext class=\"lc-muted lc-small\" x=\"${(M.left + W - M.right) / 2}\" y=\"${H - 10}\" text-anchor=\"middle\">${esc(spec.x.label)}\u003c/text>`,\n `\u003ctext class=\"lc-muted lc-small\" x=\"${M.left - 36}\" y=\"${M.top - 16}\">${esc(spec.y.label)}\u003c/text>`,\n );\n // Lines, then ringed markers with tooltips, then end values that don't collide.\n const ends: Array\u003c{ x: number; y: number; text: string }> = [];\n spec.series.forEach((s, i) => {\n const pts = s.points.map(([x, y]) => [sx(x), sy(y)] as const);\n out.push(\n `\u003cg class=\"lc-s${i + 1}\">\u003cpath fill=\"none\" d=\"${pts.map(([x, y], j) => `${j ? \"L\" : \"M\"}${x} ${y}`).join(\"\")}\"/>`,\n );\n s.points.forEach(([x, y], j) => {\n const [px, py] = pts[j]!;\n out.push(\n `\u003cg class=\"lc-pt\">${marker(i, px, py)}\u003ctitle>${esc(`${s.name}: ${fmt(x)} ${spec.x.label.toLowerCase()} → ${spec.prefix ?? \"\"}${y}${spec.unit ? ` ${spec.unit}` : \"\"}`)}\u003c/title>\u003c/g>`,\n );\n });\n out.push(`\u003c/g>`);\n const [lastX, lastY] = s.points[s.points.length - 1]!;\n const [ex, ey] = pts[pts.length - 1]!;\n ends.push({\n x: ex,\n y: ey,\n text: `${spec.prefix ?? \"\"}${lastY}${spec.unit ? ` ${spec.unit}` : \"\"}`,\n });\n void lastX;\n });\n for (const end of ends) {\n const clash = ends.some(\n (o) => o !== end && Math.abs(o.x - end.x) \u003c 40 && Math.abs(o.y - end.y) \u003c 14,\n );\n if (clash) continue;\n // Near the right edge the label sits above and to the left of its point.\n const right = end.x > W - M.right - 60;\n out.push(\n `\u003ctext 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)}\u003c/text>`,\n );\n }\n out.push(`\u003c/svg>`);\n return out.join(\"\");\n};\n\n/** Colours by role; the host maps these to its own theme. */\nexport const chartCss = (vars: {\n readonly surface: string;\n readonly text: string;\n readonly muted: string;\n readonly grid: string;\n readonly series: readonly [string, string, string];\n}) => `\n.lc{display:block;width:100%;height:auto;font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",Helvetica,Arial,sans-serif}\n.lc-surface{fill:${vars.surface}}\n.lc-text{fill:${vars.text}}\n.lc-muted{fill:${vars.muted}}\n.lc-title{font-size:14px;font-weight:600}\n.lc-small{font-size:11px}\n.lc-grid{stroke:${vars.grid};stroke-width:1}\n.lc-s1,.lc-s2,.lc-s3{stroke-width:2;stroke-linejoin:round;stroke-linecap:round}\n.lc-s1{stroke:${vars.series[0]};fill:${vars.series[0]}}\n.lc-s2{stroke:${vars.series[1]};fill:${vars.series[1]}}\n.lc-s3{stroke:${vars.series[2]};fill:${vars.series[2]}}\n.lc-s1 path[fill=none],.lc-s2 path[fill=none],.lc-s3 path[fill=none]{fill:none}\n.lc-pt>*{stroke:${vars.surface};stroke-width:2}\n.lc-label{stroke:${vars.surface};stroke-width:3;paint-order:stroke;stroke-linejoin:round}\n`;\n","preview":null,"history":null,"version":"5c011561050aaa2ba78c07771dccd533d8aa4618"},"/_session?shell=1":null}