react-cheminfo 0.15.0 → 0.16.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 +96 -0
- package/lib/core.d.ts +1 -0
- package/lib/core.d.ts.map +1 -1
- package/lib/core.js +1 -0
- package/lib/core.js.map +1 -1
- package/lib/credits/core/credits.d.ts +17 -0
- package/lib/credits/core/credits.d.ts.map +1 -1
- package/lib/credits/core/credits.js +20 -0
- package/lib/credits/core/credits.js.map +1 -1
- package/lib/ecosystem/core/sites.d.ts.map +1 -1
- package/lib/ecosystem/core/sites.js +40 -0
- package/lib/ecosystem/core/sites.js.map +1 -1
- package/lib/ecosystem/core/types.d.ts +1 -1
- package/lib/ecosystem/core/types.d.ts.map +1 -1
- package/lib/ecosystem/ui/glyphs.d.ts.map +1 -1
- package/lib/ecosystem/ui/glyphs.js +11 -0
- package/lib/ecosystem/ui/glyphs.js.map +1 -1
- package/lib/parallel/core/index.d.ts +27 -0
- package/lib/parallel/core/index.d.ts.map +1 -0
- package/lib/parallel/core/index.js +20 -0
- package/lib/parallel/core/index.js.map +1 -0
- package/lib/parallel/core/parallelAxes.d.ts +103 -0
- package/lib/parallel/core/parallelAxes.d.ts.map +1 -0
- package/lib/parallel/core/parallelAxes.js +146 -0
- package/lib/parallel/core/parallelAxes.js.map +1 -0
- package/lib/parallel/core/parallelBrush.d.ts +79 -0
- package/lib/parallel/core/parallelBrush.d.ts.map +1 -0
- package/lib/parallel/core/parallelBrush.js +99 -0
- package/lib/parallel/core/parallelBrush.js.map +1 -0
- package/lib/parallel/core/parallelExtent.d.ts +28 -0
- package/lib/parallel/core/parallelExtent.d.ts.map +1 -0
- package/lib/parallel/core/parallelExtent.js +47 -0
- package/lib/parallel/core/parallelExtent.js.map +1 -0
- package/lib/parallel/core/parallelFilter.d.ts +36 -0
- package/lib/parallel/core/parallelFilter.d.ts.map +1 -0
- package/lib/parallel/core/parallelFilter.js +74 -0
- package/lib/parallel/core/parallelFilter.js.map +1 -0
- package/lib/parallel/core/parallelHit.d.ts +47 -0
- package/lib/parallel/core/parallelHit.d.ts.map +1 -0
- package/lib/parallel/core/parallelHit.js +81 -0
- package/lib/parallel/core/parallelHit.js.map +1 -0
- package/lib/parallel/core/parallelPaint.d.ts +104 -0
- package/lib/parallel/core/parallelPaint.d.ts.map +1 -0
- package/lib/parallel/core/parallelPaint.js +160 -0
- package/lib/parallel/core/parallelPaint.js.map +1 -0
- package/lib/parallel/core/parallelPalette.d.ts +31 -0
- package/lib/parallel/core/parallelPalette.d.ts.map +1 -0
- package/lib/parallel/core/parallelPalette.js +68 -0
- package/lib/parallel/core/parallelPalette.js.map +1 -0
- package/lib/parallel/core/parallelTicks.d.ts +34 -0
- package/lib/parallel/core/parallelTicks.d.ts.map +1 -0
- package/lib/parallel/core/parallelTicks.js +116 -0
- package/lib/parallel/core/parallelTicks.js.map +1 -0
- package/lib/parallel/core/parallelTypes.d.ts +129 -0
- package/lib/parallel/core/parallelTypes.d.ts.map +1 -0
- package/lib/parallel/core/parallelTypes.js +29 -0
- package/lib/parallel/core/parallelTypes.js.map +1 -0
- package/lib/parallel/ui/ParallelAxesLayer.d.ts +28 -0
- package/lib/parallel/ui/ParallelAxesLayer.d.ts.map +1 -0
- package/lib/parallel/ui/ParallelAxesLayer.js +29 -0
- package/lib/parallel/ui/ParallelAxesLayer.js.map +1 -0
- package/lib/parallel/ui/ParallelCoordinates.d.ts +21 -0
- package/lib/parallel/ui/ParallelCoordinates.d.ts.map +1 -0
- package/lib/parallel/ui/ParallelCoordinates.js +109 -0
- package/lib/parallel/ui/ParallelCoordinates.js.map +1 -0
- package/lib/parallel/ui/ParallelLabels.d.ts +26 -0
- package/lib/parallel/ui/ParallelLabels.d.ts.map +1 -0
- package/lib/parallel/ui/ParallelLabels.js +25 -0
- package/lib/parallel/ui/ParallelLabels.js.map +1 -0
- package/lib/parallel/ui/ParallelTooltip.d.ts +26 -0
- package/lib/parallel/ui/ParallelTooltip.d.ts.map +1 -0
- package/lib/parallel/ui/ParallelTooltip.js +30 -0
- package/lib/parallel/ui/ParallelTooltip.js.map +1 -0
- package/lib/parallel/ui/index.d.ts +25 -0
- package/lib/parallel/ui/index.d.ts.map +1 -0
- package/lib/parallel/ui/index.js +18 -0
- package/lib/parallel/ui/index.js.map +1 -0
- package/lib/parallel/ui/parallelCoordinatesProps.d.ts +185 -0
- package/lib/parallel/ui/parallelCoordinatesProps.d.ts.map +1 -0
- package/lib/parallel/ui/parallelCoordinatesProps.js +11 -0
- package/lib/parallel/ui/parallelCoordinatesProps.js.map +1 -0
- package/lib/parallel/ui/parallelGestureModel.d.ts +52 -0
- package/lib/parallel/ui/parallelGestureModel.d.ts.map +1 -0
- package/lib/parallel/ui/parallelGestureModel.js +80 -0
- package/lib/parallel/ui/parallelGestureModel.js.map +1 -0
- package/lib/parallel/ui/parallelInk.d.ts +76 -0
- package/lib/parallel/ui/parallelInk.d.ts.map +1 -0
- package/lib/parallel/ui/parallelInk.js +113 -0
- package/lib/parallel/ui/parallelInk.js.map +1 -0
- package/lib/parallel/ui/parallelPlotModel.d.ts +55 -0
- package/lib/parallel/ui/parallelPlotModel.d.ts.map +1 -0
- package/lib/parallel/ui/parallelPlotModel.js +113 -0
- package/lib/parallel/ui/parallelPlotModel.js.map +1 -0
- package/lib/parallel/ui/parallelStyles.d.ts +83 -0
- package/lib/parallel/ui/parallelStyles.d.ts.map +1 -0
- package/lib/parallel/ui/parallelStyles.js +131 -0
- package/lib/parallel/ui/parallelStyles.js.map +1 -0
- package/lib/parallel/ui/useParallelCanvas.d.ts +49 -0
- package/lib/parallel/ui/useParallelCanvas.d.ts.map +1 -0
- package/lib/parallel/ui/useParallelCanvas.js +58 -0
- package/lib/parallel/ui/useParallelCanvas.js.map +1 -0
- package/lib/parallel/ui/useParallelGesture.d.ts +67 -0
- package/lib/parallel/ui/useParallelGesture.d.ts.map +1 -0
- package/lib/parallel/ui/useParallelGesture.js +139 -0
- package/lib/parallel/ui/useParallelGesture.js.map +1 -0
- package/lib/structure/ui/useToolbarFloor.d.ts.map +1 -1
- package/lib/structure/ui/useToolbarFloor.js +29 -22
- package/lib/structure/ui/useToolbarFloor.js.map +1 -1
- package/lib/ui.d.ts +1 -0
- package/lib/ui.d.ts.map +1 -1
- package/lib/ui.js +1 -0
- package/lib/ui.js.map +1 -1
- package/package.json +1 -1
- package/src/core.ts +1 -0
- package/src/credits/core/credits.ts +21 -0
- package/src/ecosystem/core/sites.ts +40 -0
- package/src/ecosystem/core/types.ts +4 -1
- package/src/ecosystem/ui/glyphs.tsx +46 -0
- package/src/parallel/core/index.ts +80 -0
- package/src/parallel/core/parallelAxes.ts +207 -0
- package/src/parallel/core/parallelBrush.ts +145 -0
- package/src/parallel/core/parallelExtent.ts +56 -0
- package/src/parallel/core/parallelFilter.ts +94 -0
- package/src/parallel/core/parallelHit.ts +106 -0
- package/src/parallel/core/parallelPaint.ts +231 -0
- package/src/parallel/core/parallelPalette.ts +80 -0
- package/src/parallel/core/parallelTicks.ts +164 -0
- package/src/parallel/core/parallelTypes.ts +147 -0
- package/src/parallel/ui/ParallelAxesLayer.tsx +123 -0
- package/src/parallel/ui/ParallelCoordinates.tsx +233 -0
- package/src/parallel/ui/ParallelLabels.tsx +56 -0
- package/src/parallel/ui/ParallelTooltip.tsx +55 -0
- package/src/parallel/ui/index.ts +50 -0
- package/src/parallel/ui/parallelCoordinatesProps.ts +199 -0
- package/src/parallel/ui/parallelGestureModel.ts +109 -0
- package/src/parallel/ui/parallelInk.ts +182 -0
- package/src/parallel/ui/parallelPlotModel.ts +130 -0
- package/src/parallel/ui/parallelStyles.ts +146 -0
- package/src/parallel/ui/useParallelCanvas.ts +104 -0
- package/src/parallel/ui/useParallelGesture.ts +227 -0
- package/src/structure/ui/useToolbarFloor.ts +27 -22
- package/src/ui.ts +1 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lines, painted into a canvas.
|
|
3
|
+
*
|
|
4
|
+
* The first canvas figure in the family, and for one reason: ten thousand
|
|
5
|
+
* polylines of a dozen points each is a hundred and twenty thousand SVG
|
|
6
|
+
* coordinates the browser would keep as elements. Everything a reader points
|
|
7
|
+
* at, brushes or reads is still SVG or HTML over the top, so only the mass of
|
|
8
|
+
* lines is pixels — which is also why saving the figure keeps the axes and
|
|
9
|
+
* loses the lines, and why a site that needs a downloadable figure should say
|
|
10
|
+
* so before choosing this one.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { ParallelAxisLayout } from './parallelAxes.ts';
|
|
14
|
+
import { PARALLEL_MARGIN, parallelPixelAt } from './parallelAxes.ts';
|
|
15
|
+
|
|
16
|
+
/** How strongly one kept line is drawn, so that the mass reads as a density. */
|
|
17
|
+
export const PARALLEL_INCLUDED_ALPHA = 0.35;
|
|
18
|
+
|
|
19
|
+
/** How wide a singled-out line's halo is, in pixels. */
|
|
20
|
+
const HALO_WIDTH = 5;
|
|
21
|
+
|
|
22
|
+
/** How wide a singled-out line itself is. */
|
|
23
|
+
const HIGHLIGHT_WIDTH = 2;
|
|
24
|
+
|
|
25
|
+
/** How large the dot at each of its axis crossings is. */
|
|
26
|
+
const HIGHLIGHT_DOT_RADIUS = 3;
|
|
27
|
+
|
|
28
|
+
const FULL_TURN = 2 * Math.PI;
|
|
29
|
+
|
|
30
|
+
/** What one pass of the line painter needs. */
|
|
31
|
+
export interface ParallelPaint {
|
|
32
|
+
/** The axes, from left to right, already placed and measured. */
|
|
33
|
+
layouts: readonly ParallelAxisLayout[];
|
|
34
|
+
/** Each axis's column, in the same order. */
|
|
35
|
+
values: ReadonlyArray<ArrayLike<number>>;
|
|
36
|
+
/** How many rows to draw. */
|
|
37
|
+
count: number;
|
|
38
|
+
/**
|
|
39
|
+
* Which rows the brushes keep, a zero meaning the row is drawn underneath in
|
|
40
|
+
* the excluded ink.
|
|
41
|
+
* @default null — every row is kept and nothing is drawn underneath
|
|
42
|
+
*/
|
|
43
|
+
included?: Uint8Array | null;
|
|
44
|
+
/**
|
|
45
|
+
* Which colour each row takes, as a step into `palette`.
|
|
46
|
+
* @default null — every line takes `line`
|
|
47
|
+
*/
|
|
48
|
+
steps?: Uint8Array | null;
|
|
49
|
+
/**
|
|
50
|
+
* The colours a step indexes, from `parallelPalette`.
|
|
51
|
+
* @default [] — nothing to index
|
|
52
|
+
*/
|
|
53
|
+
palette?: readonly string[];
|
|
54
|
+
/** The ink a line takes when no quantity is colouring the lines. */
|
|
55
|
+
line: string;
|
|
56
|
+
/** The ink the rows a brush left out are drawn in. */
|
|
57
|
+
excluded: string;
|
|
58
|
+
/**
|
|
59
|
+
* How strongly a kept line is drawn. Turn it down for a dense library.
|
|
60
|
+
* @default PARALLEL_INCLUDED_ALPHA
|
|
61
|
+
*/
|
|
62
|
+
includedAlpha?: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** One row drawn over the mass, and the ink it is drawn in. */
|
|
66
|
+
export interface ParallelHighlight {
|
|
67
|
+
/** The row. */
|
|
68
|
+
row: number;
|
|
69
|
+
/** Its ink. */
|
|
70
|
+
color: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What the highlight pass needs. */
|
|
74
|
+
export interface ParallelHighlightPaint {
|
|
75
|
+
/** The rows to draw, the last one on top. */
|
|
76
|
+
highlights: readonly ParallelHighlight[];
|
|
77
|
+
/** The axes, from left to right. */
|
|
78
|
+
layouts: readonly ParallelAxisLayout[];
|
|
79
|
+
/** Each axis's column, in the same order. */
|
|
80
|
+
values: ReadonlyArray<ArrayLike<number>>;
|
|
81
|
+
/** The ink the halo under each line is drawn in. */
|
|
82
|
+
halo: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Resize a canvas to the device pixel grid and move its origin to the drawing
|
|
87
|
+
* area, which also clears whatever it held.
|
|
88
|
+
* @param canvas - The canvas, or `null` before it is mounted.
|
|
89
|
+
* @param width - Width of the figure, in CSS pixels.
|
|
90
|
+
* @param height - Its height, in CSS pixels.
|
|
91
|
+
* @returns The context, or `null` when there is nothing to draw on.
|
|
92
|
+
*/
|
|
93
|
+
export function preparePlotCanvas(
|
|
94
|
+
canvas: HTMLCanvasElement | null,
|
|
95
|
+
width: number,
|
|
96
|
+
height: number,
|
|
97
|
+
): CanvasRenderingContext2D | null {
|
|
98
|
+
if (canvas === null) return null;
|
|
99
|
+
const context = canvas.getContext('2d');
|
|
100
|
+
if (context === null) return null;
|
|
101
|
+
const pixelRatio = Math.max(globalThis.devicePixelRatio || 1, 1);
|
|
102
|
+
// Assigning the backing store is what clears it; there is no second clear.
|
|
103
|
+
canvas.width = Math.round(width * pixelRatio);
|
|
104
|
+
canvas.height = Math.round(height * pixelRatio);
|
|
105
|
+
context.resetTransform();
|
|
106
|
+
context.scale(pixelRatio, pixelRatio);
|
|
107
|
+
context.translate(PARALLEL_MARGIN.left, PARALLEL_MARGIN.top);
|
|
108
|
+
return context;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Draw one line per row: the ones a brush left out first, underneath, then the
|
|
113
|
+
* kept ones over them.
|
|
114
|
+
*
|
|
115
|
+
* The excluded rows are one single path stroked once, in a light grey rather
|
|
116
|
+
* than a translucent black: overlapping segments of one stroked path
|
|
117
|
+
* accumulate alpha, so a translucent black saturates to black wherever the
|
|
118
|
+
* library is dense, while a light grey is a colour the pile can only converge
|
|
119
|
+
* on.
|
|
120
|
+
* @param context - A context already scaled and moved to the drawing area by {@link preparePlotCanvas}.
|
|
121
|
+
* @param paint - See {@link ParallelPaint}.
|
|
122
|
+
*/
|
|
123
|
+
export function paintParallelLines(
|
|
124
|
+
context: CanvasRenderingContext2D,
|
|
125
|
+
paint: ParallelPaint,
|
|
126
|
+
): void {
|
|
127
|
+
const { layouts, values, count, line, excluded, includedAlpha } = paint;
|
|
128
|
+
if (layouts.length < 2) return;
|
|
129
|
+
const included = paint.included ?? null;
|
|
130
|
+
const steps = paint.steps ?? null;
|
|
131
|
+
const palette = paint.palette ?? [];
|
|
132
|
+
context.lineWidth = 1;
|
|
133
|
+
|
|
134
|
+
if (included !== null) {
|
|
135
|
+
context.strokeStyle = excluded;
|
|
136
|
+
context.beginPath();
|
|
137
|
+
for (let row = 0; row < count; row++) {
|
|
138
|
+
if (included[row] !== 0) continue;
|
|
139
|
+
traceParallelRow(context, row, layouts, values);
|
|
140
|
+
}
|
|
141
|
+
context.stroke();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
context.globalAlpha = includedAlpha ?? PARALLEL_INCLUDED_ALPHA;
|
|
145
|
+
context.strokeStyle = line;
|
|
146
|
+
let previousStep = -1;
|
|
147
|
+
for (let row = 0; row < count; row++) {
|
|
148
|
+
if (included !== null && included[row] === 0) continue;
|
|
149
|
+
if (steps !== null) {
|
|
150
|
+
const step = steps[row] as number;
|
|
151
|
+
if (step !== previousStep) {
|
|
152
|
+
const color = palette[step];
|
|
153
|
+
if (color !== undefined) context.strokeStyle = color;
|
|
154
|
+
previousStep = step;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
context.beginPath();
|
|
158
|
+
traceParallelRow(context, row, layouts, values);
|
|
159
|
+
context.stroke();
|
|
160
|
+
}
|
|
161
|
+
context.globalAlpha = 1;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Draw a few singled-out rows over the mass, each over a halo and dotted at
|
|
166
|
+
* every axis crossing.
|
|
167
|
+
* @param context - A context already scaled and moved to the drawing area.
|
|
168
|
+
* @param paint - See {@link ParallelHighlightPaint}.
|
|
169
|
+
*/
|
|
170
|
+
export function paintParallelHighlights(
|
|
171
|
+
context: CanvasRenderingContext2D,
|
|
172
|
+
paint: ParallelHighlightPaint,
|
|
173
|
+
): void {
|
|
174
|
+
const { highlights, layouts, values, halo } = paint;
|
|
175
|
+
if (layouts.length < 2) return;
|
|
176
|
+
context.lineJoin = 'round';
|
|
177
|
+
context.lineCap = 'round';
|
|
178
|
+
for (const { row, color } of highlights) {
|
|
179
|
+
context.beginPath();
|
|
180
|
+
traceParallelRow(context, row, layouts, values);
|
|
181
|
+
context.strokeStyle = halo;
|
|
182
|
+
context.lineWidth = HALO_WIDTH;
|
|
183
|
+
context.stroke();
|
|
184
|
+
context.strokeStyle = color;
|
|
185
|
+
context.lineWidth = HIGHLIGHT_WIDTH;
|
|
186
|
+
context.stroke();
|
|
187
|
+
|
|
188
|
+
context.fillStyle = color;
|
|
189
|
+
for (let index = 0; index < layouts.length; index++) {
|
|
190
|
+
const layout = layouts[index];
|
|
191
|
+
const column = values[index];
|
|
192
|
+
if (layout === undefined || column === undefined) continue;
|
|
193
|
+
const y = parallelPixelAt(layout.pixels, column[row] as number);
|
|
194
|
+
if (!Number.isFinite(y)) continue;
|
|
195
|
+
context.beginPath();
|
|
196
|
+
context.arc(layout.x, y, HIGHLIGHT_DOT_RADIUS, 0, FULL_TURN);
|
|
197
|
+
context.fill();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Add one row's line to the path being built.
|
|
204
|
+
* @param context - A context already scaled and moved to the drawing area.
|
|
205
|
+
* @param row - The row to trace.
|
|
206
|
+
* @param layouts - The axes, from left to right.
|
|
207
|
+
* @param values - Each axis's column, in the same order.
|
|
208
|
+
*/
|
|
209
|
+
export function traceParallelRow(
|
|
210
|
+
context: CanvasRenderingContext2D,
|
|
211
|
+
row: number,
|
|
212
|
+
layouts: readonly ParallelAxisLayout[],
|
|
213
|
+
values: ReadonlyArray<ArrayLike<number>>,
|
|
214
|
+
): void {
|
|
215
|
+
let started = false;
|
|
216
|
+
for (let index = 0; index < layouts.length; index++) {
|
|
217
|
+
const layout = layouts[index];
|
|
218
|
+
const column = values[index];
|
|
219
|
+
if (layout === undefined || column === undefined) continue;
|
|
220
|
+
const y = parallelPixelAt(layout.pixels, column[row] as number);
|
|
221
|
+
// A row whose value on this axis is not known yet has no point to draw:
|
|
222
|
+
// the line skips that axis rather than collapsing onto one of its ends.
|
|
223
|
+
if (!Number.isFinite(y)) continue;
|
|
224
|
+
if (started) {
|
|
225
|
+
context.lineTo(layout.x, y);
|
|
226
|
+
} else {
|
|
227
|
+
context.moveTo(layout.x, y);
|
|
228
|
+
started = true;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The colour each line is drawn in, as a step into a sampled ramp.
|
|
3
|
+
*
|
|
4
|
+
* A canvas reassigns `strokeStyle` once per line, and a thousand different
|
|
5
|
+
* colours is a thousand style writes. Quantising the ramp to 121 steps and
|
|
6
|
+
* keeping the rows in the order they came means the style is written about
|
|
7
|
+
* 121 times for the whole figure — while no reader can tell a hundred-and-
|
|
8
|
+
* twenty-first of a ramp apart from its neighbour.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { ColorScale } from '../../color/core/interpolate.ts';
|
|
12
|
+
import { evenScale, sampleScale } from '../../color/core/interpolate.ts';
|
|
13
|
+
import { positionInRange } from '../../color/core/scale.ts';
|
|
14
|
+
import { resolveColorScale } from '../../color/core/scaleText.ts';
|
|
15
|
+
import { DEFAULT_COLOR_SCALE_ID } from '../../color/core/scales.ts';
|
|
16
|
+
|
|
17
|
+
import { parallelExtent } from './parallelExtent.ts';
|
|
18
|
+
import type { ParallelColorBy } from './parallelTypes.ts';
|
|
19
|
+
|
|
20
|
+
/** How many steps the ramp is quantised into; there is one more colour than steps. */
|
|
21
|
+
export const PARALLEL_PALETTE_STEPS = 120;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The ramp, sampled into the steps a line's colour is looked up by.
|
|
25
|
+
* @param scale - The ramp, as the registry hands it over or as a plain list of colours. Defaults to viridis.
|
|
26
|
+
* @returns `PARALLEL_PALETTE_STEPS + 1` colours, from the low end to the high end.
|
|
27
|
+
*/
|
|
28
|
+
export function parallelPalette(
|
|
29
|
+
scale?: ColorScale | readonly string[],
|
|
30
|
+
): readonly string[] {
|
|
31
|
+
return sampleScale(resolveRamp(scale), PARALLEL_PALETTE_STEPS + 1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Which step of the ramp each row takes.
|
|
36
|
+
*
|
|
37
|
+
* Worked out once per data change rather than per repaint, and held as bytes:
|
|
38
|
+
* a library of ten thousand rows costs ten kilobytes and the painter's inner
|
|
39
|
+
* loop reads an integer instead of placing a value on a ramp.
|
|
40
|
+
* @param color - The quantity the lines are coloured by.
|
|
41
|
+
* @param count - How many rows there are.
|
|
42
|
+
* @returns One step per row, from `0` to {@link PARALLEL_PALETTE_STEPS}.
|
|
43
|
+
*/
|
|
44
|
+
export function parallelColorSteps(
|
|
45
|
+
color: ParallelColorBy,
|
|
46
|
+
count: number,
|
|
47
|
+
): Uint8Array {
|
|
48
|
+
const steps = new Uint8Array(count);
|
|
49
|
+
const extent = parallelExtent(color.values, count);
|
|
50
|
+
const min = color.min ?? extent.min;
|
|
51
|
+
const max = color.max ?? extent.max;
|
|
52
|
+
const logarithmic = color.logarithmic ?? false;
|
|
53
|
+
const span = max - min;
|
|
54
|
+
const rows = Math.min(count, color.values.length);
|
|
55
|
+
for (let row = 0; row < rows; row++) {
|
|
56
|
+
const value = color.values[row] as number;
|
|
57
|
+
steps[row] = logarithmic
|
|
58
|
+
? Math.round(
|
|
59
|
+
positionInRange(value, min, max, { logarithmic: true }) *
|
|
60
|
+
PARALLEL_PALETTE_STEPS,
|
|
61
|
+
)
|
|
62
|
+
: colorStep(value, min, span);
|
|
63
|
+
}
|
|
64
|
+
return steps;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function resolveRamp(scale?: ColorScale | readonly string[]): ColorScale {
|
|
68
|
+
if (scale === undefined) {
|
|
69
|
+
return resolveColorScale(DEFAULT_COLOR_SCALE_ID).scale;
|
|
70
|
+
}
|
|
71
|
+
return 'stops' in scale ? scale : evenScale(scale);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function colorStep(value: number, min: number, span: number): number {
|
|
75
|
+
if (span <= 0) return 0;
|
|
76
|
+
const ratio = (value - min) / span;
|
|
77
|
+
if (Number.isNaN(ratio) || ratio < 0) return 0;
|
|
78
|
+
if (ratio > 1) return PARALLEL_PALETTE_STEPS;
|
|
79
|
+
return Math.round(ratio * PARALLEL_PALETTE_STEPS);
|
|
80
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How one axis is graduated.
|
|
3
|
+
*
|
|
4
|
+
* Five evenly spaced marks across the raw extent, not `chartAxisScale`'s nice
|
|
5
|
+
* ones: nicing moves the ends of the axis, and a figure whose whole job is to
|
|
6
|
+
* compare rows against each other must end exactly where the data does. A
|
|
7
|
+
* caller who wants round numbers asks for them with `nice`, per axis.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
chartAxisScale,
|
|
12
|
+
chartTickDecimals,
|
|
13
|
+
} from '../../chart/core/chartAxisScale.ts';
|
|
14
|
+
import { formatDecimal } from '../../format/core/numbers.ts';
|
|
15
|
+
import { roundTo } from '../../format/core/roundTo.ts';
|
|
16
|
+
|
|
17
|
+
import type { ParallelAxis, ParallelTick } from './parallelTypes.ts';
|
|
18
|
+
|
|
19
|
+
/** How many graduations an axis carries when the caller asks for no number. */
|
|
20
|
+
export const PARALLEL_TICK_COUNT = 5;
|
|
21
|
+
|
|
22
|
+
/** The most decimals a graduation is ever written with. */
|
|
23
|
+
const MOST_TICK_DECIMALS = 6;
|
|
24
|
+
|
|
25
|
+
/** How far a label may sit from its own value: a tenth of the spacing. */
|
|
26
|
+
const LABEL_TOLERANCE = 10;
|
|
27
|
+
|
|
28
|
+
/** An axis's ends and the graduations written along it. */
|
|
29
|
+
export interface ParallelGraduation {
|
|
30
|
+
/** The two ends of the axis, low first, after any nicing. */
|
|
31
|
+
domain: readonly [number, number];
|
|
32
|
+
/** The graduations, from the bottom of the axis to the top. */
|
|
33
|
+
ticks: readonly ParallelTick[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The graduations of one axis, and the ends they are spread between.
|
|
38
|
+
*
|
|
39
|
+
* Three ways, in order: the caller's own list wins outright — a coded quantity
|
|
40
|
+
* reading `none`, `low`, `high` is nobody else's to invent; `nice` hands the
|
|
41
|
+
* axis to `chartAxisScale`, which rounds both ends outward to whole steps; and
|
|
42
|
+
* otherwise the extent is divided evenly and a mark whose label repeats the
|
|
43
|
+
* one below it is dropped, so an axis of whole numbers from 0 to 2 reads
|
|
44
|
+
* `0 1 2` rather than writing `1` twice.
|
|
45
|
+
* @param axis - The axis, for its formatting and how many marks it wants.
|
|
46
|
+
* @param min - The value at the bottom of the axis.
|
|
47
|
+
* @param max - The value at its top.
|
|
48
|
+
* @returns The ends and the graduations. See {@link ParallelGraduation}.
|
|
49
|
+
*/
|
|
50
|
+
export function parallelTicks(
|
|
51
|
+
axis: ParallelAxis,
|
|
52
|
+
min: number,
|
|
53
|
+
max: number,
|
|
54
|
+
): ParallelGraduation {
|
|
55
|
+
const domain = [min, max] as const;
|
|
56
|
+
if (axis.ticks !== undefined) return { domain, ticks: axis.ticks };
|
|
57
|
+
|
|
58
|
+
const wanted = Math.max(2, Math.trunc(axis.tickCount ?? PARALLEL_TICK_COUNT));
|
|
59
|
+
if (axis.nice === true && axis.scale !== 'log') {
|
|
60
|
+
return nicedGraduation(axis, min, max, wanted);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const values = evenValues(min, max, wanted, axis.scale === 'log');
|
|
64
|
+
return { domain, ticks: labelled(values, axis.format) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function nicedGraduation(
|
|
68
|
+
axis: ParallelAxis,
|
|
69
|
+
min: number,
|
|
70
|
+
max: number,
|
|
71
|
+
wanted: number,
|
|
72
|
+
): ParallelGraduation {
|
|
73
|
+
const scale = chartAxisScale(min, max, { count: wanted, nice: true });
|
|
74
|
+
const ticks: ParallelTick[] = [];
|
|
75
|
+
for (let index = 0; index < scale.values.length; index++) {
|
|
76
|
+
const value = scale.values[index] as number;
|
|
77
|
+
const label =
|
|
78
|
+
axis.format === undefined
|
|
79
|
+
? (scale.labels[index] ?? formatDecimal(value, scale.decimals))
|
|
80
|
+
: axis.format(value);
|
|
81
|
+
if (ticks.at(-1)?.label === label) continue;
|
|
82
|
+
ticks.push({ value, label });
|
|
83
|
+
}
|
|
84
|
+
return { domain: scale.domain, ticks };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function evenValues(
|
|
88
|
+
min: number,
|
|
89
|
+
max: number,
|
|
90
|
+
wanted: number,
|
|
91
|
+
logarithmic: boolean,
|
|
92
|
+
): number[] {
|
|
93
|
+
const values: number[] = [];
|
|
94
|
+
const low = logarithmic ? Math.log10(Math.max(min, smallestStep(max))) : min;
|
|
95
|
+
const high = logarithmic ? Math.log10(Math.max(max, smallestStep(max))) : max;
|
|
96
|
+
const span = high - low;
|
|
97
|
+
for (let index = 0; index < wanted; index++) {
|
|
98
|
+
const placed = low + (span * index) / (wanted - 1);
|
|
99
|
+
values.push(logarithmic ? 10 ** placed : placed);
|
|
100
|
+
}
|
|
101
|
+
return values;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function labelled(
|
|
105
|
+
values: readonly number[],
|
|
106
|
+
format: ((value: number) => string) | undefined,
|
|
107
|
+
): ParallelTick[] {
|
|
108
|
+
const decimals = format === undefined ? tickDecimals(values) : 0;
|
|
109
|
+
const ticks: ParallelTick[] = [];
|
|
110
|
+
for (const value of values) {
|
|
111
|
+
const label =
|
|
112
|
+
format === undefined ? formatDecimal(value, decimals) : format(value);
|
|
113
|
+
if (ticks.at(-1)?.label === label) continue;
|
|
114
|
+
ticks.push({ value, label });
|
|
115
|
+
}
|
|
116
|
+
return ticks;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* As many decimals as the spacing needs, and one more whenever rounding would
|
|
121
|
+
* move a mark by more than a tenth of that spacing — which is what keeps an
|
|
122
|
+
* axis from 12.3 to 157 reading `12 48 85 121 157` while one from −1.5 to 2.5
|
|
123
|
+
* still reads `−1.5 −0.5 0.5 1.5 2.5` rather than rounding both away.
|
|
124
|
+
* @param values - The graduations, in the axis's own units.
|
|
125
|
+
* @returns How many decimals each of their labels carries.
|
|
126
|
+
*/
|
|
127
|
+
function tickDecimals(values: readonly number[]): number {
|
|
128
|
+
const step = smallestGap(values);
|
|
129
|
+
const tolerance = step / LABEL_TOLERANCE;
|
|
130
|
+
let decimals = chartTickDecimals(step);
|
|
131
|
+
while (
|
|
132
|
+
decimals < MOST_TICK_DECIMALS &&
|
|
133
|
+
!faithful(values, decimals, tolerance)
|
|
134
|
+
) {
|
|
135
|
+
decimals++;
|
|
136
|
+
}
|
|
137
|
+
return decimals;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function faithful(
|
|
141
|
+
values: readonly number[],
|
|
142
|
+
decimals: number,
|
|
143
|
+
tolerance: number,
|
|
144
|
+
): boolean {
|
|
145
|
+
for (const value of values) {
|
|
146
|
+
if (Math.abs(value - roundTo(value, decimals)) > tolerance) return false;
|
|
147
|
+
}
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function smallestGap(values: readonly number[]): number {
|
|
152
|
+
let smallest = Number.POSITIVE_INFINITY;
|
|
153
|
+
for (let index = 1; index < values.length; index++) {
|
|
154
|
+
const gap = Math.abs(
|
|
155
|
+
(values[index] as number) - (values[index - 1] as number),
|
|
156
|
+
);
|
|
157
|
+
if (gap > 0 && gap < smallest) smallest = gap;
|
|
158
|
+
}
|
|
159
|
+
return Number.isFinite(smallest) ? smallest : 1;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function smallestStep(max: number): number {
|
|
163
|
+
return max > 0 ? max * 1e-12 : 1;
|
|
164
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a parallel-coordinates figure is asked to draw: one axis per column,
|
|
3
|
+
* the intervals the reader brushed on them, and the quantity the lines are
|
|
4
|
+
* coloured by.
|
|
5
|
+
*
|
|
6
|
+
* The figure itself is index-based — every axis hands over its column as an
|
|
7
|
+
* `ArrayLike<number>` read in place — so a repaint costs an indexed read per
|
|
8
|
+
* value rather than a call. {@link parallelAxisOf} is the one place a row type
|
|
9
|
+
* is named, and it is run once per data change rather than once per repaint.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ColorScale } from '../../color/core/interpolate.ts';
|
|
13
|
+
|
|
14
|
+
/** How a value is placed along an axis. */
|
|
15
|
+
export type ParallelAxisScale = 'linear' | 'log';
|
|
16
|
+
|
|
17
|
+
/** One labelled graduation a caller writes itself. */
|
|
18
|
+
export interface ParallelTick {
|
|
19
|
+
/** Where it sits, in the axis's own units. */
|
|
20
|
+
value: number;
|
|
21
|
+
/** What is written beside it. */
|
|
22
|
+
label: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** One vertical axis of a parallel-coordinates figure, and the column it draws. */
|
|
26
|
+
export interface ParallelAxis {
|
|
27
|
+
/**
|
|
28
|
+
* A stable key, which is how a brushed range is reported and how a caller
|
|
29
|
+
* names the axis the colour is read from. Never an index: axes are added and
|
|
30
|
+
* reordered, and a range keyed on a position outlives the axis it belonged
|
|
31
|
+
* to.
|
|
32
|
+
*/
|
|
33
|
+
id: string;
|
|
34
|
+
/** What it is called, written over the axis. */
|
|
35
|
+
label: string;
|
|
36
|
+
/**
|
|
37
|
+
* Every row's value, in the figure's row order. Read in place — nothing is
|
|
38
|
+
* copied — so a `Float64Array` is the cheapest thing to pass.
|
|
39
|
+
*/
|
|
40
|
+
values: ArrayLike<number>;
|
|
41
|
+
/**
|
|
42
|
+
* The two ends of the axis, low first. Left out, they are the column's own
|
|
43
|
+
* smallest and largest finite value, and a column with no spread at all is
|
|
44
|
+
* opened by half a unit on each side so that it still draws.
|
|
45
|
+
* @default undefined — the column's own range
|
|
46
|
+
*/
|
|
47
|
+
domain?: readonly [number, number];
|
|
48
|
+
/**
|
|
49
|
+
* How values are placed between the ends. `log` needs a domain whose low end
|
|
50
|
+
* is above zero; a value at or below zero is pinned to the low end.
|
|
51
|
+
* @default 'linear'
|
|
52
|
+
*/
|
|
53
|
+
scale?: ParallelAxisScale;
|
|
54
|
+
/**
|
|
55
|
+
* How a tick is written. Left out, the ticks carry as many decimals as the
|
|
56
|
+
* spacing needs.
|
|
57
|
+
* @default undefined
|
|
58
|
+
*/
|
|
59
|
+
format?: (value: number) => string;
|
|
60
|
+
/**
|
|
61
|
+
* The graduations, written out by the caller — what a coded quantity needs,
|
|
62
|
+
* where `0`, `1`, `2` should read `none`, `low`, `high`.
|
|
63
|
+
* @default undefined — the figure graduates the axis itself
|
|
64
|
+
*/
|
|
65
|
+
ticks?: readonly ParallelTick[];
|
|
66
|
+
/**
|
|
67
|
+
* Roughly how many graduations to aim for, when the figure picks them.
|
|
68
|
+
* @default 5
|
|
69
|
+
*/
|
|
70
|
+
tickCount?: number;
|
|
71
|
+
/**
|
|
72
|
+
* Whether the ends are rounded outward to whole tick steps. Off, so the axis
|
|
73
|
+
* ends exactly on the data — a reader comparing two molecules on a drug
|
|
74
|
+
* score must see the best one touch the top of its axis, not float below a
|
|
75
|
+
* round number nothing reached.
|
|
76
|
+
* @default false
|
|
77
|
+
*/
|
|
78
|
+
nice?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* The unit written after the axis name, e.g. `g/mol`.
|
|
81
|
+
* @default '' — nothing is written
|
|
82
|
+
*/
|
|
83
|
+
unit?: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** An interval kept on one axis, in that axis's own units, low first. */
|
|
87
|
+
export type ParallelRange = readonly [number, number];
|
|
88
|
+
|
|
89
|
+
/** Which rows a set of brushes keeps, keyed by axis id. */
|
|
90
|
+
export type ParallelRanges = Readonly<Record<string, ParallelRange | null>>;
|
|
91
|
+
|
|
92
|
+
/** How the lines are coloured. */
|
|
93
|
+
export interface ParallelColorBy {
|
|
94
|
+
/** The quantity read for each row, in the figure's row order. */
|
|
95
|
+
values: ArrayLike<number>;
|
|
96
|
+
/**
|
|
97
|
+
* The value at the low end of the ramp.
|
|
98
|
+
* @default the smallest finite value
|
|
99
|
+
*/
|
|
100
|
+
min?: number;
|
|
101
|
+
/**
|
|
102
|
+
* The value at its high end.
|
|
103
|
+
* @default the largest finite value
|
|
104
|
+
*/
|
|
105
|
+
max?: number;
|
|
106
|
+
/**
|
|
107
|
+
* The ramp, as the registry hands it over or as a plain list of colours.
|
|
108
|
+
* @default viridis — `resolveColorScale(DEFAULT_COLOR_SCALE_ID).scale`
|
|
109
|
+
*/
|
|
110
|
+
scale?: ColorScale | readonly string[];
|
|
111
|
+
/**
|
|
112
|
+
* Whether the quantity is read on a base-10 logarithmic ramp.
|
|
113
|
+
* @default false
|
|
114
|
+
*/
|
|
115
|
+
logarithmic?: boolean;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** An axis described by the field to read off each row, rather than by its column. */
|
|
119
|
+
export type ParallelAxisSource<Row> = Omit<ParallelAxis, 'values'> & {
|
|
120
|
+
/**
|
|
121
|
+
* The value this axis reads off one row. Called once per row per data
|
|
122
|
+
* change, never per repaint.
|
|
123
|
+
*/
|
|
124
|
+
readonly value: (row: Row) => number;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* One axis built by reading a field off each row.
|
|
129
|
+
*
|
|
130
|
+
* The one place a row type is named: the figure itself reads numbers out of a
|
|
131
|
+
* typed array, so a repaint costs an indexed read rather than a call per
|
|
132
|
+
* value. Call it inside a `useMemo` keyed on the rows.
|
|
133
|
+
* @param rows - The rows, in the order the figure will index them.
|
|
134
|
+
* @param axis - The axis, with the field to read instead of its values.
|
|
135
|
+
* @returns The axis, with its values filled in.
|
|
136
|
+
*/
|
|
137
|
+
export function parallelAxisOf<Row>(
|
|
138
|
+
rows: readonly Row[],
|
|
139
|
+
axis: ParallelAxisSource<Row>,
|
|
140
|
+
): ParallelAxis {
|
|
141
|
+
const { value, ...rest } = axis;
|
|
142
|
+
const values = new Float64Array(rows.length);
|
|
143
|
+
for (let row = 0; row < rows.length; row++) {
|
|
144
|
+
values[row] = value(rows[row] as Row);
|
|
145
|
+
}
|
|
146
|
+
return { ...rest, values };
|
|
147
|
+
}
|