sparklemacsparkline 1.0.0 → 1.0.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/README.md +3 -1
- package/package.json +3 -2
- package/src/sparkline.js +62 -7
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ $ npx sparklemacsparkline --stdout --labels 2006,2007,2008,2009,2010 4,42,50,34,
|
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
```html
|
|
16
|
-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 16" width="48" height="16" role="img" class="sparkline"><title>Sparkline of 5 values from 2006 to 2010: rising from 4 to a peak of 50 in 2008, then falling to 13</title><path class="sparkline-line" d="M1.44 13.
|
|
16
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 16" width="48" height="16" role="img" class="sparkline"><title>Sparkline of 5 values from 2006 to 2010: rising from 4 to a peak of 50 in 2008, then falling to 13</title><path class="sparkline-line" d="M1.44 13.51 12.72 3.54 24 1.44l11.28 4.2 11.28 5.51" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" vector-effect="non-scaling-stroke"/><circle class="sparkline-dot" cx="46.56" cy="11.15" r="1.44" fill="currentColor"/></svg>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
@@ -115,6 +115,8 @@ A series that crosses zero gets a faint axis at zero, as the sign of a value is
|
|
|
115
115
|
|
|
116
116
|
Presentation is inline by default: Colors are set as presentation attributes on the elements, which keeps the output valid as a standalone file and, unlike an embedded `style` element, cannot leak rules into a page it’s inlined in. As the default color is `currentColor`, an inlined sparkline still follows the surrounding text color, dark mode, and forced-colors mode. (Custom colors don’t adapt to forced-colors mode.)
|
|
117
117
|
|
|
118
|
+
The markup is as short as valid SVG allows: Path data uses the shortest of absolute and relative commands per segment, numbers are rounded to two decimals without leading zeros, and default attributes are left out. What remains is needed for a standalone file (`xmlns`, quoted attributes)—a minifier can still drop that when the sparkline is inlined in HTML.
|
|
119
|
+
|
|
118
120
|
With the external style, the elements carry class names only (`sparkline`, `sparkline-line`, `sparkline-dot`, `sparkline-axis`), leaving all presentation to a style sheet. Without classes, the elements can be selected by context, as in `h2 svg path`.
|
|
119
121
|
|
|
120
122
|
## Working on Sparkle MacSparkline
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"bin": {
|
|
4
4
|
"sparklemacsparkline": "bin/sparklemacsparkline.js"
|
|
5
5
|
},
|
|
6
|
-
"description": "
|
|
6
|
+
"description": "Non-British SVG sparkline generator for build steps and the command line, as seen on dxsrc.org and meiert.com",
|
|
7
7
|
"devDependencies": {
|
|
8
8
|
"@eslint/js": "^10.0.1",
|
|
9
9
|
"eslint": "^10.15.0",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"keywords": [
|
|
26
26
|
"accessibility",
|
|
27
27
|
"charts",
|
|
28
|
+
"data-visualization",
|
|
28
29
|
"sparklemacsparkline",
|
|
29
30
|
"sparklines",
|
|
30
31
|
"static-site-generators",
|
|
@@ -47,5 +48,5 @@
|
|
|
47
48
|
},
|
|
48
49
|
"type": "module",
|
|
49
50
|
"types": "src/index.d.ts",
|
|
50
|
-
"version": "1.0.
|
|
51
|
+
"version": "1.0.1"
|
|
51
52
|
}
|
package/src/sparkline.js
CHANGED
|
@@ -30,6 +30,64 @@ function round(number) {
|
|
|
30
30
|
return Math.round(number * 100) / 100;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
// A minus sign or a second decimal point already separates two numbers
|
|
34
|
+
function needsSpace(previous, next) {
|
|
35
|
+
return !(next.startsWith('-') || (next.startsWith('.') && previous.includes('.')));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The shortest path data through the points: per segment, the shortest of
|
|
39
|
+
// absolute and relative lineto (and their horizontal and vertical forms),
|
|
40
|
+
// repeating command letters only where the command changes. Relative coordinates
|
|
41
|
+
// come from the rounded points, so they add up to exactly the same positions.
|
|
42
|
+
function makePath(points) {
|
|
43
|
+
// Per state—the current command and whether the last number has a decimal
|
|
44
|
+
// point—the shortest path so far, as its length and its last part, which
|
|
45
|
+
// links back to the ones before (no nested arrays, as series can be long)
|
|
46
|
+
let states = new Map();
|
|
47
|
+
const [xStart, yStart] = points[0].map(format);
|
|
48
|
+
const textStart = `${xStart}${needsSpace(xStart, yStart) ? ' ' : ''}${yStart}`;
|
|
49
|
+
// `m` from the origin is absolute, too, but makes the coordinates after it relative
|
|
50
|
+
for (const command of ['L', 'l']) {
|
|
51
|
+
const text = `${command === 'L' ? 'M' : 'm'}${textStart}`;
|
|
52
|
+
states.set(command + yStart.includes('.'), { command, last: yStart, length: text.length, part: { text } });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
for (let index = 1; index < points.length; index++) {
|
|
56
|
+
const [x0, y0] = points[index - 1];
|
|
57
|
+
const [x1, y1] = points[index];
|
|
58
|
+
const dx = round(x1 - x0);
|
|
59
|
+
const dy = round(y1 - y0);
|
|
60
|
+
const options = [['L', [x1, y1]], ['l', [dx, dy]]];
|
|
61
|
+
if (dy === 0) options.push(['H', [x1]], ['h', [dx]]);
|
|
62
|
+
// Only in dense or narrow charts, where neighboring points round to the same x
|
|
63
|
+
if (dx === 0) options.push(['V', [y1]], ['v', [dy]]);
|
|
64
|
+
|
|
65
|
+
const statesNext = new Map();
|
|
66
|
+
for (const state of states.values()) {
|
|
67
|
+
for (const [command, numbers] of options) {
|
|
68
|
+
let text = command === state.command ? '' : command;
|
|
69
|
+
let last = text ? null : state.last;
|
|
70
|
+
for (const number of numbers.map(format)) {
|
|
71
|
+
if (last !== null && needsSpace(last, number)) text += ' ';
|
|
72
|
+
text += number;
|
|
73
|
+
last = number;
|
|
74
|
+
}
|
|
75
|
+
const key = command + last.includes('.');
|
|
76
|
+
const length = state.length + text.length;
|
|
77
|
+
if (!statesNext.has(key) || length < statesNext.get(key).length) {
|
|
78
|
+
statesNext.set(key, { command, last, length, part: { previous: state.part, text } });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
states = statesNext;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const parts = [];
|
|
86
|
+
let { part } = [...states.values()].reduce((a, b) => (b.length < a.length ? b : a));
|
|
87
|
+
for (; part; part = part.previous) parts.push(part.text);
|
|
88
|
+
return parts.reverse().join('');
|
|
89
|
+
}
|
|
90
|
+
|
|
33
91
|
function escapeAttribute(value) {
|
|
34
92
|
return String(value)
|
|
35
93
|
.replace(/&/g, '&')
|
|
@@ -180,10 +238,7 @@ export function sparkline(values, options = {}) {
|
|
|
180
238
|
const points = values.map((value, index) => [round(padding + index * step), toY(value)]);
|
|
181
239
|
const [pointLastX, pointLastY] = points.at(-1);
|
|
182
240
|
|
|
183
|
-
|
|
184
|
-
// command letter is only needed once
|
|
185
|
-
const [pointHead, ...pointsRest] = points.map(([x, y]) => `${format(x)} ${format(y)}`);
|
|
186
|
-
const d = `M${pointHead}L${pointsRest.join(' ')}`;
|
|
241
|
+
const d = makePath(points);
|
|
187
242
|
|
|
188
243
|
const attributesPath = isInline
|
|
189
244
|
? ` fill="none" stroke="${escapeAttribute(foreground)}" stroke-width="${format(widthStroke)}"` +
|
|
@@ -203,7 +258,7 @@ export function sparkline(values, options = {}) {
|
|
|
203
258
|
const crossesZero = domainMin < 0 && domainMax > 0;
|
|
204
259
|
const yZero = toY(0);
|
|
205
260
|
const markupAxis = axis && crossesZero
|
|
206
|
-
? `<line${naming('-axis')}
|
|
261
|
+
? `<line${naming('-axis')} y1="${format(yZero)}" x2="${format(width)}" y2="${format(yZero)}"${attributesAxis}/>`
|
|
207
262
|
: '';
|
|
208
263
|
|
|
209
264
|
const markupPath = `<path${naming('-line')} d="${d}"${attributesPath}/>`;
|
|
@@ -215,7 +270,7 @@ export function sparkline(values, options = {}) {
|
|
|
215
270
|
const markupTitle = `<title>${escapeAttribute(describeSeries(values, { labels, title }))}</title>`;
|
|
216
271
|
const attributesRole = decorative ? ' aria-hidden="true"' : ' role="img"';
|
|
217
272
|
|
|
218
|
-
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}"` +
|
|
219
|
-
` width="${width}" height="${height}"${attributesRole}${naming('')}>` +
|
|
273
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${format(width)} ${format(height)}"` +
|
|
274
|
+
` width="${format(width)}" height="${format(height)}"${attributesRole}${naming('')}>` +
|
|
220
275
|
`${markupTitle}${markupBackground}${markupAxis}${markupPath}${markupDot}</svg>`;
|
|
221
276
|
}
|