@n1kname/charts 0.12.0 → 0.13.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/spark.css +20 -0
- package/src/spark.mjs +32 -20
- package/src/spark.test.mjs +23 -0
package/package.json
CHANGED
package/src/spark.css
CHANGED
|
@@ -37,6 +37,21 @@
|
|
|
37
37
|
stroke-width: 1.5;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
/* The run compared against, such as last year's: a thin line in the muted text colour, under the
|
|
41
|
+
marks, so the run in colour reads as now and the grey one as then. */
|
|
42
|
+
.spark .spark-compare {
|
|
43
|
+
fill: none;
|
|
44
|
+
stroke: var(--c-text-muted);
|
|
45
|
+
stroke-opacity: 0.7;
|
|
46
|
+
stroke-width: 1.25;
|
|
47
|
+
stroke-linejoin: round;
|
|
48
|
+
stroke-linecap: round;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.spark .spark-compare-dot {
|
|
52
|
+
fill: var(--c-text-muted);
|
|
53
|
+
}
|
|
54
|
+
|
|
40
55
|
/* The reference: quiet, dashed, under the marks. */
|
|
41
56
|
.spark .spark-ref {
|
|
42
57
|
stroke: var(--c-text-muted);
|
|
@@ -59,6 +74,11 @@
|
|
|
59
74
|
fill: none;
|
|
60
75
|
}
|
|
61
76
|
|
|
77
|
+
.spark .spark-compare {
|
|
78
|
+
stroke: GrayText;
|
|
79
|
+
stroke-opacity: 1;
|
|
80
|
+
}
|
|
81
|
+
|
|
62
82
|
.spark .spark-ref {
|
|
63
83
|
stroke: GrayText;
|
|
64
84
|
stroke-opacity: 1;
|
package/src/spark.mjs
CHANGED
|
@@ -20,12 +20,16 @@ export const SPARK_KINDS = ['bar', 'line', 'area'];
|
|
|
20
20
|
// values: numbers, oldest first; a missing one (null) is a gap. kind: 'bar', 'line' or 'area'.
|
|
21
21
|
// reference: a value dashed across, drawn only when it falls inside the scale. width and height in
|
|
22
22
|
// px. label: what a screen reader hears; without one the spark is hidden from it, because the
|
|
23
|
-
// figure beside it says the same. last: a dot on the newest value of a line or area
|
|
24
|
-
//
|
|
25
|
-
// stand on zero.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
// figure beside it says the same. last: a dot on the newest value of a line or area, the newest
|
|
24
|
+
// one there is when the run ends in gaps. zero: false for a line or area scaled to its own
|
|
25
|
+
// values, for a rate that only moves a little; bars always stand on zero. compare: a second run
|
|
26
|
+
// over the same steps, such as the same months a year earlier, drawn as a quiet line under the
|
|
27
|
+
// marks and on the same scale, so the two can be read against each other.
|
|
28
|
+
export const sparkline = (values, { kind = 'bar', reference, width = 72, height = 44, label, last = false, zero = true, compare } = {}) => {
|
|
29
|
+
const other = Array.isArray(compare) ? compare : [];
|
|
30
|
+
if (other.length > values.length) values = [...values, ...Array(other.length - values.length).fill(null)];
|
|
31
|
+
const shown = [...values, ...other].filter(has);
|
|
32
|
+
if (!values.some(has)) return '';
|
|
29
33
|
if (!SPARK_KINDS.includes(kind)) throw new Error(`sparkline: kind is one of ${SPARK_KINDS.join(', ')}, not ${kind}`);
|
|
30
34
|
// From zero, with a little headroom so the top bar or the peak doesn't touch the edge. Below
|
|
31
35
|
// zero when a value is. Without zero, the values' own range with a tenth of it either side.
|
|
@@ -39,9 +43,27 @@ export const sparkline = (values, { kind = 'bar', reference, width = 72, height
|
|
|
39
43
|
const y = (v) => t + ih - ((v - lo) / (hi - lo || 1)) * ih;
|
|
40
44
|
// What bars stand on and an area closes on: zero, or the scale's foot when zero is below it.
|
|
41
45
|
const base = y(Math.max(lo, Math.min(0, hi)));
|
|
46
|
+
// Where step i stands across: a bar's middle, or a point along a line.
|
|
47
|
+
const band = iw / values.length;
|
|
48
|
+
const step = values.length > 1 ? iw / (values.length - 1) : 0;
|
|
49
|
+
const xAt = (i) => (kind === 'bar' ? l + band * i + band / 2 : values.length > 1 ? l + step * i : l + iw / 2);
|
|
50
|
+
const line = (pts) => pts.map(([px, py], i) => `${i ? 'L' : 'M'}${n(px)} ${n(py)}`).join('');
|
|
51
|
+
const runsOf = (vs) => {
|
|
52
|
+
const runs = [];
|
|
53
|
+
vs.forEach((v, i) => {
|
|
54
|
+
if (!has(v)) return runs.push(null);
|
|
55
|
+
const p = [xAt(i), y(v)];
|
|
56
|
+
const run = runs.at(-1);
|
|
57
|
+
if (run) run.push(p);
|
|
58
|
+
else runs.push([p]);
|
|
59
|
+
});
|
|
60
|
+
return runs.filter(Boolean);
|
|
61
|
+
};
|
|
62
|
+
const behind = runsOf(other.slice(0, values.length))
|
|
63
|
+
.map((pts) => (pts.length > 1 ? `<path class="spark-compare" d="${line(pts)}"/>` : `<circle class="spark-compare-dot" cx="${n(pts[0][0])}" cy="${n(pts[0][1])}" r="1.25"/>`))
|
|
64
|
+
.join('');
|
|
42
65
|
let marks = '';
|
|
43
66
|
if (kind === 'bar') {
|
|
44
|
-
const band = iw / values.length;
|
|
45
67
|
// Thin bars when there are many, so they stay apart.
|
|
46
68
|
const bw = Math.min(values.length > 8 ? 4 : 7, band * 0.8);
|
|
47
69
|
marks = values
|
|
@@ -50,24 +72,14 @@ export const sparkline = (values, { kind = 'bar', reference, width = 72, height
|
|
|
50
72
|
)
|
|
51
73
|
.join('');
|
|
52
74
|
} else {
|
|
53
|
-
const step = values.length > 1 ? iw / (values.length - 1) : 0;
|
|
54
75
|
// One run per stretch without a gap; one value alone sits in the middle.
|
|
55
|
-
const
|
|
56
|
-
values.forEach((v, i) => {
|
|
57
|
-
if (!has(v)) return runs.push(null);
|
|
58
|
-
const p = [values.length > 1 ? l + step * i : l + iw / 2, y(v)];
|
|
59
|
-
const run = runs.at(-1);
|
|
60
|
-
if (run) run.push(p);
|
|
61
|
-
else runs.push([p]);
|
|
62
|
-
});
|
|
63
|
-
const paths = runs.filter(Boolean);
|
|
64
|
-
const line = (pts) => pts.map(([px, py], i) => `${i ? 'L' : 'M'}${n(px)} ${n(py)}`).join('');
|
|
76
|
+
const paths = runsOf(values);
|
|
65
77
|
const fill = kind === 'area' ? paths.map((pts) => `<path class="spark-fill" d="${line(pts)}L${n(pts.at(-1)[0])} ${n(base)}L${n(pts[0][0])} ${n(base)}Z"/>`).join('') : '';
|
|
66
78
|
const stroke = paths.map((pts) => (pts.length > 1 ? `<path class="spark-stroke" d="${line(pts)}"/>` : `<circle class="spark-dot" cx="${n(pts[0][0])}" cy="${n(pts[0][1])}" r="1.5"/>`)).join('');
|
|
67
|
-
const end = last
|
|
79
|
+
const end = last ? paths.at(-1).at(-1) : null;
|
|
68
80
|
marks = fill + stroke + (end ? `<circle class="spark-dot spark-last" cx="${n(end[0])}" cy="${n(end[1])}" r="2.5"/>` : '');
|
|
69
81
|
}
|
|
70
82
|
const ref = has(reference) && reference <= hi && reference >= lo ? `<line class="spark-ref" x1="${l}" x2="${width - r}" y1="${n(y(reference))}" y2="${n(y(reference))}"/>` : '';
|
|
71
83
|
const a11y = label ? `role="img" aria-label="${esc(label)}"` : 'aria-hidden="true"';
|
|
72
|
-
return `<svg class="spark" data-kind="${kind}" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" ${a11y}>${ref}${marks}</svg>`;
|
|
84
|
+
return `<svg class="spark" data-kind="${kind}" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" ${a11y}>${ref}${behind}${marks}</svg>`;
|
|
73
85
|
};
|
package/src/spark.test.mjs
CHANGED
|
@@ -41,6 +41,29 @@ assert.equal(area.querySelectorAll('.spark-fill').length, 1);
|
|
|
41
41
|
assert.match(area.querySelector('.spark-fill').getAttribute('d'), /Z$/);
|
|
42
42
|
assert.ok(area.querySelector('.spark-last'), 'last: a dot on the newest value');
|
|
43
43
|
|
|
44
|
+
// last: on the newest value there is, when the run ends in gaps (a year so far).
|
|
45
|
+
const sofar = svg(sparkline([3, 5, 4, null, null], { kind: 'line', last: true }));
|
|
46
|
+
const dot = sofar.querySelector('.spark-last');
|
|
47
|
+
const ends = sofar.querySelector('.spark-stroke').getAttribute('d').match(/L([\d.]+) ([\d.]+)$/);
|
|
48
|
+
assert.ok(dot, 'a run that ends in gaps still gets its dot');
|
|
49
|
+
assert.equal(dot.getAttribute('cx'), ends[1], 'on the newest value drawn');
|
|
50
|
+
|
|
51
|
+
// compare: a quiet line under the marks, on the same scale, at the same steps.
|
|
52
|
+
const yoy = svg(sparkline([3, 5, null, null], { kind: 'line', compare: [4, 10, 6, 2] }));
|
|
53
|
+
const cmp = yoy.querySelector('.spark-compare');
|
|
54
|
+
assert.ok(cmp, 'the compared run is drawn');
|
|
55
|
+
assert.ok(yoy.innerHTML.indexOf('spark-compare') < yoy.innerHTML.indexOf('spark-stroke'), 'under the marks');
|
|
56
|
+
const cmpPts = [...cmp.getAttribute('d').matchAll(/[ML]([\d.]+) ([\d.]+)/g)].map((m) => [Number(m[1]), Number(m[2])]);
|
|
57
|
+
const nowPts = [...yoy.querySelector('.spark-stroke').getAttribute('d').matchAll(/[ML]([\d.]+) ([\d.]+)/g)].map((m) => [Number(m[1]), Number(m[2])]);
|
|
58
|
+
assert.equal(cmpPts.length, 4);
|
|
59
|
+
assert.deepEqual(nowPts.map((p) => p[0]), cmpPts.slice(0, 2).map((p) => p[0]), 'the same steps across');
|
|
60
|
+
assert.ok(cmpPts[1][1] < nowPts[1][1], 'one scale: the higher value stands higher');
|
|
61
|
+
assert.ok(cmpPts[1][1] < 6, 'the compared peak sets the top');
|
|
62
|
+
assert.ok(svg(sparkline([2, 4], { kind: 'bar', compare: [3, 3] })).querySelector('.spark-compare'), 'over bars too');
|
|
63
|
+
assert.equal(sparkline([null, null], { compare: [1, 2] }), '', 'nothing of its own, no svg');
|
|
64
|
+
const longer = svg(sparkline([1], { kind: 'line', compare: [1, 2, 3] }));
|
|
65
|
+
assert.equal([...longer.querySelector('.spark-compare').getAttribute('d').matchAll(/[ML]/g)].length, 3, 'a longer compared run sets the steps');
|
|
66
|
+
|
|
44
67
|
// A label makes it an image with a name.
|
|
45
68
|
const named = svg(sparkline([1, 2], { label: 'Steps, last 12 months <rising>' }));
|
|
46
69
|
assert.equal(named.getAttribute('role'), 'img');
|