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,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a caller hands a `ParallelCoordinates`.
|
|
3
|
+
*
|
|
4
|
+
* The types the figure is *told* about — an axis, an interval, a colour ramp —
|
|
5
|
+
* live in `parallel/core`, because the same words describe the mask a table
|
|
6
|
+
* beside the figure filters on and the layout the painter reads. What is left
|
|
7
|
+
* here is what only a React component has: the render props, the inks a canvas
|
|
8
|
+
* cannot read off a stylesheet, and the controlled pairs.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { ReactNode } from 'react';
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
ParallelAxis,
|
|
15
|
+
ParallelColorBy,
|
|
16
|
+
ParallelRange,
|
|
17
|
+
ParallelRanges,
|
|
18
|
+
} from '../core/parallelTypes.ts';
|
|
19
|
+
|
|
20
|
+
/** What the pointer is over, and where. */
|
|
21
|
+
export interface ParallelHover {
|
|
22
|
+
/** The row, as an index into the axes' value arrays. */
|
|
23
|
+
index: number;
|
|
24
|
+
/** Where the pointer is, in pixels from the figure's left edge. */
|
|
25
|
+
x: number;
|
|
26
|
+
/** Where it is, in pixels from its top edge. */
|
|
27
|
+
y: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The colours a canvas is painted with, which cannot read a custom property. */
|
|
31
|
+
export interface ParallelInk {
|
|
32
|
+
/**
|
|
33
|
+
* The rows a brush left out, drawn under the rest.
|
|
34
|
+
* @default a light grey the dense mass can only converge on
|
|
35
|
+
*/
|
|
36
|
+
excluded?: string;
|
|
37
|
+
/**
|
|
38
|
+
* The rows nothing is colouring, when no quantity was given.
|
|
39
|
+
* @default the value of `--text-muted` on the figure
|
|
40
|
+
*/
|
|
41
|
+
line?: string;
|
|
42
|
+
/**
|
|
43
|
+
* The row under the pointer.
|
|
44
|
+
* @default the value of `--text` on the figure
|
|
45
|
+
*/
|
|
46
|
+
hover?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The rows singled out elsewhere — picked in a table, pinned by the reader.
|
|
49
|
+
* @default the value of `--accent` on the figure
|
|
50
|
+
*/
|
|
51
|
+
selection?: string;
|
|
52
|
+
/**
|
|
53
|
+
* The halo a singled-out line is drawn over, so it reads against the mass.
|
|
54
|
+
* @default the value of `--surface`, at 85%
|
|
55
|
+
*/
|
|
56
|
+
halo?: string;
|
|
57
|
+
/**
|
|
58
|
+
* The axis lines, their ticks and their labels.
|
|
59
|
+
* @default the value of `--text-muted`
|
|
60
|
+
*/
|
|
61
|
+
axis?: string;
|
|
62
|
+
/**
|
|
63
|
+
* How strongly a kept line is drawn. Turn it down for a dense library.
|
|
64
|
+
* @default 0.35
|
|
65
|
+
*/
|
|
66
|
+
includedAlpha?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** What `ParallelCoordinates` needs. */
|
|
70
|
+
export interface ParallelCoordinatesProps {
|
|
71
|
+
/** The axes, from left to right. Fewer than two draws no lines. */
|
|
72
|
+
axes: readonly ParallelAxis[];
|
|
73
|
+
/**
|
|
74
|
+
* How many rows to draw.
|
|
75
|
+
* @default the shortest axis's value count
|
|
76
|
+
*/
|
|
77
|
+
count?: number;
|
|
78
|
+
/** Total width, in pixels, normally from `useContainerSize`. */
|
|
79
|
+
width: number;
|
|
80
|
+
/**
|
|
81
|
+
* Total height, axis names and tick labels included.
|
|
82
|
+
* @default 320
|
|
83
|
+
*/
|
|
84
|
+
height?: number;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The interval each axis keeps, keyed by axis id. Present, the caller owns
|
|
88
|
+
* the brushes — which is what lets a table beside the figure filter on the
|
|
89
|
+
* same ranges. The band being dragged stays inside the component either way,
|
|
90
|
+
* so a controlled parent is never asked to re-render sixty times a second.
|
|
91
|
+
* @default undefined — the figure keeps its own
|
|
92
|
+
*/
|
|
93
|
+
ranges?: ParallelRanges;
|
|
94
|
+
/**
|
|
95
|
+
* The intervals in force before the reader touches anything.
|
|
96
|
+
* @default undefined — no axis is brushed
|
|
97
|
+
*/
|
|
98
|
+
defaultRanges?: ParallelRanges;
|
|
99
|
+
/**
|
|
100
|
+
* Called when a brush is released, with the interval it keeps or `null` when
|
|
101
|
+
* it was cleared — never while the band is being dragged.
|
|
102
|
+
* @default undefined
|
|
103
|
+
*/
|
|
104
|
+
onRangeChange?: (axisId: string, range: ParallelRange | null) => void;
|
|
105
|
+
/**
|
|
106
|
+
* Called on every frame a band grows, for a caption saying what letting go
|
|
107
|
+
* would keep. Nothing else should be wired to it.
|
|
108
|
+
* @default undefined
|
|
109
|
+
*/
|
|
110
|
+
onRangePreview?: (axisId: string, range: ParallelRange | null) => void;
|
|
111
|
+
/**
|
|
112
|
+
* Which rows the brushes keep, one entry per row, a zero meaning excluded.
|
|
113
|
+
* Left out, it is worked out from the ranges with `parallelIncludedMask`,
|
|
114
|
+
* which is exported so a table beside the figure filters on the same answer
|
|
115
|
+
* rather than on a second definition of "kept".
|
|
116
|
+
* @default undefined — worked out from the ranges
|
|
117
|
+
*/
|
|
118
|
+
included?: Uint8Array;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The row under the pointer, or `-1`. Present, the caller owns it, which is
|
|
122
|
+
* what lets a table hovered elsewhere light up its line here.
|
|
123
|
+
* @default undefined — the figure keeps its own
|
|
124
|
+
*/
|
|
125
|
+
hovered?: number;
|
|
126
|
+
/**
|
|
127
|
+
* Called with the row under the pointer, or `-1`, whenever that changes.
|
|
128
|
+
* @default undefined
|
|
129
|
+
*/
|
|
130
|
+
onHoverChange?: (index: number) => void;
|
|
131
|
+
/**
|
|
132
|
+
* The rows singled out elsewhere, drawn over the mass in the selection ink.
|
|
133
|
+
* A row that is also the hovered one is drawn once, in the hover ink.
|
|
134
|
+
* @default undefined — nothing is singled out
|
|
135
|
+
*/
|
|
136
|
+
selected?: readonly number[];
|
|
137
|
+
/**
|
|
138
|
+
* Called when a line is clicked, with the row, or `-1` for empty ground.
|
|
139
|
+
* @default undefined — the lines are not clickable
|
|
140
|
+
*/
|
|
141
|
+
onRowClick?: (index: number) => void;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The quantity the lines are coloured by.
|
|
145
|
+
* @default undefined — every line takes one ink
|
|
146
|
+
*/
|
|
147
|
+
color?: ParallelColorBy;
|
|
148
|
+
/**
|
|
149
|
+
* The id of the axis the lines are coloured by, for the common case where it
|
|
150
|
+
* is one of the axes already drawn. Ignored when `color` is given.
|
|
151
|
+
* @default undefined
|
|
152
|
+
*/
|
|
153
|
+
colorAxis?: string;
|
|
154
|
+
/**
|
|
155
|
+
* The canvas colours, merged over the ones read off the figure's own tokens.
|
|
156
|
+
* @default {}
|
|
157
|
+
*/
|
|
158
|
+
ink?: ParallelInk;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The card shown over the row under the pointer. It is placed by the figure,
|
|
162
|
+
* flipped rather than clipped at either edge, so a caller writes only what
|
|
163
|
+
* it says.
|
|
164
|
+
* @default undefined — no card is shown
|
|
165
|
+
*/
|
|
166
|
+
renderTooltip?: (hover: ParallelHover) => ReactNode;
|
|
167
|
+
/**
|
|
168
|
+
* How an axis is named. The default writes the label and its unit; a caller
|
|
169
|
+
* wraps it to hang a help tooltip off the name.
|
|
170
|
+
* @default the axis label, and its unit after it
|
|
171
|
+
*/
|
|
172
|
+
renderAxisLabel?: (axis: ParallelAxis, index: number) => ReactNode;
|
|
173
|
+
/**
|
|
174
|
+
* What is shown in place of the figure when there is nothing to draw.
|
|
175
|
+
* @default undefined — an empty figure of the given height
|
|
176
|
+
*/
|
|
177
|
+
empty?: ReactNode;
|
|
178
|
+
/**
|
|
179
|
+
* What floats over the figure — a legend, a colour scale key, a caption.
|
|
180
|
+
* @default undefined
|
|
181
|
+
*/
|
|
182
|
+
overlay?: ReactNode;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* What a screen reader is told the figure shows.
|
|
186
|
+
* @default a sentence naming the axes and the row count
|
|
187
|
+
*/
|
|
188
|
+
label?: string;
|
|
189
|
+
/**
|
|
190
|
+
* A class for the figure's outermost element, for a site placing it.
|
|
191
|
+
* @default undefined
|
|
192
|
+
*/
|
|
193
|
+
className?: string;
|
|
194
|
+
/**
|
|
195
|
+
* Value of the `data-testid` attribute of the wrapper.
|
|
196
|
+
* @default undefined
|
|
197
|
+
*/
|
|
198
|
+
testId?: string;
|
|
199
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The questions the gesture asks that need neither React nor state: where a
|
|
3
|
+
* press landed, which axis it reached, and which band each axis should be
|
|
4
|
+
* drawn with once the one being dragged is taken into account.
|
|
5
|
+
*
|
|
6
|
+
* Kept out of the hook the way `lassoGesture.ts` is kept out of its own — each
|
|
7
|
+
* of these has exactly one answer, and none of them needs a render to be read.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { SurfaceEvent } from '../../scatter/ui/lassoGesture.ts';
|
|
11
|
+
import { surfacePosition } from '../../scatter/ui/lassoGesture.ts';
|
|
12
|
+
import type { ParallelAxisLayout } from '../core/parallelAxes.ts';
|
|
13
|
+
import { PARALLEL_MARGIN } from '../core/parallelAxes.ts';
|
|
14
|
+
import type { ParallelBand } from '../core/parallelBrush.ts';
|
|
15
|
+
import {
|
|
16
|
+
PARALLEL_BRUSH_HALF_WIDTH,
|
|
17
|
+
parallelBandOf,
|
|
18
|
+
} from '../core/parallelBrush.ts';
|
|
19
|
+
import type { ParallelRanges } from '../core/parallelTypes.ts';
|
|
20
|
+
|
|
21
|
+
/** A band being dragged, before the interval it keeps is reported. */
|
|
22
|
+
export interface ParallelDraft {
|
|
23
|
+
/** The axis it is on. */
|
|
24
|
+
axis: string;
|
|
25
|
+
/** Where it stands this frame. */
|
|
26
|
+
band: ParallelBand;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Where an event landed, relative to the top left of the drawing area.
|
|
31
|
+
* @param event - The event, for where on the surface it happened.
|
|
32
|
+
* @returns The position, in pixels from the corner of the drawing area.
|
|
33
|
+
*/
|
|
34
|
+
export function parallelPointOf(event: SurfaceEvent): {
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
} {
|
|
38
|
+
const position = surfacePosition(event, 0, 0);
|
|
39
|
+
return {
|
|
40
|
+
x: position.x - PARALLEL_MARGIN.left,
|
|
41
|
+
y: position.y - PARALLEL_MARGIN.top,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Which axis a press at that horizontal position took hold of.
|
|
47
|
+
* @param x - Pixels from the left of the drawing area.
|
|
48
|
+
* @param axes - The axes, from left to right.
|
|
49
|
+
* @returns The axis's index, or `-1` when the press landed between two of them.
|
|
50
|
+
*/
|
|
51
|
+
export function parallelAxisNear(
|
|
52
|
+
x: number,
|
|
53
|
+
axes: readonly ParallelAxisLayout[],
|
|
54
|
+
): number {
|
|
55
|
+
let nearest = -1;
|
|
56
|
+
let nearestDistance = PARALLEL_BRUSH_HALF_WIDTH;
|
|
57
|
+
for (let index = 0; index < axes.length; index++) {
|
|
58
|
+
const axis = axes[index];
|
|
59
|
+
if (axis === undefined) continue;
|
|
60
|
+
const distance = Math.abs(x - axis.x);
|
|
61
|
+
if (distance <= nearestDistance) {
|
|
62
|
+
nearestDistance = distance;
|
|
63
|
+
nearest = index;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return nearest;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* One axis, by the id a range is keyed on.
|
|
71
|
+
* @param axes - The axes, from left to right.
|
|
72
|
+
* @param axisId - The id to find.
|
|
73
|
+
* @returns The axis, or `undefined` when it is no longer drawn.
|
|
74
|
+
*/
|
|
75
|
+
export function parallelAxisById(
|
|
76
|
+
axes: readonly ParallelAxisLayout[],
|
|
77
|
+
axisId: string,
|
|
78
|
+
): ParallelAxisLayout | undefined {
|
|
79
|
+
for (const axis of axes) {
|
|
80
|
+
if (axis.id === axisId) return axis;
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The band to draw on each axis: the one being dragged where there is one, and
|
|
87
|
+
* the interval in force everywhere else.
|
|
88
|
+
* @param axes - The axes, from left to right.
|
|
89
|
+
* @param ranges - The interval each axis keeps, or `null` for none.
|
|
90
|
+
* @param draft - The band being dragged, or `null` when nothing is.
|
|
91
|
+
* @returns The bands, keyed by axis id; an axis with none is left out.
|
|
92
|
+
*/
|
|
93
|
+
export function parallelBandsOf(
|
|
94
|
+
axes: readonly ParallelAxisLayout[],
|
|
95
|
+
ranges: ParallelRanges,
|
|
96
|
+
draft: ParallelDraft | null,
|
|
97
|
+
): ReadonlyMap<string, ParallelBand> {
|
|
98
|
+
const drawn = new Map<string, ParallelBand>();
|
|
99
|
+
for (const axis of axes) {
|
|
100
|
+
if (draft !== null && draft.axis === axis.id) {
|
|
101
|
+
drawn.set(axis.id, draft.band);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const range = ranges[axis.id];
|
|
105
|
+
if (range === null || range === undefined) continue;
|
|
106
|
+
drawn.set(axis.id, parallelBandOf(range, axis));
|
|
107
|
+
}
|
|
108
|
+
return drawn;
|
|
109
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The colours the canvas is painted with, read off the figure itself.
|
|
3
|
+
*
|
|
4
|
+
* A canvas takes a colour, not a CSS declaration, so `var(--accent)` means
|
|
5
|
+
* nothing to it — and the family's accent is the one colour a site owns, so it
|
|
6
|
+
* cannot be written into a shared library either. The figure therefore asks
|
|
7
|
+
* the browser what its own tokens resolved to, once it is on the page, and
|
|
8
|
+
* paints with the answer.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { useCallback, useMemo, useState } from 'react';
|
|
12
|
+
|
|
13
|
+
import { parseHexColor } from '../../color/core/hex.ts';
|
|
14
|
+
import { FAMILY_TOKEN_VALUES } from '../../tokens/core/familyTokens.ts';
|
|
15
|
+
import { PARALLEL_INCLUDED_ALPHA } from '../core/parallelPaint.ts';
|
|
16
|
+
|
|
17
|
+
import type { ParallelInk } from './parallelCoordinatesProps.ts';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The rows a brush left out. A light grey rather than a translucent black:
|
|
21
|
+
* overlapping segments of one stroked path accumulate alpha, so a translucent
|
|
22
|
+
* black saturates wherever the library is dense, while this is the colour the
|
|
23
|
+
* pile can only converge on.
|
|
24
|
+
*/
|
|
25
|
+
const EXCLUDED = 'rgba(171, 179, 191, 0.16)';
|
|
26
|
+
|
|
27
|
+
/** How solid the halo under a singled-out line is. */
|
|
28
|
+
const HALO_ALPHA = 0.85;
|
|
29
|
+
|
|
30
|
+
/** The halo a page that declares no surface colour still gets. */
|
|
31
|
+
const WHITE_HALO = `rgba(255, 255, 255, ${HALO_ALPHA})`;
|
|
32
|
+
|
|
33
|
+
/** The four tokens the figure paints from, as the page resolved them. */
|
|
34
|
+
export interface ParallelTokenInk {
|
|
35
|
+
/** What `--text` resolved to. */
|
|
36
|
+
text: string;
|
|
37
|
+
/** What `--text-muted` resolved to. */
|
|
38
|
+
textMuted: string;
|
|
39
|
+
/** What `--surface` resolved to. */
|
|
40
|
+
surface: string;
|
|
41
|
+
/** What `--accent` resolved to, or the text colour when a site declares none. */
|
|
42
|
+
accent: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Every colour the figure paints with, resolved to something a canvas takes. */
|
|
46
|
+
export interface ParallelInkValues {
|
|
47
|
+
/** The rows a brush left out. */
|
|
48
|
+
excluded: string;
|
|
49
|
+
/** The rows nothing is colouring. */
|
|
50
|
+
line: string;
|
|
51
|
+
/** The row under the pointer. */
|
|
52
|
+
hover: string;
|
|
53
|
+
/** The rows singled out elsewhere. */
|
|
54
|
+
selection: string;
|
|
55
|
+
/** The halo a singled-out line is drawn over. */
|
|
56
|
+
halo: string;
|
|
57
|
+
/** The axis lines, their ticks and their labels. */
|
|
58
|
+
axis: string;
|
|
59
|
+
/** How strongly a kept line is drawn. */
|
|
60
|
+
includedAlpha: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* What the figure's own tokens resolved to.
|
|
65
|
+
* @param element - The figure, once it is on the page, or `null` before it is.
|
|
66
|
+
* @returns The four colours. Off the page — a server render, a test — the
|
|
67
|
+
* family's own token values stand in, so the figure never paints with nothing.
|
|
68
|
+
*/
|
|
69
|
+
export function readParallelTokens(element: Element | null): ParallelTokenInk {
|
|
70
|
+
const text = tokenColor(element, '--text', FAMILY_TOKEN_VALUES['--text']);
|
|
71
|
+
return {
|
|
72
|
+
text,
|
|
73
|
+
textMuted: tokenColor(
|
|
74
|
+
element,
|
|
75
|
+
'--text-muted',
|
|
76
|
+
FAMILY_TOKEN_VALUES['--text-muted'],
|
|
77
|
+
),
|
|
78
|
+
surface: tokenColor(element, '--surface', FAMILY_TOKEN_VALUES['--surface']),
|
|
79
|
+
// A site that declares no accent still singles a row out, in its ink.
|
|
80
|
+
accent: tokenColor(element, '--accent', text),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The inks to paint with, from the page's tokens and the caller's overrides.
|
|
86
|
+
* @param tokens - What the figure's own tokens resolved to.
|
|
87
|
+
* @param ink - What the caller insists on, over every one of them.
|
|
88
|
+
* @returns The inks.
|
|
89
|
+
*/
|
|
90
|
+
export function parallelInkFrom(
|
|
91
|
+
tokens: ParallelTokenInk,
|
|
92
|
+
ink: ParallelInk = {},
|
|
93
|
+
): ParallelInkValues {
|
|
94
|
+
return {
|
|
95
|
+
excluded: ink.excluded ?? EXCLUDED,
|
|
96
|
+
line: ink.line ?? tokens.textMuted,
|
|
97
|
+
hover: ink.hover ?? tokens.text,
|
|
98
|
+
selection: ink.selection ?? tokens.accent,
|
|
99
|
+
halo: ink.halo ?? haloOf(tokens.surface),
|
|
100
|
+
axis: ink.axis ?? tokens.textMuted,
|
|
101
|
+
includedAlpha: ink.includedAlpha ?? PARALLEL_INCLUDED_ALPHA,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The inks to paint with, read off one element.
|
|
107
|
+
* @param element - The figure, once it is on the page, or `null` before it is.
|
|
108
|
+
* @param ink - What the caller insists on, over everything read off the page.
|
|
109
|
+
* @returns The inks.
|
|
110
|
+
*/
|
|
111
|
+
export function readParallelInk(
|
|
112
|
+
element: Element | null,
|
|
113
|
+
ink: ParallelInk = {},
|
|
114
|
+
): ParallelInkValues {
|
|
115
|
+
return parallelInkFrom(readParallelTokens(element), ink);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The inks, and the ref that has to reach the figure for them to be read. */
|
|
119
|
+
export interface ParallelInkBinding {
|
|
120
|
+
/** The colours to paint with. */
|
|
121
|
+
ink: ParallelInkValues;
|
|
122
|
+
/** Put on the figure's outermost element. */
|
|
123
|
+
ref: (element: Element | null) => void;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The inks, with the page's own tokens read the moment the figure is mounted.
|
|
128
|
+
*
|
|
129
|
+
* A callback ref rather than an effect, because the question is asked once, of
|
|
130
|
+
* the element itself, and there is nothing to keep in step afterwards.
|
|
131
|
+
* @param ink - What the caller insists on.
|
|
132
|
+
* @returns The inks and the ref. See {@link ParallelInkBinding}.
|
|
133
|
+
*/
|
|
134
|
+
export function useParallelInk(ink: ParallelInk = {}): ParallelInkBinding {
|
|
135
|
+
const [tokens, setTokens] = useState<ParallelTokenInk>(readFamilyTokens);
|
|
136
|
+
const { excluded, line, hover, selection, halo, axis, includedAlpha } = ink;
|
|
137
|
+
|
|
138
|
+
const ref = useCallback((element: Element | null) => {
|
|
139
|
+
if (element !== null) setTokens(readParallelTokens(element));
|
|
140
|
+
}, []);
|
|
141
|
+
|
|
142
|
+
const resolved = useMemo(
|
|
143
|
+
() =>
|
|
144
|
+
parallelInkFrom(tokens, {
|
|
145
|
+
excluded,
|
|
146
|
+
line,
|
|
147
|
+
hover,
|
|
148
|
+
selection,
|
|
149
|
+
halo,
|
|
150
|
+
axis,
|
|
151
|
+
includedAlpha,
|
|
152
|
+
}),
|
|
153
|
+
[tokens, excluded, line, hover, selection, halo, axis, includedAlpha],
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
return { ink: resolved, ref };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function readFamilyTokens(): ParallelTokenInk {
|
|
160
|
+
return readParallelTokens(null);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function tokenColor(
|
|
164
|
+
element: Element | null,
|
|
165
|
+
name: string,
|
|
166
|
+
fallback: string,
|
|
167
|
+
): string {
|
|
168
|
+
const resolve = globalThis.getComputedStyle;
|
|
169
|
+
if (element === null || typeof resolve !== 'function') return fallback;
|
|
170
|
+
const declared = resolve(element).getPropertyValue(name).trim();
|
|
171
|
+
return declared === '' ? fallback : declared;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function haloOf(surface: string): string {
|
|
175
|
+
try {
|
|
176
|
+
const { red, green, blue } = parseHexColor(surface);
|
|
177
|
+
return `rgba(${red}, ${green}, ${blue}, ${HALO_ALPHA})`;
|
|
178
|
+
} catch {
|
|
179
|
+
// A surface written as anything but a hex colour still gets a halo.
|
|
180
|
+
return WHITE_HALO;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The small answers the figure needs before it renders: how many rows there
|
|
3
|
+
* are, what is colouring the lines, which rows are drawn over the mass, and
|
|
4
|
+
* what a screen reader is told.
|
|
5
|
+
*
|
|
6
|
+
* Kept out of the component for the reason `scatterPlotModel.ts` is: none of
|
|
7
|
+
* them needs React to be read or checked, and a component that is mostly
|
|
8
|
+
* arithmetic is a component nobody can see the layout in.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { formatInteger } from '../../format/core/numbers.ts';
|
|
12
|
+
import type { ParallelHighlight } from '../core/parallelPaint.ts';
|
|
13
|
+
import type {
|
|
14
|
+
ParallelAxis,
|
|
15
|
+
ParallelColorBy,
|
|
16
|
+
ParallelRanges,
|
|
17
|
+
} from '../core/parallelTypes.ts';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* How many rows the figure draws.
|
|
21
|
+
* @param axes - The axes, whose columns are read.
|
|
22
|
+
* @param count - What the caller insists on, if anything.
|
|
23
|
+
* @returns The count — the shortest column, so no axis is ever read past its
|
|
24
|
+
* own end.
|
|
25
|
+
*/
|
|
26
|
+
export function parallelRowCount(
|
|
27
|
+
axes: readonly ParallelAxis[],
|
|
28
|
+
count?: number,
|
|
29
|
+
): number {
|
|
30
|
+
if (count !== undefined) return Math.max(0, Math.trunc(count));
|
|
31
|
+
let shortest = 0;
|
|
32
|
+
for (let index = 0; index < axes.length; index++) {
|
|
33
|
+
const axis = axes[index];
|
|
34
|
+
if (axis === undefined) continue;
|
|
35
|
+
const length = axis.values.length;
|
|
36
|
+
if (index === 0 || length < shortest) shortest = length;
|
|
37
|
+
}
|
|
38
|
+
return shortest;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The quantity the lines are coloured by.
|
|
43
|
+
* @param color - The quantity the caller gave outright.
|
|
44
|
+
* @param colorAxis - The id of an axis already drawn, for the common case.
|
|
45
|
+
* @param axes - The axes, to find that id among.
|
|
46
|
+
* @returns The quantity, or `undefined` when the lines all take one ink. An
|
|
47
|
+
* axis named by `colorAxis` lends its own domain as well as its values, so the
|
|
48
|
+
* ramp and the axis beside it read the same range.
|
|
49
|
+
*/
|
|
50
|
+
export function parallelColorOf(
|
|
51
|
+
color: ParallelColorBy | undefined,
|
|
52
|
+
colorAxis: string | undefined,
|
|
53
|
+
axes: readonly ParallelAxis[],
|
|
54
|
+
): ParallelColorBy | undefined {
|
|
55
|
+
if (color !== undefined) return color;
|
|
56
|
+
if (colorAxis === undefined) return undefined;
|
|
57
|
+
for (const axis of axes) {
|
|
58
|
+
if (axis.id !== colorAxis) continue;
|
|
59
|
+
const domain = axis.domain;
|
|
60
|
+
if (domain === undefined) return { values: axis.values };
|
|
61
|
+
return {
|
|
62
|
+
values: axis.values,
|
|
63
|
+
min: Math.min(domain[0], domain[1]),
|
|
64
|
+
max: Math.max(domain[0], domain[1]),
|
|
65
|
+
logarithmic: axis.scale === 'log',
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether any axis on the figure carries an interval at all.
|
|
73
|
+
* @param axes - The axes, whose ids the intervals are keyed by.
|
|
74
|
+
* @param ranges - The interval each axis keeps.
|
|
75
|
+
* @returns Whether anything is brushed, which is what decides whether a line
|
|
76
|
+
* is ever drawn in the excluded ink.
|
|
77
|
+
*/
|
|
78
|
+
export function parallelIsBrushed(
|
|
79
|
+
axes: readonly ParallelAxis[],
|
|
80
|
+
ranges: ParallelRanges,
|
|
81
|
+
): boolean {
|
|
82
|
+
for (const axis of axes) {
|
|
83
|
+
const range = ranges[axis.id];
|
|
84
|
+
if (range !== null && range !== undefined) return true;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The rows drawn over the mass, in the order they are painted.
|
|
91
|
+
* @param selected - The rows singled out elsewhere.
|
|
92
|
+
* @param hovered - The row under the pointer, or `-1`.
|
|
93
|
+
* @param selectionInk - The ink a singled-out row takes.
|
|
94
|
+
* @param hoverInk - The ink the hovered row takes.
|
|
95
|
+
* @returns The highlights, the hovered row last so that it paints on top and
|
|
96
|
+
* a row that is both selected and hovered is drawn once rather than twice.
|
|
97
|
+
*/
|
|
98
|
+
export function parallelHighlightsOf(
|
|
99
|
+
selected: readonly number[] | undefined,
|
|
100
|
+
hovered: number,
|
|
101
|
+
selectionInk: string,
|
|
102
|
+
hoverInk: string,
|
|
103
|
+
): ParallelHighlight[] {
|
|
104
|
+
const highlights: ParallelHighlight[] = [];
|
|
105
|
+
if (selected !== undefined) {
|
|
106
|
+
for (const row of selected) {
|
|
107
|
+
if (row < 0 || row === hovered) continue;
|
|
108
|
+
highlights.push({ row, color: selectionInk });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (hovered >= 0) highlights.push({ row: hovered, color: hoverInk });
|
|
112
|
+
return highlights;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* What a screen reader is told the figure shows.
|
|
117
|
+
* @param axes - The axes, from left to right.
|
|
118
|
+
* @param count - How many rows are drawn.
|
|
119
|
+
* @returns The sentence, naming every axis and how much is on the figure.
|
|
120
|
+
*/
|
|
121
|
+
export function parallelFigureLabel(
|
|
122
|
+
axes: readonly ParallelAxis[],
|
|
123
|
+
count: number,
|
|
124
|
+
): string {
|
|
125
|
+
const names: string[] = [];
|
|
126
|
+
for (const axis of axes) names.push(axis.label);
|
|
127
|
+
const rows = count === 1 ? '1 row' : `${formatInteger(count)} rows`;
|
|
128
|
+
if (names.length === 0) return `Parallel coordinates of ${rows}.`;
|
|
129
|
+
return `Parallel coordinates of ${rows}, on ${names.join(', ')}.`;
|
|
130
|
+
}
|