@vitavision/charts 0.8.1 → 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/BarChart.tsx +2 -8
- package/src/ColormapLegend.tsx +250 -11
- package/src/Frame.tsx +25 -66
- package/src/Histogram.tsx +3 -16
- package/src/LineChart.tsx +41 -4
- package/src/LineProfile.tsx +2 -14
- package/src/ScatterChart.tsx +416 -0
- package/src/ScoreHistogram.tsx +4 -21
- package/src/ThresholdRule.tsx +1 -12
- package/src/band.ts +105 -0
- package/src/bars.ts +1 -5
- package/src/colormap.ts +87 -11
- package/src/index.ts +19 -26
- package/src/interaction.tsx +16 -28
- package/src/interpolate.ts +27 -0
- package/src/scale.ts +4 -20
- package/src/scatter.ts +77 -0
package/src/colormap.ts
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Colour maps for a magnitude: a residual's size, a per-cell error, a confidence, a validity
|
|
3
|
+
* fraction, or a signed quantity around zero.
|
|
4
4
|
*
|
|
5
5
|
* Only perceptually uniform maps ship, so equal steps in the data look like equal steps.
|
|
6
|
+
*
|
|
7
|
+
* Sequential, low to high:
|
|
6
8
|
* - `viridis` is the default on the chrome.
|
|
7
9
|
* - `cividis` is for when the map alone carries the finding and the reader's colour vision is
|
|
8
10
|
* unknown.
|
|
11
|
+
* - `magma` and `inferno` run from black to a pale yellow through purple and red or orange. They
|
|
12
|
+
* read well over a dark image, where `viridis`' dark end disappears.
|
|
13
|
+
*
|
|
14
|
+
* Diverging, for a signed value whose zero matters (a displacement toward or away from a
|
|
15
|
+
* centre, a difference between two runs):
|
|
16
|
+
* - `blueOrange` runs from a deep blue through a neutral light grey at the middle to a deep
|
|
17
|
+
* orange-brown. Both arms fall in lightness at the same rate and carry the same amount of
|
|
18
|
+
* colour, so +x and -x look equally strong, and its hues avoid the red-green axis. Centre it on zero with `symmetricDomain`.
|
|
9
19
|
*
|
|
10
20
|
* A continuous quality is a magnitude even when low values are "good": stepping it onto verdict
|
|
11
21
|
* colours (green / amber / red) claims a threshold judgement nobody made.
|
|
12
22
|
*
|
|
13
|
-
* The anchors are matplotlib
|
|
14
|
-
* between them stays within a few units of the full 256-entry tables.
|
|
23
|
+
* The sequential anchors are the matplotlib maps sampled at nine evenly spaced points. Linear
|
|
24
|
+
* interpolation between them stays within a few units of the full 256-entry tables. The
|
|
25
|
+
* diverging map was constructed in OKLab (equal lightness and chroma steps along each arm,
|
|
26
|
+
* mirrored in lightness, kept inside sRGB) and sampled at seventeen points.
|
|
15
27
|
*/
|
|
16
28
|
|
|
17
29
|
/** The shipped maps. */
|
|
18
|
-
export type ColormapName = "viridis" | "cividis";
|
|
30
|
+
export type ColormapName = "viridis" | "cividis" | "magma" | "inferno" | "blueOrange";
|
|
19
31
|
|
|
20
32
|
type Rgb = readonly [number, number, number];
|
|
21
33
|
|
|
@@ -31,6 +43,47 @@ const ANCHORS: Record<ColormapName, readonly Rgb[]> = {
|
|
|
31
43
|
[170, 220, 50],
|
|
32
44
|
[253, 231, 37],
|
|
33
45
|
],
|
|
46
|
+
magma: [
|
|
47
|
+
[0, 0, 4],
|
|
48
|
+
[29, 17, 71],
|
|
49
|
+
[81, 18, 124],
|
|
50
|
+
[131, 38, 129],
|
|
51
|
+
[183, 55, 121],
|
|
52
|
+
[231, 82, 99],
|
|
53
|
+
[252, 137, 97],
|
|
54
|
+
[254, 196, 136],
|
|
55
|
+
[252, 253, 191],
|
|
56
|
+
],
|
|
57
|
+
inferno: [
|
|
58
|
+
[0, 0, 4],
|
|
59
|
+
[33, 12, 74],
|
|
60
|
+
[87, 16, 110],
|
|
61
|
+
[138, 34, 106],
|
|
62
|
+
[188, 55, 84],
|
|
63
|
+
[228, 90, 49],
|
|
64
|
+
[249, 142, 9],
|
|
65
|
+
[249, 203, 53],
|
|
66
|
+
[252, 255, 164],
|
|
67
|
+
],
|
|
68
|
+
blueOrange: [
|
|
69
|
+
[0, 70, 140],
|
|
70
|
+
[33, 90, 158],
|
|
71
|
+
[64, 112, 172],
|
|
72
|
+
[92, 133, 185],
|
|
73
|
+
[121, 155, 198],
|
|
74
|
+
[150, 176, 211],
|
|
75
|
+
[179, 198, 222],
|
|
76
|
+
[209, 220, 233],
|
|
77
|
+
[242, 242, 242],
|
|
78
|
+
[230, 216, 207],
|
|
79
|
+
[216, 190, 175],
|
|
80
|
+
[201, 165, 144],
|
|
81
|
+
[186, 140, 113],
|
|
82
|
+
[170, 116, 83],
|
|
83
|
+
[153, 91, 52],
|
|
84
|
+
[136, 67, 13],
|
|
85
|
+
[113, 52, 0],
|
|
86
|
+
],
|
|
34
87
|
cividis: [
|
|
35
88
|
[0, 34, 78],
|
|
36
89
|
[18, 53, 112],
|
|
@@ -45,7 +98,33 @@ const ANCHORS: Record<ColormapName, readonly Rgb[]> = {
|
|
|
45
98
|
};
|
|
46
99
|
|
|
47
100
|
/** The names of the shipped maps. */
|
|
48
|
-
export const COLORMAPS: readonly ColormapName[] = ["viridis", "cividis"];
|
|
101
|
+
export const COLORMAPS: readonly ColormapName[] = ["viridis", "cividis", "magma", "inferno", "blueOrange"];
|
|
102
|
+
|
|
103
|
+
/** The maps that run through a neutral middle, for a signed value: use them over a domain centred on zero. */
|
|
104
|
+
export const DIVERGING_COLORMAPS: readonly ColormapName[] = ["blueOrange"];
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Whether a map is diverging.
|
|
108
|
+
*
|
|
109
|
+
* @param name - The map.
|
|
110
|
+
* @returns `true` for a map meant for a signed value around zero.
|
|
111
|
+
*/
|
|
112
|
+
export function isDivergingColormap(name: ColormapName): boolean {
|
|
113
|
+
return DIVERGING_COLORMAPS.includes(name);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A domain centred on zero that holds a given one: `[-m, m]` with `m` the largest absolute end.
|
|
118
|
+
* Feed it a diverging map so the middle colour means zero.
|
|
119
|
+
*
|
|
120
|
+
* @param domain - `[low, high]`, or the values to cover.
|
|
121
|
+
* @returns `[-m, m]`; `[-1, 1]` when no end is finite and non-zero.
|
|
122
|
+
*/
|
|
123
|
+
export function symmetricDomain(domain: readonly number[]): [number, number] {
|
|
124
|
+
let m = 0;
|
|
125
|
+
for (const v of domain) if (Number.isFinite(v) && Math.abs(v) > m) m = Math.abs(v);
|
|
126
|
+
return m > 0 ? [-m, m] : [-1, 1];
|
|
127
|
+
}
|
|
49
128
|
|
|
50
129
|
/**
|
|
51
130
|
* The colour of `t` on a map.
|
|
@@ -74,11 +153,7 @@ export function colormapRgb(name: ColormapName, t: number): [number, number, num
|
|
|
74
153
|
const f = scaled - i;
|
|
75
154
|
const a = anchors[i]!;
|
|
76
155
|
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
|
-
];
|
|
156
|
+
return [Math.round(a[0] + f * (b[0] - a[0])), Math.round(a[1] + f * (b[1] - a[1])), Math.round(a[2] + f * (b[2] - a[2]))];
|
|
82
157
|
}
|
|
83
158
|
|
|
84
159
|
/**
|
|
@@ -87,6 +162,7 @@ export function colormapRgb(name: ColormapName, t: number): [number, number, num
|
|
|
87
162
|
* @param name - The map.
|
|
88
163
|
* @param value - The value.
|
|
89
164
|
* @param domain - `[low, high]`: low maps to the map's start. A reversed domain reverses the map.
|
|
165
|
+
* For a diverging map pass a domain centred on zero (`symmetricDomain`).
|
|
90
166
|
* @returns A CSS colour.
|
|
91
167
|
*/
|
|
92
168
|
export function colormapValue(name: ColormapName, value: number, domain: readonly [number, number]): string {
|
package/src/index.ts
CHANGED
|
@@ -19,39 +19,29 @@ export {
|
|
|
19
19
|
type Variant,
|
|
20
20
|
} from "./Frame";
|
|
21
21
|
|
|
22
|
-
export {
|
|
23
|
-
extent,
|
|
24
|
-
formatTick,
|
|
25
|
-
histogram,
|
|
26
|
-
linePath,
|
|
27
|
-
linearScale,
|
|
28
|
-
logScale,
|
|
29
|
-
niceStep,
|
|
30
|
-
padDomain,
|
|
31
|
-
type Scale,
|
|
32
|
-
type Tick,
|
|
33
|
-
} from "./scale";
|
|
22
|
+
export { extent, formatTick, histogram, linePath, linearScale, logScale, niceStep, padDomain, type Scale, type Tick } from "./scale";
|
|
34
23
|
|
|
35
24
|
export { LineChart, type LineChartProps, type Series } from "./LineChart";
|
|
25
|
+
export { bandPath, formatWithBand, type SeriesBand } from "./band";
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
ScatterChart,
|
|
29
|
+
type ScatterChartProps,
|
|
30
|
+
type ScatterColourBy,
|
|
31
|
+
type ScatterPick,
|
|
32
|
+
type ScatterPoint,
|
|
33
|
+
type ScatterReferenceLine,
|
|
34
|
+
type ScatterSeries,
|
|
35
|
+
} from "./ScatterChart";
|
|
36
|
+
export { SCATTER_COLOUR_STEPS, colourDomain, colourStep, nearestProjected } from "./scatter";
|
|
36
37
|
|
|
37
38
|
export { StackedBars, type BarRow, type StackedBarsProps } from "./BarChart";
|
|
38
39
|
|
|
39
|
-
export {
|
|
40
|
-
Histogram,
|
|
41
|
-
type HistogramCounts,
|
|
42
|
-
type HistogramOptions,
|
|
43
|
-
type HistogramProps,
|
|
44
|
-
type HistogramValues,
|
|
45
|
-
} from "./Histogram";
|
|
40
|
+
export { Histogram, type HistogramCounts, type HistogramOptions, type HistogramProps, type HistogramValues } from "./Histogram";
|
|
46
41
|
|
|
47
42
|
export { ScoreHistogram, type ScoreHistogramProps } from "./ScoreHistogram";
|
|
48
43
|
|
|
49
|
-
export {
|
|
50
|
-
LineProfile,
|
|
51
|
-
type EdgeMark,
|
|
52
|
-
type LineProfileProps,
|
|
53
|
-
type ProfileSeries,
|
|
54
|
-
} from "./LineProfile";
|
|
44
|
+
export { LineProfile, type EdgeMark, type LineProfileProps, type ProfileSeries } from "./LineProfile";
|
|
55
45
|
|
|
56
46
|
export {
|
|
57
47
|
Bands,
|
|
@@ -67,11 +57,14 @@ export {
|
|
|
67
57
|
|
|
68
58
|
export {
|
|
69
59
|
COLORMAPS,
|
|
60
|
+
DIVERGING_COLORMAPS,
|
|
70
61
|
colormap,
|
|
71
62
|
colormapGradient,
|
|
72
63
|
colormapRgb,
|
|
73
64
|
colormapValue,
|
|
65
|
+
isDivergingColormap,
|
|
66
|
+
symmetricDomain,
|
|
74
67
|
type ColormapName,
|
|
75
68
|
} from "./colormap";
|
|
76
69
|
|
|
77
|
-
export { ColormapLegend, type ColormapLegendProps } from "./ColormapLegend";
|
|
70
|
+
export { ColormapLegend, type ColormapClipped, type ColormapLegendProps, type ColormapMarker } from "./ColormapLegend";
|
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/scale.ts
CHANGED
|
@@ -128,11 +128,7 @@ export function extent(values: readonly number[]): [number, number] {
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
/** A linear scale from `domain` onto `[rangeStart, rangeEnd]` pixels. */
|
|
131
|
-
export function linearScale(
|
|
132
|
-
domain: [number, number],
|
|
133
|
-
rangeStart: number,
|
|
134
|
-
rangeEnd: number,
|
|
135
|
-
): Scale {
|
|
131
|
+
export function linearScale(domain: [number, number], rangeStart: number, rangeEnd: number): Scale {
|
|
136
132
|
const [low, high] = padDomain(domain[0], domain[1]);
|
|
137
133
|
const span = high - low;
|
|
138
134
|
return {
|
|
@@ -179,11 +175,7 @@ export function logScale(domain: [number, number], rangeStart: number, rangeEnd:
|
|
|
179
175
|
// One tick per decade, thinned when the range covers more decades than fit.
|
|
180
176
|
const stride = Math.max(1, Math.ceil(span / Math.max(1, count)));
|
|
181
177
|
const ticks: Tick[] = [];
|
|
182
|
-
for (
|
|
183
|
-
let exponent = Math.ceil(paddedLow);
|
|
184
|
-
exponent <= paddedHigh + 1e-9;
|
|
185
|
-
exponent += stride
|
|
186
|
-
) {
|
|
178
|
+
for (let exponent = Math.ceil(paddedLow); exponent <= paddedHigh + 1e-9; exponent += stride) {
|
|
187
179
|
const value = 10 ** exponent;
|
|
188
180
|
ticks.push({ value, label: formatTick(value, value) });
|
|
189
181
|
}
|
|
@@ -193,11 +185,7 @@ export function logScale(domain: [number, number], rangeStart: number, rangeEnd:
|
|
|
193
185
|
}
|
|
194
186
|
|
|
195
187
|
/** An SVG `d` attribute through the projected points, skipping non-finite ones. */
|
|
196
|
-
export function linePath(
|
|
197
|
-
points: readonly { x: number; y: number }[],
|
|
198
|
-
xScale: Scale,
|
|
199
|
-
yScale: Scale,
|
|
200
|
-
): string {
|
|
188
|
+
export function linePath(points: readonly { x: number; y: number }[], xScale: Scale, yScale: Scale): string {
|
|
201
189
|
const commands: string[] = [];
|
|
202
190
|
let pen = "M";
|
|
203
191
|
for (const point of points) {
|
|
@@ -219,11 +207,7 @@ export function linePath(
|
|
|
219
207
|
* Used for the score histogram, where the two classes must share one set of edges or the
|
|
220
208
|
* bars cannot be compared. Callers pass the shared domain in for exactly that reason.
|
|
221
209
|
*/
|
|
222
|
-
export function histogram(
|
|
223
|
-
values: readonly number[],
|
|
224
|
-
domain: [number, number],
|
|
225
|
-
count: number,
|
|
226
|
-
): number[] {
|
|
210
|
+
export function histogram(values: readonly number[], domain: [number, number], count: number): number[] {
|
|
227
211
|
const bins = new Array<number>(Math.max(1, count)).fill(0);
|
|
228
212
|
const [low, high] = domain;
|
|
229
213
|
const span = high - low;
|
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
|
+
}
|