mirror of
https://github.com/bitbaum/kits.git
synced 2026-12-04 05:17:19 +01:00
* 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>
72 lines
5.7 KiB
JavaScript
72 lines
5.7 KiB
JavaScript
"use client";
|
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import { formatX, seriesColour, xPosition } from "../index.js";
|
|
import { LineChart } from "../react/LineChart.js";
|
|
const DEFAULT = { hidden: [], from: null, to: null, mode: "value" };
|
|
const key = (x) => String(x);
|
|
/**
|
|
* A line chart the reader can work: switch series off and on, narrow the
|
|
* periods, and see change since the first period instead of the figures. The
|
|
* drawing is chartkit's own `LineChart` — the same marks, links and table on
|
|
* the server and after every change — and the view is plain data the app can
|
|
* put in its URL, so what a reader set up is a link they can share.
|
|
*/
|
|
export function LineExplorer({ series, texts, initial, defaultView, onViewChange, changeUnit, unit, yFrom, locale, ...chart }) {
|
|
const home = { ...DEFAULT, ...defaultView };
|
|
const start = { ...home, ...initial };
|
|
const [view, setView] = useState(start);
|
|
const first = useRef(true);
|
|
useEffect(() => {
|
|
if (first.current) {
|
|
first.current = false;
|
|
return;
|
|
}
|
|
onViewChange?.(view);
|
|
// Only a change of view fires it: the app's callback may be a new function each render.
|
|
}, [view]);
|
|
const coloured = useMemo(() => series.map((s, i) => ({ ...s, colour: seriesColour(s, i) })), [series]);
|
|
const periods = useMemo(() => [
|
|
...new Map(series.flatMap((s) => s.points.map((p) => [key(p.x), p.x]))).values(),
|
|
].sort((a, b) => xPosition(a) - xPosition(b)), [series]);
|
|
const index = (k, fallback) => {
|
|
const i = k === null ? -1 : periods.findIndex((p) => key(p) === k);
|
|
return i < 0 ? fallback : i;
|
|
};
|
|
const lo = index(view.from, 0);
|
|
const hi = Math.max(lo, index(view.to, periods.length - 1));
|
|
const inRange = (x) => {
|
|
const pos = xPosition(x);
|
|
return (periods.length === 0 || (pos >= xPosition(periods[lo]) && pos <= xPosition(periods[hi])));
|
|
};
|
|
const visible = coloured
|
|
.filter((s) => !view.hidden.includes(s.id))
|
|
.map((s) => {
|
|
const points = s.points.filter((p) => inRange(p.x));
|
|
if (view.mode === "value")
|
|
return { ...s, points };
|
|
const base = points[0]?.y;
|
|
return {
|
|
...s,
|
|
points: base === undefined ? [] : points.map((p) => ({ ...p, y: p.y - base })),
|
|
};
|
|
})
|
|
.filter((s) => s.points.length > 0);
|
|
const shownCount = coloured.length - view.hidden.length;
|
|
const changed = [...view.hidden].sort().join() !== [...home.hidden].sort().join() ||
|
|
lo !== index(home.from, 0) ||
|
|
hi !== index(home.to, periods.length - 1) ||
|
|
view.mode !== home.mode;
|
|
const set = (patch) => setView((v) => ({ ...v, ...patch }));
|
|
const toggle = (id) => setView((v) => ({
|
|
...v,
|
|
hidden: v.hidden.includes(id) ? v.hidden.filter((h) => h !== id) : [...v.hidden, id],
|
|
}));
|
|
const changeLabel = texts.change.replace("{period}", periods[lo] === undefined ? "" : formatX(periods[lo], locale));
|
|
return (_jsxs("div", { className: "ck-explore", children: [_jsxs("div", { className: "ck-explore-controls", children: [coloured.length > 1 && (_jsxs("fieldset", { className: "ck-explore-group", children: [_jsx("legend", { className: "ck-explore-legend", children: texts.series }), _jsx("div", { className: "ck-explore-chips", children: coloured.map((s) => {
|
|
const on = !view.hidden.includes(s.id);
|
|
const last = on && shownCount === 1;
|
|
return (_jsxs("button", { type: "button", className: "ck-explore-chip", "aria-pressed": on, "aria-disabled": last || undefined, title: last ? texts.lastSeries : undefined, onClick: () => !last && toggle(s.id), children: [_jsx("span", { className: "ck-legend-swatch", "data-series": s.colour, "aria-hidden": "true" }), _jsx("span", { className: "ck-explore-chip-label", children: s.label })] }, s.id));
|
|
}) })] })), periods.length > 2 && (_jsxs("div", { className: "ck-explore-range", children: [_jsxs("label", { className: "ck-explore-field", children: [_jsx("span", { children: texts.from }), _jsx("select", { value: key(periods[lo]), onChange: (e) => set({ from: e.target.value === key(periods[0]) ? null : e.target.value }), children: periods.slice(0, hi + 1).map((p) => (_jsx("option", { value: key(p), children: formatX(p, locale) }, key(p)))) })] }), _jsxs("label", { className: "ck-explore-field", children: [_jsx("span", { children: texts.to }), _jsx("select", { value: key(periods[hi]), onChange: (e) => set({ to: e.target.value === key(periods.at(-1)) ? null : e.target.value }), children: periods.slice(lo).map((p) => (_jsx("option", { value: key(p), children: formatX(p, locale) }, key(p)))) })] })] })), _jsxs("fieldset", { className: "ck-explore-group", children: [_jsx("legend", { className: "ck-explore-legend", children: texts.mode }), _jsxs("div", { className: "ck-explore-chips", children: [_jsx("button", { type: "button", className: "ck-explore-chip ck-explore-choice", "aria-pressed": view.mode === "value", onClick: () => set({ mode: "value" }), children: texts.value }), _jsx("button", { type: "button", className: "ck-explore-chip ck-explore-choice", "aria-pressed": view.mode === "change", onClick: () => set({ mode: "change" }), children: changeLabel })] })] }), changed && (_jsx("button", { type: "button", className: "ck-explore-reset", onClick: () => setView(home), children: texts.reset }))] }), _jsx(LineChart, { ...chart, locale: locale, series: visible, unit: view.mode === "change" ? (changeUnit ?? unit) : unit, yFrom: view.mode === "change" ? undefined : yFrom })] }));
|
|
}
|
|
//# sourceMappingURL=LineExplorer.js.map
|