@n1kname/charts 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/package.json +28 -3
- package/src/donut.mjs +54 -0
- package/src/donut.test.mjs +23 -0
- package/src/index.mjs +3 -0
- package/src/plot.mjs +47 -0
- package/src/theme.mjs +30 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nikki Veldhuis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/charts",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.mjs",
|
|
8
|
+
"./donut": "./src/donut.mjs",
|
|
9
|
+
"./plot": "./src/plot.mjs",
|
|
10
|
+
"./theme": "./src/theme.mjs"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@observablehq/plot": "0.6.17",
|
|
14
|
+
"d3-shape": "3.2.0",
|
|
15
|
+
"linkedom": "0.18.12"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/n1kname/design-system.git",
|
|
23
|
+
"directory": "packages/charts"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"check": "node src/donut.test.mjs"
|
|
30
|
+
}
|
|
6
31
|
}
|
package/src/donut.mjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Plot has no arc mark, so a donut is drawn here with d3-shape, which Plot's own
|
|
2
|
+
// d3 dependency includes. Output is an SVG string filled with CSS variables:
|
|
3
|
+
// themes need no JavaScript, and the data stays in the page that holds it.
|
|
4
|
+
//
|
|
5
|
+
// A donut is for part-to-whole at a glance. To compare close values, use bars.
|
|
6
|
+
|
|
7
|
+
import { arc, pie } from 'd3-shape';
|
|
8
|
+
import { chrome } from './theme.mjs';
|
|
9
|
+
|
|
10
|
+
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
11
|
+
|
|
12
|
+
// First slice starts at 12 o'clock and runs clockwise, largest first: the input
|
|
13
|
+
// order is kept, because a sort would change which slice sits beside which.
|
|
14
|
+
export const slices = (data, { inner, outer, gap = 2 }) => {
|
|
15
|
+
const layout = pie().sort(null).value((d) => d.value);
|
|
16
|
+
// The gap is a fixed width of surface, so the pad angle comes from the mid radius.
|
|
17
|
+
const mid = (inner + outer) / 2;
|
|
18
|
+
const shape = arc().innerRadius(inner).outerRadius(outer).padAngle(gap / mid).padRadius(mid);
|
|
19
|
+
return layout(data).map((a) => ({
|
|
20
|
+
...a.data,
|
|
21
|
+
path: shape(a),
|
|
22
|
+
mid: (a.startAngle + a.endAngle) / 2,
|
|
23
|
+
}));
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Colour: an explicit `color` per datum, otherwise chrome.neutral. Identity is
|
|
27
|
+
// never colour alone: the caller lists every slice beside the ring.
|
|
28
|
+
const fill = (d) => d.color ?? chrome.neutral;
|
|
29
|
+
|
|
30
|
+
const ring = (parts, cx, cy) =>
|
|
31
|
+
`<g transform="translate(${cx} ${cy})">` +
|
|
32
|
+
parts.map((p) => `<path d="${p.path}" fill="${fill(p)}"><title>${esc(p.label)}: ${esc(p.valueLabel ?? p.value)}</title></path>`).join('') +
|
|
33
|
+
'</g>';
|
|
34
|
+
|
|
35
|
+
const centre = (title, cx, cy, size, leading) => {
|
|
36
|
+
const y0 = cy - ((title.length - 1) * leading) / 2;
|
|
37
|
+
return title
|
|
38
|
+
.map((line, i) => `<text x="${cx}" y="${y0 + i * leading}" text-anchor="middle" dominant-baseline="central" font-family="var(--font-display)" font-weight="600" font-size="${size}" fill="${chrome.text}">${esc(line)}</text>`)
|
|
39
|
+
.join('');
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// The ring alone. Labels go in an HTML list beside it, where they keep their real
|
|
43
|
+
// size: text inside an SVG shrinks with the column.
|
|
44
|
+
export const donutRing = (data, { size = 320, outer = 156, inner = 108, title = [], fontSize = 20 } = {}) => {
|
|
45
|
+
const c = size / 2;
|
|
46
|
+
return (
|
|
47
|
+
`<svg viewBox="0 0 ${size} ${size}" width="100%" height="auto" aria-hidden="true" focusable="false">` +
|
|
48
|
+
ring(slices(data, { inner, outer }), c, c) +
|
|
49
|
+
centre(title, c, c, fontSize, fontSize * 1.25) +
|
|
50
|
+
'</svg>'
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const swatch = fill;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Synthetic data only, like everything in this repo.
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { donutRing, slices } from './donut.mjs';
|
|
4
|
+
|
|
5
|
+
const data = [
|
|
6
|
+
{ label: 'Alpha & co', value: 50, valueLabel: '50%', color: 'var(--c-chart-1)', strong: true },
|
|
7
|
+
{ label: 'Beta', value: 30, valueLabel: '30%' },
|
|
8
|
+
{ label: 'Gamma', value: 20, valueLabel: '20%' },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const parts = slices(data, { inner: 50, outer: 100 });
|
|
12
|
+
assert.equal(parts.length, 3);
|
|
13
|
+
assert.ok(Math.abs(parts[0].mid - Math.PI / 2) < 1e-9, 'first slice starts at 12 o\'clock, runs clockwise');
|
|
14
|
+
assert.ok(parts[0].mid < parts[1].mid && parts[1].mid < parts[2].mid, 'input order is kept');
|
|
15
|
+
|
|
16
|
+
const svg = donutRing(data, { title: ['Title & more'] });
|
|
17
|
+
assert.ok(svg.includes('Alpha & co'), 'labels are escaped');
|
|
18
|
+
assert.ok(svg.includes('Title & more'), 'the title is escaped');
|
|
19
|
+
assert.ok(svg.includes('var(--c-chart-1)'), 'an explicit colour is used');
|
|
20
|
+
assert.ok(svg.includes('var(--c-chart-neutral)'), 'the rest fall back to the neutral tone');
|
|
21
|
+
assert.ok(!/#[0-9a-f]{6}/i.test(svg), 'no hex: colours are tokens');
|
|
22
|
+
|
|
23
|
+
console.log('donut: all assertions pass');
|
package/src/index.mjs
ADDED
package/src/plot.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Observable Plot rendered at build time to a plain SVG string. No Plot code
|
|
2
|
+
// reaches the browser, so a page ships no chart JavaScript and its data stays in
|
|
3
|
+
// that page's HTML. Pinned: Plot is pre-1.0.
|
|
4
|
+
|
|
5
|
+
import * as Plot from '@observablehq/plot';
|
|
6
|
+
import { parseHTML } from 'linkedom';
|
|
7
|
+
import { chrome } from './theme.mjs';
|
|
8
|
+
|
|
9
|
+
export { Plot };
|
|
10
|
+
|
|
11
|
+
const { document } = parseHTML('<!doctype html><html><body></body></html>');
|
|
12
|
+
|
|
13
|
+
// The caller's wrapper speaks for the chart (role="img" with a label), so the SVG
|
|
14
|
+
// is hidden from screen readers and Plot's labels on its inner parts are dropped.
|
|
15
|
+
export const renderPlot = (options) => {
|
|
16
|
+
const node = Plot.plot({
|
|
17
|
+
document,
|
|
18
|
+
style: {
|
|
19
|
+
background: 'transparent',
|
|
20
|
+
color: chrome.textMuted,
|
|
21
|
+
// A system font on purpose. Safari lays out SVG text with the fallback font's
|
|
22
|
+
// widths and then paints the web font, which shifts anything centred or
|
|
23
|
+
// right-aligned. A font that never swaps has nothing to shift.
|
|
24
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
|
|
25
|
+
fontSize: '13px',
|
|
26
|
+
overflow: 'visible',
|
|
27
|
+
},
|
|
28
|
+
...options,
|
|
29
|
+
});
|
|
30
|
+
node.setAttribute('aria-hidden', 'true');
|
|
31
|
+
return node.outerHTML.replace(/(<(?!svg)[a-z]+[^>]*?) aria-label="[^"]*"/g, '$1');
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Hairline, solid, one step off the surface. Never dashed.
|
|
35
|
+
export const grid = (axis = 'y') =>
|
|
36
|
+
(axis === 'y' ? Plot.gridY : Plot.gridX)({ stroke: chrome.grid, strokeOpacity: 1 });
|
|
37
|
+
|
|
38
|
+
// Text inside an SVG shrinks with it, so one drawing is unreadable on a phone. This
|
|
39
|
+
// draws the chart twice, each sized for its column, and CSS shows one. `build` takes
|
|
40
|
+
// the width and whether it is the narrow drawing, and returns Plot options.
|
|
41
|
+
export const WIDE = 720;
|
|
42
|
+
export const NARROW = 320;
|
|
43
|
+
|
|
44
|
+
export const renderResponsive = (build) => ({
|
|
45
|
+
wide: renderPlot({ width: WIDE, ...build(WIDE, false) }),
|
|
46
|
+
narrow: renderPlot({ width: NARROW, ...build(NARROW, true) }),
|
|
47
|
+
});
|
package/src/theme.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Charts name tokens, never hex, so a chart follows the theme toggle by CSS
|
|
2
|
+
// alone and needs no JavaScript at runtime.
|
|
3
|
+
|
|
4
|
+
const v = (name) => `var(--c-${name})`;
|
|
5
|
+
|
|
6
|
+
export const categorical = [1, 2, 3, 4, 5, 6, 7, 8].map((i) => v(`chart-${i}`));
|
|
7
|
+
export const sequential = [1, 2, 3, 4, 5, 6, 7].map((i) => v(`seq-${i}`));
|
|
8
|
+
export const sequential2 = [1, 2, 3, 4, 5, 6, 7].map((i) => v(`seq2-${i}`));
|
|
9
|
+
export const diverging = [
|
|
10
|
+
v('div-neg-3'), v('div-neg-2'), v('div-neg-1'), v('div-mid'),
|
|
11
|
+
v('div-pos-1'), v('div-pos-2'), v('div-pos-3'),
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
export const chrome = {
|
|
15
|
+
grid: v('chart-grid'),
|
|
16
|
+
axis: v('chart-axis'),
|
|
17
|
+
neutral: v('chart-neutral'),
|
|
18
|
+
text: v('text'),
|
|
19
|
+
textMuted: v('text-muted'),
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Tooltips: instant, because hover is too frequent to animate. Set in a system font, because
|
|
23
|
+
// Safari misplaces centred SVG text in a web font. Every value also needs a table.
|
|
24
|
+
export const tipStyle = {
|
|
25
|
+
fill: 'var(--c-surface)',
|
|
26
|
+
stroke: 'var(--c-border-strong)',
|
|
27
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
|
|
28
|
+
fontSize: 12,
|
|
29
|
+
textPadding: 8,
|
|
30
|
+
};
|
package/README.md
DELETED