swiftplot 0.4.0 → 0.5.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.
- package/README.md +23 -5
- package/dist/chart.js +295 -29
- package/dist/charts/controller.d.ts +3 -4
- package/dist/charts/controller.js +74 -24
- package/dist/charts/index.d.ts +1 -1
- package/dist/charts/index.js +1 -1
- package/dist/charts/mount.js +3 -0
- package/dist/charts/types.d.ts +4 -0
- package/dist/color.d.ts +12 -0
- package/dist/color.js +61 -0
- package/dist/equal.d.ts +20 -0
- package/dist/equal.js +106 -0
- package/dist/features/legend.js +8 -7
- package/dist/features/pointer.d.ts +18 -1
- package/dist/features/pointer.js +19 -1
- package/dist/features/tooltip.js +14 -7
- package/dist/features/zoom.js +2 -2
- package/dist/gl.d.ts +2 -0
- package/dist/gl.js +8 -1
- package/dist/marks/sankey.js +3 -3
- package/dist/types.d.ts +63 -4
- package/package.json +1 -1
|
@@ -1,28 +1,10 @@
|
|
|
1
1
|
import { plot } from '../chart.js';
|
|
2
|
+
import { equal } from '../equal.js';
|
|
2
3
|
import { themeOf } from './defaults.js';
|
|
3
4
|
import { source } from './source.js';
|
|
4
5
|
const STATE = ['view', 'hidden', 'selected', 'pointer'];
|
|
5
6
|
const CALLBACKS = ['onHiddenChange', 'onClick', 'onPointerChange'];
|
|
6
|
-
export const PASSED = new Set([...STATE, ...CALLBACKS, 'onViewChange']);
|
|
7
|
-
export const plain = (v) => typeof v === 'object' &&
|
|
8
|
-
v !== null &&
|
|
9
|
-
(Array.isArray(v) || Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);
|
|
10
|
-
/** Plain objects and arrays by value, anything else (typed arrays, functions, themes) by identity. */
|
|
11
|
-
export const equal = (a, b) => same(a, b, new Map());
|
|
12
|
-
function same(a, b, seen) {
|
|
13
|
-
if (Object.is(a, b))
|
|
14
|
-
return true;
|
|
15
|
-
if (!plain(a) || !plain(b) || Array.isArray(a) !== Array.isArray(b))
|
|
16
|
-
return false;
|
|
17
|
-
if (Array.isArray(a) && a.length !== b.length)
|
|
18
|
-
return false;
|
|
19
|
-
const pairs = seen.get(a) ?? seen.set(a, new Set()).get(a);
|
|
20
|
-
if (pairs.has(b))
|
|
21
|
-
return true;
|
|
22
|
-
pairs.add(b);
|
|
23
|
-
const keys = Object.keys(a);
|
|
24
|
-
return (keys.length === Object.keys(b).length && keys.every(k => Object.hasOwn(b, k) && same(a[k], b[k], seen)));
|
|
25
|
-
}
|
|
7
|
+
export const PASSED = new Set([...STATE, ...CALLBACKS, 'onViewChange', 'onLayout', 'onHover']);
|
|
26
8
|
/** As the core keeps a mark: by key, type and options. */
|
|
27
9
|
const sameMarks = (a, b) => a.length === b.length &&
|
|
28
10
|
a.every((m, i) => m.key === b[i].key && m.type === b[i].type && equal(m.options, b[i].options));
|
|
@@ -56,7 +38,14 @@ export function create(el, initial, build) {
|
|
|
56
38
|
return value;
|
|
57
39
|
};
|
|
58
40
|
let internals;
|
|
59
|
-
|
|
41
|
+
let overlay;
|
|
42
|
+
const keep = c => {
|
|
43
|
+
overlay ??= internals?.overlay;
|
|
44
|
+
if (overlay)
|
|
45
|
+
c.overlay = overlay;
|
|
46
|
+
internals = c;
|
|
47
|
+
return { destroy() { } };
|
|
48
|
+
};
|
|
60
49
|
const windowed = {};
|
|
61
50
|
const remember = (v) => {
|
|
62
51
|
const whole = internals.domain();
|
|
@@ -73,8 +62,48 @@ export function create(el, initial, build) {
|
|
|
73
62
|
remember(v);
|
|
74
63
|
props.onViewChange?.(v);
|
|
75
64
|
};
|
|
65
|
+
// The app may update the chart from onLayout or onHover, which must not run inside an update still being applied.
|
|
66
|
+
let applying = false;
|
|
67
|
+
let pending = {};
|
|
68
|
+
let hovered = false;
|
|
69
|
+
const tell = (hover) => {
|
|
70
|
+
hovered = !!hover;
|
|
71
|
+
props.onHover?.(hover);
|
|
72
|
+
};
|
|
73
|
+
const onLayout = (layout) => {
|
|
74
|
+
if (applying)
|
|
75
|
+
pending.layout = layout;
|
|
76
|
+
else
|
|
77
|
+
props.onLayout?.(layout);
|
|
78
|
+
};
|
|
79
|
+
const onHover = (hover) => {
|
|
80
|
+
if (applying)
|
|
81
|
+
pending.hover = hover;
|
|
82
|
+
else
|
|
83
|
+
tell(hover);
|
|
84
|
+
};
|
|
85
|
+
// The hover is read after onLayout, as an update made from it reports a newer one itself.
|
|
86
|
+
const flush = () => {
|
|
87
|
+
const { layout } = pending;
|
|
88
|
+
delete pending.layout;
|
|
89
|
+
try {
|
|
90
|
+
if (layout)
|
|
91
|
+
props.onLayout?.(layout);
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
const { hover } = pending;
|
|
95
|
+
delete pending.hover;
|
|
96
|
+
if (hover !== undefined && !destroyed)
|
|
97
|
+
tell(hover);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
76
100
|
const wanted = (p, b) => {
|
|
77
|
-
const w = {
|
|
101
|
+
const w = {
|
|
102
|
+
...b.options,
|
|
103
|
+
onViewChange,
|
|
104
|
+
onLayout: p.onLayout && onLayout,
|
|
105
|
+
onHover: p.onHover && onHover,
|
|
106
|
+
};
|
|
78
107
|
for (const k of CALLBACKS)
|
|
79
108
|
w[k] = p[k];
|
|
80
109
|
for (const k of STATE)
|
|
@@ -106,8 +135,18 @@ export function create(el, initial, build) {
|
|
|
106
135
|
let destroyed = false;
|
|
107
136
|
let live = true;
|
|
108
137
|
let held = [];
|
|
109
|
-
/** With r, the options are built again from it; without, only passed props changed. */
|
|
110
138
|
function apply(next, r) {
|
|
139
|
+
applying = true;
|
|
140
|
+
try {
|
|
141
|
+
change(next, r);
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
applying = false;
|
|
145
|
+
flush();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** With r, the options are built again from it; without, only passed props changed. */
|
|
149
|
+
function change(next, r) {
|
|
111
150
|
let b = built;
|
|
112
151
|
let t = themed;
|
|
113
152
|
let th = theme;
|
|
@@ -135,6 +174,7 @@ export function create(el, initial, build) {
|
|
|
135
174
|
const fail = (e) => {
|
|
136
175
|
if (t !== themed)
|
|
137
176
|
t.dispose();
|
|
177
|
+
pending = hovered ? { hover: null } : {};
|
|
138
178
|
held = hidden;
|
|
139
179
|
try {
|
|
140
180
|
chart = plot(el, { ...sent, ...carried(props), use: [...built.use(), keep] });
|
|
@@ -147,6 +187,8 @@ export function create(el, initial, build) {
|
|
|
147
187
|
if (!live)
|
|
148
188
|
return;
|
|
149
189
|
live = false;
|
|
190
|
+
if (hovered)
|
|
191
|
+
pending.hover = null;
|
|
150
192
|
chart.destroy();
|
|
151
193
|
};
|
|
152
194
|
const commit = (s) => {
|
|
@@ -187,7 +229,12 @@ export function create(el, initial, build) {
|
|
|
187
229
|
if (!(k in want) && STATE.includes(k))
|
|
188
230
|
continue;
|
|
189
231
|
const [was, now] = [kept[k], want[k]];
|
|
190
|
-
|
|
232
|
+
// A theme replaced by an equal one is how an app asks for changed CSS variables to be read again.
|
|
233
|
+
const same = k === 'theme'
|
|
234
|
+
? was === now
|
|
235
|
+
: k === 'marks'
|
|
236
|
+
? sameMarks(was, now)
|
|
237
|
+
: equal(was, now);
|
|
191
238
|
if (!same)
|
|
192
239
|
partial[k] = now;
|
|
193
240
|
}
|
|
@@ -211,6 +258,9 @@ export function create(el, initial, build) {
|
|
|
211
258
|
get chart() {
|
|
212
259
|
return chart;
|
|
213
260
|
},
|
|
261
|
+
get overlay() {
|
|
262
|
+
return (overlay ??= chart.overlay);
|
|
263
|
+
},
|
|
214
264
|
update(partial) {
|
|
215
265
|
if (destroyed)
|
|
216
266
|
return;
|
package/dist/charts/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export { heatmapChart } from './heatmap.js';
|
|
|
6
6
|
export { funnelChart, pieChart, radialBarChart } from './parts.js';
|
|
7
7
|
export { radarChart } from './radar.js';
|
|
8
8
|
export { areaChart, barChart, lineChart, scatterChart } from './xy.js';
|
|
9
|
-
export { equal, plain } from '
|
|
9
|
+
export { equal, plain } from '../equal.js';
|
|
10
10
|
export type { EasyChart } from './controller.js';
|
|
11
11
|
export { mount } from './mount.js';
|
|
12
12
|
export type { Mount } from './mount.js';
|
package/dist/charts/index.js
CHANGED
|
@@ -6,5 +6,5 @@ export { heatmapChart } from './heatmap.js';
|
|
|
6
6
|
export { funnelChart, pieChart, radialBarChart } from './parts.js';
|
|
7
7
|
export { radarChart } from './radar.js';
|
|
8
8
|
export { areaChart, barChart, lineChart, scatterChart } from './xy.js';
|
|
9
|
-
export { equal, plain } from '
|
|
9
|
+
export { equal, plain } from '../equal.js';
|
|
10
10
|
export { mount } from './mount.js';
|
package/dist/charts/mount.js
CHANGED
package/dist/charts/types.d.ts
CHANGED
|
@@ -147,6 +147,10 @@ export type Base = {
|
|
|
147
147
|
pointer?: Options['pointer'];
|
|
148
148
|
/** Called as the user's pointer moves over the plot, and with null as it leaves; never for update(). */
|
|
149
149
|
onPointerChange?: Options['onPointerChange'];
|
|
150
|
+
/** Called with the chart's geometry after the first paint and whenever it changes, for drawing your own nodes over the chart. */
|
|
151
|
+
onLayout?: Options['onLayout'];
|
|
152
|
+
/** Called as the user's pointer moves over the plot, in CSS pixels with what is within 20 px of it, and with null as it leaves. */
|
|
153
|
+
onHover?: Options['onHover'];
|
|
150
154
|
};
|
|
151
155
|
type Name<T> = keyof T & string;
|
|
152
156
|
type OneOrMore<T> = T | readonly T[];
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS variables in a colour, resolved where the chart stands: anything but a string naming one comes
|
|
3
|
+
* back as it is. A colour with no value becomes `muted`, with one warning. What was read is kept
|
|
4
|
+
* until forget() or reread().
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolver(root: HTMLElement, muted: () => string): {
|
|
7
|
+
color: <T>(css: T) => T | string;
|
|
8
|
+
forget: () => void;
|
|
9
|
+
reread: () => boolean;
|
|
10
|
+
};
|
|
11
|
+
/** Lets a context's fillStyle and strokeStyle take what `color` resolves. */
|
|
12
|
+
export declare function extend(ctx: CanvasRenderingContext2D, color: (css: unknown) => unknown): void;
|
package/dist/color.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { light } from './theme.js';
|
|
2
|
+
/**
|
|
3
|
+
* CSS variables in a colour, resolved where the chart stands: anything but a string naming one comes
|
|
4
|
+
* back as it is. A colour with no value becomes `muted`, with one warning. What was read is kept
|
|
5
|
+
* until forget() or reread().
|
|
6
|
+
*/
|
|
7
|
+
export function resolver(root, muted) {
|
|
8
|
+
const known = new Map();
|
|
9
|
+
const warned = new Set();
|
|
10
|
+
const read = (css) => {
|
|
11
|
+
const probe = document.createElement('div');
|
|
12
|
+
// The root's own values, against a page's rules for its children and properties that do not inherit.
|
|
13
|
+
// Every word is taken, as CSS escapes can spell even a name's dashes.
|
|
14
|
+
const names = css.match(/(?:\\[\da-f]{1,6}(?:\r\n|\s)?|\\.|[\w-]|[^\0-\x7f])+/gi) ?? [];
|
|
15
|
+
probe.style.cssText =
|
|
16
|
+
'display:none;' + ['color', 'color-scheme', ...names].map(k => `${k}:inherit!important`).join(';');
|
|
17
|
+
// Two properties that take any colour, are not inherited and start out black and white: without a
|
|
18
|
+
// value each keeps its own, so they differ.
|
|
19
|
+
probe.style.setProperty('flood-color', css, 'important');
|
|
20
|
+
probe.style.setProperty('lighting-color', css, 'important');
|
|
21
|
+
(root.isConnected ? root : document.body).append(probe);
|
|
22
|
+
const { floodColor, lightingColor } = getComputedStyle(probe);
|
|
23
|
+
probe.remove();
|
|
24
|
+
return floodColor === lightingColor ? floodColor : undefined;
|
|
25
|
+
};
|
|
26
|
+
const color = (css) => {
|
|
27
|
+
if (typeof css !== 'string' || !css.includes('var('))
|
|
28
|
+
return css;
|
|
29
|
+
let found = known.get(css);
|
|
30
|
+
if (found !== undefined)
|
|
31
|
+
return found;
|
|
32
|
+
found = read(css);
|
|
33
|
+
if (found === undefined) {
|
|
34
|
+
if (!warned.has(css))
|
|
35
|
+
console.warn(`swiftplot: the colour "${css}" has no value; drawn in muted`);
|
|
36
|
+
warned.add(css);
|
|
37
|
+
found = css === muted() ? light.muted : color(muted());
|
|
38
|
+
}
|
|
39
|
+
known.set(css, found);
|
|
40
|
+
return found;
|
|
41
|
+
};
|
|
42
|
+
/** Reads the colours known again; whether any has changed. */
|
|
43
|
+
const reread = () => {
|
|
44
|
+
const was = [...known];
|
|
45
|
+
known.clear();
|
|
46
|
+
return was.some(([css, v]) => color(css) !== v);
|
|
47
|
+
};
|
|
48
|
+
return { color, forget: () => known.clear(), reread };
|
|
49
|
+
}
|
|
50
|
+
/** Lets a context's fillStyle and strokeStyle take what `color` resolves. */
|
|
51
|
+
export function extend(ctx, color) {
|
|
52
|
+
for (const k of ['fillStyle', 'strokeStyle']) {
|
|
53
|
+
const native = Object.getOwnPropertyDescriptor(CanvasRenderingContext2D.prototype, k);
|
|
54
|
+
Object.defineProperty(ctx, k, {
|
|
55
|
+
...native,
|
|
56
|
+
set(v) {
|
|
57
|
+
native.set.call(ctx, color(v));
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/equal.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const plain: (v: unknown) => v is Record<string, unknown>;
|
|
2
|
+
/** Plain objects and arrays by value, anything else (typed arrays, functions) by identity. */
|
|
3
|
+
export declare const equal: (a: unknown, b: unknown) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Data by value, typed arrays too, and instances of a class by their own fields and toJSON(), where they have
|
|
6
|
+
* either; any two functions are alike, as inline ones are new each time.
|
|
7
|
+
*/
|
|
8
|
+
export declare const sameData: (a: unknown, b: unknown) => boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Data kept from later writes in place, to compare with another copy.
|
|
11
|
+
* ponytail: of a long list only the length, `row` and LONG values evenly spread, so a write in place to another row of
|
|
12
|
+
* one is missed; a digest of the whole list would see it, for a pass over every row each time the pointer comes.
|
|
13
|
+
*/
|
|
14
|
+
export declare const copied: (v: unknown, row?: number, seen?: Map<object, unknown>, lists?: [ArrayLike<unknown>, unknown[]][]) => unknown;
|
|
15
|
+
/** A copy of data for `row`, and `to()`, which moves it to another row without reading the rest again. */
|
|
16
|
+
export declare function keep(of: unknown, row?: number): {
|
|
17
|
+
of: unknown;
|
|
18
|
+
data: unknown;
|
|
19
|
+
to(next?: number): void;
|
|
20
|
+
};
|
package/dist/equal.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export const plain = (v) => typeof v === 'object' &&
|
|
2
|
+
v !== null &&
|
|
3
|
+
(Array.isArray(v) || Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);
|
|
4
|
+
/** Plain objects and arrays by value, anything else (typed arrays, functions) by identity. */
|
|
5
|
+
export const equal = (a, b) => same(a, b, false, new Map());
|
|
6
|
+
function same(a, b, data, seen) {
|
|
7
|
+
if (Object.is(a, b))
|
|
8
|
+
return true;
|
|
9
|
+
if (plain(a) && plain(b))
|
|
10
|
+
return Array.isArray(a) === Array.isArray(b) && alike(a, b, data, seen);
|
|
11
|
+
if (!data)
|
|
12
|
+
return false;
|
|
13
|
+
if (typeof a === 'function' && typeof b === 'function')
|
|
14
|
+
return true;
|
|
15
|
+
if (typed(a) && typed(b))
|
|
16
|
+
return every(a, b, Object.is);
|
|
17
|
+
return akin(a, b) && alike(a, b, data, seen);
|
|
18
|
+
}
|
|
19
|
+
function alike(a, b, data, seen) {
|
|
20
|
+
if (Array.isArray(a) && a.length !== b.length)
|
|
21
|
+
return false;
|
|
22
|
+
const pairs = seen.get(a) ?? seen.set(a, new Set()).get(a);
|
|
23
|
+
if (pairs.has(b))
|
|
24
|
+
return true;
|
|
25
|
+
pairs.add(b);
|
|
26
|
+
if (data && long(a) && long(b))
|
|
27
|
+
return every(a, b, (x, y) => same(x, y, data, seen));
|
|
28
|
+
if (!plain(a)) {
|
|
29
|
+
if (listed(a) && listed(b))
|
|
30
|
+
return same([...a], [...b], data, seen);
|
|
31
|
+
// Dates, URLs and others keep their value in no field of their own.
|
|
32
|
+
if (!same(json(a), json(b), data, seen))
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const keys = Object.keys(a);
|
|
36
|
+
if (!plain(a) && !keys.length && json(a) === undefined)
|
|
37
|
+
return false;
|
|
38
|
+
return (keys.length === Object.keys(b).length &&
|
|
39
|
+
keys.every(k => Object.hasOwn(b, k) && same(a[k], b[k], data, seen)));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Data by value, typed arrays too, and instances of a class by their own fields and toJSON(), where they have
|
|
43
|
+
* either; any two functions are alike, as inline ones are new each time.
|
|
44
|
+
*/
|
|
45
|
+
export const sameData = (a, b) => same(a, b, true, new Map());
|
|
46
|
+
const typed = (v) => ArrayBuffer.isView(v) && typeof v.length === 'number';
|
|
47
|
+
const akin = (a, b) => typeof a === 'object' &&
|
|
48
|
+
typeof b === 'object' &&
|
|
49
|
+
a !== null &&
|
|
50
|
+
b !== null &&
|
|
51
|
+
Object.getPrototypeOf(a) === Object.getPrototypeOf(b);
|
|
52
|
+
const listed = (v) => v instanceof Map || v instanceof Set;
|
|
53
|
+
const json = (v) => typeof v.toJSON === 'function' ? v.toJSON() : undefined;
|
|
54
|
+
/** A longer list is read by index, as the keys of a million rows take tens of milliseconds, but a sparse one is not. */
|
|
55
|
+
const LONG = 1000;
|
|
56
|
+
const long = (v) => Array.isArray(v) && v.length > LONG && [0, v.length >> 1, v.length - 1].every(i => i in v);
|
|
57
|
+
const every = (a, b, eq) => {
|
|
58
|
+
const n = a.length;
|
|
59
|
+
if (n !== b.length)
|
|
60
|
+
return false;
|
|
61
|
+
for (let i = 0; i < n; i++)
|
|
62
|
+
if (!Object.is(a[i], b[i]) && !eq(a[i], b[i]))
|
|
63
|
+
return false;
|
|
64
|
+
return true;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Data kept from later writes in place, to compare with another copy.
|
|
68
|
+
* ponytail: of a long list only the length, `row` and LONG values evenly spread, so a write in place to another row of
|
|
69
|
+
* one is missed; a digest of the whole list would see it, for a pass over every row each time the pointer comes.
|
|
70
|
+
*/
|
|
71
|
+
export const copied = (v, row = 0, seen = new Map(), lists = []) => {
|
|
72
|
+
if (typed(v) && v.length <= LONG)
|
|
73
|
+
return v.slice();
|
|
74
|
+
if (!typed(v) && !plain(v))
|
|
75
|
+
return v;
|
|
76
|
+
if (seen.has(v))
|
|
77
|
+
return seen.get(v);
|
|
78
|
+
if (typed(v) || long(v)) {
|
|
79
|
+
const c = [v.length];
|
|
80
|
+
seen.set(v, c);
|
|
81
|
+
lists.push([v, c]);
|
|
82
|
+
const step = (v.length - 1) / LONG;
|
|
83
|
+
for (let i = 0; i <= LONG; i++)
|
|
84
|
+
c.push(copied(v[Math.round(i * step)], row, seen, lists));
|
|
85
|
+
c.push(copied(v[row], row, seen, lists));
|
|
86
|
+
return c;
|
|
87
|
+
}
|
|
88
|
+
const c = (Array.isArray(v) ? Object.assign([], { length: v.length }) : Object.create(null));
|
|
89
|
+
seen.set(v, c);
|
|
90
|
+
for (const k of Object.keys(v))
|
|
91
|
+
c[k] = copied(v[k], row, seen, lists);
|
|
92
|
+
return c;
|
|
93
|
+
};
|
|
94
|
+
/** A copy of data for `row`, and `to()`, which moves it to another row without reading the rest again. */
|
|
95
|
+
export function keep(of, row) {
|
|
96
|
+
const seen = new Map();
|
|
97
|
+
const lists = [];
|
|
98
|
+
return {
|
|
99
|
+
of,
|
|
100
|
+
data: copied(of, row, seen, lists),
|
|
101
|
+
to(next = 0) {
|
|
102
|
+
for (const [list, c] of lists)
|
|
103
|
+
c[c.length - 1] = copied(list[next], next, seen, lists);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
package/dist/features/legend.js
CHANGED
|
@@ -44,13 +44,13 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
44
44
|
const hidden = chart.hidden();
|
|
45
45
|
chart.listed().forEach((k, at) => {
|
|
46
46
|
const id = k.group ?? k.label;
|
|
47
|
-
k.entries?.forEach((e, row) => entries.push({ ...e, id, part: [at, row] }));
|
|
47
|
+
k.entries?.forEach((e, row) => entries.push({ ...e, color: chart.color(e.color), id, part: [at, row] }));
|
|
48
48
|
if (k.ramp && (id === undefined || !hidden.includes(id)))
|
|
49
49
|
ramps.push({ label: k.label, ...k.ramp() });
|
|
50
50
|
if (k.entries || k.ramp || k.label === undefined || id === undefined || listed.has(id))
|
|
51
51
|
return;
|
|
52
52
|
listed.add(id);
|
|
53
|
-
entries.push({ label: k.label, color: k.color, id, group: k.group });
|
|
53
|
+
entries.push({ label: k.label, color: chart.color(k.color), id, group: k.group });
|
|
54
54
|
});
|
|
55
55
|
// By default entries and strips share one budget of rows; a strip left out goes behind "+N more",
|
|
56
56
|
// which needs a row of entries to sit in.
|
|
@@ -167,7 +167,8 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
167
167
|
chart.focus(e?.group ?? null, mark && { mark, row: e.part[1] });
|
|
168
168
|
};
|
|
169
169
|
// One listener on the chart, not enter and leave on each entry: an entry rebuilt or removed
|
|
170
|
-
// under the pointer never gets its leave. Capturing, so a tooltip sets its own focus after.
|
|
170
|
+
// under the pointer never gets its leave. Capturing on the host, so a tooltip sets its own focus after.
|
|
171
|
+
const host = root.parentElement;
|
|
171
172
|
const entryAt = (target) => {
|
|
172
173
|
const item = [...shown.keys()].find(i => i.contains(target));
|
|
173
174
|
return [item, item && shown.get(item)];
|
|
@@ -208,8 +209,8 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
208
209
|
if ((e.key === 'Enter' || e.key === ' ') && flip(e.target))
|
|
209
210
|
e.preventDefault();
|
|
210
211
|
});
|
|
211
|
-
|
|
212
|
-
|
|
212
|
+
host.addEventListener('pointerdown', move, true);
|
|
213
|
+
host.addEventListener('pointermove', move, true);
|
|
213
214
|
root.addEventListener('pointerleave', lift);
|
|
214
215
|
root.addEventListener('pointercancel', leave);
|
|
215
216
|
document.addEventListener('pointerdown', leave, true);
|
|
@@ -223,8 +224,8 @@ export function legend({ toggle = true, rows: cap } = {}) {
|
|
|
223
224
|
},
|
|
224
225
|
letGo: () => held && leave(),
|
|
225
226
|
destroy() {
|
|
226
|
-
|
|
227
|
-
|
|
227
|
+
host.removeEventListener('pointerdown', move, true);
|
|
228
|
+
host.removeEventListener('pointermove', move, true);
|
|
228
229
|
root.removeEventListener('pointerleave', lift);
|
|
229
230
|
root.removeEventListener('pointercancel', leave);
|
|
230
231
|
document.removeEventListener('pointerdown', leave, true);
|
|
@@ -15,7 +15,24 @@ export declare function position(chart: Internals, clientX: number, clientY: num
|
|
|
15
15
|
y: number;
|
|
16
16
|
};
|
|
17
17
|
};
|
|
18
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* The presses on each overlay's children by pointer id, false once released with the click still to come, and what
|
|
20
|
+
* the live chart does when they change: by overlay, which a chart made again takes over.
|
|
21
|
+
*/
|
|
22
|
+
export declare const presses: WeakMap<HTMLElement, {
|
|
23
|
+
held: Map<number, boolean>;
|
|
24
|
+
changed(): void;
|
|
25
|
+
}>;
|
|
26
|
+
/** Whether this is the release of a press on an overlay child by a pointer that goes on hovering. */
|
|
27
|
+
export declare const released: ({ overlay }: Internals, e: PointerEvent) => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Whether a click or a double click on the root ends a press that began on an overlay child and was released off it: the
|
|
30
|
+
* browser sends it to the common ancestor of the two ends.
|
|
31
|
+
*/
|
|
32
|
+
export declare const strayed: ({ overlay }: Internals, e: MouseEvent) => boolean;
|
|
33
|
+
/** 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. */
|
|
34
|
+
export declare const owns: (chart: Internals, target: EventTarget | null) => boolean;
|
|
35
|
+
/** Where a pointer event is in CSS pixels, and whether it is on the plot of a chart with axes itself, and not on the app's DOM over it. */
|
|
19
36
|
export declare function locate(chart: Internals, e: MouseEvent): {
|
|
20
37
|
px: number;
|
|
21
38
|
py: number;
|
package/dist/features/pointer.js
CHANGED
|
@@ -16,7 +16,25 @@ export function position(chart, clientX, clientY) {
|
|
|
16
16
|
const inside = px >= m.left && px <= m.left + w && py >= m.top && py <= m.top + h;
|
|
17
17
|
return { px, py, inside, w, h, at: { x: (px - m.left) / w, y: 1 - (py - m.top) / h } };
|
|
18
18
|
}
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* The presses on each overlay's children by pointer id, false once released with the click still to come, and what
|
|
21
|
+
* the live chart does when they change: by overlay, which a chart made again takes over.
|
|
22
|
+
*/
|
|
23
|
+
export const presses = new WeakMap();
|
|
24
|
+
/** Whether this is the release of a press on an overlay child by a pointer that goes on hovering. */
|
|
25
|
+
export const released = ({ overlay }, e) => e.pointerType !== 'touch' && !!overlay && presses.get(overlay)?.held.get(e.pointerId) === false;
|
|
26
|
+
/**
|
|
27
|
+
* Whether a click or a double click on the root ends a press that began on an overlay child and was released off it: the
|
|
28
|
+
* browser sends it to the common ancestor of the two ends.
|
|
29
|
+
*/
|
|
30
|
+
export const strayed = ({ overlay }, e) => {
|
|
31
|
+
const held = overlay && presses.get(overlay)?.held;
|
|
32
|
+
// Some browsers send a click as a MouseEvent, which does not name its pointer, and so are double clicks.
|
|
33
|
+
return !!held && (e instanceof PointerEvent ? held.has(e.pointerId) : held.size > 0);
|
|
34
|
+
};
|
|
35
|
+
/** 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
|
+
export const owns = (chart, target) => target === chart.root || !!chart.overlay?.contains(target);
|
|
37
|
+
/** Where a pointer event is in CSS pixels, and whether it is on the plot of a chart with axes itself, and not on the app's DOM over it. */
|
|
20
38
|
export function locate(chart, e) {
|
|
21
39
|
const { inside, ...where } = position(chart, e.clientX, e.clientY);
|
|
22
40
|
return { ...where, over: inside && chart.frame() === 'axes' && e.target === chart.root };
|
package/dist/features/tooltip.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NEAREST_PX } from '../chart.js';
|
|
2
|
-
import { position } from './pointer.js';
|
|
2
|
+
import { owns, position, released } from './pointer.js';
|
|
3
3
|
const FOCUS_PX = 30;
|
|
4
4
|
export function tooltip(t) {
|
|
5
5
|
return chart => {
|
|
@@ -27,7 +27,7 @@ export function tooltip(t) {
|
|
|
27
27
|
const e = document.createElement('div');
|
|
28
28
|
e.textContent = r.text;
|
|
29
29
|
if (r.color)
|
|
30
|
-
e.style.color = r.color;
|
|
30
|
+
e.style.color = chart.color(r.color);
|
|
31
31
|
return e;
|
|
32
32
|
}));
|
|
33
33
|
const fx = x > width / 2;
|
|
@@ -57,7 +57,7 @@ export function tooltip(t) {
|
|
|
57
57
|
fromApp = true;
|
|
58
58
|
({ x: px, y: py } = p);
|
|
59
59
|
}
|
|
60
|
-
if (chart.drag.active)
|
|
60
|
+
if (chart.drag.active || chart.drag.child)
|
|
61
61
|
return hide();
|
|
62
62
|
if (t.mode === 'nearest') {
|
|
63
63
|
if (py === undefined)
|
|
@@ -127,12 +127,12 @@ export function tooltip(t) {
|
|
|
127
127
|
};
|
|
128
128
|
const move = (e) => {
|
|
129
129
|
inside = true;
|
|
130
|
-
if (chart.drag.active) {
|
|
130
|
+
if (chart.drag.active || chart.drag.child) {
|
|
131
131
|
at = null;
|
|
132
132
|
return hide();
|
|
133
133
|
}
|
|
134
134
|
// Over another feature's layer, such as a legend, the focus is that feature's to set.
|
|
135
|
-
if (e.target
|
|
135
|
+
if (!owns(chart, e.target)) {
|
|
136
136
|
at = null;
|
|
137
137
|
held = false;
|
|
138
138
|
fromApp = false;
|
|
@@ -143,6 +143,10 @@ export function tooltip(t) {
|
|
|
143
143
|
held = false;
|
|
144
144
|
place();
|
|
145
145
|
};
|
|
146
|
+
const release = (e) => {
|
|
147
|
+
if (released(chart, e))
|
|
148
|
+
move(e);
|
|
149
|
+
};
|
|
146
150
|
const leave = () => {
|
|
147
151
|
at = null;
|
|
148
152
|
held = false;
|
|
@@ -170,7 +174,9 @@ export function tooltip(t) {
|
|
|
170
174
|
out();
|
|
171
175
|
};
|
|
172
176
|
root.addEventListener('pointerdown', move);
|
|
173
|
-
|
|
177
|
+
// Capturing, so a child that stops its own events still moves the tip and brings it back.
|
|
178
|
+
root.addEventListener('pointermove', move, true);
|
|
179
|
+
root.addEventListener('pointerup', release, true);
|
|
174
180
|
root.addEventListener('pointerleave', lift);
|
|
175
181
|
root.addEventListener('pointercancel', out);
|
|
176
182
|
document.addEventListener('pointerdown', away, true);
|
|
@@ -183,7 +189,8 @@ export function tooltip(t) {
|
|
|
183
189
|
letGo: () => held && leave(),
|
|
184
190
|
destroy() {
|
|
185
191
|
root.removeEventListener('pointerdown', move);
|
|
186
|
-
root.removeEventListener('pointermove', move);
|
|
192
|
+
root.removeEventListener('pointermove', move, true);
|
|
193
|
+
root.removeEventListener('pointerup', release, true);
|
|
187
194
|
root.removeEventListener('pointerleave', lift);
|
|
188
195
|
root.removeEventListener('pointercancel', out);
|
|
189
196
|
document.removeEventListener('pointerdown', away, true);
|
package/dist/features/zoom.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { locate as where, settle as release, SLOP_PX, unselect } from './pointer.js';
|
|
1
|
+
import { locate as where, settle as release, SLOP_PX, strayed, unselect } from './pointer.js';
|
|
2
2
|
// How far a drag that pans both axes must move up or down before it pans y: a hand does not move level.
|
|
3
3
|
const LEVEL_PX = 8;
|
|
4
4
|
const DOUBLE_TAP_MS = 300;
|
|
@@ -126,7 +126,7 @@ export function zoom(z = {}) {
|
|
|
126
126
|
};
|
|
127
127
|
const whole = () => ask(sides.length === 2 ? null : { [sides[0]]: undefined });
|
|
128
128
|
const reset = (e) => {
|
|
129
|
-
if (locate(e).over)
|
|
129
|
+
if (locate(e).over && !strayed(chart, e))
|
|
130
130
|
whole();
|
|
131
131
|
};
|
|
132
132
|
const touches = new Map();
|
package/dist/gl.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { GlLayer } from './types.js';
|
|
2
2
|
export declare function program(gl: WebGL2RenderingContext, vertex: string, fragment: string): WebGLProgram;
|
|
3
3
|
export declare function glLayer(canvas: HTMLCanvasElement): GlLayer | null;
|
|
4
|
+
/** A colour for addColorStop, which takes no CSS variable. */
|
|
5
|
+
export declare function stop(ctx: CanvasRenderingContext2D, color: string): string;
|
|
4
6
|
/** A CSS colour as red, green, blue and opacity from 0 to 1. */
|
|
5
7
|
export declare function rgba(ctx: CanvasRenderingContext2D, color: string): number[];
|
package/dist/gl.js
CHANGED
|
@@ -38,6 +38,13 @@ export function glLayer(canvas) {
|
|
|
38
38
|
},
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
|
+
/** A colour for addColorStop, which takes no CSS variable. */
|
|
42
|
+
export function stop(ctx, color) {
|
|
43
|
+
if (!color.includes('var('))
|
|
44
|
+
return color;
|
|
45
|
+
ctx.fillStyle = color;
|
|
46
|
+
return ctx.fillStyle;
|
|
47
|
+
}
|
|
41
48
|
/** A CSS colour as red, green, blue and opacity from 0 to 1. */
|
|
42
49
|
export function rgba(ctx, color) {
|
|
43
50
|
ctx.fillStyle = color;
|
|
@@ -46,7 +53,7 @@ export function rgba(ctx, color) {
|
|
|
46
53
|
if (css.startsWith('#'))
|
|
47
54
|
return [1, 3, 5].map(i => parseInt(css.slice(i, i + 2), 16) / 255).concat(1);
|
|
48
55
|
const c = new OffscreenCanvas(1, 1).getContext('2d');
|
|
49
|
-
c.fillStyle =
|
|
56
|
+
c.fillStyle = css;
|
|
50
57
|
c.fillRect(0, 0, 1, 1);
|
|
51
58
|
return [...c.getImageData(0, 0, 1, 1).data].map(v => v / 255);
|
|
52
59
|
}
|
package/dist/marks/sankey.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { glLayer, program, rgba } from '../gl.js';
|
|
1
|
+
import { glLayer, program, rgba, stop } from '../gl.js';
|
|
2
2
|
// Canvas fills a ribbon at a time, and from about here a hover takes longer than a frame.
|
|
3
3
|
const GL_FROM = 1000;
|
|
4
4
|
const SEGMENTS = 24;
|
|
@@ -332,8 +332,8 @@ export function sankey(o) {
|
|
|
332
332
|
? undefined
|
|
333
333
|
: ribbons.map((_, i) => {
|
|
334
334
|
const across = ctx.createLinearGradient(x[source[i]] + thick, 0, x[target[i]], 0);
|
|
335
|
-
across.addColorStop(0, shade(source[i]));
|
|
336
|
-
across.addColorStop(1, shade(target[i]));
|
|
335
|
+
across.addColorStop(0, stop(ctx, shade(source[i])));
|
|
336
|
+
across.addColorStop(1, stop(ctx, shade(target[i])));
|
|
337
337
|
return across;
|
|
338
338
|
});
|
|
339
339
|
return {
|