@vitavision/charts 0.8.3 → 0.8.4
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 +24 -5
- package/dist/index.d.ts +372 -77
- package/dist/index.js +1294 -406
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/ColormapLegend.tsx +250 -11
- package/src/Frame.tsx +15 -2
- package/src/LineChart.tsx +45 -4
- package/src/ScatterChart.tsx +416 -0
- package/src/band.ts +105 -0
- package/src/colormap.ts +86 -6
- package/src/index.ts +24 -2
- package/src/interaction.tsx +16 -28
- package/src/interpolate.ts +27 -0
- package/src/scatter.ts +77 -0
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useState } from "react";
|
|
1
|
+
import { useCallback, useId, useMemo, useState } from "react";
|
|
2
2
|
import { cn, toneColor } from "@vitavision/ui";
|
|
3
3
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
//#region src/Frame.tsx
|
|
@@ -93,7 +93,7 @@ const AXIS = "currentColor";
|
|
|
93
93
|
* Renders a `<figure>` holding an SVG with `role="img"` named by `label`, and the `footer`
|
|
94
94
|
* as its `<figcaption>`. `children` are drawn in viewBox units over the grid.
|
|
95
95
|
*/
|
|
96
|
-
function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, children, label, footer, variant = "panel", className, size, figureRef }) {
|
|
96
|
+
function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, xFormat, children, label, footer, variant = "panel", className, size, figureRef }) {
|
|
97
97
|
const horizontal = xScale.ticks(xTicks);
|
|
98
98
|
const vertical = yScale.ticks(yTicks);
|
|
99
99
|
const plotArea = areaFor(variant, size);
|
|
@@ -145,7 +145,7 @@ function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, childre
|
|
|
145
145
|
textAnchor: "middle",
|
|
146
146
|
fontSize: 10,
|
|
147
147
|
fill: AXIS,
|
|
148
|
-
children: tick.label
|
|
148
|
+
children: xFormat ? xFormat(tick.value) : tick.label
|
|
149
149
|
})] }, `x-${tick.value}`);
|
|
150
150
|
}),
|
|
151
151
|
/* @__PURE__ */ jsx("line", {
|
|
@@ -202,14 +202,32 @@ function Legend({ items, className }) {
|
|
|
202
202
|
className: cn("flex flex-wrap items-center gap-x-4 gap-y-1", className),
|
|
203
203
|
children: items.map((item) => /* @__PURE__ */ jsxs("li", {
|
|
204
204
|
className: "flex items-center gap-1.5",
|
|
205
|
-
children: [
|
|
206
|
-
"
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
205
|
+
children: [
|
|
206
|
+
item.band === void 0 ? /* @__PURE__ */ jsx("span", {
|
|
207
|
+
"aria-hidden": true,
|
|
208
|
+
className: "inline-block h-0.5 w-4 rounded-full",
|
|
209
|
+
style: { backgroundColor: item.colour }
|
|
210
|
+
}) : /* @__PURE__ */ jsxs("span", {
|
|
211
|
+
"aria-hidden": true,
|
|
212
|
+
"data-band-swatch": "",
|
|
213
|
+
className: "relative inline-block h-2.5 w-4",
|
|
214
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
215
|
+
className: "absolute inset-0 rounded-sm opacity-20",
|
|
216
|
+
style: { backgroundColor: item.colour }
|
|
217
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
218
|
+
className: "absolute inset-x-0 top-1/2 h-0.5 -translate-y-1/2 rounded-full",
|
|
219
|
+
style: { backgroundColor: item.colour }
|
|
220
|
+
})]
|
|
221
|
+
}),
|
|
222
|
+
/* @__PURE__ */ jsx("span", {
|
|
223
|
+
className: "font-mono text-xs",
|
|
224
|
+
children: item.label
|
|
225
|
+
}),
|
|
226
|
+
item.band !== void 0 && /* @__PURE__ */ jsx("span", {
|
|
227
|
+
className: "font-mono text-xs text-fg-subtle",
|
|
228
|
+
children: item.band
|
|
229
|
+
})
|
|
230
|
+
]
|
|
213
231
|
}, item.label))
|
|
214
232
|
});
|
|
215
233
|
}
|
|
@@ -433,6 +451,114 @@ function histogram(values, domain, count) {
|
|
|
433
451
|
return bins;
|
|
434
452
|
}
|
|
435
453
|
//#endregion
|
|
454
|
+
//#region src/interpolate.ts
|
|
455
|
+
/** Reading a series between its points. */
|
|
456
|
+
/**
|
|
457
|
+
* A series' value at `x`, linearly interpolated between the points either side, or `null`
|
|
458
|
+
* outside its range or across a gap.
|
|
459
|
+
*
|
|
460
|
+
* @param points - The series, in ascending `x`.
|
|
461
|
+
* @param x - Where.
|
|
462
|
+
* @returns The interpolated value, or `null`.
|
|
463
|
+
*/
|
|
464
|
+
function seriesValueAt(points, x) {
|
|
465
|
+
if (points.length === 0) return null;
|
|
466
|
+
if (points.length === 1) return points[0].x === x ? points[0].y : null;
|
|
467
|
+
let lo = 0;
|
|
468
|
+
let hi = points.length - 1;
|
|
469
|
+
if (x < points[lo].x || x > points[hi].x) return null;
|
|
470
|
+
while (hi - lo > 1) {
|
|
471
|
+
const mid = lo + hi >> 1;
|
|
472
|
+
if (points[mid].x <= x) lo = mid;
|
|
473
|
+
else hi = mid;
|
|
474
|
+
}
|
|
475
|
+
const a = points[lo];
|
|
476
|
+
const b = points[hi];
|
|
477
|
+
if (!Number.isFinite(a.y) || !Number.isFinite(b.y)) return null;
|
|
478
|
+
if (b.x === a.x) return a.y;
|
|
479
|
+
return a.y + (x - a.x) / (b.x - a.x) * (b.y - a.y);
|
|
480
|
+
}
|
|
481
|
+
//#endregion
|
|
482
|
+
//#region src/band.ts
|
|
483
|
+
/**
|
|
484
|
+
* The arithmetic of a series' error band: the ribbon between a lower and an upper value at every
|
|
485
|
+
* point of a series, its outline, and its value at an x between two points. Pure, so the geometry
|
|
486
|
+
* is testable apart from the SVG.
|
|
487
|
+
*/
|
|
488
|
+
/** The band's edges at point `index`, ordered low to high, or `null` where either is missing or non-finite. */
|
|
489
|
+
function bandBoundsAt(band, index) {
|
|
490
|
+
const a = band.lower[index];
|
|
491
|
+
const b = band.upper[index];
|
|
492
|
+
if (a === void 0 || b === void 0 || !Number.isFinite(a) || !Number.isFinite(b)) return null;
|
|
493
|
+
return a <= b ? [a, b] : [b, a];
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* The outline of a band as an SVG path `d`: the upper edge left to right and the lower edge back,
|
|
497
|
+
* closed, once for every run of points whose x and both edges are finite. A run of one point has
|
|
498
|
+
* no area and draws nothing; a gap ends the run, so the band is not bridged across missing data.
|
|
499
|
+
*
|
|
500
|
+
* @param points - The series' points, in draw order.
|
|
501
|
+
* @param band - The edges, by point index. Missing entries count as gaps.
|
|
502
|
+
* @param xScale - The horizontal scale.
|
|
503
|
+
* @param yScale - The vertical scale.
|
|
504
|
+
* @returns The path, or `""` when there is nothing to fill.
|
|
505
|
+
*/
|
|
506
|
+
function bandPath(points, band, xScale, yScale) {
|
|
507
|
+
const runs = [];
|
|
508
|
+
let run = [];
|
|
509
|
+
const close = () => {
|
|
510
|
+
if (run.length > 1) runs.push(run);
|
|
511
|
+
run = [];
|
|
512
|
+
};
|
|
513
|
+
points.forEach((point, index) => {
|
|
514
|
+
const bounds = bandBoundsAt(band, index);
|
|
515
|
+
if (!Number.isFinite(point.x) || bounds === null) {
|
|
516
|
+
close();
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
run.push({
|
|
520
|
+
x: xScale.project(point.x),
|
|
521
|
+
lo: yScale.project(bounds[0]),
|
|
522
|
+
hi: yScale.project(bounds[1])
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
close();
|
|
526
|
+
const f = (n) => n.toFixed(2);
|
|
527
|
+
return runs.map((r) => {
|
|
528
|
+
const top = r.map((p, i) => `${i === 0 ? "M" : "L"}${f(p.x)} ${f(p.hi)}`);
|
|
529
|
+
const bottom = r.map((p) => `L${f(p.x)} ${f(p.lo)}`).reverse();
|
|
530
|
+
return `${[...top, ...bottom].join(" ")} Z`;
|
|
531
|
+
}).join(" ");
|
|
532
|
+
}
|
|
533
|
+
/** Every finite edge of a band, for the chart's y extent. */
|
|
534
|
+
function bandValues(band) {
|
|
535
|
+
return [...band.lower, ...band.upper].filter(Number.isFinite);
|
|
536
|
+
}
|
|
537
|
+
/** A series' band at `x`, interpolated like {@link seriesValueAt}: `[lower, upper]`, or `null` outside the series or across a gap. */
|
|
538
|
+
function bandValueAt(points, band, x) {
|
|
539
|
+
const edge = (pick) => seriesValueAt(points.map((point, index) => ({
|
|
540
|
+
x: point.x,
|
|
541
|
+
y: bandBoundsAt(band, index)?.[pick] ?? NaN
|
|
542
|
+
})), x);
|
|
543
|
+
const lo = edge(0);
|
|
544
|
+
const hi = edge(1);
|
|
545
|
+
return lo === null || hi === null ? null : [lo, hi];
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* A value with its band as text: `3.2 ± 0.4` when the band is symmetric about the value, else
|
|
549
|
+
* `3.2 [2.8, 3.9]`.
|
|
550
|
+
*
|
|
551
|
+
* @param value - The series' value.
|
|
552
|
+
* @param bounds - The band's `[lower, upper]` there.
|
|
553
|
+
* @param format - Formats one number.
|
|
554
|
+
*/
|
|
555
|
+
function formatWithBand(value, bounds, format) {
|
|
556
|
+
const [lo, hi] = bounds;
|
|
557
|
+
const up = hi - value;
|
|
558
|
+
const down = value - lo;
|
|
559
|
+
return Math.abs(up - down) <= 1e-6 * Math.max(Math.abs(up), Math.abs(down), Number.MIN_VALUE) ? `${format(value)} ± ${format(up)}` : `${format(value)} [${format(lo)}, ${format(hi)}]`;
|
|
560
|
+
}
|
|
561
|
+
//#endregion
|
|
436
562
|
//#region src/interaction.tsx
|
|
437
563
|
/**
|
|
438
564
|
* What `LineChart` and `LineProfile` share beyond the frame:
|
|
@@ -489,7 +615,7 @@ function Markers({ markers, xScale, area }) {
|
|
|
489
615
|
* The pointer layer over the plot area: a crosshair and a value readout while hovering, the
|
|
490
616
|
* external `cursor` otherwise, and clicks as picks.
|
|
491
617
|
*/
|
|
492
|
-
function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick, format = (value) => Math.abs(value) >= 100 ? value.toFixed(0) : value.toPrecision(3) }) {
|
|
618
|
+
function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick, format = (value) => Math.abs(value) >= 100 ? value.toFixed(0) : value.toPrecision(3), xFormat }) {
|
|
493
619
|
const [hover, setHover] = useState(null);
|
|
494
620
|
const interactive = onHover !== void 0 || onPick !== void 0;
|
|
495
621
|
const xAt = (event) => {
|
|
@@ -509,10 +635,13 @@ function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick
|
|
|
509
635
|
const x = shown === null ? null : xScale.project(shown);
|
|
510
636
|
const readout = hover === null ? [] : series.flatMap((entry) => {
|
|
511
637
|
const y = seriesValueAt(entry.points, hover);
|
|
512
|
-
|
|
638
|
+
if (y === null) return [];
|
|
639
|
+
const bounds = entry.band ? bandValueAt(entry.points, entry.band, hover) : null;
|
|
640
|
+
return [{
|
|
513
641
|
name: entry.name,
|
|
514
642
|
colour: entry.colour,
|
|
515
|
-
y
|
|
643
|
+
y,
|
|
644
|
+
bounds
|
|
516
645
|
}];
|
|
517
646
|
});
|
|
518
647
|
return /* @__PURE__ */ jsxs("g", {
|
|
@@ -543,7 +672,7 @@ function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick
|
|
|
543
672
|
textAnchor: x > (area.x0 + area.x1) / 2 ? "end" : "start",
|
|
544
673
|
fontSize: 10,
|
|
545
674
|
fill: "currentColor",
|
|
546
|
-
children: [format(hover), ...readout.map((entry) => `${entry.name} ${format(entry.y)}`)].join(" · ")
|
|
675
|
+
children: [(xFormat ?? format)(hover), ...readout.map((entry) => `${entry.name} ${entry.bounds ? formatWithBand(entry.y, entry.bounds, format) : format(entry.y)}`)].join(" · ")
|
|
547
676
|
}),
|
|
548
677
|
interactive && /* @__PURE__ */ jsx("rect", {
|
|
549
678
|
"data-plot-target": "",
|
|
@@ -567,31 +696,6 @@ function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick
|
|
|
567
696
|
]
|
|
568
697
|
});
|
|
569
698
|
}
|
|
570
|
-
/**
|
|
571
|
-
* A series' value at `x`, linearly interpolated between the points either side, or `null`
|
|
572
|
-
* outside its range or across a gap.
|
|
573
|
-
*
|
|
574
|
-
* @param points - The series, in ascending `x`.
|
|
575
|
-
* @param x - Where.
|
|
576
|
-
* @returns The interpolated value, or `null`.
|
|
577
|
-
*/
|
|
578
|
-
function seriesValueAt(points, x) {
|
|
579
|
-
if (points.length === 0) return null;
|
|
580
|
-
if (points.length === 1) return points[0].x === x ? points[0].y : null;
|
|
581
|
-
let lo = 0;
|
|
582
|
-
let hi = points.length - 1;
|
|
583
|
-
if (x < points[lo].x || x > points[hi].x) return null;
|
|
584
|
-
while (hi - lo > 1) {
|
|
585
|
-
const mid = lo + hi >> 1;
|
|
586
|
-
if (points[mid].x <= x) lo = mid;
|
|
587
|
-
else hi = mid;
|
|
588
|
-
}
|
|
589
|
-
const a = points[lo];
|
|
590
|
-
const b = points[hi];
|
|
591
|
-
if (!Number.isFinite(a.y) || !Number.isFinite(b.y)) return null;
|
|
592
|
-
if (b.x === a.x) return a.y;
|
|
593
|
-
return a.y + (x - a.x) / (b.x - a.x) * (b.y - a.y);
|
|
594
|
-
}
|
|
595
699
|
function clamp(value, low, high) {
|
|
596
700
|
return Math.min(high, Math.max(low, value));
|
|
597
701
|
}
|
|
@@ -604,11 +708,12 @@ function clamp(value, low, high) {
|
|
|
604
708
|
* An SVG `role="img"` named by `label`, with the legend as real text under it. Series are
|
|
605
709
|
* told apart by the legend's names, never by colour alone. `bands` and `markers` mark x
|
|
606
710
|
* ranges and positions; `onHover` adds a crosshair and a value readout, `onPick` makes a
|
|
607
|
-
* click report its x, and `cursor` draws an x chosen elsewhere.
|
|
711
|
+
* click report its x, and `cursor` draws an x chosen elsewhere. A series' `band` adds an error
|
|
712
|
+
* band, and `xFormat` writes the x labels.
|
|
608
713
|
*/
|
|
609
|
-
function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDomain, underlay, footer, showLegend = true, variant = "panel", className, height = 200, bands, markers, cursor, onHover, onPick }) {
|
|
714
|
+
function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDomain, xFormat, underlay, footer, showLegend = true, variant = "panel", className, height = 200, bands, markers, cursor, onHover, onPick }) {
|
|
610
715
|
const allX = series.flatMap((entry) => entry.points.map((point) => point.x));
|
|
611
|
-
const allY = series.flatMap((entry) => entry.points.map((point) => point.y));
|
|
716
|
+
const allY = series.flatMap((entry) => [...entry.points.map((point) => point.y), ...entry.band ? bandValues(entry.band) : []]);
|
|
612
717
|
const [fluidRef, fluidSize] = useFluidSize(height);
|
|
613
718
|
const size = variant === "fluid" ? fluidSize : void 0;
|
|
614
719
|
const plotArea = areaFor(variant, size);
|
|
@@ -628,11 +733,13 @@ function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDoma
|
|
|
628
733
|
className,
|
|
629
734
|
size,
|
|
630
735
|
figureRef: variant === "fluid" ? fluidRef : void 0,
|
|
736
|
+
xFormat,
|
|
631
737
|
footer: /* @__PURE__ */ jsxs("div", {
|
|
632
738
|
className: "flex flex-col gap-1",
|
|
633
739
|
children: [showLegend && /* @__PURE__ */ jsx(Legend, { items: coloured.map((entry) => ({
|
|
634
740
|
label: entry.name,
|
|
635
|
-
colour: entry.colour
|
|
741
|
+
colour: entry.colour,
|
|
742
|
+
...entry.band ? { band: entry.band.label ?? "± band" } : {}
|
|
636
743
|
})) }), footer]
|
|
637
744
|
}),
|
|
638
745
|
children: [
|
|
@@ -641,6 +748,14 @@ function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDoma
|
|
|
641
748
|
xScale,
|
|
642
749
|
area: plotArea
|
|
643
750
|
}),
|
|
751
|
+
coloured.map((entry) => entry.band && /* @__PURE__ */ jsx("path", {
|
|
752
|
+
"data-series-band": entry.name,
|
|
753
|
+
"aria-hidden": true,
|
|
754
|
+
d: bandPath(entry.points, entry.band, xScale, yScale),
|
|
755
|
+
fill: entry.colour,
|
|
756
|
+
fillOpacity: .16,
|
|
757
|
+
stroke: "none"
|
|
758
|
+
}, `band-${entry.name}`)),
|
|
644
759
|
underlay?.(xScale, yScale),
|
|
645
760
|
coloured.map((entry) => entry.points.length === 1 ? /* @__PURE__ */ jsx("circle", {
|
|
646
761
|
cx: xScale.project(entry.points[0]?.x ?? 0),
|
|
@@ -667,185 +782,1145 @@ function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDoma
|
|
|
667
782
|
series: coloured,
|
|
668
783
|
cursor,
|
|
669
784
|
onHover,
|
|
670
|
-
onPick
|
|
785
|
+
onPick,
|
|
786
|
+
xFormat
|
|
671
787
|
})
|
|
672
788
|
]
|
|
673
789
|
});
|
|
674
790
|
}
|
|
675
791
|
//#endregion
|
|
676
|
-
//#region src/
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
792
|
+
//#region src/colormap.ts
|
|
793
|
+
const ANCHORS = {
|
|
794
|
+
viridis: [
|
|
795
|
+
[
|
|
796
|
+
68,
|
|
797
|
+
1,
|
|
798
|
+
84
|
|
799
|
+
],
|
|
800
|
+
[
|
|
801
|
+
71,
|
|
802
|
+
44,
|
|
803
|
+
122
|
|
804
|
+
],
|
|
805
|
+
[
|
|
806
|
+
59,
|
|
807
|
+
81,
|
|
808
|
+
139
|
|
809
|
+
],
|
|
810
|
+
[
|
|
811
|
+
44,
|
|
812
|
+
113,
|
|
813
|
+
142
|
|
814
|
+
],
|
|
815
|
+
[
|
|
816
|
+
33,
|
|
817
|
+
144,
|
|
818
|
+
141
|
|
819
|
+
],
|
|
820
|
+
[
|
|
821
|
+
39,
|
|
822
|
+
173,
|
|
823
|
+
129
|
|
824
|
+
],
|
|
825
|
+
[
|
|
826
|
+
92,
|
|
827
|
+
200,
|
|
828
|
+
99
|
|
829
|
+
],
|
|
830
|
+
[
|
|
831
|
+
170,
|
|
832
|
+
220,
|
|
833
|
+
50
|
|
834
|
+
],
|
|
835
|
+
[
|
|
836
|
+
253,
|
|
837
|
+
231,
|
|
838
|
+
37
|
|
839
|
+
]
|
|
840
|
+
],
|
|
841
|
+
magma: [
|
|
842
|
+
[
|
|
843
|
+
0,
|
|
844
|
+
0,
|
|
845
|
+
4
|
|
846
|
+
],
|
|
847
|
+
[
|
|
848
|
+
29,
|
|
849
|
+
17,
|
|
850
|
+
71
|
|
851
|
+
],
|
|
852
|
+
[
|
|
853
|
+
81,
|
|
854
|
+
18,
|
|
855
|
+
124
|
|
856
|
+
],
|
|
857
|
+
[
|
|
858
|
+
131,
|
|
859
|
+
38,
|
|
860
|
+
129
|
|
861
|
+
],
|
|
862
|
+
[
|
|
863
|
+
183,
|
|
864
|
+
55,
|
|
865
|
+
121
|
|
866
|
+
],
|
|
867
|
+
[
|
|
868
|
+
231,
|
|
869
|
+
82,
|
|
870
|
+
99
|
|
871
|
+
],
|
|
872
|
+
[
|
|
873
|
+
252,
|
|
874
|
+
137,
|
|
875
|
+
97
|
|
876
|
+
],
|
|
877
|
+
[
|
|
878
|
+
254,
|
|
879
|
+
196,
|
|
880
|
+
136
|
|
881
|
+
],
|
|
882
|
+
[
|
|
883
|
+
252,
|
|
884
|
+
253,
|
|
885
|
+
191
|
|
886
|
+
]
|
|
887
|
+
],
|
|
888
|
+
inferno: [
|
|
889
|
+
[
|
|
890
|
+
0,
|
|
891
|
+
0,
|
|
892
|
+
4
|
|
893
|
+
],
|
|
894
|
+
[
|
|
895
|
+
33,
|
|
896
|
+
12,
|
|
897
|
+
74
|
|
898
|
+
],
|
|
899
|
+
[
|
|
900
|
+
87,
|
|
901
|
+
16,
|
|
902
|
+
110
|
|
903
|
+
],
|
|
904
|
+
[
|
|
905
|
+
138,
|
|
906
|
+
34,
|
|
907
|
+
106
|
|
908
|
+
],
|
|
909
|
+
[
|
|
910
|
+
188,
|
|
911
|
+
55,
|
|
912
|
+
84
|
|
913
|
+
],
|
|
914
|
+
[
|
|
915
|
+
228,
|
|
916
|
+
90,
|
|
917
|
+
49
|
|
918
|
+
],
|
|
919
|
+
[
|
|
920
|
+
249,
|
|
921
|
+
142,
|
|
922
|
+
9
|
|
923
|
+
],
|
|
924
|
+
[
|
|
925
|
+
249,
|
|
926
|
+
203,
|
|
927
|
+
53
|
|
928
|
+
],
|
|
929
|
+
[
|
|
930
|
+
252,
|
|
931
|
+
255,
|
|
932
|
+
164
|
|
933
|
+
]
|
|
934
|
+
],
|
|
935
|
+
blueOrange: [
|
|
936
|
+
[
|
|
937
|
+
0,
|
|
938
|
+
70,
|
|
939
|
+
140
|
|
940
|
+
],
|
|
941
|
+
[
|
|
942
|
+
33,
|
|
943
|
+
90,
|
|
944
|
+
158
|
|
945
|
+
],
|
|
946
|
+
[
|
|
947
|
+
64,
|
|
948
|
+
112,
|
|
949
|
+
172
|
|
950
|
+
],
|
|
951
|
+
[
|
|
952
|
+
92,
|
|
953
|
+
133,
|
|
954
|
+
185
|
|
955
|
+
],
|
|
956
|
+
[
|
|
957
|
+
121,
|
|
958
|
+
155,
|
|
959
|
+
198
|
|
960
|
+
],
|
|
961
|
+
[
|
|
962
|
+
150,
|
|
963
|
+
176,
|
|
964
|
+
211
|
|
965
|
+
],
|
|
966
|
+
[
|
|
967
|
+
179,
|
|
968
|
+
198,
|
|
969
|
+
222
|
|
970
|
+
],
|
|
971
|
+
[
|
|
972
|
+
209,
|
|
973
|
+
220,
|
|
974
|
+
233
|
|
975
|
+
],
|
|
976
|
+
[
|
|
977
|
+
242,
|
|
978
|
+
242,
|
|
979
|
+
242
|
|
980
|
+
],
|
|
981
|
+
[
|
|
982
|
+
230,
|
|
983
|
+
216,
|
|
984
|
+
207
|
|
985
|
+
],
|
|
986
|
+
[
|
|
987
|
+
216,
|
|
988
|
+
190,
|
|
989
|
+
175
|
|
990
|
+
],
|
|
991
|
+
[
|
|
992
|
+
201,
|
|
993
|
+
165,
|
|
994
|
+
144
|
|
995
|
+
],
|
|
996
|
+
[
|
|
997
|
+
186,
|
|
998
|
+
140,
|
|
999
|
+
113
|
|
1000
|
+
],
|
|
1001
|
+
[
|
|
1002
|
+
170,
|
|
1003
|
+
116,
|
|
1004
|
+
83
|
|
1005
|
+
],
|
|
1006
|
+
[
|
|
1007
|
+
153,
|
|
1008
|
+
91,
|
|
1009
|
+
52
|
|
1010
|
+
],
|
|
1011
|
+
[
|
|
1012
|
+
136,
|
|
1013
|
+
67,
|
|
1014
|
+
13
|
|
1015
|
+
],
|
|
1016
|
+
[
|
|
1017
|
+
113,
|
|
1018
|
+
52,
|
|
1019
|
+
0
|
|
1020
|
+
]
|
|
1021
|
+
],
|
|
1022
|
+
cividis: [
|
|
1023
|
+
[
|
|
1024
|
+
0,
|
|
1025
|
+
34,
|
|
1026
|
+
78
|
|
1027
|
+
],
|
|
1028
|
+
[
|
|
1029
|
+
18,
|
|
1030
|
+
53,
|
|
1031
|
+
112
|
|
1032
|
+
],
|
|
1033
|
+
[
|
|
1034
|
+
59,
|
|
1035
|
+
73,
|
|
1036
|
+
108
|
|
1037
|
+
],
|
|
1038
|
+
[
|
|
1039
|
+
87,
|
|
1040
|
+
93,
|
|
1041
|
+
109
|
|
1042
|
+
],
|
|
1043
|
+
[
|
|
1044
|
+
112,
|
|
1045
|
+
113,
|
|
1046
|
+
115
|
|
1047
|
+
],
|
|
1048
|
+
[
|
|
1049
|
+
138,
|
|
1050
|
+
135,
|
|
1051
|
+
121
|
|
1052
|
+
],
|
|
1053
|
+
[
|
|
1054
|
+
165,
|
|
1055
|
+
157,
|
|
1056
|
+
116
|
|
1057
|
+
],
|
|
1058
|
+
[
|
|
1059
|
+
196,
|
|
1060
|
+
182,
|
|
1061
|
+
101
|
|
1062
|
+
],
|
|
1063
|
+
[
|
|
1064
|
+
254,
|
|
1065
|
+
232,
|
|
1066
|
+
56
|
|
1067
|
+
]
|
|
1068
|
+
]
|
|
1069
|
+
};
|
|
1070
|
+
/** The names of the shipped maps. */
|
|
1071
|
+
const COLORMAPS = [
|
|
1072
|
+
"viridis",
|
|
1073
|
+
"cividis",
|
|
1074
|
+
"magma",
|
|
1075
|
+
"inferno",
|
|
1076
|
+
"blueOrange"
|
|
1077
|
+
];
|
|
1078
|
+
/** The maps that run through a neutral middle, for a signed value: use them over a domain centred on zero. */
|
|
1079
|
+
const DIVERGING_COLORMAPS = ["blueOrange"];
|
|
1080
|
+
/**
|
|
1081
|
+
* Whether a map is diverging.
|
|
1082
|
+
*
|
|
1083
|
+
* @param name - The map.
|
|
1084
|
+
* @returns `true` for a map meant for a signed value around zero.
|
|
1085
|
+
*/
|
|
1086
|
+
function isDivergingColormap(name) {
|
|
1087
|
+
return DIVERGING_COLORMAPS.includes(name);
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* A domain centred on zero that holds a given one: `[-m, m]` with `m` the largest absolute end.
|
|
1091
|
+
* Feed it a diverging map so the middle colour means zero.
|
|
1092
|
+
*
|
|
1093
|
+
* @param domain - `[low, high]`, or the values to cover.
|
|
1094
|
+
* @returns `[-m, m]`; `[-1, 1]` when no end is finite and non-zero.
|
|
1095
|
+
*/
|
|
1096
|
+
function symmetricDomain(domain) {
|
|
1097
|
+
let m = 0;
|
|
1098
|
+
for (const v of domain) if (Number.isFinite(v) && Math.abs(v) > m) m = Math.abs(v);
|
|
1099
|
+
return m > 0 ? [-m, m] : [-1, 1];
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* The colour of `t` on a map.
|
|
1103
|
+
*
|
|
1104
|
+
* @param name - The map.
|
|
1105
|
+
* @param t - Position in `[0, 1]`; clamped. A non-finite `t` gives the map's low end.
|
|
1106
|
+
* @returns A CSS `rgb(r g b)` colour.
|
|
1107
|
+
*/
|
|
1108
|
+
function colormap(name, t) {
|
|
1109
|
+
const [r, g, b] = colormapRgb(name, t);
|
|
1110
|
+
return `rgb(${r} ${g} ${b})`;
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* The colour of `t` on a map, as integer channels.
|
|
1114
|
+
*
|
|
1115
|
+
* @param name - The map.
|
|
1116
|
+
* @param t - Position in `[0, 1]`; clamped.
|
|
1117
|
+
* @returns `[r, g, b]`, each 0–255.
|
|
1118
|
+
*/
|
|
1119
|
+
function colormapRgb(name, t) {
|
|
1120
|
+
const anchors = ANCHORS[name];
|
|
1121
|
+
const scaled = (Number.isFinite(t) ? Math.min(1, Math.max(0, t)) : 0) * (anchors.length - 1);
|
|
1122
|
+
const i = Math.min(anchors.length - 2, Math.floor(scaled));
|
|
1123
|
+
const f = scaled - i;
|
|
1124
|
+
const a = anchors[i];
|
|
1125
|
+
const b = anchors[i + 1];
|
|
1126
|
+
return [
|
|
1127
|
+
Math.round(a[0] + f * (b[0] - a[0])),
|
|
1128
|
+
Math.round(a[1] + f * (b[1] - a[1])),
|
|
1129
|
+
Math.round(a[2] + f * (b[2] - a[2]))
|
|
1130
|
+
];
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* A value's colour on a map, over a domain.
|
|
1134
|
+
*
|
|
1135
|
+
* @param name - The map.
|
|
1136
|
+
* @param value - The value.
|
|
1137
|
+
* @param domain - `[low, high]`: low maps to the map's start. A reversed domain reverses the map.
|
|
1138
|
+
* For a diverging map pass a domain centred on zero (`symmetricDomain`).
|
|
1139
|
+
* @returns A CSS colour.
|
|
1140
|
+
*/
|
|
1141
|
+
function colormapValue(name, value, domain) {
|
|
1142
|
+
const [low, high] = domain;
|
|
1143
|
+
const span = high - low;
|
|
1144
|
+
return colormap(name, span === 0 ? 0 : (value - low) / span);
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* A CSS `linear-gradient` of a map, for a legend bar.
|
|
1148
|
+
*
|
|
1149
|
+
* @param name - The map.
|
|
1150
|
+
* @param direction - The gradient's direction. Defaults to `to right`.
|
|
1151
|
+
* @returns A CSS `background-image` value.
|
|
1152
|
+
*/
|
|
1153
|
+
function colormapGradient(name, direction = "to right") {
|
|
1154
|
+
const anchors = ANCHORS[name];
|
|
1155
|
+
return `linear-gradient(${direction}, ${anchors.map(([r, g, b], i) => `rgb(${r} ${g} ${b}) ${(i / (anchors.length - 1) * 100).toFixed(1)}%`).join(", ")})`;
|
|
1156
|
+
}
|
|
1157
|
+
//#endregion
|
|
1158
|
+
//#region src/ColormapLegend.tsx
|
|
1159
|
+
/**
|
|
1160
|
+
* The scale of a colour map, shown beside the map: every chart or map shows its scale, with units.
|
|
1161
|
+
*/
|
|
1162
|
+
const defaultFormat = (value) => String(Number(value.toPrecision(3)));
|
|
1163
|
+
/** The tick values: a given list inside the domain, or round values, or the default for the layout. */
|
|
1164
|
+
function tickValues(ticks, domain, symmetric) {
|
|
1165
|
+
const [low, high] = domain;
|
|
1166
|
+
const inside = (v) => Number.isFinite(v) && v >= Math.min(low, high) - 1e-12 && v <= Math.max(low, high) + 1e-12;
|
|
1167
|
+
if (typeof ticks === "number") {
|
|
1168
|
+
const span = Math.abs(high - low);
|
|
1169
|
+
return [
|
|
1170
|
+
low,
|
|
1171
|
+
...linearScale([low, high], 0, 1).ticks(ticks).map((tick) => tick.value).filter((v) => inside(v) && Math.abs(v - low) > .08 * span && Math.abs(v - high) > .08 * span),
|
|
1172
|
+
high
|
|
1173
|
+
];
|
|
1174
|
+
}
|
|
1175
|
+
if (ticks !== void 0) return ticks.filter(inside);
|
|
1176
|
+
return symmetric ? [
|
|
1177
|
+
low,
|
|
1178
|
+
0,
|
|
1179
|
+
high
|
|
1180
|
+
] : [low, high];
|
|
1181
|
+
}
|
|
1182
|
+
/** `marker` as a list of `{ value, label }`, in domain, in the order given. */
|
|
1183
|
+
function markerList(marker, low, high) {
|
|
1184
|
+
if (marker === void 0) return [];
|
|
1185
|
+
const list = typeof marker === "number" || !Array.isArray(marker) ? [marker] : [...marker];
|
|
1186
|
+
const lo = Math.min(low, high) - 1e-12;
|
|
1187
|
+
const hi = Math.max(low, high) + 1e-12;
|
|
1188
|
+
return list.map((entry) => typeof entry === "number" ? { value: entry } : entry).filter((entry) => Number.isFinite(entry.value) && entry.value >= lo && entry.value <= hi);
|
|
1189
|
+
}
|
|
1190
|
+
/** The cap on a clipped end: an arrow in the map's end colour, pointing outward. */
|
|
1191
|
+
function EndCap({ map, end, orientation }) {
|
|
1192
|
+
const horizontal = orientation === "horizontal";
|
|
1193
|
+
const clip = horizontal ? end === "high" ? "polygon(0 0, 100% 50%, 0 100%)" : "polygon(100% 0, 0 50%, 100% 100%)" : end === "high" ? "polygon(50% 0, 100% 100%, 0 100%)" : "polygon(0 0, 100% 0, 50% 100%)";
|
|
1194
|
+
return /* @__PURE__ */ jsx("span", {
|
|
1195
|
+
"aria-hidden": true,
|
|
1196
|
+
"data-cap": end,
|
|
1197
|
+
className: `absolute ${horizontal ? end === "high" ? "left-full top-0.5 ml-px h-2 w-1.5" : "right-full top-0.5 mr-px h-2 w-1.5" : end === "high" ? "bottom-full left-0 mb-px h-1.5 w-2.5" : "top-full left-0 mt-px h-1.5 w-2.5"}`,
|
|
1198
|
+
style: {
|
|
1199
|
+
backgroundColor: colormap(map, end === "high" ? 1 : 0),
|
|
1200
|
+
clipPath: clip
|
|
1201
|
+
}
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* A gradient bar with its values and unit.
|
|
1206
|
+
*
|
|
1207
|
+
* - **Horizontal**: one line, "Edge error 0 [bar] 2.5 px". With `ticks` or `symmetric` the values
|
|
1208
|
+
* sit under the bar at their position instead.
|
|
1209
|
+
* - **Vertical**: the label above a column bar, the high end at the top, the values beside the
|
|
1210
|
+
* bar at their position, and the unit after the label.
|
|
1211
|
+
*
|
|
1212
|
+
* `marker` ticks values on the bar (a threshold, the value under a cursor) and `clipped` says
|
|
1213
|
+
* the scale saturates at an end, with a cap and `≥` / `≤` on its value.
|
|
1214
|
+
*
|
|
1215
|
+
* The bar is decorative; the label, the values and the markers' text carry the meaning.
|
|
1216
|
+
*/
|
|
1217
|
+
function ColormapLegend({ map, domain, unit, label, format = defaultFormat, orientation = "horizontal", ticks, symmetric = false, marker, clipped, length = 160, className }) {
|
|
1218
|
+
const [low, high] = symmetric ? symmetricDomain(domain) : domain;
|
|
1219
|
+
const span = high - low;
|
|
1220
|
+
const end = (value) => unit ? `${format(value)} ${unit}` : format(value);
|
|
1221
|
+
const fraction = (value) => span === 0 ? 0 : (value - low) / span;
|
|
1222
|
+
const markers = markerList(marker, low, high);
|
|
1223
|
+
const capLow = clipped === "low" || clipped === "both";
|
|
1224
|
+
const capHigh = clipped === "high" || clipped === "both";
|
|
1225
|
+
const text = (value, isLow, isHigh) => `${isHigh && capHigh ? "≥ " : isLow && capLow ? "≤ " : ""}${format(value)}`;
|
|
1226
|
+
const markerText = (entry) => entry.label ?? end(entry.value);
|
|
1227
|
+
if (orientation === "horizontal" && ticks === void 0 && !symmetric && markers.length === 0) return /* @__PURE__ */ jsxs("div", {
|
|
1228
|
+
className: cn("flex items-center gap-2 text-xs text-fg-muted", className),
|
|
1229
|
+
"data-colormap": map,
|
|
1230
|
+
children: [
|
|
1231
|
+
/* @__PURE__ */ jsx("span", { children: label }),
|
|
1232
|
+
/* @__PURE__ */ jsx("span", {
|
|
1233
|
+
className: "font-mono tabular-nums",
|
|
1234
|
+
children: capLow ? `≤ ${end(low)}` : end(low)
|
|
1235
|
+
}),
|
|
1236
|
+
/* @__PURE__ */ jsxs("span", {
|
|
1237
|
+
"aria-hidden": true,
|
|
1238
|
+
className: "relative h-2 w-24 rounded-full",
|
|
1239
|
+
style: { backgroundImage: colormapGradient(map) },
|
|
1240
|
+
children: [capLow && /* @__PURE__ */ jsx(EndCap, {
|
|
1241
|
+
map,
|
|
1242
|
+
end: "low",
|
|
1243
|
+
orientation: "horizontal"
|
|
1244
|
+
}), capHigh && /* @__PURE__ */ jsx(EndCap, {
|
|
1245
|
+
map,
|
|
1246
|
+
end: "high",
|
|
1247
|
+
orientation: "horizontal"
|
|
1248
|
+
})]
|
|
1249
|
+
}),
|
|
1250
|
+
/* @__PURE__ */ jsx("span", {
|
|
1251
|
+
className: "font-mono tabular-nums",
|
|
1252
|
+
children: capHigh ? `≥ ${end(high)}` : end(high)
|
|
1253
|
+
})
|
|
1254
|
+
]
|
|
1255
|
+
});
|
|
1256
|
+
const values = tickValues(ticks, [low, high], symmetric);
|
|
1257
|
+
if (capLow && !values.includes(low)) values.unshift(low);
|
|
1258
|
+
if (capHigh && !values.includes(high)) values.push(high);
|
|
1259
|
+
const tickText = (value) => text(value, value === low, value === high);
|
|
1260
|
+
if (orientation === "horizontal") return /* @__PURE__ */ jsxs("div", {
|
|
1261
|
+
className: cn("flex items-start gap-2 text-xs text-fg-muted", className),
|
|
1262
|
+
"data-colormap": map,
|
|
1263
|
+
children: [
|
|
1264
|
+
/* @__PURE__ */ jsx("span", {
|
|
1265
|
+
className: markers.length > 0 ? "pt-4" : void 0,
|
|
1266
|
+
children: label
|
|
1267
|
+
}),
|
|
1268
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1269
|
+
className: cn("relative w-32 pb-4", markers.length > 0 && "pt-4", (capLow || capHigh) && "mx-2"),
|
|
1270
|
+
children: [
|
|
1271
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1272
|
+
className: "relative mt-0.5 h-2",
|
|
1273
|
+
children: [
|
|
1274
|
+
/* @__PURE__ */ jsx("div", {
|
|
1275
|
+
"aria-hidden": true,
|
|
1276
|
+
className: "absolute inset-0 rounded-full",
|
|
1277
|
+
style: { backgroundImage: colormapGradient(map) }
|
|
1278
|
+
}),
|
|
1279
|
+
capLow && /* @__PURE__ */ jsx(EndCap, {
|
|
1280
|
+
map,
|
|
1281
|
+
end: "low",
|
|
1282
|
+
orientation: "horizontal"
|
|
1283
|
+
}),
|
|
1284
|
+
capHigh && /* @__PURE__ */ jsx(EndCap, {
|
|
1285
|
+
map,
|
|
1286
|
+
end: "high",
|
|
1287
|
+
orientation: "horizontal"
|
|
1288
|
+
}),
|
|
1289
|
+
markers.map((entry) => /* @__PURE__ */ jsx("span", {
|
|
1290
|
+
"aria-hidden": true,
|
|
1291
|
+
"data-marker-tick": "",
|
|
1292
|
+
className: "absolute -top-0.5 h-3 w-0.5 -translate-x-1/2 rounded-full bg-fg ring-1 ring-surface",
|
|
1293
|
+
style: { left: `${(fraction(entry.value) * 100).toFixed(2)}%` }
|
|
1294
|
+
}, `m-${entry.value}-${entry.label ?? ""}`))
|
|
1295
|
+
]
|
|
1296
|
+
}),
|
|
1297
|
+
markers.map((entry) => /* @__PURE__ */ jsx("span", {
|
|
1298
|
+
"data-marker": "",
|
|
1299
|
+
className: "absolute top-0 -translate-x-1/2 whitespace-nowrap font-mono text-[10px] leading-none tabular-nums text-fg",
|
|
1300
|
+
style: { left: `${(fraction(entry.value) * 100).toFixed(2)}%` },
|
|
1301
|
+
children: markerText(entry)
|
|
1302
|
+
}, `mt-${entry.value}-${entry.label ?? ""}`)),
|
|
1303
|
+
values.map((value) => /* @__PURE__ */ jsx("span", {
|
|
1304
|
+
"data-tick": "",
|
|
1305
|
+
className: cn("absolute -translate-x-1/2 font-mono text-[10px] tabular-nums", markers.length > 0 ? "top-7" : "top-3"),
|
|
1306
|
+
style: { left: `${(fraction(value) * 100).toFixed(2)}%` },
|
|
1307
|
+
children: tickText(value)
|
|
1308
|
+
}, value))
|
|
1309
|
+
]
|
|
1310
|
+
}),
|
|
1311
|
+
unit && /* @__PURE__ */ jsx("span", {
|
|
1312
|
+
className: cn("text-fg-subtle", markers.length > 0 && "pt-4"),
|
|
1313
|
+
children: unit
|
|
1314
|
+
})
|
|
1315
|
+
]
|
|
1316
|
+
});
|
|
1317
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1318
|
+
className: cn("inline-flex flex-col gap-1.5 text-xs text-fg-muted", className),
|
|
1319
|
+
"data-colormap": map,
|
|
1320
|
+
"data-orientation": "vertical",
|
|
1321
|
+
children: [/* @__PURE__ */ jsxs("span", { children: [label, unit && /* @__PURE__ */ jsx("span", {
|
|
1322
|
+
className: "ml-1 text-fg-subtle",
|
|
1323
|
+
children: unit
|
|
1324
|
+
})] }), /* @__PURE__ */ jsxs("div", {
|
|
1325
|
+
className: "relative my-1.5 flex",
|
|
1326
|
+
style: { height: length },
|
|
1327
|
+
children: [
|
|
1328
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1329
|
+
className: "relative w-2.5",
|
|
1330
|
+
children: [
|
|
1331
|
+
/* @__PURE__ */ jsx("div", {
|
|
1332
|
+
"aria-hidden": true,
|
|
1333
|
+
className: "absolute inset-0 rounded-full",
|
|
1334
|
+
style: { backgroundImage: colormapGradient(map, "to top") }
|
|
1335
|
+
}),
|
|
1336
|
+
capLow && /* @__PURE__ */ jsx(EndCap, {
|
|
1337
|
+
map,
|
|
1338
|
+
end: "low",
|
|
1339
|
+
orientation: "vertical"
|
|
1340
|
+
}),
|
|
1341
|
+
capHigh && /* @__PURE__ */ jsx(EndCap, {
|
|
1342
|
+
map,
|
|
1343
|
+
end: "high",
|
|
1344
|
+
orientation: "vertical"
|
|
1345
|
+
}),
|
|
1346
|
+
markers.map((entry) => /* @__PURE__ */ jsx("span", {
|
|
1347
|
+
"aria-hidden": true,
|
|
1348
|
+
"data-marker-tick": "",
|
|
1349
|
+
className: "absolute -left-0.5 -right-0.5 h-0.5 translate-y-1/2 rounded-full bg-fg ring-1 ring-surface",
|
|
1350
|
+
style: { bottom: `${(fraction(entry.value) * 100).toFixed(2)}%` }
|
|
1351
|
+
}, `m-${entry.value}-${entry.label ?? ""}`))
|
|
1352
|
+
]
|
|
1353
|
+
}),
|
|
1354
|
+
/* @__PURE__ */ jsx("div", {
|
|
1355
|
+
className: "relative ml-1.5 min-w-8",
|
|
1356
|
+
children: values.map((value) => /* @__PURE__ */ jsx("span", {
|
|
1357
|
+
"data-tick": "",
|
|
1358
|
+
className: "absolute left-0 translate-y-1/2 whitespace-nowrap font-mono text-[10px] leading-none tabular-nums",
|
|
1359
|
+
style: { bottom: `${(fraction(value) * 100).toFixed(2)}%` },
|
|
1360
|
+
children: tickText(value)
|
|
1361
|
+
}, value))
|
|
1362
|
+
}),
|
|
1363
|
+
markers.length > 0 && /* @__PURE__ */ jsx("div", {
|
|
1364
|
+
className: "relative ml-1.5 min-w-8",
|
|
1365
|
+
children: markers.map((entry) => /* @__PURE__ */ jsx("span", {
|
|
1366
|
+
"data-marker": "",
|
|
1367
|
+
className: "absolute left-0 translate-y-1/2 whitespace-nowrap font-mono text-[10px] leading-none tabular-nums text-fg",
|
|
1368
|
+
style: { bottom: `${(fraction(entry.value) * 100).toFixed(2)}%` },
|
|
1369
|
+
children: markerText(entry)
|
|
1370
|
+
}, `mt-${entry.value}-${entry.label ?? ""}`))
|
|
1371
|
+
})
|
|
1372
|
+
]
|
|
1373
|
+
})]
|
|
1374
|
+
});
|
|
1375
|
+
}
|
|
1376
|
+
//#endregion
|
|
1377
|
+
//#region src/scatter.ts
|
|
1378
|
+
/**
|
|
1379
|
+
* The arithmetic behind `ScatterChart`: which point is under the pointer, which colour step a
|
|
1380
|
+
* value falls in, and the domain a colour scale covers.
|
|
1381
|
+
*
|
|
1382
|
+
* Pure: the component is a thin list of SVG elements over it.
|
|
1383
|
+
*/
|
|
1384
|
+
/** The number of colour steps a value-coloured series is quantised into: one path each. */
|
|
1385
|
+
const SCATTER_COLOUR_STEPS = 24;
|
|
1386
|
+
/**
|
|
1387
|
+
* The projected point nearest to a position, within a radius.
|
|
1388
|
+
*
|
|
1389
|
+
* @param xy - Flat `[x0, y0, x1, y1, …]` in the plot's own units (viewBox units); a non-finite
|
|
1390
|
+
* coordinate is skipped.
|
|
1391
|
+
* @param x - The query's x.
|
|
1392
|
+
* @param y - The query's y.
|
|
1393
|
+
* @param radius - How far from a point the query still picks it.
|
|
1394
|
+
* @returns The index of the nearest point (the lower index on a tie), or `-1`.
|
|
1395
|
+
*/
|
|
1396
|
+
function nearestProjected(xy, x, y, radius) {
|
|
1397
|
+
const limit = radius * radius;
|
|
1398
|
+
let best = -1;
|
|
1399
|
+
let bestD = Infinity;
|
|
1400
|
+
const count = Math.floor(xy.length / 2);
|
|
1401
|
+
for (let i = 0; i < count; i++) {
|
|
1402
|
+
const dx = xy[2 * i] - x;
|
|
1403
|
+
const dy = xy[2 * i + 1] - y;
|
|
1404
|
+
const d = dx * dx + dy * dy;
|
|
1405
|
+
if (d <= limit && d < bestD) {
|
|
1406
|
+
best = i;
|
|
1407
|
+
bestD = d;
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
return best;
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* The colour step of a value.
|
|
1414
|
+
*
|
|
1415
|
+
* @param value - The point's value.
|
|
1416
|
+
* @param domain - `[low, high]`: `low` maps to the first step.
|
|
1417
|
+
* @param steps - How many steps. Defaults to `SCATTER_COLOUR_STEPS`.
|
|
1418
|
+
* @returns An integer in `[0, steps - 1]`; `-1` for a non-finite value; a degenerate domain puts
|
|
1419
|
+
* every value in step `0`.
|
|
1420
|
+
*/
|
|
1421
|
+
function colourStep(value, domain, steps = 24) {
|
|
1422
|
+
if (!Number.isFinite(value)) return -1;
|
|
1423
|
+
const [low, high] = domain;
|
|
1424
|
+
if (!(high > low)) return 0;
|
|
1425
|
+
return Math.min(steps - 1, Math.max(0, Math.floor((value - low) / (high - low) * steps)));
|
|
1426
|
+
}
|
|
1427
|
+
/**
|
|
1428
|
+
* The domain a value colouring covers.
|
|
1429
|
+
*
|
|
1430
|
+
* @param values - Every point's value; non-finite ones are ignored.
|
|
1431
|
+
* @param symmetric - Centre it on zero, for a diverging map.
|
|
1432
|
+
* @param given - A domain the caller fixed. It wins, centred if `symmetric`.
|
|
1433
|
+
* @returns `[low, high]`; `[0, 1]` when there is no finite value.
|
|
1434
|
+
*/
|
|
1435
|
+
function colourDomain(values, symmetric, given) {
|
|
1436
|
+
let low = Infinity;
|
|
1437
|
+
let high = -Infinity;
|
|
1438
|
+
if (given) [low, high] = given;
|
|
1439
|
+
else {
|
|
1440
|
+
for (const v of values) {
|
|
1441
|
+
if (!Number.isFinite(v)) continue;
|
|
1442
|
+
if (v < low) low = v;
|
|
1443
|
+
if (v > high) high = v;
|
|
1444
|
+
}
|
|
1445
|
+
if (low > high) return symmetric ? [-1, 1] : [0, 1];
|
|
1446
|
+
}
|
|
1447
|
+
return symmetric ? symmetricDomain([low, high]) : [low, high];
|
|
1448
|
+
}
|
|
1449
|
+
//#endregion
|
|
1450
|
+
//#region src/ScatterChart.tsx
|
|
1451
|
+
/**
|
|
1452
|
+
* A scatter plot: points over two numeric axes, each optionally coloured by a third value.
|
|
1453
|
+
*
|
|
1454
|
+
* Points are drawn as a few batched paths (one per series, or one per colour step with
|
|
1455
|
+
* `colourBy`), never one element per point, and the pointer is resolved from the projected
|
|
1456
|
+
* positions, not from the DOM. A few thousand points stay cheap.
|
|
1457
|
+
*/
|
|
1458
|
+
/** How near the pointer must be to a point, in viewBox units, to pick it. */
|
|
1459
|
+
const PICK_RADIUS = 10;
|
|
1460
|
+
const MUTED = "var(--fg-subtle)";
|
|
1461
|
+
const formatValue = (value) => Math.abs(value) >= 100 ? value.toFixed(0) : String(Number(value.toPrecision(3)));
|
|
1462
|
+
/**
|
|
1463
|
+
* A scatter plot over a shared frame — residuals against position, one run against another.
|
|
1464
|
+
*
|
|
1465
|
+
* @remarks
|
|
1466
|
+
* An SVG `role="img"` named by `label`, with the series legend (or, with `colourBy`, the colour
|
|
1467
|
+
* scale) as real text under it. Series are told apart by the legend's names, never by colour
|
|
1468
|
+
* alone. `referenceLines` mark a value on either axis; `onHover` rings the nearest point and
|
|
1469
|
+
* reads its numbers, `onPick` reports a click's point, and `highlighted` rings one chosen
|
|
1470
|
+
* elsewhere. Picking is by pointer, so the same points must be reachable from a table beside
|
|
1471
|
+
* the chart. A point with a non-finite coordinate is skipped.
|
|
1472
|
+
*/
|
|
1473
|
+
function ScatterChart({ series, label, xLabel, yLabel, xDomain, yDomain, colourBy, referenceLines, markerSize = 4, underlay, highlighted, onHover, onPick, footer, showLegend = true, variant = "panel", className, height = 200 }) {
|
|
1474
|
+
const clipId = useId();
|
|
1475
|
+
const [fluidRef, fluidSize] = useFluidSize(height);
|
|
1476
|
+
const size = variant === "fluid" ? fluidSize : void 0;
|
|
1477
|
+
const area = areaFor(variant, size);
|
|
1478
|
+
const allX = [];
|
|
1479
|
+
const allY = [];
|
|
1480
|
+
for (const entry of series) for (const p of entry.points) {
|
|
1481
|
+
allX.push(p.x);
|
|
1482
|
+
allY.push(p.y);
|
|
1483
|
+
}
|
|
1484
|
+
const xScale = linearScale(xDomain ?? paddedExtent(allX), area.x0, area.x1);
|
|
1485
|
+
const yScale = linearScale(yDomain ?? paddedExtent(allY), area.y0, area.y1);
|
|
1486
|
+
const symmetric = colourBy ? colourBy.symmetric ?? isDivergingColormap(colourBy.map) : false;
|
|
1487
|
+
const domain = useMemo(() => colourBy ? colourDomain(series.flatMap((s) => s.points.map((p) => p.value ?? NaN)), symmetric, colourBy.domain) : null, [
|
|
1488
|
+
series,
|
|
1489
|
+
colourBy,
|
|
1490
|
+
symmetric
|
|
1491
|
+
]);
|
|
1492
|
+
const projected = useMemo(() => {
|
|
1493
|
+
const xy = [];
|
|
1494
|
+
const owner = [];
|
|
1495
|
+
series.forEach((entry, s) => {
|
|
1496
|
+
entry.points.forEach((p, index) => {
|
|
1497
|
+
const px = xScale.project(p.x);
|
|
1498
|
+
const py = yScale.project(p.y);
|
|
1499
|
+
if (!Number.isFinite(p.x) || !Number.isFinite(p.y) || !Number.isFinite(px) || !Number.isFinite(py)) return;
|
|
1500
|
+
xy.push(px, py);
|
|
1501
|
+
owner.push({
|
|
1502
|
+
series: s,
|
|
1503
|
+
index
|
|
1504
|
+
});
|
|
1505
|
+
});
|
|
1506
|
+
});
|
|
1507
|
+
return {
|
|
1508
|
+
xy,
|
|
1509
|
+
owner
|
|
1510
|
+
};
|
|
1511
|
+
}, [
|
|
1512
|
+
series,
|
|
1513
|
+
xScale.domain[0],
|
|
1514
|
+
xScale.domain[1],
|
|
1515
|
+
yScale.domain[0],
|
|
1516
|
+
yScale.domain[1],
|
|
1517
|
+
area.x0,
|
|
1518
|
+
area.x1,
|
|
1519
|
+
area.y0,
|
|
1520
|
+
area.y1
|
|
1521
|
+
]);
|
|
1522
|
+
const batches = useMemo(() => {
|
|
1523
|
+
const out = [];
|
|
1524
|
+
let cursor = 0;
|
|
1525
|
+
series.forEach((entry, s) => {
|
|
1526
|
+
const parts = /* @__PURE__ */ new Map();
|
|
1527
|
+
while (cursor < projected.owner.length && projected.owner[cursor].series === s) {
|
|
1528
|
+
const point = entry.points[projected.owner[cursor].index];
|
|
1529
|
+
const step = colourBy && domain ? colourStep(point.value ?? NaN, domain) : 0;
|
|
1530
|
+
parts.set(step, `${parts.get(step) ?? ""}M${projected.xy[2 * cursor].toFixed(2)} ${projected.xy[2 * cursor + 1].toFixed(2)}h0`);
|
|
1531
|
+
cursor++;
|
|
1532
|
+
}
|
|
1533
|
+
for (const [step, d] of parts) {
|
|
1534
|
+
let colour;
|
|
1535
|
+
if (!colourBy) colour = entry.colour ?? seriesColour(s);
|
|
1536
|
+
else if (step < 0) colour = MUTED;
|
|
1537
|
+
else {
|
|
1538
|
+
const [r, g, b] = colormapRgb(colourBy.map, (step + .5) / 24);
|
|
1539
|
+
colour = `rgb(${r} ${g} ${b})`;
|
|
1540
|
+
}
|
|
1541
|
+
out.push({
|
|
1542
|
+
key: `${s}|${step}`,
|
|
1543
|
+
d,
|
|
1544
|
+
colour
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
1548
|
+
return out;
|
|
1549
|
+
}, [
|
|
1550
|
+
series,
|
|
1551
|
+
projected,
|
|
1552
|
+
colourBy,
|
|
1553
|
+
domain
|
|
1554
|
+
]);
|
|
1555
|
+
const [hover, setHover] = useState(null);
|
|
1556
|
+
const pickAt = (event) => {
|
|
1557
|
+
const svg = event.currentTarget.ownerSVGElement;
|
|
1558
|
+
const ctm = svg?.getScreenCTM();
|
|
1559
|
+
let x = event.clientX;
|
|
1560
|
+
let y = event.clientY;
|
|
1561
|
+
if (svg && ctm) {
|
|
1562
|
+
const point = svg.createSVGPoint();
|
|
1563
|
+
point.x = event.clientX;
|
|
1564
|
+
point.y = event.clientY;
|
|
1565
|
+
const local = point.matrixTransform(ctm.inverse());
|
|
1566
|
+
x = local.x;
|
|
1567
|
+
y = local.y;
|
|
1568
|
+
}
|
|
1569
|
+
const found = nearestProjected(projected.xy, x, y, PICK_RADIUS);
|
|
1570
|
+
return found < 0 ? null : pointOf(found);
|
|
1571
|
+
};
|
|
1572
|
+
const pointOf = (k) => {
|
|
1573
|
+
const { series: s, index } = projected.owner[k];
|
|
1574
|
+
const entry = series[s];
|
|
1575
|
+
const point = entry.points[index];
|
|
1576
|
+
return {
|
|
1577
|
+
series: entry.name,
|
|
1578
|
+
index,
|
|
1579
|
+
x: point.x,
|
|
1580
|
+
y: point.y,
|
|
1581
|
+
value: point.value
|
|
1582
|
+
};
|
|
1583
|
+
};
|
|
1584
|
+
const ringed = hover ?? (highlighted ? findPick(series, highlighted) : null);
|
|
1585
|
+
const ringAt = ringed ? {
|
|
1586
|
+
cx: xScale.project(ringed.x),
|
|
1587
|
+
cy: yScale.project(ringed.y)
|
|
1588
|
+
} : null;
|
|
1589
|
+
const interactive = onHover !== void 0 || onPick !== void 0;
|
|
1590
|
+
const radius = markerSize / 2;
|
|
1591
|
+
return /* @__PURE__ */ jsxs(Frame, {
|
|
1592
|
+
xScale,
|
|
1593
|
+
yScale,
|
|
1594
|
+
xLabel,
|
|
1595
|
+
yLabel,
|
|
1596
|
+
label,
|
|
1597
|
+
variant,
|
|
1598
|
+
className,
|
|
1599
|
+
size,
|
|
1600
|
+
figureRef: variant === "fluid" ? fluidRef : void 0,
|
|
1601
|
+
footer: /* @__PURE__ */ jsxs("div", {
|
|
1602
|
+
className: "flex flex-col gap-1",
|
|
1603
|
+
children: [colourBy && domain ? /* @__PURE__ */ jsx(ColormapLegend, {
|
|
1604
|
+
map: colourBy.map,
|
|
1605
|
+
domain,
|
|
1606
|
+
label: colourBy.label,
|
|
1607
|
+
unit: colourBy.unit,
|
|
1608
|
+
symmetric,
|
|
1609
|
+
format: formatValue
|
|
1610
|
+
}) : showLegend && /* @__PURE__ */ jsx(Legend, { items: series.map((entry, s) => ({
|
|
1611
|
+
label: entry.name,
|
|
1612
|
+
colour: entry.colour ?? seriesColour(s)
|
|
1613
|
+
})) }), footer]
|
|
1614
|
+
}),
|
|
1615
|
+
children: [
|
|
1616
|
+
/* @__PURE__ */ jsx("defs", { children: /* @__PURE__ */ jsx("clipPath", {
|
|
1617
|
+
id: clipId,
|
|
1618
|
+
children: /* @__PURE__ */ jsx("rect", {
|
|
1619
|
+
x: area.x0,
|
|
1620
|
+
y: area.y1,
|
|
1621
|
+
width: area.x1 - area.x0,
|
|
1622
|
+
height: area.y0 - area.y1
|
|
1623
|
+
})
|
|
1624
|
+
}) }),
|
|
1625
|
+
underlay?.(xScale, yScale),
|
|
1626
|
+
referenceLines && referenceLines.length > 0 && /* @__PURE__ */ jsx(ReferenceLines, {
|
|
1627
|
+
lines: referenceLines,
|
|
1628
|
+
xScale,
|
|
1629
|
+
yScale,
|
|
1630
|
+
area
|
|
1631
|
+
}),
|
|
1632
|
+
/* @__PURE__ */ jsxs("g", {
|
|
1633
|
+
clipPath: `url(#${clipId})`,
|
|
1634
|
+
"data-points": "",
|
|
1635
|
+
fill: "none",
|
|
1636
|
+
strokeLinecap: "round",
|
|
1637
|
+
children: [batches.map((batch) => /* @__PURE__ */ jsx("path", {
|
|
1638
|
+
d: batch.d,
|
|
1639
|
+
stroke: "var(--surface)",
|
|
1640
|
+
strokeWidth: markerSize + 1.5,
|
|
1641
|
+
opacity: .7
|
|
1642
|
+
}, batch.key)), batches.map((batch) => /* @__PURE__ */ jsx("path", {
|
|
1643
|
+
"data-batch": batch.key,
|
|
1644
|
+
d: batch.d,
|
|
1645
|
+
stroke: batch.colour,
|
|
1646
|
+
strokeWidth: markerSize
|
|
1647
|
+
}, `p|${batch.key}`))]
|
|
1648
|
+
}),
|
|
1649
|
+
ringAt && /* @__PURE__ */ jsxs("g", {
|
|
1650
|
+
"data-highlight": "",
|
|
1651
|
+
pointerEvents: "none",
|
|
1652
|
+
children: [/* @__PURE__ */ jsx("circle", {
|
|
1653
|
+
...ringAt,
|
|
1654
|
+
r: radius + 3,
|
|
1655
|
+
fill: "none",
|
|
1656
|
+
stroke: "var(--surface)",
|
|
1657
|
+
strokeWidth: 3.5
|
|
1658
|
+
}), /* @__PURE__ */ jsx("circle", {
|
|
1659
|
+
...ringAt,
|
|
1660
|
+
r: radius + 3,
|
|
1661
|
+
fill: "none",
|
|
1662
|
+
stroke: "var(--signal)",
|
|
1663
|
+
strokeWidth: 1.75
|
|
1664
|
+
})]
|
|
1665
|
+
}),
|
|
1666
|
+
hover && /* @__PURE__ */ jsx("text", {
|
|
1667
|
+
"data-readout": "",
|
|
1668
|
+
x: xScale.project(hover.x) > (area.x0 + area.x1) / 2 ? area.x1 - 4 : area.x0 + 4,
|
|
1669
|
+
y: area.y1 + 10,
|
|
1670
|
+
textAnchor: xScale.project(hover.x) > (area.x0 + area.x1) / 2 ? "end" : "start",
|
|
1671
|
+
fontSize: 10,
|
|
1672
|
+
fill: "currentColor",
|
|
1673
|
+
children: [`${formatValue(hover.x)}, ${formatValue(hover.y)}`, hover.value !== void 0 ? formatValue(hover.value) : null].filter((part) => part !== null).join(" · ")
|
|
1674
|
+
}),
|
|
1675
|
+
interactive && /* @__PURE__ */ jsx("rect", {
|
|
1676
|
+
"data-plot-target": "",
|
|
1677
|
+
x: area.x0,
|
|
1678
|
+
y: area.y1,
|
|
1679
|
+
width: area.x1 - area.x0,
|
|
1680
|
+
height: area.y0 - area.y1,
|
|
1681
|
+
fill: "transparent",
|
|
1682
|
+
style: { cursor: onPick ? "pointer" : "crosshair" },
|
|
1683
|
+
onPointerMove: (event) => {
|
|
1684
|
+
const next = pickAt(event);
|
|
1685
|
+
setHover(next);
|
|
1686
|
+
onHover?.(next);
|
|
1687
|
+
},
|
|
1688
|
+
onPointerLeave: () => {
|
|
1689
|
+
setHover(null);
|
|
1690
|
+
onHover?.(null);
|
|
1691
|
+
},
|
|
1692
|
+
onClick: (event) => {
|
|
1693
|
+
const picked = pickAt(event);
|
|
1694
|
+
if (picked) onPick?.(picked);
|
|
1695
|
+
}
|
|
1696
|
+
})
|
|
1697
|
+
]
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
/** The extent of the values with 4 % on each side, so a marker on an extreme is not cut by the axis or the frame. */
|
|
1701
|
+
function paddedExtent(values) {
|
|
1702
|
+
const [low, high] = extent(values);
|
|
1703
|
+
const pad = (high - low) * .04;
|
|
1704
|
+
return [low - pad, high + pad];
|
|
1705
|
+
}
|
|
1706
|
+
/** The point a `highlighted` names, or `null` when it names none that exists and is finite. */
|
|
1707
|
+
function findPick(series, at) {
|
|
1708
|
+
const point = series.find((s) => s.name === at.series)?.points[at.index];
|
|
1709
|
+
if (!point || !Number.isFinite(point.x) || !Number.isFinite(point.y)) return null;
|
|
1710
|
+
return {
|
|
1711
|
+
series: at.series,
|
|
1712
|
+
index: at.index,
|
|
1713
|
+
x: point.x,
|
|
1714
|
+
y: point.y,
|
|
1715
|
+
value: point.value
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
/** Dashed rules across the plot, with their labels at the end. */
|
|
1719
|
+
function ReferenceLines({ lines, xScale, yScale, area }) {
|
|
1720
|
+
return /* @__PURE__ */ jsx("g", {
|
|
1721
|
+
"data-reference-lines": "",
|
|
1722
|
+
children: lines.map((line) => {
|
|
1723
|
+
const horizontal = line.axis === "y";
|
|
1724
|
+
const at = (horizontal ? yScale : xScale).project(line.value);
|
|
1725
|
+
const [lo, hi] = horizontal ? [area.y1, area.y0] : [area.x0, area.x1];
|
|
1726
|
+
if (!Number.isFinite(at) || at < Math.min(lo, hi) - .5 || at > Math.max(lo, hi) + .5) return null;
|
|
1727
|
+
const colour = toneColor(line.tone, "muted");
|
|
1728
|
+
return /* @__PURE__ */ jsxs("g", {
|
|
1729
|
+
"data-reference-line": line.axis,
|
|
1730
|
+
children: [/* @__PURE__ */ jsx("line", {
|
|
1731
|
+
x1: horizontal ? area.x0 : at,
|
|
1732
|
+
x2: horizontal ? area.x1 : at,
|
|
1733
|
+
y1: horizontal ? at : area.y0,
|
|
1734
|
+
y2: horizontal ? at : area.y1,
|
|
1735
|
+
stroke: colour,
|
|
1736
|
+
strokeWidth: 1.25,
|
|
1737
|
+
strokeDasharray: "3 2"
|
|
1738
|
+
}), line.label && /* @__PURE__ */ jsx("text", {
|
|
1739
|
+
x: horizontal ? area.x1 - 3 : at + 3,
|
|
1740
|
+
y: horizontal ? at - 3 : area.y1 + 10,
|
|
1741
|
+
textAnchor: horizontal ? "end" : "start",
|
|
1742
|
+
fontSize: 9,
|
|
1743
|
+
fill: colour,
|
|
1744
|
+
children: line.label
|
|
1745
|
+
})]
|
|
1746
|
+
}, `${line.axis}|${line.value}|${line.label ?? ""}`);
|
|
1747
|
+
})
|
|
1748
|
+
});
|
|
1749
|
+
}
|
|
1750
|
+
//#endregion
|
|
1751
|
+
//#region src/BarChart.tsx
|
|
1752
|
+
/**
|
|
1753
|
+
* Horizontal stacked bars — a per-category breakdown.
|
|
1754
|
+
*
|
|
1755
|
+
* Horizontal because the categories are free text — a defect type, a part family — and a
|
|
1756
|
+
* vertical axis would either truncate them or turn them sideways. Stacked because the
|
|
1757
|
+
* usual question is "of this category, how many landed in each outcome", which is a
|
|
1758
|
+
* proportion of a known total rather than independent counts.
|
|
1759
|
+
*/
|
|
1760
|
+
/**
|
|
1761
|
+
* Horizontal stacked bars, one row per category, all on one shared scale (the widest row's
|
|
1762
|
+
* total).
|
|
1763
|
+
*
|
|
1764
|
+
* @remarks
|
|
1765
|
+
* Exposed as an ARIA `table`: each row has a `rowheader` (the category) and a text cell that
|
|
1766
|
+
* spells every non-zero segment out as `"<value> <name>"`, so nothing depends on colour or
|
|
1767
|
+
* on bar width. A row with no non-zero segment shows its total.
|
|
1768
|
+
*/
|
|
1769
|
+
function StackedBars({ rows, label, className }) {
|
|
1770
|
+
const widest = Math.max(1, ...rows.map((row) => row.segments.reduce((total, segment) => total + segment.value, 0)));
|
|
1771
|
+
return /* @__PURE__ */ jsx("div", {
|
|
1772
|
+
className: cn("flex flex-col gap-2", className),
|
|
1773
|
+
role: "table",
|
|
1774
|
+
"aria-label": label,
|
|
1775
|
+
children: rows.map((row) => {
|
|
1776
|
+
const total = row.segments.reduce((sum, segment) => sum + segment.value, 0);
|
|
1777
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1778
|
+
className: "flex items-center gap-3 text-xs",
|
|
1779
|
+
role: "row",
|
|
1780
|
+
children: [
|
|
1781
|
+
/* @__PURE__ */ jsx("span", {
|
|
1782
|
+
className: "w-28 shrink-0 truncate font-mono",
|
|
1783
|
+
role: "rowheader",
|
|
1784
|
+
title: row.label,
|
|
1785
|
+
children: row.label
|
|
1786
|
+
}),
|
|
1787
|
+
/* @__PURE__ */ jsx("div", {
|
|
1788
|
+
className: "flex h-4 flex-1 overflow-hidden rounded-control bg-raised",
|
|
1789
|
+
role: "cell",
|
|
1790
|
+
children: row.segments.map((segment) => segment.value === 0 ? null : /* @__PURE__ */ jsx("div", {
|
|
1791
|
+
className: "h-full",
|
|
1792
|
+
style: {
|
|
1793
|
+
width: `${segment.value / widest * 100}%`,
|
|
1794
|
+
backgroundColor: segment.colour
|
|
1795
|
+
},
|
|
1796
|
+
title: `${segment.name}: ${segment.value}`
|
|
1797
|
+
}, segment.name))
|
|
1798
|
+
}),
|
|
1799
|
+
/* @__PURE__ */ jsx("span", {
|
|
1800
|
+
className: "w-24 shrink-0 text-right font-mono text-fg-muted",
|
|
1801
|
+
role: "cell",
|
|
1802
|
+
children: row.segments.filter((segment) => segment.value > 0).map((segment) => `${segment.value} ${segment.name}`).join(" · ") || `${total}`
|
|
1803
|
+
})
|
|
1804
|
+
]
|
|
1805
|
+
}, row.label);
|
|
1806
|
+
})
|
|
1807
|
+
});
|
|
1808
|
+
}
|
|
1809
|
+
/** A finite, ordered, non-degenerate domain; `[0, 1]` when `domain` is unusable. */
|
|
1810
|
+
function usableDomain(low, high) {
|
|
1811
|
+
return padDomain(Math.min(low, high), Math.max(low, high));
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Bin raw values into `bins` equal-width buckets.
|
|
1815
|
+
*
|
|
1816
|
+
* @param values - The samples. Non-finite values are ignored, and so are values outside an
|
|
1817
|
+
* explicit `domain` (the upper edge itself belongs to the last bin).
|
|
1818
|
+
* @param bins - Bucket count; a non-finite or sub-1 request falls back to 1, and `undefined`
|
|
1819
|
+
* to {@link DEFAULT_BINS}.
|
|
1820
|
+
* @param domain - The x range. Omitted: the data's extent. A single distinct value is
|
|
1821
|
+
* widened so it lands in a middle bin rather than on an edge.
|
|
1822
|
+
* @returns The counts and the domain they span.
|
|
1823
|
+
*/
|
|
1824
|
+
function binValues(values, bins, domain) {
|
|
1825
|
+
const count = bins === void 0 ? 32 : Number.isFinite(bins) ? Math.max(1, Math.floor(bins)) : 1;
|
|
1826
|
+
const finite = [];
|
|
1827
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
1828
|
+
const value = values[index];
|
|
1829
|
+
if (value !== void 0 && Number.isFinite(value)) finite.push(value);
|
|
1830
|
+
}
|
|
1831
|
+
const [low, high] = domain ? usableDomain(domain[0], domain[1]) : usableDomain(...extent(finite));
|
|
1832
|
+
const counts = new Array(count).fill(0);
|
|
1833
|
+
const span = high - low;
|
|
1834
|
+
for (const value of finite) {
|
|
1835
|
+
if (value < low || value > high) continue;
|
|
1836
|
+
const index = Math.min(count - 1, Math.floor((value - low) / span * count));
|
|
1837
|
+
counts[index] = (counts[index] ?? 0) + 1;
|
|
1838
|
+
}
|
|
1839
|
+
return {
|
|
1840
|
+
counts,
|
|
1841
|
+
domain: [low, high]
|
|
1842
|
+
};
|
|
1843
|
+
}
|
|
1844
|
+
/**
|
|
1845
|
+
* Take pre-binned counts as given, with the junk removed.
|
|
1846
|
+
*
|
|
1847
|
+
* @param counts - One count per bin, left to right.
|
|
1848
|
+
* @param domain - The x range the bins span.
|
|
1849
|
+
* @returns The counts with every non-finite or negative entry set to 0, and a usable domain.
|
|
1850
|
+
*/
|
|
1851
|
+
function cleanCounts(counts, domain) {
|
|
1852
|
+
return {
|
|
1853
|
+
counts: Array.from(counts, (value) => Number.isFinite(value) && value > 0 ? value : 0),
|
|
1854
|
+
domain: usableDomain(domain[0], domain[1])
|
|
1855
|
+
};
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* The bin containing `x`, or `null` when `x` is not finite, outside `domain`, or there are
|
|
1859
|
+
* no bins. The domain's upper edge belongs to the last bin.
|
|
1860
|
+
*/
|
|
1861
|
+
function binAt(binCount, domain, x) {
|
|
1862
|
+
if (x === null || x === void 0 || !Number.isFinite(x) || binCount < 1) return null;
|
|
1863
|
+
const [low, high] = domain;
|
|
1864
|
+
if (x < low || x > high) return null;
|
|
1865
|
+
return Math.min(binCount - 1, Math.floor((x - low) / (high - low) * binCount));
|
|
1866
|
+
}
|
|
1867
|
+
/**
|
|
1868
|
+
* One SVG path `d` drawing every non-empty bar as a rectangle from the baseline.
|
|
1869
|
+
*
|
|
1870
|
+
* @param counts - The bins' counts.
|
|
1871
|
+
* @param yScale - Maps a count to a pixel `y`.
|
|
1872
|
+
* @param area - The plot area; the bins fill `x0`…`x1` and stand on `y0`.
|
|
1873
|
+
* @param select - `{ skip: null }` (the default) draws every bar; `{ only: i }` draws just
|
|
1874
|
+
* bin `i`; `{ skip: i }` draws all but bin `i` — so a highlighted bin is its own path.
|
|
1875
|
+
*/
|
|
1876
|
+
function barsPath(counts, yScale, area, select = { skip: null }) {
|
|
1877
|
+
const width = (area.x1 - area.x0) / Math.max(1, counts.length);
|
|
1878
|
+
const barWidth = Math.max(.5, width > 3 ? width - .5 : width);
|
|
1879
|
+
const parts = [];
|
|
1880
|
+
counts.forEach((count, index) => {
|
|
1881
|
+
if (count <= 0) return;
|
|
1882
|
+
if ("only" in select ? index !== select.only : index === select.skip) return;
|
|
1883
|
+
const x = area.x0 + index * width;
|
|
1884
|
+
const y = yScale.project(count);
|
|
1885
|
+
if (!Number.isFinite(y)) return;
|
|
1886
|
+
parts.push(`M${x.toFixed(2)} ${area.y0}V${y.toFixed(2)}h${barWidth.toFixed(2)}V${area.y0}z`);
|
|
1887
|
+
});
|
|
1888
|
+
return parts.join("");
|
|
1889
|
+
}
|
|
1890
|
+
/** The largest count, at least 1, so an empty or all-zero histogram still has a y axis. */
|
|
1891
|
+
function tallest(...series) {
|
|
1892
|
+
let max = 1;
|
|
1893
|
+
for (const counts of series) for (const count of counts) if (count > max) max = count;
|
|
1894
|
+
return max;
|
|
1895
|
+
}
|
|
1896
|
+
//#endregion
|
|
1897
|
+
//#region src/ThresholdRule.tsx
|
|
1898
|
+
/** A dashed vertical rule at `value`; nothing when it is missing or not finite. */
|
|
1899
|
+
function ThresholdRule({ value, xScale, area }) {
|
|
1900
|
+
if (value === void 0 || !Number.isFinite(value)) return null;
|
|
1901
|
+
const x = xScale.project(value);
|
|
1902
|
+
return /* @__PURE__ */ jsx("line", {
|
|
1903
|
+
"data-threshold": "",
|
|
1904
|
+
x1: x,
|
|
1905
|
+
x2: x,
|
|
1906
|
+
y1: area.y0,
|
|
1907
|
+
y2: area.y1,
|
|
1908
|
+
stroke: "currentColor",
|
|
1909
|
+
strokeWidth: 1.25,
|
|
1910
|
+
strokeDasharray: "3 2"
|
|
1911
|
+
});
|
|
1912
|
+
}
|
|
1913
|
+
//#endregion
|
|
1914
|
+
//#region src/Histogram.tsx
|
|
1915
|
+
/**
|
|
1916
|
+
* One distribution as a bar chart over equal-width bins.
|
|
1917
|
+
*
|
|
1918
|
+
* @remarks
|
|
1919
|
+
* An SVG `role="img"` named by `label`. `threshold` draws a dashed decision rule, `markers`
|
|
1920
|
+
* tick the x axis, `bands` shade x ranges, `onHover` adds a crosshair and the x under it,
|
|
1921
|
+
* `onPick` reports the x of a click, and `cursor` draws the bin containing an x chosen
|
|
1922
|
+
* elsewhere in the selection colour (`--signal`) — no highlight when it lies outside the
|
|
1923
|
+
* domain. Empty data draws an empty frame. For two classes sharing a threshold use
|
|
849
1924
|
* {@link ScoreHistogram}.
|
|
850
1925
|
*/
|
|
851
1926
|
function Histogram(props) {
|
|
@@ -1112,193 +2187,6 @@ function LineProfile({ series, edges = [], label, xLabel = "arc length (px)", yL
|
|
|
1112
2187
|
});
|
|
1113
2188
|
}
|
|
1114
2189
|
//#endregion
|
|
1115
|
-
|
|
1116
|
-
const ANCHORS = {
|
|
1117
|
-
viridis: [
|
|
1118
|
-
[
|
|
1119
|
-
68,
|
|
1120
|
-
1,
|
|
1121
|
-
84
|
|
1122
|
-
],
|
|
1123
|
-
[
|
|
1124
|
-
71,
|
|
1125
|
-
44,
|
|
1126
|
-
122
|
|
1127
|
-
],
|
|
1128
|
-
[
|
|
1129
|
-
59,
|
|
1130
|
-
81,
|
|
1131
|
-
139
|
|
1132
|
-
],
|
|
1133
|
-
[
|
|
1134
|
-
44,
|
|
1135
|
-
113,
|
|
1136
|
-
142
|
|
1137
|
-
],
|
|
1138
|
-
[
|
|
1139
|
-
33,
|
|
1140
|
-
144,
|
|
1141
|
-
141
|
|
1142
|
-
],
|
|
1143
|
-
[
|
|
1144
|
-
39,
|
|
1145
|
-
173,
|
|
1146
|
-
129
|
|
1147
|
-
],
|
|
1148
|
-
[
|
|
1149
|
-
92,
|
|
1150
|
-
200,
|
|
1151
|
-
99
|
|
1152
|
-
],
|
|
1153
|
-
[
|
|
1154
|
-
170,
|
|
1155
|
-
220,
|
|
1156
|
-
50
|
|
1157
|
-
],
|
|
1158
|
-
[
|
|
1159
|
-
253,
|
|
1160
|
-
231,
|
|
1161
|
-
37
|
|
1162
|
-
]
|
|
1163
|
-
],
|
|
1164
|
-
cividis: [
|
|
1165
|
-
[
|
|
1166
|
-
0,
|
|
1167
|
-
34,
|
|
1168
|
-
78
|
|
1169
|
-
],
|
|
1170
|
-
[
|
|
1171
|
-
18,
|
|
1172
|
-
53,
|
|
1173
|
-
112
|
|
1174
|
-
],
|
|
1175
|
-
[
|
|
1176
|
-
59,
|
|
1177
|
-
73,
|
|
1178
|
-
108
|
|
1179
|
-
],
|
|
1180
|
-
[
|
|
1181
|
-
87,
|
|
1182
|
-
93,
|
|
1183
|
-
109
|
|
1184
|
-
],
|
|
1185
|
-
[
|
|
1186
|
-
112,
|
|
1187
|
-
113,
|
|
1188
|
-
115
|
|
1189
|
-
],
|
|
1190
|
-
[
|
|
1191
|
-
138,
|
|
1192
|
-
135,
|
|
1193
|
-
121
|
|
1194
|
-
],
|
|
1195
|
-
[
|
|
1196
|
-
165,
|
|
1197
|
-
157,
|
|
1198
|
-
116
|
|
1199
|
-
],
|
|
1200
|
-
[
|
|
1201
|
-
196,
|
|
1202
|
-
182,
|
|
1203
|
-
101
|
|
1204
|
-
],
|
|
1205
|
-
[
|
|
1206
|
-
254,
|
|
1207
|
-
232,
|
|
1208
|
-
56
|
|
1209
|
-
]
|
|
1210
|
-
]
|
|
1211
|
-
};
|
|
1212
|
-
/** The names of the shipped maps. */
|
|
1213
|
-
const COLORMAPS = ["viridis", "cividis"];
|
|
1214
|
-
/**
|
|
1215
|
-
* The colour of `t` on a map.
|
|
1216
|
-
*
|
|
1217
|
-
* @param name - The map.
|
|
1218
|
-
* @param t - Position in `[0, 1]`; clamped. A non-finite `t` gives the map's low end.
|
|
1219
|
-
* @returns A CSS `rgb(r g b)` colour.
|
|
1220
|
-
*/
|
|
1221
|
-
function colormap(name, t) {
|
|
1222
|
-
const [r, g, b] = colormapRgb(name, t);
|
|
1223
|
-
return `rgb(${r} ${g} ${b})`;
|
|
1224
|
-
}
|
|
1225
|
-
/**
|
|
1226
|
-
* The colour of `t` on a map, as integer channels.
|
|
1227
|
-
*
|
|
1228
|
-
* @param name - The map.
|
|
1229
|
-
* @param t - Position in `[0, 1]`; clamped.
|
|
1230
|
-
* @returns `[r, g, b]`, each 0–255.
|
|
1231
|
-
*/
|
|
1232
|
-
function colormapRgb(name, t) {
|
|
1233
|
-
const anchors = ANCHORS[name];
|
|
1234
|
-
const scaled = (Number.isFinite(t) ? Math.min(1, Math.max(0, t)) : 0) * (anchors.length - 1);
|
|
1235
|
-
const i = Math.min(anchors.length - 2, Math.floor(scaled));
|
|
1236
|
-
const f = scaled - i;
|
|
1237
|
-
const a = anchors[i];
|
|
1238
|
-
const b = anchors[i + 1];
|
|
1239
|
-
return [
|
|
1240
|
-
Math.round(a[0] + f * (b[0] - a[0])),
|
|
1241
|
-
Math.round(a[1] + f * (b[1] - a[1])),
|
|
1242
|
-
Math.round(a[2] + f * (b[2] - a[2]))
|
|
1243
|
-
];
|
|
1244
|
-
}
|
|
1245
|
-
/**
|
|
1246
|
-
* A value's colour on a map, over a domain.
|
|
1247
|
-
*
|
|
1248
|
-
* @param name - The map.
|
|
1249
|
-
* @param value - The value.
|
|
1250
|
-
* @param domain - `[low, high]`: low maps to the map's start. A reversed domain reverses the map.
|
|
1251
|
-
* @returns A CSS colour.
|
|
1252
|
-
*/
|
|
1253
|
-
function colormapValue(name, value, domain) {
|
|
1254
|
-
const [low, high] = domain;
|
|
1255
|
-
const span = high - low;
|
|
1256
|
-
return colormap(name, span === 0 ? 0 : (value - low) / span);
|
|
1257
|
-
}
|
|
1258
|
-
/**
|
|
1259
|
-
* A CSS `linear-gradient` of a map, for a legend bar.
|
|
1260
|
-
*
|
|
1261
|
-
* @param name - The map.
|
|
1262
|
-
* @param direction - The gradient's direction. Defaults to `to right`.
|
|
1263
|
-
* @returns A CSS `background-image` value.
|
|
1264
|
-
*/
|
|
1265
|
-
function colormapGradient(name, direction = "to right") {
|
|
1266
|
-
const anchors = ANCHORS[name];
|
|
1267
|
-
return `linear-gradient(${direction}, ${anchors.map(([r, g, b], i) => `rgb(${r} ${g} ${b}) ${(i / (anchors.length - 1) * 100).toFixed(1)}%`).join(", ")})`;
|
|
1268
|
-
}
|
|
1269
|
-
//#endregion
|
|
1270
|
-
//#region src/ColormapLegend.tsx
|
|
1271
|
-
/**
|
|
1272
|
-
* The scale of a colour map, shown beside the map: every chart or map shows its scale, with units.
|
|
1273
|
-
*/
|
|
1274
|
-
/**
|
|
1275
|
-
* A gradient bar with its end values and unit, read as one line: "Edge error 0 [bar] 2.5 px".
|
|
1276
|
-
* The bar is decorative; the label and the end values carry the meaning as text.
|
|
1277
|
-
*/
|
|
1278
|
-
function ColormapLegend({ map, domain, unit, label, format = (value) => String(Number(value.toPrecision(3))), className }) {
|
|
1279
|
-
const end = (value) => unit ? `${format(value)} ${unit}` : format(value);
|
|
1280
|
-
return /* @__PURE__ */ jsxs("div", {
|
|
1281
|
-
className: cn("flex items-center gap-2 text-xs text-fg-muted", className),
|
|
1282
|
-
"data-colormap": map,
|
|
1283
|
-
children: [
|
|
1284
|
-
/* @__PURE__ */ jsx("span", { children: label }),
|
|
1285
|
-
/* @__PURE__ */ jsx("span", {
|
|
1286
|
-
className: "font-mono tabular-nums",
|
|
1287
|
-
children: end(domain[0])
|
|
1288
|
-
}),
|
|
1289
|
-
/* @__PURE__ */ jsx("span", {
|
|
1290
|
-
"aria-hidden": true,
|
|
1291
|
-
className: "h-2 w-24 rounded-full",
|
|
1292
|
-
style: { backgroundImage: colormapGradient(map) }
|
|
1293
|
-
}),
|
|
1294
|
-
/* @__PURE__ */ jsx("span", {
|
|
1295
|
-
className: "font-mono tabular-nums",
|
|
1296
|
-
children: end(domain[1])
|
|
1297
|
-
})
|
|
1298
|
-
]
|
|
1299
|
-
});
|
|
1300
|
-
}
|
|
1301
|
-
//#endregion
|
|
1302
|
-
export { Bands, COLORMAPS, ColormapLegend, DEFECT_COLOUR, FLUID_DEFAULT_HEIGHT, Frame, Histogram, Legend, LineChart, LineProfile, Markers, NORMAL_COLOUR, PlotInteraction, SERIES_COLOURS, ScoreHistogram, StackedBars, areaFor, colormap, colormapGradient, colormapRgb, colormapValue, extent, formatTick, histogram, linePath, linearScale, logScale, niceStep, padDomain, plotArea, seriesColour, seriesValueAt, useFluidSize };
|
|
2190
|
+
export { Bands, COLORMAPS, ColormapLegend, DEFECT_COLOUR, DIVERGING_COLORMAPS, FLUID_DEFAULT_HEIGHT, Frame, Histogram, Legend, LineChart, LineProfile, Markers, NORMAL_COLOUR, PlotInteraction, SCATTER_COLOUR_STEPS, SERIES_COLOURS, ScatterChart, ScoreHistogram, StackedBars, areaFor, bandPath, colormap, colormapGradient, colormapRgb, colormapValue, colourDomain, colourStep, extent, formatTick, formatWithBand, histogram, isDivergingColormap, linePath, linearScale, logScale, nearestProjected, niceStep, padDomain, plotArea, seriesColour, seriesValueAt, symmetricDomain, useFluidSize };
|
|
1303
2191
|
|
|
1304
2192
|
//# sourceMappingURL=index.js.map
|