@vitavision/charts 0.8.4 → 0.8.5
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/dist/index.d.ts +23 -2
- package/dist/index.js +145 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/Frame.tsx +8 -1
- package/src/Histogram.tsx +36 -3
- package/src/ThresholdRule.tsx +141 -2
- package/src/interaction.tsx +6 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitavision/charts",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.5",
|
|
4
4
|
"description": "Small SVG charts on @vitavision/ui: histogram, line, bar, line profile, scales, colour maps, hover and pick.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"react-dom": "^19.2.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@vitavision/ui": "^0.14.
|
|
49
|
+
"@vitavision/ui": "^0.14.3"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@storybook/react-vite": "10.6.0",
|
package/src/Frame.tsx
CHANGED
|
@@ -123,6 +123,12 @@ export interface FrameProps {
|
|
|
123
123
|
className?: string | undefined;
|
|
124
124
|
/** For `fluid`: the measured size (see {@link useFluidSize}). */
|
|
125
125
|
size?: { width: number; height: number } | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* The plot holds focusable controls (a draggable threshold). The SVG is then a
|
|
128
|
+
* `role="group"` named by `label` instead of an `img`, whose children assistive
|
|
129
|
+
* technology would otherwise hide.
|
|
130
|
+
*/
|
|
131
|
+
interactive?: boolean | undefined;
|
|
126
132
|
/** A ref to the outer `<figure>`, e.g. {@link useFluidSize}'s measuring ref. */
|
|
127
133
|
figureRef?: Ref<HTMLElement> | undefined;
|
|
128
134
|
}
|
|
@@ -149,6 +155,7 @@ export function Frame({
|
|
|
149
155
|
className,
|
|
150
156
|
size,
|
|
151
157
|
figureRef,
|
|
158
|
+
interactive = false,
|
|
152
159
|
}: FrameProps) {
|
|
153
160
|
const horizontal = xScale.ticks(xTicks);
|
|
154
161
|
const vertical = yScale.ticks(yTicks);
|
|
@@ -157,7 +164,7 @@ export function Frame({
|
|
|
157
164
|
|
|
158
165
|
return (
|
|
159
166
|
<figure ref={figureRef} className={cn("flex flex-col gap-1 text-fg-muted", className)}>
|
|
160
|
-
<svg role="img" aria-label={label} viewBox={`0 0 ${PLOT.width} ${PLOT.height}`} className="w-full">
|
|
167
|
+
<svg role={interactive ? "group" : "img"} aria-label={label} viewBox={`0 0 ${PLOT.width} ${PLOT.height}`} className="w-full">
|
|
161
168
|
{vertical.map((tick) => {
|
|
162
169
|
const y = yScale.project(tick.value);
|
|
163
170
|
return (
|
package/src/Histogram.tsx
CHANGED
|
@@ -12,7 +12,7 @@ import { barsPath, binAt, binValues, cleanCounts, tallest } from "./bars";
|
|
|
12
12
|
import { FLUID_DEFAULT_HEIGHT, Frame, areaFor, seriesColour, useFluidSize, type Variant } from "./Frame";
|
|
13
13
|
import { Bands, Markers, PlotInteraction, type InteractionProps } from "./interaction";
|
|
14
14
|
import { linearScale, logScale } from "./scale";
|
|
15
|
-
import { ThresholdRule } from "./ThresholdRule";
|
|
15
|
+
import { DraggableThreshold, ThresholdRule } from "./ThresholdRule";
|
|
16
16
|
|
|
17
17
|
/** Props every {@link Histogram} takes, whichever way its data is given. */
|
|
18
18
|
export interface HistogramOptions extends InteractionProps {
|
|
@@ -29,6 +29,21 @@ export interface HistogramOptions extends InteractionProps {
|
|
|
29
29
|
* draws its own). Omitted or non-finite: none. For other x positions use `markers`.
|
|
30
30
|
*/
|
|
31
31
|
threshold?: number | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Makes `threshold` draggable: called with the new x, in data units and clamped to the
|
|
34
|
+
* domain, while the rule is dragged or stepped. The chart does not keep the value — move
|
|
35
|
+
* `threshold` to follow it. Needs a `threshold`. The rule becomes a focusable slider
|
|
36
|
+
* (Arrow keys step one bin, Page keys ten, Home and End go to the ends), and the SVG a
|
|
37
|
+
* `group` rather than an `img`.
|
|
38
|
+
*/
|
|
39
|
+
onThresholdChange?: ((x: number) => void) | undefined;
|
|
40
|
+
/** The threshold slider's accessible name. Default "Threshold". */
|
|
41
|
+
thresholdLabel?: string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Freezes a draggable threshold. A string is the reason, shown as the rule's tooltip
|
|
44
|
+
* (`"Locked by the saved model"`).
|
|
45
|
+
*/
|
|
46
|
+
thresholdDisabled?: boolean | string | undefined;
|
|
32
47
|
/** Counts span decades in a long-tailed distribution; a log axis keeps the tail visible. */
|
|
33
48
|
logY?: boolean | undefined;
|
|
34
49
|
/** Where the chart is going (see {@link Variant}). Default `"panel"`. */
|
|
@@ -75,7 +90,7 @@ export type HistogramProps = HistogramOptions & (HistogramValues | HistogramCoun
|
|
|
75
90
|
* @remarks
|
|
76
91
|
* An SVG `role="img"` named by `label`. `threshold` draws a dashed decision rule, `markers`
|
|
77
92
|
* tick the x axis, `bands` shade x ranges, `onHover` adds a crosshair and the x under it,
|
|
78
|
-
* `onPick` reports the x of a click, and `cursor` draws the bin containing an x chosen
|
|
93
|
+
* `onPick` reports the x of a click, `onThresholdChange` makes the threshold a draggable slider, and `cursor` draws the bin containing an x chosen
|
|
79
94
|
* elsewhere in the selection colour (`--signal`) — no highlight when it lies outside the
|
|
80
95
|
* domain. Empty data draws an empty frame. For two classes sharing a threshold use
|
|
81
96
|
* {@link ScoreHistogram}.
|
|
@@ -92,6 +107,9 @@ export function Histogram(props: HistogramProps) {
|
|
|
92
107
|
footer,
|
|
93
108
|
className,
|
|
94
109
|
threshold,
|
|
110
|
+
onThresholdChange,
|
|
111
|
+
thresholdLabel = "Threshold",
|
|
112
|
+
thresholdDisabled = false,
|
|
95
113
|
bands,
|
|
96
114
|
markers,
|
|
97
115
|
cursor,
|
|
@@ -113,6 +131,7 @@ export function Histogram(props: HistogramProps) {
|
|
|
113
131
|
|
|
114
132
|
const highlighted = binAt(counts.length, domain, cursor);
|
|
115
133
|
const hasHighlight = highlighted !== null && (counts[highlighted] ?? 0) > 0;
|
|
134
|
+
const draggable = onThresholdChange !== undefined && threshold !== undefined && Number.isFinite(threshold);
|
|
116
135
|
const xTitle = [xLabel, unit ? `(${unit})` : undefined].filter(Boolean).join(" ") || undefined;
|
|
117
136
|
|
|
118
137
|
return (
|
|
@@ -127,6 +146,7 @@ export function Histogram(props: HistogramProps) {
|
|
|
127
146
|
size={size}
|
|
128
147
|
figureRef={variant === "fluid" ? fluidRef : undefined}
|
|
129
148
|
footer={footer}
|
|
149
|
+
interactive={draggable}
|
|
130
150
|
>
|
|
131
151
|
{bands && bands.length > 0 && <Bands bands={bands} xScale={xScale} area={plotArea} />}
|
|
132
152
|
<path
|
|
@@ -138,11 +158,24 @@ export function Histogram(props: HistogramProps) {
|
|
|
138
158
|
{hasHighlight && (
|
|
139
159
|
<path data-cursor-bin={highlighted} d={barsPath(counts, yScale, plotArea, { only: highlighted })} fill="var(--signal)" />
|
|
140
160
|
)}
|
|
141
|
-
<ThresholdRule value={threshold} xScale={xScale} area={plotArea} />
|
|
161
|
+
{!draggable && <ThresholdRule value={threshold} xScale={xScale} area={plotArea} />}
|
|
142
162
|
{markers && markers.length > 0 && <Markers markers={markers} xScale={xScale} area={plotArea} />}
|
|
143
163
|
{(onHover || onPick) && (
|
|
144
164
|
<PlotInteraction xScale={xScale} yScale={yScale} area={plotArea} series={[]} onHover={onHover} onPick={onPick} />
|
|
145
165
|
)}
|
|
166
|
+
{/* After the pointer layer: the grab area has to sit above it to be reached. */}
|
|
167
|
+
{draggable && (
|
|
168
|
+
<DraggableThreshold
|
|
169
|
+
value={threshold}
|
|
170
|
+
xScale={xScale}
|
|
171
|
+
area={plotArea}
|
|
172
|
+
step={(domain[1] - domain[0]) / Math.max(1, counts.length)}
|
|
173
|
+
onChange={onThresholdChange}
|
|
174
|
+
label={thresholdLabel}
|
|
175
|
+
unit={unit}
|
|
176
|
+
disabled={thresholdDisabled}
|
|
177
|
+
/>
|
|
178
|
+
)}
|
|
146
179
|
</Frame>
|
|
147
180
|
);
|
|
148
181
|
}
|
package/src/ThresholdRule.tsx
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The decision threshold of a histogram: a dashed rule across the plot at one x
|
|
2
|
+
* The decision threshold of a histogram: a dashed rule across the plot at one x, and — when
|
|
3
|
+
* the caller can take a new value — a handle on it that a pointer drags and the arrow keys
|
|
4
|
+
* step.
|
|
3
5
|
*/
|
|
4
6
|
|
|
5
|
-
import
|
|
7
|
+
import { useRef } from "react";
|
|
8
|
+
|
|
9
|
+
import { defaultFormat, type PlotArea } from "./interaction";
|
|
6
10
|
import type { Scale } from "./scale";
|
|
7
11
|
|
|
8
12
|
/** A dashed vertical rule at `value`; nothing when it is missing or not finite. */
|
|
@@ -11,3 +15,138 @@ export function ThresholdRule({ value, xScale, area }: { value: number | undefin
|
|
|
11
15
|
const x = xScale.project(value);
|
|
12
16
|
return <line data-threshold="" x1={x} x2={x} y1={area.y0} y2={area.y1} stroke="currentColor" strokeWidth={1.25} strokeDasharray="3 2" />;
|
|
13
17
|
}
|
|
18
|
+
|
|
19
|
+
/** How wide the grab area around the rule is, in viewBox units. */
|
|
20
|
+
const GRAB = 12;
|
|
21
|
+
|
|
22
|
+
/** Props of {@link DraggableThreshold}. */
|
|
23
|
+
export interface DraggableThresholdProps {
|
|
24
|
+
/** Where the rule is, in x domain units. */
|
|
25
|
+
value: number | undefined;
|
|
26
|
+
xScale: Scale;
|
|
27
|
+
area: PlotArea;
|
|
28
|
+
/** One arrow-key step, in x domain units (a bin's width). */
|
|
29
|
+
step: number;
|
|
30
|
+
/** Called with the new x while dragging or stepping, clamped to the domain. */
|
|
31
|
+
onChange: (x: number) => void;
|
|
32
|
+
/** The slider's accessible name. */
|
|
33
|
+
label: string;
|
|
34
|
+
/** Appended to the value text, e.g. `DN`. */
|
|
35
|
+
unit?: string | undefined;
|
|
36
|
+
/** Not draggable. A string is the reason, shown as the handle's tooltip. */
|
|
37
|
+
disabled?: boolean | string | undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The threshold as a slider: the dashed rule plus a wider invisible grab area and a small
|
|
42
|
+
* handle at the top. Pointer capture keeps a drag going outside the plot.
|
|
43
|
+
*/
|
|
44
|
+
export function DraggableThreshold({ value, xScale, area, step, onChange, label, unit, disabled = false }: DraggableThresholdProps) {
|
|
45
|
+
const dragging = useRef(false);
|
|
46
|
+
if (value === undefined || !Number.isFinite(value)) return null;
|
|
47
|
+
|
|
48
|
+
const [low, high] = [Math.min(...xScale.domain), Math.max(...xScale.domain)];
|
|
49
|
+
const off = disabled !== false;
|
|
50
|
+
const reason = typeof disabled === "string" ? disabled : undefined;
|
|
51
|
+
const x = xScale.project(value);
|
|
52
|
+
|
|
53
|
+
const emit = (next: number) => onChange(Math.min(high, Math.max(low, next)));
|
|
54
|
+
const xAt = (event: { clientX: number; clientY: number; currentTarget: SVGGElement }): number => {
|
|
55
|
+
const svg = event.currentTarget.ownerSVGElement;
|
|
56
|
+
const ctm = svg?.getScreenCTM();
|
|
57
|
+
if (!svg || !ctm) return value;
|
|
58
|
+
const point = svg.createSVGPoint();
|
|
59
|
+
point.x = event.clientX;
|
|
60
|
+
point.y = event.clientY;
|
|
61
|
+
return xScale.invert(point.matrixTransform(ctm.inverse()).x);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const keyStep = (key: string): number | null => {
|
|
65
|
+
switch (key) {
|
|
66
|
+
case "ArrowRight":
|
|
67
|
+
case "ArrowUp":
|
|
68
|
+
return value + step;
|
|
69
|
+
case "ArrowLeft":
|
|
70
|
+
case "ArrowDown":
|
|
71
|
+
return value - step;
|
|
72
|
+
case "PageUp":
|
|
73
|
+
return value + 10 * step;
|
|
74
|
+
case "PageDown":
|
|
75
|
+
return value - 10 * step;
|
|
76
|
+
case "Home":
|
|
77
|
+
return low;
|
|
78
|
+
case "End":
|
|
79
|
+
return high;
|
|
80
|
+
default:
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<g
|
|
87
|
+
data-threshold-handle=""
|
|
88
|
+
data-disabled={off ? "" : undefined}
|
|
89
|
+
role="slider"
|
|
90
|
+
tabIndex={off ? -1 : 0}
|
|
91
|
+
aria-label={label}
|
|
92
|
+
aria-orientation="horizontal"
|
|
93
|
+
aria-valuemin={low}
|
|
94
|
+
aria-valuemax={high}
|
|
95
|
+
aria-valuenow={value}
|
|
96
|
+
aria-valuetext={`${defaultFormat(value)}${unit ? ` ${unit}` : ""}`}
|
|
97
|
+
aria-disabled={off || undefined}
|
|
98
|
+
className="group text-fg outline-none"
|
|
99
|
+
style={{ cursor: off ? "not-allowed" : "col-resize", touchAction: "none" }}
|
|
100
|
+
onKeyDown={(event) => {
|
|
101
|
+
if (off) return;
|
|
102
|
+
const next = keyStep(event.key);
|
|
103
|
+
if (next === null) return;
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
emit(next);
|
|
106
|
+
}}
|
|
107
|
+
onPointerDown={(event) => {
|
|
108
|
+
if (off) return;
|
|
109
|
+
dragging.current = true;
|
|
110
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
111
|
+
event.currentTarget.focus();
|
|
112
|
+
emit(xAt(event));
|
|
113
|
+
}}
|
|
114
|
+
onPointerMove={(event) => {
|
|
115
|
+
if (dragging.current) emit(xAt(event));
|
|
116
|
+
}}
|
|
117
|
+
onPointerUp={(event) => {
|
|
118
|
+
dragging.current = false;
|
|
119
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
|
|
120
|
+
}}
|
|
121
|
+
onPointerCancel={() => {
|
|
122
|
+
dragging.current = false;
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
{reason !== undefined && <title>{reason}</title>}
|
|
126
|
+
<rect x={x - GRAB / 2} y={area.y1} width={GRAB} height={area.y0 - area.y1} fill="transparent" />
|
|
127
|
+
<line
|
|
128
|
+
data-threshold=""
|
|
129
|
+
x1={x}
|
|
130
|
+
x2={x}
|
|
131
|
+
y1={area.y0}
|
|
132
|
+
y2={area.y1}
|
|
133
|
+
stroke="currentColor"
|
|
134
|
+
strokeWidth={1.25}
|
|
135
|
+
strokeDasharray="3 2"
|
|
136
|
+
opacity={off ? 0.6 : 1}
|
|
137
|
+
/>
|
|
138
|
+
<path data-handle="" d={`M${x - 4} ${area.y1 - 1} h8 l-4 6 z`} fill="currentColor" opacity={off ? 0.6 : 1} />
|
|
139
|
+
<rect
|
|
140
|
+
x={x - GRAB / 2}
|
|
141
|
+
y={area.y1 - 2}
|
|
142
|
+
width={GRAB}
|
|
143
|
+
height={area.y0 - area.y1 + 2}
|
|
144
|
+
rx={2}
|
|
145
|
+
fill="none"
|
|
146
|
+
stroke="var(--signal)"
|
|
147
|
+
strokeWidth={2}
|
|
148
|
+
className="hidden group-focus-visible:block"
|
|
149
|
+
/>
|
|
150
|
+
</g>
|
|
151
|
+
);
|
|
152
|
+
}
|
package/src/interaction.tsx
CHANGED
|
@@ -123,6 +123,11 @@ export function Markers({ markers, xScale, area }: { markers: readonly Marker[];
|
|
|
123
123
|
);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
/** The default readout format: whole numbers from 100 up, three significant digits below. */
|
|
127
|
+
export function defaultFormat(value: number): string {
|
|
128
|
+
return Math.abs(value) >= 100 ? value.toFixed(0) : value.toPrecision(3);
|
|
129
|
+
}
|
|
130
|
+
|
|
126
131
|
/**
|
|
127
132
|
* The pointer layer over the plot area: a crosshair and a value readout while hovering, the
|
|
128
133
|
* external `cursor` otherwise, and clicks as picks.
|
|
@@ -135,7 +140,7 @@ export function PlotInteraction({
|
|
|
135
140
|
cursor,
|
|
136
141
|
onHover,
|
|
137
142
|
onPick,
|
|
138
|
-
format =
|
|
143
|
+
format = defaultFormat,
|
|
139
144
|
xFormat,
|
|
140
145
|
}: {
|
|
141
146
|
xScale: Scale;
|