swiftplot 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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 './controller.js';
9
+ export { equal, plain } from '../equal.js';
10
10
  export { mount } from './mount.js';
@@ -13,6 +13,9 @@ export const mount = (el, options) => {
13
13
  get chart() {
14
14
  return c.chart;
15
15
  },
16
+ get overlay() {
17
+ return c.overlay;
18
+ },
16
19
  update(partial) {
17
20
  c.update('theme' in partial ? { ...partial, theme: partial.theme ?? light } : partial);
18
21
  },
@@ -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[];
@@ -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
+ }
@@ -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
+ }
@@ -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
- root.addEventListener('pointerdown', move, true);
212
- root.addEventListener('pointermove', move, true);
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
- root.removeEventListener('pointerdown', move, true);
227
- root.removeEventListener('pointermove', move, true);
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
- /** Where a pointer event is in CSS pixels, and whether it is over the plot of a chart with axes. */
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;
@@ -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
- /** Where a pointer event is in CSS pixels, and whether it is over the plot of a chart with axes. */
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 };
@@ -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 !== root) {
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
- root.addEventListener('pointermove', move);
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);
@@ -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 = color;
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
  }
@@ -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 {
package/dist/types.d.ts CHANGED
@@ -222,6 +222,44 @@ export type Margin = {
222
222
  left: number;
223
223
  };
224
224
  export type Span = [number, number];
225
+ /** The geometry of a chart, for an app drawing its own nodes over it; each change gives a new one, never mutated. Pixels are CSS pixels relative to the chart. */
226
+ export type Layout = {
227
+ readonly width: number;
228
+ readonly height: number;
229
+ /** The plot rectangle is inside it. */
230
+ readonly margin: Readonly<Margin>;
231
+ readonly frame: NonNullable<Options['frame']>;
232
+ /** What is shown on an axis, in real values, as the app passes them; the axes and the conversions are there only with frame 'axes'. */
233
+ readonly x?: LayoutAxis;
234
+ readonly y?: LayoutAxis;
235
+ /** Only when there is a second axis. */
236
+ readonly y2?: LayoutAxis;
237
+ /** A real value as a pixel, also outside what is shown. */
238
+ readonly xPx?: (x: number) => number;
239
+ /** On `y` unless the second axis is named. */
240
+ readonly yPx?: (y: number, axis?: 'y' | 'y2') => number;
241
+ /** A pixel as a real value. */
242
+ readonly xValue?: (px: number) => number;
243
+ readonly yValue?: (px: number, axis?: 'y' | 'y2') => number;
244
+ };
245
+ export type LayoutAxis = {
246
+ readonly domain: Readonly<Span>;
247
+ readonly ticks: readonly Readonly<Tick>[];
248
+ };
249
+ /**
250
+ * The user's pointer, and the nearest thing within 20 px of it or null. px and py are the pointer in CSS
251
+ * pixels relative to the chart; the hit is the one onClick gets, in real values, and its own px and py
252
+ * are where it is anchored, such as a dot's centre. onClick's second argument differs: it is values, not pixels.
253
+ */
254
+ export type Hover = {
255
+ px: number;
256
+ py: number;
257
+ hit: (Hit & {
258
+ mark: Mark;
259
+ px: number;
260
+ py: number;
261
+ }) | null;
262
+ };
225
263
  export type Options = {
226
264
  /** The width in CSS pixels; when omitted the chart follows its container. */
227
265
  width?: number;
@@ -256,7 +294,7 @@ export type Options = {
256
294
  description?: string;
257
295
  /** Opacity multiplier for every other group while one is focused. @default 1 */
258
296
  dim?: number;
259
- /** `light` when left out. Replacing it with another object repaints the chart and restyles its text. */
297
+ /** `light` when left out. Replaced by another object, CSS variables are read again, and the chart is restyled if a colour changed. */
260
298
  theme?: Theme;
261
299
  /**
262
300
  * The part of each axis shown, in its values; a side left out shows the whole domain. It is shown
@@ -320,10 +358,24 @@ export type Options = {
320
358
  y: number;
321
359
  y2?: number;
322
360
  } | null): void;
361
+ /**
362
+ * Called with the chart's geometry after the first paint, inside plot(), and whenever the size, the
363
+ * margin, what an axis shows, its ticks or its title change; also when it is set where there was
364
+ * none, but not for another function in its place. Nothing is called while the chart has no room.
365
+ */
366
+ onLayout?(layout: Layout): void;
367
+ /**
368
+ * Called as the user's pointer moves over the plot, and when what is under it at rest changes, such as
369
+ * after a zoom or new data; with null once as it leaves the plot or a drag starts. A mark that answers no
370
+ * pointer, such as a line, gives a null hit. Nothing is called twice for the same pointer and hit.
371
+ */
372
+ onHover?(hover: Hover | null): void;
323
373
  };
324
374
  export type Chart = {
325
375
  update(options: Partial<Omit<Options, 'use'>>): void;
326
376
  destroy(): void;
377
+ /** An element over the whole chart, under its tooltip, for the app's own DOM. Made when first read; it takes no pointer. */
378
+ readonly overlay: HTMLElement;
327
379
  };
328
380
  /**
329
381
  * Wraps a repaint. `draw` paints the chart now; `draw(0)` paints it with no marks. `why` says what
@@ -339,6 +391,8 @@ export type Internals = {
339
391
  root: HTMLElement;
340
392
  /** The 2D canvas holding the grid and every mark that is not on WebGL. */
341
393
  canvas: HTMLCanvasElement;
394
+ /** What Chart.overlay gave, once it was read. Set to carry over that of a chart this one replaces. */
395
+ overlay: HTMLElement | undefined;
342
396
  /** One painter per chart; the last one set wins. */
343
397
  onPaint(painter: Painter | undefined): void;
344
398
  /**
@@ -376,6 +430,8 @@ export type Internals = {
376
430
  };
377
431
  margin(): Margin;
378
432
  theme(): Theme;
433
+ /** A CSS colour with its variables resolved, as the canvas draws it. */
434
+ color(css: string): string;
379
435
  xPx(x: number): number;
380
436
  /** On `y` unless the second axis is named; a mark's own is yAxis(mark). */
381
437
  yPx(y: number, axis?: 'y' | 'y2'): number;
@@ -425,13 +481,15 @@ export type Internals = {
425
481
  * gets none, and 'shift' when a drag with shift held pans and a brush keeps the plain ones; with no
426
482
  * brush a plain drag then pans too, unless zoom() was given `drag: 'shift'`. `active` is set while
427
483
  * a drag or a gesture is under way, and until the click after its release has gone by.
484
+ * `child` is set while a press that began on a child of the overlay is held.
428
485
  */
429
486
  drag: {
430
487
  brush: boolean;
431
488
  pan: 'plain' | 'shift' | false;
432
489
  active: boolean;
490
+ readonly child: boolean;
433
491
  };
434
- /** Runs every feature's update(), for a change under a resting pointer that the chart cannot see, such as the end of a drag. */
492
+ /** Runs every feature's update() and then reports the hover to the app, for a change under a resting pointer that the chart cannot see, such as the end of a drag. */
435
493
  sync(): void;
436
494
  hit(px: number, py: number, max: number): (Hit & {
437
495
  mark: Mark;
@@ -452,8 +510,9 @@ export type Internals = {
452
510
  };
453
511
  /**
454
512
  * update() runs whenever what is under a resting pointer may have changed: after the first paint, for
455
- * new options or a new size, a series hidden or shown, a new window, and at the end of a drag.
456
- * letGo() runs before the repaint for each of these but the first paint, the end of a drag and an
513
+ * new options or a new size, a series hidden or shown, a new window, at the end of a drag, and at the
514
+ * start and the end of a press on a child of the overlay. A theme that changes no colour is no new option.
515
+ * letGo() runs before the repaint for each of these but the first paint, the end of a drag, a press and an
457
516
  * update of only `pointer` or callbacks, so a focus dropped there does not cut a data animation short.
458
517
  */
459
518
  export type Feature = (chart: Internals) => {
package/package.json CHANGED
@@ -1,14 +1,25 @@
1
1
  {
2
2
  "name": "swiftplot",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "Small, fast charts on Canvas 2D and WebGL2",
5
+ "keywords": [
6
+ "chart",
7
+ "charts",
8
+ "plot",
9
+ "canvas",
10
+ "webgl",
11
+ "visualization",
12
+ "dataviz",
13
+ "timeseries",
14
+ "typescript"
15
+ ],
5
16
  "license": "MIT",
6
17
  "repository": {
7
18
  "type": "git",
8
19
  "url": "git+https://github.com/GusevP/swiftplot.git",
9
20
  "directory": "packages/core"
10
21
  },
11
- "homepage": "https://github.com/GusevP/swiftplot#readme",
22
+ "homepage": "https://swiftplot.dev",
12
23
  "bugs": {
13
24
  "url": "https://github.com/GusevP/swiftplot/issues"
14
25
  },