@bigpixelrocket/uiok 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/NOTICE +569 -0
- package/README.md +44 -2
- package/dist/VisuallyHidden.d.ts +13 -0
- package/dist/VisuallyHidden.js +13 -0
- package/dist/activity/ActivityBar.d.ts +29 -0
- package/dist/activity/ActivityBar.js +21 -0
- package/dist/activity/activity.css +43 -0
- package/dist/activity/index.d.ts +2 -0
- package/dist/activity/index.js +2 -0
- package/dist/button/Button.d.ts +26 -0
- package/dist/button/Button.js +14 -0
- package/dist/button/IconButton.d.ts +18 -0
- package/dist/button/IconButton.js +14 -0
- package/dist/button/button.css +95 -0
- package/dist/button/index.d.ts +3 -0
- package/dist/button/index.js +3 -0
- package/dist/charts/Drawing.d.ts +27 -0
- package/dist/charts/Drawing.js +117 -0
- package/dist/charts/Drawing.logic.d.ts +2 -0
- package/dist/charts/Drawing.logic.js +22 -0
- package/dist/charts/Line.logic.d.ts +33 -0
- package/dist/charts/Line.logic.js +72 -0
- package/dist/charts/LineChart.d.ts +34 -0
- package/dist/charts/LineChart.js +100 -0
- package/dist/charts/Pie.logic.d.ts +16 -0
- package/dist/charts/Pie.logic.js +28 -0
- package/dist/charts/PieChart.d.ts +20 -0
- package/dist/charts/PieChart.js +16 -0
- package/dist/charts/ResultChart.d.ts +47 -0
- package/dist/charts/ResultChart.js +175 -0
- package/dist/charts/ResultChart.logic.d.ts +42 -0
- package/dist/charts/ResultChart.logic.js +165 -0
- package/dist/charts/Scatter.logic.d.ts +55 -0
- package/dist/charts/Scatter.logic.js +134 -0
- package/dist/charts/ScatterChart.d.ts +36 -0
- package/dist/charts/ScatterChart.js +99 -0
- package/dist/charts/charts.css +310 -0
- package/dist/charts/index.d.ts +14 -0
- package/dist/charts/index.js +11 -0
- package/dist/charts/seriesColour.d.ts +2 -0
- package/dist/charts/seriesColour.js +8 -0
- package/dist/clock/index.d.ts +2 -0
- package/dist/clock/index.js +2 -0
- package/dist/clock/useClock.d.ts +14 -0
- package/dist/clock/useClock.js +57 -0
- package/dist/datetime/DateTimePicker.d.ts +50 -0
- package/dist/datetime/DateTimePicker.js +98 -0
- package/dist/datetime/DateTimePicker.logic.d.ts +59 -0
- package/dist/datetime/DateTimePicker.logic.js +103 -0
- package/dist/datetime/datetime.css +115 -0
- package/dist/datetime/index.d.ts +7 -0
- package/dist/datetime/index.js +7 -0
- package/dist/dialog/ConfirmDialog.d.ts +73 -0
- package/dist/dialog/ConfirmDialog.js +30 -0
- package/dist/dialog/DialogLegend.d.ts +21 -0
- package/dist/dialog/DialogLegend.js +28 -0
- package/dist/dialog/DialogLegend.logic.d.ts +145 -0
- package/dist/dialog/DialogLegend.logic.js +71 -0
- package/dist/dialog/ModalDialog.d.ts +101 -0
- package/dist/dialog/ModalDialog.js +184 -0
- package/dist/dialog/dialog.css +283 -0
- package/dist/dialog/index.d.ts +8 -0
- package/dist/dialog/index.js +8 -0
- package/dist/filter/FilterEscape.d.ts +16 -0
- package/dist/filter/FilterEscape.js +15 -0
- package/dist/filter/FilterIcon.d.ts +8 -0
- package/dist/filter/FilterIcon.js +11 -0
- package/dist/filter/filter.css +42 -0
- package/dist/filter/index.d.ts +3 -0
- package/dist/filter/index.js +3 -0
- package/dist/grid/DataGrid.d.ts +149 -0
- package/dist/grid/DataGrid.js +373 -0
- package/dist/grid/DataGrid.logic.d.ts +57 -0
- package/dist/grid/DataGrid.logic.js +121 -0
- package/dist/grid/grid.css +206 -0
- package/dist/grid/index.d.ts +7 -0
- package/dist/grid/index.js +7 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +32 -0
- package/dist/keyboard/BrowserKeys.d.ts +15 -0
- package/dist/keyboard/BrowserKeys.js +28 -0
- package/dist/keyboard/KeyChord.logic.d.ts +25 -0
- package/dist/keyboard/KeyChord.logic.js +50 -0
- package/dist/keyboard/Keyboard.logic.d.ts +84 -0
- package/dist/keyboard/Keyboard.logic.js +137 -0
- package/dist/keyboard/OverlayWatch.d.ts +28 -0
- package/dist/keyboard/OverlayWatch.js +119 -0
- package/dist/keyboard/PageSelectAll.d.ts +14 -0
- package/dist/keyboard/PageSelectAll.js +22 -0
- package/dist/keyboard/index.d.ts +9 -0
- package/dist/keyboard/index.js +9 -0
- package/dist/keychord/KeyChord.d.ts +16 -0
- package/dist/keychord/KeyChord.js +55 -0
- package/dist/keychord/index.d.ts +2 -0
- package/dist/keychord/index.js +2 -0
- package/dist/keychord/keychord.css +41 -0
- package/dist/labels/UiokLabels.d.ts +41 -0
- package/dist/labels/UiokLabels.js +31 -0
- package/dist/labels/UiokProvider.d.ts +44 -0
- package/dist/labels/UiokProvider.js +54 -0
- package/dist/labels/charts.d.ts +34 -0
- package/dist/labels/charts.js +15 -0
- package/dist/labels/dateTime.d.ts +32 -0
- package/dist/labels/dateTime.js +14 -0
- package/dist/labels/dialog.d.ts +18 -0
- package/dist/labels/dialog.js +6 -0
- package/dist/labels/index.d.ts +13 -0
- package/dist/labels/index.js +13 -0
- package/dist/labels/keys.d.ts +28 -0
- package/dist/labels/keys.js +53 -0
- package/dist/labels/legend.d.ts +21 -0
- package/dist/labels/legend.js +7 -0
- package/dist/labels/listing.d.ts +16 -0
- package/dist/labels/listing.js +8 -0
- package/dist/labels/pager.d.ts +32 -0
- package/dist/labels/pager.js +20 -0
- package/dist/labels/palette.d.ts +17 -0
- package/dist/labels/palette.js +6 -0
- package/dist/labels/selectAll.d.ts +15 -0
- package/dist/labels/selectAll.js +6 -0
- package/dist/labels/terminal.d.ts +25 -0
- package/dist/labels/terminal.js +9 -0
- package/dist/legend/KeyLegend.logic.d.ts +84 -0
- package/dist/legend/KeyLegend.logic.js +109 -0
- package/dist/legend/KeyLegendStrip.d.ts +36 -0
- package/dist/legend/KeyLegendStrip.js +82 -0
- package/dist/legend/index.d.ts +3 -0
- package/dist/legend/index.js +3 -0
- package/dist/legend/legend.css +148 -0
- package/dist/listing/CurrentDot.d.ts +15 -0
- package/dist/listing/CurrentDot.js +13 -0
- package/dist/listing/Listing.d.ts +230 -0
- package/dist/listing/Listing.js +265 -0
- package/dist/listing/PathParts.logic.d.ts +17 -0
- package/dist/listing/PathParts.logic.js +18 -0
- package/dist/listing/index.d.ts +10 -0
- package/dist/listing/index.js +10 -0
- package/dist/listing/listing.css +867 -0
- package/dist/menu/MenuQuery.logic.d.ts +69 -0
- package/dist/menu/MenuQuery.logic.js +75 -0
- package/dist/menu/fuzzyMatch.d.ts +31 -0
- package/dist/menu/fuzzyMatch.js +296 -0
- package/dist/menu/index.d.ts +9 -0
- package/dist/menu/index.js +9 -0
- package/dist/menu/menu.css +203 -0
- package/dist/menu/useBellBeat.d.ts +8 -0
- package/dist/menu/useBellBeat.js +26 -0
- package/dist/namecut/NameCut.d.ts +22 -0
- package/dist/namecut/NameCut.js +28 -0
- package/dist/namecut/NameCut.logic.d.ts +51 -0
- package/dist/namecut/NameCut.logic.js +120 -0
- package/dist/namecut/index.d.ts +3 -0
- package/dist/namecut/index.js +3 -0
- package/dist/namecut/namecut.css +67 -0
- package/dist/pager/Pager.d.ts +37 -0
- package/dist/pager/Pager.js +38 -0
- package/dist/pager/Pager.logic.d.ts +13 -0
- package/dist/pager/Pager.logic.js +63 -0
- package/dist/pager/index.d.ts +7 -0
- package/dist/pager/index.js +7 -0
- package/dist/pager/pager.css +62 -0
- package/dist/palette/Palette.d.ts +79 -0
- package/dist/palette/Palette.js +60 -0
- package/dist/palette/index.d.ts +6 -0
- package/dist/palette/index.js +6 -0
- package/dist/palette/palette.css +159 -0
- package/dist/pane/PaneActions.d.ts +104 -0
- package/dist/pane/PaneActions.js +62 -0
- package/dist/pane/PaneFrame.d.ts +51 -0
- package/dist/pane/PaneFrame.js +28 -0
- package/dist/pane/PaneKeys.d.ts +41 -0
- package/dist/pane/PaneKeys.js +104 -0
- package/dist/pane/PaneKeys.logic.d.ts +95 -0
- package/dist/pane/PaneKeys.logic.js +109 -0
- package/dist/pane/index.d.ts +11 -0
- package/dist/pane/index.js +11 -0
- package/dist/pane/usePaneKeys.d.ts +141 -0
- package/dist/pane/usePaneKeys.js +313 -0
- package/dist/run/RunHeader.d.ts +50 -0
- package/dist/run/RunHeader.js +31 -0
- package/dist/run/RunProgress.logic.d.ts +29 -0
- package/dist/run/RunProgress.logic.js +34 -0
- package/dist/run/index.d.ts +3 -0
- package/dist/run/index.js +3 -0
- package/dist/run/run.css +190 -0
- package/dist/segmented/SegmentedChoice.d.ts +22 -0
- package/dist/segmented/SegmentedChoice.js +31 -0
- package/dist/segmented/index.d.ts +2 -0
- package/dist/segmented/index.js +2 -0
- package/dist/segmented/segmented.css +49 -0
- package/dist/selectall/CheckRange.logic.d.ts +8 -0
- package/dist/selectall/CheckRange.logic.js +23 -0
- package/dist/selectall/SelectAllBar.d.ts +25 -0
- package/dist/selectall/SelectAllBar.js +24 -0
- package/dist/selectall/index.d.ts +7 -0
- package/dist/selectall/index.js +7 -0
- package/dist/selectall/selectall.css +31 -0
- package/dist/store/KeepUnchanged.logic.d.ts +19 -0
- package/dist/store/KeepUnchanged.logic.js +33 -0
- package/dist/store/SameProps.d.ts +15 -0
- package/dist/store/SameProps.js +46 -0
- package/dist/store/ValueStore.d.ts +17 -0
- package/dist/store/ValueStore.js +23 -0
- package/dist/store/index.d.ts +8 -0
- package/dist/store/index.js +8 -0
- package/dist/store/useStableHandlers.d.ts +13 -0
- package/dist/store/useStableHandlers.js +22 -0
- package/dist/styles.css +85 -0
- package/dist/tabs/PageTabs.d.ts +38 -0
- package/dist/tabs/PageTabs.js +33 -0
- package/dist/tabs/index.d.ts +2 -0
- package/dist/tabs/index.js +2 -0
- package/dist/tabs/tabs.css +64 -0
- package/dist/terminal/Terminal.d.ts +104 -0
- package/dist/terminal/Terminal.js +309 -0
- package/dist/terminal/Terminal.logic.d.ts +66 -0
- package/dist/terminal/Terminal.logic.js +156 -0
- package/dist/terminal/TerminalMenu.d.ts +36 -0
- package/dist/terminal/TerminalMenu.js +87 -0
- package/dist/terminal/TerminalMenu.logic.d.ts +78 -0
- package/dist/terminal/TerminalMenu.logic.js +68 -0
- package/dist/terminal/TerminalScrollback.d.ts +10 -0
- package/dist/terminal/TerminalScrollback.js +11 -0
- package/dist/terminal/ghostty/LICENSE +21 -0
- package/dist/terminal/ghostty/core.d.ts +315 -0
- package/dist/terminal/ghostty/core.js +1189 -0
- package/dist/terminal/ghostty/fallbackColors.d.ts +12 -0
- package/dist/terminal/ghostty/fallbackColors.js +23 -0
- package/dist/terminal/ghostty/fonts/LICENSE +21 -0
- package/dist/terminal/ghostty/fonts/SymbolsNerdFontMono-Regular.woff2 +0 -0
- package/dist/terminal/ghostty/keyCodes.logic.d.ts +20 -0
- package/dist/terminal/ghostty/keyCodes.logic.js +249 -0
- package/dist/terminal/ghostty/links.logic.d.ts +39 -0
- package/dist/terminal/ghostty/links.logic.js +84 -0
- package/dist/terminal/ghostty/renderer.d.ts +98 -0
- package/dist/terminal/ghostty/renderer.js +481 -0
- package/dist/terminal/ghostty/runtime.d.ts +126 -0
- package/dist/terminal/ghostty/runtime.js +346 -0
- package/dist/terminal/ghostty/spriteGlyphs.logic.d.ts +64 -0
- package/dist/terminal/ghostty/spriteGlyphs.logic.js +546 -0
- package/dist/terminal/ghostty/surface.d.ts +285 -0
- package/dist/terminal/ghostty/surface.js +1565 -0
- package/dist/terminal/ghostty/surface.logic.d.ts +181 -0
- package/dist/terminal/ghostty/surface.logic.js +392 -0
- package/dist/terminal/ghostty/vendor/LICENSE +21 -0
- package/dist/terminal/ghostty/vendor/ghostty-effects.wasm +0 -0
- package/dist/terminal/ghostty/vendor/ghostty-vt.wasm +0 -0
- package/dist/terminal/index.d.ts +12 -0
- package/dist/terminal/index.js +12 -0
- package/dist/terminal/terminal.css +169 -0
- package/dist/text/MarkedText.d.ts +14 -0
- package/dist/text/MarkedText.js +11 -0
- package/dist/text/highlightSegments.d.ts +13 -0
- package/dist/text/highlightSegments.js +29 -0
- package/dist/text/index.d.ts +3 -0
- package/dist/text/index.js +3 -0
- package/dist/text/text.css +11 -0
- package/dist/themes/attributes.d.ts +21 -0
- package/dist/themes/attributes.js +41 -0
- package/dist/themes/catalog.d.ts +125 -0
- package/dist/themes/catalog.js +940 -0
- package/dist/themes/color.d.ts +29 -0
- package/dist/themes/color.js +52 -0
- package/dist/themes/ids.d.ts +139 -0
- package/dist/themes/ids.js +59 -0
- package/dist/themes/index.d.ts +12 -0
- package/dist/themes/index.js +12 -0
- package/dist/themes/themes.generated.css +1830 -0
- package/dist/themes/tokens.d.ts +189 -0
- package/dist/themes/tokens.js +462 -0
- package/dist/tooltip/Tooltip.d.ts +21 -0
- package/dist/tooltip/Tooltip.js +105 -0
- package/dist/tooltip/Tooltip.logic.d.ts +49 -0
- package/dist/tooltip/Tooltip.logic.js +50 -0
- package/dist/tooltip/index.d.ts +3 -0
- package/dist/tooltip/index.js +3 -0
- package/dist/tooltip/tooltip.css +45 -0
- package/package.json +91 -4
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
|
+
import { type LinePoint } from './Line.logic.js';
|
|
3
|
+
/** A series of a {@link LineChart}: its label in the legend and the read-out, and its values in time order. */
|
|
4
|
+
export interface LineSeries {
|
|
5
|
+
readonly label: string;
|
|
6
|
+
readonly points: readonly LinePoint[];
|
|
7
|
+
}
|
|
8
|
+
/** Props for {@link LineChart}. */
|
|
9
|
+
export interface LineChartProps {
|
|
10
|
+
/** What the chart shows, for screen readers ("Questions"). */
|
|
11
|
+
readonly label: string;
|
|
12
|
+
/** The series, each drawn in the next chart token. */
|
|
13
|
+
readonly series: readonly LineSeries[];
|
|
14
|
+
/** The time axis's start, in Unix milliseconds. */
|
|
15
|
+
readonly from: number;
|
|
16
|
+
/** The time axis's end, in Unix milliseconds; a point outside `from` to `to` is not drawn. */
|
|
17
|
+
readonly to: number;
|
|
18
|
+
/** A value as the axis and the read-out write it. */
|
|
19
|
+
readonly format: (value: number) => string;
|
|
20
|
+
/** A moment as the axis and the read-out write it (`10:42:05`). */
|
|
21
|
+
readonly timeText: (at: number) => string;
|
|
22
|
+
/** Hands over the time range a drag across the plot picked, earlier end first; without it the plot only reads out. */
|
|
23
|
+
readonly onSelect?: (from: number, to: number) => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A time-series line chart drawn in SVG from the chart tokens, in light and
|
|
27
|
+
* dark: a line per series over a time axis from `from` to `to` and a value
|
|
28
|
+
* axis from zero, with a legend under it. A missing value breaks its line.
|
|
29
|
+
* Hovering the plot marks the nearest moment and reads out every series'
|
|
30
|
+
* value there. With `onSelect`, dragging across the plot picks the time
|
|
31
|
+
* range between the press and the release and hands it over. The chart is
|
|
32
|
+
* as wide as its frame and 55% as high, between 120 and 360 pixels.
|
|
33
|
+
*/
|
|
34
|
+
export declare function LineChart({ label, series, from, to, format, timeText, onSelect }: LineChartProps): ReactElement;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
import { useLabels } from '../labels/UiokProvider.js';
|
|
4
|
+
import { linePath, nearestMoment, timeTickLabels, valueDomain } from './Line.logic.js';
|
|
5
|
+
import { scaled, ticksOf, unscaled } from './Scatter.logic.js';
|
|
6
|
+
import { seriesColour } from './seriesColour.js';
|
|
7
|
+
/** The room the axes' ticks take around the plot. */
|
|
8
|
+
const MARGIN = { top: 8, right: 10, bottom: 22, left: 52 };
|
|
9
|
+
/**
|
|
10
|
+
* A time-series line chart drawn in SVG from the chart tokens, in light and
|
|
11
|
+
* dark: a line per series over a time axis from `from` to `to` and a value
|
|
12
|
+
* axis from zero, with a legend under it. A missing value breaks its line.
|
|
13
|
+
* Hovering the plot marks the nearest moment and reads out every series'
|
|
14
|
+
* value there. With `onSelect`, dragging across the plot picks the time
|
|
15
|
+
* range between the press and the release and hands it over. The chart is
|
|
16
|
+
* as wide as its frame and 55% as high, between 120 and 360 pixels.
|
|
17
|
+
*/
|
|
18
|
+
export function LineChart({ label, series, from, to, format, timeText, onSelect }) {
|
|
19
|
+
const labels = useLabels().charts;
|
|
20
|
+
const frame = useRef(null);
|
|
21
|
+
const svg = useRef(null);
|
|
22
|
+
const [labelWidths, setLabelWidths] = useState({});
|
|
23
|
+
const [width, setWidth] = useState(320);
|
|
24
|
+
const [hover, setHover] = useState(null);
|
|
25
|
+
const [drag, setDrag] = useState(null);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const element = frame.current;
|
|
28
|
+
if (element === null || typeof ResizeObserver === 'undefined')
|
|
29
|
+
return;
|
|
30
|
+
const observer = new ResizeObserver(([entry]) => {
|
|
31
|
+
const measured = entry?.contentRect.width ?? 0;
|
|
32
|
+
if (measured > 0)
|
|
33
|
+
setWidth(measured);
|
|
34
|
+
});
|
|
35
|
+
observer.observe(element);
|
|
36
|
+
return () => observer.disconnect();
|
|
37
|
+
}, []);
|
|
38
|
+
const height = Math.round(Math.min(360, Math.max(120, width * 0.55)));
|
|
39
|
+
// The first layout uses a conservative estimate; SVG supplies real glyph widths
|
|
40
|
+
// before paint, including locale-specific numeric separators and units.
|
|
41
|
+
const widthOf = (text) => labelWidths[text] ?? Array.from(text).length * 8;
|
|
42
|
+
const values = valueDomain(series.flatMap((entry) => entry.points.map((point) => point.value)));
|
|
43
|
+
const valueTicks = ticksOf(values, 'number', Math.max(2, Math.floor((height - MARGIN.top - MARGIN.bottom) / 40)));
|
|
44
|
+
const left = Math.max(MARGIN.left, ...valueTicks.map((tick) => widthOf(format(tick)) + 8));
|
|
45
|
+
const plotWidth = Math.max(40, width - left - MARGIN.right);
|
|
46
|
+
const plotHeight = Math.max(40, height - MARGIN.top - MARGIN.bottom);
|
|
47
|
+
const time = { min: from, max: to > from ? to : from + 1 };
|
|
48
|
+
const px = (at) => scaled(at, time, 0, plotWidth);
|
|
49
|
+
const py = (value) => scaled(value, values, plotHeight, 0);
|
|
50
|
+
const timeTicks = ticksOf(time, 'time', Math.max(2, Math.floor(plotWidth / 90)));
|
|
51
|
+
const timeLabels = timeTickLabels(timeTicks, timeText, px, plotWidth, widthOf);
|
|
52
|
+
const labelKey = JSON.stringify([...timeTicks.map(timeText), ...valueTicks.map(format)]);
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
const measured = {};
|
|
55
|
+
for (const label of svg.current?.querySelectorAll('.line-measure text') ?? []) {
|
|
56
|
+
if (typeof label.getComputedTextLength !== 'function')
|
|
57
|
+
continue;
|
|
58
|
+
const width = label.getComputedTextLength();
|
|
59
|
+
if (width > 0)
|
|
60
|
+
measured[label.textContent ?? ''] = Math.ceil(width);
|
|
61
|
+
}
|
|
62
|
+
setLabelWidths((known) => Object.entries(measured).some(([text, width]) => known[text] !== width) ? measured : known);
|
|
63
|
+
}, [labelKey]);
|
|
64
|
+
const moments = [
|
|
65
|
+
...new Set(series.flatMap((entry) => entry.points.map((point) => point.at)))
|
|
66
|
+
].sort((a, b) => a - b);
|
|
67
|
+
/** The moment a pointer stands over in the plot. */
|
|
68
|
+
const momentOf = (event) => {
|
|
69
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
70
|
+
const x = Math.min(plotWidth, Math.max(0, event.clientX - box.left));
|
|
71
|
+
return unscaled(x, time, 0, plotWidth);
|
|
72
|
+
};
|
|
73
|
+
const shown = hover === null ? null : nearestMoment(moments, hover);
|
|
74
|
+
return (_jsxs("figure", { "aria-label": label, className: "line-chart", children: [_jsxs("div", { className: "line-frame", ref: frame, children: [_jsxs("svg", { "aria-hidden": true, className: "line-svg", height: height, ref: svg, width: width, children: [_jsx("g", { className: "line-tick line-measure", visibility: "hidden", children: [...new Set([...timeTicks.map(timeText), ...valueTicks.map(format)])].map((text) => (_jsx("text", { children: text }, text))) }), _jsxs("g", { transform: `translate(${left} ${MARGIN.top})`, children: [timeTicks.map((tick) => (_jsx("line", { className: "line-grid", x1: px(tick), x2: px(tick), y1: 0, y2: plotHeight }, `grid${tick}`))), timeLabels.map((tick) => (_jsx("g", { className: "line-tick", children: _jsx("text", { dy: "1.1em", textAnchor: "start", x: tick.left, y: plotHeight + 4, children: tick.text }) }, `x${tick.at}`))), valueTicks.map((tick) => (_jsxs("g", { className: "line-tick", transform: `translate(0 ${py(tick)})`, children: [_jsx("line", { className: "line-grid", x1: 0, x2: plotWidth }), _jsx("text", { dy: "0.32em", textAnchor: "end", x: -6, children: format(tick) })] }, `y${tick}`))), _jsx("rect", { className: "line-axes", height: plotHeight, width: plotWidth }), series.map((entry, index) => (_jsx("path", { className: "line-series", d: linePath(entry.points.filter((point) => point.at >= time.min && point.at <= time.max), px, py), stroke: seriesColour(index) }, `${index}:${entry.label}`))), shown !== null && (_jsx("line", { className: "line-guide", x1: px(shown), x2: px(shown), y1: 0, y2: plotHeight })), drag !== null && (_jsx("rect", { className: "line-selection", height: plotHeight, width: Math.abs(px(drag.to) - px(drag.from)), x: Math.min(px(drag.from), px(drag.to)) })), _jsx("rect", { className: onSelect === undefined ? 'line-plot' : 'line-plot selectable', "data-testid": "line-plot", height: plotHeight, onPointerDown: (event) => {
|
|
75
|
+
if (onSelect === undefined || event.button !== 0)
|
|
76
|
+
return;
|
|
77
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
78
|
+
const at = momentOf(event);
|
|
79
|
+
setDrag({ from: at, to: at });
|
|
80
|
+
}, onPointerLeave: () => setHover(null), onPointerMove: (event) => {
|
|
81
|
+
const at = momentOf(event);
|
|
82
|
+
setHover(at);
|
|
83
|
+
if (drag !== null)
|
|
84
|
+
setDrag({ from: drag.from, to: at });
|
|
85
|
+
}, onPointerUp: (event) => {
|
|
86
|
+
if (drag === null)
|
|
87
|
+
return;
|
|
88
|
+
const end = momentOf(event);
|
|
89
|
+
setDrag(null);
|
|
90
|
+
// A click, or a drag of less than a pixel, picks no range.
|
|
91
|
+
if (onSelect !== undefined && Math.abs(px(end) - px(drag.from)) >= 2) {
|
|
92
|
+
onSelect(Math.min(drag.from, end), Math.max(drag.from, end));
|
|
93
|
+
}
|
|
94
|
+
}, width: plotWidth })] })] }), shown !== null && (_jsxs("div", { className: "line-readout", role: "status", style: px(shown) > plotWidth / 2
|
|
95
|
+
? { right: width - left - px(shown) + 8 }
|
|
96
|
+
: { left: left + px(shown) + 8 }, children: [_jsx("strong", { children: labels.time(timeText(shown)) }), series.map((entry, index) => {
|
|
97
|
+
const value = entry.points.find((point) => point.at === shown)?.value ?? null;
|
|
98
|
+
return (_jsxs("span", { children: [_jsx("span", { "aria-hidden": true, className: "line-swatch", style: { background: seriesColour(index) } }), labels.seriesValue(entry.label, value === null ? '---' : format(value))] }, `${index}:${entry.label}`));
|
|
99
|
+
})] }))] }), _jsx("ul", { className: "line-legend", children: series.map((entry, index) => (_jsxs("li", { children: [_jsx("span", { "aria-hidden": true, className: "line-swatch", style: { background: seriesColour(index) } }), entry.label] }, `${index}:${entry.label}`))) })] }));
|
|
100
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** A slice of a pie: what it stands for and how much. */
|
|
2
|
+
export interface PieSlice {
|
|
3
|
+
readonly label: string;
|
|
4
|
+
readonly value: number;
|
|
5
|
+
}
|
|
6
|
+
/** A slice as drawn: its share of the whole and its path in a circle of radius `r` at the origin. */
|
|
7
|
+
export interface PieArc extends PieSlice {
|
|
8
|
+
readonly share: number;
|
|
9
|
+
readonly path: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The arcs of `slices`, clockwise from twelve o'clock in the order given;
|
|
13
|
+
* a slice of nothing or less is left out, and a lone slice is the whole
|
|
14
|
+
* circle.
|
|
15
|
+
*/
|
|
16
|
+
export declare function pieArcs(slices: readonly PieSlice[], r: number): PieArc[];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The geometry of the pie chart (`PieChart.tsx`) and of `ResultChart`'s pie:
|
|
3
|
+
* each slice's share and its arc as an SVG path.
|
|
4
|
+
*/
|
|
5
|
+
/** A point on the circle of radius `r` at `turn` turns from twelve o'clock, clockwise. */
|
|
6
|
+
const pointAt = (r, turn) => {
|
|
7
|
+
const angle = turn * 2 * Math.PI;
|
|
8
|
+
return `${(r * Math.sin(angle)).toFixed(3)} ${(-r * Math.cos(angle)).toFixed(3)}`;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The arcs of `slices`, clockwise from twelve o'clock in the order given;
|
|
12
|
+
* a slice of nothing or less is left out, and a lone slice is the whole
|
|
13
|
+
* circle.
|
|
14
|
+
*/
|
|
15
|
+
export function pieArcs(slices, r) {
|
|
16
|
+
const kept = slices.filter((slice) => slice.value > 0);
|
|
17
|
+
const total = kept.reduce((sum, slice) => sum + slice.value, 0);
|
|
18
|
+
let turn = 0;
|
|
19
|
+
return kept.map((slice) => {
|
|
20
|
+
const share = slice.value / total;
|
|
21
|
+
const start = turn;
|
|
22
|
+
turn += share;
|
|
23
|
+
const path = share >= 1
|
|
24
|
+
? `M 0 ${-r} A ${r} ${r} 0 1 1 0 ${r} A ${r} ${r} 0 1 1 0 ${-r} Z`
|
|
25
|
+
: `M 0 0 L ${pointAt(r, start)} A ${r} ${r} 0 ${share > 0.5 ? 1 : 0} 1 ${pointAt(r, turn)} Z`;
|
|
26
|
+
return { ...slice, share, path };
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
|
+
import { type PieSlice } from './Pie.logic.js';
|
|
3
|
+
/** Props for {@link PieChart}. */
|
|
4
|
+
export interface PieChartProps {
|
|
5
|
+
/** What the chart shows, for screen readers ("Time per state"). */
|
|
6
|
+
readonly label: string;
|
|
7
|
+
/** The slices, clockwise from twelve o'clock in this order; one of nothing or less is left out. */
|
|
8
|
+
readonly slices: readonly PieSlice[];
|
|
9
|
+
/** A slice's value as its legend row writes it, before the share. */
|
|
10
|
+
readonly format: (value: number) => string;
|
|
11
|
+
/** The pie's width and height in pixels; 160 unless given. */
|
|
12
|
+
readonly size?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A pie chart drawn in SVG from the chart tokens, in light and dark: a slice
|
|
16
|
+
* per value, clockwise from twelve o'clock, and a legend beside it naming
|
|
17
|
+
* each slice with its value and its share. Hovering a slice or its legend
|
|
18
|
+
* row lights both.
|
|
19
|
+
*/
|
|
20
|
+
export declare function PieChart({ label, slices, format, size }: PieChartProps): ReactElement;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { pieArcs } from './Pie.logic.js';
|
|
4
|
+
import { seriesColour } from './seriesColour.js';
|
|
5
|
+
/**
|
|
6
|
+
* A pie chart drawn in SVG from the chart tokens, in light and dark: a slice
|
|
7
|
+
* per value, clockwise from twelve o'clock, and a legend beside it naming
|
|
8
|
+
* each slice with its value and its share. Hovering a slice or its legend
|
|
9
|
+
* row lights both.
|
|
10
|
+
*/
|
|
11
|
+
export function PieChart({ label, slices, format, size = 160 }) {
|
|
12
|
+
const [lit, setLit] = useState(null);
|
|
13
|
+
const r = size / 2 - 2;
|
|
14
|
+
const arcs = pieArcs(slices, r);
|
|
15
|
+
return (_jsxs("figure", { "aria-label": label, className: "pie-chart", children: [_jsx("svg", { "aria-hidden": true, height: size, viewBox: `${-size / 2} ${-size / 2} ${size} ${size}`, width: size, children: arcs.map((arc, index) => (_jsx("path", { className: lit === index ? 'pie-slice lit' : 'pie-slice', d: arc.path, fill: seriesColour(index), onPointerEnter: () => setLit(index), onPointerLeave: () => setLit(null) }, arc.label))) }), _jsx("ul", { className: "pie-legend", children: arcs.map((arc, index) => (_jsxs("li", { className: lit === index ? 'lit' : undefined, onPointerEnter: () => setLit(index), onPointerLeave: () => setLit(null), children: [_jsx("span", { "aria-hidden": true, className: "pie-swatch", style: { background: seriesColour(index) } }), _jsx("span", { className: "pie-label", children: arc.label }), _jsxs("span", { className: "pie-value", children: [format(arc.value), " (", (arc.share * 100).toFixed(1), "%)"] })] }, arc.label))) })] }));
|
|
16
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type ReactElement, type Ref } from 'react';
|
|
2
|
+
import { type ChartSeries, type ResultChartType } from './ResultChart.logic.js';
|
|
3
|
+
/** Props for {@link ResultChart}. */
|
|
4
|
+
export interface ResultChartProps {
|
|
5
|
+
/** How the table is drawn. */
|
|
6
|
+
readonly type: ResultChartType;
|
|
7
|
+
/** Whether series stack on each other; only bar, column, and area stack (`STACKABLE_CHART_TYPES`). */
|
|
8
|
+
readonly stacked: boolean;
|
|
9
|
+
/** The title drawn above the legend, and the accessible name; empty for none (named by `charts.chart`). */
|
|
10
|
+
readonly title: string;
|
|
11
|
+
/** The X axis's title: the category or number axis (the vertical one for bar). */
|
|
12
|
+
readonly xLabel: string;
|
|
13
|
+
/** The value axis's title (the horizontal one for bar). */
|
|
14
|
+
readonly yLabel: string;
|
|
15
|
+
/** Each row's X value as text, a category apiece; a pie's slice names. */
|
|
16
|
+
readonly categories: readonly string[];
|
|
17
|
+
/** Each row's X value as a number (Unix milliseconds for a time), for timeline and scatter; null where it has none. */
|
|
18
|
+
readonly xValues: readonly (number | null)[];
|
|
19
|
+
/** The series, each a value per row; a pie draws the first alone. */
|
|
20
|
+
readonly series: readonly ChartSeries[];
|
|
21
|
+
/** The drawing's width in pixels. */
|
|
22
|
+
readonly width: number;
|
|
23
|
+
/** The drawing's height in pixels. */
|
|
24
|
+
readonly height: number;
|
|
25
|
+
/** Receives the `<svg>`, for saving it with `svgMarkup` and `svgPng`. */
|
|
26
|
+
readonly svgRef?: Ref<SVGSVGElement>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A chart of a table of rows, in SVG from the chart tokens: bar
|
|
30
|
+
* (horizontal) and column (vertical), each grouped or stacked; line, spline
|
|
31
|
+
* (a monotone curve), and area (filled to zero, or stacked); pie of one
|
|
32
|
+
* series; timeline over a time axis; and scatter over a number axis. Title,
|
|
33
|
+
* legend, and axis labels are drawn inside the SVG, so its picture saves
|
|
34
|
+
* whole; hovering a value reads it out under the drawing. Each mark names
|
|
35
|
+
* its value in `data-series` and `data-at`, which one listener on the
|
|
36
|
+
* drawing reads, and the hover lives in a store the read-out follows, so
|
|
37
|
+
* the marks draw again only when the chart changes.
|
|
38
|
+
*/
|
|
39
|
+
declare function ResultChartDrawing({ type, stacked, title, xLabel, yLabel, categories, xValues, series, width, height, svgRef }: ResultChartProps): ReactElement;
|
|
40
|
+
/**
|
|
41
|
+
* A chart of a table of rows in one of eight types (`ResultChartType`),
|
|
42
|
+
* drawn in SVG from the chart tokens with its title, legend, and axes inside,
|
|
43
|
+
* and a read-out of the value under the pointer. It draws again only when a
|
|
44
|
+
* prop changes, so keep `categories`, `xValues`, and `series` stable.
|
|
45
|
+
*/
|
|
46
|
+
export declare const ResultChart: import("react").MemoExoticComponent<typeof ResultChartDrawing>;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { memo, useState, useSyncExternalStore } from 'react';
|
|
3
|
+
import { useLabels } from '../labels/UiokProvider.js';
|
|
4
|
+
import { createValueStore } from '../store/ValueStore.js';
|
|
5
|
+
import { pieArcs } from './Pie.logic.js';
|
|
6
|
+
import { STACKABLE_CHART_TYPES, cutLabel, legendLayout, shownLabels, splinePath, stackedRanges, valueDomain } from './ResultChart.logic.js';
|
|
7
|
+
import { domainOf, scaled, tickLabel, ticksOf } from './Scatter.logic.js';
|
|
8
|
+
import { seriesColour as colour } from './seriesColour.js';
|
|
9
|
+
/** A value as an axis and the read-out write it: six significant digits, no exponent for everyday sizes. */
|
|
10
|
+
const valueText = (value) => String(Number(value.toPrecision(6)));
|
|
11
|
+
/**
|
|
12
|
+
* The value under the pointer, read out under the drawing: it follows the
|
|
13
|
+
* hover on its own, so the pointer moving from mark to mark draws this line
|
|
14
|
+
* alone, never the chart's marks.
|
|
15
|
+
*/
|
|
16
|
+
function ResultChartReadout({ hover, type, categories, series }) {
|
|
17
|
+
const labels = useLabels().charts;
|
|
18
|
+
const hovered = useSyncExternalStore(hover.subscribe, hover.get);
|
|
19
|
+
const shown = (seriesIndex, at) => {
|
|
20
|
+
const value = series[seriesIndex]?.values[at];
|
|
21
|
+
return labels.seriesCategoryValue(series[seriesIndex]?.name ?? '', categories[at] ?? '', value === null || value === undefined ? labels.noValue : valueText(value));
|
|
22
|
+
};
|
|
23
|
+
return (_jsx("p", { "aria-live": "polite", className: "result-chart-readout", children: hovered === null
|
|
24
|
+
? // A no-break space keeps the line's height while nothing is hovered.
|
|
25
|
+
'\u00a0'
|
|
26
|
+
: type === 'pie'
|
|
27
|
+
? labels.seriesValue(categories[hovered.at] ?? '', valueText(series[0]?.values[hovered.at] ?? 0))
|
|
28
|
+
: shown(hovered.series, hovered.at) }));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A chart of a table of rows, in SVG from the chart tokens: bar
|
|
32
|
+
* (horizontal) and column (vertical), each grouped or stacked; line, spline
|
|
33
|
+
* (a monotone curve), and area (filled to zero, or stacked); pie of one
|
|
34
|
+
* series; timeline over a time axis; and scatter over a number axis. Title,
|
|
35
|
+
* legend, and axis labels are drawn inside the SVG, so its picture saves
|
|
36
|
+
* whole; hovering a value reads it out under the drawing. Each mark names
|
|
37
|
+
* its value in `data-series` and `data-at`, which one listener on the
|
|
38
|
+
* drawing reads, and the hover lives in a store the read-out follows, so
|
|
39
|
+
* the marks draw again only when the chart changes.
|
|
40
|
+
*/
|
|
41
|
+
function ResultChartDrawing({ type, stacked, title, xLabel, yLabel, categories, xValues, series, width, height, svgRef }) {
|
|
42
|
+
const labels = useLabels().charts;
|
|
43
|
+
const [hover] = useState(() => createValueStore(null));
|
|
44
|
+
const stack = stacked && STACKABLE_CHART_TYPES.has(type);
|
|
45
|
+
const legendNames = (type === 'pie' ? categories : series.map((entry) => entry.name)).slice(0, 24);
|
|
46
|
+
const legendPlaces = legendLayout(legendNames.map((name) => cutLabel(name, 24)), width - 24);
|
|
47
|
+
const legendRows = legendPlaces.reduce((rows, place) => Math.max(rows, place.row + 1), 0);
|
|
48
|
+
const top = (title === '' ? 8 : 28) + Math.min(4, legendRows) * 16;
|
|
49
|
+
const values = valueDomain(series, stack);
|
|
50
|
+
const valueTicks = type === 'pie' ? [] : ticksOf(values, 'number');
|
|
51
|
+
// The value axis's title sits clear of its widest tick (11px figures run about 6.5px each).
|
|
52
|
+
const tickWidth = valueTicks.reduce((widest, tick) => Math.max(widest, valueText(tick).length), 0);
|
|
53
|
+
const margin = {
|
|
54
|
+
top,
|
|
55
|
+
right: 16,
|
|
56
|
+
bottom: 44,
|
|
57
|
+
left: type === 'bar' ? 110 : Math.max(64, Math.ceil(tickWidth * 6.5) + 30)
|
|
58
|
+
};
|
|
59
|
+
const plotWidth = Math.max(40, width - margin.left - margin.right);
|
|
60
|
+
const plotHeight = Math.max(40, height - margin.top - margin.bottom);
|
|
61
|
+
const ranges = stack ? stackedRanges(series) : [];
|
|
62
|
+
const count = categories.length;
|
|
63
|
+
const band = (vertical) => (vertical ? plotHeight : plotWidth) / Math.max(1, count);
|
|
64
|
+
const numericX = type === 'timeline' || type === 'scatter';
|
|
65
|
+
const xDomain = domainOf(xValues.filter((value) => value !== null), type === 'timeline' ? 'time' : 'number');
|
|
66
|
+
/** A value's place on the value axis. */
|
|
67
|
+
const along = (value) => type === 'bar' ? scaled(value, values, 0, plotWidth) : scaled(value, values, plotHeight, 0);
|
|
68
|
+
/** A category's centre on the category axis, or a number's place on the X axis. */
|
|
69
|
+
const across = (at) => {
|
|
70
|
+
if (numericX)
|
|
71
|
+
return scaled(xValues[at] ?? xDomain.min, xDomain, 0, plotWidth);
|
|
72
|
+
return band(type === 'bar') * (at + 0.5);
|
|
73
|
+
};
|
|
74
|
+
const plot = [];
|
|
75
|
+
if (type === 'pie') {
|
|
76
|
+
const first = series[0];
|
|
77
|
+
const r = Math.max(10, Math.min(plotWidth, plotHeight) / 2 - 4);
|
|
78
|
+
const slices = categories.map((label, at) => ({
|
|
79
|
+
label,
|
|
80
|
+
value: Math.max(0, first?.values[at] ?? 0)
|
|
81
|
+
}));
|
|
82
|
+
const arcs = pieArcs(slices, r);
|
|
83
|
+
// pieArcs leaves out slices of nothing, so each arc maps back to the
|
|
84
|
+
// category it came from; the legend and the read-out go by category.
|
|
85
|
+
const kept = slices.flatMap((slice, at) => (slice.value > 0 ? [at] : []));
|
|
86
|
+
plot.push(_jsx("g", { transform: `translate(${plotWidth / 2} ${plotHeight / 2})`, children: arcs.map((arc, at) => {
|
|
87
|
+
const category = kept[at] ?? at;
|
|
88
|
+
return (_jsx("path", { className: "result-chart-mark", d: arc.path, fill: colour(category), "data-at": category, "data-series": 0 }, category));
|
|
89
|
+
}) }, "pie"));
|
|
90
|
+
}
|
|
91
|
+
else if (type === 'column' || type === 'bar') {
|
|
92
|
+
const vertical = type === 'bar';
|
|
93
|
+
const group = band(vertical) * 0.8;
|
|
94
|
+
const each = stack ? group : group / Math.max(1, series.length);
|
|
95
|
+
series.forEach((entry, seriesIndex) => {
|
|
96
|
+
entry.values.forEach((value, at) => {
|
|
97
|
+
if (value === null)
|
|
98
|
+
return;
|
|
99
|
+
const range = stack ? ranges[seriesIndex]?.[at] : { from: 0, to: value };
|
|
100
|
+
if (range === undefined)
|
|
101
|
+
return;
|
|
102
|
+
const start = band(vertical) * at + (band(vertical) - group) / 2 + (stack ? 0 : each * seriesIndex);
|
|
103
|
+
const a = along(Math.min(range.from, range.to));
|
|
104
|
+
const b = along(Math.max(range.from, range.to));
|
|
105
|
+
plot.push(_jsx("rect", { className: "result-chart-mark", "data-at": at, "data-series": seriesIndex, fill: colour(seriesIndex), height: vertical ? Math.max(1, each - 1) : Math.max(1, Math.abs(a - b)), width: vertical ? Math.max(1, Math.abs(b - a)) : Math.max(1, each - 1), x: vertical ? Math.min(a, b) : start, y: vertical ? start : Math.min(a, b) }, `${seriesIndex}-${at}`));
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
series.forEach((entry, seriesIndex) => {
|
|
111
|
+
const order = entry.values.map((_value, at) => at);
|
|
112
|
+
if (numericX)
|
|
113
|
+
order.sort((a, b) => (xValues[a] ?? 0) - (xValues[b] ?? 0));
|
|
114
|
+
const points = order.map((at) => {
|
|
115
|
+
const value = stack ? ranges[seriesIndex]?.[at]?.to : entry.values[at];
|
|
116
|
+
return value === null || value === undefined || (numericX && xValues[at] === null)
|
|
117
|
+
? null
|
|
118
|
+
: [across(at), along(value)];
|
|
119
|
+
});
|
|
120
|
+
if (type === 'area') {
|
|
121
|
+
const base = order.map((at) => [
|
|
122
|
+
across(at),
|
|
123
|
+
along(stack ? (ranges[seriesIndex]?.[at]?.from ?? 0) : Math.max(values.min, 0))
|
|
124
|
+
]);
|
|
125
|
+
const kept = points.flatMap((point, index) => (point === null ? [] : [{ point, index }]));
|
|
126
|
+
if (kept.length > 0) {
|
|
127
|
+
const outline = [
|
|
128
|
+
...kept.map(({ point }) => point),
|
|
129
|
+
...kept.map(({ index }) => base[index] ?? [0, 0]).reverse()
|
|
130
|
+
];
|
|
131
|
+
plot.push(_jsx("path", { className: "result-chart-area", d: `${outline.map(([x, y], at) => `${at === 0 ? 'M' : 'L'}${x} ${y}`).join(' ')} Z`, fill: colour(seriesIndex) }, `area-${seriesIndex}`));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (type !== 'scatter') {
|
|
135
|
+
plot.push(_jsx("path", { className: "result-chart-line", d: type === 'spline'
|
|
136
|
+
? splinePath(points)
|
|
137
|
+
: points
|
|
138
|
+
.map((point, at) => point === null
|
|
139
|
+
? ''
|
|
140
|
+
: `${at === 0 || points[at - 1] === null ? 'M' : 'L'}${point[0]} ${point[1]}`)
|
|
141
|
+
.join(' '), fill: "none", stroke: colour(seriesIndex) }, `line-${seriesIndex}`));
|
|
142
|
+
}
|
|
143
|
+
points.forEach((point, index) => {
|
|
144
|
+
if (point === null)
|
|
145
|
+
return;
|
|
146
|
+
const at = order[index] ?? index;
|
|
147
|
+
plot.push(_jsx("circle", { className: "result-chart-mark", cx: point[0], cy: point[1], "data-at": at, "data-series": seriesIndex, fill: colour(seriesIndex), r: type === 'scatter' ? 4 : 2.5 }, `dot-${seriesIndex}-${at}`));
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/** The mark the pointer came onto, read from its `data-` attributes; anything else keeps the last. */
|
|
152
|
+
const onPointerOver = (event) => {
|
|
153
|
+
const mark = event.target instanceof Element ? event.target : null;
|
|
154
|
+
const at = mark?.getAttribute('data-at');
|
|
155
|
+
const seriesIndex = mark?.getAttribute('data-series');
|
|
156
|
+
if (at === null || at === undefined || seriesIndex === null || seriesIndex === undefined) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const held = hover.get();
|
|
160
|
+
const next = { series: Number(seriesIndex), at: Number(at) };
|
|
161
|
+
if (held === null || held.series !== next.series || held.at !== next.at)
|
|
162
|
+
hover.set(next);
|
|
163
|
+
};
|
|
164
|
+
const labelled = shownLabels(count, type === 'bar' ? plotHeight : plotWidth, type === 'bar' ? 18 : 72);
|
|
165
|
+
return (_jsxs("figure", { className: "result-chart", children: [_jsxs("svg", { "aria-label": title === '' ? labels.chart : title, height: height, onPointerLeave: () => hover.set(null), onPointerOver: onPointerOver, ref: svgRef, role: "img", viewBox: `0 0 ${width} ${height}`, width: width, children: [title !== '' && (_jsx("text", { className: "result-chart-title", textAnchor: "middle", x: width / 2, y: 18, children: title })), _jsx("g", { transform: `translate(12 ${title === '' ? 8 : 28})`, children: legendPlaces.map((place, at) => place.row >= 4 ? null : (_jsxs("g", { transform: `translate(${place.x} ${place.row * 16})`, children: [_jsx("rect", { fill: colour(at), height: 9, rx: 2, width: 9, y: 2 }), _jsx("text", { className: "result-chart-legend", x: 14, y: 10, children: place.text })] }, at))) }), _jsxs("g", { transform: `translate(${margin.left} ${margin.top})`, children: [type !== 'pie' && (_jsxs("g", { className: "result-chart-axis", children: [valueTicks.map((tick) => type === 'bar' ? (_jsxs("g", { transform: `translate(${along(tick)} 0)`, children: [_jsx("line", { className: "result-chart-grid", y1: 0, y2: plotHeight }), _jsx("text", { textAnchor: "middle", y: plotHeight + 14, children: valueText(tick) })] }, tick)) : (_jsxs("g", { transform: `translate(0 ${along(tick)})`, children: [_jsx("line", { className: "result-chart-grid", x1: 0, x2: plotWidth }), _jsx("text", { dominantBaseline: "middle", textAnchor: "end", x: -6, children: valueText(tick) })] }, tick))), numericX
|
|
166
|
+
? ticksOf(xDomain, type === 'timeline' ? 'time' : 'number').map((tick) => (_jsx("text", { textAnchor: "middle", x: scaled(tick, xDomain, 0, plotWidth), y: plotHeight + 14, children: tickLabel(tick, { title: xLabel, kind: type === 'timeline' ? 'time' : 'number' }, xDomain) }, tick)))
|
|
167
|
+
: categories.map((category, at) => labelled.has(at) ? (type === 'bar' ? (_jsx("text", { dominantBaseline: "middle", textAnchor: "end", x: -6, y: across(at), children: cutLabel(category, 16) }, at)) : (_jsx("text", { textAnchor: "middle", x: across(at), y: plotHeight + 14, children: cutLabel(category, 12) }, at))) : null), _jsx("line", { className: "result-chart-baseline", x1: 0, x2: plotWidth, y1: plotHeight, y2: plotHeight }), _jsx("text", { className: "result-chart-axis-title", textAnchor: "middle", x: plotWidth / 2, y: plotHeight + 34, children: type === 'bar' ? yLabel : xLabel }), _jsx("text", { className: "result-chart-axis-title", textAnchor: "middle", transform: `translate(${-margin.left + 12} ${plotHeight / 2}) rotate(-90)`, children: type === 'bar' ? xLabel : yLabel })] })), plot] })] }), _jsx(ResultChartReadout, { categories: categories, hover: hover, series: series, type: type })] }));
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* A chart of a table of rows in one of eight types (`ResultChartType`),
|
|
171
|
+
* drawn in SVG from the chart tokens with its title, legend, and axes inside,
|
|
172
|
+
* and a read-out of the value under the pointer. It draws again only when a
|
|
173
|
+
* prop changes, so keep `categories`, `xValues`, and `series` stable.
|
|
174
|
+
*/
|
|
175
|
+
export const ResultChart = memo(ResultChartDrawing);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Domain } from './Scatter.logic.js';
|
|
2
|
+
/** The types `ResultChart` draws, in the order a type picker lists them. */
|
|
3
|
+
export declare const RESULT_CHART_TYPES: readonly ["bar", "column", "line", "spline", "area", "pie", "timeline", "scatter"];
|
|
4
|
+
/** One of the types `ResultChart` draws: bar, column, line, spline, area, pie, timeline, or scatter. */
|
|
5
|
+
export type ResultChartType = (typeof RESULT_CHART_TYPES)[number];
|
|
6
|
+
/** The types `stacked` applies to; the others draw each series on its own. */
|
|
7
|
+
export declare const STACKABLE_CHART_TYPES: ReadonlySet<ResultChartType>;
|
|
8
|
+
/** A series: its name and a value per category (one a row), null where the row holds none. */
|
|
9
|
+
export interface ChartSeries {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly values: readonly (number | null)[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The value axis's domain: from zero (or below, for a negative value) to
|
|
15
|
+
* the largest value, or the largest stacked sum, so bars and areas grow from
|
|
16
|
+
* zero; one unit wide where every value is equal.
|
|
17
|
+
*/
|
|
18
|
+
export declare function valueDomain(series: readonly ChartSeries[], stacked: boolean): Domain;
|
|
19
|
+
/** Where each series' segment starts and ends in a stack, by category: positive values up, negative down. */
|
|
20
|
+
export declare function stackedRanges(series: readonly ChartSeries[]): (readonly {
|
|
21
|
+
readonly from: number;
|
|
22
|
+
readonly to: number;
|
|
23
|
+
}[])[];
|
|
24
|
+
/**
|
|
25
|
+
* A smooth path through points, a monotone cubic (Fritsch and Carlson), so
|
|
26
|
+
* the spline never swings past a point's value; a gap (null) breaks it.
|
|
27
|
+
*/
|
|
28
|
+
export declare function splinePath(points: readonly ([number, number] | null)[]): string;
|
|
29
|
+
/** Which category labels an axis of `span` pixels shows, about one per `room` pixels, the first always. */
|
|
30
|
+
export declare function shownLabels(count: number, span: number, room?: number): Set<number>;
|
|
31
|
+
/** A label cut to `limit` characters with an ellipsis, so an axis stays readable. */
|
|
32
|
+
export declare const cutLabel: (text: string, limit?: number) => string;
|
|
33
|
+
/**
|
|
34
|
+
* Where each legend entry goes: one after another on a row, each as wide
|
|
35
|
+
* as its swatch, its text at about 6.5 pixels a character, and a gap,
|
|
36
|
+
* wrapping to the next row where the next would pass `width`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function legendLayout(texts: readonly string[], width: number): {
|
|
39
|
+
readonly text: string;
|
|
40
|
+
readonly x: number;
|
|
41
|
+
readonly row: number;
|
|
42
|
+
}[];
|