swiftplot 0.5.1 → 0.5.2
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/README.md +4 -4
- package/dist/chart.js +17 -13
- package/dist/charts/defaults.js +4 -2
- package/dist/color.d.ts +2 -0
- package/dist/color.js +5 -3
- package/dist/features/animate.js +7 -7
- package/dist/features/brush.js +12 -12
- package/dist/features/legend.js +14 -14
- package/dist/features/pointer.js +3 -2
- package/dist/features/tooltip.js +4 -4
- package/dist/features/zoom.js +19 -19
- package/dist/text.js +1 -0
- package/dist/types.d.ts +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -98,11 +98,11 @@ Gzip size of the minified bundle of each entry in `site/scripts/size/`, built fr
|
|
|
98
98
|
| entry | imports | KB |
|
|
99
99
|
| ------------------ | ------------------------------------------------------- | ---: |
|
|
100
100
|
| plot-line | `plot` and `line` from `swiftplot` | 14.4 |
|
|
101
|
-
| plot-line-features | the same with `tooltip`, `legend`, `zoom` and `animate` | 19.
|
|
102
|
-
| line-chart | `lineChart` from `swiftplot/charts` | 37.
|
|
101
|
+
| plot-line-features | the same with `tooltip`, `legend`, `zoom` and `animate` | 19.1 |
|
|
102
|
+
| line-chart | `lineChart` from `swiftplot/charts` | 37.6 |
|
|
103
103
|
| all-charts | every chart of `swiftplot/charts` | 54.7 |
|
|
104
|
-
| react-line-chart | `LineChart` from `@swiftplot/react`, React not counted |
|
|
105
|
-
| vue-line-chart | `LineChart` from `@swiftplot/vue`, Vue not counted |
|
|
104
|
+
| react-line-chart | `LineChart` from `@swiftplot/react`, React not counted | 39.3 |
|
|
105
|
+
| vue-line-chart | `LineChart` from `@swiftplot/vue`, Vue not counted | 39.8 |
|
|
106
106
|
|
|
107
107
|
On chart-bench's page, which bundles `plot`, `line`, `area`, `ticks`, `dots`, `tooltip` and `animate`, swiftplot 0.5.0 adds 19.3 KB and uPlot 23.9 KB (`results/bundle.json`: the arm's page less the same page with no chart). uPlot ships more in its size: it is one module that comes whole, with its legend, cursor, selection and series types whether a page uses them or not.
|
|
108
108
|
|
package/dist/chart.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { extend, resolver } from './color.js';
|
|
1
|
+
import { extend, resolver, windowOf } from './color.js';
|
|
2
2
|
import { copied, keep, sameData } from './equal.js';
|
|
3
3
|
import { owns, position, presses, released, strayed } from './features/pointer.js';
|
|
4
4
|
import { upTo } from './marks/common.js';
|
|
@@ -65,12 +65,14 @@ const clamp = (want, [p, q]) => {
|
|
|
65
65
|
*/
|
|
66
66
|
export function plot(el, options) {
|
|
67
67
|
let o = options;
|
|
68
|
-
|
|
69
|
-
const
|
|
68
|
+
const doc = el.ownerDocument;
|
|
69
|
+
const win = windowOf(el);
|
|
70
|
+
let dpr = win.devicePixelRatio;
|
|
71
|
+
const root = doc.createElement('div');
|
|
70
72
|
root.style.cssText = `position:relative;font:${FONT};pointer-events:auto`;
|
|
71
73
|
let overlay;
|
|
72
74
|
const layer = (tag, css = '') => {
|
|
73
|
-
const e =
|
|
75
|
+
const e = doc.createElement(tag);
|
|
74
76
|
e.style.cssText = `position:absolute;pointer-events:none;${css}`;
|
|
75
77
|
return root.insertBefore(e, overlay?.parentNode === root ? overlay : null);
|
|
76
78
|
};
|
|
@@ -305,7 +307,7 @@ export function plot(el, options) {
|
|
|
305
307
|
return;
|
|
306
308
|
noted = key;
|
|
307
309
|
boxes = kept.map(k => {
|
|
308
|
-
const el =
|
|
310
|
+
const el = doc.createElement('div');
|
|
309
311
|
el.textContent = k.text;
|
|
310
312
|
el.style.cssText = `position:absolute;white-space:nowrap;line-height:16px;left:${k.l}px;top:${k.t}px;width:${k.r - k.l}px;text-align:center`;
|
|
311
313
|
if (k.color)
|
|
@@ -333,7 +335,7 @@ export function plot(el, options) {
|
|
|
333
335
|
clipped = '';
|
|
334
336
|
}
|
|
335
337
|
lines.replaceChildren(...wanted.map(r => {
|
|
336
|
-
const e =
|
|
338
|
+
const e = doc.createElement('div');
|
|
337
339
|
const w = Number(r.width) / dpr;
|
|
338
340
|
const along = (at, lo, size) => Math.min(Math.max(Math.round(at - r.width / 2) / dpr, lo / dpr), (lo + size) / dpr - w);
|
|
339
341
|
e.style.cssText =
|
|
@@ -657,9 +659,9 @@ export function plot(el, options) {
|
|
|
657
659
|
if (key !== axesKey) {
|
|
658
660
|
axesKey = key;
|
|
659
661
|
// Axis text is DOM, not fillText: the first text drawn on a canvas cost ~20 ms at 4x CPU slowdown.
|
|
660
|
-
const labels =
|
|
662
|
+
const labels = doc.createDocumentFragment();
|
|
661
663
|
const text = (s, x, y, transform) => {
|
|
662
|
-
const e =
|
|
664
|
+
const e = doc.createElement('div');
|
|
663
665
|
e.textContent = s;
|
|
664
666
|
e.style.cssText = `position:absolute;white-space:nowrap;left:${x}px;top:${y}px;transform:${transform}`;
|
|
665
667
|
labels.appendChild(e);
|
|
@@ -1048,6 +1050,8 @@ export function plot(el, options) {
|
|
|
1048
1050
|
return next;
|
|
1049
1051
|
};
|
|
1050
1052
|
const internals = {
|
|
1053
|
+
doc,
|
|
1054
|
+
win,
|
|
1051
1055
|
root,
|
|
1052
1056
|
canvas,
|
|
1053
1057
|
get overlay() {
|
|
@@ -1289,9 +1293,9 @@ export function plot(el, options) {
|
|
|
1289
1293
|
tracking = on;
|
|
1290
1294
|
for (const type of RELEASES)
|
|
1291
1295
|
if (on)
|
|
1292
|
-
|
|
1296
|
+
doc.addEventListener(type, release, true);
|
|
1293
1297
|
else
|
|
1294
|
-
|
|
1298
|
+
doc.removeEventListener(type, release, true);
|
|
1295
1299
|
};
|
|
1296
1300
|
const adopt = (e) => {
|
|
1297
1301
|
const changed = () => {
|
|
@@ -1407,7 +1411,7 @@ export function plot(el, options) {
|
|
|
1407
1411
|
// A density query matches one value only, so each change is listened for once and asked for again.
|
|
1408
1412
|
let density;
|
|
1409
1413
|
const redensify = () => {
|
|
1410
|
-
dpr = devicePixelRatio;
|
|
1414
|
+
dpr = win.devicePixelRatio;
|
|
1411
1415
|
fit(canvas);
|
|
1412
1416
|
if (glCanvas)
|
|
1413
1417
|
fit(glCanvas);
|
|
@@ -1418,7 +1422,7 @@ export function plot(el, options) {
|
|
|
1418
1422
|
report();
|
|
1419
1423
|
};
|
|
1420
1424
|
const watch = () => {
|
|
1421
|
-
density = matchMedia(`(resolution: ${dpr}dppx)`);
|
|
1425
|
+
density = win.matchMedia(`(resolution: ${dpr}dppx)`);
|
|
1422
1426
|
density.addEventListener('change', redensify, { once: true });
|
|
1423
1427
|
};
|
|
1424
1428
|
const teardown = () => {
|
|
@@ -1456,7 +1460,7 @@ export function plot(el, options) {
|
|
|
1456
1460
|
root.addEventListener('pointermove', point, true);
|
|
1457
1461
|
if (refresh(true))
|
|
1458
1462
|
sync();
|
|
1459
|
-
observer = new ResizeObserver(() => {
|
|
1463
|
+
observer = new win.ResizeObserver(() => {
|
|
1460
1464
|
letGo();
|
|
1461
1465
|
if (refresh(false, measure() ? 'size' : undefined))
|
|
1462
1466
|
sync();
|
package/dist/charts/defaults.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { windowOf } from '../color.js';
|
|
1
2
|
import { palette, paletteDark } from '../paint.js';
|
|
2
3
|
import { dark, light } from '../theme.js';
|
|
3
4
|
export const DAY = 864e5;
|
|
@@ -94,6 +95,7 @@ export function spacing(x, fallback) {
|
|
|
94
95
|
/** 'auto' reads the element's computed color-scheme; onChange fires when a 'light dark' page flips. */
|
|
95
96
|
export function themeOf(el, prop, onChange) {
|
|
96
97
|
const none = () => { };
|
|
98
|
+
const win = windowOf(el);
|
|
97
99
|
if (prop === 'light')
|
|
98
100
|
return { theme: light, dispose: none };
|
|
99
101
|
if (prop === 'dark')
|
|
@@ -101,7 +103,7 @@ export function themeOf(el, prop, onChange) {
|
|
|
101
103
|
if (prop !== undefined && prop !== 'auto')
|
|
102
104
|
return { theme: prop, dispose: none };
|
|
103
105
|
const fixed = () => {
|
|
104
|
-
const words = getComputedStyle(el).colorScheme.split(/\s+/);
|
|
106
|
+
const words = win.getComputedStyle(el).colorScheme.split(/\s+/);
|
|
105
107
|
if (!words.includes('dark'))
|
|
106
108
|
return light;
|
|
107
109
|
return words.includes('light') ? undefined : dark;
|
|
@@ -109,7 +111,7 @@ export function themeOf(el, prop, onChange) {
|
|
|
109
111
|
const theme = fixed();
|
|
110
112
|
if (theme)
|
|
111
113
|
return { theme, dispose: none };
|
|
112
|
-
const mq = matchMedia('(prefers-color-scheme: dark)');
|
|
114
|
+
const mq = win.matchMedia('(prefers-color-scheme: dark)');
|
|
113
115
|
const resolve = () => fixed() ?? (mq.matches ? dark : light);
|
|
114
116
|
const listen = () => onChange(resolve());
|
|
115
117
|
mq.addEventListener('change', listen);
|
package/dist/color.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** The window an element is in, or this one for an element of a document that has none. */
|
|
2
|
+
export declare const windowOf: (el: Node) => Window & typeof globalThis;
|
|
1
3
|
/**
|
|
2
4
|
* CSS variables in a colour, resolved where the chart stands: anything but a string naming one comes
|
|
3
5
|
* back as it is. A colour with no value becomes `muted`, with one warning. What was read is kept
|
package/dist/color.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { light } from './theme.js';
|
|
2
|
+
/** The window an element is in, or this one for an element of a document that has none. */
|
|
3
|
+
export const windowOf = (el) => (el.ownerDocument?.defaultView ?? globalThis);
|
|
2
4
|
/**
|
|
3
5
|
* CSS variables in a colour, resolved where the chart stands: anything but a string naming one comes
|
|
4
6
|
* back as it is. A colour with no value becomes `muted`, with one warning. What was read is kept
|
|
@@ -8,7 +10,7 @@ export function resolver(root, muted) {
|
|
|
8
10
|
const known = new Map();
|
|
9
11
|
const warned = new Set();
|
|
10
12
|
const read = (css) => {
|
|
11
|
-
const probe =
|
|
13
|
+
const probe = root.ownerDocument.createElement('div');
|
|
12
14
|
// The root's own values, against a page's rules for its children and properties that do not inherit.
|
|
13
15
|
// Every word is taken, as CSS escapes can spell even a name's dashes.
|
|
14
16
|
const names = css.match(/(?:\\[\da-f]{1,6}(?:\r\n|\s)?|\\.|[\w-]|[^\0-\x7f])+/gi) ?? [];
|
|
@@ -18,8 +20,8 @@ export function resolver(root, muted) {
|
|
|
18
20
|
// value each keeps its own, so they differ.
|
|
19
21
|
probe.style.setProperty('flood-color', css, 'important');
|
|
20
22
|
probe.style.setProperty('lighting-color', css, 'important');
|
|
21
|
-
(root.isConnected ? root :
|
|
22
|
-
const { floodColor, lightingColor } = getComputedStyle(probe);
|
|
23
|
+
(root.isConnected ? root : root.ownerDocument.body).append(probe);
|
|
24
|
+
const { floodColor, lightingColor } = windowOf(root).getComputedStyle(probe);
|
|
23
25
|
probe.remove();
|
|
24
26
|
return floodColor === lightingColor ? floodColor : undefined;
|
|
25
27
|
};
|
package/dist/features/animate.js
CHANGED
|
@@ -13,8 +13,8 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
13
13
|
if (!Number.isFinite(duration) || duration <= 0)
|
|
14
14
|
return () => ({ destroy() { } });
|
|
15
15
|
return chart => {
|
|
16
|
-
const { canvas } = chart;
|
|
17
|
-
const motion = matchMedia('(prefers-reduced-motion: reduce)');
|
|
16
|
+
const { canvas, win } = chart;
|
|
17
|
+
const motion = win.matchMedia('(prefers-reduced-motion: reduce)');
|
|
18
18
|
const ctx = canvas.getContext('2d');
|
|
19
19
|
const from = new OffscreenCanvas(0, 0);
|
|
20
20
|
const to = new OffscreenCanvas(0, 0);
|
|
@@ -29,7 +29,7 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
29
29
|
picture.getContext('2d').drawImage(canvas, 0, 0);
|
|
30
30
|
};
|
|
31
31
|
const stop = () => {
|
|
32
|
-
cancelAnimationFrame(frame);
|
|
32
|
+
win.cancelAnimationFrame(frame);
|
|
33
33
|
frame = 0;
|
|
34
34
|
entering = false;
|
|
35
35
|
shown = undefined;
|
|
@@ -37,7 +37,7 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
37
37
|
chart.morph(1);
|
|
38
38
|
};
|
|
39
39
|
const run = (step, drawn = false) => {
|
|
40
|
-
const start = performance.now();
|
|
40
|
+
const start = win.performance.now();
|
|
41
41
|
const show = (e) => {
|
|
42
42
|
step(e);
|
|
43
43
|
shown = () => step(e);
|
|
@@ -46,13 +46,13 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
46
46
|
const t = motion.matches ? 1 : Math.min(1, Math.max(0, (now - start) / duration));
|
|
47
47
|
show(1 - (1 - t) ** 3);
|
|
48
48
|
if (t < 1)
|
|
49
|
-
frame = requestAnimationFrame(tick);
|
|
49
|
+
frame = win.requestAnimationFrame(tick);
|
|
50
50
|
else
|
|
51
51
|
stop();
|
|
52
52
|
};
|
|
53
53
|
if (!drawn)
|
|
54
54
|
show(0);
|
|
55
|
-
frame = requestAnimationFrame(tick);
|
|
55
|
+
frame = win.requestAnimationFrame(tick);
|
|
56
56
|
};
|
|
57
57
|
// 'copy' replaces the canvas with what is drawn, so the pictures need no clearing first.
|
|
58
58
|
const blend = (mix) => (t) => {
|
|
@@ -89,7 +89,7 @@ export function animate({ duration = 300, enter = 'reveal' } = {}) {
|
|
|
89
89
|
return shown();
|
|
90
90
|
}
|
|
91
91
|
const now = performance.now();
|
|
92
|
-
const still = why !== 'data' || frame !== 0 || now - last < duration ||
|
|
92
|
+
const still = why !== 'data' || frame !== 0 || now - last < duration || chart.doc.hidden || motion.matches;
|
|
93
93
|
stop();
|
|
94
94
|
if (why === 'data')
|
|
95
95
|
last = now;
|
package/dist/features/brush.js
CHANGED
|
@@ -2,7 +2,7 @@ import { locate, settle, SLOP_PX, unselect } from './pointer.js';
|
|
|
2
2
|
export function brush(b = {}) {
|
|
3
3
|
const [bx, by] = ['x', 'y'].map(k => (b.axes ?? 'x').includes(k));
|
|
4
4
|
return chart => {
|
|
5
|
-
const { drag } = chart;
|
|
5
|
+
const { drag, doc } = chart;
|
|
6
6
|
drag.brush = true;
|
|
7
7
|
const shade = chart.layer('div', 'display:none;opacity:0.2');
|
|
8
8
|
let reported = false;
|
|
@@ -28,11 +28,11 @@ export function brush(b = {}) {
|
|
|
28
28
|
if (!over)
|
|
29
29
|
return;
|
|
30
30
|
from = { id: e.pointerId, x: px, y: py };
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
doc.addEventListener('pointermove', move, true);
|
|
32
|
+
doc.addEventListener('pointerup', up, true);
|
|
33
|
+
doc.addEventListener('pointercancel', cancel, true);
|
|
34
|
+
doc.addEventListener('keydown', key, true);
|
|
35
|
+
doc.addEventListener('selectstart', unselect, true);
|
|
36
36
|
clear();
|
|
37
37
|
};
|
|
38
38
|
const move = (e) => {
|
|
@@ -74,11 +74,11 @@ export function brush(b = {}) {
|
|
|
74
74
|
};
|
|
75
75
|
const end = () => {
|
|
76
76
|
from = undefined;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
doc.removeEventListener('pointermove', move, true);
|
|
78
|
+
doc.removeEventListener('pointerup', up, true);
|
|
79
|
+
doc.removeEventListener('pointercancel', cancel, true);
|
|
80
|
+
doc.removeEventListener('keydown', key, true);
|
|
81
|
+
doc.removeEventListener('selectstart', unselect, true);
|
|
82
82
|
if (drag.active)
|
|
83
83
|
settling = settle(chart);
|
|
84
84
|
};
|
|
@@ -118,7 +118,7 @@ export function brush(b = {}) {
|
|
|
118
118
|
gone = true;
|
|
119
119
|
from = undefined;
|
|
120
120
|
end();
|
|
121
|
-
clearTimeout(settling);
|
|
121
|
+
chart.win.clearTimeout(settling);
|
|
122
122
|
drag.brush = drag.active = false;
|
|
123
123
|
chart.root.removeEventListener('pointerdown', down);
|
|
124
124
|
shade.remove();
|
package/dist/features/legend.js
CHANGED
|
@@ -17,12 +17,12 @@ function ends(lo, hi) {
|
|
|
17
17
|
*/
|
|
18
18
|
export function legend({ toggle = true, rows: cap } = {}) {
|
|
19
19
|
return chart => {
|
|
20
|
-
const { root } = chart;
|
|
20
|
+
const { root, doc } = chart;
|
|
21
21
|
const toggles = (e) => toggle && !e.part;
|
|
22
22
|
const off = (e) => e.id !== undefined && chart.hidden().includes(e.id);
|
|
23
23
|
const box = chart.layer('div', `color:${chart.theme().text}`);
|
|
24
24
|
const shown = new Map();
|
|
25
|
-
const cut = new ResizeObserver(seen => {
|
|
25
|
+
const cut = new chart.win.ResizeObserver(seen => {
|
|
26
26
|
for (const { target: t } of seen) {
|
|
27
27
|
if (t.scrollWidth > t.clientWidth)
|
|
28
28
|
t.setAttribute('title', t.textContent);
|
|
@@ -103,19 +103,19 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
103
103
|
const key = JSON.stringify([rows, unlisted, strips]);
|
|
104
104
|
if (key !== built) {
|
|
105
105
|
built = key;
|
|
106
|
-
const
|
|
107
|
-
const focused = [...shown].find(([item]) => item.contains(
|
|
106
|
+
const tree = root.getRootNode();
|
|
107
|
+
const focused = [...shown].find(([item]) => item.contains(tree.activeElement))?.[1].id;
|
|
108
108
|
shown.clear();
|
|
109
109
|
cut.disconnect();
|
|
110
110
|
box.replaceChildren(...rows.map((row, r) => {
|
|
111
|
-
const line =
|
|
111
|
+
const line = doc.createElement('div');
|
|
112
112
|
line.style.cssText = `display:flex;gap:${GAP}px;height:${ROW}px;align-items:center;white-space:nowrap;overflow:hidden`;
|
|
113
113
|
const capped = r === rows.length - 1 && unlisted.length > 0;
|
|
114
114
|
for (const e of row) {
|
|
115
|
-
const item = line.appendChild(
|
|
115
|
+
const item = line.appendChild(doc.createElement('span'));
|
|
116
116
|
// Only the entries take the pointer: the rest of the strip stays the chart's.
|
|
117
117
|
item.style.cssText = `pointer-events:auto;${capped ? 'flex:0 1 auto;min-width:0' : 'flex:none;max-width:100%'};overflow:hidden;text-overflow:ellipsis`;
|
|
118
|
-
const swatch = item.appendChild(
|
|
118
|
+
const swatch = item.appendChild(doc.createElement('span'));
|
|
119
119
|
swatch.style.cssText =
|
|
120
120
|
'display:inline-block;width:10px;height:10px;margin-right:6px;border-radius:2px';
|
|
121
121
|
swatch.style.backgroundColor = e.color;
|
|
@@ -129,16 +129,16 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
129
129
|
shown.set(item, e);
|
|
130
130
|
}
|
|
131
131
|
if (capped) {
|
|
132
|
-
const item = line.appendChild(
|
|
132
|
+
const item = line.appendChild(doc.createElement('span'));
|
|
133
133
|
item.style.cssText = 'pointer-events:auto;flex:none';
|
|
134
134
|
item.textContent = more(unlisted.length);
|
|
135
135
|
item.title = unlisted.join(', ');
|
|
136
136
|
}
|
|
137
137
|
return line;
|
|
138
138
|
}), ...strips.map(s => {
|
|
139
|
-
const line =
|
|
139
|
+
const line = doc.createElement('div');
|
|
140
140
|
line.style.cssText = `display:flex;gap:6px;height:${ROW}px;align-items:center;white-space:nowrap;overflow:hidden`;
|
|
141
|
-
const strip =
|
|
141
|
+
const strip = doc.createElement('span');
|
|
142
142
|
strip.style.cssText = 'display:inline-block;width:120px;height:10px;border-radius:2px';
|
|
143
143
|
strip.style.backgroundImage = `linear-gradient(to right,${s.colors.join()})`;
|
|
144
144
|
line.append(...(s.label ? [s.label, ' '] : []), s.ends[0], strip, s.ends[1]);
|
|
@@ -213,13 +213,13 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
213
213
|
host.addEventListener('pointermove', move, true);
|
|
214
214
|
root.addEventListener('pointerleave', lift);
|
|
215
215
|
root.addEventListener('pointercancel', leave);
|
|
216
|
-
|
|
216
|
+
doc.addEventListener('pointerdown', leave, true);
|
|
217
217
|
return {
|
|
218
218
|
// The entries may have moved under a pointer at rest, so what it is over is looked up again.
|
|
219
219
|
update() {
|
|
220
220
|
box.style.color = chart.theme().text;
|
|
221
|
-
const
|
|
222
|
-
if (at && !over(
|
|
221
|
+
const tree = root.isConnected && root.getRootNode();
|
|
222
|
+
if (at && !over(tree ? tree.elementFromPoint(...at) : null))
|
|
223
223
|
at = undefined;
|
|
224
224
|
},
|
|
225
225
|
letGo: () => held && leave(),
|
|
@@ -228,7 +228,7 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
228
228
|
host.removeEventListener('pointermove', move, true);
|
|
229
229
|
root.removeEventListener('pointerleave', lift);
|
|
230
230
|
root.removeEventListener('pointercancel', leave);
|
|
231
|
-
|
|
231
|
+
doc.removeEventListener('pointerdown', leave, true);
|
|
232
232
|
cut.disconnect();
|
|
233
233
|
box.remove();
|
|
234
234
|
},
|
package/dist/features/pointer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export const SLOP_PX = 4;
|
|
2
2
|
export const unselect = (e) => e.preventDefault();
|
|
3
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(() => {
|
|
4
|
+
export const settle = (chart) => chart.win.setTimeout(() => {
|
|
5
5
|
chart.drag.active = false;
|
|
6
6
|
chart.sync();
|
|
7
7
|
}, 0);
|
|
@@ -30,7 +30,8 @@ export const released = ({ overlay }, e) => e.pointerType !== 'touch' && !!overl
|
|
|
30
30
|
export const strayed = ({ overlay }, e) => {
|
|
31
31
|
const held = overlay && presses.get(overlay)?.held;
|
|
32
32
|
// Some browsers send a click as a MouseEvent, which does not name its pointer, and so are double clicks.
|
|
33
|
-
|
|
33
|
+
const id = e.pointerId;
|
|
34
|
+
return !!held && (id === undefined ? held.size > 0 : held.has(id));
|
|
34
35
|
};
|
|
35
36
|
/** Whether a hover is the chart's own: on it or on the app's DOM over it, and not on a control such as a legend's. */
|
|
36
37
|
export const owns = (chart, target) => target === chart.root || !!chart.overlay?.contains(target);
|
package/dist/features/tooltip.js
CHANGED
|
@@ -3,7 +3,7 @@ import { owns, position, released } from './pointer.js';
|
|
|
3
3
|
const FOCUS_PX = 30;
|
|
4
4
|
export function tooltip(t) {
|
|
5
5
|
return chart => {
|
|
6
|
-
const { root } = chart;
|
|
6
|
+
const { root, doc } = chart;
|
|
7
7
|
const cross = chart.layer('div', 'display:none;border-left:1px dashed');
|
|
8
8
|
const tip = chart.layer('div', 'display:none;left:0;top:0;border:1px solid;padding:4px 8px;box-sizing:border-box;width:max-content;max-width:100%;max-height:100%;overflow:hidden;white-space:normal;overflow-wrap:anywhere;z-index:100');
|
|
9
9
|
const restyle = () => {
|
|
@@ -24,7 +24,7 @@ export function tooltip(t) {
|
|
|
24
24
|
const show = (x, y, rows) => {
|
|
25
25
|
const { width, height } = chart.size();
|
|
26
26
|
tip.replaceChildren(...rows.map(r => {
|
|
27
|
-
const e =
|
|
27
|
+
const e = doc.createElement('div');
|
|
28
28
|
e.textContent = r.text;
|
|
29
29
|
if (r.color)
|
|
30
30
|
e.style.color = chart.color(r.color);
|
|
@@ -179,7 +179,7 @@ export function tooltip(t) {
|
|
|
179
179
|
root.addEventListener('pointerup', release, true);
|
|
180
180
|
root.addEventListener('pointerleave', lift);
|
|
181
181
|
root.addEventListener('pointercancel', out);
|
|
182
|
-
|
|
182
|
+
doc.addEventListener('pointerdown', away, true);
|
|
183
183
|
return {
|
|
184
184
|
update() {
|
|
185
185
|
restyle();
|
|
@@ -193,7 +193,7 @@ export function tooltip(t) {
|
|
|
193
193
|
root.removeEventListener('pointerup', release, true);
|
|
194
194
|
root.removeEventListener('pointerleave', lift);
|
|
195
195
|
root.removeEventListener('pointercancel', out);
|
|
196
|
-
|
|
196
|
+
doc.removeEventListener('pointerdown', away, true);
|
|
197
197
|
cross.remove();
|
|
198
198
|
tip.remove();
|
|
199
199
|
},
|
package/dist/features/zoom.js
CHANGED
|
@@ -7,7 +7,7 @@ const TAP_MS = 500;
|
|
|
7
7
|
export function zoom(z = {}) {
|
|
8
8
|
const sides = ['x', 'y'].filter(k => (z.axes ?? 'x').includes(k));
|
|
9
9
|
return chart => {
|
|
10
|
-
const { root, drag } = chart;
|
|
10
|
+
const { root, drag, doc, win } = chart;
|
|
11
11
|
const locate = (e) => where(chart, e);
|
|
12
12
|
if (z.drag !== false)
|
|
13
13
|
drag.pan = z.drag === 'pan' ? 'plain' : 'shift';
|
|
@@ -23,7 +23,7 @@ export function zoom(z = {}) {
|
|
|
23
23
|
};
|
|
24
24
|
const ask = (want) => {
|
|
25
25
|
pending = want;
|
|
26
|
-
frame ||= requestAnimationFrame(flush);
|
|
26
|
+
frame ||= win.requestAnimationFrame(flush);
|
|
27
27
|
};
|
|
28
28
|
const wheel = (e) => {
|
|
29
29
|
if (z.wheel === false)
|
|
@@ -69,10 +69,10 @@ export function zoom(z = {}) {
|
|
|
69
69
|
if (!over)
|
|
70
70
|
return;
|
|
71
71
|
from = { id: e.pointerId, x: px, y: py, view: shown(), w, h };
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
doc.addEventListener('pointermove', move, true);
|
|
73
|
+
doc.addEventListener('pointerup', up, true);
|
|
74
|
+
doc.addEventListener('pointercancel', up, true);
|
|
75
|
+
doc.addEventListener('selectstart', unselect, true);
|
|
76
76
|
};
|
|
77
77
|
const move = (e) => {
|
|
78
78
|
if (!from || e.pointerId !== from.id)
|
|
@@ -107,10 +107,10 @@ export function zoom(z = {}) {
|
|
|
107
107
|
};
|
|
108
108
|
const end = () => {
|
|
109
109
|
from = undefined;
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
110
|
+
doc.removeEventListener('pointermove', move, true);
|
|
111
|
+
doc.removeEventListener('pointerup', up, true);
|
|
112
|
+
doc.removeEventListener('pointercancel', up, true);
|
|
113
|
+
doc.removeEventListener('selectstart', unselect, true);
|
|
114
114
|
};
|
|
115
115
|
const up = (e) => {
|
|
116
116
|
if (e.pointerId !== from?.id)
|
|
@@ -121,7 +121,7 @@ export function zoom(z = {}) {
|
|
|
121
121
|
};
|
|
122
122
|
const settle = () => (settling = release(chart));
|
|
123
123
|
const engage = () => {
|
|
124
|
-
clearTimeout(settling);
|
|
124
|
+
win.clearTimeout(settling);
|
|
125
125
|
drag.active = true;
|
|
126
126
|
};
|
|
127
127
|
const whole = () => ask(sides.length === 2 ? null : { [sides[0]]: undefined });
|
|
@@ -145,9 +145,9 @@ export function zoom(z = {}) {
|
|
|
145
145
|
if (!over || touches.size >= 2)
|
|
146
146
|
return;
|
|
147
147
|
if (!touches.size) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
doc.addEventListener('pointermove', touchMove, true);
|
|
149
|
+
doc.addEventListener('pointerup', touchUp, true);
|
|
150
|
+
doc.addEventListener('pointercancel', touchCancel, true);
|
|
151
151
|
}
|
|
152
152
|
touches.set(e.pointerId, { px, py, at });
|
|
153
153
|
if (touches.size === 2) {
|
|
@@ -194,9 +194,9 @@ export function zoom(z = {}) {
|
|
|
194
194
|
touches.clear();
|
|
195
195
|
pinch = tap = undefined;
|
|
196
196
|
again = false;
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
197
|
+
doc.removeEventListener('pointermove', touchMove, true);
|
|
198
|
+
doc.removeEventListener('pointerup', touchUp, true);
|
|
199
|
+
doc.removeEventListener('pointercancel', touchCancel, true);
|
|
200
200
|
if (drag.active)
|
|
201
201
|
settle();
|
|
202
202
|
};
|
|
@@ -234,8 +234,8 @@ export function zoom(z = {}) {
|
|
|
234
234
|
touchEnd();
|
|
235
235
|
root.style.touchAction = touchAction;
|
|
236
236
|
root.removeEventListener('pointerdown', touchDown, true);
|
|
237
|
-
cancelAnimationFrame(frame);
|
|
238
|
-
clearTimeout(settling);
|
|
237
|
+
win.cancelAnimationFrame(frame);
|
|
238
|
+
win.clearTimeout(settling);
|
|
239
239
|
drag.pan = drag.active = false;
|
|
240
240
|
root.removeEventListener('wheel', wheel);
|
|
241
241
|
root.removeEventListener('pointerdown', down);
|
package/dist/text.js
CHANGED
|
@@ -14,6 +14,7 @@ export function textWidth(s) {
|
|
|
14
14
|
if (w !== undefined)
|
|
15
15
|
return w;
|
|
16
16
|
if (pen === undefined) {
|
|
17
|
+
// oxlint-disable-next-line no-restricted-globals -- one canvas for every window: FONT is the same in all of them
|
|
17
18
|
pen = typeof document === 'undefined' ? null : document.createElement('canvas').getContext('2d');
|
|
18
19
|
if (pen)
|
|
19
20
|
pen.font = FONT;
|
package/dist/types.d.ts
CHANGED
|
@@ -388,6 +388,13 @@ export type Painter = (draw: (reveal?: number) => void, why: 'data' | 'size' | '
|
|
|
388
388
|
* work from that paint on.
|
|
389
389
|
*/
|
|
390
390
|
export type Internals = {
|
|
391
|
+
/** The document of the element the chart was made in, which creates its nodes and takes its listeners. */
|
|
392
|
+
doc: Document;
|
|
393
|
+
/**
|
|
394
|
+
* That document's window, or this one when it has none: frames, timers, observers, media queries and
|
|
395
|
+
* styles are asked of it.
|
|
396
|
+
*/
|
|
397
|
+
win: Window & typeof globalThis;
|
|
391
398
|
root: HTMLElement;
|
|
392
399
|
/** The 2D canvas holding the grid and every mark that is not on WebGL. */
|
|
393
400
|
canvas: HTMLCanvasElement;
|