swiftplot 0.0.1 → 0.3.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 (115) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +348 -3
  3. package/dist/chart.d.ts +7 -0
  4. package/dist/chart.js +1311 -0
  5. package/dist/charts/build.d.ts +52 -0
  6. package/dist/charts/build.js +63 -0
  7. package/dist/charts/candlestick.d.ts +8 -0
  8. package/dist/charts/candlestick.js +105 -0
  9. package/dist/charts/combo.d.ts +8 -0
  10. package/dist/charts/combo.js +50 -0
  11. package/dist/charts/controller.d.ts +32 -0
  12. package/dist/charts/controller.js +231 -0
  13. package/dist/charts/defaults.d.ts +20 -0
  14. package/dist/charts/defaults.js +121 -0
  15. package/dist/charts/distribution.d.ts +15 -0
  16. package/dist/charts/distribution.js +178 -0
  17. package/dist/charts/fields.d.ts +64 -0
  18. package/dist/charts/fields.js +251 -0
  19. package/dist/charts/flow.d.ts +15 -0
  20. package/dist/charts/flow.js +189 -0
  21. package/dist/charts/heatmap.d.ts +9 -0
  22. package/dist/charts/heatmap.js +104 -0
  23. package/dist/charts/index.d.ts +13 -0
  24. package/dist/charts/index.js +10 -0
  25. package/dist/charts/mount.d.ts +5 -0
  26. package/dist/charts/mount.js +21 -0
  27. package/dist/charts/parts.d.ts +23 -0
  28. package/dist/charts/parts.js +66 -0
  29. package/dist/charts/radar.d.ts +7 -0
  30. package/dist/charts/radar.js +77 -0
  31. package/dist/charts/source.d.ts +24 -0
  32. package/dist/charts/source.js +225 -0
  33. package/dist/charts/types.d.ts +643 -0
  34. package/dist/charts/types.js +1 -0
  35. package/dist/charts/xy.d.ts +46 -0
  36. package/dist/charts/xy.js +421 -0
  37. package/dist/features/animate.d.ts +20 -0
  38. package/dist/features/animate.js +118 -0
  39. package/dist/features/brush.d.ts +15 -0
  40. package/dist/features/brush.js +128 -0
  41. package/dist/features/legend.d.ts +13 -0
  42. package/dist/features/legend.js +236 -0
  43. package/dist/features/pointer.d.ts +29 -0
  44. package/dist/features/pointer.js +23 -0
  45. package/dist/features/tooltip.d.ts +22 -0
  46. package/dist/features/tooltip.js +192 -0
  47. package/dist/features/zoom.d.ts +14 -0
  48. package/dist/features/zoom.js +246 -0
  49. package/dist/gl.d.ts +5 -0
  50. package/dist/gl.js +52 -0
  51. package/dist/index.d.ts +62 -0
  52. package/dist/index.js +32 -0
  53. package/dist/marks/arcs.d.ts +31 -0
  54. package/dist/marks/arcs.js +125 -0
  55. package/dist/marks/area.d.ts +25 -0
  56. package/dist/marks/area.js +106 -0
  57. package/dist/marks/bar.d.ts +52 -0
  58. package/dist/marks/bar.js +243 -0
  59. package/dist/marks/box.d.ts +33 -0
  60. package/dist/marks/box.js +182 -0
  61. package/dist/marks/candlestick.d.ts +22 -0
  62. package/dist/marks/candlestick.js +133 -0
  63. package/dist/marks/common.d.ts +102 -0
  64. package/dist/marks/common.js +250 -0
  65. package/dist/marks/dots.d.ts +16 -0
  66. package/dist/marks/dots.js +158 -0
  67. package/dist/marks/errorBars.d.ts +24 -0
  68. package/dist/marks/errorBars.js +99 -0
  69. package/dist/marks/funnel.d.ts +30 -0
  70. package/dist/marks/funnel.js +122 -0
  71. package/dist/marks/heatmap.d.ts +20 -0
  72. package/dist/marks/heatmap.js +139 -0
  73. package/dist/marks/line.d.ts +21 -0
  74. package/dist/marks/line.js +75 -0
  75. package/dist/marks/radar.d.ts +29 -0
  76. package/dist/marks/radar.js +162 -0
  77. package/dist/marks/radialBars.d.ts +34 -0
  78. package/dist/marks/radialBars.js +111 -0
  79. package/dist/marks/rects.d.ts +43 -0
  80. package/dist/marks/rects.js +120 -0
  81. package/dist/marks/rule.d.ts +47 -0
  82. package/dist/marks/rule.js +83 -0
  83. package/dist/marks/sankey.d.ts +53 -0
  84. package/dist/marks/sankey.js +369 -0
  85. package/dist/marks/sunburst.d.ts +31 -0
  86. package/dist/marks/sunburst.js +186 -0
  87. package/dist/marks/ticks.d.ts +12 -0
  88. package/dist/marks/ticks.js +26 -0
  89. package/dist/marks/tree.d.ts +49 -0
  90. package/dist/marks/tree.js +113 -0
  91. package/dist/marks/treemap.d.ts +30 -0
  92. package/dist/marks/treemap.js +164 -0
  93. package/dist/paint.d.ts +35 -0
  94. package/dist/paint.js +71 -0
  95. package/dist/scale.d.ts +16 -0
  96. package/dist/scale.js +97 -0
  97. package/dist/scales/band.d.ts +12 -0
  98. package/dist/scales/band.js +27 -0
  99. package/dist/scales/bare.d.ts +6 -0
  100. package/dist/scales/bare.js +10 -0
  101. package/dist/scales/color.d.ts +20 -0
  102. package/dist/scales/color.js +21 -0
  103. package/dist/scales/log.d.ts +27 -0
  104. package/dist/scales/log.js +98 -0
  105. package/dist/scales/time.d.ts +37 -0
  106. package/dist/scales/time.js +183 -0
  107. package/dist/text.d.ts +8 -0
  108. package/dist/text.js +33 -0
  109. package/dist/theme.d.ts +10 -0
  110. package/dist/theme.js +8 -0
  111. package/dist/transforms.d.ts +47 -0
  112. package/dist/transforms.js +119 -0
  113. package/dist/types.d.ts +465 -0
  114. package/dist/types.js +1 -0
  115. package/package.json +31 -6
@@ -0,0 +1,106 @@
1
+ import { stack } from '../transforms.js';
2
+ import { painter } from '../paint.js';
3
+ import { base, chains, columns, extent, joined, reach, sampleAt, smooth, span, stackKey } from './common.js';
4
+ const layers = (members) => stack(members.map(m => m.y)).map((laid, i) => {
5
+ const { y: _, stack: __, ...rest } = members[i];
6
+ return area({ ...rest, ...laid });
7
+ });
8
+ export function area(o) {
9
+ if ('y' in o && o.stack !== undefined) {
10
+ return {
11
+ ...layers([o])[0],
12
+ options: o,
13
+ arrange: {
14
+ key: stackKey('area', o),
15
+ lay: members => layers(members.map(k => k.options)),
16
+ },
17
+ };
18
+ }
19
+ const { color, curve = 'linear', alpha = 1 } = o;
20
+ const step = curve === 'step';
21
+ const given = 'y' in o ? [o.y] : [o.y2, o.y1];
22
+ const [[x, y2, y1], cuts] = o.join ? joined(o.x, ...given) : [[o.x, ...given]];
23
+ const outlines = y1 ? [y2, y1] : [y2];
24
+ const ys = o.fill ? extent(outlines) : undefined;
25
+ return {
26
+ ...base('area', o, [o.x], [given[1] ?? (o.x.length ? [0] : []), given[0]]),
27
+ oneShape: true,
28
+ sampleAtX(v, straight) {
29
+ // A step line holds its last value, but the fill ends at the last x.
30
+ if (v > x[x.length - 1])
31
+ return undefined;
32
+ const drawn = straight && curve === 'smooth' ? 'linear' : curve;
33
+ const top = sampleAt(x, y2, v, drawn, y1, cuts);
34
+ const bottom = y1 ? sampleAt(x, y1, v, drawn, y2, cuts) : 0;
35
+ if (top === undefined || bottom === undefined)
36
+ return undefined;
37
+ return { y1: bottom, y2: top, band: y1 !== undefined };
38
+ },
39
+ prepare({ ctx, ox, kx, oy, ky, left, width }) {
40
+ // Both outlines break where either value is missing, so their runs share their columns.
41
+ // A pixel column's first point can lie up to half a pixel inside the plot, so the area reads one further.
42
+ const rows = span(x, ox, kx, left - 1, left + width + 1, curve === 'smooth' ? 2 : 1);
43
+ const u = columns(x, y2, ox, kx, oy, ky, !step || o.join, y1, rows, cuts);
44
+ const l = y1 && columns(x, y1, ox, kx, oy, ky, !step || o.join, y2, rows, cuts);
45
+ const ux = step ? u.col : u.at;
46
+ const lx = l && (step ? l.col : l.at);
47
+ const path = new Path2D();
48
+ for (const runs of chains(u.runs)) {
49
+ runs.forEach(([a, b, kept], j) => {
50
+ const go = j ? 'lineTo' : 'moveTo';
51
+ if (curve === 'smooth' && kept === b - a) {
52
+ path[go](u.at[a], u.first[a]);
53
+ smooth(path, u.at.subarray(a, b), u.first.subarray(a, b), b - a);
54
+ return;
55
+ }
56
+ for (let i = a; i < b; i++) {
57
+ const px = ux[i];
58
+ if (i === a)
59
+ path[go](px, step && !j ? u.lo[i] : u.first[i]);
60
+ else
61
+ path.lineTo(px, step ? u.last[i - 1] : u.first[i]);
62
+ path.lineTo(px, u.lo[i]);
63
+ path.lineTo(px, u.last[i]);
64
+ }
65
+ });
66
+ for (const [a, b, kept] of l ? [...runs].reverse() : []) {
67
+ if (curve === 'smooth' && kept === b - a) {
68
+ path.lineTo(l.at[b - 1], l.first[b - 1]);
69
+ smooth(path, l.at.subarray(a, b), l.first.subarray(a, b), b - a, true);
70
+ continue;
71
+ }
72
+ for (let i = b - 1; i >= a; i--) {
73
+ const px = lx[i];
74
+ path.lineTo(px, l.last[i]);
75
+ path.lineTo(px, l.hi[i]);
76
+ if (!step || i > a)
77
+ path.lineTo(px, step ? l.last[i - 1] : l.first[i]);
78
+ else if (a > runs[0][0])
79
+ path.lineTo(px, l.first[i]);
80
+ }
81
+ }
82
+ if (!l) {
83
+ path.lineTo(ux[runs[runs.length - 1][1] - 1], oy);
84
+ path.lineTo(ux[runs[0][0]], oy);
85
+ }
86
+ path.closePath();
87
+ }
88
+ let ink = color;
89
+ if (u.n && o.fill) {
90
+ // How far up and down the whole area reaches, for a blend to run across.
91
+ const [high, low] = reach(ys, outlines, oy, ky);
92
+ const box = l
93
+ ? [left, high, left + width, low]
94
+ : [left, Math.min(oy, high), left + width, Math.max(oy, low)];
95
+ ink = painter(ctx, o.fill, color)(box, l ? undefined : oy);
96
+ }
97
+ return {
98
+ paint(a) {
99
+ ctx.globalAlpha = alpha * a;
100
+ ctx.fillStyle = ink;
101
+ ctx.fill(path);
102
+ },
103
+ };
104
+ },
105
+ };
106
+ }
@@ -0,0 +1,52 @@
1
+ import { type Paint } from '../paint.js';
2
+ import type { Col, Mark } from '../types.js';
3
+ import type { Common } from './common.js';
4
+ /**
5
+ * One rectangle per row, from (x1, y1) to (x2, y2) in data units; either pair may be in any order.
6
+ * A single `y` (or `x`) stands for a pair that starts at zero. On the canvas blended bars are
7
+ * filled one at a time.
8
+ */
9
+ export type BarOptions = Common & {
10
+ /** Fills the bars with something other than their colour; a blend runs over each bar, and an upright one on an unstacked `y` bar from its value end to the baseline. */
11
+ fill?: Paint;
12
+ /** Rounds by so many CSS pixels the value end of a bar from zero, or every corner; WebGL bars stay square. */
13
+ radius?: number;
14
+ /** Takes so many CSS pixels off the side of each bar, to set touching bars apart; a bar keeps a pixel. */
15
+ gap?: number;
16
+ } & ((({
17
+ /** One end of each bar along x, in data units. */
18
+ x1: Col;
19
+ /** The other end of each bar along x. */
20
+ x2: Col;
21
+ } | {
22
+ /** Where each bar ends along x; it starts at zero. */
23
+ x: Col;
24
+ }) & ({
25
+ /** One end of each bar along y, in data units. */
26
+ y1: Col;
27
+ /** The other end of each bar along y. */
28
+ y2: Col;
29
+ } | {
30
+ /** Where each bar ends along y; it starts at zero. */
31
+ y: Col;
32
+ }) & {
33
+ stack?: never;
34
+ dodge?: never;
35
+ width?: never;
36
+ }) | ({
37
+ /** The place of each bar's category on a band axis. */
38
+ x: Col;
39
+ /** Each bar's value. */
40
+ y: Col;
41
+ /** How much of x the bars of one place take together, in data units. @default 0.8 */
42
+ width?: number;
43
+ } & ({
44
+ /** Bars with the same id are stacked over one another, in the order listed, among those shown. */
45
+ stack: string;
46
+ dodge?: never;
47
+ } | {
48
+ /** Bars with the same id stand side by side in their category, in the order listed, among those shown. */
49
+ dodge: string;
50
+ stack?: never;
51
+ })));
52
+ export declare function bar(o: BarOptions): Mark;
@@ -0,0 +1,243 @@
1
+ import { glLayer } from '../gl.js';
2
+ import { painter, stops } from '../paint.js';
3
+ import { bands, stack } from '../transforms.js';
4
+ import { base, stackKey } from './common.js';
5
+ import { focused, gpuRows } from './rects.js';
6
+ const arranged = (members) => {
7
+ const stacked = members[0]?.stack !== undefined && stack(members.map(m => m.y));
8
+ return members.map((m, i) => {
9
+ const { x, y, stack: _, dodge: __, width, ...rest } = m;
10
+ const [x1, x2] = bands(x, stacked ? { width } : { slot: i, of: members.length, width });
11
+ return stacked ? make({ ...rest, x1, x2, ...stacked[i] }, y) : make({ ...rest, x1, x2, y });
12
+ });
13
+ };
14
+ const bounds = (o) => ({
15
+ x1: 'x' in o ? undefined : o.x1,
16
+ x2: 'x' in o ? o.x : o.x2,
17
+ y1: 'y' in o ? undefined : o.y1,
18
+ y2: 'y' in o ? o.y : o.y2,
19
+ });
20
+ // ponytail: a morph rebuilds every bar each frame, so beyond this many they cross-fade instead.
21
+ const MORPH_MAX = 2000;
22
+ // Edges are snapped to device pixels so neighbours meet without a seam. Holds l, t, r, b per bar,
23
+ // as doubles: a bar far outside the domain must not wrap round into view as a 32-bit integer would.
24
+ function boxes({ x1, x2, y1, y2 }, { ox, kx, oy, ky }, [gapX, gapY] = [0, 0]) {
25
+ const box = new Float64Array(x2.length * 4);
26
+ for (let i = 0, j = 0; j < box.length; i++, j += 4) {
27
+ const xa = x1 ? x1[i] : 0;
28
+ const xb = x2[i];
29
+ const ya = y1 ? y1[i] : 0;
30
+ const yb = y2[i];
31
+ const l = (box[j] = Math.round(ox + Math.min(xa, xb) * kx));
32
+ const t = (box[j + 1] = Math.round(oy - Math.max(ya, yb) * ky));
33
+ // A bar with a size keeps one pixel of it, so a narrow bin or a small value stays visible.
34
+ const r = Math.max(Math.round(ox + Math.max(xa, xb) * kx), l + +(xa !== xb));
35
+ const b = Math.max(Math.round(oy - Math.min(ya, yb) * ky), t + +(ya !== yb));
36
+ box[j + 2] = r - Math.max(0, Math.min(gapX, r - l - 1));
37
+ box[j + 3] = b - Math.max(0, Math.min(gapY, b - t - 1));
38
+ }
39
+ return box;
40
+ }
41
+ function rects(box) {
42
+ const path = new Path2D();
43
+ for (let j = 0; j < box.length; j += 4) {
44
+ path.rect(box[j], box[j + 1], box[j + 2] - box[j], box[j + 3] - box[j + 1]);
45
+ }
46
+ return path;
47
+ }
48
+ export function bar(o) {
49
+ if (o.stack === undefined && o.dodge === undefined)
50
+ return make(o);
51
+ const a = o;
52
+ return {
53
+ ...arranged([a])[0],
54
+ options: o,
55
+ arrange: {
56
+ key: a.stack !== undefined ? stackKey('bar', a) : `bar:dodge:${a.dodge}`,
57
+ lay: members => arranged(members.map(k => k.options)),
58
+ },
59
+ };
60
+ }
61
+ function make(o, value) {
62
+ const { color, alpha = 1 } = o;
63
+ const own = bounds(o);
64
+ const { x1, x2, y2 } = own;
65
+ const n = x2.length;
66
+ const zero = n ? [0] : [];
67
+ const corner = o.radius ?? 0;
68
+ // The gap comes off a bar's thickness: its width, or its height when it lies down.
69
+ const flat = 'x' in o && !('y' in o);
70
+ const gaps = (dpr) => {
71
+ const g = Math.round((o.gap ?? 0) * dpr);
72
+ return flat ? [0, g] : [g, 0];
73
+ };
74
+ const laid = (of, view) => boxes(of, view, gaps(view.dpr));
75
+ const trace = (path, box, i, dpr) => {
76
+ const j = i * 4;
77
+ const [l, t, w, h] = [box[j], box[j + 1], box[j + 2] - box[j], box[j + 3] - box[j + 1]];
78
+ const r = Math.min(corner * dpr, w / 2, h / 2);
79
+ if (!(r >= 1))
80
+ return path.rect(l, t, w, h);
81
+ // Top left, top right, bottom right, bottom left: the value end of a bar that starts at zero.
82
+ const up = y2[i] >= 0 ? [r, r, 0, 0] : [0, 0, r, r];
83
+ const along = x2[i] >= 0 ? [0, r, r, 0] : [r, 0, 0, r];
84
+ const grounded = (own.y1 ? own.y1[i] : 0) === 0;
85
+ path.roundRect(l, t, w, h, flat ? along : grounded ? up : r);
86
+ };
87
+ const shaped = (box, dpr) => {
88
+ if (!corner)
89
+ return rects(box);
90
+ const path = new Path2D();
91
+ for (let i = 0; i < n; i++)
92
+ trace(path, box, i, dpr);
93
+ return path;
94
+ };
95
+ const blended = typeof o.fill === 'object' ? o.fill : undefined;
96
+ const upright = blended?.toward === 'down' || blended?.toward === 'up';
97
+ const rows = gpuRows(new WeakMap(), [x1, x2], [own.y1, y2]);
98
+ // ponytail: linear scan per pointer move. The value end is y2, or x2 for a bar given as a
99
+ // single x; a horizontal bar given all four bounds still reports as a vertical one.
100
+ const hit = ({ dpr, left, top, width, height }, boxed) => (px, py, max) => {
101
+ const box = boxed();
102
+ const x = px * dpr;
103
+ const y = py * dpr;
104
+ if (x < left || x > left + width || y < top || y > top + height)
105
+ return null;
106
+ for (let i = n - 1; i >= 0; i--) {
107
+ const j = i * 4;
108
+ if (x >= box[j] && y >= box[j + 1] && x < box[j + 2] && y < box[j + 3]) {
109
+ const y1i = own.y1 ? own.y1[i] : 0;
110
+ return 'x' in o
111
+ ? { x: x2[i], y: (y1i + y2[i]) / 2, distance: max, row: i }
112
+ : {
113
+ x: ((x1 ? x1[i] : 0) + x2[i]) / 2,
114
+ y: y2[i],
115
+ distance: max,
116
+ row: i,
117
+ ...(value && { value: value[i] }),
118
+ };
119
+ }
120
+ }
121
+ return null;
122
+ };
123
+ return {
124
+ ...base('bar', o, [x2, x1 ?? zero], [y2, own.y1 ?? zero]),
125
+ prepare(view, previous) {
126
+ const { ctx } = view;
127
+ const box = laid(own, view);
128
+ const full = shaped(box, view.dpr);
129
+ const ink = painter(ctx, o.fill, color);
130
+ let path = full;
131
+ // What is drawn now, which during a move is not yet `box`; hit() keeps answering for `box`.
132
+ let shown = box;
133
+ // Rows are matched by position, so the previous bars must be as many.
134
+ const before = previous?.type === 'bar' && previous.points === n && n <= MORPH_MAX
135
+ ? previous.options
136
+ : undefined;
137
+ let start;
138
+ return {
139
+ paint(k, row, rest = 1) {
140
+ for (let i = 0, j = 0; blended && i < n; i++, j += 4) {
141
+ ctx.globalAlpha = alpha * k * (row === undefined || row >= n || row === i ? 1 : rest);
142
+ ctx.fillStyle = ink([...shown.subarray(j, j + 4)], 'y' in o ? view.oy : undefined);
143
+ ctx.beginPath();
144
+ trace(ctx, shown, i, view.dpr);
145
+ ctx.fill();
146
+ }
147
+ if (blended)
148
+ return;
149
+ ctx.fillStyle = ink([]);
150
+ ctx.globalAlpha = alpha * k;
151
+ if (row === undefined || row >= n)
152
+ return ctx.fill(path);
153
+ const one = new Path2D();
154
+ trace(one, shown, row, view.dpr);
155
+ ctx.fill(one);
156
+ // The rest is drawn through a hole where that bar is, so it is not painted twice.
157
+ const around = new Path2D(one);
158
+ around.rect(0, 0, ctx.canvas.width, ctx.canvas.height);
159
+ ctx.save();
160
+ ctx.clip(around, 'evenodd');
161
+ ctx.globalAlpha = alpha * k * rest;
162
+ ctx.fill(path);
163
+ ctx.restore();
164
+ },
165
+ select(rows, outline) {
166
+ for (const i of rows) {
167
+ const one = new Path2D();
168
+ trace(one, shown, i, view.dpr);
169
+ ctx.globalAlpha = alpha;
170
+ ctx.fillStyle = ink([...shown.subarray(i * 4, i * 4 + 4)], 'y' in o ? view.oy : undefined);
171
+ ctx.fill(one);
172
+ ctx.save();
173
+ ctx.clip(one);
174
+ ctx.globalAlpha = 1;
175
+ ctx.strokeStyle = outline;
176
+ ctx.lineWidth = 2 * view.dpr;
177
+ ctx.stroke(one);
178
+ ctx.restore();
179
+ }
180
+ },
181
+ morph: before &&
182
+ (t => {
183
+ if (t >= 1)
184
+ return void ([path, shown] = [full, box]);
185
+ // In this view, not the one the previous bars were drawn in: the axes have already moved.
186
+ const from = (start ??= laid(bounds(before), view));
187
+ shown = box.map((v, i) => Math.round(from[i] + (v - from[i]) * t));
188
+ path = shaped(shown, view.dpr);
189
+ }),
190
+ hit: hit(view, () => box),
191
+ };
192
+ },
193
+ gl: {
194
+ layer: glLayer,
195
+ under: true,
196
+ prepare(view, { gl }) {
197
+ const based = upright && 'y' in o;
198
+ const blend = blended && {
199
+ stops: stops(view.ctx, blended, color, based),
200
+ upright,
201
+ based,
202
+ };
203
+ const { draw, once } = rows.on(view, gl, typeof o.fill === 'string' ? o.fill : color, 0, blend);
204
+ let box;
205
+ // Moved and shrunk by half the gap each, the far edge alone gives way, as on the canvas.
206
+ const [gx, gy] = gaps(view.dpr);
207
+ const part = {
208
+ xa: 0,
209
+ xb: 1,
210
+ ya: 2,
211
+ yb: 3,
212
+ snap: true,
213
+ shift: [-gx / 2, -gy / 2],
214
+ pad: [-gx / 2, -gy / 2],
215
+ };
216
+ return {
217
+ paint(k, row, rest = 1) {
218
+ once(() => {
219
+ for (const [strength, ...run] of focused(n, row, rest))
220
+ draw(part, alpha * k * strength, ...run);
221
+ });
222
+ },
223
+ // A pixel keeps the first rectangle to reach it, so the inset fill leaves a ring of the outline.
224
+ select(chosen, outline) {
225
+ const ring = rows.on(view, gl, outline).draw;
226
+ const inset = { ...part, pad: part.pad.map(p => p - view.dpr) };
227
+ once(() => {
228
+ for (const i of chosen)
229
+ draw(inset, alpha, i, 1);
230
+ for (const i of chosen)
231
+ ring(part, 1, i, 1);
232
+ });
233
+ },
234
+ hit: hit(view, () => (box ??= laid(own, view))),
235
+ };
236
+ },
237
+ },
238
+ release(layer) {
239
+ if (layer)
240
+ rows.release(layer.gl);
241
+ },
242
+ };
243
+ }
@@ -0,0 +1,33 @@
1
+ import { type Paint } from '../paint.js';
2
+ import type { Col, Mark } from '../types.js';
3
+ import type { Common } from './common.js';
4
+ /**
5
+ * A box from q1 to q3 with a line at the median and whiskers out to min and max, one per x.
6
+ * ponytail: upright only, and the whiskers go to whatever min and max are given; outliers are
7
+ * for the caller to leave out and draw with dots().
8
+ */
9
+ export type BoxOptions = Common & {
10
+ /** Where each box stands along x. */
11
+ x: Col;
12
+ /** Where the lower whisker ends. */
13
+ min: Col;
14
+ /** The bottom of the box, the first quartile. */
15
+ q1: Col;
16
+ /** Where the line across the box is drawn. */
17
+ median: Col;
18
+ /** The top of the box, the third quartile. */
19
+ q3: Col;
20
+ /** Where the upper whisker ends. */
21
+ max: Col;
22
+ /** The box's width in x units, so 0.6 on a band axis is 60% of the category. @default 0.6 */
23
+ width?: number;
24
+ /** What the boxes are filled with: a colour, drawn faint, or a blend down each box, drawn as given. */
25
+ fill?: Paint;
26
+ /** Rounds the boxes' corners by so many CSS pixels; boxes drawn by WebGL stay square. */
27
+ radius?: number;
28
+ /** The width of the median line in CSS pixels; 1.5 like the other lines unless given. */
29
+ medianWidth?: number;
30
+ /** The opacity of the whiskers against the rest; 1 unless given. */
31
+ whiskers?: number;
32
+ };
33
+ export declare function box(o: BoxOptions): Mark;
@@ -0,0 +1,182 @@
1
+ import { glLayer } from '../gl.js';
2
+ import { painter, stops } from '../paint.js';
3
+ import { base, ends } 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
+ const [first, last] = ends(x);
12
+ const rows = gpuRows(new WeakMap(), [x], [min, q1, median, q3, max]);
13
+ // Each box's left, top, right and bottom in device pixels; a row with a number missing has none.
14
+ const bounds = ({ ox, kx, oy, ky }) => {
15
+ const edges = new Float64Array(n * 4).fill(NaN);
16
+ for (let i = 0; i < n; i++) {
17
+ if (![x, min, q1, median, q3, max].every(c => Number.isFinite(c[i])))
18
+ continue;
19
+ const mid = ox + x[i] * kx;
20
+ const l = Math.round(mid - (width * kx) / 2);
21
+ const r = Math.max(Math.round(mid + (width * kx) / 2), l + 1);
22
+ edges.set([l, Math.round(oy - q3[i] * ky), r, Math.round(oy - q1[i] * ky)], i * 4);
23
+ }
24
+ return edges;
25
+ };
26
+ // The box itself answers, with its median; reported as the weakest hit like any filled shape.
27
+ const hit = ({ dpr, left, top, width: plotWidth, height }, bounded) => (px, py, reach) => {
28
+ const [hx, hy] = [px * dpr, py * dpr];
29
+ if (hx < left || hx > left + plotWidth || hy < top || hy > top + height)
30
+ return null;
31
+ const edges = bounded();
32
+ for (let i = n - 1; i >= 0; i--) {
33
+ const j = i * 4;
34
+ if (hx >= edges[j] && hy >= edges[j + 1] && hx < edges[j + 2] && hy < edges[j + 3]) {
35
+ return { x: x[i], y: median[i], distance: reach, row: i };
36
+ }
37
+ }
38
+ return null;
39
+ };
40
+ return {
41
+ ...base('box', o, [x, [first - width / 2, last + width / 2]], [min, max]),
42
+ overhang: 0.75,
43
+ prepare(view) {
44
+ const { ctx, dpr, ox, kx, oy, ky } = view;
45
+ const edges = bounds(view);
46
+ const y = (v) => Math.round(oy - v * ky);
47
+ const shape = (path, i) => {
48
+ const [l, t, w, h] = [
49
+ edges[i * 4],
50
+ edges[i * 4 + 1],
51
+ edges[i * 4 + 2] - edges[i * 4],
52
+ edges[i * 4 + 3] - edges[i * 4 + 1],
53
+ ];
54
+ const r = Math.min(corner * dpr, w / 2, h / 2);
55
+ if (r >= 1)
56
+ path.roundRect(l, t, w, h, r);
57
+ else
58
+ path.rect(l, t, w, h);
59
+ };
60
+ // The bodies, their outlines, the whiskers with their ends, and the medians.
61
+ const paths = (rows) => {
62
+ const [body, frame, whisk, mid] = [new Path2D(), new Path2D(), new Path2D(), new Path2D()];
63
+ for (let i = 0; i < n; i++) {
64
+ const [l, t, r, b] = [edges[i * 4], edges[i * 4 + 1], edges[i * 4 + 2], edges[i * 4 + 3]];
65
+ // A row with a number missing draws nothing: see errorBars for what a NaN does to a path.
66
+ if (!rows(i) || Number.isNaN(l))
67
+ continue;
68
+ const centre = ox + x[i] * kx;
69
+ shape(body, i);
70
+ shape(frame, i);
71
+ for (const [from, to] of [
72
+ [t, y(max[i])],
73
+ [b, y(min[i])],
74
+ ]) {
75
+ whisk.moveTo(centre, from);
76
+ whisk.lineTo(centre, to);
77
+ whisk.moveTo(centre - (r - l) / 4, to);
78
+ whisk.lineTo(centre + (r - l) / 4, to);
79
+ }
80
+ mid.moveTo(l, y(median[i]));
81
+ mid.lineTo(r, y(median[i]));
82
+ }
83
+ return { body, frame, whisk, mid, rows };
84
+ };
85
+ const all = paths(() => true);
86
+ // The one focused row and the rest, kept for as long as the focus stays on that row.
87
+ let split;
88
+ const ink = painter(ctx, o.fill, color);
89
+ const draw = ({ body, frame, whisk, mid, rows }, k) => {
90
+ ctx.strokeStyle = color;
91
+ ctx.globalAlpha = alpha * k * (blended ? 1 : 0.25);
92
+ // A blend runs over each box's own height, so each is filled on its own.
93
+ for (let i = 0; blended && i < n; i++) {
94
+ if (!rows(i) || Number.isNaN(edges[i * 4]))
95
+ continue;
96
+ ctx.fillStyle = ink([...edges.subarray(i * 4, i * 4 + 4)]);
97
+ ctx.beginPath();
98
+ shape(ctx, i);
99
+ ctx.fill();
100
+ }
101
+ if (!blended) {
102
+ ctx.fillStyle = ink([]);
103
+ ctx.fill(body);
104
+ }
105
+ ctx.lineWidth = 1.5 * dpr;
106
+ ctx.globalAlpha = alpha * k * faint;
107
+ ctx.stroke(whisk);
108
+ ctx.globalAlpha = alpha * k;
109
+ ctx.stroke(frame);
110
+ ctx.lineWidth = heavy * dpr;
111
+ ctx.stroke(mid);
112
+ };
113
+ return {
114
+ paint(a, row, rest = 1) {
115
+ if (row === undefined || row >= n)
116
+ return draw(all, a);
117
+ if (split?.row !== row)
118
+ split = { row, one: paths(i => i === row), others: paths(i => i !== row) };
119
+ draw(split.others, a * rest);
120
+ draw(split.one, a);
121
+ },
122
+ hit: hit(view, () => edges),
123
+ };
124
+ },
125
+ gl: {
126
+ layer: glLayer,
127
+ under: true,
128
+ prepare(view, { gl }) {
129
+ const upright = blended?.toward === 'down' || blended?.toward === 'up';
130
+ const blend = blended && { stops: stops(view.ctx, blended, color), upright, based: false };
131
+ const { draw, once } = rows.on(view, gl, color, 0.75);
132
+ const filled = rows.on(view, gl, typeof o.fill === 'string' ? o.fill : color, 0.75, blend).draw;
133
+ // Columns: x, then min, q1, median, q3 and max. Half the box's width, and half a line's.
134
+ const [w, h] = [Math.max((width * view.kx) / 2, 0.5), 0.75 * view.dpr];
135
+ const at = (ya, yb, pad, more) => ({
136
+ xa: 0,
137
+ xb: 0,
138
+ ya,
139
+ yb,
140
+ pad,
141
+ snap: true,
142
+ ...more,
143
+ });
144
+ const body = at(2, 4, [w, 0], { alpha: blended ? 1 : 0.25 });
145
+ // The median, the four sides, and the two whiskers with their ends: the first to reach a pixel keeps it.
146
+ const lines = [
147
+ at(3, 3, [w, (heavy * view.dpr) / 2]),
148
+ at(4, 4, [w + h, h]),
149
+ at(2, 2, [w + h, h]),
150
+ at(2, 4, [h, h], { shift: [-w, 0] }),
151
+ at(2, 4, [h, h], { shift: [w, 0] }),
152
+ at(4, 5, [h, 0], { alpha: faint }),
153
+ at(1, 2, [h, 0], { alpha: faint }),
154
+ at(5, 5, [w / 2, h], { alpha: faint }),
155
+ at(1, 1, [w / 2, h], { alpha: faint }),
156
+ ];
157
+ let edges;
158
+ return {
159
+ // The bodies as one filled shape and then the lines as one stroke, as on the canvas.
160
+ paint(a, row, rest = 1) {
161
+ for (const [parts, pen] of [
162
+ [[body], filled],
163
+ [lines, draw],
164
+ ]) {
165
+ once(() => {
166
+ for (const [strength, ...run] of focused(n, row, rest)) {
167
+ for (const part of parts)
168
+ pen(part, alpha * a * strength, ...run);
169
+ }
170
+ });
171
+ }
172
+ },
173
+ hit: hit(view, () => (edges ??= bounds(view))),
174
+ };
175
+ },
176
+ },
177
+ release(layer) {
178
+ if (layer)
179
+ rows.release(layer.gl);
180
+ },
181
+ };
182
+ }
@@ -0,0 +1,22 @@
1
+ import type { Col, Mark } from '../types.js';
2
+ import type { Common } from './common.js';
3
+ /** A candle per x: a wick from low to high and a body from open to close. */
4
+ export type CandlestickOptions = Common & {
5
+ /** Where each candle stands along x. */
6
+ x: Col;
7
+ /** One end of the body. */
8
+ open: Col;
9
+ /** The top of the wick. */
10
+ high: Col;
11
+ /** The bottom of the wick. */
12
+ low: Col;
13
+ /** The other end of the body; below `open`, the candle takes the `down` colour. */
14
+ close: Col;
15
+ /** The body's width in x units, so 0.6 on a band axis is 60% of the category. @default 0.6 */
16
+ width?: number;
17
+ /** The colour of a candle whose close is not below its open; `color` unless given. */
18
+ up?: string;
19
+ /** The colour of a candle whose close is below its open; `color` unless given. */
20
+ down?: string;
21
+ };
22
+ export declare function candlestick(o: CandlestickOptions): Mark;