@n1kname/charts 0.14.0 → 0.15.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 +1 -1
- package/src/lines.mjs +30 -2
- package/src/lines.test.mjs +17 -0
package/package.json
CHANGED
package/src/lines.mjs
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
// Rows are wide: one object per x, a number (or null for no value) per series key. A null
|
|
7
7
|
// breaks the line rather than joining across it. With `time`, x is a Date per row and the
|
|
8
8
|
// steps need not be even, such as weeks with one missing.
|
|
9
|
+
//
|
|
10
|
+
// A band shades the range between two keys, such as a benchmark envelope from its lowest to its
|
|
11
|
+
// highest value, behind the lines, and ends in its own label.
|
|
9
12
|
|
|
10
13
|
import * as Plot from '@observablehq/plot';
|
|
11
14
|
import { chrome, tipStyle } from './theme.mjs';
|
|
@@ -45,11 +48,14 @@ export const lines = (
|
|
|
45
48
|
marginTop = 10,
|
|
46
49
|
// Room on the right: by default for the end labels, or a little for the last x label with a legend.
|
|
47
50
|
marginRight = legend ? 8 : 58,
|
|
51
|
+
// Ranges shaded behind the lines: { low, high, label, color }, two keys per row.
|
|
52
|
+
bands = [],
|
|
48
53
|
},
|
|
49
54
|
width,
|
|
50
55
|
) => {
|
|
51
56
|
const has = (r, key) => typeof r[key] === 'number';
|
|
52
|
-
const
|
|
57
|
+
const keys = [...series.map((s) => s.key), ...bands.flatMap((b) => [b.low, b.high])];
|
|
58
|
+
const values = rows.flatMap((r) => keys.filter((k) => has(r, k)).map((k) => r[k]));
|
|
53
59
|
const domain = [y[0] === 'auto' ? Math.min(...values) : y[0], y[1] === 'auto' ? Math.max(...values) : y[1]];
|
|
54
60
|
|
|
55
61
|
// The tooltip names every series with a value at that x. A silent one, such as the
|
|
@@ -60,7 +66,8 @@ export const lines = (
|
|
|
60
66
|
...series.filter((s) => s.silent && s.continues && has(r, s.key) && !has(r, s.continues)),
|
|
61
67
|
];
|
|
62
68
|
const tipped = rows.filter((r) => told(r).length);
|
|
63
|
-
const
|
|
69
|
+
const ranged = (r) => bands.filter((b) => has(r, b.low) && has(r, b.high)).map((b) => `${b.label} ${format(r[b.low], digits)}–${format(r[b.high], digits)}`);
|
|
70
|
+
const title = says ?? ((r) => [r[x], ...told(r).map((s) => `${s.label} ${format(r[s.key], digits)}`), ...ranged(r), unit].join('\n'));
|
|
64
71
|
|
|
65
72
|
const named = series
|
|
66
73
|
.map((s) => ({ s, name: s.endLabel ?? (s.silent ? false : s.label) }))
|
|
@@ -80,6 +87,27 @@ export const lines = (
|
|
|
80
87
|
marks: [
|
|
81
88
|
Plot.gridY({ stroke: chrome.grid, strokeOpacity: 1, ticks: 5 }),
|
|
82
89
|
...(zero ? [Plot.ruleY([0], { stroke: chrome.axis })] : []),
|
|
90
|
+
...bands.flatMap((b) => {
|
|
91
|
+
const inside = rows.filter((r) => has(r, b.low) && has(r, b.high));
|
|
92
|
+
const last = inside.at(-1);
|
|
93
|
+
return [
|
|
94
|
+
Plot.areaY(inside, { x, y1: b.low, y2: b.high, fill: b.color ?? chrome.neutral, fillOpacity: b.opacity ?? 0.38, className: 'enter-fade' }),
|
|
95
|
+
...(legend || !b.label
|
|
96
|
+
? []
|
|
97
|
+
: [
|
|
98
|
+
Plot.text(last ? [last] : [], {
|
|
99
|
+
x,
|
|
100
|
+
y: (r) => (r[b.low] + r[b.high]) / 2,
|
|
101
|
+
text: () => b.endLabel ?? b.label,
|
|
102
|
+
dx: 7,
|
|
103
|
+
textAnchor: 'start',
|
|
104
|
+
fill: chrome.textMuted,
|
|
105
|
+
fontSize: 11,
|
|
106
|
+
className: 'enter-fade',
|
|
107
|
+
}),
|
|
108
|
+
]),
|
|
109
|
+
];
|
|
110
|
+
}),
|
|
83
111
|
...series.map((s) =>
|
|
84
112
|
Plot.line(rows, {
|
|
85
113
|
x,
|
package/src/lines.test.mjs
CHANGED
|
@@ -94,3 +94,20 @@ console.log('lines: all assertions pass');
|
|
|
94
94
|
assert.ok(!names(own).includes('Tip') && !names(own).includes('RuleX'), 'no tip or crosshair with pointer: false');
|
|
95
95
|
assert.ok(!own.marks.flat().some((m) => m?.r === 4), 'no pointer dot with pointer: false');
|
|
96
96
|
}
|
|
97
|
+
|
|
98
|
+
// Bands: a range shaded behind the lines, in the domain, in the tooltip, with an end label.
|
|
99
|
+
{
|
|
100
|
+
const rows = [
|
|
101
|
+
{ year: '2012', top: 100, lo: 100, hi: 100 },
|
|
102
|
+
{ year: '2013', top: 121, lo: 112, hi: 116 },
|
|
103
|
+
{ year: '2014', top: 146, lo: 125, hi: 135 },
|
|
104
|
+
];
|
|
105
|
+
const o = lines({ rows, x: 'year', unit: 'index', series: [{ key: 'top', label: 'Top quartile', color: 'var(--c-chart-2)', endLabel: '21%' }], bands: [{ low: 'lo', high: 'hi', label: 'Benchmarks', endLabel: '12–16%' }] }, 500);
|
|
106
|
+
const svg = renderPlot(o);
|
|
107
|
+
assert.ok(o.marks.some((m) => m.constructor.name === 'Area'), 'the band is an area');
|
|
108
|
+
assert.ok(svg.includes('>12–16%<'), 'the band ends in its label');
|
|
109
|
+
assert.ok(o.y.domain[1] >= 146 && o.y.domain[0] <= 100, 'the domain takes in the band and the lines');
|
|
110
|
+
const tip = o.marks.at(-1);
|
|
111
|
+
assert.equal(tip.channels.title.value(rows[1]), '2013\nTop quartile 121\nBenchmarks 112–116\nindex', 'the tooltip gives the band as a range');
|
|
112
|
+
console.log('lines: bands pass');
|
|
113
|
+
}
|