swiftplot 0.3.0 → 0.4.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 CHANGED
@@ -60,20 +60,21 @@ chart.destroy();
60
60
 
61
61
  - **Defaults:**
62
62
 
63
- | Thing | Default | Override |
64
- | ------------------- | ------------------------------------------------------------------------- | -------------------------------------- |
65
- | Axis labels | the field's name; none for the row index, an accessor or several y fields | `xAxis.label`, `yAxis.label` |
66
- | Colours | `palette` in order, `paletteDark` on the dark theme | `colors` |
67
- | Tooltip | on; text in the y axis format, else two decimals at most | `tooltip: false` |
68
- | Legend | on with two or more series or named parts; a heatmap's strip of colours | `legend: false` or `legend()` options |
69
- | Zoom | off | `zoom: true` or `zoom()` options |
70
- | Animation | off | `animate: true` or `animate()` options |
71
- | Theme | follows the page | `theme: 'light' \| 'dark' \| Theme` |
72
- | Size | follows the container | `width`, `height`, `margin` |
73
- | Several y on areas | overlapping, half transparent | `stack: true` |
74
- | Several y on bars | side by side | `stack: true` |
75
- | Line and area shape | straight, with gaps | `curve`, `join` |
76
- | Scatter radius | 4 px | `r`: a number or a field |
63
+ | Thing | Default | Override |
64
+ | ------------------- | ------------------------------------------------------------------------- | --------------------------------------- |
65
+ | Axis labels | the field's name; none for the row index, an accessor or several y fields | `xAxis.label`, `yAxis.label` |
66
+ | Colours | `palette` in order, `paletteDark` on the dark theme | `colors` |
67
+ | Tooltip | on; text in the y axis format, else two decimals at most | `tooltip: false` |
68
+ | Legend | on with two or more series or named parts; a heatmap's strip of colours | `legend: false` or `legend()` options |
69
+ | Zoom | off | `zoom: true` or `zoom()` options |
70
+ | Animation | on: a reveal on the first paint, a move or a cross-fade on new data | `animate: false` or `animate()` options |
71
+ | Hover | everything else fades to 0.3 while one series or part is pointed at | `dim: false` or another opacity |
72
+ | Theme | follows the page | `theme: 'light' \| 'dark' \| Theme` |
73
+ | Size | follows the container | `width`, `height`, `margin` |
74
+ | Several y on areas | overlapping, half transparent | `stack: true` |
75
+ | Several y on bars | side by side | `stack: true` |
76
+ | Line and area shape | straight, with gaps | `curve`, `join` |
77
+ | Scatter radius | 4 px | `r`: a number or a field |
77
78
 
78
79
  - **Marks added to a chart:** line, area, bar, scatter and combo charts take `rules` (`{ x }` or `{ y }`, with `text`, `dashed` and `color`), `zones` (`{ x: [from, to] }` or `{ y: [from, to] }`, with `text` and `color`) and `brush` (`true` or `{ axes }`; with `zoom` its release shows the range), whose range goes to `onBrush`. `errorBars: { low, high, cap, width }` on line, bar and scatter charts names two fields that are values on the y axis, not distances from y, for a chart of one unstacked `y`. A line takes `fill` (`true` or a gradient) and a scatter `alpha`, `ring` and `shape: 'tick'`.
79
80
  - **Stacking** counts a missing value as zero, so a stacked area has no gap where an unstacked one would. Long series are stacked over the union of their x values.
package/dist/chart.js CHANGED
@@ -778,9 +778,7 @@ export function plot(el, options) {
778
778
  let several = false;
779
779
  // One row is picked out where there is no other series to set the hovered one against, or where
780
780
  // the row is a named part of its mark, such as a slice, which is then the thing pointed at.
781
- const drawnRow = () => (o.dim ?? 1) !== 1 && focusedRow && !focusedRow.mark.rowsAlike && (!several || focusedRow.mark.entries)
782
- ? focusedRow
783
- : undefined;
781
+ const drawnRow = () => (o.dim ?? 1) !== 1 && focusedRow && (!several || focusedRow.mark.entries) ? focusedRow : undefined;
784
782
  const chosen = () => {
785
783
  if (!o.selected?.length)
786
784
  return undefined;
@@ -9,6 +9,7 @@ export declare const sizeOf: (p: Base) => {
9
9
  height: number | undefined;
10
10
  margin: Partial<import("../types.js").Margin> | undefined;
11
11
  description: string | undefined;
12
+ dim: number;
12
13
  };
13
14
  export declare const featuresOf: (p: Base & Pick<Axes, 'zoom'>, listed: number) => {
14
15
  tooltip: boolean;
@@ -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({ width: p.width, height: p.height, margin: p.margin, description: p.description });
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),
@@ -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 false */
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. */
@@ -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
- // and of several filled marks the one drawn last.
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
- if (a !== b && py >= Math.min(a, b) && py <= Math.max(a, b))
114
- fill = k.group;
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
- const group = line ?? fill ?? null;
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`;
@@ -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);
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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swiftplot",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Small, fast charts on Canvas 2D and WebGL2",
5
5
  "license": "MIT",
6
6
  "repository": {