swiftplot 0.0.0-stage → 0.1.1
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 +327 -2
- package/dist/chart.d.ts +3 -0
- package/dist/chart.js +1226 -0
- package/dist/charts/controller.d.ts +32 -0
- package/dist/charts/controller.js +231 -0
- package/dist/charts/defaults.d.ts +12 -0
- package/dist/charts/defaults.js +60 -0
- package/dist/charts/fields.d.ts +34 -0
- package/dist/charts/fields.js +167 -0
- package/dist/charts/index.d.ts +6 -0
- package/dist/charts/index.js +3 -0
- package/dist/charts/mount.d.ts +5 -0
- package/dist/charts/mount.js +21 -0
- package/dist/charts/source.d.ts +24 -0
- package/dist/charts/source.js +225 -0
- package/dist/charts/types.d.ts +104 -0
- package/dist/charts/types.js +1 -0
- package/dist/charts/xy.d.ts +36 -0
- package/dist/charts/xy.js +220 -0
- package/dist/features/animate.d.ts +11 -0
- package/dist/features/animate.js +118 -0
- package/dist/features/brush.d.ts +13 -0
- package/dist/features/brush.js +127 -0
- package/dist/features/legend.d.ts +15 -0
- package/dist/features/legend.js +237 -0
- package/dist/features/pointer.d.ts +29 -0
- package/dist/features/pointer.js +23 -0
- package/dist/features/tooltip.d.ts +14 -0
- package/dist/features/tooltip.js +190 -0
- package/dist/features/zoom.d.ts +13 -0
- package/dist/features/zoom.js +228 -0
- package/dist/gl.d.ts +5 -0
- package/dist/gl.js +52 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +31 -0
- package/dist/marks/arcs.d.ts +25 -0
- package/dist/marks/arcs.js +124 -0
- package/dist/marks/area.d.ts +24 -0
- package/dist/marks/area.js +106 -0
- package/dist/marks/bar.d.ts +44 -0
- package/dist/marks/bar.js +243 -0
- package/dist/marks/box.d.ts +27 -0
- package/dist/marks/box.js +188 -0
- package/dist/marks/common.d.ts +84 -0
- package/dist/marks/common.js +236 -0
- package/dist/marks/dots.d.ts +15 -0
- package/dist/marks/dots.js +156 -0
- package/dist/marks/errorBars.d.ts +19 -0
- package/dist/marks/errorBars.js +79 -0
- package/dist/marks/funnel.d.ts +25 -0
- package/dist/marks/funnel.js +121 -0
- package/dist/marks/heatmap.d.ts +19 -0
- package/dist/marks/heatmap.js +139 -0
- package/dist/marks/line.d.ts +19 -0
- package/dist/marks/line.js +75 -0
- package/dist/marks/radar.d.ts +22 -0
- package/dist/marks/radar.js +155 -0
- package/dist/marks/radialBars.d.ts +27 -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 +28 -0
- package/dist/marks/rule.js +80 -0
- package/dist/marks/sankey.d.ts +41 -0
- package/dist/marks/sankey.js +366 -0
- package/dist/marks/sunburst.d.ts +26 -0
- package/dist/marks/sunburst.js +185 -0
- package/dist/marks/ticks.d.ts +9 -0
- package/dist/marks/ticks.js +26 -0
- package/dist/marks/tree.d.ts +41 -0
- package/dist/marks/tree.js +110 -0
- package/dist/marks/treemap.d.ts +25 -0
- package/dist/marks/treemap.js +159 -0
- package/dist/paint.d.ts +30 -0
- package/dist/paint.js +66 -0
- package/dist/scale.d.ts +16 -0
- package/dist/scale.js +96 -0
- package/dist/scales/band.d.ts +9 -0
- package/dist/scales/band.js +23 -0
- package/dist/scales/bare.d.ts +3 -0
- package/dist/scales/bare.js +7 -0
- package/dist/scales/color.d.ts +18 -0
- package/dist/scales/color.js +21 -0
- package/dist/scales/log.d.ts +20 -0
- package/dist/scales/log.js +97 -0
- package/dist/scales/time.d.ts +30 -0
- package/dist/scales/time.js +181 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +8 -0
- package/dist/transforms.d.ts +43 -0
- package/dist/transforms.js +118 -0
- package/dist/types.d.ts +434 -0
- package/dist/types.js +1 -0
- package/package.json +39 -3
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { NEAREST_PX } from '../chart.js';
|
|
2
|
+
import { position } from './pointer.js';
|
|
3
|
+
const FOCUS_PX = 30;
|
|
4
|
+
export function tooltip(t) {
|
|
5
|
+
return chart => {
|
|
6
|
+
const { root } = chart;
|
|
7
|
+
const cross = chart.layer('div', 'display:none;border-left:1px dashed');
|
|
8
|
+
const tip = chart.layer('div', 'display:none;left:0;top:0;border:1px solid;padding:4px 8px;box-sizing:border-box;width:max-content;max-width:100%;max-height:100%;overflow:hidden;white-space:normal;overflow-wrap:anywhere;z-index:100');
|
|
9
|
+
const restyle = () => {
|
|
10
|
+
const { text, muted, surface, border } = chart.theme();
|
|
11
|
+
cross.style.borderLeftColor = muted;
|
|
12
|
+
Object.assign(tip.style, { color: text, background: surface, borderColor: border });
|
|
13
|
+
};
|
|
14
|
+
restyle();
|
|
15
|
+
let at = null;
|
|
16
|
+
let held = false;
|
|
17
|
+
// The user's pointer is on the chart, maybe over another feature's layer, so the app's is not shown.
|
|
18
|
+
let inside = false;
|
|
19
|
+
let fromApp = false;
|
|
20
|
+
const hide = () => {
|
|
21
|
+
cross.style.display = tip.style.display = 'none';
|
|
22
|
+
chart.focus(null);
|
|
23
|
+
};
|
|
24
|
+
const show = (x, y, rows) => {
|
|
25
|
+
const { width, height } = chart.size();
|
|
26
|
+
tip.replaceChildren(...rows.map(r => {
|
|
27
|
+
const e = document.createElement('div');
|
|
28
|
+
e.textContent = r.text;
|
|
29
|
+
if (r.color)
|
|
30
|
+
e.style.color = r.color;
|
|
31
|
+
return e;
|
|
32
|
+
}));
|
|
33
|
+
const fx = x > width / 2;
|
|
34
|
+
const fy = y > height / 2;
|
|
35
|
+
tip.style.display = '';
|
|
36
|
+
const left = fx ? `${x - 12}px - 100%` : `${x + 12}px`;
|
|
37
|
+
const top = fy ? `${y - 12}px - 100%` : `${y + 12}px`;
|
|
38
|
+
tip.style.transform = `translate(clamp(0px, ${left}, ${width}px - 100%), clamp(0px, ${top}, ${height}px - 100%))`;
|
|
39
|
+
};
|
|
40
|
+
const place = () => {
|
|
41
|
+
const { width, height } = chart.size();
|
|
42
|
+
let px;
|
|
43
|
+
let py;
|
|
44
|
+
if (at) {
|
|
45
|
+
fromApp = false;
|
|
46
|
+
({ px, py } = position(chart, ...at));
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const p = inside ? null : chart.pointer();
|
|
50
|
+
if (!p) {
|
|
51
|
+
if (fromApp) {
|
|
52
|
+
fromApp = false;
|
|
53
|
+
hide();
|
|
54
|
+
}
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
fromApp = true;
|
|
58
|
+
({ x: px, y: py } = p);
|
|
59
|
+
}
|
|
60
|
+
if (chart.drag.active)
|
|
61
|
+
return hide();
|
|
62
|
+
if (t.mode === 'nearest') {
|
|
63
|
+
if (py === undefined)
|
|
64
|
+
return hide();
|
|
65
|
+
const hit = chart.hit(px, py, NEAREST_PX);
|
|
66
|
+
if (!hit)
|
|
67
|
+
return hide();
|
|
68
|
+
chart.focus(hit.mark.group ?? null, hit.row === undefined ? undefined : { mark: hit.mark, row: hit.row });
|
|
69
|
+
// A mark made of named parts, such as slices, is spoken for by the part that was hit.
|
|
70
|
+
const part = hit.row === undefined ? undefined : hit.mark.entries?.[hit.row];
|
|
71
|
+
const { label = '', color } = part ?? hit.mark;
|
|
72
|
+
const text = t.text(label, chart.real('x', hit.x), chart.real('y', hit.value ?? hit.y), hit.row);
|
|
73
|
+
show(chart.xPx(hit.x), chart.yPx(hit.y), [{ text, color: hit.color ?? color }]);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const m = chart.margin();
|
|
77
|
+
const off = py !== undefined && (py < m.top || py > height - m.bottom);
|
|
78
|
+
if (px < m.left || px > width - m.right || off)
|
|
79
|
+
return hide();
|
|
80
|
+
const v = chart.xValue(px);
|
|
81
|
+
const rows = [{ text: t.title(chart.real('x', v)) }];
|
|
82
|
+
// A line within reach of the pointer is focused before the filled mark the pointer is in,
|
|
83
|
+
// and of several filled marks the one drawn last.
|
|
84
|
+
let near = FOCUS_PX;
|
|
85
|
+
let line;
|
|
86
|
+
let fill;
|
|
87
|
+
for (const k of chart.marks()) {
|
|
88
|
+
if (!k.sampleAtX)
|
|
89
|
+
continue;
|
|
90
|
+
const s = k.sampleAtX(v);
|
|
91
|
+
const value = typeof s === 'number'
|
|
92
|
+
? chart.real('y', s)
|
|
93
|
+
: s &&
|
|
94
|
+
(s.band ? chart.real('y', s.y2) - chart.real('y', s.y1) : chart.real('y', s.value ?? s.y2));
|
|
95
|
+
if (k.label !== undefined) {
|
|
96
|
+
rows.push({
|
|
97
|
+
text: `${k.label}: ${value === undefined ? '–' : t.value(value)}`,
|
|
98
|
+
color: k.color,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (s === undefined || k.group === undefined || py === undefined)
|
|
102
|
+
continue;
|
|
103
|
+
if (typeof s === 'number') {
|
|
104
|
+
const gap = Math.abs(chart.yPx(s) - py);
|
|
105
|
+
if (gap < near)
|
|
106
|
+
[near, line] = [gap, k.group];
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const a = chart.yPx(s.y1);
|
|
110
|
+
const b = chart.yPx(s.y2);
|
|
111
|
+
if (a !== b && py >= Math.min(a, b) && py <= Math.max(a, b))
|
|
112
|
+
fill = k.group;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const group = line ?? fill ?? null;
|
|
116
|
+
cross.style.display = '';
|
|
117
|
+
cross.style.left = `${px}px`;
|
|
118
|
+
cross.style.top = `${m.top}px`;
|
|
119
|
+
cross.style.height = `${height - m.top - m.bottom}px`;
|
|
120
|
+
show(px, m.top, rows);
|
|
121
|
+
chart.focus(group);
|
|
122
|
+
};
|
|
123
|
+
const move = (e) => {
|
|
124
|
+
inside = true;
|
|
125
|
+
if (chart.drag.active) {
|
|
126
|
+
at = null;
|
|
127
|
+
return hide();
|
|
128
|
+
}
|
|
129
|
+
// Over another feature's layer, such as a legend, the focus is that feature's to set.
|
|
130
|
+
if (e.target !== root) {
|
|
131
|
+
at = null;
|
|
132
|
+
held = false;
|
|
133
|
+
fromApp = false;
|
|
134
|
+
cross.style.display = tip.style.display = 'none';
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
at = [e.clientX, e.clientY];
|
|
138
|
+
held = false;
|
|
139
|
+
place();
|
|
140
|
+
};
|
|
141
|
+
const leave = () => {
|
|
142
|
+
at = null;
|
|
143
|
+
held = false;
|
|
144
|
+
inside = false;
|
|
145
|
+
hide();
|
|
146
|
+
};
|
|
147
|
+
const out = () => {
|
|
148
|
+
leave();
|
|
149
|
+
place();
|
|
150
|
+
};
|
|
151
|
+
// A lifted finger leaves the chart, and a tip it tapped open is meant to stay until a tap elsewhere.
|
|
152
|
+
const lift = (e) => {
|
|
153
|
+
inside = false;
|
|
154
|
+
if (e.pointerType !== 'touch')
|
|
155
|
+
return out();
|
|
156
|
+
held = !!at && tip.style.display !== 'none';
|
|
157
|
+
if (held)
|
|
158
|
+
return;
|
|
159
|
+
at = null;
|
|
160
|
+
place();
|
|
161
|
+
};
|
|
162
|
+
// Capturing, so a control that stops the tap still dismisses, and a tap on the chart sets its own state after.
|
|
163
|
+
const away = () => {
|
|
164
|
+
if (at)
|
|
165
|
+
out();
|
|
166
|
+
};
|
|
167
|
+
root.addEventListener('pointerdown', move);
|
|
168
|
+
root.addEventListener('pointermove', move);
|
|
169
|
+
root.addEventListener('pointerleave', lift);
|
|
170
|
+
root.addEventListener('pointercancel', out);
|
|
171
|
+
document.addEventListener('pointerdown', away, true);
|
|
172
|
+
return {
|
|
173
|
+
update() {
|
|
174
|
+
restyle();
|
|
175
|
+
place();
|
|
176
|
+
},
|
|
177
|
+
// A lifted finger is no longer anywhere on the chart, so what it tapped is let go.
|
|
178
|
+
letGo: () => held && leave(),
|
|
179
|
+
destroy() {
|
|
180
|
+
root.removeEventListener('pointerdown', move);
|
|
181
|
+
root.removeEventListener('pointermove', move);
|
|
182
|
+
root.removeEventListener('pointerleave', lift);
|
|
183
|
+
root.removeEventListener('pointercancel', out);
|
|
184
|
+
document.removeEventListener('pointerdown', away, true);
|
|
185
|
+
cross.remove();
|
|
186
|
+
tip.remove();
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Feature } from '../types.js';
|
|
2
|
+
export type ZoomOptions = {
|
|
3
|
+
/** 'x' when left out. */
|
|
4
|
+
axes?: 'x' | 'y' | 'xy';
|
|
5
|
+
/** 'ctrl' when left out: ctrl or cmd and the wheel zoom, and a plain wheel scrolls the page. */
|
|
6
|
+
wheel?: 'ctrl' | 'plain' | false;
|
|
7
|
+
/**
|
|
8
|
+
* Which drag pans: a plain one, or one with shift held. Left out, a plain drag pans unless a brush
|
|
9
|
+
* is in use. 'pan' takes every mouse drag, so a brush() beside it gets none.
|
|
10
|
+
*/
|
|
11
|
+
drag?: 'pan' | 'shift' | false;
|
|
12
|
+
};
|
|
13
|
+
export declare function zoom(z?: ZoomOptions): Feature;
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { locate as where, settle as release, SLOP_PX, unselect } from './pointer.js';
|
|
2
|
+
const DOUBLE_TAP_MS = 300;
|
|
3
|
+
const DOUBLE_TAP_PX = 20;
|
|
4
|
+
const TAP_MS = 500;
|
|
5
|
+
export function zoom(z = {}) {
|
|
6
|
+
const sides = ['x', 'y'].filter(k => (z.axes ?? 'x').includes(k));
|
|
7
|
+
return chart => {
|
|
8
|
+
const { root, drag } = chart;
|
|
9
|
+
const locate = (e) => where(chart, e);
|
|
10
|
+
if (z.drag !== false)
|
|
11
|
+
drag.pan = z.drag === 'pan' ? 'plain' : 'shift';
|
|
12
|
+
let pending;
|
|
13
|
+
const shown = () => chart.fit(pending === undefined ? {} : pending);
|
|
14
|
+
let frame = 0;
|
|
15
|
+
const flush = () => {
|
|
16
|
+
frame = 0;
|
|
17
|
+
const want = pending;
|
|
18
|
+
pending = undefined;
|
|
19
|
+
if (want !== undefined)
|
|
20
|
+
chart.propose(want);
|
|
21
|
+
};
|
|
22
|
+
const ask = (want) => {
|
|
23
|
+
pending = want;
|
|
24
|
+
frame ||= requestAnimationFrame(flush);
|
|
25
|
+
};
|
|
26
|
+
const wheel = (e) => {
|
|
27
|
+
if (z.wheel === false)
|
|
28
|
+
return;
|
|
29
|
+
const plain = z.wheel === 'plain';
|
|
30
|
+
if (!plain && !e.ctrlKey && !e.metaKey)
|
|
31
|
+
return;
|
|
32
|
+
const { over, at } = locate(e);
|
|
33
|
+
if (!over || !e.deltaY)
|
|
34
|
+
return;
|
|
35
|
+
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? chart.size().height : 1;
|
|
36
|
+
const k = Math.exp(e.deltaY * unit * 0.002);
|
|
37
|
+
const base = shown();
|
|
38
|
+
const want = { ...pending };
|
|
39
|
+
for (const s of sides) {
|
|
40
|
+
const [lo, hi] = base[s];
|
|
41
|
+
const a = lo + at[s] * (hi - lo);
|
|
42
|
+
const around = (t) => [a - (a - lo) * t, a + (hi - a) * t];
|
|
43
|
+
const step = around(k);
|
|
44
|
+
const [flo, fhi] = chart.fit({ [s]: step })[s];
|
|
45
|
+
const t = (fhi - flo) / (hi - lo);
|
|
46
|
+
want[s] =
|
|
47
|
+
k > 1 || Math.abs(fhi - flo) <= Math.abs(step[1] - step[0]) ? step : t < 1 ? around(t) : [lo, hi];
|
|
48
|
+
}
|
|
49
|
+
const next = chart.fit(want);
|
|
50
|
+
const still = sides.every(s => String(next[s]) === String(base[s]));
|
|
51
|
+
if (plain && !e.ctrlKey && !e.metaKey && still)
|
|
52
|
+
return;
|
|
53
|
+
e.preventDefault();
|
|
54
|
+
if (!still)
|
|
55
|
+
ask(want);
|
|
56
|
+
};
|
|
57
|
+
let from;
|
|
58
|
+
let settling = 0;
|
|
59
|
+
const down = (e) => {
|
|
60
|
+
if (from || e.pointerType === 'touch' || e.button !== 0 || z.drag === false)
|
|
61
|
+
return;
|
|
62
|
+
const shift = z.drag === 'shift' || (z.drag === undefined && drag.brush);
|
|
63
|
+
if (shift && !e.shiftKey)
|
|
64
|
+
return;
|
|
65
|
+
const { px, py, over, w, h } = locate(e);
|
|
66
|
+
if (!over)
|
|
67
|
+
return;
|
|
68
|
+
from = { id: e.pointerId, x: px, y: py, view: shown(), w, h };
|
|
69
|
+
document.addEventListener('pointermove', move, true);
|
|
70
|
+
document.addEventListener('pointerup', up, true);
|
|
71
|
+
document.addEventListener('pointercancel', up, true);
|
|
72
|
+
document.addEventListener('selectstart', unselect, true);
|
|
73
|
+
};
|
|
74
|
+
const move = (e) => {
|
|
75
|
+
if (!from || e.pointerId !== from.id)
|
|
76
|
+
return;
|
|
77
|
+
const { px, py } = locate(e);
|
|
78
|
+
const [dx, dy] = [px - from.x, py - from.y];
|
|
79
|
+
if (!drag.active && Math.hypot(dx, dy) < SLOP_PX)
|
|
80
|
+
return;
|
|
81
|
+
drag.active = true;
|
|
82
|
+
const want = {};
|
|
83
|
+
for (const s of sides) {
|
|
84
|
+
const [lo, hi] = from.view[s];
|
|
85
|
+
const shift = s === 'x' ? (-dx * (hi - lo)) / from.w : (dy * (hi - lo)) / from.h;
|
|
86
|
+
want[s] = [lo + shift, hi + shift];
|
|
87
|
+
}
|
|
88
|
+
ask(want);
|
|
89
|
+
};
|
|
90
|
+
const end = () => {
|
|
91
|
+
from = undefined;
|
|
92
|
+
document.removeEventListener('pointermove', move, true);
|
|
93
|
+
document.removeEventListener('pointerup', up, true);
|
|
94
|
+
document.removeEventListener('pointercancel', up, true);
|
|
95
|
+
document.removeEventListener('selectstart', unselect, true);
|
|
96
|
+
};
|
|
97
|
+
const up = (e) => {
|
|
98
|
+
if (e.pointerId !== from?.id)
|
|
99
|
+
return;
|
|
100
|
+
end();
|
|
101
|
+
if (drag.active)
|
|
102
|
+
settle();
|
|
103
|
+
};
|
|
104
|
+
const settle = () => (settling = release(chart));
|
|
105
|
+
const engage = () => {
|
|
106
|
+
clearTimeout(settling);
|
|
107
|
+
drag.active = true;
|
|
108
|
+
};
|
|
109
|
+
const whole = () => ask(sides.length === 2 ? null : { [sides[0]]: undefined });
|
|
110
|
+
const reset = (e) => {
|
|
111
|
+
if (locate(e).over)
|
|
112
|
+
whole();
|
|
113
|
+
};
|
|
114
|
+
const touches = new Map();
|
|
115
|
+
let pinch;
|
|
116
|
+
let tap;
|
|
117
|
+
let again = false;
|
|
118
|
+
let lastTap;
|
|
119
|
+
const span = (a, b) => ({
|
|
120
|
+
mid: { x: (a.at.x + b.at.x) / 2, y: (a.at.y + b.at.y) / 2 },
|
|
121
|
+
d: Math.hypot(a.px - b.px, a.py - b.py),
|
|
122
|
+
});
|
|
123
|
+
const touchDown = (e) => {
|
|
124
|
+
if (e.pointerType !== 'touch')
|
|
125
|
+
return;
|
|
126
|
+
const { px, py, at, over } = locate(e);
|
|
127
|
+
if (!over || touches.size >= 2)
|
|
128
|
+
return;
|
|
129
|
+
if (!touches.size) {
|
|
130
|
+
document.addEventListener('pointermove', touchMove, true);
|
|
131
|
+
document.addEventListener('pointerup', touchUp, true);
|
|
132
|
+
document.addEventListener('pointercancel', touchCancel, true);
|
|
133
|
+
}
|
|
134
|
+
touches.set(e.pointerId, { px, py, at });
|
|
135
|
+
if (touches.size === 2) {
|
|
136
|
+
engage();
|
|
137
|
+
tap = lastTap = undefined;
|
|
138
|
+
again = false;
|
|
139
|
+
const [a, b] = [...touches.keys()];
|
|
140
|
+
pinch = { a, b, ...span(touches.get(a), touches.get(b)), view: shown() };
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
again =
|
|
144
|
+
!!lastTap &&
|
|
145
|
+
performance.now() - lastTap.t < DOUBLE_TAP_MS &&
|
|
146
|
+
Math.hypot(px - lastTap.x, py - lastTap.y) < DOUBLE_TAP_PX;
|
|
147
|
+
if (again)
|
|
148
|
+
engage();
|
|
149
|
+
lastTap = undefined;
|
|
150
|
+
tap = { x: px, y: py, t: performance.now() };
|
|
151
|
+
};
|
|
152
|
+
const touchMove = (e) => {
|
|
153
|
+
const p = touches.get(e.pointerId);
|
|
154
|
+
if (!p)
|
|
155
|
+
return;
|
|
156
|
+
const { px, py, at } = locate(e);
|
|
157
|
+
Object.assign(p, { px, py, at });
|
|
158
|
+
if (tap && Math.hypot(p.px - tap.x, p.py - tap.y) >= SLOP_PX)
|
|
159
|
+
tap = undefined;
|
|
160
|
+
if (!pinch)
|
|
161
|
+
return;
|
|
162
|
+
const { mid, d } = span(touches.get(pinch.a), touches.get(pinch.b));
|
|
163
|
+
const k = d && pinch.d ? pinch.d / d : 1;
|
|
164
|
+
const want = {};
|
|
165
|
+
for (const s of sides) {
|
|
166
|
+
const [lo, hi] = pinch.view[s];
|
|
167
|
+
const under = lo + pinch.mid[s] * (hi - lo);
|
|
168
|
+
const [flo, fhi] = chart.fit({ [s]: [lo, lo + (hi - lo) * k] })[s];
|
|
169
|
+
const width = fhi - flo;
|
|
170
|
+
const start = under - mid[s] * width;
|
|
171
|
+
want[s] = [start, start + width];
|
|
172
|
+
}
|
|
173
|
+
ask(want);
|
|
174
|
+
};
|
|
175
|
+
const touchEnd = () => {
|
|
176
|
+
touches.clear();
|
|
177
|
+
pinch = tap = undefined;
|
|
178
|
+
again = false;
|
|
179
|
+
document.removeEventListener('pointermove', touchMove, true);
|
|
180
|
+
document.removeEventListener('pointerup', touchUp, true);
|
|
181
|
+
document.removeEventListener('pointercancel', touchCancel, true);
|
|
182
|
+
if (drag.active)
|
|
183
|
+
settle();
|
|
184
|
+
};
|
|
185
|
+
const touchUp = (e) => {
|
|
186
|
+
if (!touches.has(e.pointerId))
|
|
187
|
+
return;
|
|
188
|
+
touches.delete(e.pointerId);
|
|
189
|
+
pinch = undefined;
|
|
190
|
+
if (touches.size)
|
|
191
|
+
return;
|
|
192
|
+
const t = performance.now();
|
|
193
|
+
if (tap && t - tap.t < TAP_MS) {
|
|
194
|
+
if (again)
|
|
195
|
+
whole();
|
|
196
|
+
else
|
|
197
|
+
lastTap = { ...tap, t };
|
|
198
|
+
}
|
|
199
|
+
touchEnd();
|
|
200
|
+
};
|
|
201
|
+
const touchCancel = (e) => {
|
|
202
|
+
if (!touches.has(e.pointerId))
|
|
203
|
+
return;
|
|
204
|
+
tap = lastTap = undefined;
|
|
205
|
+
touchEnd();
|
|
206
|
+
};
|
|
207
|
+
const touchAction = root.style.touchAction;
|
|
208
|
+
root.style.touchAction = 'pan-x pan-y';
|
|
209
|
+
root.addEventListener('wheel', wheel, { passive: false });
|
|
210
|
+
root.addEventListener('pointerdown', down);
|
|
211
|
+
root.addEventListener('pointerdown', touchDown, true);
|
|
212
|
+
root.addEventListener('dblclick', reset);
|
|
213
|
+
return {
|
|
214
|
+
destroy() {
|
|
215
|
+
end();
|
|
216
|
+
touchEnd();
|
|
217
|
+
root.style.touchAction = touchAction;
|
|
218
|
+
root.removeEventListener('pointerdown', touchDown, true);
|
|
219
|
+
cancelAnimationFrame(frame);
|
|
220
|
+
clearTimeout(settling);
|
|
221
|
+
drag.pan = drag.active = false;
|
|
222
|
+
root.removeEventListener('wheel', wheel);
|
|
223
|
+
root.removeEventListener('pointerdown', down);
|
|
224
|
+
root.removeEventListener('dblclick', reset);
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
}
|
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,60 @@
|
|
|
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 { dots } from './marks/dots.js';
|
|
12
|
+
export { errorBars } from './marks/errorBars.js';
|
|
13
|
+
export { funnel } from './marks/funnel.js';
|
|
14
|
+
export { radar } from './marks/radar.js';
|
|
15
|
+
export { radialBars } from './marks/radialBars.js';
|
|
16
|
+
export { sunburst } from './marks/sunburst.js';
|
|
17
|
+
export { fromTree } from './marks/tree.js';
|
|
18
|
+
export { treemap } from './marks/treemap.js';
|
|
19
|
+
export { heatmap } from './marks/heatmap.js';
|
|
20
|
+
export { line } from './marks/line.js';
|
|
21
|
+
export { fromLinks, sankey } from './marks/sankey.js';
|
|
22
|
+
export { ticks } from './marks/ticks.js';
|
|
23
|
+
export { rule, zone } from './marks/rule.js';
|
|
24
|
+
export { brand, gradient, palette, paletteDark } from './paint.js';
|
|
25
|
+
export type { Gradient, Paint } 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 type { ColorScale } from './scales/color.js';
|
|
30
|
+
export { log } from './scales/log.js';
|
|
31
|
+
export type { LogAxis, LogOptions } from './scales/log.js';
|
|
32
|
+
export { time } from './scales/time.js';
|
|
33
|
+
export { dark, light } from './theme.js';
|
|
34
|
+
export type { Theme } from './theme.js';
|
|
35
|
+
export { bands, bin, fiveNumbers, stack } from './transforms.js';
|
|
36
|
+
export type { AnimateOptions } from './features/animate.js';
|
|
37
|
+
export type { BrushOptions } from './features/brush.js';
|
|
38
|
+
export type { LegendOptions } from './features/legend.js';
|
|
39
|
+
export type { TooltipOptions } from './features/tooltip.js';
|
|
40
|
+
export type { ZoomOptions } from './features/zoom.js';
|
|
41
|
+
export type { ArcsOptions } from './marks/arcs.js';
|
|
42
|
+
export type { AreaOptions } from './marks/area.js';
|
|
43
|
+
export type { BarOptions } from './marks/bar.js';
|
|
44
|
+
export type { BoxOptions } from './marks/box.js';
|
|
45
|
+
export type { DotsOptions } from './marks/dots.js';
|
|
46
|
+
export type { ErrorBarsOptions } from './marks/errorBars.js';
|
|
47
|
+
export type { FunnelOptions } from './marks/funnel.js';
|
|
48
|
+
export type { RadarOptions } from './marks/radar.js';
|
|
49
|
+
export type { RadialBarsOptions } from './marks/radialBars.js';
|
|
50
|
+
export type { HeatmapOptions } from './marks/heatmap.js';
|
|
51
|
+
export type { LineOptions } from './marks/line.js';
|
|
52
|
+
export type { SankeyOptions } from './marks/sankey.js';
|
|
53
|
+
export type { TicksOptions } from './marks/ticks.js';
|
|
54
|
+
export type { RuleOptions, ZoneOptions } from './marks/rule.js';
|
|
55
|
+
export type { BandAxis } from './scales/band.js';
|
|
56
|
+
export type { TimeAxis, TimeOptions } from './scales/time.js';
|
|
57
|
+
export type * from './types.js';
|
|
58
|
+
export type { SunburstOptions } from './marks/sunburst.js';
|
|
59
|
+
export type { Nested, TreeColumns } from './marks/tree.js';
|
|
60
|
+
export type { TreemapOptions } from './marks/treemap.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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 { dots } from './marks/dots.js';
|
|
12
|
+
export { errorBars } from './marks/errorBars.js';
|
|
13
|
+
export { funnel } from './marks/funnel.js';
|
|
14
|
+
export { radar } from './marks/radar.js';
|
|
15
|
+
export { radialBars } from './marks/radialBars.js';
|
|
16
|
+
export { sunburst } from './marks/sunburst.js';
|
|
17
|
+
export { fromTree } from './marks/tree.js';
|
|
18
|
+
export { treemap } from './marks/treemap.js';
|
|
19
|
+
export { heatmap } from './marks/heatmap.js';
|
|
20
|
+
export { line } from './marks/line.js';
|
|
21
|
+
export { fromLinks, sankey } from './marks/sankey.js';
|
|
22
|
+
export { ticks } from './marks/ticks.js';
|
|
23
|
+
export { rule, zone } from './marks/rule.js';
|
|
24
|
+
export { brand, gradient, palette, paletteDark } from './paint.js';
|
|
25
|
+
export { band } from './scales/band.js';
|
|
26
|
+
export { bare } from './scales/bare.js';
|
|
27
|
+
export { sequential } from './scales/color.js';
|
|
28
|
+
export { log } from './scales/log.js';
|
|
29
|
+
export { time } from './scales/time.js';
|
|
30
|
+
export { dark, light } from './theme.js';
|
|
31
|
+
export { bands, bin, fiveNumbers, stack } from './transforms.js';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Slices of a pie, or of a ring when `inner` (the hole's share of the radius) is above 0, for a
|
|
4
|
+
* polar frame. They start at 12 o'clock and run clockwise, each as wide as its value's share of
|
|
5
|
+
* the total; a value that is negative or not a number counts as 0. Colours repeat when there are
|
|
6
|
+
* fewer than slices. `labels` name the slices for a legend.
|
|
7
|
+
*/
|
|
8
|
+
export type ArcsOptions = {
|
|
9
|
+
values: Col;
|
|
10
|
+
colors: string[];
|
|
11
|
+
labels?: string[];
|
|
12
|
+
inner?: number;
|
|
13
|
+
/** Text on each slice that has room for it: true for its share as a percentage, or your own. */
|
|
14
|
+
text?: true | ((row: number, share: number) => string);
|
|
15
|
+
/**
|
|
16
|
+
* Each slice starts at this opacity and reaches its full colour at its end, clockwise. Where
|
|
17
|
+
* the browser has no conic gradient the slices stay solid.
|
|
18
|
+
*/
|
|
19
|
+
sweep?: number;
|
|
20
|
+
alpha?: number;
|
|
21
|
+
key?: string;
|
|
22
|
+
label?: string;
|
|
23
|
+
group?: string;
|
|
24
|
+
};
|
|
25
|
+
export declare function arcs(o: ArcsOptions): Mark;
|