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.
- package/LICENSE +21 -0
- package/README.md +327 -2
- package/dist/chart.d.ts +3 -0
- package/dist/chart.js +1226 -0
- package/dist/charts/controller.d.ts +32 -0
- package/dist/charts/controller.js +231 -0
- package/dist/charts/defaults.d.ts +12 -0
- package/dist/charts/defaults.js +60 -0
- package/dist/charts/fields.d.ts +34 -0
- package/dist/charts/fields.js +167 -0
- package/dist/charts/index.d.ts +6 -0
- package/dist/charts/index.js +3 -0
- package/dist/charts/mount.d.ts +5 -0
- package/dist/charts/mount.js +21 -0
- package/dist/charts/source.d.ts +24 -0
- package/dist/charts/source.js +225 -0
- package/dist/charts/types.d.ts +104 -0
- package/dist/charts/types.js +1 -0
- package/dist/charts/xy.d.ts +36 -0
- package/dist/charts/xy.js +220 -0
- package/dist/features/animate.d.ts +11 -0
- package/dist/features/animate.js +118 -0
- package/dist/features/brush.d.ts +13 -0
- package/dist/features/brush.js +127 -0
- package/dist/features/legend.d.ts +15 -0
- package/dist/features/legend.js +237 -0
- package/dist/features/pointer.d.ts +29 -0
- package/dist/features/pointer.js +23 -0
- package/dist/features/tooltip.d.ts +14 -0
- package/dist/features/tooltip.js +190 -0
- package/dist/features/zoom.d.ts +13 -0
- package/dist/features/zoom.js +228 -0
- package/dist/gl.d.ts +5 -0
- package/dist/gl.js +52 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +31 -0
- package/dist/marks/arcs.d.ts +25 -0
- package/dist/marks/arcs.js +124 -0
- package/dist/marks/area.d.ts +24 -0
- package/dist/marks/area.js +106 -0
- package/dist/marks/bar.d.ts +44 -0
- package/dist/marks/bar.js +243 -0
- package/dist/marks/box.d.ts +27 -0
- package/dist/marks/box.js +188 -0
- package/dist/marks/common.d.ts +84 -0
- package/dist/marks/common.js +236 -0
- package/dist/marks/dots.d.ts +15 -0
- package/dist/marks/dots.js +156 -0
- package/dist/marks/errorBars.d.ts +19 -0
- package/dist/marks/errorBars.js +79 -0
- package/dist/marks/funnel.d.ts +25 -0
- package/dist/marks/funnel.js +121 -0
- package/dist/marks/heatmap.d.ts +19 -0
- package/dist/marks/heatmap.js +139 -0
- package/dist/marks/line.d.ts +19 -0
- package/dist/marks/line.js +75 -0
- package/dist/marks/radar.d.ts +22 -0
- package/dist/marks/radar.js +155 -0
- package/dist/marks/radialBars.d.ts +27 -0
- package/dist/marks/radialBars.js +111 -0
- package/dist/marks/rects.d.ts +43 -0
- package/dist/marks/rects.js +120 -0
- package/dist/marks/rule.d.ts +28 -0
- package/dist/marks/rule.js +80 -0
- package/dist/marks/sankey.d.ts +41 -0
- package/dist/marks/sankey.js +366 -0
- package/dist/marks/sunburst.d.ts +26 -0
- package/dist/marks/sunburst.js +185 -0
- package/dist/marks/ticks.d.ts +9 -0
- package/dist/marks/ticks.js +26 -0
- package/dist/marks/tree.d.ts +41 -0
- package/dist/marks/tree.js +110 -0
- package/dist/marks/treemap.d.ts +25 -0
- package/dist/marks/treemap.js +159 -0
- package/dist/paint.d.ts +30 -0
- package/dist/paint.js +66 -0
- package/dist/scale.d.ts +16 -0
- package/dist/scale.js +96 -0
- package/dist/scales/band.d.ts +9 -0
- package/dist/scales/band.js +23 -0
- package/dist/scales/bare.d.ts +3 -0
- package/dist/scales/bare.js +7 -0
- package/dist/scales/color.d.ts +18 -0
- package/dist/scales/color.js +21 -0
- package/dist/scales/log.d.ts +20 -0
- package/dist/scales/log.js +97 -0
- package/dist/scales/time.d.ts +30 -0
- package/dist/scales/time.js +181 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +8 -0
- package/dist/transforms.d.ts +43 -0
- package/dist/transforms.js +118 -0
- package/dist/types.d.ts +434 -0
- package/dist/types.js +1 -0
- package/package.json +39 -3
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marks are revealed from the left (or fade in) on the first paint. When the data changes they
|
|
3
|
+
* move to their new shape if every changed mark can (bars, row for row) and cross-fade otherwise.
|
|
4
|
+
* The entrance and the cross-fade blend two pictures of the chart, so a frame costs the same
|
|
5
|
+
* whatever the data size; a move repaints the chart each frame.
|
|
6
|
+
* Nothing animates for a resize or a focus change, with reduced motion, in a hidden tab, or
|
|
7
|
+
* while updates arrive less than `duration` ms apart.
|
|
8
|
+
* ponytail: marks on WebGL take part in the entrance, but swap at once when the data changes.
|
|
9
|
+
*/
|
|
10
|
+
// A move falls back to the cross-fade when one repaint of the chart takes longer than this, in ms.
|
|
11
|
+
const REPAINT_BUDGET = 6;
|
|
12
|
+
export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
13
|
+
if (!Number.isFinite(duration) || duration <= 0)
|
|
14
|
+
return () => ({ destroy() { } });
|
|
15
|
+
return chart => {
|
|
16
|
+
const { canvas } = chart;
|
|
17
|
+
const motion = matchMedia('(prefers-reduced-motion: reduce)');
|
|
18
|
+
const ctx = canvas.getContext('2d');
|
|
19
|
+
const from = new OffscreenCanvas(0, 0);
|
|
20
|
+
const to = new OffscreenCanvas(0, 0);
|
|
21
|
+
let frame = 0;
|
|
22
|
+
let last = -Infinity;
|
|
23
|
+
let entered = false;
|
|
24
|
+
let entering = false;
|
|
25
|
+
let shown;
|
|
26
|
+
const keep = (picture) => {
|
|
27
|
+
picture.width = canvas.width;
|
|
28
|
+
picture.height = canvas.height;
|
|
29
|
+
picture.getContext('2d').drawImage(canvas, 0, 0);
|
|
30
|
+
};
|
|
31
|
+
const stop = () => {
|
|
32
|
+
cancelAnimationFrame(frame);
|
|
33
|
+
frame = 0;
|
|
34
|
+
entering = false;
|
|
35
|
+
shown = undefined;
|
|
36
|
+
from.width = to.width = 0;
|
|
37
|
+
chart.morph(1);
|
|
38
|
+
};
|
|
39
|
+
const run = (step, drawn = false) => {
|
|
40
|
+
const start = performance.now();
|
|
41
|
+
const show = (e) => {
|
|
42
|
+
step(e);
|
|
43
|
+
shown = () => step(e);
|
|
44
|
+
};
|
|
45
|
+
const tick = (now) => {
|
|
46
|
+
const t = motion.matches ? 1 : Math.min(1, Math.max(0, (now - start) / duration));
|
|
47
|
+
show(1 - (1 - t) ** 3);
|
|
48
|
+
if (t < 1)
|
|
49
|
+
frame = requestAnimationFrame(tick);
|
|
50
|
+
else
|
|
51
|
+
stop();
|
|
52
|
+
};
|
|
53
|
+
if (!drawn)
|
|
54
|
+
show(0);
|
|
55
|
+
frame = requestAnimationFrame(tick);
|
|
56
|
+
};
|
|
57
|
+
// 'copy' replaces the canvas with what is drawn, so the pictures need no clearing first.
|
|
58
|
+
const blend = (mix) => (t) => {
|
|
59
|
+
ctx.save();
|
|
60
|
+
ctx.globalCompositeOperation = 'copy';
|
|
61
|
+
mix(t);
|
|
62
|
+
ctx.restore();
|
|
63
|
+
};
|
|
64
|
+
// Additive, so a pixel that is the same in both pictures keeps its value throughout.
|
|
65
|
+
const fade = blend(t => {
|
|
66
|
+
ctx.globalAlpha = 1 - t;
|
|
67
|
+
ctx.drawImage(from, 0, 0);
|
|
68
|
+
ctx.globalCompositeOperation = 'lighter';
|
|
69
|
+
ctx.globalAlpha = t;
|
|
70
|
+
ctx.drawImage(to, 0, 0);
|
|
71
|
+
});
|
|
72
|
+
// The chart says what a reveal has uncovered; here it becomes the clip for the canvas.
|
|
73
|
+
const reveal = blend(t => {
|
|
74
|
+
const scale = canvas.width / chart.size().width;
|
|
75
|
+
ctx.drawImage(from, 0, 0);
|
|
76
|
+
ctx.beginPath();
|
|
77
|
+
for (const [x, y] of chart.revealed(t))
|
|
78
|
+
ctx.lineTo(x * scale, y * scale);
|
|
79
|
+
ctx.clip();
|
|
80
|
+
ctx.drawImage(to, 0, 0);
|
|
81
|
+
});
|
|
82
|
+
chart.onPaint((draw, why) => {
|
|
83
|
+
// A focus change lands while the entrance runs: it goes into both pictures, which may have a new layout.
|
|
84
|
+
if (why === 'focus' && entering && shown) {
|
|
85
|
+
draw(0);
|
|
86
|
+
keep(from);
|
|
87
|
+
draw();
|
|
88
|
+
keep(to);
|
|
89
|
+
return shown();
|
|
90
|
+
}
|
|
91
|
+
const now = performance.now();
|
|
92
|
+
const still = why !== 'data' || frame !== 0 || now - last < duration || document.hidden || motion.matches;
|
|
93
|
+
stop();
|
|
94
|
+
if (why === 'data')
|
|
95
|
+
last = now;
|
|
96
|
+
const first = !entered;
|
|
97
|
+
entered = true;
|
|
98
|
+
if (still)
|
|
99
|
+
return draw();
|
|
100
|
+
if (first)
|
|
101
|
+
draw(0);
|
|
102
|
+
keep(from);
|
|
103
|
+
if (!first && chart.morph(0)) {
|
|
104
|
+
draw();
|
|
105
|
+
if (performance.now() - now < REPAINT_BUDGET)
|
|
106
|
+
return run(t => (chart.morph(t), draw()), true);
|
|
107
|
+
chart.morph(1);
|
|
108
|
+
}
|
|
109
|
+
draw();
|
|
110
|
+
keep(to);
|
|
111
|
+
if (!first)
|
|
112
|
+
return run(fade);
|
|
113
|
+
entering = true;
|
|
114
|
+
run(enter === 'fade' ? t => (chart.reveal(1, t), fade(t)) : t => (chart.reveal(t), reveal(t)));
|
|
115
|
+
});
|
|
116
|
+
return { destroy: stop };
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Feature, Span } from '../types.js';
|
|
2
|
+
export type BrushOptions = {
|
|
3
|
+
/** 'x' when left out: the rectangle spans the plot's height and the range its whole y window. */
|
|
4
|
+
axes?: 'x' | 'y' | 'xy';
|
|
5
|
+
/** Show the range brushed once it is reported, on the brushed axes, and drop the rectangle. */
|
|
6
|
+
zoom?: boolean;
|
|
7
|
+
/** The range brushed, in axis values, on release; null once a range reported is cleared. */
|
|
8
|
+
onSelect?(range: {
|
|
9
|
+
x: Span;
|
|
10
|
+
y: Span;
|
|
11
|
+
} | null): void;
|
|
12
|
+
};
|
|
13
|
+
export declare function brush(b?: BrushOptions): Feature;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { locate, settle, SLOP_PX, unselect } from './pointer.js';
|
|
2
|
+
export function brush(b = {}) {
|
|
3
|
+
const [bx, by] = ['x', 'y'].map(k => (b.axes ?? 'x').includes(k));
|
|
4
|
+
return chart => {
|
|
5
|
+
const { drag } = chart;
|
|
6
|
+
drag.brush = true;
|
|
7
|
+
const shade = chart.layer('div', 'display:none;opacity:0.2');
|
|
8
|
+
let reported = false;
|
|
9
|
+
let from;
|
|
10
|
+
let box;
|
|
11
|
+
let settling = 0;
|
|
12
|
+
let gone = false;
|
|
13
|
+
const clear = () => {
|
|
14
|
+
shade.style.display = 'none';
|
|
15
|
+
box = undefined;
|
|
16
|
+
if (!reported)
|
|
17
|
+
return;
|
|
18
|
+
reported = false;
|
|
19
|
+
b.onSelect?.(null);
|
|
20
|
+
};
|
|
21
|
+
const down = (e) => {
|
|
22
|
+
if (from ||
|
|
23
|
+
e.pointerType === 'touch' ||
|
|
24
|
+
e.button !== 0 ||
|
|
25
|
+
(drag.pan && (drag.pan === 'plain' || e.shiftKey)))
|
|
26
|
+
return;
|
|
27
|
+
const { px, py, over } = locate(chart, e);
|
|
28
|
+
if (!over)
|
|
29
|
+
return;
|
|
30
|
+
from = { id: e.pointerId, x: px, y: py };
|
|
31
|
+
document.addEventListener('pointermove', move, true);
|
|
32
|
+
document.addEventListener('pointerup', up, true);
|
|
33
|
+
document.addEventListener('pointercancel', cancel, true);
|
|
34
|
+
document.addEventListener('keydown', key, true);
|
|
35
|
+
document.addEventListener('selectstart', unselect, true);
|
|
36
|
+
clear();
|
|
37
|
+
};
|
|
38
|
+
const move = (e) => {
|
|
39
|
+
if (!from || from.dropped || e.pointerId !== from.id)
|
|
40
|
+
return;
|
|
41
|
+
const { width, height } = chart.size();
|
|
42
|
+
const m = chart.margin();
|
|
43
|
+
const [l, r, t, btm] = [m.left, width - m.right, m.top, height - m.bottom];
|
|
44
|
+
const { px, py } = locate(chart, e);
|
|
45
|
+
const x = Math.min(Math.max(px, l), r);
|
|
46
|
+
const y = Math.min(Math.max(py, t), btm);
|
|
47
|
+
const [dx, dy] = [bx ? x - from.x : 0, by ? y - from.y : 0];
|
|
48
|
+
if (!box && Math.hypot(dx, dy) < SLOP_PX)
|
|
49
|
+
return;
|
|
50
|
+
drag.active = true;
|
|
51
|
+
box = {
|
|
52
|
+
left: bx ? Math.min(from.x, x) : l,
|
|
53
|
+
right: bx ? Math.max(from.x, x) : r,
|
|
54
|
+
top: by ? Math.min(from.y, y) : t,
|
|
55
|
+
bottom: by ? Math.max(from.y, y) : btm,
|
|
56
|
+
};
|
|
57
|
+
Object.assign(shade.style, {
|
|
58
|
+
display: '',
|
|
59
|
+
background: chart.theme().muted,
|
|
60
|
+
left: `${box.left}px`,
|
|
61
|
+
top: `${box.top}px`,
|
|
62
|
+
width: `${box.right - box.left}px`,
|
|
63
|
+
height: `${box.bottom - box.top}px`,
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const abandon = () => {
|
|
67
|
+
if (from)
|
|
68
|
+
from.dropped = true;
|
|
69
|
+
clear();
|
|
70
|
+
};
|
|
71
|
+
const key = (e) => {
|
|
72
|
+
if (e.key === 'Escape')
|
|
73
|
+
abandon();
|
|
74
|
+
};
|
|
75
|
+
const end = () => {
|
|
76
|
+
from = undefined;
|
|
77
|
+
document.removeEventListener('pointermove', move, true);
|
|
78
|
+
document.removeEventListener('pointerup', up, true);
|
|
79
|
+
document.removeEventListener('pointercancel', cancel, true);
|
|
80
|
+
document.removeEventListener('keydown', key, true);
|
|
81
|
+
document.removeEventListener('selectstart', unselect, true);
|
|
82
|
+
if (drag.active)
|
|
83
|
+
settling = settle(chart);
|
|
84
|
+
};
|
|
85
|
+
const cancel = (e) => {
|
|
86
|
+
if (e.pointerId !== from?.id)
|
|
87
|
+
return;
|
|
88
|
+
abandon();
|
|
89
|
+
end();
|
|
90
|
+
};
|
|
91
|
+
const up = (e) => {
|
|
92
|
+
if (e.pointerId !== from?.id)
|
|
93
|
+
return;
|
|
94
|
+
move(e);
|
|
95
|
+
const kept = !from.dropped && box;
|
|
96
|
+
end();
|
|
97
|
+
if (!kept)
|
|
98
|
+
return;
|
|
99
|
+
const asc = (p, q) => (p <= q ? [p, q] : [q, p]);
|
|
100
|
+
const span = (k, [p, q]) => asc(chart.real(k, p), chart.real(k, q));
|
|
101
|
+
const view = chart.view();
|
|
102
|
+
const picked = {
|
|
103
|
+
x: bx ? asc(chart.xValue(kept.left), chart.xValue(kept.right)) : view.x,
|
|
104
|
+
y: by ? asc(chart.yValue(kept.bottom), chart.yValue(kept.top)) : view.y,
|
|
105
|
+
};
|
|
106
|
+
reported = !b.zoom;
|
|
107
|
+
b.onSelect?.({ x: span('x', picked.x), y: span('y', picked.y) });
|
|
108
|
+
if (!b.zoom || gone)
|
|
109
|
+
return;
|
|
110
|
+
clear();
|
|
111
|
+
chart.propose({ ...(bx && { x: picked.x }), ...(by && { y: picked.y }) });
|
|
112
|
+
};
|
|
113
|
+
chart.root.addEventListener('pointerdown', down);
|
|
114
|
+
return {
|
|
115
|
+
letGo: abandon,
|
|
116
|
+
destroy() {
|
|
117
|
+
gone = true;
|
|
118
|
+
from = undefined;
|
|
119
|
+
end();
|
|
120
|
+
clearTimeout(settling);
|
|
121
|
+
drag.brush = drag.active = false;
|
|
122
|
+
chart.root.removeEventListener('pointerdown', down);
|
|
123
|
+
shade.remove();
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Feature } from '../types.js';
|
|
2
|
+
export type LegendOptions = {
|
|
3
|
+
/** Whether a click, Enter or Space on an entry hides or shows its series; true when left out. */
|
|
4
|
+
toggle?: boolean;
|
|
5
|
+
/** Caps the rows of entries, by default at what fits in a third of the chart's height. */
|
|
6
|
+
rows?: number;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* One entry per group of labelled marks, in rows above the plot; pointing at one focuses its group,
|
|
10
|
+
* and with `toggle` (the default) a click or Enter or Space hides or shows it.
|
|
11
|
+
* ponytail: entry widths are estimated at 7 px a character, because measuring text (DOM or canvas)
|
|
12
|
+
* costs about 16 ms at 4x CPU slowdown before the first paint. A row that turns out wider is cut off,
|
|
13
|
+
* and a label wider than the plot ends in an ellipsis, titled once layout shows it cut.
|
|
14
|
+
*/
|
|
15
|
+
export declare function legend({ toggle, rows: cap }?: LegendOptions): Feature;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
const ROW = 20;
|
|
2
|
+
const SWATCH = 16;
|
|
3
|
+
const GAP = 14;
|
|
4
|
+
/** The two ends of a domain to as few digits as tell them apart, and at least three. */
|
|
5
|
+
function ends(lo, hi) {
|
|
6
|
+
for (let digits = 3;; digits++) {
|
|
7
|
+
const said = [lo, hi].map(v => String(+v.toPrecision(digits)));
|
|
8
|
+
if (said[0] !== said[1] || lo === hi || digits > 16)
|
|
9
|
+
return said;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One entry per group of labelled marks, in rows above the plot; pointing at one focuses its group,
|
|
14
|
+
* and with `toggle` (the default) a click or Enter or Space hides or shows it.
|
|
15
|
+
* ponytail: entry widths are estimated at 7 px a character, because measuring text (DOM or canvas)
|
|
16
|
+
* costs about 16 ms at 4x CPU slowdown before the first paint. A row that turns out wider is cut off,
|
|
17
|
+
* and a label wider than the plot ends in an ellipsis, titled once layout shows it cut.
|
|
18
|
+
*/
|
|
19
|
+
export function legend({ toggle = true, rows: cap } = {}) {
|
|
20
|
+
return chart => {
|
|
21
|
+
const { root } = chart;
|
|
22
|
+
const toggles = (e) => toggle && !e.part;
|
|
23
|
+
const off = (e) => e.id !== undefined && chart.hidden().includes(e.id);
|
|
24
|
+
const box = chart.layer('div', `color:${chart.theme().text}`);
|
|
25
|
+
const shown = new Map();
|
|
26
|
+
const cut = new ResizeObserver(seen => {
|
|
27
|
+
for (const { target: t } of seen) {
|
|
28
|
+
if (t.scrollWidth > t.clientWidth)
|
|
29
|
+
t.setAttribute('title', t.textContent);
|
|
30
|
+
else
|
|
31
|
+
t.removeAttribute('title');
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
let built = '';
|
|
35
|
+
let hovered;
|
|
36
|
+
let at;
|
|
37
|
+
let held = false;
|
|
38
|
+
let current;
|
|
39
|
+
chart.reserve((left, width) => {
|
|
40
|
+
// One pass over the marks. A mark's own entries come before its strip of colours, and a
|
|
41
|
+
// mark with either is not also listed by its label.
|
|
42
|
+
const entries = [];
|
|
43
|
+
const ramps = [];
|
|
44
|
+
const listed = new Set();
|
|
45
|
+
const hidden = chart.hidden();
|
|
46
|
+
chart.listed().forEach((k, at) => {
|
|
47
|
+
const id = k.group ?? k.label;
|
|
48
|
+
k.entries?.forEach((e, row) => entries.push({ ...e, id, part: [at, row] }));
|
|
49
|
+
if (k.ramp && (id === undefined || !hidden.includes(id)))
|
|
50
|
+
ramps.push({ label: k.label, ...k.ramp() });
|
|
51
|
+
if (k.entries || k.ramp || k.label === undefined || id === undefined || listed.has(id))
|
|
52
|
+
return;
|
|
53
|
+
listed.add(id);
|
|
54
|
+
entries.push({ label: k.label, color: k.color, id, group: k.group });
|
|
55
|
+
});
|
|
56
|
+
// By default entries and strips share one budget of rows; a strip left out goes behind "+N more",
|
|
57
|
+
// which needs a row of entries to sit in.
|
|
58
|
+
const budget = Math.max(1, Math.floor((chart.size().height / 3 - 4) / ROW) || 1);
|
|
59
|
+
const kept = cap === undefined
|
|
60
|
+
? Math.min(ramps.length, budget - (entries.length || ramps.length > budget ? 1 : 0))
|
|
61
|
+
: ramps.length;
|
|
62
|
+
const rows = [[]];
|
|
63
|
+
const most = Math.max(1, Math.floor(cap ?? budget - kept) || 1);
|
|
64
|
+
const wide = (e) => SWATCH + e.label.length * 7;
|
|
65
|
+
const more = (n) => `+${n} more`;
|
|
66
|
+
let used = 0;
|
|
67
|
+
let rest = [];
|
|
68
|
+
for (const [i, e] of entries.entries()) {
|
|
69
|
+
if (used && used + GAP + wide(e) > width) {
|
|
70
|
+
if (rows.length === most) {
|
|
71
|
+
rest = entries.slice(i);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
rows.push([]);
|
|
75
|
+
used = 0;
|
|
76
|
+
}
|
|
77
|
+
rows.at(-1).push(e);
|
|
78
|
+
used += (used && GAP) + wide(e);
|
|
79
|
+
}
|
|
80
|
+
// A strip is described by a few of its colours, which is also all the page is given of them.
|
|
81
|
+
const strips = ramps.map(r => ({
|
|
82
|
+
label: r.label,
|
|
83
|
+
ends: ends(r.lo, r.hi),
|
|
84
|
+
colors: [0, 51, 102, 153, 204, 255].map(i => {
|
|
85
|
+
const p = r.table[i];
|
|
86
|
+
return `rgb(${p & 255},${(p >> 8) & 255},${(p >> 16) & 255})`;
|
|
87
|
+
}),
|
|
88
|
+
}));
|
|
89
|
+
const unlisted = [
|
|
90
|
+
...rest.map(e => e.label),
|
|
91
|
+
...strips.splice(kept).map(s => s.label ?? s.ends.join('–')),
|
|
92
|
+
];
|
|
93
|
+
const last = rows.at(-1);
|
|
94
|
+
while (unlisted.length && last.length > 1 && used + GAP + more(unlisted.length).length * 7 > width) {
|
|
95
|
+
const out = last.pop();
|
|
96
|
+
unlisted.unshift(out.label);
|
|
97
|
+
used -= GAP + wide(out);
|
|
98
|
+
}
|
|
99
|
+
if (!entries.length && !unlisted.length)
|
|
100
|
+
rows.length = 0;
|
|
101
|
+
box.style.left = `${left}px`;
|
|
102
|
+
box.style.top = '4px';
|
|
103
|
+
box.style.width = `${width}px`;
|
|
104
|
+
const key = JSON.stringify([rows, unlisted, strips]);
|
|
105
|
+
if (key !== built) {
|
|
106
|
+
built = key;
|
|
107
|
+
const doc = root.getRootNode();
|
|
108
|
+
const focused = [...shown].find(([item]) => item.contains(doc.activeElement))?.[1].id;
|
|
109
|
+
shown.clear();
|
|
110
|
+
cut.disconnect();
|
|
111
|
+
box.replaceChildren(...rows.map((row, r) => {
|
|
112
|
+
const line = document.createElement('div');
|
|
113
|
+
line.style.cssText = `display:flex;gap:${GAP}px;height:${ROW}px;align-items:center;white-space:nowrap;overflow:hidden`;
|
|
114
|
+
const capped = r === rows.length - 1 && unlisted.length > 0;
|
|
115
|
+
for (const e of row) {
|
|
116
|
+
const item = line.appendChild(document.createElement('span'));
|
|
117
|
+
// Only the entries take the pointer: the rest of the strip stays the chart's.
|
|
118
|
+
item.style.cssText = `pointer-events:auto;${capped ? 'flex:0 1 auto;min-width:0' : 'flex:none;max-width:100%'};overflow:hidden;text-overflow:ellipsis`;
|
|
119
|
+
const swatch = item.appendChild(document.createElement('span'));
|
|
120
|
+
swatch.style.cssText =
|
|
121
|
+
'display:inline-block;width:10px;height:10px;margin-right:6px;border-radius:2px';
|
|
122
|
+
swatch.style.backgroundColor = e.color;
|
|
123
|
+
item.append(e.label);
|
|
124
|
+
cut.observe(item);
|
|
125
|
+
if (toggles(e)) {
|
|
126
|
+
item.setAttribute('role', 'button');
|
|
127
|
+
item.tabIndex = 0;
|
|
128
|
+
}
|
|
129
|
+
if (e.group !== undefined || e.part || toggles(e))
|
|
130
|
+
shown.set(item, e);
|
|
131
|
+
}
|
|
132
|
+
if (capped) {
|
|
133
|
+
const item = line.appendChild(document.createElement('span'));
|
|
134
|
+
item.style.cssText = 'pointer-events:auto;flex:none';
|
|
135
|
+
item.textContent = more(unlisted.length);
|
|
136
|
+
item.title = unlisted.join(', ');
|
|
137
|
+
}
|
|
138
|
+
return line;
|
|
139
|
+
}), ...strips.map(s => {
|
|
140
|
+
const line = document.createElement('div');
|
|
141
|
+
line.style.cssText = `display:flex;gap:6px;height:${ROW}px;align-items:center;white-space:nowrap;overflow:hidden`;
|
|
142
|
+
const strip = document.createElement('span');
|
|
143
|
+
strip.style.cssText = 'display:inline-block;width:120px;height:10px;border-radius:2px';
|
|
144
|
+
strip.style.backgroundImage = `linear-gradient(to right,${s.colors.join()})`;
|
|
145
|
+
line.append(...(s.label ? [s.label, ' '] : []), s.ends[0], strip, s.ends[1]);
|
|
146
|
+
return line;
|
|
147
|
+
}));
|
|
148
|
+
if (focused !== undefined)
|
|
149
|
+
[...shown].find(([, e]) => toggles(e) && e.id === focused)?.[0].focus();
|
|
150
|
+
}
|
|
151
|
+
for (const [item, e] of shown) {
|
|
152
|
+
item.style.opacity = off(e) ? '0.4' : '';
|
|
153
|
+
if (toggles(e))
|
|
154
|
+
item.setAttribute('aria-pressed', String(!off(e)));
|
|
155
|
+
}
|
|
156
|
+
const lines = rows.length + strips.length;
|
|
157
|
+
return lines ? lines * ROW + 4 : 0;
|
|
158
|
+
});
|
|
159
|
+
// The mark is looked up when pointed at: after an update that kept the entries as they were,
|
|
160
|
+
// the list is not rebuilt, but the mark at that place is a new one.
|
|
161
|
+
const point = (e) => {
|
|
162
|
+
const id = e && JSON.stringify([e.group, e.part]);
|
|
163
|
+
if (id === hovered)
|
|
164
|
+
return;
|
|
165
|
+
hovered = id;
|
|
166
|
+
current = e;
|
|
167
|
+
const mark = e?.part && chart.listed()[e.part[0]];
|
|
168
|
+
chart.focus(e?.group ?? null, mark && { mark, row: e.part[1] });
|
|
169
|
+
};
|
|
170
|
+
// One listener on the chart, not enter and leave on each entry: an entry rebuilt or removed
|
|
171
|
+
// under the pointer never gets its leave. Capturing, so a tooltip sets its own focus after.
|
|
172
|
+
const entryAt = (target) => {
|
|
173
|
+
const item = [...shown.keys()].find(i => i.contains(target));
|
|
174
|
+
return [item, item && shown.get(item)];
|
|
175
|
+
};
|
|
176
|
+
const over = (target) => {
|
|
177
|
+
const [item, e] = entryAt(target);
|
|
178
|
+
point(e && !off(e) ? e : undefined);
|
|
179
|
+
return item;
|
|
180
|
+
};
|
|
181
|
+
const move = (e) => {
|
|
182
|
+
held = false;
|
|
183
|
+
at = over(e.target) && [e.clientX, e.clientY];
|
|
184
|
+
};
|
|
185
|
+
const leave = () => {
|
|
186
|
+
held = false;
|
|
187
|
+
at = undefined;
|
|
188
|
+
point(undefined);
|
|
189
|
+
};
|
|
190
|
+
const lift = (e) => {
|
|
191
|
+
if (e.pointerType !== 'touch' || (current && toggles(current)))
|
|
192
|
+
leave();
|
|
193
|
+
else
|
|
194
|
+
held = true;
|
|
195
|
+
};
|
|
196
|
+
const flip = (target) => {
|
|
197
|
+
const [, e] = entryAt(target);
|
|
198
|
+
if (!e || !toggles(e))
|
|
199
|
+
return false;
|
|
200
|
+
// Let go first: hide() repaints at once, and a pointer still recorded would focus the entry again.
|
|
201
|
+
at = undefined;
|
|
202
|
+
point(undefined);
|
|
203
|
+
const ids = chart.hidden();
|
|
204
|
+
chart.hide(ids.includes(e.id) ? ids.filter(i => i !== e.id) : [...ids, e.id]);
|
|
205
|
+
return true;
|
|
206
|
+
};
|
|
207
|
+
box.addEventListener('click', e => flip(e.target));
|
|
208
|
+
box.addEventListener('keydown', e => {
|
|
209
|
+
if ((e.key === 'Enter' || e.key === ' ') && flip(e.target))
|
|
210
|
+
e.preventDefault();
|
|
211
|
+
});
|
|
212
|
+
root.addEventListener('pointerdown', move, true);
|
|
213
|
+
root.addEventListener('pointermove', move, true);
|
|
214
|
+
root.addEventListener('pointerleave', lift);
|
|
215
|
+
root.addEventListener('pointercancel', leave);
|
|
216
|
+
document.addEventListener('pointerdown', leave, true);
|
|
217
|
+
return {
|
|
218
|
+
// The entries may have moved under a pointer at rest, so what it is over is looked up again.
|
|
219
|
+
update() {
|
|
220
|
+
box.style.color = chart.theme().text;
|
|
221
|
+
const doc = root.isConnected && root.getRootNode();
|
|
222
|
+
if (at && !over(doc ? doc.elementFromPoint(...at) : null))
|
|
223
|
+
at = undefined;
|
|
224
|
+
},
|
|
225
|
+
letGo: () => held && leave(),
|
|
226
|
+
destroy() {
|
|
227
|
+
root.removeEventListener('pointerdown', move, true);
|
|
228
|
+
root.removeEventListener('pointermove', move, true);
|
|
229
|
+
root.removeEventListener('pointerleave', lift);
|
|
230
|
+
root.removeEventListener('pointercancel', leave);
|
|
231
|
+
document.removeEventListener('pointerdown', leave, true);
|
|
232
|
+
cut.disconnect();
|
|
233
|
+
box.remove();
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Internals } from '../types.js';
|
|
2
|
+
export declare const SLOP_PX = 4;
|
|
3
|
+
export declare const unselect: (e: Event) => void;
|
|
4
|
+
/** Clears `drag.active` after the click the browser sends for this release, which a drag must not count as one. */
|
|
5
|
+
export declare const settle: (chart: Internals) => number;
|
|
6
|
+
/** Where a point of the page is on the chart in CSS pixels, and whether it is inside the plot rectangle. */
|
|
7
|
+
export declare function position(chart: Internals, clientX: number, clientY: number): {
|
|
8
|
+
px: number;
|
|
9
|
+
py: number;
|
|
10
|
+
inside: boolean;
|
|
11
|
+
w: number;
|
|
12
|
+
h: number;
|
|
13
|
+
at: {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
/** Where a pointer event is in CSS pixels, and whether it is over the plot of a chart with axes. */
|
|
19
|
+
export declare function locate(chart: Internals, e: MouseEvent): {
|
|
20
|
+
px: number;
|
|
21
|
+
py: number;
|
|
22
|
+
w: number;
|
|
23
|
+
h: number;
|
|
24
|
+
at: {
|
|
25
|
+
x: number;
|
|
26
|
+
y: number;
|
|
27
|
+
};
|
|
28
|
+
over: boolean;
|
|
29
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const SLOP_PX = 4;
|
|
2
|
+
export const unselect = (e) => e.preventDefault();
|
|
3
|
+
/** Clears `drag.active` after the click the browser sends for this release, which a drag must not count as one. */
|
|
4
|
+
export const settle = (chart) => setTimeout(() => {
|
|
5
|
+
chart.drag.active = false;
|
|
6
|
+
chart.sync();
|
|
7
|
+
}, 0);
|
|
8
|
+
/** Where a point of the page is on the chart in CSS pixels, and whether it is inside the plot rectangle. */
|
|
9
|
+
export function position(chart, clientX, clientY) {
|
|
10
|
+
const { width, height } = chart.size();
|
|
11
|
+
const m = chart.margin();
|
|
12
|
+
const box = chart.root.getBoundingClientRect();
|
|
13
|
+
const px = ((clientX - box.left) * width) / box.width;
|
|
14
|
+
const py = ((clientY - box.top) * height) / box.height;
|
|
15
|
+
const [w, h] = [width - m.left - m.right, height - m.top - m.bottom];
|
|
16
|
+
const inside = px >= m.left && px <= m.left + w && py >= m.top && py <= m.top + h;
|
|
17
|
+
return { px, py, inside, w, h, at: { x: (px - m.left) / w, y: 1 - (py - m.top) / h } };
|
|
18
|
+
}
|
|
19
|
+
/** Where a pointer event is in CSS pixels, and whether it is over the plot of a chart with axes. */
|
|
20
|
+
export function locate(chart, e) {
|
|
21
|
+
const { inside, ...where } = position(chart, e.clientX, e.clientY);
|
|
22
|
+
return { ...where, over: inside && chart.frame() === 'axes' && e.target === chart.root };
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Feature } from '../types.js';
|
|
2
|
+
export type TooltipOptions =
|
|
3
|
+
/** One row per labelled mark that can be sampled at the pointer's x, with a crosshair; a band gives its thickness. */
|
|
4
|
+
{
|
|
5
|
+
mode: 'x';
|
|
6
|
+
title: (x: number) => string;
|
|
7
|
+
value: (y: number) => string;
|
|
8
|
+
}
|
|
9
|
+
/** x and y are where the mark says it was hit, y being the row's own value where it has one; row is its row there. */
|
|
10
|
+
| {
|
|
11
|
+
mode: 'nearest';
|
|
12
|
+
text: (label: string, x: number, y: number, row?: number) => string;
|
|
13
|
+
};
|
|
14
|
+
export declare function tooltip(t: TooltipOptions): Feature;
|