react-cheminfo 0.15.0 → 0.17.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.
Files changed (157) hide show
  1. package/README.md +96 -0
  2. package/lib/core.d.ts +1 -0
  3. package/lib/core.d.ts.map +1 -1
  4. package/lib/core.js +1 -0
  5. package/lib/core.js.map +1 -1
  6. package/lib/credits/core/credits.d.ts +17 -0
  7. package/lib/credits/core/credits.d.ts.map +1 -1
  8. package/lib/credits/core/credits.js +20 -0
  9. package/lib/credits/core/credits.js.map +1 -1
  10. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  11. package/lib/ecosystem/core/sites.js +40 -0
  12. package/lib/ecosystem/core/sites.js.map +1 -1
  13. package/lib/ecosystem/core/types.d.ts +1 -1
  14. package/lib/ecosystem/core/types.d.ts.map +1 -1
  15. package/lib/ecosystem/ui/glyphs.d.ts.map +1 -1
  16. package/lib/ecosystem/ui/glyphs.js +11 -0
  17. package/lib/ecosystem/ui/glyphs.js.map +1 -1
  18. package/lib/parallel/core/index.d.ts +29 -0
  19. package/lib/parallel/core/index.d.ts.map +1 -0
  20. package/lib/parallel/core/index.js +22 -0
  21. package/lib/parallel/core/index.js.map +1 -0
  22. package/lib/parallel/core/parallelAxes.d.ts +103 -0
  23. package/lib/parallel/core/parallelAxes.d.ts.map +1 -0
  24. package/lib/parallel/core/parallelAxes.js +146 -0
  25. package/lib/parallel/core/parallelAxes.js.map +1 -0
  26. package/lib/parallel/core/parallelBrush.d.ts +100 -0
  27. package/lib/parallel/core/parallelBrush.d.ts.map +1 -0
  28. package/lib/parallel/core/parallelBrush.js +149 -0
  29. package/lib/parallel/core/parallelBrush.js.map +1 -0
  30. package/lib/parallel/core/parallelExtent.d.ts +28 -0
  31. package/lib/parallel/core/parallelExtent.d.ts.map +1 -0
  32. package/lib/parallel/core/parallelExtent.js +47 -0
  33. package/lib/parallel/core/parallelExtent.js.map +1 -0
  34. package/lib/parallel/core/parallelFilter.d.ts +49 -0
  35. package/lib/parallel/core/parallelFilter.d.ts.map +1 -0
  36. package/lib/parallel/core/parallelFilter.js +107 -0
  37. package/lib/parallel/core/parallelFilter.js.map +1 -0
  38. package/lib/parallel/core/parallelHit.d.ts +47 -0
  39. package/lib/parallel/core/parallelHit.d.ts.map +1 -0
  40. package/lib/parallel/core/parallelHit.js +81 -0
  41. package/lib/parallel/core/parallelHit.js.map +1 -0
  42. package/lib/parallel/core/parallelOrder.d.ts +36 -0
  43. package/lib/parallel/core/parallelOrder.d.ts.map +1 -0
  44. package/lib/parallel/core/parallelOrder.js +65 -0
  45. package/lib/parallel/core/parallelOrder.js.map +1 -0
  46. package/lib/parallel/core/parallelPaint.d.ts +108 -0
  47. package/lib/parallel/core/parallelPaint.d.ts.map +1 -0
  48. package/lib/parallel/core/parallelPaint.js +169 -0
  49. package/lib/parallel/core/parallelPaint.js.map +1 -0
  50. package/lib/parallel/core/parallelPalette.d.ts +40 -0
  51. package/lib/parallel/core/parallelPalette.d.ts.map +1 -0
  52. package/lib/parallel/core/parallelPalette.js +81 -0
  53. package/lib/parallel/core/parallelPalette.js.map +1 -0
  54. package/lib/parallel/core/parallelSelection.d.ts +43 -0
  55. package/lib/parallel/core/parallelSelection.d.ts.map +1 -0
  56. package/lib/parallel/core/parallelSelection.js +87 -0
  57. package/lib/parallel/core/parallelSelection.js.map +1 -0
  58. package/lib/parallel/core/parallelTicks.d.ts +34 -0
  59. package/lib/parallel/core/parallelTicks.d.ts.map +1 -0
  60. package/lib/parallel/core/parallelTicks.js +119 -0
  61. package/lib/parallel/core/parallelTicks.js.map +1 -0
  62. package/lib/parallel/core/parallelTypes.d.ts +140 -0
  63. package/lib/parallel/core/parallelTypes.d.ts.map +1 -0
  64. package/lib/parallel/core/parallelTypes.js +29 -0
  65. package/lib/parallel/core/parallelTypes.js.map +1 -0
  66. package/lib/parallel/ui/ParallelAxesLayer.d.ts +28 -0
  67. package/lib/parallel/ui/ParallelAxesLayer.d.ts.map +1 -0
  68. package/lib/parallel/ui/ParallelAxesLayer.js +29 -0
  69. package/lib/parallel/ui/ParallelAxesLayer.js.map +1 -0
  70. package/lib/parallel/ui/ParallelCoordinates.d.ts +21 -0
  71. package/lib/parallel/ui/ParallelCoordinates.d.ts.map +1 -0
  72. package/lib/parallel/ui/ParallelCoordinates.js +114 -0
  73. package/lib/parallel/ui/ParallelCoordinates.js.map +1 -0
  74. package/lib/parallel/ui/ParallelLabels.d.ts +32 -0
  75. package/lib/parallel/ui/ParallelLabels.d.ts.map +1 -0
  76. package/lib/parallel/ui/ParallelLabels.js +37 -0
  77. package/lib/parallel/ui/ParallelLabels.js.map +1 -0
  78. package/lib/parallel/ui/ParallelTooltip.d.ts +26 -0
  79. package/lib/parallel/ui/ParallelTooltip.d.ts.map +1 -0
  80. package/lib/parallel/ui/ParallelTooltip.js +30 -0
  81. package/lib/parallel/ui/ParallelTooltip.js.map +1 -0
  82. package/lib/parallel/ui/index.d.ts +29 -0
  83. package/lib/parallel/ui/index.d.ts.map +1 -0
  84. package/lib/parallel/ui/index.js +20 -0
  85. package/lib/parallel/ui/index.js.map +1 -0
  86. package/lib/parallel/ui/parallelCoordinatesProps.d.ts +203 -0
  87. package/lib/parallel/ui/parallelCoordinatesProps.d.ts.map +1 -0
  88. package/lib/parallel/ui/parallelCoordinatesProps.js +11 -0
  89. package/lib/parallel/ui/parallelCoordinatesProps.js.map +1 -0
  90. package/lib/parallel/ui/parallelGestureModel.d.ts +83 -0
  91. package/lib/parallel/ui/parallelGestureModel.d.ts.map +1 -0
  92. package/lib/parallel/ui/parallelGestureModel.js +110 -0
  93. package/lib/parallel/ui/parallelGestureModel.js.map +1 -0
  94. package/lib/parallel/ui/parallelInk.d.ts +76 -0
  95. package/lib/parallel/ui/parallelInk.d.ts.map +1 -0
  96. package/lib/parallel/ui/parallelInk.js +113 -0
  97. package/lib/parallel/ui/parallelInk.js.map +1 -0
  98. package/lib/parallel/ui/parallelPlotModel.d.ts +55 -0
  99. package/lib/parallel/ui/parallelPlotModel.d.ts.map +1 -0
  100. package/lib/parallel/ui/parallelPlotModel.js +113 -0
  101. package/lib/parallel/ui/parallelPlotModel.js.map +1 -0
  102. package/lib/parallel/ui/parallelStyles.d.ts +106 -0
  103. package/lib/parallel/ui/parallelStyles.d.ts.map +1 -0
  104. package/lib/parallel/ui/parallelStyles.js +153 -0
  105. package/lib/parallel/ui/parallelStyles.js.map +1 -0
  106. package/lib/parallel/ui/useParallelAxisDrag.d.ts +60 -0
  107. package/lib/parallel/ui/useParallelAxisDrag.d.ts.map +1 -0
  108. package/lib/parallel/ui/useParallelAxisDrag.js +96 -0
  109. package/lib/parallel/ui/useParallelAxisDrag.js.map +1 -0
  110. package/lib/parallel/ui/useParallelCanvas.d.ts +49 -0
  111. package/lib/parallel/ui/useParallelCanvas.d.ts.map +1 -0
  112. package/lib/parallel/ui/useParallelCanvas.js +58 -0
  113. package/lib/parallel/ui/useParallelCanvas.js.map +1 -0
  114. package/lib/parallel/ui/useParallelGesture.d.ts +72 -0
  115. package/lib/parallel/ui/useParallelGesture.d.ts.map +1 -0
  116. package/lib/parallel/ui/useParallelGesture.js +164 -0
  117. package/lib/parallel/ui/useParallelGesture.js.map +1 -0
  118. package/lib/structure/ui/useToolbarFloor.d.ts.map +1 -1
  119. package/lib/structure/ui/useToolbarFloor.js +29 -22
  120. package/lib/structure/ui/useToolbarFloor.js.map +1 -1
  121. package/lib/ui.d.ts +1 -0
  122. package/lib/ui.d.ts.map +1 -1
  123. package/lib/ui.js +1 -0
  124. package/lib/ui.js.map +1 -1
  125. package/package.json +1 -1
  126. package/src/core.ts +1 -0
  127. package/src/credits/core/credits.ts +21 -0
  128. package/src/ecosystem/core/sites.ts +40 -0
  129. package/src/ecosystem/core/types.ts +4 -1
  130. package/src/ecosystem/ui/glyphs.tsx +46 -0
  131. package/src/parallel/core/index.ts +96 -0
  132. package/src/parallel/core/parallelAxes.ts +207 -0
  133. package/src/parallel/core/parallelBrush.ts +211 -0
  134. package/src/parallel/core/parallelExtent.ts +56 -0
  135. package/src/parallel/core/parallelFilter.ts +131 -0
  136. package/src/parallel/core/parallelHit.ts +106 -0
  137. package/src/parallel/core/parallelOrder.ts +73 -0
  138. package/src/parallel/core/parallelPaint.ts +242 -0
  139. package/src/parallel/core/parallelPalette.ts +93 -0
  140. package/src/parallel/core/parallelSelection.ts +99 -0
  141. package/src/parallel/core/parallelTicks.ts +168 -0
  142. package/src/parallel/core/parallelTypes.ts +159 -0
  143. package/src/parallel/ui/ParallelAxesLayer.tsx +129 -0
  144. package/src/parallel/ui/ParallelCoordinates.tsx +252 -0
  145. package/src/parallel/ui/ParallelLabels.tsx +77 -0
  146. package/src/parallel/ui/ParallelTooltip.tsx +55 -0
  147. package/src/parallel/ui/index.ts +65 -0
  148. package/src/parallel/ui/parallelCoordinatesProps.ts +217 -0
  149. package/src/parallel/ui/parallelGestureModel.ts +159 -0
  150. package/src/parallel/ui/parallelInk.ts +182 -0
  151. package/src/parallel/ui/parallelPlotModel.ts +130 -0
  152. package/src/parallel/ui/parallelStyles.ts +188 -0
  153. package/src/parallel/ui/useParallelAxisDrag.ts +147 -0
  154. package/src/parallel/ui/useParallelCanvas.ts +104 -0
  155. package/src/parallel/ui/useParallelGesture.ts +260 -0
  156. package/src/structure/ui/useToolbarFloor.ts +27 -22
  157. package/src/ui.ts +1 -0
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Which row the pointer is over.
3
+ *
4
+ * Only the segment between the two axes the pointer sits between is tested, so
5
+ * the cost is one distance per row whatever the number of axes. The vertical
6
+ * gap to the segment is projected onto its normal, so a steep line is no
7
+ * harder to pick than a flat one — without which the only rows a reader can
8
+ * ever point at are the uninteresting ones.
9
+ */
10
+
11
+ import type { ParallelAxisLayout } from './parallelAxes.ts';
12
+ import { parallelPixelAt } from './parallelAxes.ts';
13
+
14
+ /** How far the pointer may sit from a line and still pick it, in pixels. */
15
+ export const PARALLEL_HOVER_TOLERANCE = 6;
16
+
17
+ /** A pointer position, relative to the top left of the drawing area. */
18
+ export interface ParallelPoint {
19
+ /** Pixels from the left of the drawing area. */
20
+ x: number;
21
+ /** Pixels from its top. */
22
+ y: number;
23
+ }
24
+
25
+ /** The pair of axes a pointer sits between, as indices into the layouts. */
26
+ export interface ParallelSegment {
27
+ /** The axis to the left of the pointer. */
28
+ left: number;
29
+ /** The axis to its right. */
30
+ right: number;
31
+ }
32
+
33
+ /**
34
+ * The pair of axes a horizontal position sits between.
35
+ * @param x - Pixels from the left of the drawing area.
36
+ * @param axes - The axes, from left to right.
37
+ * @param tolerance - How far past the first and the last axis still counts. Defaults to {@link PARALLEL_HOVER_TOLERANCE}.
38
+ * @returns The pair, or `null` when there are fewer than two axes or the
39
+ * position is past either end of the figure.
40
+ */
41
+ export function parallelSegmentAt(
42
+ x: number,
43
+ axes: readonly ParallelAxisLayout[],
44
+ tolerance: number = PARALLEL_HOVER_TOLERANCE,
45
+ ): ParallelSegment | null {
46
+ const first = axes[0];
47
+ const last = axes.at(-1);
48
+ if (first === undefined || last === undefined || axes.length < 2) return null;
49
+ if (x < first.x - tolerance || x > last.x + tolerance) return null;
50
+ for (let index = 1; index < axes.length; index++) {
51
+ const right = axes[index];
52
+ if (right === undefined) continue;
53
+ if (x <= right.x || index === axes.length - 1) {
54
+ return { left: index - 1, right: index };
55
+ }
56
+ }
57
+ return null;
58
+ }
59
+
60
+ /**
61
+ * The row whose line runs closest to the pointer.
62
+ * @param point - Where the pointer is, relative to the drawing area.
63
+ * @param axes - The axes, from left to right.
64
+ * @param values - Each axis's column, in the same order.
65
+ * @param count - How many rows are drawn.
66
+ * @param included - Which rows a brush keeps, a zero meaning the row is not pickable. Defaults to every row.
67
+ * @param tolerance - How far the pointer may sit from a line. Defaults to {@link PARALLEL_HOVER_TOLERANCE}.
68
+ * @returns The row, or `-1` when the pointer is over no line.
69
+ */
70
+ export function parallelNearestRow(
71
+ point: ParallelPoint,
72
+ axes: readonly ParallelAxisLayout[],
73
+ values: ReadonlyArray<ArrayLike<number>>,
74
+ count: number,
75
+ included?: Uint8Array,
76
+ tolerance: number = PARALLEL_HOVER_TOLERANCE,
77
+ ): number {
78
+ const segment = parallelSegmentAt(point.x, axes, tolerance);
79
+ if (segment === null) return -1;
80
+ const left = axes[segment.left];
81
+ const right = axes[segment.right];
82
+ const leftColumn = values[segment.left];
83
+ const rightColumn = values[segment.right];
84
+ if (left === undefined || right === undefined) return -1;
85
+ if (leftColumn === undefined || rightColumn === undefined) return -1;
86
+
87
+ const width = right.x - left.x;
88
+ if (width <= 0) return -1;
89
+ const ratio = (point.x - left.x) / width;
90
+ const rows = Math.min(count, leftColumn.length, rightColumn.length);
91
+
92
+ let nearest = -1;
93
+ let nearestDistance = tolerance;
94
+ for (let row = 0; row < rows; row++) {
95
+ if (included?.[row] === 0) continue;
96
+ const leftY = parallelPixelAt(left.pixels, leftColumn[row] as number);
97
+ const rightY = parallelPixelAt(right.pixels, rightColumn[row] as number);
98
+ const gap = Math.abs(leftY + (rightY - leftY) * ratio - point.y);
99
+ const distance = (gap * width) / Math.hypot(width, rightY - leftY);
100
+ if (distance < nearestDistance) {
101
+ nearestDistance = distance;
102
+ nearest = row;
103
+ }
104
+ }
105
+ return nearest;
106
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Which order the axes stand in, when the reader is the one deciding.
3
+ *
4
+ * A parallel-coordinates figure only shows a relationship between two columns
5
+ * that stand next to each other, so the order is not decoration: moving an
6
+ * axis is how a reader asks a different question of the same data. The
7
+ * arithmetic of the move is here, away from the pointer that starts it, so
8
+ * that dragging a name and pressing an arrow key are the same operation.
9
+ */
10
+
11
+ import type { ParallelAxisLayout } from './parallelAxes.ts';
12
+
13
+ /**
14
+ * The same ids with one of them moved.
15
+ * @param ids - The axes, from left to right.
16
+ * @param from - Which one is moving.
17
+ * @param to - The place it lands in, counted in the list it left.
18
+ * @returns The new order. The list comes back unchanged when either place is
19
+ * outside it or when nothing moves, so a caller can report without comparing.
20
+ */
21
+ export function parallelMoveAxis(
22
+ ids: readonly string[],
23
+ from: number,
24
+ to: number,
25
+ ): string[] {
26
+ const order = [...ids];
27
+ if (from < 0 || from >= order.length) return order;
28
+ if (to < 0 || to >= order.length || to === from) return order;
29
+ const [moved] = order.splice(from, 1);
30
+ if (moved === undefined) return [...ids];
31
+ order.splice(to, 0, moved);
32
+ return order;
33
+ }
34
+
35
+ /**
36
+ * Where an axis dragged to that position would land.
37
+ * @param x - Where the pointer is, in pixels from the left of the drawing area.
38
+ * @param layouts - The axes, from left to right.
39
+ * @returns The index of the axis whose place it would take, or `-1` when there
40
+ * is nowhere to land. Nearest axis rather than nearest gap: a reader dragging
41
+ * a name over its neighbour means the two to swap, and the neighbour's own
42
+ * name is the thing they are aiming at.
43
+ */
44
+ export function parallelDropIndex(
45
+ x: number,
46
+ layouts: readonly ParallelAxisLayout[],
47
+ ): number {
48
+ let nearest = -1;
49
+ let nearestDistance = Number.POSITIVE_INFINITY;
50
+ for (let index = 0; index < layouts.length; index++) {
51
+ const layout = layouts[index];
52
+ if (layout === undefined) continue;
53
+ const distance = Math.abs(x - layout.x);
54
+ if (distance < nearestDistance) {
55
+ nearestDistance = distance;
56
+ nearest = index;
57
+ }
58
+ }
59
+ return nearest;
60
+ }
61
+
62
+ /**
63
+ * The ids of the axes, from left to right.
64
+ * @param layouts - The axes, already placed.
65
+ * @returns Their ids, which is what an order is reported as.
66
+ */
67
+ export function parallelAxisOrder(
68
+ layouts: readonly ParallelAxisLayout[],
69
+ ): string[] {
70
+ const ids: string[] = [];
71
+ for (const layout of layouts) ids.push(layout.id);
72
+ return ids;
73
+ }
@@ -0,0 +1,242 @@
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
+ // A step past the end of the ramp is a quantity nobody knows for this
153
+ // row; it is drawn in the neutral ink rather than in the ramp's first
154
+ // colour, which means the smallest value in the set.
155
+ context.strokeStyle = palette[step] ?? line;
156
+ previousStep = step;
157
+ }
158
+ }
159
+ context.beginPath();
160
+ traceParallelRow(context, row, layouts, values);
161
+ context.stroke();
162
+ }
163
+ context.globalAlpha = 1;
164
+ }
165
+
166
+ /**
167
+ * Draw a few singled-out rows over the mass, each over a halo and dotted at
168
+ * every axis crossing.
169
+ * @param context - A context already scaled and moved to the drawing area.
170
+ * @param paint - See {@link ParallelHighlightPaint}.
171
+ */
172
+ export function paintParallelHighlights(
173
+ context: CanvasRenderingContext2D,
174
+ paint: ParallelHighlightPaint,
175
+ ): void {
176
+ const { highlights, layouts, values, halo } = paint;
177
+ if (layouts.length < 2) return;
178
+ context.lineJoin = 'round';
179
+ context.lineCap = 'round';
180
+ for (const { row, color } of highlights) {
181
+ context.beginPath();
182
+ traceParallelRow(context, row, layouts, values);
183
+ context.strokeStyle = halo;
184
+ context.lineWidth = HALO_WIDTH;
185
+ context.stroke();
186
+ context.strokeStyle = color;
187
+ context.lineWidth = HIGHLIGHT_WIDTH;
188
+ context.stroke();
189
+
190
+ context.fillStyle = color;
191
+ for (let index = 0; index < layouts.length; index++) {
192
+ const layout = layouts[index];
193
+ const column = values[index];
194
+ if (layout === undefined || column === undefined) continue;
195
+ const y = parallelPixelAt(layout.pixels, column[row] as number);
196
+ if (!Number.isFinite(y)) continue;
197
+ context.beginPath();
198
+ context.arc(layout.x, y, HIGHLIGHT_DOT_RADIUS, 0, FULL_TURN);
199
+ context.fill();
200
+ }
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Add one row's line to the path being built.
206
+ *
207
+ * The line is one polyline per unbroken run of known values, so a row with a
208
+ * gap in the middle is drawn as two, and nothing is painted over an axis the
209
+ * row has no value on.
210
+ * @param context - A context already scaled and moved to the drawing area.
211
+ * @param row - The row to trace.
212
+ * @param layouts - The axes, from left to right.
213
+ * @param values - Each axis's column, in the same order.
214
+ */
215
+ export function traceParallelRow(
216
+ context: CanvasRenderingContext2D,
217
+ row: number,
218
+ layouts: readonly ParallelAxisLayout[],
219
+ values: ReadonlyArray<ArrayLike<number>>,
220
+ ): void {
221
+ let started = false;
222
+ for (let index = 0; index < layouts.length; index++) {
223
+ const layout = layouts[index];
224
+ const column = values[index];
225
+ if (layout === undefined || column === undefined) continue;
226
+ const y = parallelPixelAt(layout.pixels, column[row] as number);
227
+ // A row whose value on this axis is not known yet has no point to draw,
228
+ // and the line breaks rather than bridging the axis: a segment drawn
229
+ // across it reads as a value the row does not hold, and the hit test,
230
+ // which has no endpoint there, cannot pick it back.
231
+ if (!Number.isFinite(y)) {
232
+ started = false;
233
+ continue;
234
+ }
235
+ if (started) {
236
+ context.lineTo(layout.x, y);
237
+ } else {
238
+ context.moveTo(layout.x, y);
239
+ started = true;
240
+ }
241
+ }
242
+ }
@@ -0,0 +1,93 @@
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 step a row whose colouring quantity is unknown takes.
25
+ *
26
+ * Past the end of the ramp on purpose: no colour indexes it, so the painter
27
+ * leaves such a row in the neutral ink rather than claiming for it the ink of
28
+ * the smallest value in the set.
29
+ */
30
+ export const PARALLEL_PALETTE_UNKNOWN = PARALLEL_PALETTE_STEPS + 1;
31
+
32
+ /**
33
+ * The ramp, sampled into the steps a line's colour is looked up by.
34
+ * @param scale - The ramp, as the registry hands it over or as a plain list of colours. Defaults to viridis.
35
+ * @returns `PARALLEL_PALETTE_STEPS + 1` colours, from the low end to the high end.
36
+ */
37
+ export function parallelPalette(
38
+ scale?: ColorScale | readonly string[],
39
+ ): readonly string[] {
40
+ return sampleScale(resolveRamp(scale), PARALLEL_PALETTE_STEPS + 1);
41
+ }
42
+
43
+ /**
44
+ * Which step of the ramp each row takes.
45
+ *
46
+ * Worked out once per data change rather than per repaint, and held as bytes:
47
+ * a library of ten thousand rows costs ten kilobytes and the painter's inner
48
+ * loop reads an integer instead of placing a value on a ramp.
49
+ * @param color - The quantity the lines are coloured by.
50
+ * @param count - How many rows there are.
51
+ * @returns One step per row, from `0` to {@link PARALLEL_PALETTE_STEPS}, or
52
+ * {@link PARALLEL_PALETTE_UNKNOWN} for a row whose value is not a number.
53
+ */
54
+ export function parallelColorSteps(
55
+ color: ParallelColorBy,
56
+ count: number,
57
+ ): Uint8Array {
58
+ const steps = new Uint8Array(count);
59
+ // A row the column does not reach is as unknown as one holding no number.
60
+ steps.fill(PARALLEL_PALETTE_UNKNOWN);
61
+ const extent = parallelExtent(color.values, count);
62
+ const min = color.min ?? extent.min;
63
+ const max = color.max ?? extent.max;
64
+ const logarithmic = color.logarithmic ?? false;
65
+ const span = max - min;
66
+ const rows = Math.min(count, color.values.length);
67
+ for (let row = 0; row < rows; row++) {
68
+ const value = color.values[row] as number;
69
+ if (!Number.isFinite(value)) continue;
70
+ steps[row] = logarithmic
71
+ ? Math.round(
72
+ positionInRange(value, min, max, { logarithmic: true }) *
73
+ PARALLEL_PALETTE_STEPS,
74
+ )
75
+ : colorStep(value, min, span);
76
+ }
77
+ return steps;
78
+ }
79
+
80
+ function resolveRamp(scale?: ColorScale | readonly string[]): ColorScale {
81
+ if (scale === undefined) {
82
+ return resolveColorScale(DEFAULT_COLOR_SCALE_ID).scale;
83
+ }
84
+ return 'stops' in scale ? scale : evenScale(scale);
85
+ }
86
+
87
+ function colorStep(value: number, min: number, span: number): number {
88
+ if (span <= 0) return 0;
89
+ const ratio = (value - min) / span;
90
+ if (Number.isNaN(ratio) || ratio < 0) return 0;
91
+ if (ratio > 1) return PARALLEL_PALETTE_STEPS;
92
+ return Math.round(ratio * PARALLEL_PALETTE_STEPS);
93
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * What an axis keeps, when it keeps more than one interval.
3
+ *
4
+ * The figure reports a list and accepts either a list or a bare interval, so a
5
+ * caller that only ever keeps one is not made to write `[[0, 1]]`. Everything
6
+ * downstream — the filter, the bands, the gesture — reads the list, which is
7
+ * what {@link parallelRangeList} is for: one normalisation, at the edge, and
8
+ * no `Array.isArray` scattered through the painter.
9
+ *
10
+ * Intervals on one axis are held sorted and disjoint. Two that touch are one
11
+ * interval drawn twice, and a reader dragging a band across its neighbour means
12
+ * to widen it rather than to stack two bands nobody can then take hold of.
13
+ */
14
+
15
+ import type { ParallelRange, ParallelSelection } from './parallelTypes.ts';
16
+
17
+ /** Nothing kept, shared so a normalisation never allocates for the common case. */
18
+ const NONE: readonly ParallelRange[] = [];
19
+
20
+ /**
21
+ * The intervals one axis keeps, however the caller wrote them.
22
+ * @param selection - One interval, several, or nothing at all.
23
+ * @returns The intervals, in the order given. Read in place — the caller's own
24
+ * array comes back when it gave one.
25
+ */
26
+ export function parallelRangeList(
27
+ selection: ParallelSelection | undefined,
28
+ ): readonly ParallelRange[] {
29
+ if (selection === null || selection === undefined) return NONE;
30
+ if (selection.length === 0) return NONE;
31
+ // A bare interval is two numbers; a list of them holds arrays.
32
+ return typeof selection[0] === 'number'
33
+ ? [selection as ParallelRange]
34
+ : (selection as readonly ParallelRange[]);
35
+ }
36
+
37
+ /**
38
+ * Whether an axis keeps anything at all.
39
+ * @param selection - What that axis keeps.
40
+ * @returns Whether any interval is in force.
41
+ */
42
+ export function parallelHasRange(
43
+ selection: ParallelSelection | undefined,
44
+ ): boolean {
45
+ return parallelRangeList(selection).length > 0;
46
+ }
47
+
48
+ /**
49
+ * The same intervals, sorted low to high and with the overlapping ones joined.
50
+ * @param ranges - The intervals, in any order, each low value first.
51
+ * @returns One list, sorted and disjoint. Intervals that touch at a point are
52
+ * joined too: a reader who dragged one band onto another meant one band.
53
+ */
54
+ export function parallelMergeRanges(
55
+ ranges: readonly ParallelRange[],
56
+ ): ParallelRange[] {
57
+ if (ranges.length === 0) return [];
58
+ const sorted = ranges
59
+ .map((range) =>
60
+ range[0] <= range[1] ? range : ([range[1], range[0]] as ParallelRange),
61
+ )
62
+ .toSorted((left, right) => left[0] - right[0]);
63
+ const merged: ParallelRange[] = [];
64
+ let low = (sorted[0] as ParallelRange)[0];
65
+ let high = (sorted[0] as ParallelRange)[1];
66
+ for (let index = 1; index < sorted.length; index++) {
67
+ const range = sorted[index] as ParallelRange;
68
+ if (range[0] > high) {
69
+ merged.push([low, high]);
70
+ low = range[0];
71
+ high = range[1];
72
+ continue;
73
+ }
74
+ if (range[1] > high) high = range[1];
75
+ }
76
+ merged.push([low, high]);
77
+ return merged;
78
+ }
79
+
80
+ /**
81
+ * The intervals an axis keeps once one of them is replaced, removed or added.
82
+ * @param ranges - The intervals in force.
83
+ * @param index - Which one is being written, or `-1` to add one.
84
+ * @param range - The interval it becomes, or `null` to take it away.
85
+ * @returns The new list, sorted and disjoint. Never the same array.
86
+ */
87
+ export function parallelWriteRange(
88
+ ranges: readonly ParallelRange[],
89
+ index: number,
90
+ range: ParallelRange | null,
91
+ ): ParallelRange[] {
92
+ const kept: ParallelRange[] = [];
93
+ for (let at = 0; at < ranges.length; at++) {
94
+ if (at === index) continue;
95
+ kept.push(ranges[at] as ParallelRange);
96
+ }
97
+ if (range !== null) kept.push(range);
98
+ return parallelMergeRanges(kept);
99
+ }