@vitavision/charts 0.6.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/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +38 -0
- package/dist/index.d.ts +420 -0
- package/dist/index.js +674 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
- package/src/BarChart.tsx +83 -0
- package/src/Frame.tsx +308 -0
- package/src/Histogram.tsx +132 -0
- package/src/LineChart.tsx +136 -0
- package/src/LineProfile.tsx +164 -0
- package/src/index.ts +44 -0
- package/src/scale.ts +236 -0
- package/src/styles.css +44 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
import { cn, toneColor } from "@vitavision/ui";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
//#region src/Frame.tsx
|
|
4
|
+
const GEOMETRY = {
|
|
5
|
+
panel: {
|
|
6
|
+
width: 480,
|
|
7
|
+
height: 280
|
|
8
|
+
},
|
|
9
|
+
wide: {
|
|
10
|
+
width: 960,
|
|
11
|
+
height: 320
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const MARGIN = {
|
|
15
|
+
left: 56,
|
|
16
|
+
right: 12,
|
|
17
|
+
top: 12,
|
|
18
|
+
bottom: 34
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The drawable rectangle for one variant, in that variant's viewBox units.
|
|
22
|
+
*
|
|
23
|
+
* @param variant - Which viewBox the chart is drawn in.
|
|
24
|
+
* @returns The plot area's edges — `x0`/`x1` left to right, `y0` the bottom and `y1` the top
|
|
25
|
+
* (SVG y grows downwards) — and the whole viewBox's `width`/`height`. Build scales over
|
|
26
|
+
* `[x0, x1]` and `[y0, y1]`.
|
|
27
|
+
*/
|
|
28
|
+
function areaFor(variant) {
|
|
29
|
+
const { width, height } = GEOMETRY[variant];
|
|
30
|
+
return {
|
|
31
|
+
x0: MARGIN.left,
|
|
32
|
+
x1: width - MARGIN.right,
|
|
33
|
+
y0: height - MARGIN.bottom,
|
|
34
|
+
y1: MARGIN.top,
|
|
35
|
+
width,
|
|
36
|
+
height
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** The default geometry, for callers that do not care which variant they are in. */
|
|
40
|
+
const plotArea = areaFor("panel");
|
|
41
|
+
/** Held at the two ends of the neutral ramp, matching the design system's chrome colours. */
|
|
42
|
+
const AXIS = "currentColor";
|
|
43
|
+
/**
|
|
44
|
+
* The plot shell every chart here composes: grid, ticks, axes, axis titles, a footer.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* Renders a `<figure>` holding an SVG with `role="img"` named by `label`, and the `footer`
|
|
48
|
+
* as its `<figcaption>`. `children` are drawn in viewBox units over the grid.
|
|
49
|
+
*/
|
|
50
|
+
function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, children, label, footer, variant = "panel", className }) {
|
|
51
|
+
const horizontal = xScale.ticks(xTicks);
|
|
52
|
+
const vertical = yScale.ticks(yTicks);
|
|
53
|
+
const plotArea = areaFor(variant);
|
|
54
|
+
const PLOT = GEOMETRY[variant];
|
|
55
|
+
return /* @__PURE__ */ jsxs("figure", {
|
|
56
|
+
className: cn("flex flex-col gap-1 text-fg-muted", className),
|
|
57
|
+
children: [/* @__PURE__ */ jsxs("svg", {
|
|
58
|
+
role: "img",
|
|
59
|
+
"aria-label": label,
|
|
60
|
+
viewBox: `0 0 ${PLOT.width} ${PLOT.height}`,
|
|
61
|
+
className: "w-full",
|
|
62
|
+
children: [
|
|
63
|
+
vertical.map((tick) => {
|
|
64
|
+
const y = yScale.project(tick.value);
|
|
65
|
+
return /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("line", {
|
|
66
|
+
x1: plotArea.x0,
|
|
67
|
+
x2: plotArea.x1,
|
|
68
|
+
y1: y,
|
|
69
|
+
y2: y,
|
|
70
|
+
stroke: AXIS,
|
|
71
|
+
strokeWidth: .5,
|
|
72
|
+
opacity: .25
|
|
73
|
+
}), /* @__PURE__ */ jsx("text", {
|
|
74
|
+
x: plotArea.x0 - 6,
|
|
75
|
+
y: y + 3.5,
|
|
76
|
+
textAnchor: "end",
|
|
77
|
+
fontSize: 10,
|
|
78
|
+
fill: AXIS,
|
|
79
|
+
children: tick.label
|
|
80
|
+
})] }, `y-${tick.value}`);
|
|
81
|
+
}),
|
|
82
|
+
horizontal.map((tick) => {
|
|
83
|
+
const x = xScale.project(tick.value);
|
|
84
|
+
return /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("line", {
|
|
85
|
+
x1: x,
|
|
86
|
+
x2: x,
|
|
87
|
+
y1: plotArea.y0,
|
|
88
|
+
y2: plotArea.y1,
|
|
89
|
+
stroke: AXIS,
|
|
90
|
+
strokeWidth: .5,
|
|
91
|
+
opacity: .15
|
|
92
|
+
}), /* @__PURE__ */ jsx("text", {
|
|
93
|
+
x,
|
|
94
|
+
y: plotArea.y0 + 14,
|
|
95
|
+
textAnchor: "middle",
|
|
96
|
+
fontSize: 10,
|
|
97
|
+
fill: AXIS,
|
|
98
|
+
children: tick.label
|
|
99
|
+
})] }, `x-${tick.value}`);
|
|
100
|
+
}),
|
|
101
|
+
/* @__PURE__ */ jsx("line", {
|
|
102
|
+
x1: plotArea.x0,
|
|
103
|
+
x2: plotArea.x1,
|
|
104
|
+
y1: plotArea.y0,
|
|
105
|
+
y2: plotArea.y0,
|
|
106
|
+
stroke: AXIS,
|
|
107
|
+
strokeWidth: 1,
|
|
108
|
+
opacity: .6
|
|
109
|
+
}),
|
|
110
|
+
/* @__PURE__ */ jsx("line", {
|
|
111
|
+
x1: plotArea.x0,
|
|
112
|
+
x2: plotArea.x0,
|
|
113
|
+
y1: plotArea.y0,
|
|
114
|
+
y2: plotArea.y1,
|
|
115
|
+
stroke: AXIS,
|
|
116
|
+
strokeWidth: 1,
|
|
117
|
+
opacity: .6
|
|
118
|
+
}),
|
|
119
|
+
children,
|
|
120
|
+
xLabel && /* @__PURE__ */ jsx("text", {
|
|
121
|
+
x: (plotArea.x0 + plotArea.x1) / 2,
|
|
122
|
+
y: PLOT.height - 2,
|
|
123
|
+
textAnchor: "middle",
|
|
124
|
+
fontSize: 10,
|
|
125
|
+
fill: AXIS,
|
|
126
|
+
children: xLabel
|
|
127
|
+
}),
|
|
128
|
+
yLabel && /* @__PURE__ */ jsx("text", {
|
|
129
|
+
transform: `translate(10 ${(plotArea.y0 + plotArea.y1) / 2}) rotate(-90)`,
|
|
130
|
+
textAnchor: "middle",
|
|
131
|
+
fontSize: 10,
|
|
132
|
+
fill: AXIS,
|
|
133
|
+
children: yLabel
|
|
134
|
+
})
|
|
135
|
+
]
|
|
136
|
+
}), footer && /* @__PURE__ */ jsx("figcaption", {
|
|
137
|
+
className: "text-xs",
|
|
138
|
+
children: footer
|
|
139
|
+
})]
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* A legend row. Kept beside the frame rather than inside the SVG so the swatches are real
|
|
144
|
+
* text and CSS rather than a second typography in viewBox units.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
* The swatches are `aria-hidden`; each series is identified by its name, never by colour
|
|
148
|
+
* alone.
|
|
149
|
+
*/
|
|
150
|
+
function Legend({ items, className }) {
|
|
151
|
+
return /* @__PURE__ */ jsx("ul", {
|
|
152
|
+
className: cn("flex flex-wrap items-center gap-x-4 gap-y-1", className),
|
|
153
|
+
children: items.map((item) => /* @__PURE__ */ jsxs("li", {
|
|
154
|
+
className: "flex items-center gap-1.5",
|
|
155
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
156
|
+
"aria-hidden": true,
|
|
157
|
+
className: "inline-block h-0.5 w-4 rounded",
|
|
158
|
+
style: { backgroundColor: item.colour }
|
|
159
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
160
|
+
className: "font-mono text-xs",
|
|
161
|
+
children: item.label
|
|
162
|
+
})]
|
|
163
|
+
}, item.label))
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The shared categorical series palette, as CSS paint values: `var(--series-1)` …
|
|
168
|
+
* `var(--series-6)`.
|
|
169
|
+
*
|
|
170
|
+
* @remarks
|
|
171
|
+
* The colours themselves are tokens in `@vitavision/charts/styles.css`, defined for both
|
|
172
|
+
* themes, so that stylesheet must be imported after `@vitavision/ui/styles.css`. The chrome
|
|
173
|
+
* carries no saturation, which makes this the one place a chart is allowed to be loud.
|
|
174
|
+
*
|
|
175
|
+
* Each slot keeps its hue family across the themes — cyan, orange, violet, blue, pink, then
|
|
176
|
+
* a neutral grey as the sixth — and only its lightness changes, so every colour holds 3:1
|
|
177
|
+
* against the panel in both. The six stay distinguishable under simulated protanopia,
|
|
178
|
+
* deuteranopia and tritanopia, and none of them is a verdict colour (`--normal`,
|
|
179
|
+
* `--defect`, `--warn`): a series is never read as a verdict (PLAN §5).
|
|
180
|
+
*
|
|
181
|
+
* Before 0.6 these were fixed hex strings; the names are unchanged.
|
|
182
|
+
*/
|
|
183
|
+
const SERIES_COLOURS = [
|
|
184
|
+
"var(--series-1)",
|
|
185
|
+
"var(--series-2)",
|
|
186
|
+
"var(--series-3)",
|
|
187
|
+
"var(--series-4)",
|
|
188
|
+
"var(--series-5)",
|
|
189
|
+
"var(--series-6)"
|
|
190
|
+
];
|
|
191
|
+
/**
|
|
192
|
+
* The paint for outcomes judged normal — `var(--normal)`, the design system's verdict token.
|
|
193
|
+
*
|
|
194
|
+
* @remarks
|
|
195
|
+
* Exported so a histogram and any caught/missed bars share one green rather than each
|
|
196
|
+
* restating it: a green that means "normal" has to be one green. Before 0.6 this was a
|
|
197
|
+
* fixed hex string.
|
|
198
|
+
*/
|
|
199
|
+
const NORMAL_COLOUR = "var(--normal)";
|
|
200
|
+
/**
|
|
201
|
+
* The paint for outcomes judged defective — `var(--defect)`, the design system's verdict
|
|
202
|
+
* token.
|
|
203
|
+
*
|
|
204
|
+
* @remarks
|
|
205
|
+
* The partner of {@link NORMAL_COLOUR}. Before 0.6 this was a fixed hex string.
|
|
206
|
+
*/
|
|
207
|
+
const DEFECT_COLOUR = "var(--defect)";
|
|
208
|
+
/**
|
|
209
|
+
* The series colour for the `index`-th series, cycling through {@link SERIES_COLOURS}.
|
|
210
|
+
*
|
|
211
|
+
* @param index - Zero-based series index. Negative or non-integer indices fall back to the
|
|
212
|
+
* neutral `var(--fg-muted)`.
|
|
213
|
+
* @returns A CSS paint value, usable as an SVG `fill`/`stroke` or a CSS `color`.
|
|
214
|
+
*/
|
|
215
|
+
function seriesColour(index) {
|
|
216
|
+
return SERIES_COLOURS[index % SERIES_COLOURS.length] ?? "var(--fg-muted)";
|
|
217
|
+
}
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/scale.ts
|
|
220
|
+
/** Smallest value a log scale will accept, so a zero loss cannot become `-Infinity`. */
|
|
221
|
+
const LOG_FLOOR = 1e-12;
|
|
222
|
+
const TICK_STEPS = [
|
|
223
|
+
1,
|
|
224
|
+
2,
|
|
225
|
+
2.5,
|
|
226
|
+
5,
|
|
227
|
+
10
|
|
228
|
+
];
|
|
229
|
+
/** Past this a label is noise; anything finer is shown as an exponent instead. */
|
|
230
|
+
const MAX_DECIMALS = 6;
|
|
231
|
+
/**
|
|
232
|
+
* A "nice" step at or above `rough` — one of 1, 2, 2.5 or 5 times a power of ten.
|
|
233
|
+
*
|
|
234
|
+
* Axis labels a reader can hold in their head are the whole reason: 0.25 / 0.50 / 0.75
|
|
235
|
+
* rather than 0.237 / 0.474 / 0.711.
|
|
236
|
+
*/
|
|
237
|
+
function niceStep(rough) {
|
|
238
|
+
if (!Number.isFinite(rough) || rough <= 0) return 1;
|
|
239
|
+
const magnitude = 10 ** Math.floor(Math.log10(rough));
|
|
240
|
+
const normalized = rough / magnitude;
|
|
241
|
+
return (TICK_STEPS.find((candidate) => candidate >= normalized - 1e-9) ?? 10) * magnitude;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* How many decimals a label needs so that neighbouring ticks do not print identically.
|
|
245
|
+
*
|
|
246
|
+
* Derived from the step rather than fixed, because one chart here might be a loss around
|
|
247
|
+
* 1e-4 and another a residual in whole pixels. Enough decimals to print the *step* exactly
|
|
248
|
+
* is the right rule, and `ceil(-log10(step))` is not it: a 0.25 step gets one decimal that
|
|
249
|
+
* way, and the axis reads 0.3 / 0.5 / 0.8 — three labels for four evenly spaced ticks.
|
|
250
|
+
*/
|
|
251
|
+
function decimalsFor(step) {
|
|
252
|
+
if (!Number.isFinite(step) || step <= 0) return 0;
|
|
253
|
+
for (let decimals = 0; decimals < MAX_DECIMALS; decimals += 1) if (Math.abs(Number(step.toFixed(decimals)) - step) < step * 1e-6) return decimals;
|
|
254
|
+
return MAX_DECIMALS;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* The label for one tick: as many decimals as the step needs, or an exponent for a
|
|
258
|
+
* magnitude below 1e-3 or from 1e6 upwards.
|
|
259
|
+
*
|
|
260
|
+
* @param value - The tick's value.
|
|
261
|
+
* @param step - The spacing between neighbouring ticks; it decides the decimals.
|
|
262
|
+
* @returns The label, or `""` for a non-finite value.
|
|
263
|
+
*/
|
|
264
|
+
function formatTick(value, step) {
|
|
265
|
+
if (!Number.isFinite(value)) return "";
|
|
266
|
+
const magnitude = Math.abs(value);
|
|
267
|
+
if (magnitude !== 0 && (magnitude < .001 || magnitude >= 1e6)) return value.toExponential(1);
|
|
268
|
+
return value.toFixed(decimalsFor(step));
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Widen a degenerate domain into one that can be drawn.
|
|
272
|
+
*
|
|
273
|
+
* A single-point series, or a constant one, has `min === max`. Left alone that divides by
|
|
274
|
+
* zero and the line vanishes; widened, the point sits in the middle of a sensible axis,
|
|
275
|
+
* which is what watching the first moments of a training run, or a profile with one sample
|
|
276
|
+
* so far, should look like.
|
|
277
|
+
*/
|
|
278
|
+
function padDomain(min, max) {
|
|
279
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) return [0, 1];
|
|
280
|
+
if (min === max) {
|
|
281
|
+
const margin = Math.abs(min) > 0 ? Math.abs(min) * .1 : 1;
|
|
282
|
+
return [min - margin, max + margin];
|
|
283
|
+
}
|
|
284
|
+
return [min, max];
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The `[min, max]` of the finite values, ignoring `NaN` and infinities.
|
|
288
|
+
*
|
|
289
|
+
* @returns `[0, 1]` when there is no finite value, so an empty series still has an axis.
|
|
290
|
+
*/
|
|
291
|
+
function extent(values) {
|
|
292
|
+
const finite = values.filter((value) => Number.isFinite(value));
|
|
293
|
+
if (finite.length === 0) return [0, 1];
|
|
294
|
+
return [Math.min(...finite), Math.max(...finite)];
|
|
295
|
+
}
|
|
296
|
+
/** A linear scale from `domain` onto `[rangeStart, rangeEnd]` pixels. */
|
|
297
|
+
function linearScale(domain, rangeStart, rangeEnd) {
|
|
298
|
+
const [low, high] = padDomain(domain[0], domain[1]);
|
|
299
|
+
const span = high - low;
|
|
300
|
+
return {
|
|
301
|
+
domain: [low, high],
|
|
302
|
+
project: (value) => rangeStart + (value - low) / span * (rangeEnd - rangeStart),
|
|
303
|
+
ticks: (count = 5) => {
|
|
304
|
+
const step = niceStep(span / Math.max(1, count));
|
|
305
|
+
const first = Math.ceil(low / step) * step;
|
|
306
|
+
const ticks = [];
|
|
307
|
+
for (let value = first; value <= high + step * 1e-9; value += step) {
|
|
308
|
+
const exact = Math.round(value / step) * step;
|
|
309
|
+
ticks.push({
|
|
310
|
+
value: exact,
|
|
311
|
+
label: formatTick(exact, step)
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return ticks;
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* A base-10 log scale, for series that span decades.
|
|
320
|
+
*
|
|
321
|
+
* A loss curve is the canonical case: three terms that span decades and start far apart
|
|
322
|
+
* are a flat line along the bottom for two of the three on a linear axis. Non-positive
|
|
323
|
+
* values are clamped to `LOG_FLOOR` rather than dropped: a value that reaches exactly zero
|
|
324
|
+
* is a real observation, and a gap in the line would read as missing data.
|
|
325
|
+
*/
|
|
326
|
+
function logScale(domain, rangeStart, rangeEnd) {
|
|
327
|
+
const [paddedLow, paddedHigh] = padDomain(Math.log10(Math.max(domain[0], LOG_FLOOR)), Math.log10(Math.max(domain[1], LOG_FLOOR)));
|
|
328
|
+
const span = paddedHigh - paddedLow;
|
|
329
|
+
return {
|
|
330
|
+
domain: [10 ** paddedLow, 10 ** paddedHigh],
|
|
331
|
+
project: (value) => {
|
|
332
|
+
return rangeStart + (Math.log10(Math.max(value, LOG_FLOOR)) - paddedLow) / span * (rangeEnd - rangeStart);
|
|
333
|
+
},
|
|
334
|
+
ticks: (count = 5) => {
|
|
335
|
+
const stride = Math.max(1, Math.ceil(span / Math.max(1, count)));
|
|
336
|
+
const ticks = [];
|
|
337
|
+
for (let exponent = Math.ceil(paddedLow); exponent <= paddedHigh + 1e-9; exponent += stride) {
|
|
338
|
+
const value = 10 ** exponent;
|
|
339
|
+
ticks.push({
|
|
340
|
+
value,
|
|
341
|
+
label: formatTick(value, value)
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return ticks;
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
/** An SVG `d` attribute through the projected points, skipping non-finite ones. */
|
|
349
|
+
function linePath(points, xScale, yScale) {
|
|
350
|
+
const commands = [];
|
|
351
|
+
let pen = "M";
|
|
352
|
+
for (const point of points) {
|
|
353
|
+
if (!Number.isFinite(point.x) || !Number.isFinite(point.y)) {
|
|
354
|
+
pen = "M";
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
commands.push(`${pen}${xScale.project(point.x).toFixed(2)} ${yScale.project(point.y).toFixed(2)}`);
|
|
358
|
+
pen = "L";
|
|
359
|
+
}
|
|
360
|
+
return commands.join(" ");
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Bin values into `count` equal-width buckets over their own extent.
|
|
364
|
+
*
|
|
365
|
+
* Used for the score histogram, where the two classes must share one set of edges or the
|
|
366
|
+
* bars cannot be compared. Callers pass the shared domain in for exactly that reason.
|
|
367
|
+
*/
|
|
368
|
+
function histogram(values, domain, count) {
|
|
369
|
+
const bins = new Array(Math.max(1, count)).fill(0);
|
|
370
|
+
const [low, high] = domain;
|
|
371
|
+
const span = high - low;
|
|
372
|
+
if (span <= 0) {
|
|
373
|
+
bins[0] = values.length;
|
|
374
|
+
return bins;
|
|
375
|
+
}
|
|
376
|
+
for (const value of values) {
|
|
377
|
+
if (!Number.isFinite(value)) continue;
|
|
378
|
+
const index = Math.min(bins.length - 1, Math.floor((value - low) / span * bins.length));
|
|
379
|
+
if (index >= 0) bins[index] = (bins[index] ?? 0) + 1;
|
|
380
|
+
}
|
|
381
|
+
return bins;
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/LineChart.tsx
|
|
385
|
+
/**
|
|
386
|
+
* A multi-series line chart over a shared x axis — training curves, ROC and PR curves.
|
|
387
|
+
*
|
|
388
|
+
* @remarks
|
|
389
|
+
* An SVG `role="img"` named by `label`, with the legend as real text under it. Series are
|
|
390
|
+
* told apart by the legend's names, never by colour alone.
|
|
391
|
+
*/
|
|
392
|
+
function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDomain, underlay, footer, showLegend = true, variant = "panel", className }) {
|
|
393
|
+
const allX = series.flatMap((entry) => entry.points.map((point) => point.x));
|
|
394
|
+
const allY = series.flatMap((entry) => entry.points.map((point) => point.y));
|
|
395
|
+
const plotArea = areaFor(variant);
|
|
396
|
+
const xScale = linearScale(xDomain ?? extent(allX), plotArea.x0, plotArea.x1);
|
|
397
|
+
const yScale = (logY ? logScale : linearScale)(yDomain ?? extent(allY), plotArea.y0, plotArea.y1);
|
|
398
|
+
const coloured = series.map((entry, index) => ({
|
|
399
|
+
...entry,
|
|
400
|
+
colour: entry.colour ?? seriesColour(index)
|
|
401
|
+
}));
|
|
402
|
+
return /* @__PURE__ */ jsxs(Frame, {
|
|
403
|
+
xScale,
|
|
404
|
+
yScale,
|
|
405
|
+
xLabel,
|
|
406
|
+
yLabel,
|
|
407
|
+
label,
|
|
408
|
+
variant,
|
|
409
|
+
className,
|
|
410
|
+
footer: /* @__PURE__ */ jsxs("div", {
|
|
411
|
+
className: "flex flex-col gap-1",
|
|
412
|
+
children: [showLegend && /* @__PURE__ */ jsx(Legend, { items: coloured.map((entry) => ({
|
|
413
|
+
label: entry.name,
|
|
414
|
+
colour: entry.colour
|
|
415
|
+
})) }), footer]
|
|
416
|
+
}),
|
|
417
|
+
children: [underlay?.(xScale, yScale), coloured.map((entry) => entry.points.length === 1 ? /* @__PURE__ */ jsx("circle", {
|
|
418
|
+
cx: xScale.project(entry.points[0]?.x ?? 0),
|
|
419
|
+
cy: yScale.project(entry.points[0]?.y ?? 0),
|
|
420
|
+
r: 2.5,
|
|
421
|
+
fill: entry.colour
|
|
422
|
+
}, entry.name) : /* @__PURE__ */ jsx("path", {
|
|
423
|
+
d: linePath(entry.points, xScale, yScale),
|
|
424
|
+
fill: "none",
|
|
425
|
+
stroke: entry.colour,
|
|
426
|
+
strokeWidth: 1.5,
|
|
427
|
+
strokeLinejoin: "round",
|
|
428
|
+
vectorEffect: "non-scaling-stroke"
|
|
429
|
+
}, entry.name))]
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region src/BarChart.tsx
|
|
434
|
+
/**
|
|
435
|
+
* Horizontal stacked bars — a per-category breakdown.
|
|
436
|
+
*
|
|
437
|
+
* Horizontal because the categories are free text — a defect type, a part family — and a
|
|
438
|
+
* vertical axis would either truncate them or turn them sideways. Stacked because the
|
|
439
|
+
* usual question is "of this category, how many landed in each outcome", which is a
|
|
440
|
+
* proportion of a known total rather than independent counts.
|
|
441
|
+
*/
|
|
442
|
+
/**
|
|
443
|
+
* Horizontal stacked bars, one row per category, all on one shared scale (the widest row's
|
|
444
|
+
* total).
|
|
445
|
+
*
|
|
446
|
+
* @remarks
|
|
447
|
+
* Exposed as an ARIA `table`: each row has a `rowheader` (the category) and a text cell that
|
|
448
|
+
* spells every non-zero segment out as `"<value> <name>"`, so nothing depends on colour or
|
|
449
|
+
* on bar width. A row with no non-zero segment shows its total.
|
|
450
|
+
*/
|
|
451
|
+
function StackedBars({ rows, label, className }) {
|
|
452
|
+
const widest = Math.max(1, ...rows.map((row) => row.segments.reduce((total, segment) => total + segment.value, 0)));
|
|
453
|
+
return /* @__PURE__ */ jsx("div", {
|
|
454
|
+
className: cn("flex flex-col gap-2", className),
|
|
455
|
+
role: "table",
|
|
456
|
+
"aria-label": label,
|
|
457
|
+
children: rows.map((row) => {
|
|
458
|
+
const total = row.segments.reduce((sum, segment) => sum + segment.value, 0);
|
|
459
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
460
|
+
className: "flex items-center gap-3 text-xs",
|
|
461
|
+
role: "row",
|
|
462
|
+
children: [
|
|
463
|
+
/* @__PURE__ */ jsx("span", {
|
|
464
|
+
className: "w-28 shrink-0 truncate font-mono",
|
|
465
|
+
role: "rowheader",
|
|
466
|
+
title: row.label,
|
|
467
|
+
children: row.label
|
|
468
|
+
}),
|
|
469
|
+
/* @__PURE__ */ jsx("div", {
|
|
470
|
+
className: "flex h-4 flex-1 overflow-hidden rounded bg-raised",
|
|
471
|
+
role: "cell",
|
|
472
|
+
children: row.segments.map((segment) => segment.value === 0 ? null : /* @__PURE__ */ jsx("div", {
|
|
473
|
+
className: "h-full",
|
|
474
|
+
style: {
|
|
475
|
+
width: `${segment.value / widest * 100}%`,
|
|
476
|
+
backgroundColor: segment.colour
|
|
477
|
+
},
|
|
478
|
+
title: `${segment.name}: ${segment.value}`
|
|
479
|
+
}, segment.name))
|
|
480
|
+
}),
|
|
481
|
+
/* @__PURE__ */ jsx("span", {
|
|
482
|
+
className: "w-24 shrink-0 text-right font-mono text-fg-muted",
|
|
483
|
+
role: "cell",
|
|
484
|
+
children: row.segments.filter((segment) => segment.value > 0).map((segment) => `${segment.value} ${segment.name}`).join(" · ") || `${total}`
|
|
485
|
+
})
|
|
486
|
+
]
|
|
487
|
+
}, row.label);
|
|
488
|
+
})
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
//#endregion
|
|
492
|
+
//#region src/Histogram.tsx
|
|
493
|
+
/**
|
|
494
|
+
* Score distribution by class, with a threshold drawn on it.
|
|
495
|
+
*
|
|
496
|
+
* This is the chart that makes a threshold decision legible: a confusion matrix says how
|
|
497
|
+
* many were wrong, and this says *why* — two distributions that barely separate, or one
|
|
498
|
+
* long normal tail crossing the line.
|
|
499
|
+
*
|
|
500
|
+
* Both classes share one set of bin edges, computed over the union of their scores. Binned
|
|
501
|
+
* separately they would be drawn on different axes and the overlap — the only thing worth
|
|
502
|
+
* looking at — would be an artefact of the binning.
|
|
503
|
+
*/
|
|
504
|
+
const plotArea$1 = areaFor("wide");
|
|
505
|
+
const NORMAL_FILL = NORMAL_COLOUR;
|
|
506
|
+
const DEFECT_FILL = DEFECT_COLOUR;
|
|
507
|
+
/**
|
|
508
|
+
* Overlaid histograms of the normal and defect score distributions, with the threshold on
|
|
509
|
+
* them.
|
|
510
|
+
*
|
|
511
|
+
* @remarks
|
|
512
|
+
* Always the `wide` variant. Painted in the verdict tokens (`--normal`, `--defect`); the
|
|
513
|
+
* legend names both classes with their counts, so the chart does not rely on colour alone.
|
|
514
|
+
*/
|
|
515
|
+
function ScoreHistogram({ normal, defect, threshold, bins = 32, label, className }) {
|
|
516
|
+
const domain = extent([...normal, ...defect]);
|
|
517
|
+
const normalBins = histogram(normal, domain, bins);
|
|
518
|
+
const defectBins = histogram(defect, domain, bins);
|
|
519
|
+
const tallest = Math.max(1, ...normalBins, ...defectBins);
|
|
520
|
+
const xScale = linearScale(domain, plotArea$1.x0, plotArea$1.x1);
|
|
521
|
+
const yScale = linearScale([0, tallest], plotArea$1.y0, plotArea$1.y1);
|
|
522
|
+
const binWidth = (plotArea$1.x1 - plotArea$1.x0) / bins;
|
|
523
|
+
const bars = (counts, fill, opacity) => counts.map((count, index) => ({
|
|
524
|
+
count,
|
|
525
|
+
x: plotArea$1.x0 + index * binWidth
|
|
526
|
+
})).map(({ count, x }) => count === 0 ? null : /* @__PURE__ */ jsx("rect", {
|
|
527
|
+
x,
|
|
528
|
+
y: yScale.project(count),
|
|
529
|
+
width: Math.max(.5, binWidth - .5),
|
|
530
|
+
height: plotArea$1.y0 - yScale.project(count),
|
|
531
|
+
fill,
|
|
532
|
+
opacity
|
|
533
|
+
}, `${fill}-${x}`));
|
|
534
|
+
return /* @__PURE__ */ jsxs(Frame, {
|
|
535
|
+
xScale,
|
|
536
|
+
yScale,
|
|
537
|
+
xLabel: "anomaly score",
|
|
538
|
+
yLabel: "samples",
|
|
539
|
+
label,
|
|
540
|
+
variant: "wide",
|
|
541
|
+
className,
|
|
542
|
+
footer: /* @__PURE__ */ jsxs("ul", {
|
|
543
|
+
className: "flex flex-wrap items-center gap-x-4 gap-y-1",
|
|
544
|
+
children: [
|
|
545
|
+
/* @__PURE__ */ jsxs("li", {
|
|
546
|
+
className: "flex items-center gap-1.5",
|
|
547
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
548
|
+
"aria-hidden": true,
|
|
549
|
+
className: "inline-block h-2 w-3 rounded-sm",
|
|
550
|
+
style: {
|
|
551
|
+
backgroundColor: NORMAL_FILL,
|
|
552
|
+
opacity: .75
|
|
553
|
+
}
|
|
554
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
555
|
+
className: "font-mono text-xs",
|
|
556
|
+
children: [
|
|
557
|
+
"normal (",
|
|
558
|
+
normal.length,
|
|
559
|
+
")"
|
|
560
|
+
]
|
|
561
|
+
})]
|
|
562
|
+
}),
|
|
563
|
+
/* @__PURE__ */ jsxs("li", {
|
|
564
|
+
className: "flex items-center gap-1.5",
|
|
565
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
566
|
+
"aria-hidden": true,
|
|
567
|
+
className: "inline-block h-2 w-3 rounded-sm",
|
|
568
|
+
style: {
|
|
569
|
+
backgroundColor: DEFECT_FILL,
|
|
570
|
+
opacity: .75
|
|
571
|
+
}
|
|
572
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
573
|
+
className: "font-mono text-xs",
|
|
574
|
+
children: [
|
|
575
|
+
"defect (",
|
|
576
|
+
defect.length,
|
|
577
|
+
")"
|
|
578
|
+
]
|
|
579
|
+
})]
|
|
580
|
+
}),
|
|
581
|
+
threshold !== void 0 && /* @__PURE__ */ jsxs("li", {
|
|
582
|
+
className: "font-mono text-xs",
|
|
583
|
+
children: ["threshold ", threshold.toFixed(4)]
|
|
584
|
+
})
|
|
585
|
+
]
|
|
586
|
+
}),
|
|
587
|
+
children: [
|
|
588
|
+
bars(normalBins, NORMAL_FILL, .65),
|
|
589
|
+
bars(defectBins, DEFECT_FILL, .65),
|
|
590
|
+
threshold !== void 0 && Number.isFinite(threshold) && /* @__PURE__ */ jsx("line", {
|
|
591
|
+
x1: xScale.project(threshold),
|
|
592
|
+
x2: xScale.project(threshold),
|
|
593
|
+
y1: plotArea$1.y0,
|
|
594
|
+
y2: plotArea$1.y1,
|
|
595
|
+
stroke: "currentColor",
|
|
596
|
+
strokeWidth: 1.25,
|
|
597
|
+
strokeDasharray: "3 2"
|
|
598
|
+
})
|
|
599
|
+
]
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
//#endregion
|
|
603
|
+
//#region src/LineProfile.tsx
|
|
604
|
+
/**
|
|
605
|
+
* One or more series against arc length along a scan line or caliper axis, with detected
|
|
606
|
+
* edges as labelled vertical rules.
|
|
607
|
+
*
|
|
608
|
+
* @remarks
|
|
609
|
+
* An SVG `role="img"` named by `label`. The legend appears only for two or more series — a
|
|
610
|
+
* single profile is named by the chart's label.
|
|
611
|
+
*/
|
|
612
|
+
function LineProfile({ series, edges = [], label, xLabel = "arc length (px)", yLabel = "value", xDomain, yDomain, footer, showLegend = true, variant = "wide", className }) {
|
|
613
|
+
const allX = [...series.flatMap((entry) => entry.points.map((point) => point.x)), ...edges.map((edge) => edge.position)];
|
|
614
|
+
const allY = series.flatMap((entry) => entry.points.map((point) => point.y));
|
|
615
|
+
const plotArea = areaFor(variant);
|
|
616
|
+
const xScale = linearScale(xDomain ?? extent(allX), plotArea.x0, plotArea.x1);
|
|
617
|
+
const yScale = linearScale(yDomain ?? extent(allY), plotArea.y0, plotArea.y1);
|
|
618
|
+
const coloured = series.map((entry, index) => ({
|
|
619
|
+
...entry,
|
|
620
|
+
colour: entry.colour ?? seriesColour(index)
|
|
621
|
+
}));
|
|
622
|
+
return /* @__PURE__ */ jsxs(Frame, {
|
|
623
|
+
xScale,
|
|
624
|
+
yScale,
|
|
625
|
+
xLabel,
|
|
626
|
+
yLabel,
|
|
627
|
+
label,
|
|
628
|
+
variant,
|
|
629
|
+
className,
|
|
630
|
+
footer: /* @__PURE__ */ jsxs("div", {
|
|
631
|
+
className: "flex flex-col gap-1",
|
|
632
|
+
children: [showLegend && series.length > 1 && /* @__PURE__ */ jsx(Legend, { items: coloured.map((entry) => ({
|
|
633
|
+
label: entry.name,
|
|
634
|
+
colour: entry.colour
|
|
635
|
+
})) }), footer]
|
|
636
|
+
}),
|
|
637
|
+
children: [coloured.map((entry) => entry.points.length === 1 ? /* @__PURE__ */ jsx("circle", {
|
|
638
|
+
cx: xScale.project(entry.points[0]?.x ?? 0),
|
|
639
|
+
cy: yScale.project(entry.points[0]?.y ?? 0),
|
|
640
|
+
r: 2.5,
|
|
641
|
+
fill: entry.colour
|
|
642
|
+
}, entry.name) : /* @__PURE__ */ jsx("path", {
|
|
643
|
+
d: linePath(entry.points, xScale, yScale),
|
|
644
|
+
fill: "none",
|
|
645
|
+
stroke: entry.colour,
|
|
646
|
+
strokeWidth: 1.5,
|
|
647
|
+
strokeLinejoin: "round",
|
|
648
|
+
vectorEffect: "non-scaling-stroke"
|
|
649
|
+
}, entry.name)), edges.map((edge) => {
|
|
650
|
+
const x = xScale.project(edge.position);
|
|
651
|
+
const colour = toneColor(edge.tone, "signal");
|
|
652
|
+
return /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("line", {
|
|
653
|
+
x1: x,
|
|
654
|
+
x2: x,
|
|
655
|
+
y1: plotArea.y0,
|
|
656
|
+
y2: plotArea.y1,
|
|
657
|
+
stroke: colour,
|
|
658
|
+
strokeWidth: 1.25,
|
|
659
|
+
strokeDasharray: "3 2"
|
|
660
|
+
}), edge.label && /* @__PURE__ */ jsx("text", {
|
|
661
|
+
x,
|
|
662
|
+
y: plotArea.y1 - 3,
|
|
663
|
+
textAnchor: "middle",
|
|
664
|
+
fontSize: 9,
|
|
665
|
+
fill: colour,
|
|
666
|
+
children: edge.label
|
|
667
|
+
})] }, `${edge.position}-${edge.label ?? ""}-${edge.tone ?? ""}`);
|
|
668
|
+
})]
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
//#endregion
|
|
672
|
+
export { DEFECT_COLOUR, Frame, Legend, LineChart, LineProfile, NORMAL_COLOUR, SERIES_COLOURS, ScoreHistogram, StackedBars, areaFor, extent, formatTick, histogram, linePath, linearScale, logScale, niceStep, padDomain, plotArea, seriesColour };
|
|
673
|
+
|
|
674
|
+
//# sourceMappingURL=index.js.map
|