swiftplot 0.1.1 → 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 +86 -61
- package/dist/chart.d.ts +4 -0
- package/dist/chart.js +172 -89
- package/dist/charts/build.d.ts +53 -0
- package/dist/charts/build.js +69 -0
- package/dist/charts/candlestick.d.ts +8 -0
- package/dist/charts/candlestick.js +105 -0
- package/dist/charts/combo.d.ts +8 -0
- package/dist/charts/combo.js +50 -0
- package/dist/charts/controller.d.ts +2 -2
- package/dist/charts/controller.js +2 -2
- package/dist/charts/defaults.d.ts +10 -2
- package/dist/charts/defaults.js +67 -6
- package/dist/charts/distribution.d.ts +15 -0
- package/dist/charts/distribution.js +178 -0
- package/dist/charts/fields.d.ts +33 -3
- package/dist/charts/fields.js +150 -66
- package/dist/charts/flow.d.ts +15 -0
- package/dist/charts/flow.js +189 -0
- package/dist/charts/heatmap.d.ts +9 -0
- package/dist/charts/heatmap.js +104 -0
- package/dist/charts/index.d.ts +8 -1
- package/dist/charts/index.js +7 -0
- package/dist/charts/parts.d.ts +23 -0
- package/dist/charts/parts.js +66 -0
- package/dist/charts/radar.d.ts +7 -0
- package/dist/charts/radar.js +77 -0
- package/dist/charts/types.d.ts +564 -23
- package/dist/charts/xy.d.ts +35 -25
- package/dist/charts/xy.js +327 -126
- package/dist/features/animate.d.ts +10 -1
- package/dist/features/animate.js +3 -3
- package/dist/features/brush.d.ts +3 -1
- package/dist/features/brush.js +2 -1
- package/dist/features/legend.d.ts +1 -3
- package/dist/features/legend.js +4 -5
- package/dist/features/tooltip.d.ts +10 -2
- package/dist/features/tooltip.js +20 -15
- package/dist/features/zoom.d.ts +1 -0
- package/dist/features/zoom.js +22 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/marks/arcs.d.ts +10 -4
- package/dist/marks/arcs.js +2 -1
- package/dist/marks/area.d.ts +9 -8
- package/dist/marks/area.js +2 -2
- package/dist/marks/bar.d.ts +18 -10
- package/dist/marks/bar.js +5 -5
- package/dist/marks/box.d.ts +7 -1
- package/dist/marks/box.js +2 -8
- package/dist/marks/candlestick.d.ts +22 -0
- package/dist/marks/candlestick.js +133 -0
- package/dist/marks/common.d.ts +18 -0
- package/dist/marks/common.js +14 -0
- package/dist/marks/dots.d.ts +6 -5
- package/dist/marks/dots.js +20 -5
- package/dist/marks/errorBars.d.ts +9 -4
- package/dist/marks/errorBars.js +21 -1
- package/dist/marks/funnel.d.ts +8 -3
- package/dist/marks/funnel.js +2 -1
- package/dist/marks/heatmap.d.ts +8 -7
- package/dist/marks/line.d.ts +8 -6
- package/dist/marks/radar.d.ts +9 -2
- package/dist/marks/radar.js +9 -2
- package/dist/marks/radialBars.d.ts +12 -5
- package/dist/marks/rects.js +15 -5
- package/dist/marks/rule.d.ts +19 -0
- package/dist/marks/rule.js +4 -1
- package/dist/marks/sankey.d.ts +14 -2
- package/dist/marks/sankey.js +4 -1
- package/dist/marks/sunburst.d.ts +5 -0
- package/dist/marks/sunburst.js +2 -1
- package/dist/marks/ticks.d.ts +4 -1
- package/dist/marks/tree.d.ts +14 -6
- package/dist/marks/tree.js +6 -3
- package/dist/marks/treemap.d.ts +5 -0
- package/dist/marks/treemap.js +7 -2
- package/dist/paint.d.ts +6 -1
- package/dist/paint.js +6 -1
- package/dist/scale.d.ts +1 -1
- package/dist/scale.js +4 -3
- package/dist/scales/band.d.ts +3 -0
- package/dist/scales/band.js +5 -1
- package/dist/scales/bare.d.ts +4 -1
- package/dist/scales/bare.js +4 -1
- package/dist/scales/color.d.ts +2 -0
- package/dist/scales/log.d.ts +8 -1
- package/dist/scales/log.js +3 -2
- package/dist/scales/time.d.ts +7 -0
- package/dist/scales/time.js +3 -1
- package/dist/text.d.ts +8 -0
- package/dist/text.js +33 -0
- package/dist/transforms.d.ts +11 -7
- package/dist/transforms.js +8 -7
- package/dist/types.d.ts +44 -15
- package/package.json +2 -2
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { glLayer } from '../gl.js';
|
|
2
|
+
import { base, ends } from './common.js';
|
|
3
|
+
import { focused, gpuRows } from './rects.js';
|
|
4
|
+
// Per candle: the body's left and right, the wick's top and bottom, the body's top and bottom.
|
|
5
|
+
const EDGES = 6;
|
|
6
|
+
export function candlestick(o) {
|
|
7
|
+
const { x, open, high, low, close, color, alpha = 1, width = 0.6 } = o;
|
|
8
|
+
const n = x.length;
|
|
9
|
+
const inks = [o.up ?? color, o.down ?? color];
|
|
10
|
+
const falls = (i) => close[i] < open[i];
|
|
11
|
+
const [first, last] = ends(x);
|
|
12
|
+
let sides;
|
|
13
|
+
// In device pixels; a row with a number missing has none.
|
|
14
|
+
const bounds = ({ ox, kx, oy, ky }) => {
|
|
15
|
+
const edges = new Float64Array(n * EDGES).fill(NaN);
|
|
16
|
+
const y = (v) => Math.round(oy - v * ky);
|
|
17
|
+
for (let i = 0; i < n; i++) {
|
|
18
|
+
if (![x, open, high, low, close].every(c => Number.isFinite(c[i])))
|
|
19
|
+
continue;
|
|
20
|
+
const mid = ox + x[i] * kx;
|
|
21
|
+
const l = Math.round(mid - (width * kx) / 2);
|
|
22
|
+
const top = y(Math.max(open[i], close[i]));
|
|
23
|
+
edges.set([
|
|
24
|
+
l,
|
|
25
|
+
Math.max(Math.round(mid + (width * kx) / 2), l + 1),
|
|
26
|
+
y(high[i]),
|
|
27
|
+
y(low[i]),
|
|
28
|
+
top,
|
|
29
|
+
Math.max(y(Math.min(open[i], close[i])), top + 1),
|
|
30
|
+
], i * EDGES);
|
|
31
|
+
}
|
|
32
|
+
return edges;
|
|
33
|
+
};
|
|
34
|
+
// The whole candle answers, wick included, and from 3 CSS pixels beside it: a candle is often a pixel or two wide.
|
|
35
|
+
const hit = ({ dpr, left, top, width: plotWidth, height }, bounded) => (px, py, reach) => {
|
|
36
|
+
const [hx, hy] = [px * dpr, py * dpr];
|
|
37
|
+
if (hx < left || hx > left + plotWidth || hy < top || hy > top + height)
|
|
38
|
+
return null;
|
|
39
|
+
const edges = bounded();
|
|
40
|
+
let [row, gap] = [-1, 3 * dpr];
|
|
41
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
42
|
+
const j = i * EDGES;
|
|
43
|
+
if (!(hy >= edges[j + 2] && hy < Math.max(edges[j + 3], edges[j + 2] + 1)))
|
|
44
|
+
continue;
|
|
45
|
+
const beside = Math.max(edges[j] - hx, hx - edges[j + 1], 0);
|
|
46
|
+
if (beside < gap || (beside === gap && row < 0))
|
|
47
|
+
[row, gap] = [i, beside];
|
|
48
|
+
}
|
|
49
|
+
return row < 0 ? null : { x: x[row], y: close[row], distance: reach, row, color: inks[+falls(row)] };
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
...base('candlestick', o, [x, [first - width / 2, last + width / 2]], [low, high]),
|
|
53
|
+
prepare(view) {
|
|
54
|
+
const { ctx, dpr } = view;
|
|
55
|
+
const edges = bounds(view);
|
|
56
|
+
const wick = Math.max(1, Math.round(dpr));
|
|
57
|
+
// The rising candles and the falling ones, each one filled shape.
|
|
58
|
+
const paths = (rows) => {
|
|
59
|
+
const made = [new Path2D(), new Path2D()];
|
|
60
|
+
for (let i = 0; i < n; i++) {
|
|
61
|
+
const j = i * EDGES;
|
|
62
|
+
const [l, r] = [edges[j], edges[j + 1]];
|
|
63
|
+
if (!rows(i) || Number.isNaN(l))
|
|
64
|
+
continue;
|
|
65
|
+
const path = made[+falls(i)];
|
|
66
|
+
path.rect(Math.round((l + r - wick) / 2), edges[j + 2], wick, Math.max(edges[j + 3] - edges[j + 2], 1));
|
|
67
|
+
path.rect(l, edges[j + 4], r - l, edges[j + 5] - edges[j + 4]);
|
|
68
|
+
}
|
|
69
|
+
return made;
|
|
70
|
+
};
|
|
71
|
+
const all = paths(() => true);
|
|
72
|
+
let split;
|
|
73
|
+
const draw = (made, k) => {
|
|
74
|
+
ctx.globalAlpha = alpha * k;
|
|
75
|
+
made.forEach((path, at) => {
|
|
76
|
+
ctx.fillStyle = inks[at];
|
|
77
|
+
ctx.fill(path);
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
paint(a, row, rest = 1) {
|
|
82
|
+
if (row === undefined || row >= n)
|
|
83
|
+
return draw(all, a);
|
|
84
|
+
if (split?.row !== row)
|
|
85
|
+
split = { row, one: paths(i => i === row), others: paths(i => i !== row) };
|
|
86
|
+
draw(split.others, a * rest);
|
|
87
|
+
draw(split.one, a);
|
|
88
|
+
},
|
|
89
|
+
hit: hit(view, () => edges),
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
gl: {
|
|
93
|
+
layer: glLayer,
|
|
94
|
+
under: true,
|
|
95
|
+
prepare(view, { gl }) {
|
|
96
|
+
// The painter takes one colour, so each direction is its own rows, with the other's left out as gaps.
|
|
97
|
+
sides ??= [false, true].map(fall => {
|
|
98
|
+
const opened = new Float64Array(n);
|
|
99
|
+
for (let i = 0; i < n; i++)
|
|
100
|
+
opened[i] = falls(i) === fall ? open[i] : NaN;
|
|
101
|
+
return gpuRows(new WeakMap(), [x], [high, low, close, opened]);
|
|
102
|
+
});
|
|
103
|
+
const pens = sides.map((rows, at) => rows.on(view, gl, inks[at]));
|
|
104
|
+
const wick = Math.max(1, Math.round(view.dpr));
|
|
105
|
+
// Columns: x, then high, low, close and open, which is last so that its gaps do not set the rows' offset.
|
|
106
|
+
// ponytail: a candle whose open equals its close shows only its wick here; give the body a pixel if it matters.
|
|
107
|
+
const parts = [
|
|
108
|
+
{ xa: 0, xb: 0, ya: 4, yb: 3, pad: [Math.max((width * view.kx) / 2, 0.5), 0], snap: true },
|
|
109
|
+
{ xa: 0, xb: 0, ya: 1, yb: 2, pad: [wick / 2, 0], shift: [(wick % 2) / 2, 0], snap: true },
|
|
110
|
+
];
|
|
111
|
+
let edges;
|
|
112
|
+
return {
|
|
113
|
+
paint(a, row, rest = 1) {
|
|
114
|
+
for (const { draw, once } of pens) {
|
|
115
|
+
once(() => {
|
|
116
|
+
for (const [strength, ...run] of focused(n, row, rest)) {
|
|
117
|
+
for (const part of parts)
|
|
118
|
+
draw(part, alpha * a * strength, ...run);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
hit: hit(view, () => (edges ??= bounds(view))),
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
release(layer) {
|
|
128
|
+
if (layer)
|
|
129
|
+
for (const rows of sides ?? [])
|
|
130
|
+
rows.release(layer.gl);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
package/dist/marks/common.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import type { Col } from '../types.js';
|
|
2
2
|
export type Common = {
|
|
3
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
3
4
|
key?: string;
|
|
5
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
4
6
|
label?: string;
|
|
7
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
5
8
|
group?: string;
|
|
9
|
+
/** A CSS colour. */
|
|
6
10
|
color: string;
|
|
11
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
7
12
|
alpha?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Draws the mark against the chart's second y axis, `y2`, which then fits it; 'y' when left out.
|
|
15
|
+
* A stack holds the marks of one `yAxis`, also on a chart with no `y2`, where all are drawn on `y`.
|
|
16
|
+
*/
|
|
17
|
+
yAxis?: 'y' | 'y2';
|
|
8
18
|
};
|
|
9
19
|
export declare const base: (type: string, o: Common, x: Col[], y: Col[]) => {
|
|
10
20
|
type: string;
|
|
@@ -13,12 +23,20 @@ export declare const base: (type: string, o: Common, x: Col[], y: Col[]) => {
|
|
|
13
23
|
group: string | undefined;
|
|
14
24
|
color: string;
|
|
15
25
|
options: Common;
|
|
26
|
+
yAxis: "y" | "y2" | undefined;
|
|
16
27
|
points: number;
|
|
17
28
|
channels: {
|
|
18
29
|
x: Col[];
|
|
19
30
|
y: Col[];
|
|
20
31
|
};
|
|
21
32
|
};
|
|
33
|
+
/** A stack's key: one id on the two y axes is two stacks, as values on two scales do not add up. */
|
|
34
|
+
export declare const stackKey: (type: string, o: {
|
|
35
|
+
yAxis?: 'y' | 'y2';
|
|
36
|
+
stack?: string;
|
|
37
|
+
}) => string;
|
|
38
|
+
/** The least and the greatest finite x, or Infinity and -Infinity when there is none. */
|
|
39
|
+
export declare function ends(x: Col): [number, number];
|
|
22
40
|
/** 'smooth' curves through the points where they are further apart than a pixel, and is 'linear' where they are not. */
|
|
23
41
|
export type Curve = 'linear' | 'step' | 'smooth';
|
|
24
42
|
/**
|
package/dist/marks/common.js
CHANGED
|
@@ -5,9 +5,23 @@ export const base = (type, o, x, y) => ({
|
|
|
5
5
|
group: o.group ?? o.label,
|
|
6
6
|
color: o.color,
|
|
7
7
|
options: o,
|
|
8
|
+
yAxis: o.yAxis,
|
|
8
9
|
points: x[0]?.length ?? 0,
|
|
9
10
|
channels: { x, y },
|
|
10
11
|
});
|
|
12
|
+
/** A stack's key: one id on the two y axes is two stacks, as values on two scales do not add up. */
|
|
13
|
+
export const stackKey = (type, o) => `${type}:stack:${o.yAxis ?? 'y'}:${o.stack}`;
|
|
14
|
+
/** The least and the greatest finite x, or Infinity and -Infinity when there is none. */
|
|
15
|
+
export function ends(x) {
|
|
16
|
+
let [first, last] = [Infinity, -Infinity];
|
|
17
|
+
for (let i = 0; i < x.length; i++) {
|
|
18
|
+
if (x[i] < first && x[i] !== -Infinity)
|
|
19
|
+
first = x[i];
|
|
20
|
+
if (x[i] > last && x[i] !== Infinity)
|
|
21
|
+
last = x[i];
|
|
22
|
+
}
|
|
23
|
+
return [first, last];
|
|
24
|
+
}
|
|
11
25
|
/**
|
|
12
26
|
* A curve on from the path's current point through n points, forwards or backwards, that never
|
|
13
27
|
* goes above or below the points it joins: its lean at each is the harmonic mean of the leans
|
package/dist/marks/dots.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import type { Col, Mark } from '../types.js';
|
|
2
2
|
import type { Common } from './common.js';
|
|
3
|
-
/**
|
|
4
|
-
* r is the radius of each point in CSS pixels. With `ring`, a width in CSS pixels, a point is a
|
|
5
|
-
* ring of its colour that wide round a middle of `fill`, the theme's surface by default; such points are
|
|
6
|
-
* always drawn on the canvas.
|
|
7
|
-
*/
|
|
3
|
+
/** A dot at each point, each with its own radius. */
|
|
8
4
|
export type DotsOptions = Common & {
|
|
5
|
+
/** The x of each point. */
|
|
9
6
|
x: Col;
|
|
7
|
+
/** The y of each point. */
|
|
10
8
|
y: Col;
|
|
9
|
+
/** The radius of each point in CSS pixels. */
|
|
11
10
|
r: Col;
|
|
11
|
+
/** Draws each point as a ring of its colour this many CSS pixels wide, always on the canvas. */
|
|
12
12
|
ring?: number;
|
|
13
|
+
/** The middle of a ring point, a CSS colour; the theme's surface when left out. */
|
|
13
14
|
fill?: string;
|
|
14
15
|
};
|
|
15
16
|
export declare function dots(o: DotsOptions): Mark;
|
package/dist/marks/dots.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { glLayer, program, rgba } from '../gl.js';
|
|
2
2
|
import { base } from './common.js';
|
|
3
3
|
const TAU = 2 * Math.PI;
|
|
4
|
+
// Measured 2026-11-24: below this the canvas mounts sooner than WebGL can be set up, also at 4x CPU slowdown.
|
|
5
|
+
const GL_FROM = 20_000;
|
|
4
6
|
const origin = (c) => {
|
|
5
7
|
for (let i = 0; i < c.length; i++)
|
|
6
8
|
if (Number.isFinite(c[i]))
|
|
@@ -38,7 +40,8 @@ export function dots(o) {
|
|
|
38
40
|
}
|
|
39
41
|
return at < 0 ? null : { x: x[at], y: y[at], distance: Math.sqrt(best), row: at };
|
|
40
42
|
};
|
|
41
|
-
const prepareGl = (view,
|
|
43
|
+
const prepareGl = (view, layer) => {
|
|
44
|
+
const { gl } = layer;
|
|
42
45
|
const u = programs.get(gl) ?? pointProgram(gl);
|
|
43
46
|
programs.set(gl, u);
|
|
44
47
|
// The offsets are subtracted in float64: a float32 attribute cannot hold e.g. millisecond timestamps.
|
|
@@ -74,10 +77,16 @@ export function dots(o) {
|
|
|
74
77
|
};
|
|
75
78
|
return {
|
|
76
79
|
// The alpha argument is unused: dense points saturate, so the layer's fade() dims a group instead.
|
|
77
|
-
paint() {
|
|
80
|
+
paint(_, row, rest = 1) {
|
|
78
81
|
ready();
|
|
79
82
|
own();
|
|
80
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);
|
|
81
90
|
},
|
|
82
91
|
select(rows, outline) {
|
|
83
92
|
ready();
|
|
@@ -96,7 +105,6 @@ export function dots(o) {
|
|
|
96
105
|
return {
|
|
97
106
|
...base('dots', o, [x], [y]),
|
|
98
107
|
overhang,
|
|
99
|
-
rowsAlike: true,
|
|
100
108
|
prepare(view) {
|
|
101
109
|
const { ctx, dpr, ox, kx, oy, ky, theme, left, top, width, height } = view;
|
|
102
110
|
// A point further out than twice the overhang cannot reach the area its mark is cut to.
|
|
@@ -124,7 +132,14 @@ export function dots(o) {
|
|
|
124
132
|
ctx.stroke(shape);
|
|
125
133
|
};
|
|
126
134
|
return {
|
|
127
|
-
paint
|
|
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
|
+
},
|
|
128
143
|
select(rows, outline) {
|
|
129
144
|
const some = new Path2D();
|
|
130
145
|
const edge = new Path2D();
|
|
@@ -144,7 +159,7 @@ export function dots(o) {
|
|
|
144
159
|
hit: hit(view),
|
|
145
160
|
};
|
|
146
161
|
},
|
|
147
|
-
gl: o.ring ? undefined : { layer: glLayer, prepare: prepareGl },
|
|
162
|
+
gl: o.ring ? undefined : { layer: glLayer, from: GL_FROM, prepare: prepareGl },
|
|
148
163
|
release(layer) {
|
|
149
164
|
const buffer = layer && buffers.get(layer.gl);
|
|
150
165
|
if (!buffer)
|
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
import type { Col, Mark } from '../types.js';
|
|
2
2
|
import type { Common } from './common.js';
|
|
3
|
-
/**
|
|
4
|
-
* A line from y1 to y2 at each x with a cap at both ends, or from x1 to x2 at each y.
|
|
5
|
-
* `cap` is half the cap's length and `width` the line width, both in CSS pixels.
|
|
6
|
-
*/
|
|
3
|
+
/** A line from y1 to y2 at each x with a cap at both ends, or from x1 to x2 at each y. */
|
|
7
4
|
export type ErrorBarsOptions = Common & {
|
|
5
|
+
/** The line's width in CSS pixels. @default 1 */
|
|
8
6
|
width?: number;
|
|
7
|
+
/** Half the length of each cap, in CSS pixels. @default 4 */
|
|
9
8
|
cap?: number;
|
|
10
9
|
} & ({
|
|
10
|
+
/** Where each upright bar stands. */
|
|
11
11
|
x: Col;
|
|
12
|
+
/** One end of each upright bar. */
|
|
12
13
|
y1: Col;
|
|
14
|
+
/** The other end of each upright bar. */
|
|
13
15
|
y2: Col;
|
|
14
16
|
} | {
|
|
17
|
+
/** Where each level bar stands. */
|
|
15
18
|
y: Col;
|
|
19
|
+
/** One end of each level bar. */
|
|
16
20
|
x1: Col;
|
|
21
|
+
/** The other end of each level bar. */
|
|
17
22
|
x2: Col;
|
|
18
23
|
});
|
|
19
24
|
export declare function errorBars(o: ErrorBarsOptions): Mark;
|
package/dist/marks/errorBars.js
CHANGED
|
@@ -7,8 +7,28 @@ export function errorBars(o) {
|
|
|
7
7
|
// `at` is the position the bar stands on; `from` and `to` are its ends along the other axis.
|
|
8
8
|
const [at, from, to] = upright ? [o.x, o.y1, o.y2] : [o.y, o.x1, o.x2];
|
|
9
9
|
const rows = upright ? gpuRows(new WeakMap(), [at], [from, to]) : gpuRows(new WeakMap(), [from, to], [at]);
|
|
10
|
+
const isWhole = (i) => Number.isFinite(at[i]) && Number.isFinite(from[i]) && Number.isFinite(to[i]);
|
|
11
|
+
let read;
|
|
12
|
+
const wholeRows = () => {
|
|
13
|
+
if (read)
|
|
14
|
+
return read;
|
|
15
|
+
const copy = (c) => Float64Array.from(at, (_, i) => (isWhole(i) ? c[i] : Number.NaN));
|
|
16
|
+
let partial = false;
|
|
17
|
+
for (let i = 0; i < at.length && !partial; i++)
|
|
18
|
+
partial = !isWhole(i) && (Number.isFinite(at[i]) || Number.isFinite(from[i]) || Number.isFinite(to[i]));
|
|
19
|
+
const [places, low, high] = partial ? [copy(at), copy(from), copy(to)] : [at, from, to];
|
|
20
|
+
return (read = { places: [places], ends: [low, high] });
|
|
21
|
+
};
|
|
10
22
|
return {
|
|
11
23
|
...base('errorBars', o, upright ? [at] : [from, to], upright ? [from, to] : [at]),
|
|
24
|
+
channels: {
|
|
25
|
+
get x() {
|
|
26
|
+
return upright ? wholeRows().places : wholeRows().ends;
|
|
27
|
+
},
|
|
28
|
+
get y() {
|
|
29
|
+
return upright ? wholeRows().ends : wholeRows().places;
|
|
30
|
+
},
|
|
31
|
+
},
|
|
12
32
|
oneShape: true,
|
|
13
33
|
overhang: Math.max(cap, width / 2),
|
|
14
34
|
prepare({ ctx, dpr, ox, kx, oy, ky }) {
|
|
@@ -16,7 +36,7 @@ export function errorBars(o) {
|
|
|
16
36
|
const path = new Path2D();
|
|
17
37
|
for (let i = 0; i < at.length; i++) {
|
|
18
38
|
// Canvas ignores a moveTo with a NaN in it, and the next lineTo would then join two rows.
|
|
19
|
-
if (!
|
|
39
|
+
if (!isWhole(i))
|
|
20
40
|
continue;
|
|
21
41
|
// A stem between the two ends, and a cap across each end.
|
|
22
42
|
if (upright) {
|
package/dist/marks/funnel.d.ts
CHANGED
|
@@ -2,13 +2,14 @@ import { type Gradient } from '../paint.js';
|
|
|
2
2
|
import type { Col, Mark } from '../types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Stages of a funnel from top to bottom, for a blank frame. All stages are equally tall and each
|
|
5
|
-
* is as wide as its value against the largest; it narrows to the width of the stage below.
|
|
6
|
-
* value that is negative or not a number counts as 0. Colours repeat when there are fewer than
|
|
7
|
-
* stages. `labels` name the stages for a legend.
|
|
5
|
+
* is as wide as its value against the largest; it narrows to the width of the stage below.
|
|
8
6
|
*/
|
|
9
7
|
export type FunnelOptions = {
|
|
8
|
+
/** Each stage's value; one that is negative or not a number counts as 0. */
|
|
10
9
|
values: Col;
|
|
10
|
+
/** The stages' colours, repeated when there are fewer than stages. */
|
|
11
11
|
colors: string[];
|
|
12
|
+
/** The stages' names, for a legend, a tooltip and `text`. */
|
|
12
13
|
labels?: string[];
|
|
13
14
|
/**
|
|
14
15
|
* Text on each stage: true for its name and its share of the first stage, or your own. It is
|
|
@@ -17,9 +18,13 @@ export type FunnelOptions = {
|
|
|
17
18
|
text?: true | ((row: number, share: number) => string);
|
|
18
19
|
/** One blend over the whole funnel in place of a colour per stage; `colors` still name the stages in a legend. */
|
|
19
20
|
fill?: Gradient;
|
|
21
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
20
22
|
alpha?: number;
|
|
23
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
21
24
|
key?: string;
|
|
25
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
22
26
|
label?: string;
|
|
27
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
23
28
|
group?: string;
|
|
24
29
|
};
|
|
25
30
|
export declare function funnel(o: FunnelOptions): Mark;
|
package/dist/marks/funnel.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { painter } from '../paint.js';
|
|
2
|
+
import { textWidth } from '../text.js';
|
|
2
3
|
export function funnel(o) {
|
|
3
4
|
const { values, colors, alpha = 1 } = o;
|
|
4
5
|
const n = values.length;
|
|
@@ -88,7 +89,7 @@ export function funnel(o) {
|
|
|
88
89
|
// The stage's half-width at the top and at the bottom of the text. Inside where even the
|
|
89
90
|
// narrower leaves room; otherwise to the right of the wider, clear of the slanted side.
|
|
90
91
|
const [above, below] = [reach(i, y - 8 * dpr), reach(i, y + 8 * dpr)];
|
|
91
|
-
if (text
|
|
92
|
+
if (textWidth(text) * dpr <= 2 * Math.min(above, below) * 0.9) {
|
|
92
93
|
labels.push({ x: cx, y, text, color: '#fff', priority: n - i });
|
|
93
94
|
}
|
|
94
95
|
else {
|
package/dist/marks/heatmap.d.ts
CHANGED
|
@@ -2,18 +2,19 @@ import type { ColorScale } from '../scales/color.js';
|
|
|
2
2
|
import type { Col, Mark } from '../types.js';
|
|
3
3
|
import type { Common } from './common.js';
|
|
4
4
|
/**
|
|
5
|
-
* A grid of cells coloured by value.
|
|
6
|
-
*
|
|
7
|
-
* x = c and y = r, which is where a band axis puts its categories; `x` and `y` give the grid's
|
|
8
|
-
* edges instead, from the first cell's to the last one's, in either order. A cell whose value is
|
|
9
|
-
* not finite is left empty. Where several cells share a pixel, the pixel shows and answers for
|
|
10
|
-
* the largest of them, so a peak is never lost.
|
|
5
|
+
* A grid of cells coloured by value. A cell whose value is not finite is left empty. Where several
|
|
6
|
+
* cells share a pixel, the pixel shows and answers for the largest of them, so a peak is never lost.
|
|
11
7
|
*/
|
|
12
|
-
export type HeatmapOptions = Omit<Common, 'color'> & {
|
|
8
|
+
export type HeatmapOptions = Omit<Common, 'color' | 'yAxis'> & {
|
|
9
|
+
/** The cells along x first: column c of row r is values[r * columns + c], and row 0 lies at the first y edge. */
|
|
13
10
|
values: Col;
|
|
11
|
+
/** How many cells a row holds, rounded down and at least 1. */
|
|
14
12
|
columns: number;
|
|
13
|
+
/** The grid's x edges, in either order; left out, column c is centred on x = c, as a band axis places it. */
|
|
15
14
|
x?: [number, number];
|
|
15
|
+
/** The grid's y edges, in either order; left out, row r is centred on y = r, as a band axis places it. */
|
|
16
16
|
y?: [number, number];
|
|
17
|
+
/** The colours the values run through. @default sequential() */
|
|
17
18
|
color?: ColorScale;
|
|
18
19
|
};
|
|
19
20
|
export declare function heatmap(o: HeatmapOptions): Mark;
|
package/dist/marks/line.d.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import type { Col, Mark } from '../types.js';
|
|
2
2
|
import { type Paint } from '../paint.js';
|
|
3
3
|
import type { Common, Curve } from './common.js';
|
|
4
|
-
/**
|
|
5
|
-
* x must be ascending. `stroke` draws the line with something other than its colour: a blend
|
|
6
|
-
* across the plot area, say. `fill` shades what is under the line, down to the bottom of the plot
|
|
7
|
-
* area: `true` for a faint fade of the line's colour, or a paint of your own. `join` draws it straight
|
|
8
|
-
* across a value that is not finite, where it would otherwise leave a gap.
|
|
9
|
-
*/
|
|
4
|
+
/** A line through points in ascending x; a y that is not finite leaves a gap unless `join` is set. */
|
|
10
5
|
export type LineOptions = Common & {
|
|
6
|
+
/** The x of each point, ascending. */
|
|
11
7
|
x: Col;
|
|
8
|
+
/** The y of each point; one that is not finite leaves a gap. */
|
|
12
9
|
y: Col;
|
|
10
|
+
/** How the line runs from point to point: straight, in steps, or smoothly. @default 'linear' */
|
|
13
11
|
curve?: Curve;
|
|
12
|
+
/** The line's width in CSS pixels. @default 1.5 */
|
|
14
13
|
width?: number;
|
|
14
|
+
/** Draws the line with something other than its colour: a blend across the plot area, say. */
|
|
15
15
|
stroke?: Paint;
|
|
16
|
+
/** Shades down to the bottom of the plot: `true` for a faint fade of the line's colour, or a paint. */
|
|
16
17
|
fill?: Paint | boolean;
|
|
18
|
+
/** Draws the line straight across a value that is not finite, where it would leave a gap. */
|
|
17
19
|
join?: boolean;
|
|
18
20
|
};
|
|
19
21
|
export declare function line(o: LineOptions): Mark;
|
package/dist/marks/radar.d.ts
CHANGED
|
@@ -3,18 +3,25 @@ import type { Col, Mark } from '../types.js';
|
|
|
3
3
|
* A radar chart for a polar frame: `marks: radar({ axes, series })`. It gives one mark for the
|
|
4
4
|
* rings, the spokes and their names, then one per series, so series are named, dimmed and hit
|
|
5
5
|
* like any other marks, and a hit's row is the spoke. Spokes start at twelve o'clock and run
|
|
6
|
-
* clockwise.
|
|
7
|
-
* value that is negative or not a number sits at the centre, and one above `max` on the rim.
|
|
6
|
+
* clockwise. A value that is negative or not a number sits at the centre.
|
|
8
7
|
*/
|
|
9
8
|
export type RadarOptions = {
|
|
9
|
+
/** The spokes' names; a series has a value for each, in this order. */
|
|
10
10
|
axes: string[];
|
|
11
|
+
/** The series, each drawn as a mark of its own with its values, colour, label, group and key. */
|
|
11
12
|
series: {
|
|
13
|
+
/** The series' value on each spoke. */
|
|
12
14
|
values: Col;
|
|
15
|
+
/** A CSS colour. */
|
|
13
16
|
color: string;
|
|
17
|
+
/** The series' name in a legend, a tooltip and the chart's description. */
|
|
14
18
|
label?: string;
|
|
19
|
+
/** Series of one group are focused, dimmed and hidden together; the label when left out. */
|
|
15
20
|
group?: string;
|
|
21
|
+
/** Pairs the series with the one it replaces in update(), to keep or morph its shape. */
|
|
16
22
|
key?: string;
|
|
17
23
|
}[];
|
|
24
|
+
/** What the rim stands for, capping larger values; the largest rounded up to a whole step when left out. */
|
|
18
25
|
max?: number;
|
|
19
26
|
/** The opacity of a series' filling; 0.15 by default. */
|
|
20
27
|
fill?: number;
|
package/dist/marks/radar.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { scale } from '../scale.js';
|
|
2
|
+
import { textWidth } from '../text.js';
|
|
2
3
|
import { TURN } from './common.js';
|
|
3
4
|
const worth = (v) => (v > 0 && Number.isFinite(v) ? v : 0);
|
|
4
5
|
export function radar(o) {
|
|
@@ -21,7 +22,7 @@ export function radar(o) {
|
|
|
21
22
|
for (let i = 0; i < n; i++) {
|
|
22
23
|
const [c, s] = [Math.abs(toward(i)[0]), Math.abs(toward(i)[1])];
|
|
23
24
|
if (c >= 0.01)
|
|
24
|
-
r = Math.min(r, (width / 2 - (axes[i]
|
|
25
|
+
r = Math.min(r, (width / 2 - (textWidth(axes[i]) + 4) * dpr) / c - 8 * dpr);
|
|
25
26
|
if (s >= 0.01)
|
|
26
27
|
r = Math.min(r, (height / 2 - 10 * dpr) / s - 12 * dpr);
|
|
27
28
|
}
|
|
@@ -146,7 +147,13 @@ export function radar(o) {
|
|
|
146
147
|
if (row < 0)
|
|
147
148
|
return null;
|
|
148
149
|
const [x, y] = at(row);
|
|
149
|
-
return {
|
|
150
|
+
return {
|
|
151
|
+
x: (x - left) / width,
|
|
152
|
+
y: 1 - (y - top) / height,
|
|
153
|
+
distance: near,
|
|
154
|
+
row,
|
|
155
|
+
value: s.values[row] ?? NaN,
|
|
156
|
+
};
|
|
150
157
|
},
|
|
151
158
|
};
|
|
152
159
|
},
|
|
@@ -1,27 +1,34 @@
|
|
|
1
1
|
import type { Col, Mark } from '../types.js';
|
|
2
2
|
/**
|
|
3
|
-
* One bar per value, bent round the centre on its own ring, for a polar frame.
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* kept for the names. A value that is negative or not a number has no bar, and one above `max`
|
|
7
|
-
* stops at the three quarters. `inner` is the share of the radius left empty in the middle.
|
|
3
|
+
* One bar per value, bent round the centre on its own ring, for a polar frame. A bar starts at
|
|
4
|
+
* twelve o'clock and runs clockwise; three quarters of a turn stand for `max`, and the last
|
|
5
|
+
* quarter is kept for the names.
|
|
8
6
|
*/
|
|
9
7
|
export type RadialBarsOptions = {
|
|
8
|
+
/** Each bar's value, the first outermost; one that is negative or not a number has no bar. */
|
|
10
9
|
values: Col;
|
|
10
|
+
/** The bars' colours, repeated when there are fewer than bars. */
|
|
11
11
|
colors: string[];
|
|
12
12
|
/** Names for a legend, and written at the start of each ring unless `text` is false. */
|
|
13
13
|
labels?: string[];
|
|
14
|
+
/** Whether the names are written at the start of each ring. @default true */
|
|
14
15
|
text?: boolean;
|
|
16
|
+
/** What three quarters of a turn stand for, capping larger values; the largest rounded up when left out. */
|
|
15
17
|
max?: number;
|
|
16
18
|
/**
|
|
17
19
|
* Each bar starts at this opacity and reaches its full colour at its end. Where the browser
|
|
18
20
|
* has no conic gradient the bars stay solid.
|
|
19
21
|
*/
|
|
20
22
|
sweep?: number;
|
|
23
|
+
/** The share of the radius left empty in the middle. @default 0.3 */
|
|
21
24
|
inner?: number;
|
|
25
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
22
26
|
alpha?: number;
|
|
27
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
23
28
|
key?: string;
|
|
29
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
24
30
|
label?: string;
|
|
31
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
25
32
|
group?: string;
|
|
26
33
|
};
|
|
27
34
|
export declare function radialBars(o: RadialBarsOptions): Mark;
|
package/dist/marks/rects.js
CHANGED
|
@@ -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
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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/marks/rule.d.ts
CHANGED
|
@@ -1,28 +1,47 @@
|
|
|
1
1
|
import type { Col, Mark } from '../types.js';
|
|
2
2
|
/** A line across the whole plot at each x or each y value, over the data and never a series. */
|
|
3
3
|
export type RuleOptions = {
|
|
4
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
4
5
|
key?: string;
|
|
6
|
+
/** A CSS colour. */
|
|
5
7
|
color: string;
|
|
8
|
+
/** The line's width in CSS pixels. @default 1 */
|
|
6
9
|
width?: number;
|
|
10
|
+
/** Draws the line dashed. @default false */
|
|
7
11
|
dashed?: boolean;
|
|
12
|
+
/** Written beside each line, where it fits inside the plot. */
|
|
8
13
|
text?: string;
|
|
9
14
|
} & ({
|
|
15
|
+
/** The x of the line, or a column of them for one line each. */
|
|
10
16
|
x: number | Col;
|
|
11
17
|
} | {
|
|
18
|
+
/** The y of the line, or a column of them for one line each. */
|
|
12
19
|
y: number | Col;
|
|
20
|
+
/** Places the line on the chart's second y axis with 'y2'. @default 'y' */
|
|
21
|
+
yAxis?: 'y' | 'y2';
|
|
13
22
|
});
|
|
14
23
|
export declare function rule(o: RuleOptions): Mark;
|
|
15
24
|
/** A band across the whole plot between two x or two y values, under the data and never a series. */
|
|
16
25
|
export type ZoneOptions = {
|
|
26
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
17
27
|
key?: string;
|
|
28
|
+
/** A CSS colour. */
|
|
18
29
|
color: string;
|
|
30
|
+
/** The band's opacity, from 0 to 1. @default 0.15 */
|
|
19
31
|
alpha?: number;
|
|
32
|
+
/** Written in the band's top left corner, where it fits inside the plot. */
|
|
20
33
|
text?: string;
|
|
21
34
|
} & ({
|
|
35
|
+
/** One edge of the band along x. */
|
|
22
36
|
x1: number;
|
|
37
|
+
/** The band's other edge along x. */
|
|
23
38
|
x2: number;
|
|
24
39
|
} | {
|
|
40
|
+
/** One edge of the band along y. */
|
|
25
41
|
y1: number;
|
|
42
|
+
/** The band's other edge along y. */
|
|
26
43
|
y2: number;
|
|
44
|
+
/** Places the band on the chart's second y axis with 'y2'. @default 'y' */
|
|
45
|
+
yAxis?: 'y' | 'y2';
|
|
27
46
|
});
|
|
28
47
|
export declare function zone(o: ZoneOptions): Mark;
|