swiftplot 0.0.1 → 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 +324 -3
  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 +31 -6
@@ -0,0 +1,124 @@
1
+ import { rgba } from '../gl.js';
2
+ import { inRing, TURN, turnAt } from './common.js';
3
+ export function arcs(o) {
4
+ const { values, colors, inner = 0, alpha = 1 } = o;
5
+ const n = values.length;
6
+ // Where each slice ends, as a share of the turn; the last entry is 1 unless every value is 0.
7
+ const ends = new Float64Array(n);
8
+ const worth = (i) => (values[i] > 0 && Number.isFinite(values[i]) ? values[i] : 0);
9
+ // Shares are summed as fractions of the largest value: the values themselves can overflow.
10
+ let most = 0;
11
+ for (let i = 0; i < n; i++)
12
+ most = Math.max(most, worth(i));
13
+ let total = 0;
14
+ for (let i = 0; i < n; i++)
15
+ ends[i] = total += worth(i) / (most || 1);
16
+ for (let i = 0; i < n; i++)
17
+ ends[i] /= total || 1;
18
+ let wide = 0;
19
+ for (let i = 0; i < n; i++)
20
+ wide += +(ends[i] > (ends[i - 1] ?? 0));
21
+ const shade = (i) => colors[i % colors.length] ?? '#888';
22
+ return {
23
+ type: 'arcs',
24
+ key: o.key,
25
+ label: o.label,
26
+ group: o.group ?? o.label,
27
+ color: shade(0),
28
+ options: o,
29
+ points: n,
30
+ channels: { x: [], y: [] },
31
+ entries: o.labels?.slice(0, n).map((label, i) => ({ label, color: shade(i) })),
32
+ prepare({ ctx, dpr, left, top, width, height }) {
33
+ const [cx, cy] = [left + width / 2, top + height / 2];
34
+ const outer = Math.max(0, Math.min(width, height) / 2);
35
+ const hole = outer * Math.max(0, Math.min(inner, 1));
36
+ // A gap of a device pixel between neighbours, which shows whatever is behind the chart.
37
+ // Slices are gathered until they are a device pixel wide at the rim, and drawn as one in
38
+ // the colour of the widest among them: more shapes than pixels cost time and show nothing.
39
+ const slices = [];
40
+ const width1 = (i) => ends[i] - (ends[i - 1] ?? 0);
41
+ let [first, lead, start] = [0, 0, 0];
42
+ for (let i = 0; i < n && outer; i++) {
43
+ if (width1(i) > width1(lead) || lead < first)
44
+ lead = i;
45
+ const end = ends[i];
46
+ if (end - start < 1 / (outer * TURN) && i < n - 1)
47
+ continue;
48
+ if (end > start) {
49
+ const gap = wide > 1 ? Math.min(0.5 / outer, ((end - start) * TURN) / 4) : 0;
50
+ const [a, b] = [start * TURN - TURN / 4 + gap, end * TURN - TURN / 4 - gap];
51
+ const path = new Path2D();
52
+ path.arc(cx, cy, outer, a, b);
53
+ path.arc(cx, cy, hole, b, a, true);
54
+ path.closePath();
55
+ slices.push({ path, from: first, to: i + 1, lead, span: [start, end] });
56
+ }
57
+ [first, start] = [i + 1, end];
58
+ }
59
+ // One paint for every slice: round the clock, each from faint at its start to full at its end.
60
+ // ponytail: past a few dozen slices a sweep in each cannot be seen and its stops slow every fill, so they stay solid.
61
+ const round = o.sweep !== undefined && slices.length <= 64 && ctx.createConicGradient?.(-TURN / 4, cx, cy);
62
+ for (const { lead, span } of round ? slices : []) {
63
+ const [red, green, blue, a = 1] = rgba(ctx, shade(lead)).map((v, at) => at < 3 ? Math.round(v * 255) : v);
64
+ round.addColorStop(span[0], `rgba(${red},${green},${blue},${a * o.sweep})`);
65
+ round.addColorStop(span[1], `rgba(${red},${green},${blue},${a})`);
66
+ }
67
+ // Where a slice's label and its tooltip sit: half-way round it and half-way out.
68
+ const anchor = (i) => {
69
+ const middle = (((ends[i - 1] ?? 0) + ends[i]) / 2) * TURN - TURN / 4;
70
+ const far = (hole + outer) / 2;
71
+ return [cx + Math.cos(middle) * far, cy + Math.sin(middle) * far];
72
+ };
73
+ const said = o.text === true ? (_, share) => `${Math.round(share * 100)}%` : o.text;
74
+ const within = (i, x, y, w, h) => inRing([cx, cy, hole, outer, ends[i - 1] ?? 0, ends[i]], x, y, w, h);
75
+ const labels = !said
76
+ ? undefined
77
+ : Array.from(ends, (end, i) => {
78
+ const share = end - (ends[i - 1] ?? 0);
79
+ // A slice narrower at the rim than one character has no room, so it is not asked.
80
+ const text = share * TURN * outer >= 7 * dpr ? said(i, share) : '';
81
+ const [x, y] = anchor(i);
82
+ if (!text || !within(i, x, y, text.length * 7 * dpr, 16 * dpr))
83
+ return undefined;
84
+ return { x, y, text, color: '#fff', priority: share };
85
+ }).filter(l => l !== undefined);
86
+ return {
87
+ labels,
88
+ paint(k, row, rest = 1) {
89
+ for (const { path, from, to, lead } of slices) {
90
+ ctx.globalAlpha = alpha * k * (row === undefined || (row >= from && row < to) ? 1 : rest);
91
+ ctx.fillStyle = round || shade(lead);
92
+ ctx.fill(path);
93
+ }
94
+ },
95
+ // A slice holds its starting edge and not its ending one. Reported at the slice's middle.
96
+ hit(px, py, reach) {
97
+ const [dx, dy] = [px * dpr - cx, py * dpr - cy];
98
+ const far = Math.hypot(dx, dy);
99
+ if (far < hole || far > outer || !outer)
100
+ return null;
101
+ const share = turnAt(dx, dy);
102
+ // The first slice to end past the pointer, which is one with a width.
103
+ let [row, past] = [0, n];
104
+ while (row < past) {
105
+ const mid = (row + past) >> 1;
106
+ if (ends[mid] > share)
107
+ past = mid;
108
+ else
109
+ row = mid + 1;
110
+ }
111
+ if (row >= n)
112
+ return null;
113
+ const [x, y] = anchor(row);
114
+ return {
115
+ x: (x - left) / width,
116
+ y: 1 - (y - top) / height,
117
+ distance: reach,
118
+ row,
119
+ };
120
+ },
121
+ };
122
+ },
123
+ };
124
+ }
@@ -0,0 +1,24 @@
1
+ import type { Col, Mark } from '../types.js';
2
+ import { type Paint } from '../paint.js';
3
+ import type { Common, Curve } from './common.js';
4
+ /**
5
+ * Filled between 0 and y, or between y1 and y2. x must be ascending. `fill` fills it with
6
+ * something other than its colour. An upright blend runs from the furthest the area reaches to
7
+ * its baseline, on both sides of it and whichever side that is, so gradient(0.4, 0) fades the area
8
+ * out where it meets the axis. Areas given the same `stack` id are stacked by the chart over the shown
9
+ * ones, in the order they are listed; they must share their x. `join` fills it straight across a
10
+ * value that is not finite, where it would otherwise leave a gap.
11
+ */
12
+ export type AreaOptions = Common & {
13
+ x: Col;
14
+ curve?: Curve;
15
+ fill?: Paint;
16
+ join?: boolean;
17
+ } & ({
18
+ y: Col;
19
+ stack?: string;
20
+ } | {
21
+ y1: Col;
22
+ y2: Col;
23
+ });
24
+ export declare function area(o: AreaOptions): Mark;
@@ -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 } 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: `area:stack:${o.stack}`,
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,44 @@
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. `gap` takes so many CSS pixels off
7
+ * the side of each bar, which sets touching bars such as a histogram's apart; a bar keeps a pixel
8
+ * of its width. `radius` rounds the value end of a bar that starts at zero by so many CSS pixels,
9
+ * and all the corners of one that does not; bars too narrow to
10
+ * show it, and bars drawn by WebGL, stay square. `fill` fills the bars with something other than
11
+ * their colour: a blend runs over each bar, and down a bar that stands on a baseline it runs from
12
+ * the value end to the baseline. On the canvas blended bars are filled one at a time.
13
+ * Bars given `x`, `y` and a `stack` or `dodge` id are laid out by the chart with the shown bars of
14
+ * that id, in the order they are listed: stacked over one another, or side by side in the slots of
15
+ * their category, `x` being the category's place on a band axis.
16
+ */
17
+ export type BarOptions = Common & {
18
+ fill?: Paint;
19
+ radius?: number;
20
+ gap?: number;
21
+ } & ((({
22
+ x1: Col;
23
+ x2: Col;
24
+ } | {
25
+ x: Col;
26
+ }) & ({
27
+ y1: Col;
28
+ y2: Col;
29
+ } | {
30
+ y: Col;
31
+ }) & {
32
+ stack?: never;
33
+ dodge?: never;
34
+ }) | ({
35
+ x: Col;
36
+ y: Col;
37
+ } & ({
38
+ stack: string;
39
+ dodge?: never;
40
+ } | {
41
+ dodge: string;
42
+ stack?: never;
43
+ })));
44
+ 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 } 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: __, ...rest } = m;
10
+ const [x1, x2] = bands(x, stacked ? {} : { slot: i, of: members.length });
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 ? `bar:stack:${a.stack}` : `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, grounded ? up : flat ? along : 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,27 @@
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
+ * `width` is the box's width in x units, so 0.6 on a band axis is 60% of the category.
7
+ * ponytail: upright only, and the whiskers go to whatever min and max are given; outliers are
8
+ * for the caller to leave out and draw with dots().
9
+ */
10
+ export type BoxOptions = Common & {
11
+ x: Col;
12
+ min: Col;
13
+ q1: Col;
14
+ median: Col;
15
+ q3: Col;
16
+ max: Col;
17
+ width?: number;
18
+ /** What the boxes are filled with: a colour, drawn faint, or a blend down each box, drawn as given. */
19
+ fill?: Paint;
20
+ /** Rounds the boxes' corners by so many CSS pixels; boxes drawn by WebGL stay square. */
21
+ radius?: number;
22
+ /** The width of the median line in CSS pixels; 1.5 like the other lines unless given. */
23
+ medianWidth?: number;
24
+ /** The opacity of the whiskers against the rest; 1 unless given. */
25
+ whiskers?: number;
26
+ };
27
+ export declare function box(o: BoxOptions): Mark;