@vitavision/charts 0.6.4 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +1 -1
- 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/src/LineProfile.tsx
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
|
|
12
12
|
import type { ReactNode } from "react";
|
|
13
13
|
|
|
14
|
-
import { Frame, Legend, areaFor, seriesColour } from "./Frame";
|
|
14
|
+
import { FLUID_DEFAULT_HEIGHT, Frame, Legend, areaFor, seriesColour, useFluidSize } from "./Frame";
|
|
15
|
+
import { Bands, Markers, PlotInteraction, type InteractionProps } from "./interaction";
|
|
15
16
|
import type { Variant } from "./Frame";
|
|
16
17
|
import { extent, linePath, linearScale } from "./scale";
|
|
17
18
|
import { toneColor, type MeasureTone } from "@vitavision/ui";
|
|
@@ -37,7 +38,7 @@ export interface EdgeMark {
|
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
/** Props for {@link LineProfile}. */
|
|
40
|
-
export interface LineProfileProps {
|
|
41
|
+
export interface LineProfileProps extends InteractionProps {
|
|
41
42
|
/** The profiles, drawn in order. */
|
|
42
43
|
series: ProfileSeries[];
|
|
43
44
|
/** Detected edge positions, drawn as vertical rules over the series. */
|
|
@@ -60,6 +61,8 @@ export interface LineProfileProps {
|
|
|
60
61
|
variant?: Variant;
|
|
61
62
|
/** Extra classes for the outer `<figure>`, merged with `cn`. */
|
|
62
63
|
className?: string | undefined;
|
|
64
|
+
/** For `variant="fluid"`: the chart's height in CSS pixels. Default 200. */
|
|
65
|
+
height?: number | undefined;
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
/**
|
|
@@ -68,7 +71,9 @@ export interface LineProfileProps {
|
|
|
68
71
|
*
|
|
69
72
|
* @remarks
|
|
70
73
|
* An SVG `role="img"` named by `label`. The legend appears only for two or more series — a
|
|
71
|
-
* single profile is named by the chart's label.
|
|
74
|
+
* single profile is named by the chart's label. `bands`, `markers`, `cursor`, `onHover` and
|
|
75
|
+
* `onPick` work as on `LineChart`: a caliper's profile can follow the canvas, and a click
|
|
76
|
+
* can pick a position along it.
|
|
72
77
|
*/
|
|
73
78
|
export function LineProfile({
|
|
74
79
|
series,
|
|
@@ -82,10 +87,18 @@ export function LineProfile({
|
|
|
82
87
|
showLegend = true,
|
|
83
88
|
variant = "wide",
|
|
84
89
|
className,
|
|
90
|
+
height = FLUID_DEFAULT_HEIGHT,
|
|
91
|
+
bands,
|
|
92
|
+
markers,
|
|
93
|
+
cursor,
|
|
94
|
+
onHover,
|
|
95
|
+
onPick,
|
|
85
96
|
}: LineProfileProps) {
|
|
86
97
|
const allX = [...series.flatMap((entry) => entry.points.map((point) => point.x)), ...edges.map((edge) => edge.position)];
|
|
87
98
|
const allY = series.flatMap((entry) => entry.points.map((point) => point.y));
|
|
88
|
-
const
|
|
99
|
+
const [fluidRef, fluidSize] = useFluidSize(height);
|
|
100
|
+
const size = variant === "fluid" ? fluidSize : undefined;
|
|
101
|
+
const plotArea = areaFor(variant, size);
|
|
89
102
|
|
|
90
103
|
const xScale = linearScale(xDomain ?? extent(allX), plotArea.x0, plotArea.x1);
|
|
91
104
|
const yScale = linearScale(yDomain ?? extent(allY), plotArea.y0, plotArea.y1);
|
|
@@ -104,6 +117,8 @@ export function LineProfile({
|
|
|
104
117
|
label={label}
|
|
105
118
|
variant={variant}
|
|
106
119
|
className={className}
|
|
120
|
+
size={size}
|
|
121
|
+
figureRef={variant === "fluid" ? fluidRef : undefined}
|
|
107
122
|
footer={
|
|
108
123
|
<div className="flex flex-col gap-1">
|
|
109
124
|
{showLegend && series.length > 1 && (
|
|
@@ -115,6 +130,7 @@ export function LineProfile({
|
|
|
115
130
|
</div>
|
|
116
131
|
}
|
|
117
132
|
>
|
|
133
|
+
{bands && bands.length > 0 && <Bands bands={bands} xScale={xScale} area={plotArea} />}
|
|
118
134
|
{coloured.map((entry) =>
|
|
119
135
|
entry.points.length === 1 ? (
|
|
120
136
|
<circle
|
|
@@ -159,6 +175,18 @@ export function LineProfile({
|
|
|
159
175
|
</g>
|
|
160
176
|
);
|
|
161
177
|
})}
|
|
178
|
+
{markers && markers.length > 0 && <Markers markers={markers} xScale={xScale} area={plotArea} />}
|
|
179
|
+
{(onHover || onPick || cursor !== undefined) && (
|
|
180
|
+
<PlotInteraction
|
|
181
|
+
xScale={xScale}
|
|
182
|
+
yScale={yScale}
|
|
183
|
+
area={plotArea}
|
|
184
|
+
series={coloured}
|
|
185
|
+
cursor={cursor}
|
|
186
|
+
onHover={onHover}
|
|
187
|
+
onPick={onPick}
|
|
188
|
+
/>
|
|
189
|
+
)}
|
|
162
190
|
</Frame>
|
|
163
191
|
);
|
|
164
192
|
}
|
package/src/colormap.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sequential colour maps for a magnitude (visual-language §4): a residual's size, a per-cell
|
|
3
|
+
* error, a confidence, a validity fraction.
|
|
4
|
+
*
|
|
5
|
+
* Only perceptually uniform maps ship, so equal steps in the data look like equal steps.
|
|
6
|
+
* - `viridis` is the default on the chrome.
|
|
7
|
+
* - `cividis` is for when the map alone carries the finding and the reader's colour vision is
|
|
8
|
+
* unknown.
|
|
9
|
+
*
|
|
10
|
+
* A continuous quality is a magnitude even when low values are "good": stepping it onto verdict
|
|
11
|
+
* colours (green / amber / red) claims a threshold judgement nobody made.
|
|
12
|
+
*
|
|
13
|
+
* The anchors are matplotlib's maps sampled at nine evenly spaced points. Linear interpolation
|
|
14
|
+
* between them stays within a few units of the full 256-entry tables.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** The shipped maps. */
|
|
18
|
+
export type ColormapName = "viridis" | "cividis";
|
|
19
|
+
|
|
20
|
+
type Rgb = readonly [number, number, number];
|
|
21
|
+
|
|
22
|
+
const ANCHORS: Record<ColormapName, readonly Rgb[]> = {
|
|
23
|
+
viridis: [
|
|
24
|
+
[68, 1, 84],
|
|
25
|
+
[71, 44, 122],
|
|
26
|
+
[59, 81, 139],
|
|
27
|
+
[44, 113, 142],
|
|
28
|
+
[33, 144, 141],
|
|
29
|
+
[39, 173, 129],
|
|
30
|
+
[92, 200, 99],
|
|
31
|
+
[170, 220, 50],
|
|
32
|
+
[253, 231, 37],
|
|
33
|
+
],
|
|
34
|
+
cividis: [
|
|
35
|
+
[0, 34, 78],
|
|
36
|
+
[18, 53, 112],
|
|
37
|
+
[59, 73, 108],
|
|
38
|
+
[87, 93, 109],
|
|
39
|
+
[112, 113, 115],
|
|
40
|
+
[138, 135, 121],
|
|
41
|
+
[165, 157, 116],
|
|
42
|
+
[196, 182, 101],
|
|
43
|
+
[254, 232, 56],
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** The names of the shipped maps. */
|
|
48
|
+
export const COLORMAPS: readonly ColormapName[] = ["viridis", "cividis"];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The colour of `t` on a map.
|
|
52
|
+
*
|
|
53
|
+
* @param name - The map.
|
|
54
|
+
* @param t - Position in `[0, 1]`; clamped. A non-finite `t` gives the map's low end.
|
|
55
|
+
* @returns A CSS `rgb(r g b)` colour.
|
|
56
|
+
*/
|
|
57
|
+
export function colormap(name: ColormapName, t: number): string {
|
|
58
|
+
const [r, g, b] = colormapRgb(name, t);
|
|
59
|
+
return `rgb(${r} ${g} ${b})`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The colour of `t` on a map, as integer channels.
|
|
64
|
+
*
|
|
65
|
+
* @param name - The map.
|
|
66
|
+
* @param t - Position in `[0, 1]`; clamped.
|
|
67
|
+
* @returns `[r, g, b]`, each 0–255.
|
|
68
|
+
*/
|
|
69
|
+
export function colormapRgb(name: ColormapName, t: number): [number, number, number] {
|
|
70
|
+
const anchors = ANCHORS[name];
|
|
71
|
+
const clamped = Number.isFinite(t) ? Math.min(1, Math.max(0, t)) : 0;
|
|
72
|
+
const scaled = clamped * (anchors.length - 1);
|
|
73
|
+
const i = Math.min(anchors.length - 2, Math.floor(scaled));
|
|
74
|
+
const f = scaled - i;
|
|
75
|
+
const a = anchors[i]!;
|
|
76
|
+
const b = anchors[i + 1]!;
|
|
77
|
+
return [
|
|
78
|
+
Math.round(a[0] + f * (b[0] - a[0])),
|
|
79
|
+
Math.round(a[1] + f * (b[1] - a[1])),
|
|
80
|
+
Math.round(a[2] + f * (b[2] - a[2])),
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A value's colour on a map, over a domain.
|
|
86
|
+
*
|
|
87
|
+
* @param name - The map.
|
|
88
|
+
* @param value - The value.
|
|
89
|
+
* @param domain - `[low, high]`: low maps to the map's start. A reversed domain reverses the map.
|
|
90
|
+
* @returns A CSS colour.
|
|
91
|
+
*/
|
|
92
|
+
export function colormapValue(name: ColormapName, value: number, domain: readonly [number, number]): string {
|
|
93
|
+
const [low, high] = domain;
|
|
94
|
+
const span = high - low;
|
|
95
|
+
return colormap(name, span === 0 ? 0 : (value - low) / span);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A CSS `linear-gradient` of a map, for a legend bar.
|
|
100
|
+
*
|
|
101
|
+
* @param name - The map.
|
|
102
|
+
* @param direction - The gradient's direction. Defaults to `to right`.
|
|
103
|
+
* @returns A CSS `background-image` value.
|
|
104
|
+
*/
|
|
105
|
+
export function colormapGradient(name: ColormapName, direction = "to right"): string {
|
|
106
|
+
const anchors = ANCHORS[name];
|
|
107
|
+
const stops = anchors.map(([r, g, b], i) => `rgb(${r} ${g} ${b}) ${((i / (anchors.length - 1)) * 100).toFixed(1)}%`);
|
|
108
|
+
return `linear-gradient(${direction}, ${stops.join(", ")})`;
|
|
109
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
export {
|
|
6
|
+
FLUID_DEFAULT_HEIGHT,
|
|
6
7
|
Frame,
|
|
7
8
|
Legend,
|
|
8
9
|
areaFor,
|
|
9
10
|
plotArea,
|
|
10
11
|
seriesColour,
|
|
12
|
+
useFluidSize,
|
|
11
13
|
DEFECT_COLOUR,
|
|
12
14
|
NORMAL_COLOUR,
|
|
13
15
|
SERIES_COLOURS,
|
|
@@ -42,3 +44,26 @@ export {
|
|
|
42
44
|
type LineProfileProps,
|
|
43
45
|
type ProfileSeries,
|
|
44
46
|
} from "./LineProfile";
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
Bands,
|
|
50
|
+
Markers,
|
|
51
|
+
PlotInteraction,
|
|
52
|
+
seriesValueAt,
|
|
53
|
+
type Band,
|
|
54
|
+
type InteractionProps,
|
|
55
|
+
type Marker,
|
|
56
|
+
type PlotArea,
|
|
57
|
+
type ReadoutSeries,
|
|
58
|
+
} from "./interaction";
|
|
59
|
+
|
|
60
|
+
export {
|
|
61
|
+
COLORMAPS,
|
|
62
|
+
colormap,
|
|
63
|
+
colormapGradient,
|
|
64
|
+
colormapRgb,
|
|
65
|
+
colormapValue,
|
|
66
|
+
type ColormapName,
|
|
67
|
+
} from "./colormap";
|
|
68
|
+
|
|
69
|
+
export { ColormapLegend, type ColormapLegendProps } from "./ColormapLegend";
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `LineChart` and `LineProfile` share beyond the frame:
|
|
3
|
+
* - x ranges and positions marked on the plot (`bands`, `markers`);
|
|
4
|
+
* - a pointer that reads the plot back (hover readout, click to pick);
|
|
5
|
+
* - a cursor the app can drive from elsewhere, e.g. the canvas a profile was sampled on.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { useState } from "react";
|
|
9
|
+
|
|
10
|
+
import { toneColor, type MeasureTone } from "@vitavision/ui";
|
|
11
|
+
|
|
12
|
+
import type { Scale } from "./scale";
|
|
13
|
+
|
|
14
|
+
/** An x range marked on the plot: a gap, a stretch out of tolerance, a region of interest. */
|
|
15
|
+
export interface Band {
|
|
16
|
+
/** Start of the range, in x domain units. */
|
|
17
|
+
from: number;
|
|
18
|
+
/** End of the range. */
|
|
19
|
+
to: number;
|
|
20
|
+
/** Its colour. Default `"muted"`. */
|
|
21
|
+
tone?: MeasureTone | undefined;
|
|
22
|
+
/** Text at the top of the band. */
|
|
23
|
+
label?: string | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A position marked on the x axis with a short tick: a station, a sample, a frame. */
|
|
27
|
+
export interface Marker {
|
|
28
|
+
/** Where, in x domain units. */
|
|
29
|
+
position: number;
|
|
30
|
+
/** Its colour. Default `"muted"`. */
|
|
31
|
+
tone?: MeasureTone | undefined;
|
|
32
|
+
/** Shown as the tick's tooltip (`<title>`). */
|
|
33
|
+
label?: string | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A series as the readout sees it. */
|
|
37
|
+
export interface ReadoutSeries {
|
|
38
|
+
/** Its name. */
|
|
39
|
+
name: string;
|
|
40
|
+
/** Its points. */
|
|
41
|
+
points: readonly { x: number; y: number }[];
|
|
42
|
+
/** Its colour. */
|
|
43
|
+
colour: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The props `LineChart` and `LineProfile` share for interaction and marking. */
|
|
47
|
+
export interface InteractionProps {
|
|
48
|
+
/** x ranges drawn behind the series. */
|
|
49
|
+
bands?: readonly Band[] | undefined;
|
|
50
|
+
/** Positions ticked on the x axis. */
|
|
51
|
+
markers?: readonly Marker[] | undefined;
|
|
52
|
+
/** An x position drawn as a vertical rule, from outside the chart (e.g. a canvas hover). */
|
|
53
|
+
cursor?: number | null | undefined;
|
|
54
|
+
/** Called with the x under the pointer (in domain units, clamped to the domain), or `null` on leaving. */
|
|
55
|
+
onHover?: ((x: number | null) => void) | undefined;
|
|
56
|
+
/** Called with the x under a click. Makes the plot area a pointer target. */
|
|
57
|
+
onPick?: ((x: number) => void) | undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Plot-area edges in viewBox units, as `areaFor` returns them (`y0` is the bottom). */
|
|
61
|
+
export interface PlotArea {
|
|
62
|
+
/** Left edge. */
|
|
63
|
+
x0: number;
|
|
64
|
+
/** Right edge. */
|
|
65
|
+
x1: number;
|
|
66
|
+
/** Bottom edge. */
|
|
67
|
+
y0: number;
|
|
68
|
+
/** Top edge. */
|
|
69
|
+
y1: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The ranges behind the series. */
|
|
73
|
+
export function Bands({ bands, xScale, area }: { bands: readonly Band[]; xScale: Scale; area: PlotArea }) {
|
|
74
|
+
return (
|
|
75
|
+
<g data-bands="">
|
|
76
|
+
{bands.map((band) => {
|
|
77
|
+
const a = clamp(xScale.project(Math.min(band.from, band.to)), area.x0, area.x1);
|
|
78
|
+
const b = clamp(xScale.project(Math.max(band.from, band.to)), area.x0, area.x1);
|
|
79
|
+
const colour = toneColor(band.tone, "muted");
|
|
80
|
+
return (
|
|
81
|
+
<g key={`${band.from}-${band.to}-${band.label ?? ""}`}>
|
|
82
|
+
<rect x={a} y={area.y1} width={Math.max(1, b - a)} height={area.y0 - area.y1} fill={colour} opacity={0.14} />
|
|
83
|
+
{band.label && (
|
|
84
|
+
<text x={(a + b) / 2} y={area.y1 + 10} textAnchor="middle" fontSize={9} fill={colour}>
|
|
85
|
+
{band.label}
|
|
86
|
+
</text>
|
|
87
|
+
)}
|
|
88
|
+
</g>
|
|
89
|
+
);
|
|
90
|
+
})}
|
|
91
|
+
</g>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The ticks on the x axis. */
|
|
96
|
+
export function Markers({ markers, xScale, area }: { markers: readonly Marker[]; xScale: Scale; area: PlotArea }) {
|
|
97
|
+
return (
|
|
98
|
+
<g data-markers="">
|
|
99
|
+
{markers.map((marker) => {
|
|
100
|
+
const x = xScale.project(marker.position);
|
|
101
|
+
if (x < area.x0 - 0.5 || x > area.x1 + 0.5) return null;
|
|
102
|
+
return (
|
|
103
|
+
<line
|
|
104
|
+
key={`${marker.position}-${marker.label ?? ""}`}
|
|
105
|
+
x1={x}
|
|
106
|
+
x2={x}
|
|
107
|
+
y1={area.y0}
|
|
108
|
+
y2={area.y0 - 6}
|
|
109
|
+
stroke={toneColor(marker.tone, "muted")}
|
|
110
|
+
strokeWidth={1.5}
|
|
111
|
+
>
|
|
112
|
+
{marker.label && <title>{marker.label}</title>}
|
|
113
|
+
</line>
|
|
114
|
+
);
|
|
115
|
+
})}
|
|
116
|
+
</g>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The pointer layer over the plot area: a crosshair and a value readout while hovering, the
|
|
122
|
+
* external `cursor` otherwise, and clicks as picks.
|
|
123
|
+
*/
|
|
124
|
+
export function PlotInteraction({
|
|
125
|
+
xScale,
|
|
126
|
+
yScale,
|
|
127
|
+
area,
|
|
128
|
+
series,
|
|
129
|
+
cursor,
|
|
130
|
+
onHover,
|
|
131
|
+
onPick,
|
|
132
|
+
format = (value: number) => (Math.abs(value) >= 100 ? value.toFixed(0) : value.toPrecision(3)),
|
|
133
|
+
}: {
|
|
134
|
+
xScale: Scale;
|
|
135
|
+
yScale: Scale;
|
|
136
|
+
area: PlotArea;
|
|
137
|
+
series: readonly ReadoutSeries[];
|
|
138
|
+
cursor?: number | null | undefined;
|
|
139
|
+
onHover?: ((x: number | null) => void) | undefined;
|
|
140
|
+
onPick?: ((x: number) => void) | undefined;
|
|
141
|
+
format?: (value: number) => string;
|
|
142
|
+
}) {
|
|
143
|
+
const [hover, setHover] = useState<number | null>(null);
|
|
144
|
+
const interactive = onHover !== undefined || onPick !== undefined;
|
|
145
|
+
|
|
146
|
+
const xAt = (event: { clientX: number; clientY: number; currentTarget: SVGRectElement }): number => {
|
|
147
|
+
const svg = event.currentTarget.ownerSVGElement;
|
|
148
|
+
const ctm = svg?.getScreenCTM();
|
|
149
|
+
let local = event.clientX;
|
|
150
|
+
if (svg && ctm) {
|
|
151
|
+
const point = svg.createSVGPoint();
|
|
152
|
+
point.x = event.clientX;
|
|
153
|
+
point.y = event.clientY;
|
|
154
|
+
local = point.matrixTransform(ctm.inverse()).x;
|
|
155
|
+
}
|
|
156
|
+
const [low, high] = xScale.domain;
|
|
157
|
+
return clamp(xScale.invert(local), Math.min(low, high), Math.max(low, high));
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const shown = hover ?? cursor ?? null;
|
|
161
|
+
const x = shown === null ? null : xScale.project(shown);
|
|
162
|
+
const readout =
|
|
163
|
+
hover === null
|
|
164
|
+
? []
|
|
165
|
+
: series.flatMap((entry) => {
|
|
166
|
+
const y = seriesValueAt(entry.points, hover);
|
|
167
|
+
return y === null ? [] : [{ name: entry.name, colour: entry.colour, y }];
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<g data-interaction="">
|
|
172
|
+
{x !== null && x >= area.x0 - 0.5 && x <= area.x1 + 0.5 && (
|
|
173
|
+
<line
|
|
174
|
+
data-crosshair=""
|
|
175
|
+
x1={x}
|
|
176
|
+
x2={x}
|
|
177
|
+
y1={area.y0}
|
|
178
|
+
y2={area.y1}
|
|
179
|
+
stroke="currentColor"
|
|
180
|
+
strokeWidth={1}
|
|
181
|
+
opacity={hover === null ? 0.6 : 0.9}
|
|
182
|
+
/>
|
|
183
|
+
)}
|
|
184
|
+
{readout.map((entry) => (
|
|
185
|
+
<circle
|
|
186
|
+
key={entry.name}
|
|
187
|
+
cx={x ?? 0}
|
|
188
|
+
cy={yScale.project(entry.y)}
|
|
189
|
+
r={3}
|
|
190
|
+
fill={entry.colour}
|
|
191
|
+
stroke="var(--surface)"
|
|
192
|
+
strokeWidth={1}
|
|
193
|
+
/>
|
|
194
|
+
))}
|
|
195
|
+
{hover !== null && x !== null && (
|
|
196
|
+
<text
|
|
197
|
+
data-readout=""
|
|
198
|
+
x={x > (area.x0 + area.x1) / 2 ? x - 6 : x + 6}
|
|
199
|
+
y={area.y1 + 10}
|
|
200
|
+
textAnchor={x > (area.x0 + area.x1) / 2 ? "end" : "start"}
|
|
201
|
+
fontSize={10}
|
|
202
|
+
fill="currentColor"
|
|
203
|
+
>
|
|
204
|
+
{[format(hover), ...readout.map((entry) => `${entry.name} ${format(entry.y)}`)].join(" · ")}
|
|
205
|
+
</text>
|
|
206
|
+
)}
|
|
207
|
+
{interactive && (
|
|
208
|
+
<rect
|
|
209
|
+
data-plot-target=""
|
|
210
|
+
x={area.x0}
|
|
211
|
+
y={area.y1}
|
|
212
|
+
width={area.x1 - area.x0}
|
|
213
|
+
height={area.y0 - area.y1}
|
|
214
|
+
fill="transparent"
|
|
215
|
+
style={{ cursor: onPick ? "pointer" : "crosshair" }}
|
|
216
|
+
onPointerMove={(event) => {
|
|
217
|
+
const value = xAt(event);
|
|
218
|
+
setHover(value);
|
|
219
|
+
onHover?.(value);
|
|
220
|
+
}}
|
|
221
|
+
onPointerLeave={() => {
|
|
222
|
+
setHover(null);
|
|
223
|
+
onHover?.(null);
|
|
224
|
+
}}
|
|
225
|
+
onClick={(event) => onPick?.(xAt(event))}
|
|
226
|
+
/>
|
|
227
|
+
)}
|
|
228
|
+
</g>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* A series' value at `x`, linearly interpolated between the points either side, or `null`
|
|
234
|
+
* outside its range or across a gap.
|
|
235
|
+
*
|
|
236
|
+
* @param points - The series, in ascending `x`.
|
|
237
|
+
* @param x - Where.
|
|
238
|
+
* @returns The interpolated value, or `null`.
|
|
239
|
+
*/
|
|
240
|
+
export function seriesValueAt(points: readonly { x: number; y: number }[], x: number): number | null {
|
|
241
|
+
if (points.length === 0) return null;
|
|
242
|
+
if (points.length === 1) return points[0]!.x === x ? points[0]!.y : null;
|
|
243
|
+
let lo = 0;
|
|
244
|
+
let hi = points.length - 1;
|
|
245
|
+
if (x < points[lo]!.x || x > points[hi]!.x) return null;
|
|
246
|
+
while (hi - lo > 1) {
|
|
247
|
+
const mid = (lo + hi) >> 1;
|
|
248
|
+
if (points[mid]!.x <= x) lo = mid;
|
|
249
|
+
else hi = mid;
|
|
250
|
+
}
|
|
251
|
+
const a = points[lo]!;
|
|
252
|
+
const b = points[hi]!;
|
|
253
|
+
if (!Number.isFinite(a.y) || !Number.isFinite(b.y)) return null;
|
|
254
|
+
if (b.x === a.x) return a.y;
|
|
255
|
+
return a.y + ((x - a.x) / (b.x - a.x)) * (b.y - a.y);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function clamp(value: number, low: number, high: number): number {
|
|
259
|
+
return Math.min(high, Math.max(low, value));
|
|
260
|
+
}
|
package/src/scale.ts
CHANGED
|
@@ -30,6 +30,8 @@ export interface Scale {
|
|
|
30
30
|
domain: [number, number];
|
|
31
31
|
/** Map one domain value onto the pixel range this scale was built for. */
|
|
32
32
|
project: (value: number) => number;
|
|
33
|
+
/** The inverse of `project`: the domain value at a pixel. Not clamped. */
|
|
34
|
+
invert: (pixel: number) => number;
|
|
33
35
|
/**
|
|
34
36
|
* Ticks inside the domain, in ascending order.
|
|
35
37
|
*
|
|
@@ -136,6 +138,7 @@ export function linearScale(
|
|
|
136
138
|
return {
|
|
137
139
|
domain: [low, high],
|
|
138
140
|
project: (value) => rangeStart + ((value - low) / span) * (rangeEnd - rangeStart),
|
|
141
|
+
invert: (pixel) => low + ((pixel - rangeStart) / (rangeEnd - rangeStart)) * span,
|
|
139
142
|
ticks: (count = 5) => {
|
|
140
143
|
const step = niceStep(span / Math.max(1, count));
|
|
141
144
|
const first = Math.ceil(low / step) * step;
|
|
@@ -171,6 +174,7 @@ export function logScale(domain: [number, number], rangeStart: number, rangeEnd:
|
|
|
171
174
|
const transformed = Math.log10(Math.max(value, LOG_FLOOR));
|
|
172
175
|
return rangeStart + ((transformed - paddedLow) / span) * (rangeEnd - rangeStart);
|
|
173
176
|
},
|
|
177
|
+
invert: (pixel) => 10 ** (paddedLow + ((pixel - rangeStart) / (rangeEnd - rangeStart)) * span),
|
|
174
178
|
ticks: (count = 5) => {
|
|
175
179
|
// One tick per decade, thinned when the range covers more decades than fit.
|
|
176
180
|
const stride = Math.max(1, Math.ceil(span / Math.max(1, count)));
|