@c9up/nebula 0.2.4 → 0.2.6
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/README.md +4 -4
- package/dist/atoms/AspectRatio.d.ts +10 -4
- package/dist/atoms/AspectRatio.js +21 -6
- package/dist/atoms/NativeSelect.d.ts +16 -0
- package/dist/atoms/NativeSelect.js +4 -1
- package/dist/atoms/Progress.d.ts +9 -1
- package/dist/atoms/Progress.js +21 -5
- package/dist/atoms/Switch.d.ts +7 -1
- package/dist/atoms/Switch.js +7 -1
- package/dist/cli/diff.d.ts +46 -0
- package/dist/cli/diff.js +73 -2
- package/dist/console/DiffCommand.js +6 -11
- package/dist/molecules/Accordion.js +1 -2
- package/dist/molecules/Collapsible.js +1 -2
- package/dist/molecules/Resizable.d.ts +6 -0
- package/dist/molecules/Resizable.js +8 -2
- package/dist/organisms/Agenda.d.ts +200 -0
- package/dist/organisms/Agenda.js +762 -0
- package/dist/organisms/AlertDialog.js +4 -5
- package/dist/organisms/Chart.d.ts +103 -15
- package/dist/organisms/Chart.js +518 -99
- package/dist/organisms/Dialog.js +7 -7
- package/dist/organisms/Sidebar.d.ts +5 -7
- package/dist/organisms/Sidebar.js +23 -6
- package/dist/organisms/Sonner.js +11 -4
- package/dist/organisms/index.d.ts +2 -1
- package/dist/organisms/index.js +2 -1
- package/dist/primitives/floatingSurface.js +60 -36
- package/dist/primitives/menuParts.js +3 -3
- package/dist/primitives/portal.d.ts +15 -0
- package/dist/primitives/portal.js +18 -1
- package/nebula.css +1 -1
- package/package.json +1 -1
- package/registry.json +13 -0
- package/src/atoms/AspectRatio.ts +21 -6
- package/src/atoms/NativeSelect.ts +20 -1
- package/src/atoms/Progress.ts +21 -5
- package/src/atoms/Switch.ts +7 -1
- package/src/cli/diff.ts +123 -2
- package/src/console/DiffCommand.ts +12 -15
- package/src/molecules/Accordion.ts +1 -2
- package/src/molecules/Collapsible.ts +1 -3
- package/src/molecules/Resizable.ts +8 -2
- package/src/organisms/Agenda.ts +1061 -0
- package/src/organisms/AlertDialog.ts +3 -5
- package/src/organisms/Chart.ts +633 -125
- package/src/organisms/Dialog.ts +6 -7
- package/src/organisms/Sidebar.ts +28 -6
- package/src/organisms/Sonner.ts +11 -4
- package/src/organisms/index.ts +26 -0
- package/src/primitives/floatingSurface.ts +74 -40
- package/src/primitives/menuParts.ts +3 -3
- package/src/primitives/portal.ts +27 -1
package/src/organisms/Chart.ts
CHANGED
|
@@ -8,19 +8,40 @@
|
|
|
8
8
|
* the shadcn chart examples are and what dashboards overwhelmingly need.
|
|
9
9
|
*
|
|
10
10
|
* Anything beyond that — stacked bars with negative values, dual axes, brushes,
|
|
11
|
-
* candlesticks — is a charting library's
|
|
12
|
-
* install one rather than have this
|
|
11
|
+
* candlesticks, horizontal bars, heatmaps, funnels — is a charting library's
|
|
12
|
+
* job, and an app needing one should install one rather than have this
|
|
13
|
+
* component grow into a bad copy.
|
|
13
14
|
*
|
|
14
15
|
* The colours come from `--chart-1` … `--chart-5`, so a chart re-themes with
|
|
15
16
|
* the rest of the design system instead of carrying its own palette.
|
|
16
17
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* **Geometry.** Text is never stretched. The drawing is two SVGs in pixel
|
|
19
|
+
* space — the value axis in a fixed gutter, the plot at full width — with
|
|
20
|
+
* every horizontal position written as a percentage of the plot's width and
|
|
21
|
+
* every vertical one in pixels. Only the curves and bars live in a nested SVG
|
|
22
|
+
* stretched to the plot (`preserveAspectRatio="none"`), with strokes that do
|
|
23
|
+
* not scale. Everything is an attribute: no `style` attribute, which a strict
|
|
24
|
+
* Content-Security-Policy blocks, and the server's markup is the chart.
|
|
25
|
+
*
|
|
26
|
+
* **Missing values.** A value that is absent, `null` or not a finite number is
|
|
27
|
+
* a gap: the line breaks, the area stops, no bar is drawn, and the table says
|
|
28
|
+
* so. Drawing it as `0` would invent a number.
|
|
29
|
+
*
|
|
30
|
+
* **Reading it.** An SVG chart is invisible to a screen reader whatever ARIA is
|
|
31
|
+
* put on it, so the same data is emitted as a real `<table>`, visually hidden.
|
|
32
|
+
* The plot also takes focus: the arrow keys walk the categories, and what the
|
|
33
|
+
* pointer or the keyboard points at is shown in a tooltip and announced in a
|
|
34
|
+
* live region.
|
|
21
35
|
*/
|
|
22
36
|
|
|
23
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
component,
|
|
39
|
+
html,
|
|
40
|
+
onMount,
|
|
41
|
+
onUnmount,
|
|
42
|
+
signal,
|
|
43
|
+
type TemplateResult,
|
|
44
|
+
} from "@c9up/aurora";
|
|
24
45
|
import { cn } from "../lib/cn.js";
|
|
25
46
|
import { uid } from "../lib/id.js";
|
|
26
47
|
import { type Reactive, read } from "../lib/props.js";
|
|
@@ -32,8 +53,20 @@ export interface ChartSeries {
|
|
|
32
53
|
color?: string;
|
|
33
54
|
}
|
|
34
55
|
|
|
35
|
-
/**
|
|
36
|
-
export type
|
|
56
|
+
/** A category label as a datum may hold it. */
|
|
57
|
+
export type ChartCategory = string | number | Date;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* One row: a category plus one numeric value per series key. A value that is
|
|
61
|
+
* missing, `null` or not a finite number is a gap.
|
|
62
|
+
*/
|
|
63
|
+
export type ChartDatum = Record<
|
|
64
|
+
string,
|
|
65
|
+
string | number | Date | null | undefined
|
|
66
|
+
>;
|
|
67
|
+
|
|
68
|
+
/** One end of the value axis: a number, or `"auto"` to fit the data. */
|
|
69
|
+
export type ChartBound = number | "auto";
|
|
37
70
|
|
|
38
71
|
export interface ChartProps {
|
|
39
72
|
data: readonly ChartDatum[];
|
|
@@ -41,214 +74,689 @@ export interface ChartProps {
|
|
|
41
74
|
/** Key in each datum holding the category label. */
|
|
42
75
|
categoryKey: string;
|
|
43
76
|
type?: "line" | "area" | "bar";
|
|
77
|
+
/** The chart's height in pixels, category labels included. Default `240`. */
|
|
44
78
|
height?: number;
|
|
45
79
|
/** Announced as the chart's name, and used as the data table's caption. */
|
|
46
80
|
label: string;
|
|
47
|
-
/** Draw horizontal
|
|
81
|
+
/** Draw a horizontal gridline at each value tick. Default `true`. */
|
|
48
82
|
grid?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* The value axis' range, `[min, max]`. Each end is a number, or `"auto"` to
|
|
85
|
+
* fit the data and round outwards to a tick. Default `["auto", "auto"]`.
|
|
86
|
+
*/
|
|
87
|
+
domain?: readonly [ChartBound, ChartBound];
|
|
88
|
+
/**
|
|
89
|
+
* Stretch an `"auto"` end to include zero. Default `true`. Always on for
|
|
90
|
+
* bars: a bar is measured from zero, and one that starts elsewhere
|
|
91
|
+
* exaggerates every difference.
|
|
92
|
+
*/
|
|
93
|
+
zero?: boolean;
|
|
94
|
+
/** Draw the smallest value at the top — a ranking, where 1 is best. */
|
|
95
|
+
reversed?: boolean;
|
|
96
|
+
/** About how many ticks the value axis gets. Default `5`. */
|
|
97
|
+
tickCount?: number;
|
|
98
|
+
/** Formats a value on the axis, in the tooltip and in the table. */
|
|
99
|
+
formatValue?: (value: number) => string;
|
|
100
|
+
/** Formats a category on the axis, in the tooltip and in the table. */
|
|
101
|
+
formatCategory?: (category: ChartCategory) => string;
|
|
102
|
+
/** BCP 47 locale for the default formatters. Default: the runtime's own. */
|
|
103
|
+
locale?: string;
|
|
104
|
+
/** What a screen reader calls the plot. Default `"chart"`. */
|
|
105
|
+
roleDescription?: string;
|
|
106
|
+
/**
|
|
107
|
+
* How a screen reader is told to read the plot. Default `"Arrow keys read
|
|
108
|
+
* each category."`.
|
|
109
|
+
*/
|
|
110
|
+
keyboardHint?: string;
|
|
49
111
|
class?: Reactive<string>;
|
|
50
112
|
}
|
|
51
113
|
|
|
52
|
-
|
|
53
|
-
const PADDING = { top: 8,
|
|
114
|
+
/** Where the plot's drawing starts and stops, in pixels from its top. */
|
|
115
|
+
const PADDING = { top: 8, bottom: 24 };
|
|
116
|
+
/** Font size of every axis label, in pixels. */
|
|
117
|
+
const FONT_SIZE = 11;
|
|
118
|
+
/**
|
|
119
|
+
* Average advance of one character at {@link FONT_SIZE}. An estimate — the
|
|
120
|
+
* server cannot measure text — erring wide, so labels never touch.
|
|
121
|
+
*/
|
|
122
|
+
const CHARACTER_WIDTH = FONT_SIZE * 0.62;
|
|
123
|
+
/** Space kept between two category labels, in pixels. */
|
|
124
|
+
const LABEL_GAP = 8;
|
|
125
|
+
/**
|
|
126
|
+
* The plot's width before it is measured: on the server, and until the first
|
|
127
|
+
* resize report. Labels are thinned for this width, then for the real one.
|
|
128
|
+
*/
|
|
129
|
+
const NOMINAL_PLOT_WIDTH = 600;
|
|
54
130
|
|
|
55
131
|
function colorFor(series: ChartSeries, index: number): string {
|
|
56
132
|
return series.color ?? `var(--chart-${(index % 5) + 1})`;
|
|
57
133
|
}
|
|
58
134
|
|
|
59
|
-
|
|
135
|
+
/** The value of `key`, or `null` for a gap. */
|
|
136
|
+
function numberAt(datum: ChartDatum, key: string): number | null {
|
|
60
137
|
const value = datum[key];
|
|
61
|
-
return typeof value === "number" ? value :
|
|
138
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
62
139
|
}
|
|
63
140
|
|
|
64
|
-
function categoryAt(datum: ChartDatum, key: string):
|
|
141
|
+
function categoryAt(datum: ChartDatum, key: string): ChartCategory {
|
|
65
142
|
const value = datum[key];
|
|
66
|
-
|
|
143
|
+
if (
|
|
144
|
+
typeof value === "string" ||
|
|
145
|
+
typeof value === "number" ||
|
|
146
|
+
value instanceof Date
|
|
147
|
+
) {
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
return "";
|
|
67
151
|
}
|
|
68
152
|
|
|
69
153
|
/**
|
|
70
|
-
* The
|
|
154
|
+
* The range the data covers.
|
|
71
155
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
156
|
+
* Gaps are skipped. With `zero` (the default), the range always includes zero.
|
|
157
|
+
* A flat range is widened — to `0`–`1` when it sits on zero, by a tenth of its
|
|
158
|
+
* value either side otherwise — so there is always something to scale
|
|
159
|
+
* against, and no data at all reads as `0`–`1`.
|
|
75
160
|
*/
|
|
76
161
|
export function valueRange(
|
|
77
162
|
data: readonly ChartDatum[],
|
|
78
163
|
series: readonly ChartSeries[],
|
|
164
|
+
options: { zero?: boolean } = {},
|
|
79
165
|
): { min: number; max: number } {
|
|
80
|
-
|
|
81
|
-
let
|
|
166
|
+
const zero = options.zero !== false;
|
|
167
|
+
let min = Number.POSITIVE_INFINITY;
|
|
168
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
82
169
|
for (const datum of data) {
|
|
83
170
|
for (const entry of series) {
|
|
84
171
|
const value = numberAt(datum, entry.key);
|
|
172
|
+
if (value === null) continue;
|
|
85
173
|
if (value < min) min = value;
|
|
86
174
|
if (value > max) max = value;
|
|
87
175
|
}
|
|
88
176
|
}
|
|
89
|
-
|
|
90
|
-
if (
|
|
177
|
+
if (min > max) return { min: 0, max: 1 };
|
|
178
|
+
if (zero) {
|
|
179
|
+
min = Math.min(min, 0);
|
|
180
|
+
max = Math.max(max, 0);
|
|
181
|
+
}
|
|
182
|
+
// Flat. With `zero` that can only be all zeros; without, a flat series
|
|
183
|
+
// away from zero is centred in a band a fifth of its value wide.
|
|
184
|
+
if (min === max) {
|
|
185
|
+
if (min === 0) return { min: 0, max: 1 };
|
|
186
|
+
const pad = Math.abs(min) * 0.1;
|
|
187
|
+
return { min: min - pad, max: max + pad };
|
|
188
|
+
}
|
|
91
189
|
return { min, max };
|
|
92
190
|
}
|
|
93
191
|
|
|
192
|
+
/**
|
|
193
|
+
* A "round" step near `raw`: 1, 2, 2.5 or 5 times a power of ten — the steps
|
|
194
|
+
* a reader can add up in their head.
|
|
195
|
+
*/
|
|
196
|
+
function niceStep(raw: number): number {
|
|
197
|
+
const exponent = Math.floor(Math.log10(raw));
|
|
198
|
+
const magnitude = 10 ** exponent;
|
|
199
|
+
const fraction = raw / magnitude;
|
|
200
|
+
const nice =
|
|
201
|
+
fraction <= 1
|
|
202
|
+
? 1
|
|
203
|
+
: fraction <= 2
|
|
204
|
+
? 2
|
|
205
|
+
: fraction <= 2.5
|
|
206
|
+
? 2.5
|
|
207
|
+
: fraction <= 5
|
|
208
|
+
? 5
|
|
209
|
+
: 10;
|
|
210
|
+
return Number((nice * magnitude).toPrecision(12));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Drop floating-point dust: `0.30000000000000004` → `0.3`. */
|
|
214
|
+
function clean(value: number): number {
|
|
215
|
+
return Number(value.toPrecision(12));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Round ticks covering `min`–`max`, about `count` of them.
|
|
220
|
+
*
|
|
221
|
+
* The range is widened outwards to the nearest tick, so the first and last
|
|
222
|
+
* ticks are the axis' ends.
|
|
223
|
+
*/
|
|
224
|
+
export function niceTicks(
|
|
225
|
+
min: number,
|
|
226
|
+
max: number,
|
|
227
|
+
count = 5,
|
|
228
|
+
): { min: number; max: number; step: number; ticks: number[] } {
|
|
229
|
+
let low = Math.min(min, max);
|
|
230
|
+
let high = Math.max(min, max);
|
|
231
|
+
if (!Number.isFinite(low) || !Number.isFinite(high)) {
|
|
232
|
+
low = 0;
|
|
233
|
+
high = 1;
|
|
234
|
+
}
|
|
235
|
+
if (low === high) {
|
|
236
|
+
const pad = low === 0 ? 1 : Math.abs(low) * 0.1;
|
|
237
|
+
low -= pad;
|
|
238
|
+
high += pad;
|
|
239
|
+
}
|
|
240
|
+
const intervals = Math.max(1, Math.round(count) - 1);
|
|
241
|
+
const step = niceStep((high - low) / intervals);
|
|
242
|
+
const first = clean(Math.floor(low / step) * step);
|
|
243
|
+
const last = clean(Math.ceil(high / step) * step);
|
|
244
|
+
const ticks: number[] = [];
|
|
245
|
+
for (let index = 0; ; index += 1) {
|
|
246
|
+
const tick = clean(first + index * step);
|
|
247
|
+
if (tick > last + step / 2) break;
|
|
248
|
+
ticks.push(tick);
|
|
249
|
+
}
|
|
250
|
+
return { min: first, max: last, step, ticks };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export interface ValueAxisOptions {
|
|
254
|
+
domain?: readonly [ChartBound, ChartBound];
|
|
255
|
+
zero?: boolean;
|
|
256
|
+
tickCount?: number;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The value axis a chart draws: its ends and its ticks.
|
|
261
|
+
*
|
|
262
|
+
* An `"auto"` end fits the data and is rounded outwards to a tick. A number is
|
|
263
|
+
* kept exactly and is a tick itself, with any round tick too close to it
|
|
264
|
+
* dropped so the two labels do not collide.
|
|
265
|
+
*/
|
|
266
|
+
export function valueAxis(
|
|
267
|
+
data: readonly ChartDatum[],
|
|
268
|
+
series: readonly ChartSeries[],
|
|
269
|
+
options: ValueAxisOptions = {},
|
|
270
|
+
): { min: number; max: number; ticks: number[] } {
|
|
271
|
+
const [lowBound, highBound] = options.domain ?? ["auto", "auto"];
|
|
272
|
+
const fitted = valueRange(data, series, { zero: options.zero });
|
|
273
|
+
const fixedLow = typeof lowBound === "number" && Number.isFinite(lowBound);
|
|
274
|
+
const fixedHigh = typeof highBound === "number" && Number.isFinite(highBound);
|
|
275
|
+
let low = fixedLow ? lowBound : fitted.min;
|
|
276
|
+
let high = fixedHigh ? highBound : fitted.max;
|
|
277
|
+
// A fixed end beyond the data on the other side: keep the fixed end and
|
|
278
|
+
// give the other one room.
|
|
279
|
+
if (low >= high) {
|
|
280
|
+
if (fixedLow && !fixedHigh) high = low + Math.abs(low || 1);
|
|
281
|
+
else if (fixedHigh && !fixedLow) low = high - Math.abs(high || 1);
|
|
282
|
+
else [low, high] = [Math.min(low, high), Math.max(low, high)];
|
|
283
|
+
}
|
|
284
|
+
const nice = niceTicks(low, high, options.tickCount);
|
|
285
|
+
const min = fixedLow ? low : nice.min;
|
|
286
|
+
const max = fixedHigh ? high : nice.max;
|
|
287
|
+
if (min === max) return { min, max: max + 1, ticks: [min, max + 1] };
|
|
288
|
+
const margin = nice.step / 2;
|
|
289
|
+
const inside = nice.ticks.filter(
|
|
290
|
+
(tick) =>
|
|
291
|
+
tick >= min &&
|
|
292
|
+
tick <= max &&
|
|
293
|
+
!(fixedLow && tick !== min && tick - min < margin) &&
|
|
294
|
+
!(fixedHigh && tick !== max && max - tick < margin),
|
|
295
|
+
);
|
|
296
|
+
const ticks = [
|
|
297
|
+
...(fixedLow && !inside.includes(min) ? [min] : []),
|
|
298
|
+
...inside,
|
|
299
|
+
...(fixedHigh && !inside.includes(max) ? [max] : []),
|
|
300
|
+
];
|
|
301
|
+
return { min, max, ticks };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Which category labels fit: every `step`-th one, the smallest step at which
|
|
306
|
+
* the widest label no longer reaches its neighbour.
|
|
307
|
+
*/
|
|
308
|
+
export function visibleCategories(
|
|
309
|
+
count: number,
|
|
310
|
+
widestLabel: number,
|
|
311
|
+
plotWidth: number,
|
|
312
|
+
): number[] {
|
|
313
|
+
if (count === 0) return [];
|
|
314
|
+
const slot = plotWidth / count;
|
|
315
|
+
const step =
|
|
316
|
+
slot <= 0
|
|
317
|
+
? count
|
|
318
|
+
: Math.max(1, Math.ceil((widestLabel + LABEL_GAP) / slot));
|
|
319
|
+
const visible: number[] = [];
|
|
320
|
+
for (let index = 0; index < count; index += step) visible.push(index);
|
|
321
|
+
return visible;
|
|
322
|
+
}
|
|
323
|
+
|
|
94
324
|
export const Chart = component<ChartProps>((props) => {
|
|
95
325
|
const titleId = uid("chart-title");
|
|
326
|
+
const plotId = uid("chart-plot");
|
|
327
|
+
const hintId = uid("chart-hint");
|
|
96
328
|
const height = props.height ?? 240;
|
|
97
329
|
const type = props.type ?? "line";
|
|
98
|
-
const
|
|
330
|
+
const data = props.data;
|
|
331
|
+
const series = props.series;
|
|
332
|
+
const count = data.length;
|
|
99
333
|
|
|
100
|
-
const
|
|
101
|
-
const
|
|
334
|
+
const numberFormat = new Intl.NumberFormat(props.locale);
|
|
335
|
+
const dateFormat = new Intl.DateTimeFormat(props.locale, {
|
|
336
|
+
dateStyle: "medium",
|
|
337
|
+
});
|
|
338
|
+
const formatValue =
|
|
339
|
+
props.formatValue ?? ((value: number) => numberFormat.format(value));
|
|
340
|
+
const formatCategory =
|
|
341
|
+
props.formatCategory ??
|
|
342
|
+
((category: ChartCategory) =>
|
|
343
|
+
category instanceof Date
|
|
344
|
+
? dateFormat.format(category)
|
|
345
|
+
: String(category));
|
|
102
346
|
|
|
103
|
-
const
|
|
104
|
-
|
|
347
|
+
const axis = valueAxis(data, series, {
|
|
348
|
+
domain: props.domain,
|
|
349
|
+
zero: type === "bar" || props.zero !== false,
|
|
350
|
+
tickCount: props.tickCount,
|
|
351
|
+
});
|
|
352
|
+
const reversed = props.reversed === true;
|
|
105
353
|
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
354
|
+
const top = PADDING.top;
|
|
355
|
+
const bottom = height - PADDING.bottom;
|
|
356
|
+
const plotHeight = Math.max(bottom - top, 1);
|
|
357
|
+
|
|
358
|
+
/** Pixels from the top for `value`. */
|
|
359
|
+
const y = (value: number): number => {
|
|
360
|
+
const share = (value - axis.min) / (axis.max - axis.min);
|
|
361
|
+
return top + (reversed ? share : 1 - share) * plotHeight;
|
|
111
362
|
};
|
|
363
|
+
/** Centre of category `index`, as a share of the plot's width. */
|
|
364
|
+
const centre = (index: number): number =>
|
|
365
|
+
count === 0 ? 0 : (index + 0.5) / count;
|
|
366
|
+
const percent = (share: number): string => `${clean(share * 100)}%`;
|
|
367
|
+
|
|
368
|
+
/** The line bars and areas grow from: zero when the axis shows it. */
|
|
369
|
+
const baselineValue = Math.min(Math.max(0, axis.min), axis.max);
|
|
370
|
+
const baseline = y(baselineValue);
|
|
112
371
|
|
|
113
|
-
const
|
|
114
|
-
|
|
372
|
+
const tickLabels = axis.ticks.map((tick) => formatValue(tick));
|
|
373
|
+
const gutter = Math.ceil(
|
|
374
|
+
Math.max(...tickLabels.map((text) => text.length), 1) * CHARACTER_WIDTH +
|
|
375
|
+
10,
|
|
115
376
|
);
|
|
377
|
+
const categoryLabels = data.map((datum) =>
|
|
378
|
+
formatCategory(categoryAt(datum, props.categoryKey)),
|
|
379
|
+
);
|
|
380
|
+
const widestCategory =
|
|
381
|
+
Math.max(...categoryLabels.map((text) => text.length), 0) * CHARACTER_WIDTH;
|
|
116
382
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
383
|
+
const plotWidth = signal(NOMINAL_PLOT_WIDTH);
|
|
384
|
+
/** The category the pointer or the keyboard points at. */
|
|
385
|
+
const active = signal<number | undefined>(undefined);
|
|
386
|
+
|
|
387
|
+
const valueText = (value: number | null): string =>
|
|
388
|
+
value === null ? "—" : formatValue(value);
|
|
389
|
+
|
|
390
|
+
/** What the tooltip shows and the live region says for `index`. */
|
|
391
|
+
function describe(index: number): string {
|
|
392
|
+
const datum = data[index];
|
|
393
|
+
if (datum === undefined) return "";
|
|
394
|
+
const values = series.map(
|
|
395
|
+
(entry) => `${entry.label}: ${valueText(numberAt(datum, entry.key))}`,
|
|
396
|
+
);
|
|
397
|
+
return `${categoryLabels[index] ?? ""} — ${values.join(", ")}`;
|
|
124
398
|
}
|
|
125
399
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
400
|
+
let observer: ResizeObserver | undefined;
|
|
401
|
+
onMount(() => {
|
|
402
|
+
if (typeof ResizeObserver !== "function") return;
|
|
403
|
+
const plot = document.getElementById(plotId);
|
|
404
|
+
if (plot === null) return;
|
|
405
|
+
observer = new ResizeObserver((entries) => {
|
|
406
|
+
const width = entries[0]?.contentRect.width;
|
|
407
|
+
if (width !== undefined && width > 0) plotWidth(width);
|
|
408
|
+
});
|
|
409
|
+
observer.observe(plot);
|
|
410
|
+
});
|
|
411
|
+
onUnmount(() => observer?.disconnect());
|
|
412
|
+
|
|
413
|
+
function pointAt(event: PointerEvent): void {
|
|
414
|
+
const plot = event.currentTarget;
|
|
415
|
+
if (!(plot instanceof Element) || count === 0) return;
|
|
416
|
+
const box = plot.getBoundingClientRect();
|
|
417
|
+
if (box.width <= 0) return;
|
|
418
|
+
const share = (event.clientX - box.left) / box.width;
|
|
419
|
+
active(Math.min(count - 1, Math.max(0, Math.floor(share * count))));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function onKeyDown(event: KeyboardEvent): void {
|
|
423
|
+
if (count === 0) return;
|
|
424
|
+
const current = active();
|
|
425
|
+
let next: number | undefined;
|
|
426
|
+
switch (event.key) {
|
|
427
|
+
case "ArrowRight":
|
|
428
|
+
case "ArrowDown":
|
|
429
|
+
next = current === undefined ? 0 : Math.min(count - 1, current + 1);
|
|
430
|
+
break;
|
|
431
|
+
case "ArrowLeft":
|
|
432
|
+
case "ArrowUp":
|
|
433
|
+
next = current === undefined ? count - 1 : Math.max(0, current - 1);
|
|
434
|
+
break;
|
|
435
|
+
case "Home":
|
|
436
|
+
next = 0;
|
|
437
|
+
break;
|
|
438
|
+
case "End":
|
|
439
|
+
next = count - 1;
|
|
440
|
+
break;
|
|
441
|
+
case "Escape":
|
|
442
|
+
if (current === undefined) return;
|
|
443
|
+
active(undefined);
|
|
444
|
+
event.preventDefault();
|
|
445
|
+
return;
|
|
446
|
+
default:
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
event.preventDefault();
|
|
450
|
+
active(next);
|
|
130
451
|
}
|
|
131
452
|
|
|
132
453
|
return html`<figure
|
|
133
454
|
data-slot="chart"
|
|
134
455
|
class="${() => cn("w-full", read(props.class))}"
|
|
135
456
|
>
|
|
136
|
-
<
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
y1="${position}"
|
|
152
|
-
y2="${position}"
|
|
153
|
-
stroke="var(--border)"
|
|
154
|
-
stroke-width="1"
|
|
155
|
-
/>`,
|
|
156
|
-
)
|
|
157
|
-
}
|
|
158
|
-
${props.series.map((series, seriesIndex) =>
|
|
159
|
-
type === "bar"
|
|
160
|
-
? renderBars(series, seriesIndex)
|
|
161
|
-
: renderCurve(series, seriesIndex),
|
|
162
|
-
)}
|
|
163
|
-
${props.data.map(
|
|
164
|
-
(datum, index) => html`<text
|
|
165
|
-
x="${x(index)}"
|
|
166
|
-
y="${height - 6}"
|
|
167
|
-
text-anchor="middle"
|
|
168
|
-
font-size="10"
|
|
457
|
+
<div class="flex w-full">
|
|
458
|
+
<svg
|
|
459
|
+
data-slot="chart-value-axis"
|
|
460
|
+
aria-hidden="true"
|
|
461
|
+
focusable="false"
|
|
462
|
+
width="${gutter}"
|
|
463
|
+
height="${height}"
|
|
464
|
+
class="shrink-0 overflow-visible"
|
|
465
|
+
>${axis.ticks.map(
|
|
466
|
+
(tick, index) => html`<g><text
|
|
467
|
+
x="${gutter - 6}"
|
|
468
|
+
y="${y(tick)}"
|
|
469
|
+
text-anchor="end"
|
|
470
|
+
dominant-baseline="middle"
|
|
471
|
+
font-size="${FONT_SIZE}"
|
|
169
472
|
fill="var(--muted-foreground)"
|
|
170
|
-
>${
|
|
171
|
-
)}
|
|
172
|
-
|
|
473
|
+
>${tickLabels[index]}</text></g>`,
|
|
474
|
+
)}</svg>
|
|
475
|
+
<div
|
|
476
|
+
id="${plotId}"
|
|
477
|
+
data-slot="chart-plot"
|
|
478
|
+
role="group"
|
|
479
|
+
aria-roledescription="${props.roleDescription ?? "chart"}"
|
|
480
|
+
aria-labelledby="${titleId}"
|
|
481
|
+
aria-describedby="${hintId}"
|
|
482
|
+
tabindex="0"
|
|
483
|
+
class="focus-visible:ring-ring/50 relative min-w-0 flex-1 rounded-sm outline-none focus-visible:ring-[3px]"
|
|
484
|
+
@pointermove="${pointAt}"
|
|
485
|
+
@pointerdown="${pointAt}"
|
|
486
|
+
@pointerleave="${() => active(undefined)}"
|
|
487
|
+
@keydown="${onKeyDown}"
|
|
488
|
+
@blur="${() => active(undefined)}"
|
|
489
|
+
>
|
|
490
|
+
<svg
|
|
491
|
+
aria-hidden="true"
|
|
492
|
+
focusable="false"
|
|
493
|
+
width="100%"
|
|
494
|
+
height="${height}"
|
|
495
|
+
class="block overflow-visible"
|
|
496
|
+
>
|
|
497
|
+
${props.grid === false ? null : axis.ticks.map(renderGridline)}
|
|
498
|
+
<svg
|
|
499
|
+
x="0"
|
|
500
|
+
y="${top - 4}"
|
|
501
|
+
width="100%"
|
|
502
|
+
height="${plotHeight + 8}"
|
|
503
|
+
viewBox="${`0 ${top - 4} ${Math.max(count, 1)} ${plotHeight + 8}`}"
|
|
504
|
+
preserveAspectRatio="none"
|
|
505
|
+
>${series.map((entry, index) =>
|
|
506
|
+
type === "bar"
|
|
507
|
+
? renderBars(entry, index)
|
|
508
|
+
: renderCurve(entry, index),
|
|
509
|
+
)}</svg>
|
|
510
|
+
${type === "bar" ? null : series.map(renderLonePoints)}
|
|
511
|
+
${() =>
|
|
512
|
+
visibleCategories(count, widestCategory, plotWidth()).map(
|
|
513
|
+
(index) => html`<g><text
|
|
514
|
+
x="${percent(centre(index))}"
|
|
515
|
+
y="${height - 6}"
|
|
516
|
+
text-anchor="middle"
|
|
517
|
+
font-size="${FONT_SIZE}"
|
|
518
|
+
fill="var(--muted-foreground)"
|
|
519
|
+
>${categoryLabels[index]}</text></g>`,
|
|
520
|
+
)}
|
|
521
|
+
${() => renderMarker(active())}
|
|
522
|
+
</svg>
|
|
523
|
+
${() => renderTooltip(active())}
|
|
524
|
+
</div>
|
|
525
|
+
</div>
|
|
526
|
+
<span id="${titleId}" class="sr-only">${props.label}</span>
|
|
527
|
+
<span id="${hintId}" class="sr-only">${
|
|
528
|
+
count === 0
|
|
529
|
+
? ""
|
|
530
|
+
: (props.keyboardHint ?? "Arrow keys read each category.")
|
|
531
|
+
}</span>
|
|
532
|
+
<div role="status" aria-live="polite" class="sr-only">${() => {
|
|
533
|
+
const index = active();
|
|
534
|
+
return index === undefined ? "" : describe(index);
|
|
535
|
+
}}</div>
|
|
173
536
|
<figcaption class="sr-only">${renderDataTable()}</figcaption>
|
|
174
537
|
<div data-slot="chart-legend" class="mt-2 flex flex-wrap justify-center gap-4 text-xs">
|
|
175
|
-
${
|
|
176
|
-
(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
class="size-2 shrink-0 rounded-[2px]"
|
|
180
|
-
style="${`background:${colorFor(series, index)}`}"
|
|
181
|
-
></span>
|
|
182
|
-
<span class="text-muted-foreground">${series.label}</span>
|
|
538
|
+
${series.map(
|
|
539
|
+
(entry, index) => html`<span class="flex items-center gap-1.5">
|
|
540
|
+
${swatch(colorFor(entry, index))}
|
|
541
|
+
<span class="text-muted-foreground">${entry.label}</span>
|
|
183
542
|
</span>`,
|
|
184
543
|
)}
|
|
185
544
|
</div>
|
|
186
545
|
</figure>`;
|
|
187
546
|
|
|
188
|
-
function
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
547
|
+
function swatch(color: string): TemplateResult {
|
|
548
|
+
return html`<svg
|
|
549
|
+
aria-hidden="true"
|
|
550
|
+
focusable="false"
|
|
551
|
+
viewBox="0 0 8 8"
|
|
552
|
+
class="size-2 shrink-0"
|
|
553
|
+
><rect width="8" height="8" rx="2" fill="${color}" /></svg>`;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function renderGridline(tick: number): TemplateResult {
|
|
557
|
+
return html`<g><line
|
|
558
|
+
x1="0"
|
|
559
|
+
x2="100%"
|
|
560
|
+
y1="${y(tick)}"
|
|
561
|
+
y2="${y(tick)}"
|
|
562
|
+
stroke="var(--border)"
|
|
563
|
+
stroke-width="1"
|
|
564
|
+
/></g>`;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* The runs of consecutive values, each one an unbroken stretch of line. A
|
|
569
|
+
* gap ends a run; nothing is drawn across it.
|
|
570
|
+
*/
|
|
571
|
+
function runsOf(entry: ChartSeries): { index: number; value: number }[][] {
|
|
572
|
+
const runs: { index: number; value: number }[][] = [];
|
|
573
|
+
let run: { index: number; value: number }[] = [];
|
|
574
|
+
data.forEach((datum, index) => {
|
|
575
|
+
const value = numberAt(datum, entry.key);
|
|
576
|
+
if (value === null) {
|
|
577
|
+
if (run.length > 0) runs.push(run);
|
|
578
|
+
run = [];
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
run.push({ index, value });
|
|
582
|
+
});
|
|
583
|
+
if (run.length > 0) runs.push(run);
|
|
584
|
+
return runs;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function renderCurve(entry: ChartSeries, index: number): TemplateResult {
|
|
588
|
+
const color = colorFor(entry, index);
|
|
589
|
+
const runs = runsOf(entry);
|
|
590
|
+
const line = runs
|
|
591
|
+
.map((run) =>
|
|
592
|
+
run
|
|
593
|
+
.map(
|
|
594
|
+
(point, at) =>
|
|
595
|
+
`${at === 0 ? "M" : "L"}${clean(point.index + 0.5)},${clean(y(point.value))}`,
|
|
596
|
+
)
|
|
597
|
+
.join(" "),
|
|
598
|
+
)
|
|
599
|
+
.join(" ");
|
|
600
|
+
const area = runs
|
|
601
|
+
.filter((run) => run.length > 1)
|
|
602
|
+
.map((run) => {
|
|
603
|
+
const first = run[0];
|
|
604
|
+
const last = run[run.length - 1];
|
|
605
|
+
if (first === undefined || last === undefined) return "";
|
|
606
|
+
const path = run
|
|
607
|
+
.map(
|
|
608
|
+
(point, at) =>
|
|
609
|
+
`${at === 0 ? "M" : "L"}${clean(point.index + 0.5)},${clean(y(point.value))}`,
|
|
610
|
+
)
|
|
611
|
+
.join(" ");
|
|
612
|
+
return `${path} L${clean(last.index + 0.5)},${clean(baseline)} L${clean(first.index + 0.5)},${clean(baseline)} Z`;
|
|
613
|
+
})
|
|
614
|
+
.join(" ");
|
|
615
|
+
return html`<g data-series="${entry.key}">
|
|
194
616
|
${
|
|
195
|
-
type === "area"
|
|
196
|
-
? html`<path d="${
|
|
617
|
+
type === "area" && area !== ""
|
|
618
|
+
? html`<path d="${area}" fill="${color}" fill-opacity="0.15" />`
|
|
197
619
|
: null
|
|
198
620
|
}
|
|
199
621
|
<path
|
|
200
|
-
d="${
|
|
622
|
+
d="${line}"
|
|
201
623
|
fill="none"
|
|
202
624
|
stroke="${color}"
|
|
203
625
|
stroke-width="2"
|
|
204
626
|
stroke-linecap="round"
|
|
205
627
|
stroke-linejoin="round"
|
|
628
|
+
vector-effect="non-scaling-stroke"
|
|
206
629
|
/>
|
|
207
630
|
</g>`;
|
|
208
631
|
}
|
|
209
632
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
633
|
+
/**
|
|
634
|
+
* A value with a gap on both sides has no segment to be part of; drawn as a
|
|
635
|
+
* line it would be invisible. A dot shows it exists.
|
|
636
|
+
*/
|
|
637
|
+
function renderLonePoints(entry: ChartSeries, index: number): TemplateResult {
|
|
638
|
+
const lone = runsOf(entry).filter((run) => run.length === 1);
|
|
639
|
+
return html`<g data-lone="${entry.key}" fill="${colorFor(entry, index)}">${lone.map(
|
|
640
|
+
(run) => {
|
|
641
|
+
const point = run[0];
|
|
642
|
+
if (point === undefined) return null;
|
|
643
|
+
return html`<circle
|
|
644
|
+
cx="${percent(centre(point.index))}"
|
|
645
|
+
cy="${y(point.value)}"
|
|
646
|
+
r="2.5"
|
|
647
|
+
/>`;
|
|
648
|
+
},
|
|
649
|
+
)}</g>`;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function renderBars(entry: ChartSeries, seriesIndex: number): TemplateResult {
|
|
216
653
|
// Share each slot between the series so grouped bars sit side by side,
|
|
217
654
|
// with a quarter of the slot left as the gap between groups.
|
|
218
|
-
const barWidth =
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
const left =
|
|
225
|
-
PADDING.left + slot * index + slot * 0.125 + barWidth * seriesIndex;
|
|
655
|
+
const barWidth = 0.75 / Math.max(series.length, 1);
|
|
656
|
+
return html`<g data-series="${entry.key}" fill="${colorFor(entry, seriesIndex)}">
|
|
657
|
+
${data.map((datum, index) => {
|
|
658
|
+
const value = numberAt(datum, entry.key);
|
|
659
|
+
if (value === null) return null;
|
|
660
|
+
const tip = y(value);
|
|
226
661
|
return html`<rect
|
|
227
|
-
x="${
|
|
228
|
-
y="${Math.min(
|
|
229
|
-
width="${barWidth}"
|
|
230
|
-
height="${Math.abs(baseline -
|
|
231
|
-
rx="2"
|
|
662
|
+
x="${clean(index + 0.125 + barWidth * seriesIndex)}"
|
|
663
|
+
y="${clean(Math.min(tip, baseline))}"
|
|
664
|
+
width="${clean(barWidth)}"
|
|
665
|
+
height="${clean(Math.abs(baseline - tip))}"
|
|
232
666
|
/>`;
|
|
233
667
|
})}
|
|
234
668
|
</g>`;
|
|
235
669
|
}
|
|
236
670
|
|
|
671
|
+
/** The guide under the pointer, and a dot on each value it crosses. */
|
|
672
|
+
function renderMarker(index: number | undefined): TemplateResult | null {
|
|
673
|
+
const datum = index === undefined ? undefined : data[index];
|
|
674
|
+
if (index === undefined || datum === undefined) return null;
|
|
675
|
+
const x = percent(centre(index));
|
|
676
|
+
return html`<g data-slot="chart-marker">
|
|
677
|
+
<line
|
|
678
|
+
x1="${x}"
|
|
679
|
+
x2="${x}"
|
|
680
|
+
y1="${top}"
|
|
681
|
+
y2="${bottom}"
|
|
682
|
+
stroke="var(--muted-foreground)"
|
|
683
|
+
stroke-width="1"
|
|
684
|
+
stroke-dasharray="3 3"
|
|
685
|
+
/>
|
|
686
|
+
${
|
|
687
|
+
type === "bar"
|
|
688
|
+
? null
|
|
689
|
+
: series.map((entry, seriesIndex) => {
|
|
690
|
+
const value = numberAt(datum, entry.key);
|
|
691
|
+
if (value === null) return null;
|
|
692
|
+
return html`<circle
|
|
693
|
+
cx="${x}"
|
|
694
|
+
cy="${y(value)}"
|
|
695
|
+
r="4"
|
|
696
|
+
fill="${colorFor(entry, seriesIndex)}"
|
|
697
|
+
stroke="var(--background)"
|
|
698
|
+
stroke-width="2"
|
|
699
|
+
/>`;
|
|
700
|
+
})
|
|
701
|
+
}
|
|
702
|
+
</g>`;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* The values at the pointed category. Hidden from assistive technology —
|
|
707
|
+
* the live region says the same — and placed through `.style`, which goes
|
|
708
|
+
* through the CSSOM: a `style` attribute would be blocked by a strict CSP.
|
|
709
|
+
*/
|
|
710
|
+
function renderTooltip(index: number | undefined): TemplateResult | null {
|
|
711
|
+
const datum = index === undefined ? undefined : data[index];
|
|
712
|
+
if (index === undefined || datum === undefined) return null;
|
|
713
|
+
const share = centre(index);
|
|
714
|
+
const side =
|
|
715
|
+
share < 0.2
|
|
716
|
+
? "translate-x-2"
|
|
717
|
+
: share > 0.8
|
|
718
|
+
? "-translate-x-full -ml-2"
|
|
719
|
+
: "-translate-x-1/2";
|
|
720
|
+
return html`<div
|
|
721
|
+
data-slot="chart-tooltip"
|
|
722
|
+
aria-hidden="true"
|
|
723
|
+
class="${cn(
|
|
724
|
+
"bg-popover text-popover-foreground pointer-events-none absolute top-0 z-10 grid min-w-32 gap-1 rounded-md border px-2.5 py-1.5 text-xs shadow-md",
|
|
725
|
+
side,
|
|
726
|
+
)}"
|
|
727
|
+
.style="${`left:${percent(share)}`}"
|
|
728
|
+
>
|
|
729
|
+
<div class="font-medium">${categoryLabels[index]}</div>
|
|
730
|
+
${series.map(
|
|
731
|
+
(entry, seriesIndex) => html`<div class="flex items-center gap-1.5">
|
|
732
|
+
${swatch(colorFor(entry, seriesIndex))}
|
|
733
|
+
<span class="text-muted-foreground">${entry.label}</span>
|
|
734
|
+
<span class="ml-auto font-mono font-medium tabular-nums">${valueText(
|
|
735
|
+
numberAt(datum, entry.key),
|
|
736
|
+
)}</span>
|
|
737
|
+
</div>`,
|
|
738
|
+
)}
|
|
739
|
+
</div>`;
|
|
740
|
+
}
|
|
741
|
+
|
|
237
742
|
/** The same numbers as a real table, for anything that cannot see the SVG. */
|
|
238
|
-
function renderDataTable():
|
|
743
|
+
function renderDataTable(): TemplateResult {
|
|
239
744
|
return html`<table>
|
|
240
745
|
<caption>${props.label}</caption>
|
|
241
746
|
<thead>
|
|
242
747
|
<tr>
|
|
243
748
|
<th scope="col">${props.categoryKey}</th>
|
|
244
|
-
${
|
|
749
|
+
${series.map((entry) => html`<th scope="col">${entry.label}</th>`)}
|
|
245
750
|
</tr>
|
|
246
751
|
</thead>
|
|
247
752
|
<tbody>
|
|
248
|
-
${
|
|
249
|
-
(datum) => html`<tr>
|
|
250
|
-
<th scope="row">${
|
|
251
|
-
${
|
|
753
|
+
${data.map(
|
|
754
|
+
(datum, index) => html`<tr>
|
|
755
|
+
<th scope="row">${categoryLabels[index]}</th>
|
|
756
|
+
${series.map(
|
|
757
|
+
(entry) =>
|
|
758
|
+
html`<td>${valueText(numberAt(datum, entry.key))}</td>`,
|
|
759
|
+
)}
|
|
252
760
|
</tr>`,
|
|
253
761
|
)}
|
|
254
762
|
</tbody>
|