@n1kname/charts 0.1.1 → 0.2.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 +4 -3
- package/src/index.mjs +1 -0
- package/src/lines.mjs +102 -0
- package/src/lines.test.mjs +47 -0
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/charts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./src/index.mjs",
|
|
8
8
|
"./donut": "./src/donut.mjs",
|
|
9
9
|
"./plot": "./src/plot.mjs",
|
|
10
|
-
"./theme": "./src/theme.mjs"
|
|
10
|
+
"./theme": "./src/theme.mjs",
|
|
11
|
+
"./lines": "./src/lines.mjs"
|
|
11
12
|
},
|
|
12
13
|
"dependencies": {
|
|
13
14
|
"@observablehq/plot": "0.6.17",
|
|
@@ -26,6 +27,6 @@
|
|
|
26
27
|
"src"
|
|
27
28
|
],
|
|
28
29
|
"scripts": {
|
|
29
|
-
"check": "node src/donut.test.mjs"
|
|
30
|
+
"check": "node src/donut.test.mjs && node src/lines.test.mjs"
|
|
30
31
|
}
|
|
31
32
|
}
|
package/src/index.mjs
CHANGED
package/src/lines.mjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Lines on one axis, for a few series in the same unit. Returns Plot options without a
|
|
2
|
+
// style, so the same chart draws at build time (renderPlot) or in the browser (Plot.plot
|
|
3
|
+
// with the page's style). Each subject line ends in its name; with `legend`, for lines that
|
|
4
|
+
// end together, they don't, and a Legend above the chart names them instead.
|
|
5
|
+
//
|
|
6
|
+
// Rows are wide: one object per x, a number (or null for no value) per series key. A null
|
|
7
|
+
// breaks the line rather than joining across it.
|
|
8
|
+
|
|
9
|
+
import * as Plot from '@observablehq/plot';
|
|
10
|
+
import { chrome, tipStyle } from './theme.mjs';
|
|
11
|
+
|
|
12
|
+
const plain = (v, digits) => v.toFixed(digits);
|
|
13
|
+
|
|
14
|
+
export const lines = (
|
|
15
|
+
{
|
|
16
|
+
rows,
|
|
17
|
+
x,
|
|
18
|
+
series,
|
|
19
|
+
unit,
|
|
20
|
+
digits = 0,
|
|
21
|
+
format = plain,
|
|
22
|
+
height = 250,
|
|
23
|
+
// Which x labels to print; the rest stay positions on the scale. Gets (label, index, width).
|
|
24
|
+
tick = (label) => label,
|
|
25
|
+
// Each end 'auto' or a number.
|
|
26
|
+
y = ['auto', 'auto'],
|
|
27
|
+
// A mark on every point. Leave off for long, dense series.
|
|
28
|
+
dots = true,
|
|
29
|
+
legend = false,
|
|
30
|
+
},
|
|
31
|
+
width,
|
|
32
|
+
) => {
|
|
33
|
+
const has = (r, key) => typeof r[key] === 'number';
|
|
34
|
+
const values = rows.flatMap((r) => series.filter((s) => has(r, s.key)).map((s) => r[s.key]));
|
|
35
|
+
const domain = [y[0] === 'auto' ? Math.min(...values) : y[0], y[1] === 'auto' ? Math.max(...values) : y[1]];
|
|
36
|
+
|
|
37
|
+
// The tooltip names every series with a value at that x. A silent one, such as the
|
|
38
|
+
// dashed rest of another line, only where the line it `continues` has none.
|
|
39
|
+
const told = (r) =>
|
|
40
|
+
series.filter((s) => has(r, s.key) && (!s.silent || (s.continues && !has(r, s.continues))));
|
|
41
|
+
const tipped = rows.filter((r) => told(r).length);
|
|
42
|
+
const title = (r) => [r[x], ...told(r).map((s) => `${s.label} ${format(r[s.key], digits)}`), unit].join('\n');
|
|
43
|
+
|
|
44
|
+
const named = series
|
|
45
|
+
.map((s) => ({ s, name: s.endLabel ?? (s.silent ? false : s.label) }))
|
|
46
|
+
.filter(({ name }) => !legend && name !== false);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
width,
|
|
50
|
+
height,
|
|
51
|
+
marginTop: 10,
|
|
52
|
+
marginLeft: 44,
|
|
53
|
+
marginRight: legend ? 8 : 58,
|
|
54
|
+
marginBottom: 30,
|
|
55
|
+
x: { type: 'point', domain: rows.map((r) => r[x]), label: null, tickSize: 0, tickFormat: (v, i) => tick(v, i, width) },
|
|
56
|
+
y: { domain, nice: true, label: null, tickSize: 0, ticks: 5, tickFormat: (v) => format(v, 0) },
|
|
57
|
+
marks: [
|
|
58
|
+
Plot.gridY({ stroke: chrome.grid, strokeOpacity: 1, ticks: 5 }),
|
|
59
|
+
...series.map((s) =>
|
|
60
|
+
Plot.line(rows, {
|
|
61
|
+
x,
|
|
62
|
+
y: (r) => (has(r, s.key) ? r[s.key] : NaN),
|
|
63
|
+
stroke: s.color,
|
|
64
|
+
strokeWidth: s.width ?? 2,
|
|
65
|
+
strokeDasharray: s.dashed ? '4 3' : undefined,
|
|
66
|
+
}),
|
|
67
|
+
),
|
|
68
|
+
...(dots
|
|
69
|
+
? series.map((s) =>
|
|
70
|
+
Plot.dot(rows.filter((r) => has(r, s.key)), {
|
|
71
|
+
x,
|
|
72
|
+
y: s.key,
|
|
73
|
+
r: 2.5,
|
|
74
|
+
fill: s.dashed ? 'var(--c-surface)' : s.color,
|
|
75
|
+
stroke: s.color,
|
|
76
|
+
strokeWidth: s.dashed ? 1.5 : 0,
|
|
77
|
+
}),
|
|
78
|
+
)
|
|
79
|
+
: []),
|
|
80
|
+
...named.map(({ s, name }) => {
|
|
81
|
+
const last = rows.findLast((r) => has(r, s.key));
|
|
82
|
+
return Plot.text(last ? [last] : [], {
|
|
83
|
+
x,
|
|
84
|
+
y: s.key,
|
|
85
|
+
text: () => name,
|
|
86
|
+
dx: 7,
|
|
87
|
+
textAnchor: 'start',
|
|
88
|
+
fill: chrome.text,
|
|
89
|
+
fontWeight: 600,
|
|
90
|
+
fontSize: 11,
|
|
91
|
+
});
|
|
92
|
+
}),
|
|
93
|
+
Plot.ruleX(tipped, Plot.pointerX({ x, stroke: chrome.axis })),
|
|
94
|
+
...series
|
|
95
|
+
.filter((s) => !s.silent)
|
|
96
|
+
.map((s) =>
|
|
97
|
+
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 })),
|
|
98
|
+
),
|
|
99
|
+
Plot.tip(tipped, Plot.pointerX({ x, y: (r) => Math.max(...told(r).map((s) => r[s.key])), title, ...tipStyle })),
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Synthetic data only, like everything in this repo.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { lines } from './lines.mjs';
|
|
4
|
+
import { renderPlot } from './plot.mjs';
|
|
5
|
+
|
|
6
|
+
const rows = [
|
|
7
|
+
{ week: 'W1', a: 2, b: 5 },
|
|
8
|
+
{ week: 'W2', a: 3, b: null },
|
|
9
|
+
{ week: 'W3', a: 4, b: 7 },
|
|
10
|
+
{ week: 'W4', a: 6, b: 8, b_open: 8 },
|
|
11
|
+
{ week: 'W5', a: 5, b_open: 9 },
|
|
12
|
+
];
|
|
13
|
+
const series = [
|
|
14
|
+
{ key: 'a', label: 'Alder', color: 'var(--c-chart-1)' },
|
|
15
|
+
{ key: 'b', label: 'Birch', color: 'var(--c-chart-2)' },
|
|
16
|
+
{ key: 'b_open', label: 'Birch, not settled', color: 'var(--c-chart-2)', dashed: true, silent: true, continues: 'b', endLabel: false },
|
|
17
|
+
];
|
|
18
|
+
const spec = { rows, x: 'week', series, unit: 'units', digits: 1, tick: (v, i) => (i % 2 ? '' : v) };
|
|
19
|
+
|
|
20
|
+
const options = lines(spec, 600);
|
|
21
|
+
const svg = renderPlot(options);
|
|
22
|
+
const ticks = [...svg.matchAll(/<text[^>]*>(W\d)<\/text>/g)].map((m) => m[1]);
|
|
23
|
+
assert.deepEqual(ticks, ['W1', 'W3', 'W5'], 'only the ticks the tick function prints');
|
|
24
|
+
assert.ok(/>Alder</.test(svg) && />Birch</.test(svg), 'subject lines end in their name');
|
|
25
|
+
assert.ok(!/Birch, not settled/.test(svg), 'a silent line has no name at its end');
|
|
26
|
+
assert.ok(svg.includes('stroke-dasharray="4 3"'), 'a dashed series is dashed');
|
|
27
|
+
assert.ok(!/#[0-9a-f]{6}/i.test(svg), 'no hex: colours are tokens');
|
|
28
|
+
|
|
29
|
+
// A null breaks the line: Birch is drawn as two pieces, W1 alone and W3 to W4.
|
|
30
|
+
const birch = svg.match(/<g(?![^>]*dasharray)[^>]*stroke="var\(--c-chart-2\)" fill="none"><path d="([^"]+)"/)[1];
|
|
31
|
+
assert.equal((birch.match(/M/g) ?? []).length, 2, 'a null breaks the line instead of joining across it');
|
|
32
|
+
|
|
33
|
+
// The tooltip: every series with a value, and a silent one only where its line has none.
|
|
34
|
+
const tip = options.marks.at(-1);
|
|
35
|
+
const title = (i) => tip.channels.title.value(rows[i]);
|
|
36
|
+
assert.equal(title(0), 'W1\nAlder 2.0\nBirch 5.0\nunits');
|
|
37
|
+
assert.equal(title(1), 'W2\nAlder 3.0\nunits', 'a missing value is left out, not shown as 0');
|
|
38
|
+
assert.equal(title(3), 'W4\nAlder 6.0\nBirch 8.0\nunits', 'a silent series stays out where its line has a value');
|
|
39
|
+
assert.equal(title(4), 'W5\nAlder 5.0\nBirch, not settled 9.0\nunits', 'and shows where it does not');
|
|
40
|
+
|
|
41
|
+
const legend = renderPlot(lines({ ...spec, legend: true }, 600));
|
|
42
|
+
assert.ok(!/>Alder</.test(legend), 'with a legend the lines carry no names');
|
|
43
|
+
|
|
44
|
+
const floor = lines({ ...spec, y: [0, 'auto'] }, 600);
|
|
45
|
+
assert.equal(floor.y.domain[0], 0, 'a fixed end of the y domain is kept');
|
|
46
|
+
|
|
47
|
+
console.log('lines: all assertions pass');
|