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,246 @@
|
|
|
1
|
+
import { locate as where, settle as release, SLOP_PX, unselect } from './pointer.js';
|
|
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
|
+
const LEVEL_PX = 8;
|
|
4
|
+
const DOUBLE_TAP_MS = 300;
|
|
5
|
+
const DOUBLE_TAP_PX = 20;
|
|
6
|
+
const TAP_MS = 500;
|
|
7
|
+
export function zoom(z = {}) {
|
|
8
|
+
const sides = ['x', 'y'].filter(k => (z.axes ?? 'x').includes(k));
|
|
9
|
+
return chart => {
|
|
10
|
+
const { root, drag } = chart;
|
|
11
|
+
const locate = (e) => where(chart, e);
|
|
12
|
+
if (z.drag !== false)
|
|
13
|
+
drag.pan = z.drag === 'pan' ? 'plain' : 'shift';
|
|
14
|
+
let pending;
|
|
15
|
+
const shown = () => chart.fit(pending === undefined ? {} : pending);
|
|
16
|
+
let frame = 0;
|
|
17
|
+
const flush = () => {
|
|
18
|
+
frame = 0;
|
|
19
|
+
const want = pending;
|
|
20
|
+
pending = undefined;
|
|
21
|
+
if (want !== undefined)
|
|
22
|
+
chart.propose(want);
|
|
23
|
+
};
|
|
24
|
+
const ask = (want) => {
|
|
25
|
+
pending = want;
|
|
26
|
+
frame ||= requestAnimationFrame(flush);
|
|
27
|
+
};
|
|
28
|
+
const wheel = (e) => {
|
|
29
|
+
if (z.wheel === false)
|
|
30
|
+
return;
|
|
31
|
+
const plain = z.wheel === 'plain';
|
|
32
|
+
if (!plain && !e.ctrlKey && !e.metaKey)
|
|
33
|
+
return;
|
|
34
|
+
const { over, at } = locate(e);
|
|
35
|
+
if (!over || !e.deltaY)
|
|
36
|
+
return;
|
|
37
|
+
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? chart.size().height : 1;
|
|
38
|
+
const k = Math.exp(e.deltaY * unit * 0.002);
|
|
39
|
+
const base = shown();
|
|
40
|
+
const want = { ...pending };
|
|
41
|
+
for (const s of sides) {
|
|
42
|
+
const [lo, hi] = base[s];
|
|
43
|
+
const a = lo + at[s] * (hi - lo);
|
|
44
|
+
const around = (t) => [a - (a - lo) * t, a + (hi - a) * t];
|
|
45
|
+
const step = around(k);
|
|
46
|
+
const [flo, fhi] = chart.fit({ [s]: step })[s];
|
|
47
|
+
const t = (fhi - flo) / (hi - lo);
|
|
48
|
+
want[s] =
|
|
49
|
+
k > 1 || Math.abs(fhi - flo) <= Math.abs(step[1] - step[0]) ? step : t < 1 ? around(t) : [lo, hi];
|
|
50
|
+
}
|
|
51
|
+
const next = chart.fit(want);
|
|
52
|
+
const still = sides.every(s => String(next[s]) === String(base[s]));
|
|
53
|
+
if (plain && !e.ctrlKey && !e.metaKey && still)
|
|
54
|
+
return;
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
if (!still)
|
|
57
|
+
ask(want);
|
|
58
|
+
};
|
|
59
|
+
// `lift` is where a drag along both axes left the level: the y spans shown then, and the height it had moved.
|
|
60
|
+
let from;
|
|
61
|
+
let settling = 0;
|
|
62
|
+
const down = (e) => {
|
|
63
|
+
if (from || e.pointerType === 'touch' || e.button !== 0 || z.drag === false)
|
|
64
|
+
return;
|
|
65
|
+
const shift = z.drag === 'shift' || (z.drag === undefined && drag.brush);
|
|
66
|
+
if (shift && !e.shiftKey)
|
|
67
|
+
return;
|
|
68
|
+
const { px, py, over, w, h } = locate(e);
|
|
69
|
+
if (!over)
|
|
70
|
+
return;
|
|
71
|
+
from = { id: e.pointerId, x: px, y: py, view: shown(), w, h };
|
|
72
|
+
document.addEventListener('pointermove', move, true);
|
|
73
|
+
document.addEventListener('pointerup', up, true);
|
|
74
|
+
document.addEventListener('pointercancel', up, true);
|
|
75
|
+
document.addEventListener('selectstart', unselect, true);
|
|
76
|
+
};
|
|
77
|
+
const move = (e) => {
|
|
78
|
+
if (!from || e.pointerId !== from.id)
|
|
79
|
+
return;
|
|
80
|
+
const { px, py } = locate(e);
|
|
81
|
+
const [dx, dy] = [px - from.x, py - from.y];
|
|
82
|
+
if (!drag.active && Math.hypot(dx, dy) < SLOP_PX)
|
|
83
|
+
return;
|
|
84
|
+
drag.active = true;
|
|
85
|
+
const want = {};
|
|
86
|
+
if (sides.includes('x')) {
|
|
87
|
+
const [lo, hi] = from.view.x;
|
|
88
|
+
const shift = (-dx * (hi - lo)) / from.w;
|
|
89
|
+
want.x = [lo + shift, hi + shift];
|
|
90
|
+
}
|
|
91
|
+
if (sides.includes('y')) {
|
|
92
|
+
const both = sides.length === 2;
|
|
93
|
+
// Until then y is left out, so an axis fitted to the x window goes on fitting; from then on only the
|
|
94
|
+
// height beyond the threshold counts, from the spans shown at that moment, both y axes together.
|
|
95
|
+
if (both && !from.lift && Math.abs(dy) >= LEVEL_PX)
|
|
96
|
+
from.lift = { ...shown(), dy: Math.sign(dy) * LEVEL_PX };
|
|
97
|
+
const base = both ? from.lift : { ...from.view, y2: undefined, dy: 0 };
|
|
98
|
+
for (const k of ['y', 'y2']) {
|
|
99
|
+
const s = base?.[k];
|
|
100
|
+
if (!s)
|
|
101
|
+
continue;
|
|
102
|
+
const shift = ((dy - base.dy) * (s[1] - s[0])) / from.h;
|
|
103
|
+
want[k] = [s[0] + shift, s[1] + shift];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
ask(want);
|
|
107
|
+
};
|
|
108
|
+
const end = () => {
|
|
109
|
+
from = undefined;
|
|
110
|
+
document.removeEventListener('pointermove', move, true);
|
|
111
|
+
document.removeEventListener('pointerup', up, true);
|
|
112
|
+
document.removeEventListener('pointercancel', up, true);
|
|
113
|
+
document.removeEventListener('selectstart', unselect, true);
|
|
114
|
+
};
|
|
115
|
+
const up = (e) => {
|
|
116
|
+
if (e.pointerId !== from?.id)
|
|
117
|
+
return;
|
|
118
|
+
end();
|
|
119
|
+
if (drag.active)
|
|
120
|
+
settle();
|
|
121
|
+
};
|
|
122
|
+
const settle = () => (settling = release(chart));
|
|
123
|
+
const engage = () => {
|
|
124
|
+
clearTimeout(settling);
|
|
125
|
+
drag.active = true;
|
|
126
|
+
};
|
|
127
|
+
const whole = () => ask(sides.length === 2 ? null : { [sides[0]]: undefined });
|
|
128
|
+
const reset = (e) => {
|
|
129
|
+
if (locate(e).over)
|
|
130
|
+
whole();
|
|
131
|
+
};
|
|
132
|
+
const touches = new Map();
|
|
133
|
+
let pinch;
|
|
134
|
+
let tap;
|
|
135
|
+
let again = false;
|
|
136
|
+
let lastTap;
|
|
137
|
+
const span = (a, b) => ({
|
|
138
|
+
mid: { x: (a.at.x + b.at.x) / 2, y: (a.at.y + b.at.y) / 2 },
|
|
139
|
+
d: Math.hypot(a.px - b.px, a.py - b.py),
|
|
140
|
+
});
|
|
141
|
+
const touchDown = (e) => {
|
|
142
|
+
if (e.pointerType !== 'touch')
|
|
143
|
+
return;
|
|
144
|
+
const { px, py, at, over } = locate(e);
|
|
145
|
+
if (!over || touches.size >= 2)
|
|
146
|
+
return;
|
|
147
|
+
if (!touches.size) {
|
|
148
|
+
document.addEventListener('pointermove', touchMove, true);
|
|
149
|
+
document.addEventListener('pointerup', touchUp, true);
|
|
150
|
+
document.addEventListener('pointercancel', touchCancel, true);
|
|
151
|
+
}
|
|
152
|
+
touches.set(e.pointerId, { px, py, at });
|
|
153
|
+
if (touches.size === 2) {
|
|
154
|
+
engage();
|
|
155
|
+
tap = lastTap = undefined;
|
|
156
|
+
again = false;
|
|
157
|
+
const [a, b] = [...touches.keys()];
|
|
158
|
+
pinch = { a, b, ...span(touches.get(a), touches.get(b)), view: shown() };
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
again =
|
|
162
|
+
!!lastTap &&
|
|
163
|
+
performance.now() - lastTap.t < DOUBLE_TAP_MS &&
|
|
164
|
+
Math.hypot(px - lastTap.x, py - lastTap.y) < DOUBLE_TAP_PX;
|
|
165
|
+
if (again)
|
|
166
|
+
engage();
|
|
167
|
+
lastTap = undefined;
|
|
168
|
+
tap = { x: px, y: py, t: performance.now() };
|
|
169
|
+
};
|
|
170
|
+
const touchMove = (e) => {
|
|
171
|
+
const p = touches.get(e.pointerId);
|
|
172
|
+
if (!p)
|
|
173
|
+
return;
|
|
174
|
+
const { px, py, at } = locate(e);
|
|
175
|
+
Object.assign(p, { px, py, at });
|
|
176
|
+
if (tap && Math.hypot(p.px - tap.x, p.py - tap.y) >= SLOP_PX)
|
|
177
|
+
tap = undefined;
|
|
178
|
+
if (!pinch)
|
|
179
|
+
return;
|
|
180
|
+
const { mid, d } = span(touches.get(pinch.a), touches.get(pinch.b));
|
|
181
|
+
const k = d && pinch.d ? pinch.d / d : 1;
|
|
182
|
+
const want = {};
|
|
183
|
+
for (const s of sides) {
|
|
184
|
+
const [lo, hi] = pinch.view[s];
|
|
185
|
+
const under = lo + pinch.mid[s] * (hi - lo);
|
|
186
|
+
const [flo, fhi] = chart.fit({ [s]: [lo, lo + (hi - lo) * k] })[s];
|
|
187
|
+
const width = fhi - flo;
|
|
188
|
+
const start = under - mid[s] * width;
|
|
189
|
+
want[s] = [start, start + width];
|
|
190
|
+
}
|
|
191
|
+
ask(want);
|
|
192
|
+
};
|
|
193
|
+
const touchEnd = () => {
|
|
194
|
+
touches.clear();
|
|
195
|
+
pinch = tap = undefined;
|
|
196
|
+
again = false;
|
|
197
|
+
document.removeEventListener('pointermove', touchMove, true);
|
|
198
|
+
document.removeEventListener('pointerup', touchUp, true);
|
|
199
|
+
document.removeEventListener('pointercancel', touchCancel, true);
|
|
200
|
+
if (drag.active)
|
|
201
|
+
settle();
|
|
202
|
+
};
|
|
203
|
+
const touchUp = (e) => {
|
|
204
|
+
if (!touches.has(e.pointerId))
|
|
205
|
+
return;
|
|
206
|
+
touches.delete(e.pointerId);
|
|
207
|
+
pinch = undefined;
|
|
208
|
+
if (touches.size)
|
|
209
|
+
return;
|
|
210
|
+
const t = performance.now();
|
|
211
|
+
if (tap && t - tap.t < TAP_MS) {
|
|
212
|
+
if (again)
|
|
213
|
+
whole();
|
|
214
|
+
else
|
|
215
|
+
lastTap = { ...tap, t };
|
|
216
|
+
}
|
|
217
|
+
touchEnd();
|
|
218
|
+
};
|
|
219
|
+
const touchCancel = (e) => {
|
|
220
|
+
if (!touches.has(e.pointerId))
|
|
221
|
+
return;
|
|
222
|
+
tap = lastTap = undefined;
|
|
223
|
+
touchEnd();
|
|
224
|
+
};
|
|
225
|
+
const touchAction = root.style.touchAction;
|
|
226
|
+
root.style.touchAction = 'pan-x pan-y';
|
|
227
|
+
root.addEventListener('wheel', wheel, { passive: false });
|
|
228
|
+
root.addEventListener('pointerdown', down);
|
|
229
|
+
root.addEventListener('pointerdown', touchDown, true);
|
|
230
|
+
root.addEventListener('dblclick', reset);
|
|
231
|
+
return {
|
|
232
|
+
destroy() {
|
|
233
|
+
end();
|
|
234
|
+
touchEnd();
|
|
235
|
+
root.style.touchAction = touchAction;
|
|
236
|
+
root.removeEventListener('pointerdown', touchDown, true);
|
|
237
|
+
cancelAnimationFrame(frame);
|
|
238
|
+
clearTimeout(settling);
|
|
239
|
+
drag.pan = drag.active = false;
|
|
240
|
+
root.removeEventListener('wheel', wheel);
|
|
241
|
+
root.removeEventListener('pointerdown', down);
|
|
242
|
+
root.removeEventListener('dblclick', reset);
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
}
|
package/dist/gl.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { GlLayer } from './types.js';
|
|
2
|
+
export declare function program(gl: WebGL2RenderingContext, vertex: string, fragment: string): WebGLProgram;
|
|
3
|
+
export declare function glLayer(canvas: HTMLCanvasElement): GlLayer | null;
|
|
4
|
+
/** A CSS colour as red, green, blue and opacity from 0 to 1. */
|
|
5
|
+
export declare function rgba(ctx: CanvasRenderingContext2D, color: string): number[];
|
package/dist/gl.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export function program(gl, vertex, fragment) {
|
|
2
|
+
const p = gl.createProgram();
|
|
3
|
+
for (const [type, src] of [
|
|
4
|
+
[gl.VERTEX_SHADER, vertex],
|
|
5
|
+
[gl.FRAGMENT_SHADER, fragment],
|
|
6
|
+
]) {
|
|
7
|
+
const s = gl.createShader(type);
|
|
8
|
+
gl.shaderSource(s, '#version 300 es\n' + src);
|
|
9
|
+
gl.compileShader(s);
|
|
10
|
+
gl.attachShader(p, s);
|
|
11
|
+
}
|
|
12
|
+
gl.linkProgram(p);
|
|
13
|
+
return p;
|
|
14
|
+
}
|
|
15
|
+
export function glLayer(canvas) {
|
|
16
|
+
const gl = canvas.getContext('webgl2', { antialias: false });
|
|
17
|
+
if (!gl)
|
|
18
|
+
return null;
|
|
19
|
+
gl.enable(gl.BLEND);
|
|
20
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
21
|
+
const fade = program(gl, 'void main(){gl_Position=vec4(float((gl_VertexID<<1)&2)*2.0-1.0,float(gl_VertexID&2)*2.0-1.0,0,1);}', 'precision mediump float;uniform float k;out vec4 o;void main(){o=vec4(0,0,0,k);}');
|
|
22
|
+
const fadeK = gl.getUniformLocation(fade, 'k');
|
|
23
|
+
return {
|
|
24
|
+
gl,
|
|
25
|
+
clear() {
|
|
26
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
27
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
28
|
+
},
|
|
29
|
+
fade(k) {
|
|
30
|
+
gl.useProgram(fade);
|
|
31
|
+
gl.uniform1f(fadeK, k);
|
|
32
|
+
gl.blendFunc(gl.ZERO, gl.SRC_ALPHA);
|
|
33
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
34
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
35
|
+
},
|
|
36
|
+
destroy() {
|
|
37
|
+
gl.getExtension('WEBGL_lose_context')?.loseContext();
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** A CSS colour as red, green, blue and opacity from 0 to 1. */
|
|
42
|
+
export function rgba(ctx, color) {
|
|
43
|
+
ctx.fillStyle = color;
|
|
44
|
+
const css = ctx.fillStyle;
|
|
45
|
+
// An opaque sRGB colour comes back as #rrggbb; anything else is read from a painted pixel, which costs more.
|
|
46
|
+
if (css.startsWith('#'))
|
|
47
|
+
return [1, 3, 5].map(i => parseInt(css.slice(i, i + 2), 16) / 255).concat(1);
|
|
48
|
+
const c = new OffscreenCanvas(1, 1).getContext('2d');
|
|
49
|
+
c.fillStyle = color;
|
|
50
|
+
c.fillRect(0, 0, 1, 1);
|
|
51
|
+
return [...c.getImageData(0, 0, 1, 1).data].map(v => v / 255);
|
|
52
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export { plot } from './chart.js';
|
|
2
|
+
export { animate } from './features/animate.js';
|
|
3
|
+
export { brush } from './features/brush.js';
|
|
4
|
+
export { legend } from './features/legend.js';
|
|
5
|
+
export { tooltip } from './features/tooltip.js';
|
|
6
|
+
export { zoom } from './features/zoom.js';
|
|
7
|
+
export { arcs } from './marks/arcs.js';
|
|
8
|
+
export { area } from './marks/area.js';
|
|
9
|
+
export { bar } from './marks/bar.js';
|
|
10
|
+
export { box } from './marks/box.js';
|
|
11
|
+
export { candlestick } from './marks/candlestick.js';
|
|
12
|
+
export { dots } from './marks/dots.js';
|
|
13
|
+
export { errorBars } from './marks/errorBars.js';
|
|
14
|
+
export { funnel } from './marks/funnel.js';
|
|
15
|
+
export { radar } from './marks/radar.js';
|
|
16
|
+
export { radialBars } from './marks/radialBars.js';
|
|
17
|
+
export { sunburst } from './marks/sunburst.js';
|
|
18
|
+
export { fromTree } from './marks/tree.js';
|
|
19
|
+
export { treemap } from './marks/treemap.js';
|
|
20
|
+
export { heatmap } from './marks/heatmap.js';
|
|
21
|
+
export { line } from './marks/line.js';
|
|
22
|
+
export { fromLinks, sankey } from './marks/sankey.js';
|
|
23
|
+
export { ticks } from './marks/ticks.js';
|
|
24
|
+
export { rule, zone } from './marks/rule.js';
|
|
25
|
+
export { brand, gradient, palette, paletteDark } from './paint.js';
|
|
26
|
+
export type { Gradient, Paint } from './paint.js';
|
|
27
|
+
export { band } from './scales/band.js';
|
|
28
|
+
export { bare } from './scales/bare.js';
|
|
29
|
+
export { sequential } from './scales/color.js';
|
|
30
|
+
export type { ColorScale } from './scales/color.js';
|
|
31
|
+
export { log } from './scales/log.js';
|
|
32
|
+
export type { LogAxis, LogOptions } from './scales/log.js';
|
|
33
|
+
export { time } from './scales/time.js';
|
|
34
|
+
export { dark, light } from './theme.js';
|
|
35
|
+
export type { Theme } from './theme.js';
|
|
36
|
+
export { bands, bin, fiveNumbers, stack } from './transforms.js';
|
|
37
|
+
export type { AnimateOptions } from './features/animate.js';
|
|
38
|
+
export type { BrushOptions } from './features/brush.js';
|
|
39
|
+
export type { LegendOptions } from './features/legend.js';
|
|
40
|
+
export type { TooltipOptions } from './features/tooltip.js';
|
|
41
|
+
export type { ZoomOptions } from './features/zoom.js';
|
|
42
|
+
export type { ArcsOptions } from './marks/arcs.js';
|
|
43
|
+
export type { AreaOptions } from './marks/area.js';
|
|
44
|
+
export type { BarOptions } from './marks/bar.js';
|
|
45
|
+
export type { BoxOptions } from './marks/box.js';
|
|
46
|
+
export type { CandlestickOptions } from './marks/candlestick.js';
|
|
47
|
+
export type { DotsOptions } from './marks/dots.js';
|
|
48
|
+
export type { ErrorBarsOptions } from './marks/errorBars.js';
|
|
49
|
+
export type { FunnelOptions } from './marks/funnel.js';
|
|
50
|
+
export type { RadarOptions } from './marks/radar.js';
|
|
51
|
+
export type { RadialBarsOptions } from './marks/radialBars.js';
|
|
52
|
+
export type { HeatmapOptions } from './marks/heatmap.js';
|
|
53
|
+
export type { LineOptions } from './marks/line.js';
|
|
54
|
+
export type { SankeyOptions } from './marks/sankey.js';
|
|
55
|
+
export type { TicksOptions } from './marks/ticks.js';
|
|
56
|
+
export type { RuleOptions, ZoneOptions } from './marks/rule.js';
|
|
57
|
+
export type { BandAxis } from './scales/band.js';
|
|
58
|
+
export type { TimeAxis, TimeOptions } from './scales/time.js';
|
|
59
|
+
export type * from './types.js';
|
|
60
|
+
export type { SunburstOptions } from './marks/sunburst.js';
|
|
61
|
+
export type { Nested, TreeColumns } from './marks/tree.js';
|
|
62
|
+
export type { TreemapOptions } from './marks/treemap.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export { plot } from './chart.js';
|
|
2
|
+
export { animate } from './features/animate.js';
|
|
3
|
+
export { brush } from './features/brush.js';
|
|
4
|
+
export { legend } from './features/legend.js';
|
|
5
|
+
export { tooltip } from './features/tooltip.js';
|
|
6
|
+
export { zoom } from './features/zoom.js';
|
|
7
|
+
export { arcs } from './marks/arcs.js';
|
|
8
|
+
export { area } from './marks/area.js';
|
|
9
|
+
export { bar } from './marks/bar.js';
|
|
10
|
+
export { box } from './marks/box.js';
|
|
11
|
+
export { candlestick } from './marks/candlestick.js';
|
|
12
|
+
export { dots } from './marks/dots.js';
|
|
13
|
+
export { errorBars } from './marks/errorBars.js';
|
|
14
|
+
export { funnel } from './marks/funnel.js';
|
|
15
|
+
export { radar } from './marks/radar.js';
|
|
16
|
+
export { radialBars } from './marks/radialBars.js';
|
|
17
|
+
export { sunburst } from './marks/sunburst.js';
|
|
18
|
+
export { fromTree } from './marks/tree.js';
|
|
19
|
+
export { treemap } from './marks/treemap.js';
|
|
20
|
+
export { heatmap } from './marks/heatmap.js';
|
|
21
|
+
export { line } from './marks/line.js';
|
|
22
|
+
export { fromLinks, sankey } from './marks/sankey.js';
|
|
23
|
+
export { ticks } from './marks/ticks.js';
|
|
24
|
+
export { rule, zone } from './marks/rule.js';
|
|
25
|
+
export { brand, gradient, palette, paletteDark } from './paint.js';
|
|
26
|
+
export { band } from './scales/band.js';
|
|
27
|
+
export { bare } from './scales/bare.js';
|
|
28
|
+
export { sequential } from './scales/color.js';
|
|
29
|
+
export { log } from './scales/log.js';
|
|
30
|
+
export { time } from './scales/time.js';
|
|
31
|
+
export { dark, light } from './theme.js';
|
|
32
|
+
export { bands, bin, fiveNumbers, stack } from './transforms.js';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Slices of a pie, or of a ring, for a polar frame. They start at 12 o'clock and run clockwise,
|
|
4
|
+
* each as wide as its value's share of the total.
|
|
5
|
+
*/
|
|
6
|
+
export type ArcsOptions = {
|
|
7
|
+
/** Each slice's value; one that is negative or not a number counts as 0. */
|
|
8
|
+
values: Col;
|
|
9
|
+
/** The slices' colours, repeated when there are fewer than slices. */
|
|
10
|
+
colors: string[];
|
|
11
|
+
/** The slices' names, for a legend and a tooltip. */
|
|
12
|
+
labels?: string[];
|
|
13
|
+
/** The hole's share of the radius; above 0 the pie is a ring. @default 0 */
|
|
14
|
+
inner?: number;
|
|
15
|
+
/** Text on each slice that has room for it: true for its share as a percentage, or your own. */
|
|
16
|
+
text?: true | ((row: number, share: number) => string);
|
|
17
|
+
/**
|
|
18
|
+
* Each slice starts at this opacity and reaches its full colour at its end, clockwise. Where
|
|
19
|
+
* the browser has no conic gradient the slices stay solid.
|
|
20
|
+
*/
|
|
21
|
+
sweep?: number;
|
|
22
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
23
|
+
alpha?: number;
|
|
24
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
25
|
+
key?: string;
|
|
26
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
27
|
+
label?: string;
|
|
28
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
29
|
+
group?: string;
|
|
30
|
+
};
|
|
31
|
+
export declare function arcs(o: ArcsOptions): Mark;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { rgba } from '../gl.js';
|
|
2
|
+
import { textWidth } from '../text.js';
|
|
3
|
+
import { inRing, TURN, turnAt } from './common.js';
|
|
4
|
+
export function arcs(o) {
|
|
5
|
+
const { values, colors, inner = 0, alpha = 1 } = o;
|
|
6
|
+
const n = values.length;
|
|
7
|
+
// Where each slice ends, as a share of the turn; the last entry is 1 unless every value is 0.
|
|
8
|
+
const ends = new Float64Array(n);
|
|
9
|
+
const worth = (i) => (values[i] > 0 && Number.isFinite(values[i]) ? values[i] : 0);
|
|
10
|
+
// Shares are summed as fractions of the largest value: the values themselves can overflow.
|
|
11
|
+
let most = 0;
|
|
12
|
+
for (let i = 0; i < n; i++)
|
|
13
|
+
most = Math.max(most, worth(i));
|
|
14
|
+
let total = 0;
|
|
15
|
+
for (let i = 0; i < n; i++)
|
|
16
|
+
ends[i] = total += worth(i) / (most || 1);
|
|
17
|
+
for (let i = 0; i < n; i++)
|
|
18
|
+
ends[i] /= total || 1;
|
|
19
|
+
let wide = 0;
|
|
20
|
+
for (let i = 0; i < n; i++)
|
|
21
|
+
wide += +(ends[i] > (ends[i - 1] ?? 0));
|
|
22
|
+
const shade = (i) => colors[i % colors.length] ?? '#888';
|
|
23
|
+
return {
|
|
24
|
+
type: 'arcs',
|
|
25
|
+
key: o.key,
|
|
26
|
+
label: o.label,
|
|
27
|
+
group: o.group ?? o.label,
|
|
28
|
+
color: shade(0),
|
|
29
|
+
options: o,
|
|
30
|
+
points: n,
|
|
31
|
+
channels: { x: [], y: [] },
|
|
32
|
+
entries: o.labels?.slice(0, n).map((label, i) => ({ label, color: shade(i) })),
|
|
33
|
+
prepare({ ctx, dpr, left, top, width, height }) {
|
|
34
|
+
const [cx, cy] = [left + width / 2, top + height / 2];
|
|
35
|
+
const outer = Math.max(0, Math.min(width, height) / 2);
|
|
36
|
+
const hole = outer * Math.max(0, Math.min(inner, 1));
|
|
37
|
+
// A gap of a device pixel between neighbours, which shows whatever is behind the chart.
|
|
38
|
+
// Slices are gathered until they are a device pixel wide at the rim, and drawn as one in
|
|
39
|
+
// the colour of the widest among them: more shapes than pixels cost time and show nothing.
|
|
40
|
+
const slices = [];
|
|
41
|
+
const width1 = (i) => ends[i] - (ends[i - 1] ?? 0);
|
|
42
|
+
let [first, lead, start] = [0, 0, 0];
|
|
43
|
+
for (let i = 0; i < n && outer; i++) {
|
|
44
|
+
if (width1(i) > width1(lead) || lead < first)
|
|
45
|
+
lead = i;
|
|
46
|
+
const end = ends[i];
|
|
47
|
+
if (end - start < 1 / (outer * TURN) && i < n - 1)
|
|
48
|
+
continue;
|
|
49
|
+
if (end > start) {
|
|
50
|
+
const gap = wide > 1 ? Math.min(0.5 / outer, ((end - start) * TURN) / 4) : 0;
|
|
51
|
+
const [a, b] = [start * TURN - TURN / 4 + gap, end * TURN - TURN / 4 - gap];
|
|
52
|
+
const path = new Path2D();
|
|
53
|
+
path.arc(cx, cy, outer, a, b);
|
|
54
|
+
path.arc(cx, cy, hole, b, a, true);
|
|
55
|
+
path.closePath();
|
|
56
|
+
slices.push({ path, from: first, to: i + 1, lead, span: [start, end] });
|
|
57
|
+
}
|
|
58
|
+
[first, start] = [i + 1, end];
|
|
59
|
+
}
|
|
60
|
+
// One paint for every slice: round the clock, each from faint at its start to full at its end.
|
|
61
|
+
// ponytail: past a few dozen slices a sweep in each cannot be seen and its stops slow every fill, so they stay solid.
|
|
62
|
+
const round = o.sweep !== undefined && slices.length <= 64 && ctx.createConicGradient?.(-TURN / 4, cx, cy);
|
|
63
|
+
for (const { lead, span } of round ? slices : []) {
|
|
64
|
+
const [red, green, blue, a = 1] = rgba(ctx, shade(lead)).map((v, at) => at < 3 ? Math.round(v * 255) : v);
|
|
65
|
+
round.addColorStop(span[0], `rgba(${red},${green},${blue},${a * o.sweep})`);
|
|
66
|
+
round.addColorStop(span[1], `rgba(${red},${green},${blue},${a})`);
|
|
67
|
+
}
|
|
68
|
+
// Where a slice's label and its tooltip sit: half-way round it and half-way out.
|
|
69
|
+
const anchor = (i) => {
|
|
70
|
+
const middle = (((ends[i - 1] ?? 0) + ends[i]) / 2) * TURN - TURN / 4;
|
|
71
|
+
const far = (hole + outer) / 2;
|
|
72
|
+
return [cx + Math.cos(middle) * far, cy + Math.sin(middle) * far];
|
|
73
|
+
};
|
|
74
|
+
const said = o.text === true ? (_, share) => `${Math.round(share * 100)}%` : o.text;
|
|
75
|
+
const within = (i, x, y, w, h) => inRing([cx, cy, hole, outer, ends[i - 1] ?? 0, ends[i]], x, y, w, h);
|
|
76
|
+
const labels = !said
|
|
77
|
+
? undefined
|
|
78
|
+
: Array.from(ends, (end, i) => {
|
|
79
|
+
const share = end - (ends[i - 1] ?? 0);
|
|
80
|
+
// A slice narrower at the rim than one character has no room, so it is not asked.
|
|
81
|
+
const text = share * TURN * outer >= 7 * dpr ? said(i, share) : '';
|
|
82
|
+
const [x, y] = anchor(i);
|
|
83
|
+
if (!text || !within(i, x, y, textWidth(text) * dpr, 16 * dpr))
|
|
84
|
+
return undefined;
|
|
85
|
+
return { x, y, text, color: '#fff', priority: share };
|
|
86
|
+
}).filter(l => l !== undefined);
|
|
87
|
+
return {
|
|
88
|
+
labels,
|
|
89
|
+
paint(k, row, rest = 1) {
|
|
90
|
+
for (const { path, from, to, lead } of slices) {
|
|
91
|
+
ctx.globalAlpha = alpha * k * (row === undefined || (row >= from && row < to) ? 1 : rest);
|
|
92
|
+
ctx.fillStyle = round || shade(lead);
|
|
93
|
+
ctx.fill(path);
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
// A slice holds its starting edge and not its ending one. Reported at the slice's middle.
|
|
97
|
+
hit(px, py, reach) {
|
|
98
|
+
const [dx, dy] = [px * dpr - cx, py * dpr - cy];
|
|
99
|
+
const far = Math.hypot(dx, dy);
|
|
100
|
+
if (far < hole || far > outer || !outer)
|
|
101
|
+
return null;
|
|
102
|
+
const share = turnAt(dx, dy);
|
|
103
|
+
// The first slice to end past the pointer, which is one with a width.
|
|
104
|
+
let [row, past] = [0, n];
|
|
105
|
+
while (row < past) {
|
|
106
|
+
const mid = (row + past) >> 1;
|
|
107
|
+
if (ends[mid] > share)
|
|
108
|
+
past = mid;
|
|
109
|
+
else
|
|
110
|
+
row = mid + 1;
|
|
111
|
+
}
|
|
112
|
+
if (row >= n)
|
|
113
|
+
return null;
|
|
114
|
+
const [x, y] = anchor(row);
|
|
115
|
+
return {
|
|
116
|
+
x: (x - left) / width,
|
|
117
|
+
y: 1 - (y - top) / height,
|
|
118
|
+
distance: reach,
|
|
119
|
+
row,
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
import { type Paint } from '../paint.js';
|
|
3
|
+
import type { Common, Curve } from './common.js';
|
|
4
|
+
/** Filled between 0 and y, or between y1 and y2. */
|
|
5
|
+
export type AreaOptions = Common & {
|
|
6
|
+
/** The x of each point, ascending. */
|
|
7
|
+
x: Col;
|
|
8
|
+
/** How the edges run from point to point: straight, in steps, or smoothly. @default 'linear' */
|
|
9
|
+
curve?: Curve;
|
|
10
|
+
/** Fills it with something other than its colour; on an unstacked `y` area an upright blend runs from its far edge to its baseline. */
|
|
11
|
+
fill?: Paint;
|
|
12
|
+
/** Fills straight across a value that is not finite, where it would leave a gap. */
|
|
13
|
+
join?: boolean;
|
|
14
|
+
} & ({
|
|
15
|
+
/** The top of the area, which is filled from 0; a value that is not finite leaves a gap. */
|
|
16
|
+
y: Col;
|
|
17
|
+
/** Areas with the same id are stacked, in the order listed, over the shown ones; they must share x. */
|
|
18
|
+
stack?: string;
|
|
19
|
+
} | {
|
|
20
|
+
/** One edge of a band, filled across to y2; a value that is not finite leaves a gap. */
|
|
21
|
+
y1: Col;
|
|
22
|
+
/** The band's other edge. */
|
|
23
|
+
y2: Col;
|
|
24
|
+
});
|
|
25
|
+
export declare function area(o: AreaOptions): Mark;
|