swiftplot 0.0.1 → 0.3.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/LICENSE +21 -0
- package/README.md +348 -3
- package/dist/chart.d.ts +7 -0
- package/dist/chart.js +1311 -0
- package/dist/charts/build.d.ts +52 -0
- package/dist/charts/build.js +63 -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 +32 -0
- package/dist/charts/controller.js +231 -0
- package/dist/charts/defaults.d.ts +20 -0
- package/dist/charts/defaults.js +121 -0
- package/dist/charts/distribution.d.ts +15 -0
- package/dist/charts/distribution.js +178 -0
- package/dist/charts/fields.d.ts +64 -0
- package/dist/charts/fields.js +251 -0
- 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 +13 -0
- package/dist/charts/index.js +10 -0
- package/dist/charts/mount.d.ts +5 -0
- package/dist/charts/mount.js +21 -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/source.d.ts +24 -0
- package/dist/charts/source.js +225 -0
- package/dist/charts/types.d.ts +643 -0
- package/dist/charts/types.js +1 -0
- package/dist/charts/xy.d.ts +46 -0
- package/dist/charts/xy.js +421 -0
- package/dist/features/animate.d.ts +20 -0
- package/dist/features/animate.js +118 -0
- package/dist/features/brush.d.ts +15 -0
- package/dist/features/brush.js +128 -0
- package/dist/features/legend.d.ts +13 -0
- package/dist/features/legend.js +236 -0
- package/dist/features/pointer.d.ts +29 -0
- package/dist/features/pointer.js +23 -0
- package/dist/features/tooltip.d.ts +22 -0
- package/dist/features/tooltip.js +192 -0
- package/dist/features/zoom.d.ts +14 -0
- package/dist/features/zoom.js +246 -0
- package/dist/gl.d.ts +5 -0
- package/dist/gl.js +52 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +32 -0
- package/dist/marks/arcs.d.ts +31 -0
- package/dist/marks/arcs.js +125 -0
- package/dist/marks/area.d.ts +25 -0
- package/dist/marks/area.js +106 -0
- package/dist/marks/bar.d.ts +52 -0
- package/dist/marks/bar.js +243 -0
- package/dist/marks/box.d.ts +33 -0
- package/dist/marks/box.js +182 -0
- package/dist/marks/candlestick.d.ts +22 -0
- package/dist/marks/candlestick.js +133 -0
- package/dist/marks/common.d.ts +102 -0
- package/dist/marks/common.js +250 -0
- package/dist/marks/dots.d.ts +16 -0
- package/dist/marks/dots.js +158 -0
- package/dist/marks/errorBars.d.ts +24 -0
- package/dist/marks/errorBars.js +99 -0
- package/dist/marks/funnel.d.ts +30 -0
- package/dist/marks/funnel.js +122 -0
- package/dist/marks/heatmap.d.ts +20 -0
- package/dist/marks/heatmap.js +139 -0
- package/dist/marks/line.d.ts +21 -0
- package/dist/marks/line.js +75 -0
- package/dist/marks/radar.d.ts +29 -0
- package/dist/marks/radar.js +162 -0
- package/dist/marks/radialBars.d.ts +34 -0
- package/dist/marks/radialBars.js +111 -0
- package/dist/marks/rects.d.ts +43 -0
- package/dist/marks/rects.js +120 -0
- package/dist/marks/rule.d.ts +47 -0
- package/dist/marks/rule.js +83 -0
- package/dist/marks/sankey.d.ts +53 -0
- package/dist/marks/sankey.js +369 -0
- package/dist/marks/sunburst.d.ts +31 -0
- package/dist/marks/sunburst.js +186 -0
- package/dist/marks/ticks.d.ts +12 -0
- package/dist/marks/ticks.js +26 -0
- package/dist/marks/tree.d.ts +49 -0
- package/dist/marks/tree.js +113 -0
- package/dist/marks/treemap.d.ts +30 -0
- package/dist/marks/treemap.js +164 -0
- package/dist/paint.d.ts +35 -0
- package/dist/paint.js +71 -0
- package/dist/scale.d.ts +16 -0
- package/dist/scale.js +97 -0
- package/dist/scales/band.d.ts +12 -0
- package/dist/scales/band.js +27 -0
- package/dist/scales/bare.d.ts +6 -0
- package/dist/scales/bare.js +10 -0
- package/dist/scales/color.d.ts +20 -0
- package/dist/scales/color.js +21 -0
- package/dist/scales/log.d.ts +27 -0
- package/dist/scales/log.js +98 -0
- package/dist/scales/time.d.ts +37 -0
- package/dist/scales/time.js +183 -0
- package/dist/text.d.ts +8 -0
- package/dist/text.js +33 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +8 -0
- package/dist/transforms.d.ts +47 -0
- package/dist/transforms.js +119 -0
- package/dist/types.d.ts +465 -0
- package/dist/types.js +1 -0
- package/package.json +31 -6
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { scale } from '../scale.js';
|
|
2
|
+
import { textWidth } from '../text.js';
|
|
3
|
+
import { TURN } from './common.js';
|
|
4
|
+
const worth = (v) => (v > 0 && Number.isFinite(v) ? v : 0);
|
|
5
|
+
export function radar(o) {
|
|
6
|
+
const { axes, series, fill = 0.15 } = o;
|
|
7
|
+
const n = axes.length;
|
|
8
|
+
let most = 0;
|
|
9
|
+
for (const s of series)
|
|
10
|
+
for (let i = 0; i < n; i++)
|
|
11
|
+
most = Math.max(most, worth(s.values[i]));
|
|
12
|
+
const hi = worth(o.max) || scale('nice', [0, most || 1]).hi;
|
|
13
|
+
const toward = (i) => {
|
|
14
|
+
const angle = (i / (n || 1)) * TURN - TURN / 4;
|
|
15
|
+
return [Math.cos(angle), Math.sin(angle)];
|
|
16
|
+
};
|
|
17
|
+
// The rim is as far out as leaves every spoke's name inside the chart, 8 px beyond the rim
|
|
18
|
+
// sideways and 12 px up or down. It keeps half the room whatever the names; one that then
|
|
19
|
+
// does not fit is left out.
|
|
20
|
+
const disc = ({ dpr, left, top, width, height }) => {
|
|
21
|
+
let r = Math.min(width, height) / 2;
|
|
22
|
+
for (let i = 0; i < n; i++) {
|
|
23
|
+
const [c, s] = [Math.abs(toward(i)[0]), Math.abs(toward(i)[1])];
|
|
24
|
+
if (c >= 0.01)
|
|
25
|
+
r = Math.min(r, (width / 2 - (textWidth(axes[i]) + 4) * dpr) / c - 8 * dpr);
|
|
26
|
+
if (s >= 0.01)
|
|
27
|
+
r = Math.min(r, (height / 2 - 10 * dpr) / s - 12 * dpr);
|
|
28
|
+
}
|
|
29
|
+
r = Math.max(r, Math.min(width, height) / 4, 0);
|
|
30
|
+
return { cx: left + width / 2, cy: top + height / 2, r };
|
|
31
|
+
};
|
|
32
|
+
// With more spokes than the rim has device pixels, one is drawn for each pixel: the longest of
|
|
33
|
+
// those it stands for. More shapes than pixels cost time and show nothing.
|
|
34
|
+
const drawn = (r, far) => {
|
|
35
|
+
const room = Math.max(1, Math.ceil(TURN * r));
|
|
36
|
+
if (n <= room)
|
|
37
|
+
return Array.from({ length: n }, (_, i) => i);
|
|
38
|
+
const best = new Int32Array(room).fill(-1);
|
|
39
|
+
for (let i = 0; i < n; i++) {
|
|
40
|
+
const at = Math.floor((i * room) / n);
|
|
41
|
+
if (best[at] < 0 || (far && far(i) > far(best[at])))
|
|
42
|
+
best[at] = i;
|
|
43
|
+
}
|
|
44
|
+
return [...best].filter(i => i >= 0);
|
|
45
|
+
};
|
|
46
|
+
const ring = (cx, cy, far, spokes) => {
|
|
47
|
+
const path = new Path2D();
|
|
48
|
+
for (const i of spokes)
|
|
49
|
+
path.lineTo(cx + toward(i)[0] * far(i), cy + toward(i)[1] * far(i));
|
|
50
|
+
path.closePath();
|
|
51
|
+
return path;
|
|
52
|
+
};
|
|
53
|
+
const grid = {
|
|
54
|
+
type: 'radar-grid',
|
|
55
|
+
color: '#888',
|
|
56
|
+
options: { axes, hi },
|
|
57
|
+
points: 0,
|
|
58
|
+
channels: { x: [], y: [] },
|
|
59
|
+
backdrop: true,
|
|
60
|
+
prepare(view) {
|
|
61
|
+
const { ctx, dpr } = view;
|
|
62
|
+
const { cx, cy, r } = disc(view);
|
|
63
|
+
const { ticks } = scale([0, hi], [0, hi], { length: r / dpr, horizontal: false });
|
|
64
|
+
// The rim is drawn and given its value even where it has been thinned out of the ticks.
|
|
65
|
+
const rings = [...new Set([...ticks.filter(t => t > 0), hi])];
|
|
66
|
+
const lines = new Path2D();
|
|
67
|
+
const spokes = drawn(r);
|
|
68
|
+
for (const t of rings)
|
|
69
|
+
lines.addPath(ring(cx, cy, () => (t / hi) * r, spokes));
|
|
70
|
+
for (const i of spokes) {
|
|
71
|
+
lines.moveTo(cx, cy);
|
|
72
|
+
lines.lineTo(cx + toward(i)[0] * r, cy + toward(i)[1] * r);
|
|
73
|
+
}
|
|
74
|
+
const labels = spokes.map(i => {
|
|
75
|
+
const [[c, s], text] = [toward(i), axes[i]];
|
|
76
|
+
const side = Math.abs(c) < 0.01 ? 'center' : c > 0 ? 'start' : 'end';
|
|
77
|
+
return { x: cx + c * (r + 8 * dpr), y: cy + s * (r + 12 * dpr), text, align: side, priority: 1 };
|
|
78
|
+
});
|
|
79
|
+
// Under its ring, clear of the name at the top of the first spoke.
|
|
80
|
+
for (const t of rings) {
|
|
81
|
+
const [x, y] = [cx + 4 * dpr, cy - (t / hi) * r + 9 * dpr];
|
|
82
|
+
labels.push({
|
|
83
|
+
x,
|
|
84
|
+
y,
|
|
85
|
+
text: String(t),
|
|
86
|
+
align: 'start',
|
|
87
|
+
color: view.theme.muted,
|
|
88
|
+
priority: t === hi ? 0.5 : 0,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
labels: r && n ? labels : [],
|
|
93
|
+
paint(k) {
|
|
94
|
+
ctx.globalAlpha = k;
|
|
95
|
+
ctx.strokeStyle = view.theme.grid;
|
|
96
|
+
ctx.lineWidth = dpr;
|
|
97
|
+
ctx.stroke(lines);
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
return [
|
|
103
|
+
grid,
|
|
104
|
+
...series.map((s) => ({
|
|
105
|
+
type: 'radar',
|
|
106
|
+
key: s.key,
|
|
107
|
+
label: s.label,
|
|
108
|
+
group: s.group ?? s.label,
|
|
109
|
+
color: s.color,
|
|
110
|
+
// With the rim and the spokes, so that a change to either redraws a series left as it was.
|
|
111
|
+
options: { ...s, hi, axes, fill },
|
|
112
|
+
points: n,
|
|
113
|
+
channels: { x: [], y: [] },
|
|
114
|
+
oneShape: true,
|
|
115
|
+
prepare(view) {
|
|
116
|
+
const { ctx, dpr, left, top, width, height } = view;
|
|
117
|
+
const { cx, cy, r } = disc(view);
|
|
118
|
+
const far = (i) => (Math.min(worth(s.values[i]), hi) / hi) * r;
|
|
119
|
+
const at = (i) => [cx + toward(i)[0] * far(i), cy + toward(i)[1] * far(i)];
|
|
120
|
+
const spokes = drawn(r, far);
|
|
121
|
+
const shape = ring(cx, cy, far, spokes);
|
|
122
|
+
const corners = new Path2D();
|
|
123
|
+
for (const i of spokes) {
|
|
124
|
+
corners.moveTo(...at(i));
|
|
125
|
+
corners.arc(...at(i), 2 * dpr, 0, TURN);
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
paint(k) {
|
|
129
|
+
ctx.fillStyle = ctx.strokeStyle = s.color;
|
|
130
|
+
ctx.lineWidth = 1.5 * dpr;
|
|
131
|
+
ctx.lineJoin = 'round';
|
|
132
|
+
ctx.globalAlpha = k * fill;
|
|
133
|
+
ctx.fill(shape);
|
|
134
|
+
ctx.globalAlpha = k;
|
|
135
|
+
ctx.stroke(shape);
|
|
136
|
+
ctx.fill(corners);
|
|
137
|
+
},
|
|
138
|
+
// The nearest corner within reach, as a row: the spoke it is on.
|
|
139
|
+
hit(px, py, max) {
|
|
140
|
+
let [row, near] = [-1, max];
|
|
141
|
+
for (let i = 0; i < n && r; i++) {
|
|
142
|
+
const [x, y] = at(i);
|
|
143
|
+
const d = Math.hypot(px - x / dpr, py - y / dpr);
|
|
144
|
+
if (d <= near)
|
|
145
|
+
[row, near] = [i, d];
|
|
146
|
+
}
|
|
147
|
+
if (row < 0)
|
|
148
|
+
return null;
|
|
149
|
+
const [x, y] = at(row);
|
|
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
|
+
};
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
})),
|
|
161
|
+
];
|
|
162
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
/**
|
|
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.
|
|
6
|
+
*/
|
|
7
|
+
export type RadialBarsOptions = {
|
|
8
|
+
/** Each bar's value, the first outermost; one that is negative or not a number has no bar. */
|
|
9
|
+
values: Col;
|
|
10
|
+
/** The bars' colours, repeated when there are fewer than bars. */
|
|
11
|
+
colors: string[];
|
|
12
|
+
/** Names for a legend, and written at the start of each ring unless `text` is false. */
|
|
13
|
+
labels?: string[];
|
|
14
|
+
/** Whether the names are written at the start of each ring. @default true */
|
|
15
|
+
text?: boolean;
|
|
16
|
+
/** What three quarters of a turn stand for, capping larger values; the largest rounded up when left out. */
|
|
17
|
+
max?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Each bar starts at this opacity and reaches its full colour at its end. Where the browser
|
|
20
|
+
* has no conic gradient the bars stay solid.
|
|
21
|
+
*/
|
|
22
|
+
sweep?: number;
|
|
23
|
+
/** The share of the radius left empty in the middle. @default 0.3 */
|
|
24
|
+
inner?: number;
|
|
25
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
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. */
|
|
28
|
+
key?: string;
|
|
29
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
30
|
+
label?: string;
|
|
31
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
32
|
+
group?: string;
|
|
33
|
+
};
|
|
34
|
+
export declare function radialBars(o: RadialBarsOptions): Mark;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { scale } from '../scale.js';
|
|
2
|
+
import { rgba } from '../gl.js';
|
|
3
|
+
import { TURN, turnAt } from './common.js';
|
|
4
|
+
const SWEEP = 0.75;
|
|
5
|
+
export function radialBars(o) {
|
|
6
|
+
const { values, colors, inner = 0.3, alpha = 1 } = o;
|
|
7
|
+
const n = values.length;
|
|
8
|
+
const worth = (v) => (v > 0 && Number.isFinite(v) ? v : 0);
|
|
9
|
+
let most = 0;
|
|
10
|
+
for (let i = 0; i < n; i++)
|
|
11
|
+
most = Math.max(most, worth(values[i]));
|
|
12
|
+
const hi = worth(o.max) || scale('nice', [0, most || 1]).hi;
|
|
13
|
+
// How far round each bar goes, as a share of a whole turn.
|
|
14
|
+
const round = Float64Array.from({ length: n }, (_, i) => (Math.min(worth(values[i]), hi) / hi) * SWEEP);
|
|
15
|
+
const shade = (i) => colors[i % colors.length] ?? '#888';
|
|
16
|
+
return {
|
|
17
|
+
type: 'radialBars',
|
|
18
|
+
key: o.key,
|
|
19
|
+
label: o.label,
|
|
20
|
+
group: o.group ?? o.label,
|
|
21
|
+
color: shade(0),
|
|
22
|
+
options: o,
|
|
23
|
+
points: n,
|
|
24
|
+
channels: { x: [], y: [] },
|
|
25
|
+
entries: o.labels?.slice(0, n).map((label, i) => ({ label, color: shade(i) })),
|
|
26
|
+
prepare({ ctx, dpr, left, top, width, height, theme }) {
|
|
27
|
+
const [cx, cy] = [left + width / 2, top + height / 2];
|
|
28
|
+
const outer = Math.max(0, Math.min(width, height) / 2);
|
|
29
|
+
const hole = outer * Math.max(0, Math.min(inner, 1));
|
|
30
|
+
const thick = (outer - hole) / (n || 1);
|
|
31
|
+
// Rings thinner than a device pixel have no gap between them: see the bands below.
|
|
32
|
+
const gap = thick < 1 ? 0 : Math.min(2 * dpr, thick / 4);
|
|
33
|
+
// Ring i between its two radii, from twelve o'clock round to a share of the turn.
|
|
34
|
+
const reach = (i) => [outer - (i + 1) * thick + gap, outer - i * thick];
|
|
35
|
+
const bent = (first, last, share) => {
|
|
36
|
+
const [near, far] = [reach(last)[0], reach(first)[1]];
|
|
37
|
+
const [a, b] = [-TURN / 4, share * TURN - TURN / 4];
|
|
38
|
+
const path = new Path2D();
|
|
39
|
+
path.arc(cx, cy, far, a, b);
|
|
40
|
+
path.arc(cx, cy, near, b, a, true);
|
|
41
|
+
path.closePath();
|
|
42
|
+
return path;
|
|
43
|
+
};
|
|
44
|
+
// Rings thinner than a device pixel are drawn several to a band a pixel thick, which is as
|
|
45
|
+
// long as its longest bar and has that bar's colour: more shapes than pixels cost time and show nothing.
|
|
46
|
+
const per = thick && thick < 1 ? Math.ceil(1 / thick) : 1;
|
|
47
|
+
const tracks = new Path2D();
|
|
48
|
+
if (per > 1)
|
|
49
|
+
tracks.addPath(bent(0, n - 1, SWEEP));
|
|
50
|
+
else
|
|
51
|
+
for (let i = 0; i < n && thick; i++)
|
|
52
|
+
tracks.addPath(bent(i, i, SWEEP));
|
|
53
|
+
const bars = [];
|
|
54
|
+
for (let from = 0; from < n && thick; from += per) {
|
|
55
|
+
const to = Math.min(from + per, n);
|
|
56
|
+
let lead = from;
|
|
57
|
+
for (let i = from; i < to; i++)
|
|
58
|
+
if (round[i] > round[lead])
|
|
59
|
+
lead = i;
|
|
60
|
+
if (!round[lead])
|
|
61
|
+
continue;
|
|
62
|
+
const ink = o.sweep === undefined ? undefined : ctx.createConicGradient?.(-TURN / 4, cx, cy);
|
|
63
|
+
if (ink) {
|
|
64
|
+
const [red, green, blue, a = 1] = rgba(ctx, shade(lead)).map((v, at) => at < 3 ? Math.round(v * 255) : v);
|
|
65
|
+
ink.addColorStop(0, `rgba(${red},${green},${blue},${a * o.sweep})`);
|
|
66
|
+
ink.addColorStop(round[lead], `rgba(${red},${green},${blue},${a})`);
|
|
67
|
+
}
|
|
68
|
+
bars.push({ path: bent(from, to - 1, round[lead]), from, to, lead, ink });
|
|
69
|
+
}
|
|
70
|
+
const anchor = (i) => {
|
|
71
|
+
const angle = (round[i] / 2) * TURN - TURN / 4;
|
|
72
|
+
const far = (reach(i)[0] + reach(i)[1]) / 2;
|
|
73
|
+
return [cx + Math.cos(angle) * far, cy + Math.sin(angle) * far];
|
|
74
|
+
};
|
|
75
|
+
const labels = [];
|
|
76
|
+
for (let i = 0; o.text !== false && thick && i < n && i < (o.labels?.length ?? 0); i++) {
|
|
77
|
+
labels.push({
|
|
78
|
+
x: cx - 6 * dpr,
|
|
79
|
+
y: cy - (reach(i)[0] + reach(i)[1]) / 2,
|
|
80
|
+
text: o.labels[i],
|
|
81
|
+
align: 'end',
|
|
82
|
+
priority: n - i,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
labels,
|
|
87
|
+
paint(k, row, rest = 1) {
|
|
88
|
+
ctx.globalAlpha = alpha * k * 0.06;
|
|
89
|
+
ctx.fillStyle = theme.text;
|
|
90
|
+
ctx.fill(tracks);
|
|
91
|
+
for (const { path, from, to, lead, ink } of bars) {
|
|
92
|
+
ctx.globalAlpha = alpha * k * (row === undefined || (row >= from && row < to) ? 1 : rest);
|
|
93
|
+
ctx.fillStyle = ink ?? shade(lead);
|
|
94
|
+
ctx.fill(path);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
// The bar the pointer is on, not its empty track. Reported at the middle of the bar.
|
|
98
|
+
hit(px, py, max) {
|
|
99
|
+
const [dx, dy] = [px * dpr - cx, py * dpr - cy];
|
|
100
|
+
const row = Math.floor((outer - Math.hypot(dx, dy)) / (thick || 1));
|
|
101
|
+
if (!(row >= 0 && row < n) || !round[row] || !thick)
|
|
102
|
+
return null;
|
|
103
|
+
if (Math.hypot(dx, dy) < reach(row)[0] || turnAt(dx, dy) >= round[row])
|
|
104
|
+
return null;
|
|
105
|
+
const [x, y] = anchor(row);
|
|
106
|
+
return { x: (x - left) / width, y: 1 - (y - top) / height, distance: max, row };
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Col, View } from '../types.js';
|
|
2
|
+
/** Two colours as red, green, blue and opacity, blended across each rectangle or down it; `based` starts it at the value end. */
|
|
3
|
+
export type Blend = {
|
|
4
|
+
stops: number[][];
|
|
5
|
+
upright: boolean;
|
|
6
|
+
based: boolean;
|
|
7
|
+
};
|
|
8
|
+
type Held = {
|
|
9
|
+
vao: WebGLVertexArrayObject;
|
|
10
|
+
buffer: WebGLBuffer;
|
|
11
|
+
};
|
|
12
|
+
/** Which columns give a rectangle its two x and two y, and how it is moved and grown in device pixels. */
|
|
13
|
+
export type Part = {
|
|
14
|
+
xa: number;
|
|
15
|
+
xb: number;
|
|
16
|
+
ya: number;
|
|
17
|
+
yb: number;
|
|
18
|
+
shift?: [number, number];
|
|
19
|
+
pad?: [number, number];
|
|
20
|
+
/** Edges rounded to device pixels, and a pixel kept of any side that has a length. */
|
|
21
|
+
snap?: boolean;
|
|
22
|
+
alpha?: number;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The columns of a mark as rows on the GPU, one row per item, uploaded once: a view changes only
|
|
26
|
+
* what the rows are multiplied by. `x` and `y` are numbered together, x first; a column left
|
|
27
|
+
* out stands for zero. A row with a number missing draws nothing.
|
|
28
|
+
*/
|
|
29
|
+
export declare function gpuRows(holds: WeakMap<WebGL2RenderingContext, Held>, x: (Col | undefined)[], y: (Col | undefined)[]): {
|
|
30
|
+
release(gl: WebGL2RenderingContext): void;
|
|
31
|
+
/**
|
|
32
|
+
* A painter for one view. draw(part, strength, from, count) fills `count` rows from `from`;
|
|
33
|
+
* once(draws) runs draws so that a pixel takes only the first rectangle to reach it, as one
|
|
34
|
+
* filled path would, and nothing shows further than `overhang` CSS pixels past the plot area.
|
|
35
|
+
*/
|
|
36
|
+
on({ ctx, dpr, ox, kx, oy, ky, left, top, width, height }: View, gl: WebGL2RenderingContext, color: string, overhang?: number, blend?: Blend): {
|
|
37
|
+
draw: (part: Part, strength: number, from?: number, count?: number) => void;
|
|
38
|
+
once: (draws: () => void) => void;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
/** The runs of rows and their strengths when one row is focused: that row first, so that it is the one a pixel keeps. */
|
|
42
|
+
export declare function focused(n: number, row: number | undefined, rest: number): [strength: number, from: number, count: number][];
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { program, rgba } from '../gl.js';
|
|
2
|
+
const programs = new WeakMap();
|
|
3
|
+
// An instance is a rectangle between two x and two y in data units, which `m` takes to device
|
|
4
|
+
// pixels; `box` then moves it and grows it by pixels, which is how a stem or a cap is made of a point.
|
|
5
|
+
// `ramp` blends `color` into `far` across each rectangle or down it; down one that stands on a
|
|
6
|
+
// baseline, it runs from the value end, which is the bottom of one below the baseline.
|
|
7
|
+
function rectProgram(gl) {
|
|
8
|
+
const p = program(gl, 'layout(location=0) in float xa;layout(location=1) in float xb;layout(location=2) in float ya;layout(location=3) in float yb;uniform vec4 m;uniform vec4 box;uniform vec2 size;uniform float snap;uniform vec2 ramp;out vec2 q;void main(){float x0=min(xa,xb)*m.x+m.y;float x1=max(xa,xb)*m.x+m.y;float y0=m.w-max(ya,yb)*m.z;float y1=m.w-min(ya,yb)*m.z;if(snap>0.5){x0=floor(x0+0.5);y0=floor(y0+0.5);x1=max(floor(x1+0.5),x0+float(xa!=xb));y1=max(floor(y1+0.5),y0+float(ya!=yb));}x0+=box.x-box.z;x1=max(x1+box.x+box.z,x0);y0+=box.y-box.w;y1=max(y1+box.y+box.w,y0);vec2 p=vec2((gl_VertexID&1)==0?x0:x1,(gl_VertexID&2)==0?y0:y1);q=vec2(float(gl_VertexID&1),float((gl_VertexID>>1)&1));if(ramp.y>0.5&&yb<ya)q.y=1.0-q.y;gl_Position=isnan(xa+xb+ya+yb)?vec4(2,2,2,1):vec4(p.x/size.x*2.0-1.0,1.0-p.y/size.y*2.0,0,1);}', 'precision highp float;uniform vec4 color;uniform vec4 far;uniform vec2 ramp;in vec2 q;out vec4 o;void main(){o=ramp.x<0.0?color:mix(color,far,ramp.x<0.5?q.x:q.y);}');
|
|
9
|
+
const at = (name) => gl.getUniformLocation(p, name);
|
|
10
|
+
return {
|
|
11
|
+
p,
|
|
12
|
+
m: at('m'),
|
|
13
|
+
box: at('box'),
|
|
14
|
+
size: at('size'),
|
|
15
|
+
snap: at('snap'),
|
|
16
|
+
color: at('color'),
|
|
17
|
+
far: at('far'),
|
|
18
|
+
ramp: at('ramp'),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The columns of a mark as rows on the GPU, one row per item, uploaded once: a view changes only
|
|
23
|
+
* what the rows are multiplied by. `x` and `y` are numbered together, x first; a column left
|
|
24
|
+
* out stands for zero. A row with a number missing draws nothing.
|
|
25
|
+
*/
|
|
26
|
+
export function gpuRows(holds, x, y) {
|
|
27
|
+
const columns = [...x, ...y];
|
|
28
|
+
const kept = columns.flatMap((c, at) => (c ? [at] : []));
|
|
29
|
+
const n = columns[kept[0] ?? 0]?.length ?? 0;
|
|
30
|
+
// Offsets are subtracted in float64: a float32 cannot hold e.g. millisecond timestamps.
|
|
31
|
+
const first = (list) => {
|
|
32
|
+
const v = list.find(c => c)?.[0];
|
|
33
|
+
return Number.isFinite(v) ? v : 0;
|
|
34
|
+
};
|
|
35
|
+
const [x0, y0] = [first(x), first(y)];
|
|
36
|
+
const zero = (at) => (at < x.length ? x0 : y0);
|
|
37
|
+
return {
|
|
38
|
+
release(gl) {
|
|
39
|
+
const held = holds.get(gl);
|
|
40
|
+
if (!held)
|
|
41
|
+
return;
|
|
42
|
+
gl.deleteVertexArray(held.vao);
|
|
43
|
+
gl.deleteBuffer(held.buffer);
|
|
44
|
+
holds.delete(gl);
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* A painter for one view. draw(part, strength, from, count) fills `count` rows from `from`;
|
|
48
|
+
* once(draws) runs draws so that a pixel takes only the first rectangle to reach it, as one
|
|
49
|
+
* filled path would, and nothing shows further than `overhang` CSS pixels past the plot area.
|
|
50
|
+
*/
|
|
51
|
+
on({ ctx, dpr, ox, kx, oy, ky, left, top, width, height }, gl, color, overhang = 0, blend) {
|
|
52
|
+
const u = programs.get(gl) ?? rectProgram(gl);
|
|
53
|
+
programs.set(gl, u);
|
|
54
|
+
let held = holds.get(gl);
|
|
55
|
+
if (!held) {
|
|
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;
|
|
62
|
+
}
|
|
63
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, held.buffer);
|
|
64
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
|
|
65
|
+
}
|
|
66
|
+
const { vao, buffer } = held;
|
|
67
|
+
const [red, green, blue, opacity = 1] = rgba(ctx, color);
|
|
68
|
+
const draw = (part, strength, from = 0, count = n) => {
|
|
69
|
+
if (count <= 0)
|
|
70
|
+
return;
|
|
71
|
+
gl.useProgram(u.p);
|
|
72
|
+
gl.bindVertexArray(vao);
|
|
73
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
74
|
+
[part.xa, part.xb, part.ya, part.yb].forEach((at, slot) => {
|
|
75
|
+
const place = kept.indexOf(at);
|
|
76
|
+
gl.vertexAttribDivisor(slot, 1);
|
|
77
|
+
if (place < 0) {
|
|
78
|
+
gl.disableVertexAttribArray(slot);
|
|
79
|
+
gl.vertexAttrib1f(slot, -zero(at));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
gl.enableVertexAttribArray(slot);
|
|
83
|
+
gl.vertexAttribPointer(slot, 1, gl.FLOAT, false, kept.length * 4, (from * kept.length + place) * 4);
|
|
84
|
+
});
|
|
85
|
+
gl.uniform4f(u.m, kx, ox + x0 * kx, ky, oy - y0 * ky);
|
|
86
|
+
gl.uniform4f(u.box, ...(part.shift ?? [0, 0]), ...(part.pad ?? [0, 0]));
|
|
87
|
+
gl.uniform2f(u.size, gl.canvas.width, gl.canvas.height);
|
|
88
|
+
gl.uniform1f(u.snap, +!!part.snap);
|
|
89
|
+
const a = strength * (blend ? 1 : opacity) * (part.alpha ?? 1);
|
|
90
|
+
const lit = ([r, g, b, alpha = 1]) => [r * alpha * a, g * alpha * a, b * alpha * a, alpha * a];
|
|
91
|
+
gl.uniform4f(u.color, ...lit(blend ? blend.stops[0] : [red, green, blue]));
|
|
92
|
+
gl.uniform4f(u.far, ...lit(blend ? blend.stops[1] : [0, 0, 0, 0]));
|
|
93
|
+
gl.uniform2f(u.ramp, blend ? +blend.upright : -1, +!!blend?.based);
|
|
94
|
+
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, 4, count);
|
|
95
|
+
gl.bindVertexArray(null);
|
|
96
|
+
};
|
|
97
|
+
const once = (draws) => {
|
|
98
|
+
const over = overhang * dpr;
|
|
99
|
+
gl.enable(gl.SCISSOR_TEST);
|
|
100
|
+
gl.scissor(left - over, gl.canvas.height - top - height - over, width + 2 * over, height + 2 * over);
|
|
101
|
+
gl.enable(gl.DEPTH_TEST);
|
|
102
|
+
gl.clear(gl.DEPTH_BUFFER_BIT);
|
|
103
|
+
draws();
|
|
104
|
+
gl.disable(gl.DEPTH_TEST);
|
|
105
|
+
gl.disable(gl.SCISSOR_TEST);
|
|
106
|
+
};
|
|
107
|
+
return { draw, once };
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** The runs of rows and their strengths when one row is focused: that row first, so that it is the one a pixel keeps. */
|
|
112
|
+
export function focused(n, row, rest) {
|
|
113
|
+
if (row === undefined || row >= n)
|
|
114
|
+
return [[1, 0, n]];
|
|
115
|
+
return [
|
|
116
|
+
[1, row, 1],
|
|
117
|
+
[rest, 0, row],
|
|
118
|
+
[rest, row + 1, n - row - 1],
|
|
119
|
+
];
|
|
120
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
/** A line across the whole plot at each x or each y value, over the data and never a series. */
|
|
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. */
|
|
5
|
+
key?: string;
|
|
6
|
+
/** A CSS colour. */
|
|
7
|
+
color: string;
|
|
8
|
+
/** The line's width in CSS pixels. @default 1 */
|
|
9
|
+
width?: number;
|
|
10
|
+
/** Draws the line dashed. @default false */
|
|
11
|
+
dashed?: boolean;
|
|
12
|
+
/** Written beside each line, where it fits inside the plot. */
|
|
13
|
+
text?: string;
|
|
14
|
+
} & ({
|
|
15
|
+
/** The x of the line, or a column of them for one line each. */
|
|
16
|
+
x: number | Col;
|
|
17
|
+
} | {
|
|
18
|
+
/** The y of the line, or a column of them for one line each. */
|
|
19
|
+
y: number | Col;
|
|
20
|
+
/** Places the line on the chart's second y axis with 'y2'. @default 'y' */
|
|
21
|
+
yAxis?: 'y' | 'y2';
|
|
22
|
+
});
|
|
23
|
+
export declare function rule(o: RuleOptions): Mark;
|
|
24
|
+
/** A band across the whole plot between two x or two y values, under the data and never a series. */
|
|
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. */
|
|
27
|
+
key?: string;
|
|
28
|
+
/** A CSS colour. */
|
|
29
|
+
color: string;
|
|
30
|
+
/** The band's opacity, from 0 to 1. @default 0.15 */
|
|
31
|
+
alpha?: number;
|
|
32
|
+
/** Written in the band's top left corner, where it fits inside the plot. */
|
|
33
|
+
text?: string;
|
|
34
|
+
} & ({
|
|
35
|
+
/** One edge of the band along x. */
|
|
36
|
+
x1: number;
|
|
37
|
+
/** The band's other edge along x. */
|
|
38
|
+
x2: number;
|
|
39
|
+
} | {
|
|
40
|
+
/** One edge of the band along y. */
|
|
41
|
+
y1: number;
|
|
42
|
+
/** The band's other edge along y. */
|
|
43
|
+
y2: number;
|
|
44
|
+
/** Places the band on the chart's second y axis with 'y2'. @default 'y' */
|
|
45
|
+
yAxis?: 'y' | 'y2';
|
|
46
|
+
});
|
|
47
|
+
export declare function zone(o: ZoneOptions): Mark;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { textWidth } from '../text.js';
|
|
2
|
+
export function rule(o) {
|
|
3
|
+
const { color, width = 1, dashed = false, text } = o;
|
|
4
|
+
return {
|
|
5
|
+
type: 'rule',
|
|
6
|
+
key: o.key,
|
|
7
|
+
yAxis: 'y' in o ? o.yAxis : undefined,
|
|
8
|
+
color,
|
|
9
|
+
options: o,
|
|
10
|
+
points: 0,
|
|
11
|
+
channels: { x: [], y: [] },
|
|
12
|
+
// It paints nothing on a canvas: as a backdrop it holds no mark off the WebGL layer and fits no domain.
|
|
13
|
+
backdrop: true,
|
|
14
|
+
prepare({ dpr, ox, kx, oy, ky, left, top, width: w, height: h }) {
|
|
15
|
+
const at = 'x' in o ? o.x : o.y;
|
|
16
|
+
const rules = [];
|
|
17
|
+
const inPlot = ({ x, y, text, align }) => {
|
|
18
|
+
const len = textWidth(text) * dpr;
|
|
19
|
+
const l = align === 'start' ? x : align === 'end' ? x - len : x - len / 2;
|
|
20
|
+
return l >= left && l + len <= left + w && y - 8 * dpr >= top && y + 8 * dpr <= top + h;
|
|
21
|
+
};
|
|
22
|
+
const labels = [];
|
|
23
|
+
const place = (...tries) => {
|
|
24
|
+
const fit = tries.find(inPlot);
|
|
25
|
+
if (fit)
|
|
26
|
+
labels.push(fit);
|
|
27
|
+
};
|
|
28
|
+
for (const v of typeof at === 'number' ? [at] : Array.from(at)) {
|
|
29
|
+
if ('x' in o) {
|
|
30
|
+
const x = ox + v * kx;
|
|
31
|
+
if (!(x >= left && x <= left + w))
|
|
32
|
+
continue;
|
|
33
|
+
rules.push({ x, color, width: width * dpr, dashed });
|
|
34
|
+
if (text)
|
|
35
|
+
place({ x: x + 4 * dpr, y: top + 10 * dpr, text, align: 'start' }, { x: x - 4 * dpr, y: top + 10 * dpr, text, align: 'end' });
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const y = oy - v * ky;
|
|
39
|
+
if (!(y >= top && y <= top + h))
|
|
40
|
+
continue;
|
|
41
|
+
rules.push({ y, color, width: width * dpr, dashed });
|
|
42
|
+
if (text)
|
|
43
|
+
place({ x: left + w - 4 * dpr, y: y - 9 * dpr, text, align: 'end' }, { x: left + w - 4 * dpr, y: y + 9 * dpr, text, align: 'end' });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { labels, rules, paint() { } };
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function zone(o) {
|
|
51
|
+
const { color, alpha = 0.15, text } = o;
|
|
52
|
+
return {
|
|
53
|
+
type: 'zone',
|
|
54
|
+
key: o.key,
|
|
55
|
+
yAxis: 'y1' in o ? o.yAxis : undefined,
|
|
56
|
+
color,
|
|
57
|
+
options: o,
|
|
58
|
+
points: 0,
|
|
59
|
+
channels: { x: [], y: [] },
|
|
60
|
+
backdrop: true,
|
|
61
|
+
prepare({ ctx, dpr, ox, kx, oy, ky, left, top, width, height }) {
|
|
62
|
+
const edges = (a, b, lo, hi) => {
|
|
63
|
+
const [p, q] = a < b ? [a, b] : [b, a];
|
|
64
|
+
return [Math.max(Math.round(p), lo), Math.min(Math.round(q), hi)];
|
|
65
|
+
};
|
|
66
|
+
const [l, r] = 'x1' in o ? edges(ox + o.x1 * kx, ox + o.x2 * kx, left, left + width) : [left, left + width];
|
|
67
|
+
const [t, b] = 'y1' in o ? edges(oy - o.y1 * ky, oy - o.y2 * ky, top, top + height) : [top, top + height];
|
|
68
|
+
if (!(l < r && t < b))
|
|
69
|
+
return { labels: [], paint() { } };
|
|
70
|
+
const [x, y] = [l + 4 * dpr, t + 10 * dpr];
|
|
71
|
+
const fits = x < left + width && y < top + height;
|
|
72
|
+
const labels = text && fits ? [{ x, y, text, align: 'start' }] : [];
|
|
73
|
+
return {
|
|
74
|
+
labels,
|
|
75
|
+
paint(k) {
|
|
76
|
+
ctx.globalAlpha = alpha * k;
|
|
77
|
+
ctx.fillStyle = color;
|
|
78
|
+
ctx.fillRect(l, t, r - l, b - t);
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Nodes in columns with a ribbon for each link, as wide as the link's value, for a blank frame.
|
|
4
|
+
* `source` and `target` hold the places of nodes in `nodes`; see fromLinks() for links given by
|
|
5
|
+
* name. A node is as tall as the larger of what flows in and what flows out. A link whose value
|
|
6
|
+
* is negative or not a number counts as 0. Rows are the nodes first, then the links, so a hit on
|
|
7
|
+
* link i has the row nodes.length + i. Pointing at a node or a link also picks out all that flows
|
|
8
|
+
* into it and out of it, through links that carry something. A link to an unknown node, a link from a node to itself,
|
|
9
|
+
* and a cycle throw.
|
|
10
|
+
* ponytail: a node sits one column after its furthest source, and each column is ordered by one
|
|
11
|
+
* sweep forwards and one back; more sweeps or a relaxation when crossings matter. A pointer move
|
|
12
|
+
* checks the bounds of every link; an index when a diagram has millions. From 1,000 nodes and
|
|
13
|
+
* links together the diagram is drawn by WebGL, where one is as quick to paint as another.
|
|
14
|
+
*/
|
|
15
|
+
export type SankeyOptions = {
|
|
16
|
+
/** The nodes' names. */
|
|
17
|
+
nodes: string[];
|
|
18
|
+
/** The place in `nodes` of the node each link leaves. */
|
|
19
|
+
source: Col;
|
|
20
|
+
/** The place in `nodes` of the node each link reaches. */
|
|
21
|
+
target: Col;
|
|
22
|
+
/** Each link's value, which its ribbon is as wide as; one negative or not a number counts as 0. */
|
|
23
|
+
value: Col;
|
|
24
|
+
/** The nodes' colours, repeated when there are fewer than nodes. */
|
|
25
|
+
colors: string[];
|
|
26
|
+
/** Each ribbon runs from the colour of the node it leaves into the colour of the one it reaches. */
|
|
27
|
+
blend?: boolean;
|
|
28
|
+
/** Writes the node names beside the nodes. @default true */
|
|
29
|
+
text?: boolean;
|
|
30
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
31
|
+
alpha?: number;
|
|
32
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
33
|
+
key?: string;
|
|
34
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
35
|
+
label?: string;
|
|
36
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
37
|
+
group?: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The columns sankey() takes, from links that name their ends. Nodes are listed as first met.
|
|
41
|
+
* @param links Each link's source and target, by node name, and its value.
|
|
42
|
+
*/
|
|
43
|
+
export declare function fromLinks(links: {
|
|
44
|
+
source: string;
|
|
45
|
+
target: string;
|
|
46
|
+
value: number;
|
|
47
|
+
}[]): {
|
|
48
|
+
nodes: string[];
|
|
49
|
+
source: number[];
|
|
50
|
+
target: number[];
|
|
51
|
+
value: number[];
|
|
52
|
+
};
|
|
53
|
+
export declare function sankey(o: SankeyOptions): Mark;
|