swiftplot 0.3.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 +38 -19
- package/dist/chart.js +296 -32
- package/dist/charts/build.d.ts +1 -0
- package/dist/charts/build.js +8 -2
- 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 +7 -1
- 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 +22 -12
- package/dist/features/zoom.js +2 -2
- package/dist/gl.d.ts +2 -0
- package/dist/gl.js +8 -1
- package/dist/marks/dots.js +17 -4
- package/dist/marks/rects.js +15 -5
- package/dist/marks/sankey.js +3 -3
- package/dist/types.d.ts +63 -6
- package/package.json +1 -1
package/dist/charts/build.js
CHANGED
|
@@ -8,12 +8,18 @@ import { create, PASSED, } from './controller.js';
|
|
|
8
8
|
import { source } from './source.js';
|
|
9
9
|
export const defined = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
10
10
|
export const on = (v) => v === true ? {} : v || false;
|
|
11
|
-
export const sizeOf = (p) => defined({
|
|
11
|
+
export const sizeOf = (p) => defined({
|
|
12
|
+
width: p.width,
|
|
13
|
+
height: p.height,
|
|
14
|
+
margin: p.margin,
|
|
15
|
+
description: p.description,
|
|
16
|
+
dim: p.dim === false ? 1 : (p.dim ?? 0.3),
|
|
17
|
+
});
|
|
12
18
|
export const featuresOf = (p, listed) => ({
|
|
13
19
|
tooltip: p.tooltip !== false,
|
|
14
20
|
legend: p.legend !== false && listed >= 2 && (typeof p.legend === 'object' ? p.legend : {}),
|
|
15
21
|
zoom: on(p.zoom),
|
|
16
|
-
animate: on(p.animate),
|
|
22
|
+
animate: on(p.animate ?? true),
|
|
17
23
|
});
|
|
18
24
|
export const install = (f, tip) => [
|
|
19
25
|
f.tooltip && tooltip(tip),
|
|
@@ -11,7 +11,7 @@ export type Context = {
|
|
|
11
11
|
/** make() runs again only when deps differ, element by element, from the last call under this name. */
|
|
12
12
|
memo: <T>(name: string, deps: readonly unknown[], make: () => T) => T;
|
|
13
13
|
};
|
|
14
|
-
type Passed = 'view' | 'hidden' | 'selected' | 'pointer' | 'onViewChange' | 'onHiddenChange' | 'onClick' | 'onPointerChange';
|
|
14
|
+
type Passed = 'view' | 'hidden' | 'selected' | 'pointer' | 'onViewChange' | 'onHiddenChange' | 'onClick' | 'onPointerChange' | 'onLayout' | 'onHover';
|
|
15
15
|
export type Built = {
|
|
16
16
|
options: Omit<Options, 'use' | Passed>;
|
|
17
17
|
/** Compared by value across updates: a change recreates the chart. */
|
|
@@ -23,10 +23,9 @@ export type EasyChart<P> = {
|
|
|
23
23
|
update(props: Partial<P>): void;
|
|
24
24
|
destroy(): void;
|
|
25
25
|
readonly chart: Chart;
|
|
26
|
+
/** The chart's own overlay, the same element with its children when the chart is made again. */
|
|
27
|
+
readonly overlay: HTMLElement;
|
|
26
28
|
};
|
|
27
29
|
export declare const PASSED: Set<string>;
|
|
28
|
-
export declare const plain: (v: unknown) => v is Record<string, unknown>;
|
|
29
|
-
/** Plain objects and arrays by value, anything else (typed arrays, functions, themes) by identity. */
|
|
30
|
-
export declare const equal: (a: unknown, b: unknown) => boolean;
|
|
31
30
|
export declare function create<P extends ControllerProps>(el: HTMLElement, initial: P, build: Build<P>): EasyChart<P>;
|
|
32
31
|
export {};
|
|
@@ -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
|
@@ -123,8 +123,10 @@ export type Base = {
|
|
|
123
123
|
tooltip?: boolean;
|
|
124
124
|
/** The legend, which hides and shows series, or its options; shown with two or more series. @default true */
|
|
125
125
|
legend?: boolean | LegendOptions;
|
|
126
|
-
/** Animation, or its options. @default
|
|
126
|
+
/** Animation, or its options: marks are revealed on the first paint and move or cross-fade when the data changes. @default true */
|
|
127
127
|
animate?: boolean | AnimateOptions;
|
|
128
|
+
/** The opacity of everything else while one series, or one part of a single series, is pointed at; `false` leaves it as it is. @default 0.3 */
|
|
129
|
+
dim?: number | false;
|
|
128
130
|
/** Width in CSS pixels; the chart follows its container when it is left out. */
|
|
129
131
|
width?: number;
|
|
130
132
|
/** Height in CSS pixels; the chart follows its container when it is left out. */
|
|
@@ -145,6 +147,10 @@ export type Base = {
|
|
|
145
147
|
pointer?: Options['pointer'];
|
|
146
148
|
/** Called as the user's pointer moves over the plot, and with null as it leaves; never for update(). */
|
|
147
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'];
|
|
148
154
|
};
|
|
149
155
|
type Name<T> = keyof T & string;
|
|
150
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)
|
|
@@ -81,11 +81,12 @@ export function tooltip(t) {
|
|
|
81
81
|
const under = chart.xValue(px);
|
|
82
82
|
const v = t.snap?.(under) ?? under;
|
|
83
83
|
const rows = [{ text: t.title(chart.real('x', v)) }];
|
|
84
|
-
// A line within reach of the pointer is focused before the filled mark the pointer is in,
|
|
85
|
-
//
|
|
84
|
+
// A line within reach of the pointer is focused before the filled mark the pointer is in, and of
|
|
85
|
+
// several filled marks the thinnest there, so each of overlapping areas has a part that picks it.
|
|
86
86
|
let near = FOCUS_PX;
|
|
87
87
|
let line;
|
|
88
88
|
let fill;
|
|
89
|
+
let thin = Infinity;
|
|
89
90
|
for (const k of chart.marks()) {
|
|
90
91
|
if (!k.sampleAtX)
|
|
91
92
|
continue;
|
|
@@ -110,11 +111,13 @@ export function tooltip(t) {
|
|
|
110
111
|
else {
|
|
111
112
|
const a = chart.yPx(s.y1, on);
|
|
112
113
|
const b = chart.yPx(s.y2, on);
|
|
113
|
-
|
|
114
|
-
|
|
114
|
+
const span = Math.abs(a - b);
|
|
115
|
+
if (span && py >= Math.min(a, b) && py <= Math.max(a, b) && span <= thin)
|
|
116
|
+
[thin, fill] = [span, k.group];
|
|
115
117
|
}
|
|
116
118
|
}
|
|
117
|
-
|
|
119
|
+
// A mark that answers the pointer where it is, such as a bar, is focused where neither applies.
|
|
120
|
+
const group = line ?? fill ?? (py === undefined ? undefined : chart.hit(px, py, 0)?.mark.group) ?? null;
|
|
118
121
|
cross.style.display = '';
|
|
119
122
|
cross.style.left = `${px}px`;
|
|
120
123
|
cross.style.top = `${m.top}px`;
|
|
@@ -124,12 +127,12 @@ export function tooltip(t) {
|
|
|
124
127
|
};
|
|
125
128
|
const move = (e) => {
|
|
126
129
|
inside = true;
|
|
127
|
-
if (chart.drag.active) {
|
|
130
|
+
if (chart.drag.active || chart.drag.child) {
|
|
128
131
|
at = null;
|
|
129
132
|
return hide();
|
|
130
133
|
}
|
|
131
134
|
// Over another feature's layer, such as a legend, the focus is that feature's to set.
|
|
132
|
-
if (e.target
|
|
135
|
+
if (!owns(chart, e.target)) {
|
|
133
136
|
at = null;
|
|
134
137
|
held = false;
|
|
135
138
|
fromApp = false;
|
|
@@ -140,6 +143,10 @@ export function tooltip(t) {
|
|
|
140
143
|
held = false;
|
|
141
144
|
place();
|
|
142
145
|
};
|
|
146
|
+
const release = (e) => {
|
|
147
|
+
if (released(chart, e))
|
|
148
|
+
move(e);
|
|
149
|
+
};
|
|
143
150
|
const leave = () => {
|
|
144
151
|
at = null;
|
|
145
152
|
held = false;
|
|
@@ -167,7 +174,9 @@ export function tooltip(t) {
|
|
|
167
174
|
out();
|
|
168
175
|
};
|
|
169
176
|
root.addEventListener('pointerdown', move);
|
|
170
|
-
|
|
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);
|
|
171
180
|
root.addEventListener('pointerleave', lift);
|
|
172
181
|
root.addEventListener('pointercancel', out);
|
|
173
182
|
document.addEventListener('pointerdown', away, true);
|
|
@@ -180,7 +189,8 @@ export function tooltip(t) {
|
|
|
180
189
|
letGo: () => held && leave(),
|
|
181
190
|
destroy() {
|
|
182
191
|
root.removeEventListener('pointerdown', move);
|
|
183
|
-
root.removeEventListener('pointermove', move);
|
|
192
|
+
root.removeEventListener('pointermove', move, true);
|
|
193
|
+
root.removeEventListener('pointerup', release, true);
|
|
184
194
|
root.removeEventListener('pointerleave', lift);
|
|
185
195
|
root.removeEventListener('pointercancel', out);
|
|
186
196
|
document.removeEventListener('pointerdown', away, true);
|