@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/src/interaction.tsx
CHANGED
|
@@ -9,6 +9,8 @@ import { useState } from "react";
|
|
|
9
9
|
|
|
10
10
|
import { toneColor, type MeasureTone } from "@vitavision/ui";
|
|
11
11
|
|
|
12
|
+
import { seriesValueAt } from "./interpolate";
|
|
13
|
+
import { bandValueAt, formatWithBand, type SeriesBand } from "./band";
|
|
12
14
|
import type { Scale } from "./scale";
|
|
13
15
|
|
|
14
16
|
/** An x range marked on the plot: a gap, a stretch out of tolerance, a region of interest. */
|
|
@@ -41,6 +43,8 @@ export interface ReadoutSeries {
|
|
|
41
43
|
points: readonly { x: number; y: number }[];
|
|
42
44
|
/** Its colour. */
|
|
43
45
|
colour: string;
|
|
46
|
+
/** Its error band: the readout then reads `value ± half-width`, or the value with its range. */
|
|
47
|
+
band?: SeriesBand | undefined;
|
|
44
48
|
}
|
|
45
49
|
|
|
46
50
|
/** The props `LineChart` and `LineProfile` share for interaction and marking. */
|
|
@@ -69,6 +73,8 @@ export interface PlotArea {
|
|
|
69
73
|
y1: number;
|
|
70
74
|
}
|
|
71
75
|
|
|
76
|
+
export { seriesValueAt };
|
|
77
|
+
|
|
72
78
|
/** The ranges behind the series. */
|
|
73
79
|
export function Bands({ bands, xScale, area }: { bands: readonly Band[]; xScale: Scale; area: PlotArea }) {
|
|
74
80
|
return (
|
|
@@ -130,6 +136,7 @@ export function PlotInteraction({
|
|
|
130
136
|
onHover,
|
|
131
137
|
onPick,
|
|
132
138
|
format = (value: number) => (Math.abs(value) >= 100 ? value.toFixed(0) : value.toPrecision(3)),
|
|
139
|
+
xFormat,
|
|
133
140
|
}: {
|
|
134
141
|
xScale: Scale;
|
|
135
142
|
yScale: Scale;
|
|
@@ -139,6 +146,8 @@ export function PlotInteraction({
|
|
|
139
146
|
onHover?: ((x: number | null) => void) | undefined;
|
|
140
147
|
onPick?: ((x: number) => void) | undefined;
|
|
141
148
|
format?: (value: number) => string;
|
|
149
|
+
/** Formats the x in the readout. Default: `format`. */
|
|
150
|
+
xFormat?: ((value: number) => string) | undefined;
|
|
142
151
|
}) {
|
|
143
152
|
const [hover, setHover] = useState<number | null>(null);
|
|
144
153
|
const interactive = onHover !== undefined || onPick !== undefined;
|
|
@@ -164,7 +173,9 @@ export function PlotInteraction({
|
|
|
164
173
|
? []
|
|
165
174
|
: series.flatMap((entry) => {
|
|
166
175
|
const y = seriesValueAt(entry.points, hover);
|
|
167
|
-
|
|
176
|
+
if (y === null) return [];
|
|
177
|
+
const bounds = entry.band ? bandValueAt(entry.points, entry.band, hover) : null;
|
|
178
|
+
return [{ name: entry.name, colour: entry.colour, y, bounds }];
|
|
168
179
|
});
|
|
169
180
|
|
|
170
181
|
return (
|
|
@@ -201,7 +212,10 @@ export function PlotInteraction({
|
|
|
201
212
|
fontSize={10}
|
|
202
213
|
fill="currentColor"
|
|
203
214
|
>
|
|
204
|
-
{[
|
|
215
|
+
{[
|
|
216
|
+
(xFormat ?? format)(hover),
|
|
217
|
+
...readout.map((entry) => `${entry.name} ${entry.bounds ? formatWithBand(entry.y, entry.bounds, format) : format(entry.y)}`),
|
|
218
|
+
].join(" · ")}
|
|
205
219
|
</text>
|
|
206
220
|
)}
|
|
207
221
|
{interactive && (
|
|
@@ -229,32 +243,6 @@ export function PlotInteraction({
|
|
|
229
243
|
);
|
|
230
244
|
}
|
|
231
245
|
|
|
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
246
|
function clamp(value: number, low: number, high: number): number {
|
|
259
247
|
return Math.min(high, Math.max(low, value));
|
|
260
248
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Reading a series between its points. */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A series' value at `x`, linearly interpolated between the points either side, or `null`
|
|
5
|
+
* outside its range or across a gap.
|
|
6
|
+
*
|
|
7
|
+
* @param points - The series, in ascending `x`.
|
|
8
|
+
* @param x - Where.
|
|
9
|
+
* @returns The interpolated value, or `null`.
|
|
10
|
+
*/
|
|
11
|
+
export function seriesValueAt(points: readonly { x: number; y: number }[], x: number): number | null {
|
|
12
|
+
if (points.length === 0) return null;
|
|
13
|
+
if (points.length === 1) return points[0]!.x === x ? points[0]!.y : null;
|
|
14
|
+
let lo = 0;
|
|
15
|
+
let hi = points.length - 1;
|
|
16
|
+
if (x < points[lo]!.x || x > points[hi]!.x) return null;
|
|
17
|
+
while (hi - lo > 1) {
|
|
18
|
+
const mid = (lo + hi) >> 1;
|
|
19
|
+
if (points[mid]!.x <= x) lo = mid;
|
|
20
|
+
else hi = mid;
|
|
21
|
+
}
|
|
22
|
+
const a = points[lo]!;
|
|
23
|
+
const b = points[hi]!;
|
|
24
|
+
if (!Number.isFinite(a.y) || !Number.isFinite(b.y)) return null;
|
|
25
|
+
if (b.x === a.x) return a.y;
|
|
26
|
+
return a.y + ((x - a.x) / (b.x - a.x)) * (b.y - a.y);
|
|
27
|
+
}
|
package/src/scatter.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic behind `ScatterChart`: which point is under the pointer, which colour step a
|
|
3
|
+
* value falls in, and the domain a colour scale covers.
|
|
4
|
+
*
|
|
5
|
+
* Pure: the component is a thin list of SVG elements over it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { symmetricDomain } from "./colormap";
|
|
9
|
+
|
|
10
|
+
/** The number of colour steps a value-coloured series is quantised into: one path each. */
|
|
11
|
+
export const SCATTER_COLOUR_STEPS = 24;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The projected point nearest to a position, within a radius.
|
|
15
|
+
*
|
|
16
|
+
* @param xy - Flat `[x0, y0, x1, y1, …]` in the plot's own units (viewBox units); a non-finite
|
|
17
|
+
* coordinate is skipped.
|
|
18
|
+
* @param x - The query's x.
|
|
19
|
+
* @param y - The query's y.
|
|
20
|
+
* @param radius - How far from a point the query still picks it.
|
|
21
|
+
* @returns The index of the nearest point (the lower index on a tie), or `-1`.
|
|
22
|
+
*/
|
|
23
|
+
export function nearestProjected(xy: ArrayLike<number>, x: number, y: number, radius: number): number {
|
|
24
|
+
const limit = radius * radius;
|
|
25
|
+
let best = -1;
|
|
26
|
+
let bestD = Infinity;
|
|
27
|
+
const count = Math.floor(xy.length / 2);
|
|
28
|
+
for (let i = 0; i < count; i++) {
|
|
29
|
+
const dx = xy[2 * i]! - x;
|
|
30
|
+
const dy = xy[2 * i + 1]! - y;
|
|
31
|
+
const d = dx * dx + dy * dy;
|
|
32
|
+
if (d <= limit && d < bestD) {
|
|
33
|
+
best = i;
|
|
34
|
+
bestD = d;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return best;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The colour step of a value.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The point's value.
|
|
44
|
+
* @param domain - `[low, high]`: `low` maps to the first step.
|
|
45
|
+
* @param steps - How many steps. Defaults to `SCATTER_COLOUR_STEPS`.
|
|
46
|
+
* @returns An integer in `[0, steps - 1]`; `-1` for a non-finite value; a degenerate domain puts
|
|
47
|
+
* every value in step `0`.
|
|
48
|
+
*/
|
|
49
|
+
export function colourStep(value: number, domain: readonly [number, number], steps = SCATTER_COLOUR_STEPS): number {
|
|
50
|
+
if (!Number.isFinite(value)) return -1;
|
|
51
|
+
const [low, high] = domain;
|
|
52
|
+
if (!(high > low)) return 0;
|
|
53
|
+
return Math.min(steps - 1, Math.max(0, Math.floor(((value - low) / (high - low)) * steps)));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The domain a value colouring covers.
|
|
58
|
+
*
|
|
59
|
+
* @param values - Every point's value; non-finite ones are ignored.
|
|
60
|
+
* @param symmetric - Centre it on zero, for a diverging map.
|
|
61
|
+
* @param given - A domain the caller fixed. It wins, centred if `symmetric`.
|
|
62
|
+
* @returns `[low, high]`; `[0, 1]` when there is no finite value.
|
|
63
|
+
*/
|
|
64
|
+
export function colourDomain(values: Iterable<number>, symmetric: boolean, given?: readonly [number, number]): [number, number] {
|
|
65
|
+
let low = Infinity;
|
|
66
|
+
let high = -Infinity;
|
|
67
|
+
if (given) [low, high] = given;
|
|
68
|
+
else {
|
|
69
|
+
for (const v of values) {
|
|
70
|
+
if (!Number.isFinite(v)) continue;
|
|
71
|
+
if (v < low) low = v;
|
|
72
|
+
if (v > high) high = v;
|
|
73
|
+
}
|
|
74
|
+
if (low > high) return symmetric ? [-1, 1] : [0, 1];
|
|
75
|
+
}
|
|
76
|
+
return symmetric ? symmetricDomain([low, high]) : [low, high];
|
|
77
|
+
}
|