@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,165 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The layout of the chart of a table (`ResultChart.tsx`): its eight types,
|
|
3
|
+
* with stacking for bar, column, and area, worked out here as numbers that
|
|
4
|
+
* the component draws.
|
|
5
|
+
*/
|
|
6
|
+
/** The types `ResultChart` draws, in the order a type picker lists them. */
|
|
7
|
+
export const RESULT_CHART_TYPES = [
|
|
8
|
+
'bar',
|
|
9
|
+
'column',
|
|
10
|
+
'line',
|
|
11
|
+
'spline',
|
|
12
|
+
'area',
|
|
13
|
+
'pie',
|
|
14
|
+
'timeline',
|
|
15
|
+
'scatter'
|
|
16
|
+
];
|
|
17
|
+
/** The types `stacked` applies to; the others draw each series on its own. */
|
|
18
|
+
export const STACKABLE_CHART_TYPES = new Set([
|
|
19
|
+
'bar',
|
|
20
|
+
'column',
|
|
21
|
+
'area'
|
|
22
|
+
]);
|
|
23
|
+
/**
|
|
24
|
+
* The value axis's domain: from zero (or below, for a negative value) to
|
|
25
|
+
* the largest value, or the largest stacked sum, so bars and areas grow from
|
|
26
|
+
* zero; one unit wide where every value is equal.
|
|
27
|
+
*/
|
|
28
|
+
export function valueDomain(series, stacked) {
|
|
29
|
+
const length = Math.max(0, ...series.map((entry) => entry.values.length));
|
|
30
|
+
let min = 0;
|
|
31
|
+
let max = 0;
|
|
32
|
+
if (stacked) {
|
|
33
|
+
for (let at = 0; at < length; at++) {
|
|
34
|
+
let up = 0;
|
|
35
|
+
let down = 0;
|
|
36
|
+
for (const entry of series) {
|
|
37
|
+
const value = entry.values[at] ?? 0;
|
|
38
|
+
if (value >= 0)
|
|
39
|
+
up += value;
|
|
40
|
+
else
|
|
41
|
+
down += value;
|
|
42
|
+
}
|
|
43
|
+
max = Math.max(max, up);
|
|
44
|
+
min = Math.min(min, down);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
for (const entry of series) {
|
|
49
|
+
for (const value of entry.values) {
|
|
50
|
+
if (value === null || !Number.isFinite(value))
|
|
51
|
+
continue;
|
|
52
|
+
max = Math.max(max, value);
|
|
53
|
+
min = Math.min(min, value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return max === min ? { min, max: min + 1 } : { min, max };
|
|
58
|
+
}
|
|
59
|
+
/** Where each series' segment starts and ends in a stack, by category: positive values up, negative down. */
|
|
60
|
+
export function stackedRanges(series) {
|
|
61
|
+
const length = Math.max(0, ...series.map((entry) => entry.values.length));
|
|
62
|
+
const up = Array.from({ length }, () => 0);
|
|
63
|
+
const down = Array.from({ length }, () => 0);
|
|
64
|
+
return series.map((entry) => Array.from({ length }, (_unused, at) => {
|
|
65
|
+
const value = entry.values[at] ?? 0;
|
|
66
|
+
const base = value >= 0 ? (up[at] ?? 0) : (down[at] ?? 0);
|
|
67
|
+
const top = base + value;
|
|
68
|
+
if (value >= 0)
|
|
69
|
+
up[at] = top;
|
|
70
|
+
else
|
|
71
|
+
down[at] = top;
|
|
72
|
+
return { from: base, to: top };
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A smooth path through points, a monotone cubic (Fritsch and Carlson), so
|
|
77
|
+
* the spline never swings past a point's value; a gap (null) breaks it.
|
|
78
|
+
*/
|
|
79
|
+
export function splinePath(points) {
|
|
80
|
+
const runs = [];
|
|
81
|
+
let run = [];
|
|
82
|
+
for (const point of points) {
|
|
83
|
+
if (point === null) {
|
|
84
|
+
if (run.length > 0)
|
|
85
|
+
runs.push(run);
|
|
86
|
+
run = [];
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
run.push(point);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (run.length > 0)
|
|
93
|
+
runs.push(run);
|
|
94
|
+
const fixed = (value) => String(Math.round(value * 100) / 100);
|
|
95
|
+
return runs
|
|
96
|
+
.map((line) => {
|
|
97
|
+
const [first] = line;
|
|
98
|
+
if (first === undefined)
|
|
99
|
+
return '';
|
|
100
|
+
if (line.length < 3) {
|
|
101
|
+
return line.map(([x, y], at) => `${at === 0 ? 'M' : 'L'}${fixed(x)} ${fixed(y)}`).join(' ');
|
|
102
|
+
}
|
|
103
|
+
const slopes = line.slice(1).map(([x, y], at) => {
|
|
104
|
+
const [px, py] = line[at] ?? [x, y];
|
|
105
|
+
return x === px ? 0 : (y - py) / (x - px);
|
|
106
|
+
});
|
|
107
|
+
const tangents = line.map((_point, at) => {
|
|
108
|
+
if (at === 0)
|
|
109
|
+
return slopes[0] ?? 0;
|
|
110
|
+
if (at === line.length - 1)
|
|
111
|
+
return slopes[at - 1] ?? 0;
|
|
112
|
+
const before = slopes[at - 1] ?? 0;
|
|
113
|
+
const after = slopes[at] ?? 0;
|
|
114
|
+
if (before * after <= 0)
|
|
115
|
+
return 0;
|
|
116
|
+
// Fritsch-Carlson limit: a tangent past three times the gentler
|
|
117
|
+
// slope would carry the curve beyond a neighbour's value.
|
|
118
|
+
const limit = 3 * Math.min(Math.abs(before), Math.abs(after));
|
|
119
|
+
return Math.sign(before) * Math.min(Math.abs((before + after) / 2), limit);
|
|
120
|
+
});
|
|
121
|
+
let path = `M${fixed(first[0])} ${fixed(first[1])}`;
|
|
122
|
+
for (let at = 1; at < line.length; at++) {
|
|
123
|
+
const [x0, y0] = line[at - 1] ?? first;
|
|
124
|
+
const [x1, y1] = line[at] ?? first;
|
|
125
|
+
const step = (x1 - x0) / 3;
|
|
126
|
+
path += ` C${fixed(x0 + step)} ${fixed(y0 + (tangents[at - 1] ?? 0) * step)} ${fixed(x1 - step)} ${fixed(y1 - (tangents[at] ?? 0) * step)} ${fixed(x1)} ${fixed(y1)}`;
|
|
127
|
+
}
|
|
128
|
+
return path;
|
|
129
|
+
})
|
|
130
|
+
.filter(Boolean)
|
|
131
|
+
.join(' ');
|
|
132
|
+
}
|
|
133
|
+
/** Which category labels an axis of `span` pixels shows, about one per `room` pixels, the first always. */
|
|
134
|
+
export function shownLabels(count, span, room = 64) {
|
|
135
|
+
const every = Math.max(1, Math.ceil(count / Math.max(1, Math.floor(span / room))));
|
|
136
|
+
const shown = new Set();
|
|
137
|
+
for (let at = 0; at < count; at += every)
|
|
138
|
+
shown.add(at);
|
|
139
|
+
return shown;
|
|
140
|
+
}
|
|
141
|
+
/** A label cut to `limit` characters with an ellipsis, so an axis stays readable. */
|
|
142
|
+
export const cutLabel = (text, limit = 18) => Array.from(text).length <= limit
|
|
143
|
+
? text
|
|
144
|
+
: `${Array.from(text)
|
|
145
|
+
.slice(0, limit - 1)
|
|
146
|
+
.join('')}…`;
|
|
147
|
+
/**
|
|
148
|
+
* Where each legend entry goes: one after another on a row, each as wide
|
|
149
|
+
* as its swatch, its text at about 6.5 pixels a character, and a gap,
|
|
150
|
+
* wrapping to the next row where the next would pass `width`.
|
|
151
|
+
*/
|
|
152
|
+
export function legendLayout(texts, width) {
|
|
153
|
+
let x = 0;
|
|
154
|
+
let row = 0;
|
|
155
|
+
return texts.map((text) => {
|
|
156
|
+
const span = 14 + Array.from(text).length * 6.5 + 14;
|
|
157
|
+
if (x > 0 && x + span > width) {
|
|
158
|
+
x = 0;
|
|
159
|
+
row += 1;
|
|
160
|
+
}
|
|
161
|
+
const place = { text, x, row };
|
|
162
|
+
x += span;
|
|
163
|
+
return place;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** How an axis reads its values: numbers, times in milliseconds, or the index of a category. */
|
|
2
|
+
export type ScatterAxisKind = 'number' | 'time' | 'category';
|
|
3
|
+
/** One axis of the chart: its title, kind, and, for categories, their names in order. */
|
|
4
|
+
export interface ScatterAxis {
|
|
5
|
+
readonly title: string;
|
|
6
|
+
readonly kind: ScatterAxisKind;
|
|
7
|
+
readonly categories?: readonly string[];
|
|
8
|
+
/** Whether a time axis holds times of day (from midnight) rather than dates. */
|
|
9
|
+
readonly timeOfDay?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/** A domain of an axis: its lowest and highest value. */
|
|
12
|
+
export interface Domain {
|
|
13
|
+
readonly min: number;
|
|
14
|
+
readonly max: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The domain that holds `values`, with a twentieth of its width to spare on
|
|
18
|
+
* each side so no point sits on the frame; a single value gets one either
|
|
19
|
+
* side, and no value the unit domain. Categories span half a step past the
|
|
20
|
+
* first and the last.
|
|
21
|
+
*/
|
|
22
|
+
export declare function domainOf(values: readonly number[], kind: ScatterAxisKind): Domain;
|
|
23
|
+
/** Where `value` of `domain` lands on a pixel range from `from` to `to`. */
|
|
24
|
+
export declare const scaled: (value: number, domain: Domain, from: number, to: number) => number;
|
|
25
|
+
/** The value of `domain` a pixel of the range from `from` to `to` stands for. */
|
|
26
|
+
export declare const unscaled: (pixel: number, domain: Domain, from: number, to: number) => number;
|
|
27
|
+
/**
|
|
28
|
+
* The ticks of an axis over `domain`, about `count` of them: round numbers;
|
|
29
|
+
* round times; or each category in the domain, every few where there are
|
|
30
|
+
* more than `count`. Each tick is counted from the first, never added up,
|
|
31
|
+
* and a step finer than a double can tell apart at the domain's values (an
|
|
32
|
+
* integer past 2^53, a deep zoom) gives no repeats, so the ticks stay few.
|
|
33
|
+
*/
|
|
34
|
+
export declare function ticksOf(domain: Domain, kind: ScatterAxisKind, count?: number): number[];
|
|
35
|
+
/** A tick's label: the number, the date or the time of day in UTC, or the category. */
|
|
36
|
+
export declare function tickLabel(value: number, axis: ScatterAxis, domain: Domain): string;
|
|
37
|
+
/**
|
|
38
|
+
* The domains a rectangle dragged from one pixel to another zooms to, in a
|
|
39
|
+
* plot of `width` by `height` (y grows upward), or null for a rectangle too
|
|
40
|
+
* small to mean a zoom (a click) or a zoom narrower than a double can tell
|
|
41
|
+
* values apart in.
|
|
42
|
+
*/
|
|
43
|
+
export declare function zoomedTo(from: {
|
|
44
|
+
readonly x: number;
|
|
45
|
+
readonly y: number;
|
|
46
|
+
}, to: {
|
|
47
|
+
readonly x: number;
|
|
48
|
+
readonly y: number;
|
|
49
|
+
}, domains: {
|
|
50
|
+
readonly x: Domain;
|
|
51
|
+
readonly y: Domain;
|
|
52
|
+
}, width: number, height: number): {
|
|
53
|
+
readonly x: Domain;
|
|
54
|
+
readonly y: Domain;
|
|
55
|
+
} | null;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The geometry of the scatter chart (`ScatterChart.tsx`), which the other
|
|
3
|
+
* charts share: each axis's domain, its ticks, where a value lands in pixels
|
|
4
|
+
* and back, and the domain a dragged rectangle zooms to.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The domain that holds `values`, with a twentieth of its width to spare on
|
|
8
|
+
* each side so no point sits on the frame; a single value gets one either
|
|
9
|
+
* side, and no value the unit domain. Categories span half a step past the
|
|
10
|
+
* first and the last.
|
|
11
|
+
*/
|
|
12
|
+
export function domainOf(values, kind) {
|
|
13
|
+
if (values.length === 0)
|
|
14
|
+
return { min: 0, max: 1 };
|
|
15
|
+
let min = Infinity;
|
|
16
|
+
let max = -Infinity;
|
|
17
|
+
for (const value of values) {
|
|
18
|
+
if (value < min)
|
|
19
|
+
min = value;
|
|
20
|
+
if (value > max)
|
|
21
|
+
max = value;
|
|
22
|
+
}
|
|
23
|
+
if (kind === 'category')
|
|
24
|
+
return { min: min - 0.5, max: max + 0.5 };
|
|
25
|
+
if (min === max) {
|
|
26
|
+
const spare = kind === 'time' ? 3_600_000 : Math.max(1, Math.abs(min) / 10);
|
|
27
|
+
return { min: min - spare, max: max + spare };
|
|
28
|
+
}
|
|
29
|
+
const spare = (max - min) / 20;
|
|
30
|
+
return { min: min - spare, max: max + spare };
|
|
31
|
+
}
|
|
32
|
+
/** Where `value` of `domain` lands on a pixel range from `from` to `to`. */
|
|
33
|
+
export const scaled = (value, domain, from, to) => from + ((value - domain.min) / (domain.max - domain.min || 1)) * (to - from);
|
|
34
|
+
/** The value of `domain` a pixel of the range from `from` to `to` stands for. */
|
|
35
|
+
export const unscaled = (pixel, domain, from, to) => domain.min + ((pixel - from) / (to - from || 1)) * (domain.max - domain.min);
|
|
36
|
+
/** The round step nearest `span / count`: 1, 2, or 5 times a power of ten. */
|
|
37
|
+
const niceStep = (span, count) => {
|
|
38
|
+
const raw = span / Math.max(1, count);
|
|
39
|
+
const power = 10 ** Math.floor(Math.log10(raw));
|
|
40
|
+
const fraction = raw / power;
|
|
41
|
+
return (fraction < 1.5 ? 1 : fraction < 3 ? 2 : fraction < 7 ? 5 : 10) * power;
|
|
42
|
+
};
|
|
43
|
+
/** Time steps a time axis may tick on, in milliseconds. */
|
|
44
|
+
const TIME_STEPS = [
|
|
45
|
+
1000,
|
|
46
|
+
60_000,
|
|
47
|
+
600_000,
|
|
48
|
+
3_600_000,
|
|
49
|
+
6 * 3_600_000,
|
|
50
|
+
86_400_000,
|
|
51
|
+
7 * 86_400_000,
|
|
52
|
+
30 * 86_400_000,
|
|
53
|
+
365 * 86_400_000
|
|
54
|
+
];
|
|
55
|
+
/**
|
|
56
|
+
* The ticks of an axis over `domain`, about `count` of them: round numbers;
|
|
57
|
+
* round times; or each category in the domain, every few where there are
|
|
58
|
+
* more than `count`. Each tick is counted from the first, never added up,
|
|
59
|
+
* and a step finer than a double can tell apart at the domain's values (an
|
|
60
|
+
* integer past 2^53, a deep zoom) gives no repeats, so the ticks stay few.
|
|
61
|
+
*/
|
|
62
|
+
export function ticksOf(domain, kind, count = 5) {
|
|
63
|
+
const span = domain.max - domain.min;
|
|
64
|
+
if (!(span > 0) || !Number.isFinite(span))
|
|
65
|
+
return [];
|
|
66
|
+
if (kind === 'category') {
|
|
67
|
+
const first = Math.max(0, Math.ceil(domain.min));
|
|
68
|
+
const last = Math.floor(domain.max);
|
|
69
|
+
const every = Math.max(1, Math.ceil((last - first + 1) / count));
|
|
70
|
+
const ticks = [];
|
|
71
|
+
for (let at = first; at <= last; at += every)
|
|
72
|
+
ticks.push(at);
|
|
73
|
+
return ticks;
|
|
74
|
+
}
|
|
75
|
+
const step = kind === 'time'
|
|
76
|
+
? (TIME_STEPS.find((candidate) => span / candidate <= count) ?? niceStep(span, count))
|
|
77
|
+
: niceStep(span, count);
|
|
78
|
+
const ticks = [];
|
|
79
|
+
const first = Math.ceil(domain.min / step);
|
|
80
|
+
// A step is at least two thirds of `span / count`, so at most 1.5 × count + 1 ticks fit;
|
|
81
|
+
// the bound only stops a step too fine to move a double.
|
|
82
|
+
for (let index = 0; index <= 3 * count + 2; index += 1) {
|
|
83
|
+
const at = (first + index) * step;
|
|
84
|
+
if (at > domain.max)
|
|
85
|
+
break;
|
|
86
|
+
// Round away the float error a multiple of the step carries.
|
|
87
|
+
const tick = Number(at.toPrecision(12));
|
|
88
|
+
if (tick !== ticks.at(-1))
|
|
89
|
+
ticks.push(tick);
|
|
90
|
+
}
|
|
91
|
+
return ticks;
|
|
92
|
+
}
|
|
93
|
+
const pad = (value) => String(value).padStart(2, '0');
|
|
94
|
+
/** A tick's label: the number, the date or the time of day in UTC, or the category. */
|
|
95
|
+
export function tickLabel(value, axis, domain) {
|
|
96
|
+
if (axis.kind === 'category')
|
|
97
|
+
return axis.categories?.[Math.round(value)] ?? '';
|
|
98
|
+
if (axis.kind === 'number')
|
|
99
|
+
return String(value);
|
|
100
|
+
if (axis.timeOfDay === true) {
|
|
101
|
+
const seconds = Math.round(value / 1000);
|
|
102
|
+
const sign = seconds < 0 ? '-' : '';
|
|
103
|
+
const whole = Math.abs(seconds);
|
|
104
|
+
return `${sign}${pad(Math.floor(whole / 3600))}:${pad(Math.floor((whole % 3600) / 60))}`;
|
|
105
|
+
}
|
|
106
|
+
const date = new Date(value);
|
|
107
|
+
const day = `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
|
|
108
|
+
return domain.max - domain.min > 3 * 86_400_000
|
|
109
|
+
? day
|
|
110
|
+
: `${day} ${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The domains a rectangle dragged from one pixel to another zooms to, in a
|
|
114
|
+
* plot of `width` by `height` (y grows upward), or null for a rectangle too
|
|
115
|
+
* small to mean a zoom (a click) or a zoom narrower than a double can tell
|
|
116
|
+
* values apart in.
|
|
117
|
+
*/
|
|
118
|
+
export function zoomedTo(from, to, domains, width, height) {
|
|
119
|
+
if (Math.abs(to.x - from.x) < 4 || Math.abs(to.y - from.y) < 4)
|
|
120
|
+
return null;
|
|
121
|
+
const left = Math.min(from.x, to.x);
|
|
122
|
+
const right = Math.max(from.x, to.x);
|
|
123
|
+
const top = Math.min(from.y, to.y);
|
|
124
|
+
const bottom = Math.max(from.y, to.y);
|
|
125
|
+
const zoomed = {
|
|
126
|
+
x: { min: unscaled(left, domains.x, 0, width), max: unscaled(right, domains.x, 0, width) },
|
|
127
|
+
y: { min: unscaled(bottom, domains.y, height, 0), max: unscaled(top, domains.y, height, 0) }
|
|
128
|
+
};
|
|
129
|
+
// Past what a double tells apart at these values, a zoom shows nothing new.
|
|
130
|
+
return finest(zoomed.x) || finest(zoomed.y) ? null : zoomed;
|
|
131
|
+
}
|
|
132
|
+
/** Whether a domain is too narrow to tell its values apart: a few doubles wide, or none. */
|
|
133
|
+
const finest = (domain) => !(domain.max - domain.min >
|
|
134
|
+
64 * Number.EPSILON * Math.max(Math.abs(domain.min), Math.abs(domain.max), 1e-300));
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ReactElement } from 'react';
|
|
2
|
+
import { type ScatterAxis } from './Scatter.logic.js';
|
|
3
|
+
/** A point of a {@link ScatterChart}: the id its caller knows it by, where it goes, and its label. */
|
|
4
|
+
export interface ScatterPoint {
|
|
5
|
+
readonly id: number;
|
|
6
|
+
readonly x: number;
|
|
7
|
+
readonly y: number;
|
|
8
|
+
readonly label: string;
|
|
9
|
+
}
|
|
10
|
+
/** Props for {@link ScatterChart}. */
|
|
11
|
+
export interface ScatterChartProps {
|
|
12
|
+
/** What the chart shows, for screen readers ("price against made"). */
|
|
13
|
+
readonly label: string;
|
|
14
|
+
/** The points; each id must be unique, and each label is the point's tooltip and accessible name. */
|
|
15
|
+
readonly points: readonly ScatterPoint[];
|
|
16
|
+
/** The horizontal axis: its title, kind, and categories. */
|
|
17
|
+
readonly x: ScatterAxis;
|
|
18
|
+
/** The vertical axis: its title, kind, and categories. */
|
|
19
|
+
readonly y: ScatterAxis;
|
|
20
|
+
/** The id of the point the caller has picked, drawn lit; null for none. */
|
|
21
|
+
readonly picked?: number | null;
|
|
22
|
+
/** Hands over a point's id when it is clicked, or focused and Return or Space is pressed. */
|
|
23
|
+
readonly onPick: (id: number) => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A scatter chart drawn in SVG from the chart tokens, in light and dark: a
|
|
27
|
+
* point per value pair on number, time, or category axes, each titled. A
|
|
28
|
+
* point's label shows on hover, and a click, Return, or Space on a point
|
|
29
|
+
* hands it to `onPick`. Dragging a rectangle over the plot zooms to it, and
|
|
30
|
+
* Reset zoom comes back to every point; the frame resizes by its bottom
|
|
31
|
+
* right corner. A caller that plots other points gives the chart a new
|
|
32
|
+
* `key`, so the zoom starts over. The drag lives in a store its rectangle
|
|
33
|
+
* follows, so dragging draws the rectangle alone and the points only once
|
|
34
|
+
* it zooms. Needs `TooltipLayer` mounted for the point tooltips.
|
|
35
|
+
*/
|
|
36
|
+
export declare function ScatterChart({ label, points, x, y, picked, onPick }: ScatterChartProps): ReactElement;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { RotateCcw } from 'lucide-react';
|
|
3
|
+
import { useEffect, useRef, useState, useSyncExternalStore } from 'react';
|
|
4
|
+
import { Button } from '../button/Button.js';
|
|
5
|
+
import { useLabels } from '../labels/UiokProvider.js';
|
|
6
|
+
import { createValueStore } from '../store/ValueStore.js';
|
|
7
|
+
import { tooltip } from '../tooltip/Tooltip.logic.js';
|
|
8
|
+
import { domainOf, scaled, tickLabel, ticksOf, zoomedTo } from './Scatter.logic.js';
|
|
9
|
+
/** The room the axes' ticks and titles take around the plot. */
|
|
10
|
+
const MARGIN = { top: 10, right: 14, bottom: 40, left: 64 };
|
|
11
|
+
/** The size the chart starts at, before the frame reports its own. */
|
|
12
|
+
const START = { width: 640, height: 360 };
|
|
13
|
+
/**
|
|
14
|
+
* The rectangle a drag draws over the plot: it follows the drag on its own,
|
|
15
|
+
* so a move draws this rectangle alone, never the points under it.
|
|
16
|
+
*/
|
|
17
|
+
function ScatterSelection({ drag }) {
|
|
18
|
+
const held = useSyncExternalStore(drag.subscribe, drag.get);
|
|
19
|
+
return held === null ? null : (_jsx("rect", { className: "scatter-selection", height: Math.abs(held.to.y - held.from.y), width: Math.abs(held.to.x - held.from.x), x: Math.min(held.from.x, held.to.x), y: Math.min(held.from.y, held.to.y) }));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A scatter chart drawn in SVG from the chart tokens, in light and dark: a
|
|
23
|
+
* point per value pair on number, time, or category axes, each titled. A
|
|
24
|
+
* point's label shows on hover, and a click, Return, or Space on a point
|
|
25
|
+
* hands it to `onPick`. Dragging a rectangle over the plot zooms to it, and
|
|
26
|
+
* Reset zoom comes back to every point; the frame resizes by its bottom
|
|
27
|
+
* right corner. A caller that plots other points gives the chart a new
|
|
28
|
+
* `key`, so the zoom starts over. The drag lives in a store its rectangle
|
|
29
|
+
* follows, so dragging draws the rectangle alone and the points only once
|
|
30
|
+
* it zooms. Needs `TooltipLayer` mounted for the point tooltips.
|
|
31
|
+
*/
|
|
32
|
+
export function ScatterChart({ label, points, x, y, picked = null, onPick }) {
|
|
33
|
+
const labels = useLabels().charts;
|
|
34
|
+
const frame = useRef(null);
|
|
35
|
+
const [size, setSize] = useState(START);
|
|
36
|
+
const [zoom, setZoom] = useState(null);
|
|
37
|
+
const [drag] = useState(() => createValueStore(null));
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
const element = frame.current;
|
|
40
|
+
if (element === null || typeof ResizeObserver === 'undefined')
|
|
41
|
+
return;
|
|
42
|
+
const observer = new ResizeObserver(([entry]) => {
|
|
43
|
+
if (entry === undefined)
|
|
44
|
+
return;
|
|
45
|
+
const { width, height } = entry.contentRect;
|
|
46
|
+
if (width > 0 && height > 0)
|
|
47
|
+
setSize({ width, height });
|
|
48
|
+
});
|
|
49
|
+
observer.observe(element);
|
|
50
|
+
return () => observer.disconnect();
|
|
51
|
+
}, []);
|
|
52
|
+
const width = Math.max(80, size.width - MARGIN.left - MARGIN.right);
|
|
53
|
+
const height = Math.max(60, size.height - MARGIN.top - MARGIN.bottom);
|
|
54
|
+
const domains = zoom ?? {
|
|
55
|
+
x: domainOf(points.map((point) => point.x), x.kind),
|
|
56
|
+
y: domainOf(points.map((point) => point.y), y.kind)
|
|
57
|
+
};
|
|
58
|
+
const px = (value) => scaled(value, domains.x, 0, width);
|
|
59
|
+
const py = (value) => scaled(value, domains.y, height, 0);
|
|
60
|
+
const shown = points.filter((point) => point.x >= domains.x.min &&
|
|
61
|
+
point.x <= domains.x.max &&
|
|
62
|
+
point.y >= domains.y.min &&
|
|
63
|
+
point.y <= domains.y.max);
|
|
64
|
+
/** Where a pointer is in the plot, in its own pixels. */
|
|
65
|
+
const inPlot = (event) => {
|
|
66
|
+
const box = event.currentTarget.getBoundingClientRect();
|
|
67
|
+
return {
|
|
68
|
+
x: Math.min(width, Math.max(0, event.clientX - box.left)),
|
|
69
|
+
y: Math.min(height, Math.max(0, event.clientY - box.top))
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
const pickKey = (event, id) => {
|
|
73
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
74
|
+
event.preventDefault();
|
|
75
|
+
onPick(id);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
return (_jsxs("figure", { "aria-label": label, className: "scatter-chart", children: [_jsx("div", { className: "scatter-frame", ref: frame, children: _jsx("svg", { className: "scatter-svg", height: size.height, role: "group", "aria-label": label, width: size.width, children: _jsxs("g", { transform: `translate(${MARGIN.left} ${MARGIN.top})`, children: [ticksOf(domains.x, x.kind, Math.max(2, Math.floor(width / 110))).map((tick) => (_jsxs("g", { className: "scatter-tick", transform: `translate(${px(tick)} 0)`, children: [_jsx("line", { className: "scatter-grid", y1: 0, y2: height }), _jsx("text", { dy: "1.2em", textAnchor: "middle", y: height + 4, children: tickLabel(tick, x, domains.x) })] }, `x${tick}`))), ticksOf(domains.y, y.kind, Math.max(2, Math.floor(height / 50))).map((tick) => (_jsxs("g", { className: "scatter-tick", transform: `translate(0 ${py(tick)})`, children: [_jsx("line", { className: "scatter-grid", x1: 0, x2: width }), _jsx("text", { dy: "0.32em", textAnchor: "end", x: -6, children: tickLabel(tick, y, domains.y) })] }, `y${tick}`))), _jsx("rect", { className: "scatter-axes", height: height, width: width }), _jsx("rect", { className: "scatter-plot", "data-testid": "scatter-plot", height: height, onPointerDown: (event) => {
|
|
79
|
+
if (event.button !== 0)
|
|
80
|
+
return;
|
|
81
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
82
|
+
const at = inPlot(event);
|
|
83
|
+
drag.set({ from: at, to: at });
|
|
84
|
+
}, onPointerMove: (event) => {
|
|
85
|
+
const held = drag.get();
|
|
86
|
+
if (held !== null)
|
|
87
|
+
drag.set({ from: held.from, to: inPlot(event) });
|
|
88
|
+
}, onPointerUp: (event) => {
|
|
89
|
+
const held = drag.get();
|
|
90
|
+
if (held === null)
|
|
91
|
+
return;
|
|
92
|
+
const zoomed = zoomedTo(held.from, inPlot(event), domains, width, height);
|
|
93
|
+
drag.set(null);
|
|
94
|
+
if (zoomed !== null)
|
|
95
|
+
setZoom(zoomed);
|
|
96
|
+
}, width: width }), _jsx(ScatterSelection, { drag: drag }), shown.map((point) => (_jsx("circle", { "aria-label": point.label, className: picked === point.id ? 'scatter-point lit' : 'scatter-point', cx: px(point.x), cy: py(point.y), onClick: () => onPick(point.id), onKeyDown: (event) => pickKey(event, point.id), r: picked === point.id ? 6 : 4, role: "button", tabIndex: 0, ...tooltip(point.label) }, point.id))), _jsx("text", { className: "scatter-title", textAnchor: "middle", x: width / 2, y: height + 34, children: x.title }), _jsx("text", { className: "scatter-title", textAnchor: "middle", transform: `translate(${-MARGIN.left + 12} ${height / 2}) rotate(-90)`, children: y.title })] }) }) }), _jsxs("figcaption", { className: "scatter-caption", children: [_jsx("span", { children: shown.length === points.length
|
|
97
|
+
? labels.points(points.length)
|
|
98
|
+
: labels.pointsShown(shown.length, points.length) }), _jsxs(Button, { disabled: zoom === null, onClick: () => setZoom(null), size: "small", children: [_jsx(RotateCcw, { "aria-hidden": true, size: 13 }), labels.resetZoom] })] })] }));
|
|
99
|
+
}
|