@n1kname/charts 0.6.0 → 0.8.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 +5 -2
- package/src/bars.mjs +5 -2
- package/src/bars.test.mjs +10 -0
- package/src/lines.mjs +14 -7
- package/src/lines.test.mjs +9 -0
- package/src/pointer.css +165 -0
- package/src/pointer.mjs +249 -0
- package/src/theme.mjs +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.8.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -11,7 +11,10 @@
|
|
|
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",
|
|
16
|
+
"./pointer": "./src/pointer.mjs",
|
|
17
|
+
"./pointer.css": "./src/pointer.css"
|
|
15
18
|
},
|
|
16
19
|
"dependencies": {
|
|
17
20
|
"@observablehq/plot": "0.6.17",
|
package/src/bars.mjs
CHANGED
|
@@ -50,6 +50,9 @@ export const bars = (
|
|
|
50
50
|
grouped = false,
|
|
51
51
|
// A run of steps set apart: { from, to, text }, the first and last x and why.
|
|
52
52
|
shade = null,
|
|
53
|
+
// Plot's own tooltip and hover band. Leave on for a chart drawn at build time; turn off when
|
|
54
|
+
// the page says what is under the pointer itself, with `pointer` from ./pointer.mjs.
|
|
55
|
+
pointer = true,
|
|
53
56
|
},
|
|
54
57
|
width,
|
|
55
58
|
) => {
|
|
@@ -170,7 +173,7 @@ export const bars = (
|
|
|
170
173
|
...shading,
|
|
171
174
|
Plot.gridY({ stroke: chrome.grid, strokeOpacity: 1, ticks: 5 }),
|
|
172
175
|
// The bar under the pointer, behind the bars.
|
|
173
|
-
Plot.barY(tipped, Plot.pointerX({ x, y1: () => domain[0], y2: () => domain[1], fill: 'var(--c-surface-hover)', inset: -2 })),
|
|
176
|
+
...(pointer ? [Plot.barY(tipped, Plot.pointerX({ x, y1: () => domain[0], y2: () => domain[1], fill: 'var(--c-surface-hover)', inset: -2 }))] : []),
|
|
174
177
|
...series.flatMap((s, i) => [
|
|
175
178
|
piece(s, i, pieces[i].filter((p) => !dashed(p.row)), false),
|
|
176
179
|
piece(s, i, pieces[i].filter((p) => dashed(p.row)), true),
|
|
@@ -180,7 +183,7 @@ export const bars = (
|
|
|
180
183
|
: []),
|
|
181
184
|
...tags,
|
|
182
185
|
Plot.ruleY([0], { stroke: chrome.axis, className: 'enter-zero' }),
|
|
183
|
-
Plot.tip(tipped, Plot.pointerX({ x, y: top, title: says, ...tipStyle })),
|
|
186
|
+
...(pointer ? [Plot.tip(tipped, Plot.pointerX({ x, y: top, title: says, ...tipStyle }))] : []),
|
|
184
187
|
],
|
|
185
188
|
};
|
|
186
189
|
};
|
package/src/bars.test.mjs
CHANGED
|
@@ -113,3 +113,13 @@ assert.equal(gTip.channels.title.value(pairs[2]), 'M3\nunits', 'an empty step sa
|
|
|
113
113
|
assert.ok(!renderPlot(bars({ ...spec, grouped: false }, 600)).includes('left out'), 'no shade unless asked');
|
|
114
114
|
|
|
115
115
|
console.log('bars: all assertions pass');
|
|
116
|
+
|
|
117
|
+
// pointer: false leaves out Plot's tooltip and hover band, for a page that draws its own.
|
|
118
|
+
{
|
|
119
|
+
const own = bars({ ...spec, pointer: false }, 600);
|
|
120
|
+
const names = (o) => o.marks.flat().filter(Boolean).map((m) => m.constructor.name);
|
|
121
|
+
assert.ok(names(options).includes('Tip'), "Plot's tip is there by default");
|
|
122
|
+
assert.ok(!names(own).includes('Tip'), 'no tip with pointer: false');
|
|
123
|
+
assert.ok(!own.marks.flat().some((m) => m?.fill === 'var(--c-surface-hover)'), 'no hover band with pointer: false');
|
|
124
|
+
assert.equal(names(options).length - names(own).length, 2, 'only those two are left out');
|
|
125
|
+
}
|
package/src/lines.mjs
CHANGED
|
@@ -33,6 +33,9 @@ export const lines = (
|
|
|
33
33
|
xTicks,
|
|
34
34
|
// A line at zero, for values that can fall below it.
|
|
35
35
|
zero = false,
|
|
36
|
+
// Plot's own tooltip, crosshair and dot. Leave on for a chart drawn at build time; turn off
|
|
37
|
+
// when the page says what is under the pointer itself, with `pointer` from ./pointer.mjs.
|
|
38
|
+
pointer = true,
|
|
36
39
|
// Words on the y axis, such as its unit, and the decimals its labels show.
|
|
37
40
|
yLabel = null,
|
|
38
41
|
yDigits = 0,
|
|
@@ -116,13 +119,17 @@ export const lines = (
|
|
|
116
119
|
className: 'enter-fade',
|
|
117
120
|
});
|
|
118
121
|
}),
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
...(pointer
|
|
123
|
+
? [
|
|
124
|
+
Plot.ruleX(tipped, Plot.pointerX({ x, stroke: chrome.axis })),
|
|
125
|
+
...series
|
|
126
|
+
.filter((s) => !s.silent)
|
|
127
|
+
.map((s) =>
|
|
128
|
+
Plot.dot(rows.filter((r) => has(r, s.key)), Plot.pointerX({ x, y: s.key, r: 4, fill: s.color, stroke: 'var(--c-surface)', strokeWidth: 2 })),
|
|
129
|
+
),
|
|
130
|
+
Plot.tip(tipped, Plot.pointerX({ x, y: (r) => Math.max(...told(r).map((s) => r[s.key])), title, ...tipStyle })),
|
|
131
|
+
]
|
|
132
|
+
: []),
|
|
126
133
|
],
|
|
127
134
|
};
|
|
128
135
|
};
|
package/src/lines.test.mjs
CHANGED
|
@@ -85,3 +85,12 @@ assert.equal(timed.marks.at(-1).channels.title.value(days[1]), 'Week of 2024-01-
|
|
|
85
85
|
assert.equal(lines({ ...spec, legend: true, marginRight: 24 }, 600).marginRight, 24, 'a page can give the last label room');
|
|
86
86
|
|
|
87
87
|
console.log('lines: all assertions pass');
|
|
88
|
+
|
|
89
|
+
// pointer: false leaves out Plot's tooltip, crosshair and dot, for a page that draws its own.
|
|
90
|
+
{
|
|
91
|
+
const own = lines({ ...spec, pointer: false }, 600);
|
|
92
|
+
const names = (o) => o.marks.flat().filter(Boolean).map((m) => m.constructor.name);
|
|
93
|
+
assert.ok(names(options).includes('Tip'), "Plot's tip is there by default");
|
|
94
|
+
assert.ok(!names(own).includes('Tip') && !names(own).includes('RuleX'), 'no tip or crosshair with pointer: false');
|
|
95
|
+
assert.ok(!own.marks.flat().some((m) => m?.r === 4), 'no pointer dot with pointer: false');
|
|
96
|
+
}
|
package/src/pointer.css
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/* What a chart says under the pointer (./pointer.mjs): the card, the focus marks, the dimming and
|
|
2
|
+
their motion. Tokens only, so it follows the theme. */
|
|
3
|
+
|
|
4
|
+
.chart-pointer-host {
|
|
5
|
+
position: relative;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.chart-pointer-host:focus-visible {
|
|
9
|
+
outline: 2px solid var(--c-focus);
|
|
10
|
+
outline-offset: 4px;
|
|
11
|
+
border-radius: var(--radius-sm);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.chart-pointer {
|
|
15
|
+
position: absolute;
|
|
16
|
+
inset: 0;
|
|
17
|
+
pointer-events: none;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.chart-pointer > * {
|
|
21
|
+
position: absolute;
|
|
22
|
+
left: 0;
|
|
23
|
+
top: 0;
|
|
24
|
+
opacity: 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.chart-pointer.is-on > * {
|
|
28
|
+
opacity: 1;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* The card: the step's name quiet, the figures bold in figures of one width. */
|
|
32
|
+
.chart-pointer-card {
|
|
33
|
+
min-width: 8.5rem;
|
|
34
|
+
padding: var(--sp-2) var(--sp-3);
|
|
35
|
+
border: 1px solid var(--c-border);
|
|
36
|
+
border-radius: var(--radius-md);
|
|
37
|
+
background: color-mix(in srgb, var(--c-surface) 92%, transparent);
|
|
38
|
+
backdrop-filter: blur(6px);
|
|
39
|
+
box-shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 8px 24px rgb(0 0 0 / 0.12);
|
|
40
|
+
font-family: var(--font-body);
|
|
41
|
+
font-size: var(--text-caption);
|
|
42
|
+
line-height: 1.35;
|
|
43
|
+
color: var(--c-text);
|
|
44
|
+
white-space: nowrap;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.chart-pointer-card p {
|
|
48
|
+
margin: 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.chart-pointer-title {
|
|
52
|
+
margin-bottom: var(--sp-1) !important;
|
|
53
|
+
color: var(--c-text-muted);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.chart-pointer-one {
|
|
57
|
+
font-size: var(--text-body);
|
|
58
|
+
font-weight: 600;
|
|
59
|
+
font-variant-numeric: tabular-nums;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.chart-pointer-card dl {
|
|
63
|
+
display: grid;
|
|
64
|
+
gap: 2px;
|
|
65
|
+
margin: 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.chart-pointer-card dl div {
|
|
69
|
+
display: flex;
|
|
70
|
+
justify-content: space-between;
|
|
71
|
+
gap: var(--sp-4);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.chart-pointer-card dt {
|
|
75
|
+
display: flex;
|
|
76
|
+
align-items: center;
|
|
77
|
+
gap: var(--sp-2);
|
|
78
|
+
color: var(--c-text-secondary);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.chart-pointer-card dd {
|
|
82
|
+
margin: 0;
|
|
83
|
+
font-weight: 600;
|
|
84
|
+
font-variant-numeric: tabular-nums;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.chart-pointer-card i {
|
|
88
|
+
display: inline-block;
|
|
89
|
+
width: 8px;
|
|
90
|
+
height: 8px;
|
|
91
|
+
border-radius: 2px;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* In a donut's hole: solid, centred, over the title. */
|
|
95
|
+
.chart-pointer-centre {
|
|
96
|
+
min-width: 0;
|
|
97
|
+
border-color: transparent;
|
|
98
|
+
background: var(--c-surface);
|
|
99
|
+
backdrop-filter: none;
|
|
100
|
+
box-shadow: none;
|
|
101
|
+
text-align: center;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/* In front of the chart: a rule through the step and a dot on the bar's top or the line. */
|
|
105
|
+
.chart-pointer-rule {
|
|
106
|
+
width: 1px;
|
|
107
|
+
background: var(--c-chart-axis);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.chart-pointer-dot {
|
|
111
|
+
width: 10px;
|
|
112
|
+
height: 10px;
|
|
113
|
+
margin: -5px 0 0 -5px;
|
|
114
|
+
border-radius: 50%;
|
|
115
|
+
background: var(--c-text);
|
|
116
|
+
box-shadow: 0 0 0 2px var(--c-surface);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/* Dimming: while one is pointed at, the other bars and slices step back to 55%, and so do the
|
|
120
|
+
swatches of the other rows in a donut's list. Their words keep full contrast. */
|
|
121
|
+
.chart-pointer-dims svg .enter-bar > *,
|
|
122
|
+
.chart-pointer-dims .ring svg path,
|
|
123
|
+
.chart-pointer-dims ul > li > :first-child {
|
|
124
|
+
transition: opacity var(--motion-base) var(--ease-out);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
.chart-pointer-dims.is-dimming svg .enter-bar > *:not(.is-on),
|
|
128
|
+
.chart-pointer-dims.is-dimming .ring svg path:not(.is-on),
|
|
129
|
+
.chart-pointer-dims.is-dimming ul > li:not(.is-on) > :first-child {
|
|
130
|
+
opacity: 0.55;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.chart-pointer-dims.is-dimming ul > li.is-on {
|
|
134
|
+
color: var(--c-text);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/* Gliding: the place moves on the spring, the figures change at once. */
|
|
138
|
+
.chart-pointer > * {
|
|
139
|
+
transition:
|
|
140
|
+
transform var(--motion-spring) var(--ease-spring),
|
|
141
|
+
opacity var(--motion-fast) var(--ease-out);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.chart-pointer.is-jump > * {
|
|
145
|
+
transition: opacity var(--motion-fast) var(--ease-out);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* Said aloud from the keyboard; not seen. */
|
|
149
|
+
.chart-pointer-live {
|
|
150
|
+
position: absolute;
|
|
151
|
+
width: 1px;
|
|
152
|
+
height: 1px;
|
|
153
|
+
overflow: hidden;
|
|
154
|
+
clip-path: inset(50%);
|
|
155
|
+
white-space: nowrap;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
@media (prefers-reduced-motion: reduce) {
|
|
159
|
+
.chart-pointer > *,
|
|
160
|
+
.chart-pointer-dims svg .enter-bar > *,
|
|
161
|
+
.chart-pointer-dims .ring svg path,
|
|
162
|
+
.chart-pointer-dims ul > li > :first-child {
|
|
163
|
+
transition: none;
|
|
164
|
+
}
|
|
165
|
+
}
|
package/src/pointer.mjs
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// What a chart says under the pointer: a small card in the page's own type, by the bar or point,
|
|
2
|
+
// with the step's name and each series' figure, and a focus on what is pointed at. Browser only,
|
|
3
|
+
// for charts drawn in the page with Plot (the chart's `svg` must know its scales) and for the
|
|
4
|
+
// donut ring. Styles in ./pointer.css.
|
|
5
|
+
//
|
|
6
|
+
// Why not Plot's tip: its text is plain and set in SVG, where Safari misplaces a web font, so it
|
|
7
|
+
// was set in a system mono; it can't carry a swatch, and it can't move. Here the card is HTML.
|
|
8
|
+
//
|
|
9
|
+
// - Bars: the other bars dim to 55% (`focus: 'dim'`, the default), or a rule runs through the bar in
|
|
10
|
+
// front with a dot on its top (`'rule'`), or both. Nothing is drawn behind the bars.
|
|
11
|
+
// - Lines: a rule through the step and a dot on the line, in front.
|
|
12
|
+
// - The card sits above a bar, centred; beside a bar too tall for that, and beside a point, on the
|
|
13
|
+
// side with room, so it never covers what it describes.
|
|
14
|
+
// - Motion: the card, rule and dot glide from step to step on the spring; the figures change at
|
|
15
|
+
// once, so reading is never delayed. It fades in fast and out a moment after the pointer leaves,
|
|
16
|
+
// so crossing a gap doesn't blink. Nothing moves under reduced motion.
|
|
17
|
+
// - Keyboard: the chart takes focus; the arrow keys step, Home and End go to the ends, Escape
|
|
18
|
+
// lets go. Each step is said in a polite live region beside the chart.
|
|
19
|
+
//
|
|
20
|
+
// It is a reading aid, not the record: every figure also needs a table.
|
|
21
|
+
//
|
|
22
|
+
// import { bars } from '@n1kname/charts/bars';
|
|
23
|
+
// import { pointer } from '@n1kname/charts/pointer';
|
|
24
|
+
// import '@n1kname/charts/pointer.css';
|
|
25
|
+
//
|
|
26
|
+
// box.replaceChildren(Plot.plot({ ...bars({ ...spec, pointer: false }, width) }));
|
|
27
|
+
// pointer(box, { rows, x: 'month', series, format: (v) => `${v} h`, label: (r) => r.month });
|
|
28
|
+
|
|
29
|
+
const el = (tag, cls, parent) => {
|
|
30
|
+
const n = document.createElement(tag);
|
|
31
|
+
if (cls) n.className = cls;
|
|
32
|
+
parent?.append(n);
|
|
33
|
+
return n;
|
|
34
|
+
};
|
|
35
|
+
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
36
|
+
const has = (r, key) => typeof r[key] === 'number';
|
|
37
|
+
|
|
38
|
+
// The live region, beside the box rather than in it: a box that speaks for its chart with
|
|
39
|
+
// role="img" has no children a screen reader reads.
|
|
40
|
+
const voice = (box) => {
|
|
41
|
+
if (box.nextElementSibling?.classList.contains('chart-pointer-live')) return box.nextElementSibling;
|
|
42
|
+
const live = document.createElement('p');
|
|
43
|
+
live.className = 'chart-pointer-live';
|
|
44
|
+
live.setAttribute('aria-live', 'polite');
|
|
45
|
+
box.after(live);
|
|
46
|
+
return live;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// A layer over the chart for the card and the focus marks. Fresh each time: a chart drawn again
|
|
50
|
+
// replaces its svg and calls this again.
|
|
51
|
+
const layer = (box) => {
|
|
52
|
+
box.classList.add('chart-pointer-host');
|
|
53
|
+
box.querySelector(':scope > .chart-pointer')?.remove();
|
|
54
|
+
const l = el('div', 'chart-pointer', box);
|
|
55
|
+
l.setAttribute('aria-hidden', 'true');
|
|
56
|
+
return l;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// The card's words: the step's name, then one figure, or a row per series with its swatch.
|
|
60
|
+
const words = (title, items) =>
|
|
61
|
+
`<p class="chart-pointer-title">${esc(title)}</p>` +
|
|
62
|
+
(items.length === 1 && !items[0].swatch
|
|
63
|
+
? `<p class="chart-pointer-one">${esc(items[0].value)}</p>`
|
|
64
|
+
: `<dl>${items.map((i) => `<div><dt><i style="background:${i.color}"></i>${esc(i.label)}</dt><dd>${esc(i.value)}</dd></div>`).join('')}</dl>`);
|
|
65
|
+
|
|
66
|
+
// Appearing, moving, leaving. The first showing is in place; after that, everything glides.
|
|
67
|
+
const presence = (l, onLeave) => {
|
|
68
|
+
let hide;
|
|
69
|
+
return {
|
|
70
|
+
show() {
|
|
71
|
+
clearTimeout(hide);
|
|
72
|
+
const first = !l.classList.contains('is-on');
|
|
73
|
+
l.classList.toggle('is-jump', first);
|
|
74
|
+
l.classList.add('is-on');
|
|
75
|
+
if (first) requestAnimationFrame(() => l.classList.remove('is-jump'));
|
|
76
|
+
return first;
|
|
77
|
+
},
|
|
78
|
+
leave() {
|
|
79
|
+
clearTimeout(hide);
|
|
80
|
+
hide = setTimeout(() => {
|
|
81
|
+
l.classList.remove('is-on');
|
|
82
|
+
onLeave?.();
|
|
83
|
+
}, 120);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Arrow keys, Home, End and Escape on a focusable box, over `count` steps.
|
|
89
|
+
const keys = (box, count, at, go, leave) => {
|
|
90
|
+
box.tabIndex = 0;
|
|
91
|
+
box.onkeydown = (e) => {
|
|
92
|
+
if (e.key === 'Escape') return leave();
|
|
93
|
+
const now = at();
|
|
94
|
+
const to = { ArrowRight: now + 1, ArrowDown: now + 1, ArrowLeft: now - 1, ArrowUp: now - 1, Home: 0, End: count - 1 }[e.key];
|
|
95
|
+
if (to === undefined) return;
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
go(now < 0 ? (e.key === 'ArrowLeft' || e.key === 'ArrowUp' ? count - 1 : 0) : Math.min(Math.max(to, 0), count - 1));
|
|
98
|
+
};
|
|
99
|
+
box.onblur = leave;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// box: the element holding the chart's svg. rows: the rows drawn, one per x. x: the rows' x key.
|
|
103
|
+
// series: [{ key, label, color }], as given to bars() or lines(). format: (value, series) → text.
|
|
104
|
+
// label: (row) → the step's name. kind: 'bars' or 'lines'. focus (bars): 'dim', 'rule' or 'both'.
|
|
105
|
+
export const pointer = (box, { rows, x, series, format, label = (r) => String(r[x]), kind = 'bars', focus = 'dim' }) => {
|
|
106
|
+
const steps = rows.map((r, i) => (series.some((s) => has(r, s.key)) ? i : -1)).filter((i) => i >= 0);
|
|
107
|
+
// The card lists a stack from the top down, as it is drawn.
|
|
108
|
+
const listed = kind === 'bars' ? [...series].reverse() : series;
|
|
109
|
+
const l = layer(box);
|
|
110
|
+
const live = voice(box);
|
|
111
|
+
const ruled = kind === 'lines' || focus !== 'dim';
|
|
112
|
+
const dims = kind === 'bars' && focus !== 'rule';
|
|
113
|
+
const rule = ruled ? el('div', 'chart-pointer-rule', l) : null;
|
|
114
|
+
const dot = ruled ? el('div', 'chart-pointer-dot', l) : null;
|
|
115
|
+
if (dot && kind === 'lines' && series.length === 1) dot.style.background = series[0].color;
|
|
116
|
+
const card = el('div', 'chart-pointer-card', l);
|
|
117
|
+
box.classList.toggle('chart-pointer-dims', dims);
|
|
118
|
+
let at = -1;
|
|
119
|
+
const p = presence(l, () => {
|
|
120
|
+
at = -1;
|
|
121
|
+
box.classList.remove('is-dimming');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const svg = () => box.querySelector('svg');
|
|
125
|
+
// A bar's top: what its positive pieces add up to. A step on lines: its highest value.
|
|
126
|
+
const top = (r) =>
|
|
127
|
+
kind === 'bars'
|
|
128
|
+
? series.reduce((s, k) => s + (has(r, k.key) && r[k.key] > 0 ? r[k.key] : 0), 0)
|
|
129
|
+
: Math.max(...series.filter((k) => has(r, k.key)).map((k) => r[k.key]));
|
|
130
|
+
const where = (i) => {
|
|
131
|
+
const s = svg();
|
|
132
|
+
const xs = s.scale('x');
|
|
133
|
+
const ys = s.scale('y');
|
|
134
|
+
const r = s.getBoundingClientRect();
|
|
135
|
+
const b = box.getBoundingClientRect();
|
|
136
|
+
const [y0, y1] = [Math.min(...ys.range), Math.max(...ys.range)].map((v) => v + r.top - b.top);
|
|
137
|
+
return {
|
|
138
|
+
cx: r.left - b.left + xs.apply(rows[i][x]) + (xs.bandwidth ?? 0) / 2,
|
|
139
|
+
bw: xs.bandwidth ?? 0,
|
|
140
|
+
y: r.top - b.top + ys.apply(top(rows[i])),
|
|
141
|
+
y0,
|
|
142
|
+
y1,
|
|
143
|
+
width: b.width,
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const show = (i, fromKeys) => {
|
|
148
|
+
if (i < 0) return;
|
|
149
|
+
at = i;
|
|
150
|
+
const r = rows[i];
|
|
151
|
+
const items = listed.filter((s) => has(r, s.key)).map((s) => ({ label: s.label, color: s.color, value: format(r[s.key], s), swatch: series.length > 1 }));
|
|
152
|
+
card.innerHTML = words(label(r), items);
|
|
153
|
+
const q = where(i);
|
|
154
|
+
if (ruled) {
|
|
155
|
+
rule.style.transform = `translateX(${q.cx}px)`;
|
|
156
|
+
rule.style.top = `${q.y0}px`;
|
|
157
|
+
rule.style.height = `${q.y1 - q.y0}px`;
|
|
158
|
+
dot.style.transform = `translate(${q.cx}px, ${q.y}px)`;
|
|
159
|
+
}
|
|
160
|
+
if (dims) {
|
|
161
|
+
const left = box.getBoundingClientRect().left;
|
|
162
|
+
for (const m of box.querySelectorAll('svg .enter-bar > *')) {
|
|
163
|
+
const b = m.getBoundingClientRect();
|
|
164
|
+
m.classList.toggle('is-on', Math.abs(b.left + b.width / 2 - left - q.cx) <= Math.max(b.width, 2) / 2 + 1);
|
|
165
|
+
}
|
|
166
|
+
box.classList.add('is-dimming');
|
|
167
|
+
}
|
|
168
|
+
const cw = card.offsetWidth;
|
|
169
|
+
const ch = card.offsetHeight;
|
|
170
|
+
const side = (gap) => (q.cx + gap + cw > q.width ? q.cx - gap - cw : q.cx + gap);
|
|
171
|
+
let cx;
|
|
172
|
+
let cy;
|
|
173
|
+
if (kind === 'bars' && q.y - ch - 10 >= 0) {
|
|
174
|
+
cx = Math.min(Math.max(q.cx - cw / 2, 0), q.width - cw);
|
|
175
|
+
cy = q.y - ch - 10;
|
|
176
|
+
} else if (kind === 'bars') {
|
|
177
|
+
cx = side(q.bw / 2 + 8);
|
|
178
|
+
cy = q.y0;
|
|
179
|
+
} else {
|
|
180
|
+
cx = side(14);
|
|
181
|
+
cy = Math.min(Math.max(q.y - ch / 2, 0), q.y1 - ch);
|
|
182
|
+
}
|
|
183
|
+
card.style.transform = `translate(${cx}px, ${cy}px)`;
|
|
184
|
+
p.show();
|
|
185
|
+
if (fromKeys) live.textContent = `${label(r)}: ${items.map((t) => (items.length > 1 ? `${t.label} ${t.value}` : t.value)).join(', ')}`;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// The nearest step with a figure, as Plot's pointer picks it.
|
|
189
|
+
const nearest = (clientX) => {
|
|
190
|
+
const s = svg();
|
|
191
|
+
const xs = s.scale('x');
|
|
192
|
+
const left = s.getBoundingClientRect().left;
|
|
193
|
+
let best = -1;
|
|
194
|
+
let gap = Infinity;
|
|
195
|
+
for (const i of steps) {
|
|
196
|
+
const d = Math.abs(left + xs.apply(rows[i][x]) + (xs.bandwidth ?? 0) / 2 - clientX);
|
|
197
|
+
if (d < gap) [best, gap] = [i, d];
|
|
198
|
+
}
|
|
199
|
+
return best;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
box.onpointermove = (e) => show(nearest(e.clientX));
|
|
203
|
+
box.onpointerleave = () => p.leave();
|
|
204
|
+
keys(box, steps.length, () => steps.indexOf(at), (k) => show(steps[k], true), () => p.leave());
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// A donut drawn with donutRing, as in the Donut component: pointing at a slice, or at its row in
|
|
208
|
+
// the list beside the ring, dims the other slices and the other rows' swatches (their words keep
|
|
209
|
+
// full contrast), and the card sits in the ring's hole,
|
|
210
|
+
// over the title, with the slice's name, figure and share. root: the element holding the ring
|
|
211
|
+
// (`.ring` or the svg's parent) and the list (`ul`, one item per slice, optional). parts: the
|
|
212
|
+
// slices as given to donutRing. format: (value) → text.
|
|
213
|
+
export const pointerDonut = (root, { parts, format = String, share = true }) => {
|
|
214
|
+
const ring = root.querySelector('.ring') ?? root.querySelector('svg').parentElement;
|
|
215
|
+
const paths = [...ring.querySelectorAll('svg path')];
|
|
216
|
+
const items = [...(root.querySelector('ul')?.children ?? [])];
|
|
217
|
+
// The card says it now; the browser's own title tooltip would say it twice.
|
|
218
|
+
ring.querySelectorAll('svg title').forEach((t) => t.remove());
|
|
219
|
+
const l = layer(ring);
|
|
220
|
+
const card = el('div', 'chart-pointer-card chart-pointer-centre', l);
|
|
221
|
+
const live = voice(root);
|
|
222
|
+
root.classList.add('chart-pointer-dims');
|
|
223
|
+
const total = parts.reduce((s, d) => s + d.value, 0);
|
|
224
|
+
let at = -1;
|
|
225
|
+
const p = presence(l, () => {
|
|
226
|
+
at = -1;
|
|
227
|
+
root.classList.remove('is-dimming');
|
|
228
|
+
});
|
|
229
|
+
const show = (i, fromKeys) => {
|
|
230
|
+
at = i;
|
|
231
|
+
const d = parts[i];
|
|
232
|
+
const pct = `${Math.round((d.value / total) * 100)}%`;
|
|
233
|
+
const value = d.valueLabel ?? format(d.value);
|
|
234
|
+
card.innerHTML = words(d.label, [{ label: d.label, color: d.color ?? 'var(--c-chart-neutral)', value: share ? `${value} · ${pct}` : value }]);
|
|
235
|
+
paths.forEach((path, k) => path.classList.toggle('is-on', k === i));
|
|
236
|
+
items.forEach((li, k) => li.classList.toggle('is-on', k === i));
|
|
237
|
+
root.classList.add('is-dimming');
|
|
238
|
+
const c = ring.getBoundingClientRect().width / 2;
|
|
239
|
+
card.style.transform = `translate(${c - card.offsetWidth / 2}px, ${c - card.offsetHeight / 2}px)`;
|
|
240
|
+
p.show();
|
|
241
|
+
if (fromKeys) live.textContent = `${d.label}: ${value}${share ? `, ${pct}` : ''}`;
|
|
242
|
+
};
|
|
243
|
+
[...paths, ...items].forEach((n, k) => {
|
|
244
|
+
const i = k % paths.length;
|
|
245
|
+
n.addEventListener('pointerenter', () => show(i));
|
|
246
|
+
n.addEventListener('pointerleave', () => p.leave());
|
|
247
|
+
});
|
|
248
|
+
keys(root, parts.length, () => at, (i) => show(i, true), () => p.leave());
|
|
249
|
+
};
|
package/src/theme.mjs
CHANGED
|
@@ -19,8 +19,9 @@ export const chrome = {
|
|
|
19
19
|
textMuted: v('text-muted'),
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
//
|
|
23
|
-
//
|
|
22
|
+
// Plot's own tooltip, for charts drawn at build time: in a system font, because Safari misplaces
|
|
23
|
+
// centred SVG text in a web font. A chart drawn in the page can say it better with `pointer`
|
|
24
|
+
// (./pointer.mjs): an HTML card in the page's type that glides. Every value also needs a table.
|
|
24
25
|
export const tipStyle = {
|
|
25
26
|
fill: 'var(--c-surface)',
|
|
26
27
|
stroke: 'var(--c-border-strong)',
|
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
|
+
};
|