@n1kname/charts 0.8.0 → 0.9.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/package.json +5 -3
- package/src/drawn.mjs +70 -0
- package/src/months.mjs +46 -0
- package/src/months.test.mjs +23 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/charts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -14,7 +14,9 @@
|
|
|
14
14
|
"./enter": "./src/enter.mjs",
|
|
15
15
|
"./update": "./src/update.mjs",
|
|
16
16
|
"./pointer": "./src/pointer.mjs",
|
|
17
|
-
"./pointer.css": "./src/pointer.css"
|
|
17
|
+
"./pointer.css": "./src/pointer.css",
|
|
18
|
+
"./months": "./src/months.mjs",
|
|
19
|
+
"./drawn": "./src/drawn.mjs"
|
|
18
20
|
},
|
|
19
21
|
"dependencies": {
|
|
20
22
|
"@observablehq/plot": "0.6.17",
|
|
@@ -33,6 +35,6 @@
|
|
|
33
35
|
"src"
|
|
34
36
|
],
|
|
35
37
|
"scripts": {
|
|
36
|
-
"check": "node src/donut.test.mjs && node src/lines.test.mjs && node src/bars.test.mjs && node src/heatmap.test.mjs"
|
|
38
|
+
"check": "node src/donut.test.mjs && node src/lines.test.mjs && node src/bars.test.mjs && node src/heatmap.test.mjs && node src/months.test.mjs"
|
|
37
39
|
}
|
|
38
40
|
}
|
package/src/drawn.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// A chart drawn in the page at the width its box has, and again when that width changes, so its
|
|
2
|
+
// text keeps its real size. The box speaks for the chart (role="img" and a label), so the SVG is
|
|
3
|
+
// hidden and Plot's own labels on its parts move to data-mark, where checks find them and screen
|
|
4
|
+
// readers don't. It arrives once (enter), and changes in place when drawn again with new figures
|
|
5
|
+
// on a page that asks for it (data-chart-motion="update" on an ancestor). `after` runs after every
|
|
6
|
+
// drawing, such as attaching the pointer, which a new SVG needs again.
|
|
7
|
+
//
|
|
8
|
+
// While the page is still arriving (a view transition, as with page-change.css, is running), the
|
|
9
|
+
// first drawing waits for it to finish: drawing a chart takes the frames the nav pill needs to
|
|
10
|
+
// glide. Called again meanwhile, it keeps the newest figures for that drawing. Returns the first
|
|
11
|
+
// drawing's svg, or a promise of it while that waits; a later call returns nothing.
|
|
12
|
+
//
|
|
13
|
+
// import { drawn } from '@n1kname/charts/drawn';
|
|
14
|
+
// drawn(box, (width) => Plot.plot({ style, ...bars({ ...spec, pointer: false }, width) }), {
|
|
15
|
+
// key: (m) => m.slice(5, 7),
|
|
16
|
+
// after: () => pointer(box, { rows, x: 'month', series, format }),
|
|
17
|
+
// });
|
|
18
|
+
import { enter } from './enter.mjs';
|
|
19
|
+
import { snapshot, update } from './update.mjs';
|
|
20
|
+
|
|
21
|
+
const boxes = new WeakMap();
|
|
22
|
+
|
|
23
|
+
export const drawn = (box, plot, { key, after } = {}) => {
|
|
24
|
+
const known = boxes.get(box);
|
|
25
|
+
const state = known ?? { width: box.clientWidth };
|
|
26
|
+
state.plot = plot;
|
|
27
|
+
state.after = after;
|
|
28
|
+
if (state.waiting) {
|
|
29
|
+
state.key = key;
|
|
30
|
+
return state.waiting;
|
|
31
|
+
}
|
|
32
|
+
const arriving = !known && document.activeViewTransition;
|
|
33
|
+
if (arriving) {
|
|
34
|
+
boxes.set(box, state);
|
|
35
|
+
state.key = key;
|
|
36
|
+
state.waiting = arriving.finished.catch(() => {}).then(() => {
|
|
37
|
+
boxes.delete(box);
|
|
38
|
+
return drawn(box, state.plot, { key: state.key, after: state.after });
|
|
39
|
+
});
|
|
40
|
+
return state.waiting;
|
|
41
|
+
}
|
|
42
|
+
const render = () => {
|
|
43
|
+
const svg = state.plot(box.clientWidth);
|
|
44
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
45
|
+
svg.querySelectorAll('[aria-label]').forEach((n) => {
|
|
46
|
+
n.dataset.mark = n.getAttribute('aria-label');
|
|
47
|
+
n.removeAttribute('aria-label');
|
|
48
|
+
});
|
|
49
|
+
box.replaceChildren(svg);
|
|
50
|
+
state.after?.();
|
|
51
|
+
return svg;
|
|
52
|
+
};
|
|
53
|
+
const moves = known && box.closest('[data-chart-motion="update"]');
|
|
54
|
+
const before = moves ? snapshot(box.querySelector('svg'), state.key) : null;
|
|
55
|
+
state.key = key;
|
|
56
|
+
const svg = render();
|
|
57
|
+
if (known) {
|
|
58
|
+
if (moves) update(before, svg, key);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
boxes.set(box, state);
|
|
62
|
+
if (svg.querySelector('[class^="enter-"]')) enter(svg);
|
|
63
|
+
new ResizeObserver(() => {
|
|
64
|
+
if (box.clientWidth !== state.width) {
|
|
65
|
+
state.width = box.clientWidth;
|
|
66
|
+
render();
|
|
67
|
+
}
|
|
68
|
+
}).observe(box);
|
|
69
|
+
return svg;
|
|
70
|
+
};
|
package/src/months.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Monthly series: every month on the axis, the gaps kept as gaps, the labels chosen by the room each
|
|
2
|
+
// month has, and bars that touch once they are too thin for a gap. For bars() and lines() over
|
|
3
|
+
// "YYYY-MM" steps, at build time or in the page.
|
|
4
|
+
|
|
5
|
+
const LONG = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
|
|
6
|
+
const SHORT = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
7
|
+
export const isMonth = (m) => /^\d{4}-\d{2}$/.test(m);
|
|
8
|
+
|
|
9
|
+
// "2026-04" → "April 2026", for a tooltip or a table.
|
|
10
|
+
export const fullMonth = (m) => (isMonth(m) ? `${LONG[Number(m.slice(5)) - 1]} ${m.slice(0, 4)}` : m);
|
|
11
|
+
|
|
12
|
+
// Every month from the first row's to the last's, so a month without figures is a gap on the axis,
|
|
13
|
+
// not a closed-up step. A missing month is { [key]: month } and nothing else.
|
|
14
|
+
export const fillMonths = (rows, key = 'month') => {
|
|
15
|
+
if (!rows.length) return rows;
|
|
16
|
+
const by = new Map(rows.map((r) => [r[key], r]));
|
|
17
|
+
let [y, m] = rows[0][key].split('-').map(Number);
|
|
18
|
+
const [y1, m1] = rows[rows.length - 1][key].split('-').map(Number);
|
|
19
|
+
const out = [];
|
|
20
|
+
while (y < y1 || (y === y1 && m <= m1)) {
|
|
21
|
+
const k = `${y}-${String(m).padStart(2, '0')}`;
|
|
22
|
+
out.push(by.get(k) ?? { [key]: k });
|
|
23
|
+
if (++m > 12) [y, m] = [y + 1, 1];
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Which months the axis names, by the room each has: every month, quarter, half year, January, or
|
|
29
|
+
// every second, third, fifth or tenth January, the first whose labels don't touch. The first named
|
|
30
|
+
// month and every January carry the year on a second line, so the axis always says which year it
|
|
31
|
+
// is. Takes the months in order and the chart's side margins; gives bars' and lines' `tick`.
|
|
32
|
+
const ROOM = 40;
|
|
33
|
+
const STRIDES = [1, 3, 6, 12, 24, 36, 60, 120];
|
|
34
|
+
export const monthTicks = (months, marginLeft, marginRight = 4) => (m, i, width) => {
|
|
35
|
+
if (!isMonth(m)) return m;
|
|
36
|
+
const step = (width - marginLeft - marginRight) / months.length;
|
|
37
|
+
const every = STRIDES.find((n) => step * n >= ROOM) ?? STRIDES.at(-1);
|
|
38
|
+
const first = Number(months[0]?.slice(0, 4));
|
|
39
|
+
const named = (k) => (every <= 12 ? (Number(k.slice(5)) - 1) % every === 0 : k.endsWith('-01') && (Number(k.slice(0, 4)) - first) % (every / 12) === 0);
|
|
40
|
+
if (!named(m)) return '';
|
|
41
|
+
const name = SHORT[Number(m.slice(5)) - 1];
|
|
42
|
+
return m.endsWith('-01') || months.find(named) === m ? `${name}\n${m.slice(0, 4)}` : name;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Bars too thin for a gap between them touch instead: a sliver of gap only blurs them.
|
|
46
|
+
export const padFor = (width, n, margins = 44, padding = 0.15) => ((width - margins) / n < 5 ? 0 : padding);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Synthetic months only.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { fillMonths, fullMonth, monthTicks, padFor } from './months.mjs';
|
|
4
|
+
|
|
5
|
+
assert.equal(fullMonth('2026-04'), 'April 2026');
|
|
6
|
+
assert.equal(fullMonth('Q1'), 'Q1', 'anything else as it is');
|
|
7
|
+
|
|
8
|
+
const filled = fillMonths([{ month: '2025-11', v: 1 }, { month: '2026-02', v: 2 }]);
|
|
9
|
+
assert.deepEqual(filled.map((r) => r.month), ['2025-11', '2025-12', '2026-01', '2026-02'], 'every month between');
|
|
10
|
+
assert.deepEqual(filled[1], { month: '2025-12' }, 'a missing month has no figures');
|
|
11
|
+
assert.deepEqual(fillMonths([]), []);
|
|
12
|
+
|
|
13
|
+
const months = Array.from({ length: 36 }, (_, i) => `${2024 + Math.floor(i / 12)}-${String((i % 12) + 1).padStart(2, '0')}`);
|
|
14
|
+
const ticks = (width) => months.map((m, i) => monthTicks(months, 40)(m, i, width)).filter(Boolean);
|
|
15
|
+
// Wide, every quarter; narrow, every January, each with its year.
|
|
16
|
+
assert.deepEqual(ticks(900).slice(0, 3), ['Jan\n2024', 'Apr', 'Jul'], 'quarters when there is room');
|
|
17
|
+
assert.deepEqual(ticks(400), ['Jan\n2024', 'Jul', 'Jan\n2025', 'Jul', 'Jan\n2026', 'Jul'], 'half years with less');
|
|
18
|
+
assert.deepEqual(ticks(250), ['Jan\n2024', 'Jan\n2025', 'Jan\n2026'], 'Januaries with less still');
|
|
19
|
+
assert.ok(ticks(5000).length === 36, 'every month on a wide chart');
|
|
20
|
+
|
|
21
|
+
assert.equal(padFor(1000, 36), 0.15, 'room for a gap');
|
|
22
|
+
assert.equal(padFor(200, 154), 0, 'thin bars touch');
|
|
23
|
+
console.log('months: all assertions pass');
|