Files
CatoandClaude Opus 5.5 aa1eb85f6c feat(chartkit): LineExplorer — a chart the reader works; numbers shown with every value a link (#5)
* feat(chartkit): LineExplorer — a chart the reader works; numbers shown with every value a link

- @bitbaum/chartkit/explore: LineExplorer ("use client") over LineChart.
  Toggle series (never the last), narrow periods, figures or change since
  the first period; the view is data the app keeps in its URL
  (initial / onViewChange). Every control 44px, every word from the app.
- LineChart `numbers`: the table behind the chart shown behind a
  disclosure, each value a link to its source. Hidden table stays text.
- TableCell can be { text, href }. LineSeries.colour keeps a colour stable.
- Browser test works the explorer at 390/1280, light/dark: toggles,
  range, change from 0, table links, 44px targets, no overflow, audit.

First of Solon's "every number clickable, slice and dice" phases; upstream
so every product gets it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(chatkit): the reader's place is where it was when history loads, not a fixed 240px

The Messenger check failed on main every run (325px): loading starts 200px
before the top (the observer's margin), so where the reader stands when the
page arrives varies. Measure the first message before and after the load;
a lost place still fails (it would jump down a whole page, off screen).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(chartkit): the explorer's figures/change switch reads as a choice, not as switched off

The unchosen option was drawn muted, like a series switched off. It is a
choice of two: the chosen one is filled, the other reads as any button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(chartkit): LineExplorer defaultView — the app's own start, what Reset returns to

A product that keeps a series off until asked for (a second grey median)
showed Reset on first load. defaultView is the starting view: Reset returns
to it and only a departure from it counts as a change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 16:41:36 +01:00

78 lines
7.2 KiB
JavaScript

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { areaPath, formatNumber, hitColumns, formatX, isDate, fixedScale, linePath, numberTicks, resolveMarkers, seriesColour, timeTicks, valueScale, xExtent, xPercent, xPosition, yPercent, } from "../index.js";
import { ChartFigure } from "./Figure.js";
/** Points with more than this many on one series draw smaller dots. */
const DENSE = 40;
/**
* One or more series on shared axes. The line is an SVG stretched to the
* plot (`preserveAspectRatio="none"`, non-scaling stroke), so it is drawn by
* the browser at any width; the axis labels and the points are HTML laid
* over it in per cent, so the text is in rem and never shrinks with the
* plot. Every point is an anchor to the page its number came from, with its
* value, period and note on hover and on keyboard focus.
*/
export function LineChart({ series, unit, caption, yFrom, yDomain, height = 208, markers = "all", area = false, locale, periodLabel = "Period", preliminaryLabel = "preliminary", referenceBands = [], numbers, }) {
for (const band of referenceBands) {
if (!Number.isFinite(band.from) || !Number.isFinite(band.to) || band.from > band.to)
throw new RangeError("Reference bands need finite bounds in ascending order");
}
const values = [
...series.flatMap((s) => s.points.map((p) => p.y)),
...referenceBands.flatMap((b) => [b.from, b.to]),
];
const scale = yDomain ? fixedScale(values, yDomain) : valueScale(values, yFrom ?? "auto");
const extent = xExtent(series);
const dated = series.some((s) => s.points.some((p) => isDate(p.x)));
const xTicks = dated
? timeTicks(extent[0], extent[1], 5, locale)
: numberTicks(extent[0], extent[1], 5, locale);
const yTicks = scale.ticks.map((v) => ({
pos: yPercent(v, scale),
label: formatNumber(v, locale),
}));
const gutter = Math.max(...yTicks.map((t) => t.label.length)) + 1;
const placed = series.map((s) => s.points.map((p) => ({ point: p, x: xPercent(p.x, extent), y: yPercent(p.y, scale) })));
const unitText = unit ? ` ${unit}` : "";
const shown = resolveMarkers(markers, Math.max(0, ...series.map((s) => s.points.length)));
const xs = [...new Set(series.flatMap((s) => s.points.map((p) => p.x)))].sort((a, b) => xPosition(a) - xPosition(b));
const table = {
head: [periodLabel, ...series.map((s) => (unit ? `${s.label} (${unit})` : s.label))],
rows: xs.map((x) => [
formatX(x, locale),
...series.map((s) => {
const p = s.points.find((q) => q.x === x);
if (!p)
return "";
const text = `${formatNumber(p.y, locale)}${p.preliminary ? ` (${preliminaryLabel})` : ""}`;
return p.href ? { text, href: p.href } : text;
}),
]),
};
return (_jsxs(ChartFigure, { kind: "line", caption: caption, table: table, numbers: numbers, style: { "--ck-gutter": `${gutter}ch`, "--ck-plot-height": `${height}px` }, children: [_jsxs("div", { className: "ck-line-plot", children: [yTicks.map((tick) => (_jsx("div", { className: "ck-line-grid", style: { top: `${tick.pos}%` }, "aria-hidden": "true", children: _jsx("span", { className: "ck-line-tick", children: tick.label }) }, tick.label))), unit && (_jsx("span", { className: "ck-line-unit", "aria-hidden": "true", children: unit })), _jsxs("svg", { className: "ck-line-svg", viewBox: "0 0 100 100", preserveAspectRatio: "none", "aria-hidden": "true", focusable: "false", children: [referenceBands.map((band, i) => (_jsx("rect", { className: "ck-reference-band", x: "0", width: "100", y: yPercent(band.to, scale), height: yPercent(band.from, scale) - yPercent(band.to, scale) }, i))), area &&
placed.map((pts, i) => (_jsx("path", { className: "ck-line-area", "data-series": seriesColour(series[i], i), d: areaPath(pts) }, `${series[i].id}:area`))), placed.map((pts, i) => (_jsx("path", { className: "ck-line-path", "data-series": seriesColour(series[i], i), d: linePath(pts), vectorEffect: "non-scaling-stroke" }, series[i].id)))] }), placed.map((pts, i) => {
const columns = shown === "all" ? undefined : hitColumns(pts);
return pts.map(({ point, x, y }, j) => (_jsx(Point, { point: point, locale: locale, preliminaryLabel: preliminaryLabel, x: x, y: y, index: seriesColour(series[i], i), dense: pts.length > DENSE, unit: unitText, label: series.length > 1 ? series[i].label : undefined, column: columns?.[j], marked: shown === "ends" && j === pts.length - 1 }, `${series[i].id}:${String(point.x)}`)));
})] }), _jsx("div", { className: "ck-line-axis", "aria-hidden": "true", children: xTicks.map((tick) => (_jsx("span", { className: `ck-line-xtick${tick.pos < 8 ? " ck-at-start" : tick.pos > 92 ? " ck-at-end" : ""}`, style: { left: `${tick.pos}%` }, children: tick.label }, tick.label + tick.pos))) }), referenceBands.length > 0 && (_jsx("ul", { className: "ck-band-labels", children: referenceBands.map((band, i) => (_jsxs("li", { children: [band.label, ": ", formatNumber(band.from, locale), "\u2013", formatNumber(band.to, locale), unitText] }, i))) })), series.length > 1 && (_jsx("ol", { className: "ck-legend", "aria-hidden": "true", children: series.map((s, i) => (_jsxs("li", { className: "ck-legend-item", children: [_jsx("span", { className: "ck-legend-swatch", "data-series": seriesColour(series[i], i) }), s.label] }, s.id))) }))] }));
}
function Point({ point, x, y, index, dense, unit, label, column, marked, locale, preliminaryLabel, }) {
const period = `${formatX(point.x, locale)}${point.preliminary ? ` · ${preliminaryLabel}` : ""}`;
const text = `${formatNumber(point.y, locale)}${unit}, ${period}${label ? ` — ${label}` : ""}${point.note ? `. ${point.note}` : ""}`;
const edge = x > 66 ? " ck-pt-end" : column && x < 33 ? " ck-pt-start" : "";
const className = column
? `ck-pt ck-pt-col${marked ? " ck-pt-marked" : ""}${edge}`
: `ck-pt${dense ? " ck-pt-dense" : ""}${edge}`;
// A hidden dot's target is a full-height strip of the plot (they tile it),
// with the dot and the tooltip placed inside it at the value.
const style = column
? {
left: `${column.left.toFixed(3)}%`,
width: `${column.width.toFixed(3)}%`,
"--ck-mx": `${(column.width > 0 ? ((x - column.left) / column.width) * 100 : 50).toFixed(2)}%`,
"--ck-y": `${y.toFixed(2)}%`,
}
: { left: `${x}%`, top: `${y}%` };
const tip = (_jsxs("span", { className: "ck-tip", role: "tooltip", children: [_jsxs("strong", { children: [formatNumber(point.y, locale), unit] }), _jsxs("span", { children: [label ? `${label} · ` : "", period] }), point.note && _jsx("span", { children: point.note })] }));
const out = point.href && /^https?:\/\//.test(point.href);
return point.href ? (_jsx("a", { href: point.href, className: className, "data-series": index, style: style, "aria-label": text, ...(out ? { target: "_blank", rel: "noopener noreferrer" } : {}), children: tip })) : (_jsx("span", { className: className, "data-series": index, style: style, tabIndex: 0, "aria-label": text, children: tip }));
}
//# sourceMappingURL=LineChart.js.map