@vitavision/charts 0.6.4 → 0.7.1
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 +13 -0
- package/dist/index.d.ts +225 -17
- package/dist/index.js +516 -57
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/ColormapLegend.tsx +47 -0
- package/src/Frame.tsx +49 -9
- package/src/LineChart.tsx +32 -4
- package/src/LineProfile.tsx +32 -4
- package/src/colormap.ts +109 -0
- package/src/index.ts +25 -0
- package/src/interaction.tsx +260 -0
- package/src/scale.ts +4 -0
package/dist/index.js
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
1
2
|
import { cn, toneColor } from "@vitavision/ui";
|
|
2
3
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
4
|
//#region src/Frame.tsx
|
|
5
|
+
/**
|
|
6
|
+
* The plot frame every chart here composes: axes, ticks, grid, labels.
|
|
7
|
+
*
|
|
8
|
+
* Charts are drawn into a fixed `viewBox` and scaled by CSS, so one component works at
|
|
9
|
+
* any panel width without measuring the DOM. The consequence is that font sizes are in
|
|
10
|
+
* viewBox units and therefore scale with the chart: the *same* 10-unit label renders at
|
|
11
|
+
* 9 px in a half-width panel and at 21 px across a full-width one, which looks like two
|
|
12
|
+
* different design systems on one screen.
|
|
13
|
+
*
|
|
14
|
+
* So the viewBox is chosen to roughly match the width the chart will actually be given.
|
|
15
|
+
* `panel` is for the two-column grids, `wide` for a chart that spans a panel on its own.
|
|
16
|
+
* That keeps the scale factor near 1 in both, and it is why callers pick a variant rather
|
|
17
|
+
* than a pixel width — the variant says where the chart is going, not how big to draw it.
|
|
18
|
+
*/
|
|
19
|
+
/** The width a `fluid` chart assumes before it has been measured, and on the server. */
|
|
20
|
+
const FLUID_INITIAL_WIDTH = 480;
|
|
21
|
+
/** A `fluid` chart's default height, in CSS pixels. */
|
|
22
|
+
const FLUID_DEFAULT_HEIGHT = 200;
|
|
4
23
|
const GEOMETRY = {
|
|
5
24
|
panel: {
|
|
6
25
|
width: 480,
|
|
@@ -21,12 +40,16 @@ const MARGIN = {
|
|
|
21
40
|
* The drawable rectangle for one variant, in that variant's viewBox units.
|
|
22
41
|
*
|
|
23
42
|
* @param variant - Which viewBox the chart is drawn in.
|
|
43
|
+
* @param size - For `fluid`: the measured size in CSS pixels (see {@link useFluidSize}).
|
|
24
44
|
* @returns The plot area's edges — `x0`/`x1` left to right, `y0` the bottom and `y1` the top
|
|
25
45
|
* (SVG y grows downwards) — and the whole viewBox's `width`/`height`. Build scales over
|
|
26
46
|
* `[x0, x1]` and `[y0, y1]`.
|
|
27
47
|
*/
|
|
28
|
-
function areaFor(variant) {
|
|
29
|
-
const { width, height } =
|
|
48
|
+
function areaFor(variant, size) {
|
|
49
|
+
const { width, height } = variant === "fluid" ? size ?? {
|
|
50
|
+
width: FLUID_INITIAL_WIDTH,
|
|
51
|
+
height: 200
|
|
52
|
+
} : GEOMETRY[variant];
|
|
30
53
|
return {
|
|
31
54
|
x0: MARGIN.left,
|
|
32
55
|
x1: width - MARGIN.right,
|
|
@@ -38,6 +61,29 @@ function areaFor(variant) {
|
|
|
38
61
|
}
|
|
39
62
|
/** The default geometry, for callers that do not care which variant they are in. */
|
|
40
63
|
const plotArea = areaFor("panel");
|
|
64
|
+
/**
|
|
65
|
+
* The size of a `fluid` chart: its container's width, measured, at `height`.
|
|
66
|
+
*
|
|
67
|
+
* @param height - The chart's height in CSS pixels. Defaults to {@link FLUID_DEFAULT_HEIGHT}.
|
|
68
|
+
* @returns `[ref, size]`. Attach `ref` to the element whose width the chart takes (the
|
|
69
|
+
* `Frame`'s `figureRef`). The size is a 480 px-wide default until the first measure.
|
|
70
|
+
*/
|
|
71
|
+
function useFluidSize(height = 200) {
|
|
72
|
+
const [width, setWidth] = useState(FLUID_INITIAL_WIDTH);
|
|
73
|
+
return [useCallback((element) => {
|
|
74
|
+
if (!element) return;
|
|
75
|
+
const measure = (w) => {
|
|
76
|
+
if (w > 0) setWidth(Math.round(w));
|
|
77
|
+
};
|
|
78
|
+
measure(element.getBoundingClientRect().width);
|
|
79
|
+
const observer = new ResizeObserver((entries) => measure(entries[0]?.contentRect.width ?? 0));
|
|
80
|
+
observer.observe(element);
|
|
81
|
+
return () => observer.disconnect();
|
|
82
|
+
}, []), {
|
|
83
|
+
width,
|
|
84
|
+
height
|
|
85
|
+
}];
|
|
86
|
+
}
|
|
41
87
|
/** Held at the two ends of the neutral ramp, matching the design system's chrome colours. */
|
|
42
88
|
const AXIS = "currentColor";
|
|
43
89
|
/**
|
|
@@ -47,12 +93,16 @@ const AXIS = "currentColor";
|
|
|
47
93
|
* Renders a `<figure>` holding an SVG with `role="img"` named by `label`, and the `footer`
|
|
48
94
|
* as its `<figcaption>`. `children` are drawn in viewBox units over the grid.
|
|
49
95
|
*/
|
|
50
|
-
function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, children, label, footer, variant = "panel", className }) {
|
|
96
|
+
function Frame({ xScale, yScale, xLabel, yLabel, xTicks = 5, yTicks = 4, children, label, footer, variant = "panel", className, size, figureRef }) {
|
|
51
97
|
const horizontal = xScale.ticks(xTicks);
|
|
52
98
|
const vertical = yScale.ticks(yTicks);
|
|
53
|
-
const plotArea = areaFor(variant);
|
|
54
|
-
const PLOT =
|
|
99
|
+
const plotArea = areaFor(variant, size);
|
|
100
|
+
const PLOT = {
|
|
101
|
+
width: plotArea.width,
|
|
102
|
+
height: plotArea.height
|
|
103
|
+
};
|
|
55
104
|
return /* @__PURE__ */ jsxs("figure", {
|
|
105
|
+
ref: figureRef,
|
|
56
106
|
className: cn("flex flex-col gap-1 text-fg-muted", className),
|
|
57
107
|
children: [/* @__PURE__ */ jsxs("svg", {
|
|
58
108
|
role: "img",
|
|
@@ -300,6 +350,7 @@ function linearScale(domain, rangeStart, rangeEnd) {
|
|
|
300
350
|
return {
|
|
301
351
|
domain: [low, high],
|
|
302
352
|
project: (value) => rangeStart + (value - low) / span * (rangeEnd - rangeStart),
|
|
353
|
+
invert: (pixel) => low + (pixel - rangeStart) / (rangeEnd - rangeStart) * span,
|
|
303
354
|
ticks: (count = 5) => {
|
|
304
355
|
const step = niceStep(span / Math.max(1, count));
|
|
305
356
|
const first = Math.ceil(low / step) * step;
|
|
@@ -331,6 +382,7 @@ function logScale(domain, rangeStart, rangeEnd) {
|
|
|
331
382
|
project: (value) => {
|
|
332
383
|
return rangeStart + (Math.log10(Math.max(value, LOG_FLOOR)) - paddedLow) / span * (rangeEnd - rangeStart);
|
|
333
384
|
},
|
|
385
|
+
invert: (pixel) => 10 ** (paddedLow + (pixel - rangeStart) / (rangeEnd - rangeStart) * span),
|
|
334
386
|
ticks: (count = 5) => {
|
|
335
387
|
const stride = Math.max(1, Math.ceil(span / Math.max(1, count)));
|
|
336
388
|
const ticks = [];
|
|
@@ -381,18 +433,185 @@ function histogram(values, domain, count) {
|
|
|
381
433
|
return bins;
|
|
382
434
|
}
|
|
383
435
|
//#endregion
|
|
436
|
+
//#region src/interaction.tsx
|
|
437
|
+
/**
|
|
438
|
+
* What `LineChart` and `LineProfile` share beyond the frame:
|
|
439
|
+
* - x ranges and positions marked on the plot (`bands`, `markers`);
|
|
440
|
+
* - a pointer that reads the plot back (hover readout, click to pick);
|
|
441
|
+
* - a cursor the app can drive from elsewhere, e.g. the canvas a profile was sampled on.
|
|
442
|
+
*/
|
|
443
|
+
/** The ranges behind the series. */
|
|
444
|
+
function Bands({ bands, xScale, area }) {
|
|
445
|
+
return /* @__PURE__ */ jsx("g", {
|
|
446
|
+
"data-bands": "",
|
|
447
|
+
children: bands.map((band) => {
|
|
448
|
+
const a = clamp(xScale.project(Math.min(band.from, band.to)), area.x0, area.x1);
|
|
449
|
+
const b = clamp(xScale.project(Math.max(band.from, band.to)), area.x0, area.x1);
|
|
450
|
+
const colour = toneColor(band.tone, "muted");
|
|
451
|
+
return /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("rect", {
|
|
452
|
+
x: a,
|
|
453
|
+
y: area.y1,
|
|
454
|
+
width: Math.max(1, b - a),
|
|
455
|
+
height: area.y0 - area.y1,
|
|
456
|
+
fill: colour,
|
|
457
|
+
opacity: .14
|
|
458
|
+
}), band.label && /* @__PURE__ */ jsx("text", {
|
|
459
|
+
x: (a + b) / 2,
|
|
460
|
+
y: area.y1 + 10,
|
|
461
|
+
textAnchor: "middle",
|
|
462
|
+
fontSize: 9,
|
|
463
|
+
fill: colour,
|
|
464
|
+
children: band.label
|
|
465
|
+
})] }, `${band.from}-${band.to}-${band.label ?? ""}`);
|
|
466
|
+
})
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
/** The ticks on the x axis. */
|
|
470
|
+
function Markers({ markers, xScale, area }) {
|
|
471
|
+
return /* @__PURE__ */ jsx("g", {
|
|
472
|
+
"data-markers": "",
|
|
473
|
+
children: markers.map((marker) => {
|
|
474
|
+
const x = xScale.project(marker.position);
|
|
475
|
+
if (x < area.x0 - .5 || x > area.x1 + .5) return null;
|
|
476
|
+
return /* @__PURE__ */ jsx("line", {
|
|
477
|
+
x1: x,
|
|
478
|
+
x2: x,
|
|
479
|
+
y1: area.y0,
|
|
480
|
+
y2: area.y0 - 6,
|
|
481
|
+
stroke: toneColor(marker.tone, "muted"),
|
|
482
|
+
strokeWidth: 1.5,
|
|
483
|
+
children: marker.label && /* @__PURE__ */ jsx("title", { children: marker.label })
|
|
484
|
+
}, `${marker.position}-${marker.label ?? ""}`);
|
|
485
|
+
})
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* The pointer layer over the plot area: a crosshair and a value readout while hovering, the
|
|
490
|
+
* external `cursor` otherwise, and clicks as picks.
|
|
491
|
+
*/
|
|
492
|
+
function PlotInteraction({ xScale, yScale, area, series, cursor, onHover, onPick, format = (value) => Math.abs(value) >= 100 ? value.toFixed(0) : value.toPrecision(3) }) {
|
|
493
|
+
const [hover, setHover] = useState(null);
|
|
494
|
+
const interactive = onHover !== void 0 || onPick !== void 0;
|
|
495
|
+
const xAt = (event) => {
|
|
496
|
+
const svg = event.currentTarget.ownerSVGElement;
|
|
497
|
+
const ctm = svg?.getScreenCTM();
|
|
498
|
+
let local = event.clientX;
|
|
499
|
+
if (svg && ctm) {
|
|
500
|
+
const point = svg.createSVGPoint();
|
|
501
|
+
point.x = event.clientX;
|
|
502
|
+
point.y = event.clientY;
|
|
503
|
+
local = point.matrixTransform(ctm.inverse()).x;
|
|
504
|
+
}
|
|
505
|
+
const [low, high] = xScale.domain;
|
|
506
|
+
return clamp(xScale.invert(local), Math.min(low, high), Math.max(low, high));
|
|
507
|
+
};
|
|
508
|
+
const shown = hover ?? cursor ?? null;
|
|
509
|
+
const x = shown === null ? null : xScale.project(shown);
|
|
510
|
+
const readout = hover === null ? [] : series.flatMap((entry) => {
|
|
511
|
+
const y = seriesValueAt(entry.points, hover);
|
|
512
|
+
return y === null ? [] : [{
|
|
513
|
+
name: entry.name,
|
|
514
|
+
colour: entry.colour,
|
|
515
|
+
y
|
|
516
|
+
}];
|
|
517
|
+
});
|
|
518
|
+
return /* @__PURE__ */ jsxs("g", {
|
|
519
|
+
"data-interaction": "",
|
|
520
|
+
children: [
|
|
521
|
+
x !== null && x >= area.x0 - .5 && x <= area.x1 + .5 && /* @__PURE__ */ jsx("line", {
|
|
522
|
+
"data-crosshair": "",
|
|
523
|
+
x1: x,
|
|
524
|
+
x2: x,
|
|
525
|
+
y1: area.y0,
|
|
526
|
+
y2: area.y1,
|
|
527
|
+
stroke: "currentColor",
|
|
528
|
+
strokeWidth: 1,
|
|
529
|
+
opacity: hover === null ? .6 : .9
|
|
530
|
+
}),
|
|
531
|
+
readout.map((entry) => /* @__PURE__ */ jsx("circle", {
|
|
532
|
+
cx: x ?? 0,
|
|
533
|
+
cy: yScale.project(entry.y),
|
|
534
|
+
r: 3,
|
|
535
|
+
fill: entry.colour,
|
|
536
|
+
stroke: "var(--surface)",
|
|
537
|
+
strokeWidth: 1
|
|
538
|
+
}, entry.name)),
|
|
539
|
+
hover !== null && x !== null && /* @__PURE__ */ jsx("text", {
|
|
540
|
+
"data-readout": "",
|
|
541
|
+
x: x > (area.x0 + area.x1) / 2 ? x - 6 : x + 6,
|
|
542
|
+
y: area.y1 + 10,
|
|
543
|
+
textAnchor: x > (area.x0 + area.x1) / 2 ? "end" : "start",
|
|
544
|
+
fontSize: 10,
|
|
545
|
+
fill: "currentColor",
|
|
546
|
+
children: [format(hover), ...readout.map((entry) => `${entry.name} ${format(entry.y)}`)].join(" · ")
|
|
547
|
+
}),
|
|
548
|
+
interactive && /* @__PURE__ */ jsx("rect", {
|
|
549
|
+
"data-plot-target": "",
|
|
550
|
+
x: area.x0,
|
|
551
|
+
y: area.y1,
|
|
552
|
+
width: area.x1 - area.x0,
|
|
553
|
+
height: area.y0 - area.y1,
|
|
554
|
+
fill: "transparent",
|
|
555
|
+
style: { cursor: onPick ? "pointer" : "crosshair" },
|
|
556
|
+
onPointerMove: (event) => {
|
|
557
|
+
const value = xAt(event);
|
|
558
|
+
setHover(value);
|
|
559
|
+
onHover?.(value);
|
|
560
|
+
},
|
|
561
|
+
onPointerLeave: () => {
|
|
562
|
+
setHover(null);
|
|
563
|
+
onHover?.(null);
|
|
564
|
+
},
|
|
565
|
+
onClick: (event) => onPick?.(xAt(event))
|
|
566
|
+
})
|
|
567
|
+
]
|
|
568
|
+
});
|
|
569
|
+
}
|
|
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
|
+
function clamp(value, low, high) {
|
|
596
|
+
return Math.min(high, Math.max(low, value));
|
|
597
|
+
}
|
|
598
|
+
//#endregion
|
|
384
599
|
//#region src/LineChart.tsx
|
|
385
600
|
/**
|
|
386
601
|
* A multi-series line chart over a shared x axis — training curves, ROC and PR curves.
|
|
387
602
|
*
|
|
388
603
|
* @remarks
|
|
389
604
|
* 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.
|
|
605
|
+
* told apart by the legend's names, never by colour alone. `bands` and `markers` mark x
|
|
606
|
+
* 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.
|
|
391
608
|
*/
|
|
392
|
-
function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDomain, underlay, footer, showLegend = true, variant = "panel", className }) {
|
|
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 }) {
|
|
393
610
|
const allX = series.flatMap((entry) => entry.points.map((point) => point.x));
|
|
394
611
|
const allY = series.flatMap((entry) => entry.points.map((point) => point.y));
|
|
395
|
-
const
|
|
612
|
+
const [fluidRef, fluidSize] = useFluidSize(height);
|
|
613
|
+
const size = variant === "fluid" ? fluidSize : void 0;
|
|
614
|
+
const plotArea = areaFor(variant, size);
|
|
396
615
|
const xScale = linearScale(xDomain ?? extent(allX), plotArea.x0, plotArea.x1);
|
|
397
616
|
const yScale = (logY ? logScale : linearScale)(yDomain ?? extent(allY), plotArea.y0, plotArea.y1);
|
|
398
617
|
const coloured = series.map((entry, index) => ({
|
|
@@ -407,6 +626,8 @@ function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDoma
|
|
|
407
626
|
label,
|
|
408
627
|
variant,
|
|
409
628
|
className,
|
|
629
|
+
size,
|
|
630
|
+
figureRef: variant === "fluid" ? fluidRef : void 0,
|
|
410
631
|
footer: /* @__PURE__ */ jsxs("div", {
|
|
411
632
|
className: "flex flex-col gap-1",
|
|
412
633
|
children: [showLegend && /* @__PURE__ */ jsx(Legend, { items: coloured.map((entry) => ({
|
|
@@ -414,19 +635,41 @@ function LineChart({ series, label, xLabel, yLabel, logY = false, xDomain, yDoma
|
|
|
414
635
|
colour: entry.colour
|
|
415
636
|
})) }), footer]
|
|
416
637
|
}),
|
|
417
|
-
children: [
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
638
|
+
children: [
|
|
639
|
+
bands && bands.length > 0 && /* @__PURE__ */ jsx(Bands, {
|
|
640
|
+
bands,
|
|
641
|
+
xScale,
|
|
642
|
+
area: plotArea
|
|
643
|
+
}),
|
|
644
|
+
underlay?.(xScale, yScale),
|
|
645
|
+
coloured.map((entry) => entry.points.length === 1 ? /* @__PURE__ */ jsx("circle", {
|
|
646
|
+
cx: xScale.project(entry.points[0]?.x ?? 0),
|
|
647
|
+
cy: yScale.project(entry.points[0]?.y ?? 0),
|
|
648
|
+
r: 2.5,
|
|
649
|
+
fill: entry.colour
|
|
650
|
+
}, entry.name) : /* @__PURE__ */ jsx("path", {
|
|
651
|
+
d: linePath(entry.points, xScale, yScale),
|
|
652
|
+
fill: "none",
|
|
653
|
+
stroke: entry.colour,
|
|
654
|
+
strokeWidth: 1.5,
|
|
655
|
+
strokeLinejoin: "round",
|
|
656
|
+
vectorEffect: "non-scaling-stroke"
|
|
657
|
+
}, entry.name)),
|
|
658
|
+
markers && markers.length > 0 && /* @__PURE__ */ jsx(Markers, {
|
|
659
|
+
markers,
|
|
660
|
+
xScale,
|
|
661
|
+
area: plotArea
|
|
662
|
+
}),
|
|
663
|
+
(onHover || onPick || cursor !== void 0) && /* @__PURE__ */ jsx(PlotInteraction, {
|
|
664
|
+
xScale,
|
|
665
|
+
yScale,
|
|
666
|
+
area: plotArea,
|
|
667
|
+
series: coloured,
|
|
668
|
+
cursor,
|
|
669
|
+
onHover,
|
|
670
|
+
onPick
|
|
671
|
+
})
|
|
672
|
+
]
|
|
430
673
|
});
|
|
431
674
|
}
|
|
432
675
|
//#endregion
|
|
@@ -607,12 +850,16 @@ function ScoreHistogram({ normal, defect, threshold, bins = 32, label, className
|
|
|
607
850
|
*
|
|
608
851
|
* @remarks
|
|
609
852
|
* 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.
|
|
853
|
+
* single profile is named by the chart's label. `bands`, `markers`, `cursor`, `onHover` and
|
|
854
|
+
* `onPick` work as on `LineChart`: a caliper's profile can follow the canvas, and a click
|
|
855
|
+
* can pick a position along it.
|
|
611
856
|
*/
|
|
612
|
-
function LineProfile({ series, edges = [], label, xLabel = "arc length (px)", yLabel = "value", xDomain, yDomain, footer, showLegend = true, variant = "wide", className }) {
|
|
857
|
+
function LineProfile({ series, edges = [], label, xLabel = "arc length (px)", yLabel = "value", xDomain, yDomain, footer, showLegend = true, variant = "wide", className, height = 200, bands, markers, cursor, onHover, onPick }) {
|
|
613
858
|
const allX = [...series.flatMap((entry) => entry.points.map((point) => point.x)), ...edges.map((edge) => edge.position)];
|
|
614
859
|
const allY = series.flatMap((entry) => entry.points.map((point) => point.y));
|
|
615
|
-
const
|
|
860
|
+
const [fluidRef, fluidSize] = useFluidSize(height);
|
|
861
|
+
const size = variant === "fluid" ? fluidSize : void 0;
|
|
862
|
+
const plotArea = areaFor(variant, size);
|
|
616
863
|
const xScale = linearScale(xDomain ?? extent(allX), plotArea.x0, plotArea.x1);
|
|
617
864
|
const yScale = linearScale(yDomain ?? extent(allY), plotArea.y0, plotArea.y1);
|
|
618
865
|
const coloured = series.map((entry, index) => ({
|
|
@@ -627,6 +874,8 @@ function LineProfile({ series, edges = [], label, xLabel = "arc length (px)", yL
|
|
|
627
874
|
label,
|
|
628
875
|
variant,
|
|
629
876
|
className,
|
|
877
|
+
size,
|
|
878
|
+
figureRef: variant === "fluid" ? fluidRef : void 0,
|
|
630
879
|
footer: /* @__PURE__ */ jsxs("div", {
|
|
631
880
|
className: "flex flex-col gap-1",
|
|
632
881
|
children: [showLegend && series.length > 1 && /* @__PURE__ */ jsx(Legend, { items: coloured.map((entry) => ({
|
|
@@ -634,41 +883,251 @@ function LineProfile({ series, edges = [], label, xLabel = "arc length (px)", yL
|
|
|
634
883
|
colour: entry.colour
|
|
635
884
|
})) }), footer]
|
|
636
885
|
}),
|
|
637
|
-
children: [
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
886
|
+
children: [
|
|
887
|
+
bands && bands.length > 0 && /* @__PURE__ */ jsx(Bands, {
|
|
888
|
+
bands,
|
|
889
|
+
xScale,
|
|
890
|
+
area: plotArea
|
|
891
|
+
}),
|
|
892
|
+
coloured.map((entry) => entry.points.length === 1 ? /* @__PURE__ */ jsx("circle", {
|
|
893
|
+
cx: xScale.project(entry.points[0]?.x ?? 0),
|
|
894
|
+
cy: yScale.project(entry.points[0]?.y ?? 0),
|
|
895
|
+
r: 2.5,
|
|
896
|
+
fill: entry.colour
|
|
897
|
+
}, entry.name) : /* @__PURE__ */ jsx("path", {
|
|
898
|
+
d: linePath(entry.points, xScale, yScale),
|
|
899
|
+
fill: "none",
|
|
900
|
+
stroke: entry.colour,
|
|
901
|
+
strokeWidth: 1.5,
|
|
902
|
+
strokeLinejoin: "round",
|
|
903
|
+
vectorEffect: "non-scaling-stroke"
|
|
904
|
+
}, entry.name)),
|
|
905
|
+
edges.map((edge) => {
|
|
906
|
+
const x = xScale.project(edge.position);
|
|
907
|
+
const colour = toneColor(edge.tone, "signal");
|
|
908
|
+
return /* @__PURE__ */ jsxs("g", { children: [/* @__PURE__ */ jsx("line", {
|
|
909
|
+
x1: x,
|
|
910
|
+
x2: x,
|
|
911
|
+
y1: plotArea.y0,
|
|
912
|
+
y2: plotArea.y1,
|
|
913
|
+
stroke: colour,
|
|
914
|
+
strokeWidth: 1.25,
|
|
915
|
+
strokeDasharray: "3 2"
|
|
916
|
+
}), edge.label && /* @__PURE__ */ jsx("text", {
|
|
917
|
+
x,
|
|
918
|
+
y: plotArea.y1 - 3,
|
|
919
|
+
textAnchor: "middle",
|
|
920
|
+
fontSize: 9,
|
|
921
|
+
fill: colour,
|
|
922
|
+
children: edge.label
|
|
923
|
+
})] }, `${edge.position}-${edge.label ?? ""}-${edge.tone ?? ""}`);
|
|
924
|
+
}),
|
|
925
|
+
markers && markers.length > 0 && /* @__PURE__ */ jsx(Markers, {
|
|
926
|
+
markers,
|
|
927
|
+
xScale,
|
|
928
|
+
area: plotArea
|
|
929
|
+
}),
|
|
930
|
+
(onHover || onPick || cursor !== void 0) && /* @__PURE__ */ jsx(PlotInteraction, {
|
|
931
|
+
xScale,
|
|
932
|
+
yScale,
|
|
933
|
+
area: plotArea,
|
|
934
|
+
series: coloured,
|
|
935
|
+
cursor,
|
|
936
|
+
onHover,
|
|
937
|
+
onPick
|
|
938
|
+
})
|
|
939
|
+
]
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
//#endregion
|
|
943
|
+
//#region src/colormap.ts
|
|
944
|
+
const ANCHORS = {
|
|
945
|
+
viridis: [
|
|
946
|
+
[
|
|
947
|
+
68,
|
|
948
|
+
1,
|
|
949
|
+
84
|
|
950
|
+
],
|
|
951
|
+
[
|
|
952
|
+
71,
|
|
953
|
+
44,
|
|
954
|
+
122
|
|
955
|
+
],
|
|
956
|
+
[
|
|
957
|
+
59,
|
|
958
|
+
81,
|
|
959
|
+
139
|
|
960
|
+
],
|
|
961
|
+
[
|
|
962
|
+
44,
|
|
963
|
+
113,
|
|
964
|
+
142
|
|
965
|
+
],
|
|
966
|
+
[
|
|
967
|
+
33,
|
|
968
|
+
144,
|
|
969
|
+
141
|
|
970
|
+
],
|
|
971
|
+
[
|
|
972
|
+
39,
|
|
973
|
+
173,
|
|
974
|
+
129
|
|
975
|
+
],
|
|
976
|
+
[
|
|
977
|
+
92,
|
|
978
|
+
200,
|
|
979
|
+
99
|
|
980
|
+
],
|
|
981
|
+
[
|
|
982
|
+
170,
|
|
983
|
+
220,
|
|
984
|
+
50
|
|
985
|
+
],
|
|
986
|
+
[
|
|
987
|
+
253,
|
|
988
|
+
231,
|
|
989
|
+
37
|
|
990
|
+
]
|
|
991
|
+
],
|
|
992
|
+
cividis: [
|
|
993
|
+
[
|
|
994
|
+
0,
|
|
995
|
+
34,
|
|
996
|
+
78
|
|
997
|
+
],
|
|
998
|
+
[
|
|
999
|
+
18,
|
|
1000
|
+
53,
|
|
1001
|
+
112
|
|
1002
|
+
],
|
|
1003
|
+
[
|
|
1004
|
+
59,
|
|
1005
|
+
73,
|
|
1006
|
+
108
|
|
1007
|
+
],
|
|
1008
|
+
[
|
|
1009
|
+
87,
|
|
1010
|
+
93,
|
|
1011
|
+
109
|
|
1012
|
+
],
|
|
1013
|
+
[
|
|
1014
|
+
112,
|
|
1015
|
+
113,
|
|
1016
|
+
115
|
|
1017
|
+
],
|
|
1018
|
+
[
|
|
1019
|
+
138,
|
|
1020
|
+
135,
|
|
1021
|
+
121
|
|
1022
|
+
],
|
|
1023
|
+
[
|
|
1024
|
+
165,
|
|
1025
|
+
157,
|
|
1026
|
+
116
|
|
1027
|
+
],
|
|
1028
|
+
[
|
|
1029
|
+
196,
|
|
1030
|
+
182,
|
|
1031
|
+
101
|
|
1032
|
+
],
|
|
1033
|
+
[
|
|
1034
|
+
254,
|
|
1035
|
+
232,
|
|
1036
|
+
56
|
|
1037
|
+
]
|
|
1038
|
+
]
|
|
1039
|
+
};
|
|
1040
|
+
/** The names of the shipped maps. */
|
|
1041
|
+
const COLORMAPS = ["viridis", "cividis"];
|
|
1042
|
+
/**
|
|
1043
|
+
* The colour of `t` on a map.
|
|
1044
|
+
*
|
|
1045
|
+
* @param name - The map.
|
|
1046
|
+
* @param t - Position in `[0, 1]`; clamped. A non-finite `t` gives the map's low end.
|
|
1047
|
+
* @returns A CSS `rgb(r g b)` colour.
|
|
1048
|
+
*/
|
|
1049
|
+
function colormap(name, t) {
|
|
1050
|
+
const [r, g, b] = colormapRgb(name, t);
|
|
1051
|
+
return `rgb(${r} ${g} ${b})`;
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* The colour of `t` on a map, as integer channels.
|
|
1055
|
+
*
|
|
1056
|
+
* @param name - The map.
|
|
1057
|
+
* @param t - Position in `[0, 1]`; clamped.
|
|
1058
|
+
* @returns `[r, g, b]`, each 0–255.
|
|
1059
|
+
*/
|
|
1060
|
+
function colormapRgb(name, t) {
|
|
1061
|
+
const anchors = ANCHORS[name];
|
|
1062
|
+
const scaled = (Number.isFinite(t) ? Math.min(1, Math.max(0, t)) : 0) * (anchors.length - 1);
|
|
1063
|
+
const i = Math.min(anchors.length - 2, Math.floor(scaled));
|
|
1064
|
+
const f = scaled - i;
|
|
1065
|
+
const a = anchors[i];
|
|
1066
|
+
const b = anchors[i + 1];
|
|
1067
|
+
return [
|
|
1068
|
+
Math.round(a[0] + f * (b[0] - a[0])),
|
|
1069
|
+
Math.round(a[1] + f * (b[1] - a[1])),
|
|
1070
|
+
Math.round(a[2] + f * (b[2] - a[2]))
|
|
1071
|
+
];
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* A value's colour on a map, over a domain.
|
|
1075
|
+
*
|
|
1076
|
+
* @param name - The map.
|
|
1077
|
+
* @param value - The value.
|
|
1078
|
+
* @param domain - `[low, high]`: low maps to the map's start. A reversed domain reverses the map.
|
|
1079
|
+
* @returns A CSS colour.
|
|
1080
|
+
*/
|
|
1081
|
+
function colormapValue(name, value, domain) {
|
|
1082
|
+
const [low, high] = domain;
|
|
1083
|
+
const span = high - low;
|
|
1084
|
+
return colormap(name, span === 0 ? 0 : (value - low) / span);
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* A CSS `linear-gradient` of a map, for a legend bar.
|
|
1088
|
+
*
|
|
1089
|
+
* @param name - The map.
|
|
1090
|
+
* @param direction - The gradient's direction. Defaults to `to right`.
|
|
1091
|
+
* @returns A CSS `background-image` value.
|
|
1092
|
+
*/
|
|
1093
|
+
function colormapGradient(name, direction = "to right") {
|
|
1094
|
+
const anchors = ANCHORS[name];
|
|
1095
|
+
return `linear-gradient(${direction}, ${anchors.map(([r, g, b], i) => `rgb(${r} ${g} ${b}) ${(i / (anchors.length - 1) * 100).toFixed(1)}%`).join(", ")})`;
|
|
1096
|
+
}
|
|
1097
|
+
//#endregion
|
|
1098
|
+
//#region src/ColormapLegend.tsx
|
|
1099
|
+
/**
|
|
1100
|
+
* The scale of a colour map, shown beside the map: visual-language §4 requires every chart or
|
|
1101
|
+
* map to show its scale, with units.
|
|
1102
|
+
*/
|
|
1103
|
+
/**
|
|
1104
|
+
* A gradient bar with its end values and unit, read as one line: "Edge error 0 [bar] 2.5 px".
|
|
1105
|
+
* The bar is decorative; the label and the end values carry the meaning as text.
|
|
1106
|
+
*/
|
|
1107
|
+
function ColormapLegend({ map, domain, unit, label, format = (value) => String(Number(value.toPrecision(3))), className }) {
|
|
1108
|
+
const end = (value) => unit ? `${format(value)} ${unit}` : format(value);
|
|
1109
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1110
|
+
className: cn("flex items-center gap-2 text-xs text-fg-muted", className),
|
|
1111
|
+
"data-colormap": map,
|
|
1112
|
+
children: [
|
|
1113
|
+
/* @__PURE__ */ jsx("span", { children: label }),
|
|
1114
|
+
/* @__PURE__ */ jsx("span", {
|
|
1115
|
+
className: "font-mono tabular-nums",
|
|
1116
|
+
children: end(domain[0])
|
|
1117
|
+
}),
|
|
1118
|
+
/* @__PURE__ */ jsx("span", {
|
|
1119
|
+
"aria-hidden": true,
|
|
1120
|
+
className: "h-2 w-24 rounded-full",
|
|
1121
|
+
style: { backgroundImage: colormapGradient(map) }
|
|
1122
|
+
}),
|
|
1123
|
+
/* @__PURE__ */ jsx("span", {
|
|
1124
|
+
className: "font-mono tabular-nums",
|
|
1125
|
+
children: end(domain[1])
|
|
1126
|
+
})
|
|
1127
|
+
]
|
|
669
1128
|
});
|
|
670
1129
|
}
|
|
671
1130
|
//#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 };
|
|
1131
|
+
export { Bands, COLORMAPS, ColormapLegend, DEFECT_COLOUR, FLUID_DEFAULT_HEIGHT, Frame, 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 };
|
|
673
1132
|
|
|
674
1133
|
//# sourceMappingURL=index.js.map
|