swiftplot 0.0.0-stage → 0.1.1

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 (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +327 -2
  3. package/dist/chart.d.ts +3 -0
  4. package/dist/chart.js +1226 -0
  5. package/dist/charts/controller.d.ts +32 -0
  6. package/dist/charts/controller.js +231 -0
  7. package/dist/charts/defaults.d.ts +12 -0
  8. package/dist/charts/defaults.js +60 -0
  9. package/dist/charts/fields.d.ts +34 -0
  10. package/dist/charts/fields.js +167 -0
  11. package/dist/charts/index.d.ts +6 -0
  12. package/dist/charts/index.js +3 -0
  13. package/dist/charts/mount.d.ts +5 -0
  14. package/dist/charts/mount.js +21 -0
  15. package/dist/charts/source.d.ts +24 -0
  16. package/dist/charts/source.js +225 -0
  17. package/dist/charts/types.d.ts +104 -0
  18. package/dist/charts/types.js +1 -0
  19. package/dist/charts/xy.d.ts +36 -0
  20. package/dist/charts/xy.js +220 -0
  21. package/dist/features/animate.d.ts +11 -0
  22. package/dist/features/animate.js +118 -0
  23. package/dist/features/brush.d.ts +13 -0
  24. package/dist/features/brush.js +127 -0
  25. package/dist/features/legend.d.ts +15 -0
  26. package/dist/features/legend.js +237 -0
  27. package/dist/features/pointer.d.ts +29 -0
  28. package/dist/features/pointer.js +23 -0
  29. package/dist/features/tooltip.d.ts +14 -0
  30. package/dist/features/tooltip.js +190 -0
  31. package/dist/features/zoom.d.ts +13 -0
  32. package/dist/features/zoom.js +228 -0
  33. package/dist/gl.d.ts +5 -0
  34. package/dist/gl.js +52 -0
  35. package/dist/index.d.ts +60 -0
  36. package/dist/index.js +31 -0
  37. package/dist/marks/arcs.d.ts +25 -0
  38. package/dist/marks/arcs.js +124 -0
  39. package/dist/marks/area.d.ts +24 -0
  40. package/dist/marks/area.js +106 -0
  41. package/dist/marks/bar.d.ts +44 -0
  42. package/dist/marks/bar.js +243 -0
  43. package/dist/marks/box.d.ts +27 -0
  44. package/dist/marks/box.js +188 -0
  45. package/dist/marks/common.d.ts +84 -0
  46. package/dist/marks/common.js +236 -0
  47. package/dist/marks/dots.d.ts +15 -0
  48. package/dist/marks/dots.js +156 -0
  49. package/dist/marks/errorBars.d.ts +19 -0
  50. package/dist/marks/errorBars.js +79 -0
  51. package/dist/marks/funnel.d.ts +25 -0
  52. package/dist/marks/funnel.js +121 -0
  53. package/dist/marks/heatmap.d.ts +19 -0
  54. package/dist/marks/heatmap.js +139 -0
  55. package/dist/marks/line.d.ts +19 -0
  56. package/dist/marks/line.js +75 -0
  57. package/dist/marks/radar.d.ts +22 -0
  58. package/dist/marks/radar.js +155 -0
  59. package/dist/marks/radialBars.d.ts +27 -0
  60. package/dist/marks/radialBars.js +111 -0
  61. package/dist/marks/rects.d.ts +43 -0
  62. package/dist/marks/rects.js +120 -0
  63. package/dist/marks/rule.d.ts +28 -0
  64. package/dist/marks/rule.js +80 -0
  65. package/dist/marks/sankey.d.ts +41 -0
  66. package/dist/marks/sankey.js +366 -0
  67. package/dist/marks/sunburst.d.ts +26 -0
  68. package/dist/marks/sunburst.js +185 -0
  69. package/dist/marks/ticks.d.ts +9 -0
  70. package/dist/marks/ticks.js +26 -0
  71. package/dist/marks/tree.d.ts +41 -0
  72. package/dist/marks/tree.js +110 -0
  73. package/dist/marks/treemap.d.ts +25 -0
  74. package/dist/marks/treemap.js +159 -0
  75. package/dist/paint.d.ts +30 -0
  76. package/dist/paint.js +66 -0
  77. package/dist/scale.d.ts +16 -0
  78. package/dist/scale.js +96 -0
  79. package/dist/scales/band.d.ts +9 -0
  80. package/dist/scales/band.js +23 -0
  81. package/dist/scales/bare.d.ts +3 -0
  82. package/dist/scales/bare.js +7 -0
  83. package/dist/scales/color.d.ts +18 -0
  84. package/dist/scales/color.js +21 -0
  85. package/dist/scales/log.d.ts +20 -0
  86. package/dist/scales/log.js +97 -0
  87. package/dist/scales/time.d.ts +30 -0
  88. package/dist/scales/time.js +181 -0
  89. package/dist/theme.d.ts +10 -0
  90. package/dist/theme.js +8 -0
  91. package/dist/transforms.d.ts +43 -0
  92. package/dist/transforms.js +118 -0
  93. package/dist/types.d.ts +434 -0
  94. package/dist/types.js +1 -0
  95. package/package.json +39 -3
@@ -0,0 +1,188 @@
1
+ import { glLayer } from '../gl.js';
2
+ import { painter, stops } from '../paint.js';
3
+ import { base } from './common.js';
4
+ import { focused, gpuRows } from './rects.js';
5
+ export function box(o) {
6
+ const { x, min, q1, median, q3, max, color, alpha = 1, width = 0.6 } = o;
7
+ const n = x.length;
8
+ const [corner, heavy, faint] = [o.radius ?? 0, o.medianWidth ?? 1.5, o.whiskers ?? 1];
9
+ const blended = typeof o.fill === 'object' ? o.fill : undefined;
10
+ // The axis must make room for the boxes, not only for their centres.
11
+ let [first, last] = [Infinity, -Infinity];
12
+ for (let i = 0; i < n; i++) {
13
+ if (x[i] < first && x[i] !== -Infinity)
14
+ first = x[i];
15
+ if (x[i] > last && x[i] !== Infinity)
16
+ last = x[i];
17
+ }
18
+ const rows = gpuRows(new WeakMap(), [x], [min, q1, median, q3, max]);
19
+ // Each box's left, top, right and bottom in device pixels; a row with a number missing has none.
20
+ const bounds = ({ ox, kx, oy, ky }) => {
21
+ const edges = new Float64Array(n * 4).fill(NaN);
22
+ for (let i = 0; i < n; i++) {
23
+ if (![x, min, q1, median, q3, max].every(c => Number.isFinite(c[i])))
24
+ continue;
25
+ const mid = ox + x[i] * kx;
26
+ const l = Math.round(mid - (width * kx) / 2);
27
+ const r = Math.max(Math.round(mid + (width * kx) / 2), l + 1);
28
+ edges.set([l, Math.round(oy - q3[i] * ky), r, Math.round(oy - q1[i] * ky)], i * 4);
29
+ }
30
+ return edges;
31
+ };
32
+ // The box itself answers, with its median; reported as the weakest hit like any filled shape.
33
+ const hit = ({ dpr, left, top, width: plotWidth, height }, bounded) => (px, py, reach) => {
34
+ const [hx, hy] = [px * dpr, py * dpr];
35
+ if (hx < left || hx > left + plotWidth || hy < top || hy > top + height)
36
+ return null;
37
+ const edges = bounded();
38
+ for (let i = n - 1; i >= 0; i--) {
39
+ const j = i * 4;
40
+ if (hx >= edges[j] && hy >= edges[j + 1] && hx < edges[j + 2] && hy < edges[j + 3]) {
41
+ return { x: x[i], y: median[i], distance: reach, row: i };
42
+ }
43
+ }
44
+ return null;
45
+ };
46
+ return {
47
+ ...base('box', o, [x, [first - width / 2, last + width / 2]], [min, max]),
48
+ overhang: 0.75,
49
+ prepare(view) {
50
+ const { ctx, dpr, ox, kx, oy, ky } = view;
51
+ const edges = bounds(view);
52
+ const y = (v) => Math.round(oy - v * ky);
53
+ const shape = (path, i) => {
54
+ const [l, t, w, h] = [
55
+ edges[i * 4],
56
+ edges[i * 4 + 1],
57
+ edges[i * 4 + 2] - edges[i * 4],
58
+ edges[i * 4 + 3] - edges[i * 4 + 1],
59
+ ];
60
+ const r = Math.min(corner * dpr, w / 2, h / 2);
61
+ if (r >= 1)
62
+ path.roundRect(l, t, w, h, r);
63
+ else
64
+ path.rect(l, t, w, h);
65
+ };
66
+ // The bodies, their outlines, the whiskers with their ends, and the medians.
67
+ const paths = (rows) => {
68
+ const [body, frame, whisk, mid] = [new Path2D(), new Path2D(), new Path2D(), new Path2D()];
69
+ for (let i = 0; i < n; i++) {
70
+ const [l, t, r, b] = [edges[i * 4], edges[i * 4 + 1], edges[i * 4 + 2], edges[i * 4 + 3]];
71
+ // A row with a number missing draws nothing: see errorBars for what a NaN does to a path.
72
+ if (!rows(i) || Number.isNaN(l))
73
+ continue;
74
+ const centre = ox + x[i] * kx;
75
+ shape(body, i);
76
+ shape(frame, i);
77
+ for (const [from, to] of [
78
+ [t, y(max[i])],
79
+ [b, y(min[i])],
80
+ ]) {
81
+ whisk.moveTo(centre, from);
82
+ whisk.lineTo(centre, to);
83
+ whisk.moveTo(centre - (r - l) / 4, to);
84
+ whisk.lineTo(centre + (r - l) / 4, to);
85
+ }
86
+ mid.moveTo(l, y(median[i]));
87
+ mid.lineTo(r, y(median[i]));
88
+ }
89
+ return { body, frame, whisk, mid, rows };
90
+ };
91
+ const all = paths(() => true);
92
+ // The one focused row and the rest, kept for as long as the focus stays on that row.
93
+ let split;
94
+ const ink = painter(ctx, o.fill, color);
95
+ const draw = ({ body, frame, whisk, mid, rows }, k) => {
96
+ ctx.strokeStyle = color;
97
+ ctx.globalAlpha = alpha * k * (blended ? 1 : 0.25);
98
+ // A blend runs over each box's own height, so each is filled on its own.
99
+ for (let i = 0; blended && i < n; i++) {
100
+ if (!rows(i) || Number.isNaN(edges[i * 4]))
101
+ continue;
102
+ ctx.fillStyle = ink([...edges.subarray(i * 4, i * 4 + 4)]);
103
+ ctx.beginPath();
104
+ shape(ctx, i);
105
+ ctx.fill();
106
+ }
107
+ if (!blended) {
108
+ ctx.fillStyle = ink([]);
109
+ ctx.fill(body);
110
+ }
111
+ ctx.lineWidth = 1.5 * dpr;
112
+ ctx.globalAlpha = alpha * k * faint;
113
+ ctx.stroke(whisk);
114
+ ctx.globalAlpha = alpha * k;
115
+ ctx.stroke(frame);
116
+ ctx.lineWidth = heavy * dpr;
117
+ ctx.stroke(mid);
118
+ };
119
+ return {
120
+ paint(a, row, rest = 1) {
121
+ if (row === undefined || row >= n)
122
+ return draw(all, a);
123
+ if (split?.row !== row)
124
+ split = { row, one: paths(i => i === row), others: paths(i => i !== row) };
125
+ draw(split.others, a * rest);
126
+ draw(split.one, a);
127
+ },
128
+ hit: hit(view, () => edges),
129
+ };
130
+ },
131
+ gl: {
132
+ layer: glLayer,
133
+ under: true,
134
+ prepare(view, { gl }) {
135
+ const upright = blended?.toward === 'down' || blended?.toward === 'up';
136
+ const blend = blended && { stops: stops(view.ctx, blended, color), upright, based: false };
137
+ const { draw, once } = rows.on(view, gl, color, 0.75);
138
+ const filled = rows.on(view, gl, typeof o.fill === 'string' ? o.fill : color, 0.75, blend).draw;
139
+ // Columns: x, then min, q1, median, q3 and max. Half the box's width, and half a line's.
140
+ const [w, h] = [Math.max((width * view.kx) / 2, 0.5), 0.75 * view.dpr];
141
+ const at = (ya, yb, pad, more) => ({
142
+ xa: 0,
143
+ xb: 0,
144
+ ya,
145
+ yb,
146
+ pad,
147
+ snap: true,
148
+ ...more,
149
+ });
150
+ const body = at(2, 4, [w, 0], { alpha: blended ? 1 : 0.25 });
151
+ // The median, the four sides, and the two whiskers with their ends: the first to reach a pixel keeps it.
152
+ const lines = [
153
+ at(3, 3, [w, (heavy * view.dpr) / 2]),
154
+ at(4, 4, [w + h, h]),
155
+ at(2, 2, [w + h, h]),
156
+ at(2, 4, [h, h], { shift: [-w, 0] }),
157
+ at(2, 4, [h, h], { shift: [w, 0] }),
158
+ at(4, 5, [h, 0], { alpha: faint }),
159
+ at(1, 2, [h, 0], { alpha: faint }),
160
+ at(5, 5, [w / 2, h], { alpha: faint }),
161
+ at(1, 1, [w / 2, h], { alpha: faint }),
162
+ ];
163
+ let edges;
164
+ return {
165
+ // The bodies as one filled shape and then the lines as one stroke, as on the canvas.
166
+ paint(a, row, rest = 1) {
167
+ for (const [parts, pen] of [
168
+ [[body], filled],
169
+ [lines, draw],
170
+ ]) {
171
+ once(() => {
172
+ for (const [strength, ...run] of focused(n, row, rest)) {
173
+ for (const part of parts)
174
+ pen(part, alpha * a * strength, ...run);
175
+ }
176
+ });
177
+ }
178
+ },
179
+ hit: hit(view, () => (edges ??= bounds(view))),
180
+ };
181
+ },
182
+ },
183
+ release(layer) {
184
+ if (layer)
185
+ rows.release(layer.gl);
186
+ },
187
+ };
188
+ }
@@ -0,0 +1,84 @@
1
+ import type { Col } from '../types.js';
2
+ export type Common = {
3
+ key?: string;
4
+ label?: string;
5
+ group?: string;
6
+ color: string;
7
+ alpha?: number;
8
+ };
9
+ export declare const base: (type: string, o: Common, x: Col[], y: Col[]) => {
10
+ type: string;
11
+ key: string | undefined;
12
+ label: string | undefined;
13
+ group: string | undefined;
14
+ color: string;
15
+ options: Common;
16
+ points: number;
17
+ channels: {
18
+ x: Col[];
19
+ y: Col[];
20
+ };
21
+ };
22
+ /** 'smooth' curves through the points where they are further apart than a pixel, and is 'linear' where they are not. */
23
+ export type Curve = 'linear' | 'step' | 'smooth';
24
+ /**
25
+ * A curve on from the path's current point through n points, forwards or backwards, that never
26
+ * goes above or below the points it joins: its lean at each is the harmonic mean of the leans
27
+ * either side of it, and level where they turn.
28
+ */
29
+ export declare function smooth(path: Path2D, xs: ArrayLike<number>, ys: ArrayLike<number>, n: number, back?: boolean): void;
30
+ export declare const TURN: number;
31
+ /** How far round the clock from twelve the point (dx, dy) from the centre is, from 0 up to 1. */
32
+ /**
33
+ * Whether level text w by h, centred at (x, y), lies inside the part of a ring round (cx, cy)
34
+ * between two radii and two shares of the turn: its nearest and furthest points between the
35
+ * radii, and its corners between the two edges.
36
+ */
37
+ export declare function inRing([cx, cy, inner, outer, from, to]: readonly number[], x: number, y: number, w: number, h: number): boolean;
38
+ export declare const turnAt: (dx: number, dy: number) => number;
39
+ /**
40
+ * One min, max and last y per device-pixel column. x must be ascending.
41
+ * `first` (each column's first y) and `at` (its exact x) are kept only with `straight`;
42
+ * without it they alias `lo` and must not be read.
43
+ * A y that is not finite once projected, or a value of `also` at the same row that is not, ends a
44
+ * run: `runs` holds each run's first column, the column after its last and how many values it
45
+ * kept, and a pixel column a run ends in is split. A run also ends after a row in `cuts`, and is
46
+ * marked as following one when nothing else broke it since.
47
+ */
48
+ export declare function columns(x: Col, y: Col, ox: number, kx: number, oy: number, ky: number, straight?: boolean, also?: Col, [from, to]?: [number, number], cuts?: Set<number>): {
49
+ n: number;
50
+ col: Int32Array<ArrayBuffer>;
51
+ lo: Float64Array<ArrayBuffer>;
52
+ hi: Float64Array<ArrayBuffer>;
53
+ last: Float64Array<ArrayBuffer>;
54
+ first: Float64Array<ArrayBuffer>;
55
+ at: Float64Array<ArrayBuffer>;
56
+ runs: [number, number, number, boolean][];
57
+ };
58
+ /** Runs of columns() put together where each follows a cut, for `join` to draw as one. */
59
+ export declare function chains<T extends [number, number, number, boolean]>(runs: T[]): T[][];
60
+ /**
61
+ * x and ys cut to the rows where every y is finite, and the kept rows a gap follows: the curve is
62
+ * drawn straight from each of those to the next.
63
+ */
64
+ export declare function joined<T extends Col[]>(x: Col, ...ys: T): [[Col, ...T], Set<number>];
65
+ /** How many rows of an ascending x are at or below v. */
66
+ export declare function upTo(x: Col, v: number): number;
67
+ /**
68
+ * The rows of an ascending x that a curve drawn between the device pixels lo and hi reads: those
69
+ * between them and `keep` pixel columns of rows beyond each, so no column is cut in two.
70
+ */
71
+ export declare function span(x: Col, ox: number, kx: number, lo: number, hi: number, keep: number): [number, number];
72
+ /** The least and greatest f(value) over the rows where f of every column is finite. */
73
+ export declare function extent(ys: Col[], f?: (v: number) => number): [number, number];
74
+ /**
75
+ * The least and greatest oy - y * ky over the rows of ys, from their extent: when an end of it
76
+ * overflows once projected, the rows are scanned again for the ones that do not.
77
+ */
78
+ export declare function reach([lo, hi]: [number, number], ys: Col[], oy: number, ky: number): [number, number];
79
+ /**
80
+ * y at v as the curve draws it: the held sample of a step, else the line or smooth() between
81
+ * samples, and the line from a row in `cuts` to the next. A row where y, or `also` like columns(), is not
82
+ * finite breaks the curve.
83
+ */
84
+ export declare function sampleAt(x: Col, y: Col, v: number, curve: Curve, also?: Col, cuts?: Set<number>): number | undefined;
@@ -0,0 +1,236 @@
1
+ export const base = (type, o, x, y) => ({
2
+ type,
3
+ key: o.key,
4
+ label: o.label,
5
+ group: o.group ?? o.label,
6
+ color: o.color,
7
+ options: o,
8
+ points: x[0]?.length ?? 0,
9
+ channels: { x, y },
10
+ });
11
+ /**
12
+ * A curve on from the path's current point through n points, forwards or backwards, that never
13
+ * goes above or below the points it joins: its lean at each is the harmonic mean of the leans
14
+ * either side of it, and level where they turn.
15
+ */
16
+ export function smooth(path, xs, ys, n, back = false) {
17
+ const at = (k) => (back ? n - 1 - k : k);
18
+ const rise = (k) => (ys[at(k + 1)] - ys[at(k)]) / (xs[at(k + 1)] - xs[at(k)] || 1);
19
+ const lean = (k) => {
20
+ if (k === 0 || k === n - 1)
21
+ return rise(Math.min(k, n - 2));
22
+ const [a, b] = [rise(k - 1), rise(k)];
23
+ return a * b > 0 ? (2 * a * b) / (a + b) : 0;
24
+ };
25
+ for (let k = 1; k < n; k++) {
26
+ const [x0, y0, x1, y1] = [xs[at(k - 1)], ys[at(k - 1)], xs[at(k)], ys[at(k)]];
27
+ const third = (x1 - x0) / 3;
28
+ path.bezierCurveTo(x0 + third, y0 + lean(k - 1) * third, x1 - third, y1 - lean(k) * third, x1, y1);
29
+ }
30
+ }
31
+ export const TURN = 2 * Math.PI;
32
+ /** How far round the clock from twelve the point (dx, dy) from the centre is, from 0 up to 1. */
33
+ /**
34
+ * Whether level text w by h, centred at (x, y), lies inside the part of a ring round (cx, cy)
35
+ * between two radii and two shares of the turn: its nearest and furthest points between the
36
+ * radii, and its corners between the two edges.
37
+ */
38
+ export function inRing([cx, cy, inner, outer, from, to], x, y, w, h) {
39
+ const [l, t, r, b] = [x - w / 2 - cx, y - h / 2 - cy, x + w / 2 - cx, y + h / 2 - cy];
40
+ const near = Math.hypot(Math.max(l, 0, -r), Math.max(t, 0, -b));
41
+ const far = Math.hypot(Math.max(-l, r), Math.max(-t, b));
42
+ if (near < inner || far > outer)
43
+ return false;
44
+ if (to - from >= 1)
45
+ return true;
46
+ return [
47
+ [l, t],
48
+ [r, t],
49
+ [r, b],
50
+ [l, b],
51
+ ].every(([px, py]) => {
52
+ const share = turnAt(px, py);
53
+ return share >= from && share < to;
54
+ });
55
+ }
56
+ export const turnAt = (dx, dy) => ((((Math.atan2(dy, dx) + TURN / 4) / TURN) % 1) + 1) % 1;
57
+ /**
58
+ * One min, max and last y per device-pixel column. x must be ascending.
59
+ * `first` (each column's first y) and `at` (its exact x) are kept only with `straight`;
60
+ * without it they alias `lo` and must not be read.
61
+ * A y that is not finite once projected, or a value of `also` at the same row that is not, ends a
62
+ * run: `runs` holds each run's first column, the column after its last and how many values it
63
+ * kept, and a pixel column a run ends in is split. A run also ends after a row in `cuts`, and is
64
+ * marked as following one when nothing else broke it since.
65
+ */
66
+ export function columns(x, y, ox, kx, oy, ky, straight = false, also, [from, to] = [0, x.length], cuts) {
67
+ const n = to - from;
68
+ const col = new Int32Array(n);
69
+ const lo = new Float64Array(n);
70
+ const hi = new Float64Array(n);
71
+ const last = new Float64Array(n);
72
+ const first = straight ? new Float64Array(n) : lo;
73
+ const at = straight ? new Float64Array(n) : lo;
74
+ const runs = [];
75
+ let cut = true;
76
+ let bridged = false;
77
+ let k = -1;
78
+ for (let i = from; i < to; i++) {
79
+ const p = ox + x[i] * kx;
80
+ const c = Math.round(p);
81
+ const v = oy - y[i] * ky;
82
+ if (!Number.isFinite(v) || (also && !Number.isFinite(oy - also[i] * ky))) {
83
+ cut = true;
84
+ bridged = false;
85
+ continue;
86
+ }
87
+ if (cut)
88
+ runs.push([k + 1, k + 1, 0, bridged]);
89
+ const run = runs[runs.length - 1];
90
+ run[2]++;
91
+ if (cut || c !== col[k]) {
92
+ k++;
93
+ run[1] = k + 1;
94
+ cut = false;
95
+ col[k] = c;
96
+ at[k] = p;
97
+ first[k] = lo[k] = hi[k] = v;
98
+ }
99
+ else if (v < lo[k])
100
+ lo[k] = v;
101
+ else if (v > hi[k])
102
+ hi[k] = v;
103
+ last[k] = v;
104
+ if (cuts?.has(i))
105
+ cut = bridged = true;
106
+ }
107
+ return { n: k + 1, col, lo, hi, last, first, at, runs };
108
+ }
109
+ /** Runs of columns() put together where each follows a cut, for `join` to draw as one. */
110
+ export function chains(runs) {
111
+ const out = [];
112
+ for (const run of runs) {
113
+ if (run[3] && out.length)
114
+ out[out.length - 1].push(run);
115
+ else
116
+ out.push([run]);
117
+ }
118
+ return out;
119
+ }
120
+ /**
121
+ * x and ys cut to the rows where every y is finite, and the kept rows a gap follows: the curve is
122
+ * drawn straight from each of those to the next.
123
+ */
124
+ export function joined(x, ...ys) {
125
+ const keep = [];
126
+ for (let i = 0; i < x.length; i++)
127
+ if (ys.every(y => Number.isFinite(y[i])))
128
+ keep.push(i);
129
+ const cuts = new Set();
130
+ for (let k = 1; k < keep.length; k++)
131
+ if (keep[k] !== keep[k - 1] + 1)
132
+ cuts.add(k - 1);
133
+ if (keep.length === x.length)
134
+ return [[x, ...ys], cuts];
135
+ const cut = (c) => Float64Array.from(keep, i => c[i]);
136
+ return [[cut(x), ...ys.map(cut)], cuts];
137
+ }
138
+ /** How many rows of an ascending x are at or below v. */
139
+ export function upTo(x, v) {
140
+ let a = 0;
141
+ let b = x.length;
142
+ while (a < b) {
143
+ const mid = (a + b) >> 1;
144
+ if (x[mid] <= v)
145
+ a = mid + 1;
146
+ else
147
+ b = mid;
148
+ }
149
+ return a;
150
+ }
151
+ /**
152
+ * The rows of an ascending x that a curve drawn between the device pixels lo and hi reads: those
153
+ * between them and `keep` pixel columns of rows beyond each, so no column is cut in two.
154
+ */
155
+ export function span(x, ox, kx, lo, hi, keep) {
156
+ const column = (i) => Math.round(ox + x[i] * kx);
157
+ let [a, b] = [upTo(x, (lo - ox) / kx), upTo(x, (hi - ox) / kx)];
158
+ if (a > b)
159
+ [a, b] = [b, a];
160
+ for (let k = 0; k < keep && a > 0; k++)
161
+ for (a--; a > 0 && column(a - 1) === column(a);)
162
+ a--;
163
+ for (let k = 0; k < keep && b < x.length; k++)
164
+ for (b++; b < x.length && column(b) === column(b - 1);)
165
+ b++;
166
+ return [a, b];
167
+ }
168
+ /** The least and greatest f(value) over the rows where f of every column is finite. */
169
+ export function extent(ys, f = (v) => v) {
170
+ let lo = Infinity;
171
+ let hi = -Infinity;
172
+ rows: for (let i = 0; i < ys[0].length; i++) {
173
+ for (const y of ys)
174
+ if (!Number.isFinite(f(y[i])))
175
+ continue rows;
176
+ for (const y of ys) {
177
+ const v = f(y[i]);
178
+ if (v < lo)
179
+ lo = v;
180
+ if (v > hi)
181
+ hi = v;
182
+ }
183
+ }
184
+ return [lo, hi];
185
+ }
186
+ /**
187
+ * The least and greatest oy - y * ky over the rows of ys, from their extent: when an end of it
188
+ * overflows once projected, the rows are scanned again for the ones that do not.
189
+ */
190
+ export function reach([lo, hi], ys, oy, ky) {
191
+ const [a, b] = [oy - lo * ky, oy - hi * ky];
192
+ if (!Number.isFinite(a) || !Number.isFinite(b))
193
+ return extent(ys, v => oy - v * ky);
194
+ return a < b ? [a, b] : [b, a];
195
+ }
196
+ /**
197
+ * y at v as the curve draws it: the held sample of a step, else the line or smooth() between
198
+ * samples, and the line from a row in `cuts` to the next. A row where y, or `also` like columns(), is not
199
+ * finite breaks the curve.
200
+ */
201
+ export function sampleAt(x, y, v, curve, also, cuts) {
202
+ const ok = (i) => Number.isFinite(y[i]) && (!also || Number.isFinite(also[i]));
203
+ const a = upTo(x, v);
204
+ if (!a)
205
+ return undefined;
206
+ const x0 = x[a - 1];
207
+ const y0 = y[a - 1];
208
+ if (!ok(a - 1))
209
+ return undefined;
210
+ if (x0 === v)
211
+ return y0;
212
+ if (a < x.length && !ok(a))
213
+ return undefined;
214
+ if (cuts?.has(a - 1))
215
+ curve = 'linear';
216
+ if (curve === 'step')
217
+ return y0;
218
+ if (a === x.length)
219
+ return undefined;
220
+ const [x1, y1] = [x[a], y[a]];
221
+ const t = (v - x0) / (x1 - x0);
222
+ if (curve !== 'smooth')
223
+ return y0 + (y1 - y0) * t;
224
+ const rise = (i) => i >= 0 && i + 1 < x.length && ok(i) && ok(i + 1) && !cuts?.has(i)
225
+ ? (y[i + 1] - y[i]) / (x[i + 1] - x[i] || 1)
226
+ : undefined;
227
+ const lean = (i) => {
228
+ const [p, q] = [rise(i - 1), rise(i)];
229
+ if (p === undefined || q === undefined)
230
+ return (p ?? q);
231
+ return p * q > 0 ? (2 * p * q) / (p + q) : 0;
232
+ };
233
+ const [m0, m1] = [lean(a - 1) * (x1 - x0), lean(a) * (x1 - x0)];
234
+ const s = 1 - t;
235
+ return s * s * (1 + 2 * t) * y0 + s * s * t * m0 + t * t * (3 - 2 * t) * y1 - t * t * s * m1;
236
+ }
@@ -0,0 +1,15 @@
1
+ import type { Col, Mark } from '../types.js';
2
+ import type { Common } from './common.js';
3
+ /**
4
+ * r is the radius of each point in CSS pixels. With `ring`, a width in CSS pixels, a point is a
5
+ * ring of its colour that wide round a middle of `fill`, the theme's surface by default; such points are
6
+ * always drawn on the canvas.
7
+ */
8
+ export type DotsOptions = Common & {
9
+ x: Col;
10
+ y: Col;
11
+ r: Col;
12
+ ring?: number;
13
+ fill?: string;
14
+ };
15
+ export declare function dots(o: DotsOptions): Mark;
@@ -0,0 +1,156 @@
1
+ import { glLayer, program, rgba } from '../gl.js';
2
+ import { base } from './common.js';
3
+ const TAU = 2 * Math.PI;
4
+ const origin = (c) => {
5
+ for (let i = 0; i < c.length; i++)
6
+ if (Number.isFinite(c[i]))
7
+ return c[i];
8
+ return 0;
9
+ };
10
+ const programs = new WeakMap();
11
+ function pointProgram(gl) {
12
+ const p = program(gl, 'in vec3 a;uniform vec4 m;uniform float dpr;out float size;void main(){gl_Position=vec4(a.x*m.x+m.y,a.y*m.z+m.w,0,1);size=a.z*2.0*dpr+1.0;gl_PointSize=size;}', 'precision mediump float;uniform vec4 color;uniform float hole;in float size;out vec4 o;void main(){float d=length(gl_PointCoord-0.5)*size;float al=clamp(size*0.5-d,0.0,1.0)*color.a;if(hole>0.0)al*=clamp(d-size*0.5+0.5+hole,0.0,1.0);if(al<=0.0)discard;o=vec4(color.rgb*al,al);}');
13
+ const a = gl.getAttribLocation(p, 'a');
14
+ gl.enableVertexAttribArray(a);
15
+ const uniform = (name) => gl.getUniformLocation(p, name);
16
+ return { p, a, m: uniform('m'), dpr: uniform('dpr'), hole: uniform('hole'), color: uniform('color') };
17
+ }
18
+ export function dots(o) {
19
+ const { x, y, r, color, alpha = 1 } = o;
20
+ const n = x.length;
21
+ const buffers = new WeakMap();
22
+ let reach = 0;
23
+ for (let i = 0; i < n; i++)
24
+ reach = Math.max(reach, r[i]);
25
+ const overhang = reach + (o.ring ?? 0) / 2;
26
+ // ponytail: linear scan per pointer move, fine at 100k points; a grid index when it shows in a profile.
27
+ const hit = ({ ox, kx, oy, ky, dpr, left, top, width, height }) => (px, py, max) => {
28
+ let best = max ** 2;
29
+ let at = -1;
30
+ for (let i = 0; i < n; i++) {
31
+ const cx = ox + x[i] * kx;
32
+ const cy = oy - y[i] * ky;
33
+ if (cx < left || cx > left + width || cy < top || cy > top + height)
34
+ continue;
35
+ const d = (cx / dpr - px) ** 2 + (cy / dpr - py) ** 2;
36
+ if (d < best)
37
+ [best, at] = [d, i];
38
+ }
39
+ return at < 0 ? null : { x: x[at], y: y[at], distance: Math.sqrt(best), row: at };
40
+ };
41
+ const prepareGl = (view, { gl }) => {
42
+ const u = programs.get(gl) ?? pointProgram(gl);
43
+ programs.set(gl, u);
44
+ // The offsets are subtracted in float64: a float32 attribute cannot hold e.g. millisecond timestamps.
45
+ // They come from the data, not the domain, so a new view changes only the uniform and never the buffer.
46
+ const x0 = origin(x);
47
+ const y0 = origin(y);
48
+ let buffer = buffers.get(gl);
49
+ if (!buffer) {
50
+ const data = new Float32Array(n * 3);
51
+ for (let i = 0; i < n; i++) {
52
+ data[3 * i] = x[i] - x0;
53
+ data[3 * i + 1] = y[i] - y0;
54
+ data[3 * i + 2] = Math.max(0, r[i]);
55
+ }
56
+ buffer = gl.createBuffer();
57
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
58
+ gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
59
+ buffers.set(gl, buffer);
60
+ }
61
+ const { ctx, kx, ox, ky, oy, dpr } = view;
62
+ const [red, green, blue, opacity = 1] = rgba(ctx, color);
63
+ const ready = () => {
64
+ const { width: w, height: h } = gl.canvas;
65
+ gl.useProgram(u.p);
66
+ gl.uniform4f(u.m, (kx * 2) / w, ((ox + x0 * kx) * 2) / w - 1, (ky * 2) / h, 1 - ((oy - y0 * ky) * 2) / h);
67
+ gl.uniform1f(u.dpr, dpr);
68
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
69
+ gl.vertexAttribPointer(u.a, 3, gl.FLOAT, false, 0, 0);
70
+ };
71
+ const own = () => {
72
+ gl.uniform1f(u.hole, 0);
73
+ gl.uniform4f(u.color, red, green, blue, alpha * opacity);
74
+ };
75
+ return {
76
+ // The alpha argument is unused: dense points saturate, so the layer's fade() dims a group instead.
77
+ paint() {
78
+ ready();
79
+ own();
80
+ gl.drawArrays(gl.POINTS, 0, n);
81
+ },
82
+ select(rows, outline) {
83
+ ready();
84
+ own();
85
+ for (const i of rows)
86
+ gl.drawArrays(gl.POINTS, i, 1);
87
+ const [r2, g2, b2, a2 = 1] = rgba(ctx, outline);
88
+ gl.uniform1f(u.hole, dpr);
89
+ gl.uniform4f(u.color, r2, g2, b2, a2);
90
+ for (const i of rows)
91
+ gl.drawArrays(gl.POINTS, i, 1);
92
+ },
93
+ hit: hit(view),
94
+ };
95
+ };
96
+ return {
97
+ ...base('dots', o, [x], [y]),
98
+ overhang,
99
+ rowsAlike: true,
100
+ prepare(view) {
101
+ const { ctx, dpr, ox, kx, oy, ky, theme, left, top, width, height } = view;
102
+ // A point further out than twice the overhang cannot reach the area its mark is cut to.
103
+ const out = 2 * overhang * dpr;
104
+ const circle = (path, i, grow = 0) => {
105
+ const px = ox + x[i] * kx;
106
+ const py = oy - y[i] * ky;
107
+ if (px < left - out || px > left + width + out || py < top - out || py > top + height + out)
108
+ return;
109
+ const radius = Math.max(0, r[i] * dpr + grow);
110
+ path.moveTo(px + radius, py);
111
+ path.arc(px, py, radius, 0, TAU);
112
+ };
113
+ const path = new Path2D();
114
+ for (let i = 0; i < n; i++)
115
+ circle(path, i);
116
+ const fill = (shape, a) => {
117
+ ctx.globalAlpha = alpha * a;
118
+ ctx.fillStyle = o.ring ? (o.fill ?? theme.surface) : color;
119
+ ctx.fill(shape);
120
+ if (!o.ring)
121
+ return;
122
+ ctx.strokeStyle = color;
123
+ ctx.lineWidth = o.ring * dpr;
124
+ ctx.stroke(shape);
125
+ };
126
+ return {
127
+ paint: a => fill(path, a),
128
+ select(rows, outline) {
129
+ const some = new Path2D();
130
+ const edge = new Path2D();
131
+ for (const i of rows) {
132
+ circle(some, i);
133
+ circle(edge, i, ((o.ring ?? 0) / 2) * dpr);
134
+ }
135
+ fill(some, 1);
136
+ ctx.save();
137
+ ctx.clip(edge);
138
+ ctx.globalAlpha = 1;
139
+ ctx.strokeStyle = outline;
140
+ ctx.lineWidth = 2 * dpr;
141
+ ctx.stroke(edge);
142
+ ctx.restore();
143
+ },
144
+ hit: hit(view),
145
+ };
146
+ },
147
+ gl: o.ring ? undefined : { layer: glLayer, prepare: prepareGl },
148
+ release(layer) {
149
+ const buffer = layer && buffers.get(layer.gl);
150
+ if (!buffer)
151
+ return;
152
+ layer.gl.deleteBuffer(buffer);
153
+ buffers.delete(layer.gl);
154
+ },
155
+ };
156
+ }