@n1kname/charts 0.6.0 → 0.7.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/package.json +3 -2
- package/src/update.mjs +93 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/charts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"./lines": "./src/lines.mjs",
|
|
12
12
|
"./bars": "./src/bars.mjs",
|
|
13
13
|
"./heatmap": "./src/heatmap.mjs",
|
|
14
|
-
"./enter": "./src/enter.mjs"
|
|
14
|
+
"./enter": "./src/enter.mjs",
|
|
15
|
+
"./update": "./src/update.mjs"
|
|
15
16
|
},
|
|
16
17
|
"dependencies": {
|
|
17
18
|
"@observablehq/plot": "0.6.17",
|
package/src/update.mjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// A chart changing in place when its figures change, such as a new year in a filter. `enter`
|
|
2
|
+
// plays once, the first time a chart is drawn; this plays on every redraw with new figures after
|
|
3
|
+
// that, and never on a resize.
|
|
4
|
+
//
|
|
5
|
+
// Each bar is known by what it stands for: its series and its place on the x axis, through a key
|
|
6
|
+
// the page gives (by default the x value itself). A year's January and the next year's January
|
|
7
|
+
// share a key when the page keys months by their month of the year, so stepping through years
|
|
8
|
+
// moves each month's bar from its old height to its new one on the spring, and the eye follows
|
|
9
|
+
// what grew and what shrank. A bar with no match grows from the zero line, as on arrival, so
|
|
10
|
+
// months that are not the same are never shown turning into each other. The axis words fade in.
|
|
11
|
+
// Nothing moves under reduced motion.
|
|
12
|
+
//
|
|
13
|
+
// Browser only, on the Web Animations API, for bars made by `bars` and drawn with Plot in the
|
|
14
|
+
// page (the bars must be the plot's own `svg`, which knows its x scale).
|
|
15
|
+
//
|
|
16
|
+
// const before = snapshot(box.querySelector('svg'), key);
|
|
17
|
+
// box.replaceChildren(svg);
|
|
18
|
+
// update(before, svg, key);
|
|
19
|
+
const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
20
|
+
|
|
21
|
+
// A motion token in milliseconds. The tokens are written in seconds (".34s"), so they are read
|
|
22
|
+
// with their unit.
|
|
23
|
+
export const ms = (name, fallback) => {
|
|
24
|
+
const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
25
|
+
const n = parseFloat(v);
|
|
26
|
+
if (Number.isNaN(n)) return fallback;
|
|
27
|
+
return v.endsWith('ms') ? n : v.endsWith('s') ? n * 1000 : n;
|
|
28
|
+
};
|
|
29
|
+
const ease = (name, fallback) => getComputedStyle(document.documentElement).getPropertyValue(name).trim() || fallback;
|
|
30
|
+
|
|
31
|
+
// Each bar with the x value it stands for, found from where it sits on the band scale.
|
|
32
|
+
const keyed = (svg, key = String) => {
|
|
33
|
+
const scale = svg.scale?.('x');
|
|
34
|
+
const domain = scale?.domain ?? [];
|
|
35
|
+
const half = (scale?.bandwidth ?? 0) / 2;
|
|
36
|
+
const centres = domain.map((d) => scale.apply(d) + half);
|
|
37
|
+
const out = new Map();
|
|
38
|
+
svg.querySelectorAll('.enter-bar').forEach((g, series) =>
|
|
39
|
+
[...g.children].forEach((el) => {
|
|
40
|
+
const b = el.getBBox();
|
|
41
|
+
const mid = b.x + b.width / 2;
|
|
42
|
+
let at = -1;
|
|
43
|
+
let best = Infinity;
|
|
44
|
+
centres.forEach((c, i) => {
|
|
45
|
+
const d = Math.abs(c - mid);
|
|
46
|
+
if (d < best) [best, at] = [d, i];
|
|
47
|
+
});
|
|
48
|
+
const x = at < 0 ? `#${out.size}` : key(domain[at]);
|
|
49
|
+
out.set(`${series}|${x}`, { el, box: b });
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
return out;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Where each bar is, before the chart is replaced.
|
|
56
|
+
export const snapshot = (svg, key) => (svg ? keyed(svg, key) : null);
|
|
57
|
+
|
|
58
|
+
export const update = (before, svg, key) => {
|
|
59
|
+
if (!before || reduced()) return;
|
|
60
|
+
const spring = { duration: ms('--motion-spring', 340), easing: ease('--ease-spring', 'ease-out') };
|
|
61
|
+
const base = { duration: ms('--motion-base', 180), easing: ease('--ease-out', 'ease-out') };
|
|
62
|
+
const after = keyed(svg, key);
|
|
63
|
+
if (!after.size) {
|
|
64
|
+
svg.animate([{ opacity: 0 }, { opacity: 1 }], base);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const zero = Math.max(...[...after.values()].map(({ box }) => box.y + box.height));
|
|
68
|
+
for (const [k, { el, box }] of after) {
|
|
69
|
+
if (box.height < 0.5 || box.width < 0.5) continue;
|
|
70
|
+
const old = before.get(k)?.box;
|
|
71
|
+
if (old) {
|
|
72
|
+
// From the old box to the new one: scaled about the new top left, then moved there.
|
|
73
|
+
el.style.transformBox = 'fill-box';
|
|
74
|
+
el.style.transformOrigin = '0 0';
|
|
75
|
+
const from = `translate(${old.x - box.x}px, ${old.y - box.y}px) scale(${old.width / box.width}, ${old.height / box.height})`;
|
|
76
|
+
el.animate([{ transform: from }, { transform: 'none' }], spring);
|
|
77
|
+
} else {
|
|
78
|
+
el.style.transformBox = 'view-box';
|
|
79
|
+
el.style.transformOrigin = `0 ${zero}px`;
|
|
80
|
+
el.animate([{ transform: 'scaleY(0)' }, { transform: 'none' }], spring);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
svg.querySelectorAll('[data-mark$="tick label"], [data-mark$="grid"]').forEach((n) => n.animate([{ opacity: 0 }, { opacity: 1 }], base));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// A figure that changes settles in place: it fades up from a little below, never counts.
|
|
87
|
+
export const settle = (el) => {
|
|
88
|
+
if (reduced()) return;
|
|
89
|
+
el.animate([{ opacity: 0.2, transform: 'translateY(3px)' }, { opacity: 1, transform: 'none' }], {
|
|
90
|
+
duration: ms('--motion-base', 180),
|
|
91
|
+
easing: ease('--ease-out', 'ease-out'),
|
|
92
|
+
});
|
|
93
|
+
};
|