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.
@@ -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
  }
@@ -40,7 +40,8 @@ export function dots(o) {
40
40
  }
41
41
  return at < 0 ? null : { x: x[at], y: y[at], distance: Math.sqrt(best), row: at };
42
42
  };
43
- const prepareGl = (view, { gl }) => {
43
+ const prepareGl = (view, layer) => {
44
+ const { gl } = layer;
44
45
  const u = programs.get(gl) ?? pointProgram(gl);
45
46
  programs.set(gl, u);
46
47
  // The offsets are subtracted in float64: a float32 attribute cannot hold e.g. millisecond timestamps.
@@ -76,10 +77,16 @@ export function dots(o) {
76
77
  };
77
78
  return {
78
79
  // The alpha argument is unused: dense points saturate, so the layer's fade() dims a group instead.
79
- paint() {
80
+ paint(_, row, rest = 1) {
80
81
  ready();
81
82
  own();
82
83
  gl.drawArrays(gl.POINTS, 0, n);
84
+ if (row === undefined || rest === 1)
85
+ return;
86
+ layer.fade(rest);
87
+ ready();
88
+ own();
89
+ gl.drawArrays(gl.POINTS, row, 1);
83
90
  },
84
91
  select(rows, outline) {
85
92
  ready();
@@ -98,7 +105,6 @@ export function dots(o) {
98
105
  return {
99
106
  ...base('dots', o, [x], [y]),
100
107
  overhang,
101
- rowsAlike: true,
102
108
  prepare(view) {
103
109
  const { ctx, dpr, ox, kx, oy, ky, theme, left, top, width, height } = view;
104
110
  // A point further out than twice the overhang cannot reach the area its mark is cut to.
@@ -126,7 +132,14 @@ export function dots(o) {
126
132
  ctx.stroke(shape);
127
133
  };
128
134
  return {
129
- paint: a => fill(path, a),
135
+ paint(a, row, rest = 1) {
136
+ if (row === undefined)
137
+ return fill(path, a);
138
+ fill(path, a * rest);
139
+ const one = new Path2D();
140
+ circle(one, row);
141
+ fill(one, a);
142
+ },
130
143
  select(rows, outline) {
131
144
  const some = new Path2D();
132
145
  const edge = new Path2D();
@@ -54,11 +54,21 @@ export function gpuRows(holds, x, y) {
54
54
  let held = holds.get(gl);
55
55
  if (!held) {
56
56
  holds.set(gl, (held = { vao: gl.createVertexArray(), buffer: gl.createBuffer() }));
57
- const data = new Float32Array(n * kept.length);
58
- for (let i = 0, j = 0; i < n; i++) {
59
- const whole = kept.every(at => Number.isFinite(columns[at][i]));
60
- for (const at of kept)
61
- data[j++] = whole ? columns[at][i] - zero(at) : NaN;
57
+ const w = kept.length;
58
+ const data = new Float32Array(n * w);
59
+ // Filled a column at a time: a closure and an iterator per row took 9 ms for 100,000 bars.
60
+ kept.forEach((at, k) => {
61
+ const c = columns[at];
62
+ const z = zero(at);
63
+ for (let i = 0, j = k; i < n; i++, j += w)
64
+ data[j] = c[i] - z;
65
+ });
66
+ for (let j = 0; j < data.length; j += w) {
67
+ let sum = 0;
68
+ for (let k = 0; k < w; k++)
69
+ sum += Math.abs(data[j + k]);
70
+ if (!(sum < Infinity))
71
+ data.fill(NaN, j, j + w);
62
72
  }
63
73
  gl.bindBuffer(gl.ARRAY_BUFFER, held.buffer);
64
74
  gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
@@ -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
@@ -159,8 +159,6 @@ export type Mark = {
159
159
  label: string;
160
160
  color: string;
161
161
  }[];
162
- /** paint() draws every row alike, so a focused row focuses the mark's group instead; select() still picks rows out. */
163
- rowsAlike?: true;
164
162
  /** paint() draws the mark as one shape and ignores a row, so selecting any of its rows draws it whole. */
165
163
  oneShape?: true;
166
164
  /**
@@ -224,6 +222,44 @@ export type Margin = {
224
222
  left: number;
225
223
  };
226
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
+ };
227
263
  export type Options = {
228
264
  /** The width in CSS pixels; when omitted the chart follows its container. */
229
265
  width?: number;
@@ -258,7 +294,7 @@ export type Options = {
258
294
  description?: string;
259
295
  /** Opacity multiplier for every other group while one is focused. @default 1 */
260
296
  dim?: number;
261
- /** `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. */
262
298
  theme?: Theme;
263
299
  /**
264
300
  * The part of each axis shown, in its values; a side left out shows the whole domain. It is shown
@@ -322,10 +358,24 @@ export type Options = {
322
358
  y: number;
323
359
  y2?: number;
324
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;
325
373
  };
326
374
  export type Chart = {
327
375
  update(options: Partial<Omit<Options, 'use'>>): void;
328
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;
329
379
  };
330
380
  /**
331
381
  * Wraps a repaint. `draw` paints the chart now; `draw(0)` paints it with no marks. `why` says what
@@ -341,6 +391,8 @@ export type Internals = {
341
391
  root: HTMLElement;
342
392
  /** The 2D canvas holding the grid and every mark that is not on WebGL. */
343
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;
344
396
  /** One painter per chart; the last one set wins. */
345
397
  onPaint(painter: Painter | undefined): void;
346
398
  /**
@@ -378,6 +430,8 @@ export type Internals = {
378
430
  };
379
431
  margin(): Margin;
380
432
  theme(): Theme;
433
+ /** A CSS colour with its variables resolved, as the canvas draws it. */
434
+ color(css: string): string;
381
435
  xPx(x: number): number;
382
436
  /** On `y` unless the second axis is named; a mark's own is yAxis(mark). */
383
437
  yPx(y: number, axis?: 'y' | 'y2'): number;
@@ -427,13 +481,15 @@ export type Internals = {
427
481
  * gets none, and 'shift' when a drag with shift held pans and a brush keeps the plain ones; with no
428
482
  * brush a plain drag then pans too, unless zoom() was given `drag: 'shift'`. `active` is set while
429
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.
430
485
  */
431
486
  drag: {
432
487
  brush: boolean;
433
488
  pan: 'plain' | 'shift' | false;
434
489
  active: boolean;
490
+ readonly child: boolean;
435
491
  };
436
- /** 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. */
437
493
  sync(): void;
438
494
  hit(px: number, py: number, max: number): (Hit & {
439
495
  mark: Mark;
@@ -454,8 +510,9 @@ export type Internals = {
454
510
  };
455
511
  /**
456
512
  * update() runs whenever what is under a resting pointer may have changed: after the first paint, for
457
- * new options or a new size, a series hidden or shown, a new window, and at the end of a drag.
458
- * 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
459
516
  * update of only `pointer` or callbacks, so a focus dropped there does not cut a data animation short.
460
517
  */
461
518
  export type Feature = (chart: Internals) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swiftplot",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Small, fast charts on Canvas 2D and WebGL2",
5
5
  "license": "MIT",
6
6
  "repository": {