qvac-charts 0.0.0 → 0.1.2
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.md +198 -0
- package/NOTICE.md +13 -0
- package/README.md +69 -0
- package/dist-lib/index.d.ts +100 -0
- package/dist-lib/index.js +28 -0
- package/dist-lib/lib/charts/index.d.ts +3 -0
- package/dist-lib/lib/charts/index.js +21 -0
- package/dist-lib/lib/charts/maps.d.ts +4 -0
- package/dist-lib/lib/charts/maps.js +197 -0
- package/dist-lib/lib/charts/parts.d.ts +6 -0
- package/dist-lib/lib/charts/parts.js +244 -0
- package/dist-lib/lib/charts/ranking.d.ts +3 -0
- package/dist-lib/lib/charts/ranking.js +62 -0
- package/dist-lib/lib/charts/slope.d.ts +2 -0
- package/dist-lib/lib/charts/slope.js +57 -0
- package/dist-lib/lib/charts/time.d.ts +4 -0
- package/dist-lib/lib/charts/time.js +112 -0
- package/dist-lib/lib/format.d.ts +3 -0
- package/dist-lib/lib/format.js +9 -0
- package/dist-lib/lib/frame.d.ts +35 -0
- package/dist-lib/lib/frame.js +56 -0
- package/dist-lib/lib/geometry.d.ts +50 -0
- package/dist-lib/lib/geometry.js +166 -0
- package/dist-lib/lib/maps/aliases.d.ts +1 -0
- package/dist-lib/lib/maps/aliases.js +77 -0
- package/dist-lib/lib/maps/brazil.d.ts +2 -0
- package/dist-lib/lib/maps/brazil.js +29 -0
- package/dist-lib/lib/maps/world.d.ts +10 -0
- package/dist-lib/lib/maps/world.js +1 -0
- package/dist-lib/lib/normalize.d.ts +7 -0
- package/dist-lib/lib/normalize.js +247 -0
- package/dist-lib/lib/pick.d.ts +3 -0
- package/dist-lib/lib/pick.js +62 -0
- package/dist-lib/lib/places.d.ts +30 -0
- package/dist-lib/lib/places.js +45 -0
- package/dist-lib/lib/render.d.ts +8 -0
- package/dist-lib/lib/render.js +12 -0
- package/dist-lib/lib/schema.d.ts +85 -0
- package/dist-lib/lib/schema.js +65 -0
- package/dist-lib/lib/spec.d.ts +58 -0
- package/dist-lib/lib/spec.js +1 -0
- package/dist-lib/lib/svg.d.ts +21 -0
- package/dist-lib/lib/svg.js +43 -0
- package/dist-lib/lib/themes.d.ts +28 -0
- package/dist-lib/lib/themes.js +71 -0
- package/dist-lib/maps.d.ts +3 -0
- package/dist-lib/maps.js +7 -0
- package/package.json +63 -1
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { callout } from '../frame.js';
|
|
2
|
+
import { arc, bounds, dodge, largestRemainder, pack, polar, squarify } from '../geometry.js';
|
|
3
|
+
import { text, wrap } from '../svg.js';
|
|
4
|
+
import { colors } from '../themes.js';
|
|
5
|
+
const byValue = (data) => [...data].sort((a, c) => c.value - a.value);
|
|
6
|
+
const sum = (data) => data.reduce((s, d) => s + d.value, 0);
|
|
7
|
+
export function bubbles(d) {
|
|
8
|
+
const { out, spec, t, b, fmt } = d;
|
|
9
|
+
const items = byValue(spec.data);
|
|
10
|
+
const color = colors(items, { t, highlight: spec.highlight });
|
|
11
|
+
const circles = pack(items.map((x) => Math.sqrt(x.value)), 0.04);
|
|
12
|
+
const box = bounds(circles);
|
|
13
|
+
const avail = spec.callout ? b.h - 190 : b.h;
|
|
14
|
+
const k = Math.min(b.w / (box.x1 - box.x0), avail / (box.y1 - box.y0));
|
|
15
|
+
const ox = b.x + (b.w - (box.x1 - box.x0) * k) / 2 - box.x0 * k;
|
|
16
|
+
const oy = b.y + (avail - (box.y1 - box.y0) * k) / 2 - box.y0 * k;
|
|
17
|
+
circles.forEach((c, i) => {
|
|
18
|
+
const item = items[i];
|
|
19
|
+
const cx = ox + c.x * k;
|
|
20
|
+
const cy = oy + c.y * k;
|
|
21
|
+
const r = c.r * k;
|
|
22
|
+
out.push(`<circle cx="${cx}" cy="${cy}" r="${r}" fill="${color(item)}"/>`);
|
|
23
|
+
out.push(...bubbleLabel(d, { item, cx, cy, r }));
|
|
24
|
+
});
|
|
25
|
+
callout(d, { x: b.x, y: b.y + b.h - 150, w: b.w });
|
|
26
|
+
}
|
|
27
|
+
function bubbleLabel({ t, fmt }, { item, cx, cy, r }) {
|
|
28
|
+
if (r > 70) {
|
|
29
|
+
const size = Math.min(56, r / 3.2);
|
|
30
|
+
return [
|
|
31
|
+
text(item.label, {
|
|
32
|
+
x: cx,
|
|
33
|
+
y: cy - size * 0.1,
|
|
34
|
+
size,
|
|
35
|
+
weight: 800,
|
|
36
|
+
fill: t.bg,
|
|
37
|
+
anchor: 'middle'
|
|
38
|
+
}),
|
|
39
|
+
text(fmt(item.value), {
|
|
40
|
+
x: cx,
|
|
41
|
+
y: cy + size * 0.95,
|
|
42
|
+
size: size * 0.7,
|
|
43
|
+
weight: 500,
|
|
44
|
+
fill: t.bg,
|
|
45
|
+
anchor: 'middle'
|
|
46
|
+
})
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
if (r > 34) {
|
|
50
|
+
const size = Math.max(16, Math.min(26, r / 2.4));
|
|
51
|
+
return [
|
|
52
|
+
text(item.label, { x: cx, y: cy, size, weight: 800, fill: t.bg, anchor: 'middle' }),
|
|
53
|
+
text(fmt(item.value), {
|
|
54
|
+
x: cx,
|
|
55
|
+
y: cy + size,
|
|
56
|
+
size: size * 0.8,
|
|
57
|
+
weight: 500,
|
|
58
|
+
fill: t.bg,
|
|
59
|
+
anchor: 'middle'
|
|
60
|
+
})
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
return [
|
|
64
|
+
text(`${item.label} ${fmt(item.value)}`, {
|
|
65
|
+
x: cx,
|
|
66
|
+
y: cy + r + 24,
|
|
67
|
+
size: 20,
|
|
68
|
+
weight: 700,
|
|
69
|
+
fill: t.muted,
|
|
70
|
+
anchor: 'middle'
|
|
71
|
+
})
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
export function treemap({ out, spec, t, b }) {
|
|
75
|
+
const items = byValue(spec.data);
|
|
76
|
+
const color = colors(items, { t, highlight: spec.highlight });
|
|
77
|
+
const total = sum(items);
|
|
78
|
+
const groups = [...new Set(items.map((d) => d.group).filter((g) => Boolean(g)))];
|
|
79
|
+
let top = b.y;
|
|
80
|
+
if (groups.length > 1) {
|
|
81
|
+
let x = b.x;
|
|
82
|
+
for (const g of groups) {
|
|
83
|
+
out.push(`<rect x="${x}" y="${top}" width="22" height="22" rx="4" fill="${color({ group: g })}"/>`);
|
|
84
|
+
out.push(text(g, { x: x + 32, y: top + 19, size: 24, fill: t.text }));
|
|
85
|
+
x += 32 + g.length * 24 * 0.56 + 36;
|
|
86
|
+
}
|
|
87
|
+
top += 48;
|
|
88
|
+
}
|
|
89
|
+
const cells = squarify(items.map((d) => d.value), { x: b.x, y: top, w: b.w, h: b.y + b.h - top });
|
|
90
|
+
cells.forEach((c, i) => {
|
|
91
|
+
const d = items[i];
|
|
92
|
+
const g = 3;
|
|
93
|
+
const share = ((d.value / total) * 100).toFixed(1) + '%';
|
|
94
|
+
const size = Math.max(18, Math.min(64, Math.sqrt(c.w * c.h) / 5.5));
|
|
95
|
+
out.push(`<rect x="${c.x + g}" y="${c.y + g}" width="${c.w - 2 * g}" height="${c.h - 2 * g}" rx="6" fill="${color(d)}"/>`);
|
|
96
|
+
if (c.w > size * 3 && c.h > size * 2.2) {
|
|
97
|
+
out.push(text(share, { x: c.x + 18, y: c.y + 18 + size, size, weight: 800, fill: t.bg }));
|
|
98
|
+
out.push(text(d.label, {
|
|
99
|
+
x: c.x + 18,
|
|
100
|
+
y: c.y + 26 + size + size * 0.6,
|
|
101
|
+
size: size * 0.5,
|
|
102
|
+
weight: 700,
|
|
103
|
+
fill: t.bg
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
else if (c.w > 60 && c.h > 40) {
|
|
107
|
+
out.push(text(share, { x: c.x + 12, y: c.y + 32, size: 20, weight: 800, fill: t.bg }));
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
export function donut(draw) {
|
|
112
|
+
const { out, spec, t, b } = draw;
|
|
113
|
+
const items = byValue(spec.data);
|
|
114
|
+
const color = colors(items, { t, highlight: spec.highlight });
|
|
115
|
+
const total = sum(items);
|
|
116
|
+
const c = { x: b.x + b.w / 2, y: b.y + b.h / 2 };
|
|
117
|
+
const R = Math.min(b.w / 2 - 200, b.h / 2 - 40);
|
|
118
|
+
const r = R * 0.6;
|
|
119
|
+
const labels = [];
|
|
120
|
+
let a = 0;
|
|
121
|
+
for (const d of items) {
|
|
122
|
+
const sweep = (d.value / total) * 360;
|
|
123
|
+
const mid = a + sweep / 2;
|
|
124
|
+
const [lx, ly] = polar(c, { r: R + 46, deg: mid });
|
|
125
|
+
out.push(`<path d="${arc(c, { outer: R, inner: r, from: a, to: a + sweep })}" fill="${color(d)}" stroke="${t.bg}" stroke-width="4"/>`);
|
|
126
|
+
labels.push({ d, mid, x: lx, y: ly, right: mid < 180 });
|
|
127
|
+
a += sweep;
|
|
128
|
+
}
|
|
129
|
+
for (const side of [true, false]) {
|
|
130
|
+
const group = labels.filter((l) => l.right === side);
|
|
131
|
+
dodge(group, { gap: 74, lo: b.y + 20, hi: b.y + b.h - 20 });
|
|
132
|
+
for (const l of group) {
|
|
133
|
+
out.push(...sliceLabel(draw, { l, c, R, total }));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const focus = items.find((d) => d.label === spec.highlight) ?? items[0];
|
|
137
|
+
const share = Math.round((focus.value / total) * 100) + '%';
|
|
138
|
+
out.push(text(share, {
|
|
139
|
+
x: c.x,
|
|
140
|
+
y: c.y + 10,
|
|
141
|
+
size: r * 0.55,
|
|
142
|
+
weight: 800,
|
|
143
|
+
fill: color(focus),
|
|
144
|
+
anchor: 'middle',
|
|
145
|
+
spacing: -2
|
|
146
|
+
}));
|
|
147
|
+
wrap(focus.label, { width: r * 1.5, size: 24 })
|
|
148
|
+
.slice(0, 2)
|
|
149
|
+
.forEach((line, i) => out.push(text(line, {
|
|
150
|
+
x: c.x,
|
|
151
|
+
y: c.y + 10 + r * 0.25 + i * 30,
|
|
152
|
+
size: 24,
|
|
153
|
+
fill: t.text,
|
|
154
|
+
anchor: 'middle'
|
|
155
|
+
})));
|
|
156
|
+
}
|
|
157
|
+
function sliceLabel({ spec, t, fmt }, { l, c, R, total }) {
|
|
158
|
+
const [ex, ey] = polar(c, { r: R + 8, deg: l.mid });
|
|
159
|
+
const tx = l.right ? Math.max(l.x, c.x + 40) : Math.min(l.x, c.x - 40);
|
|
160
|
+
const anchor = l.right ? 'start' : 'end';
|
|
161
|
+
const ox = l.right ? tx + 8 : tx - 8;
|
|
162
|
+
const share = Math.round((l.d.value / total) * 100) + '%';
|
|
163
|
+
const detail = spec.suffix === '%' ? share : `${share} · ${fmt(l.d.value)}`;
|
|
164
|
+
return [
|
|
165
|
+
`<path d="M${ex},${ey}L${tx},${l.y - 8}" stroke="${t.muted}" stroke-width="1.5" fill="none"/>`,
|
|
166
|
+
text(l.d.label, { x: ox, y: l.y - 2, size: 26, weight: 800, fill: t.text, anchor }),
|
|
167
|
+
text(detail, { x: ox, y: l.y + 26, size: 22, fill: t.muted, anchor })
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
export function waffle({ out, spec, t, b, fmt }) {
|
|
171
|
+
const items = byValue(spec.data);
|
|
172
|
+
const focus = items.find((d) => d.label === spec.highlight) ?? items[0];
|
|
173
|
+
const ordered = [focus, ...items.filter((d) => d !== focus)];
|
|
174
|
+
const total = sum(items);
|
|
175
|
+
const cells = largestRemainder(ordered.map((d) => (d.value / total) * 100));
|
|
176
|
+
const fill = (d, i) => {
|
|
177
|
+
if (d === focus)
|
|
178
|
+
return t.accent;
|
|
179
|
+
return items.length === 2 ? t.base : t.palette[(i + 1) % t.palette.length];
|
|
180
|
+
};
|
|
181
|
+
const legendH = Math.ceil(ordered.length / 2) * 110;
|
|
182
|
+
const side = Math.min(b.w, b.h - legendH - 30);
|
|
183
|
+
const step = side / 10;
|
|
184
|
+
const gx = b.x + (b.w - side) / 2;
|
|
185
|
+
let k = 0;
|
|
186
|
+
ordered.forEach((d, i) => {
|
|
187
|
+
for (let c = 0; c < cells[i]; c++, k++) {
|
|
188
|
+
const x = gx + (k % 10) * step;
|
|
189
|
+
const y = b.y + Math.floor(k / 10) * step;
|
|
190
|
+
out.push(`<rect x="${x + 4}" y="${y + 4}" width="${step - 8}" height="${step - 8}" rx="${step * 0.18}" fill="${fill(d, i)}"/>`);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
const ly = b.y + side + 40;
|
|
194
|
+
ordered.forEach((d, i) => {
|
|
195
|
+
const x = b.x + (i % 2) * (b.w / 2);
|
|
196
|
+
const y = ly + Math.floor(i / 2) * 110;
|
|
197
|
+
out.push(text(cells[i] + '%', { x, y: y + 56, size: 64, weight: 800, fill: fill(d, i), spacing: -2 }));
|
|
198
|
+
out.push(text(d.label, { x: x + 170, y: y + 34, size: 26, weight: 800, fill: t.text }));
|
|
199
|
+
if (spec.suffix !== '%') {
|
|
200
|
+
out.push(text(fmt(d.value), { x: x + 170, y: y + 64, size: 22, fill: t.muted }));
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
export function stacked({ out, spec, t, b }) {
|
|
205
|
+
const rows = [...new Set(spec.data.map((d) => d.label))];
|
|
206
|
+
const parts = [...new Set(spec.data.map((d) => d.group ?? ''))];
|
|
207
|
+
const color = (p, i) => p === spec.highlight ? t.accent : t.palette[(i + 1) % t.palette.length];
|
|
208
|
+
let x = b.x;
|
|
209
|
+
parts.forEach((p, i) => {
|
|
210
|
+
out.push(`<rect x="${x}" y="${b.y}" width="22" height="22" rx="4" fill="${color(p, i)}"/>`);
|
|
211
|
+
out.push(text(p, { x: x + 32, y: b.y + 19, size: 24, fill: t.text }));
|
|
212
|
+
x += 32 + p.length * 24 * 0.56 + 36;
|
|
213
|
+
});
|
|
214
|
+
const top = b.y + 70;
|
|
215
|
+
const rowH = Math.min(110, (b.h - 70) / rows.length);
|
|
216
|
+
const labelW = Math.min(240, Math.max(...rows.map((r) => r.length)) * 28 * 0.56) + 24;
|
|
217
|
+
const x0 = b.x + labelW;
|
|
218
|
+
const span = b.w - labelW;
|
|
219
|
+
rows.forEach((r, ri) => {
|
|
220
|
+
const cy = top + ri * rowH + rowH / 2;
|
|
221
|
+
const cells = parts.map((p) => spec.data.find((d) => d.label === r && (d.group ?? '') === p)?.value ?? 0);
|
|
222
|
+
const total = cells.reduce((s, v) => s + v, 0) || 1;
|
|
223
|
+
let cx = x0;
|
|
224
|
+
out.push(text(r, { x: b.x, y: cy + 10, size: 28, weight: 700, fill: t.text }));
|
|
225
|
+
cells.forEach((v, pi) => {
|
|
226
|
+
const w = (v / total) * span;
|
|
227
|
+
if (w <= 0) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
out.push(`<rect x="${cx + 1.5}" y="${cy - rowH * 0.34}" width="${Math.max(0, w - 3)}" height="${rowH * 0.68}" rx="4" fill="${color(parts[pi], pi)}"/>`);
|
|
231
|
+
if (w > 70) {
|
|
232
|
+
out.push(text(Math.round((v / total) * 100) + '%', {
|
|
233
|
+
x: cx + w / 2,
|
|
234
|
+
y: cy + 9,
|
|
235
|
+
size: 24,
|
|
236
|
+
weight: 800,
|
|
237
|
+
fill: t.bg,
|
|
238
|
+
anchor: 'middle'
|
|
239
|
+
}));
|
|
240
|
+
}
|
|
241
|
+
cx += w;
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { callout } from '../frame.js';
|
|
2
|
+
import { ceiling, stroke } from '../geometry.js';
|
|
3
|
+
import { text } from '../svg.js';
|
|
4
|
+
export function rankedBar(d) {
|
|
5
|
+
const { out, spec, t, b, fmt } = d;
|
|
6
|
+
const rows = [...spec.data].sort((a, c) => c.value - a.value);
|
|
7
|
+
const hl = spec.highlight ?? rows[0].label;
|
|
8
|
+
const max = ceiling(rows.map((r) => r.value));
|
|
9
|
+
const rowH = Math.min(86, b.h / rows.length);
|
|
10
|
+
const labelW = Math.min(300, Math.max(...rows.map((r) => r.label.length)) * 30 * 0.56);
|
|
11
|
+
const x0 = b.x + 64 + labelW + 20;
|
|
12
|
+
const span = b.x + b.w - x0 - 170;
|
|
13
|
+
rows.forEach((r, i) => {
|
|
14
|
+
const cy = b.y + i * rowH + rowH / 2;
|
|
15
|
+
const on = r.label === hl;
|
|
16
|
+
const w = Math.max(6, (r.value / max) * span);
|
|
17
|
+
const fill = on ? t.accent : t.base;
|
|
18
|
+
out.push(`<circle cx="${b.x + 22}" cy="${cy}" r="22" fill="${on ? t.accent : t.faint}"/>`);
|
|
19
|
+
out.push(text(String(i + 1), {
|
|
20
|
+
x: b.x + 22,
|
|
21
|
+
y: cy + 8,
|
|
22
|
+
size: 22,
|
|
23
|
+
weight: 800,
|
|
24
|
+
fill: on ? t.bg : t.muted,
|
|
25
|
+
anchor: 'middle'
|
|
26
|
+
}));
|
|
27
|
+
out.push(text(r.label, { x: b.x + 64, y: cy + 10, size: 30, weight: on ? 800 : 500, fill: t.text }));
|
|
28
|
+
out.push(`<rect x="${x0}" y="${cy - rowH * 0.31}" width="${w}" height="${rowH * 0.62}" rx="6" fill="${fill}"/>`);
|
|
29
|
+
out.push(text(fmt(r.value), {
|
|
30
|
+
x: x0 + w + 14,
|
|
31
|
+
y: cy + 10,
|
|
32
|
+
size: 30,
|
|
33
|
+
weight: 800,
|
|
34
|
+
fill: on ? t.accent : t.text
|
|
35
|
+
}));
|
|
36
|
+
});
|
|
37
|
+
callout(d, { x: b.x + b.w / 2, y: b.y + b.h - 150, w: b.w / 2 });
|
|
38
|
+
}
|
|
39
|
+
export function radial({ out, spec, t, b, fmt }) {
|
|
40
|
+
const rows = [...spec.data].sort((a, c) => c.value - a.value);
|
|
41
|
+
const max = ceiling(rows.map((r) => r.value));
|
|
42
|
+
const R = Math.min(b.w / 2, b.h / 2) - 10;
|
|
43
|
+
const c = { x: b.x + b.w / 2, y: b.y + b.h / 2 };
|
|
44
|
+
const band = (R * 0.78) / rows.length;
|
|
45
|
+
rows.forEach((d, i) => {
|
|
46
|
+
const r = R - i * band - band / 2;
|
|
47
|
+
const on = d.label === (spec.highlight ?? rows[0].label);
|
|
48
|
+
const color = on ? t.accent : t.palette[(i + 1) % t.palette.length];
|
|
49
|
+
const sw = band * 0.7;
|
|
50
|
+
const size = Math.min(26, band * 0.42);
|
|
51
|
+
out.push(`<path d="${stroke(c, { r, from: 0, to: 270 })}" stroke="${t.faint}" stroke-width="${sw}" fill="none" stroke-linecap="round"/>`);
|
|
52
|
+
out.push(`<path d="${stroke(c, { r, from: 0, to: Math.max(2, (d.value / max) * 270) })}" stroke="${color}" stroke-width="${sw}" fill="none" stroke-linecap="round"/>`);
|
|
53
|
+
out.push(text(`${d.label} ${fmt(d.value)}`, {
|
|
54
|
+
x: c.x - 18,
|
|
55
|
+
y: c.y - r + size * 0.36,
|
|
56
|
+
size,
|
|
57
|
+
weight: on ? 800 : 600,
|
|
58
|
+
fill: on ? t.accent : t.text,
|
|
59
|
+
anchor: 'end'
|
|
60
|
+
}));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { dodge } from '../geometry.js';
|
|
2
|
+
import { text } from '../svg.js';
|
|
3
|
+
const paired = (d) => d.previous !== undefined;
|
|
4
|
+
export function slope({ out, spec, t, b, fmt }) {
|
|
5
|
+
const items = spec.data.filter(paired);
|
|
6
|
+
const all = items.flatMap((d) => [d.value, d.previous]);
|
|
7
|
+
const lo = Math.min(0, ...all);
|
|
8
|
+
const hi = Math.max(...all) * 1.05;
|
|
9
|
+
const lx = b.x + 300;
|
|
10
|
+
const rx = b.x + b.w - 300;
|
|
11
|
+
const top = b.y + 90;
|
|
12
|
+
const bottom = b.y + b.h - 30;
|
|
13
|
+
const py = (v) => bottom - ((v - lo) / (hi - lo)) * (bottom - top);
|
|
14
|
+
const color = (d) => {
|
|
15
|
+
if (d.label === spec.highlight)
|
|
16
|
+
return t.accent;
|
|
17
|
+
return d.value >= d.previous ? t.palette[2] : t.palette[4];
|
|
18
|
+
};
|
|
19
|
+
for (const [x, word] of [
|
|
20
|
+
[lx, spec.from ?? 'Before'],
|
|
21
|
+
[rx, spec.to ?? 'After']
|
|
22
|
+
]) {
|
|
23
|
+
out.push(text(word, { x, y: b.y + 30, size: 28, weight: 800, fill: t.muted, anchor: 'middle' }));
|
|
24
|
+
}
|
|
25
|
+
for (const x of [lx, rx]) {
|
|
26
|
+
out.push(`<rect x="${x - 1}" y="${top - 20}" width="2" height="${bottom - top + 20}" fill="${t.faint}"/>`);
|
|
27
|
+
}
|
|
28
|
+
for (const d of items) {
|
|
29
|
+
const on = d.label === spec.highlight;
|
|
30
|
+
out.push(`<path d="M${lx},${py(d.previous)}L${rx},${py(d.value)}" stroke="${color(d)}" stroke-width="${on ? 8 : 5}" fill="none" opacity="${spec.highlight && !on ? 0.55 : 1}"/>`);
|
|
31
|
+
out.push(`<circle cx="${lx}" cy="${py(d.previous)}" r="8" fill="${color(d)}"/>`);
|
|
32
|
+
out.push(`<circle cx="${rx}" cy="${py(d.value)}" r="8" fill="${color(d)}"/>`);
|
|
33
|
+
}
|
|
34
|
+
const left = items.map((d) => ({ d, y: py(d.previous) }));
|
|
35
|
+
const right = items.map((d) => ({ d, y: py(d.value) }));
|
|
36
|
+
dodge(left, { gap: 40, lo: top - 10, hi: bottom + 10 });
|
|
37
|
+
dodge(right, { gap: 40, lo: top - 10, hi: bottom + 10 });
|
|
38
|
+
for (const l of left) {
|
|
39
|
+
out.push(text(`${l.d.label} ${fmt(l.d.previous)}`, {
|
|
40
|
+
x: lx - 20,
|
|
41
|
+
y: l.y + 9,
|
|
42
|
+
size: 24,
|
|
43
|
+
weight: 700,
|
|
44
|
+
fill: t.text,
|
|
45
|
+
anchor: 'end'
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
for (const r of right) {
|
|
49
|
+
out.push(text(`${fmt(r.d.value)} ${r.d.label}`, {
|
|
50
|
+
x: rx + 20,
|
|
51
|
+
y: r.y + 9,
|
|
52
|
+
size: 24,
|
|
53
|
+
weight: 700,
|
|
54
|
+
fill: color(r.d)
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { callout } from '../frame.js';
|
|
2
|
+
import { ceiling, dodge, ticks } from '../geometry.js';
|
|
3
|
+
import { text, wrap } from '../svg.js';
|
|
4
|
+
function grid({ out, t, fmt }, { py, left, width, max }) {
|
|
5
|
+
const tk = ticks(max);
|
|
6
|
+
const td = tk.some((v) => !Number.isInteger(v)) ? 1 : 0;
|
|
7
|
+
for (const v of tk) {
|
|
8
|
+
out.push(`<rect x="${left}" y="${py(v)}" width="${width}" height="1.5" fill="${t.faint}"/>`);
|
|
9
|
+
out.push(text(fmt(v, td), { x: left - 14, y: py(v) + 8, size: 22, fill: t.muted, anchor: 'end' }));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function line(d) {
|
|
13
|
+
const { out, spec, t, b } = d;
|
|
14
|
+
const pts = spec.data;
|
|
15
|
+
const max = ceiling(pts.map((p) => p.value)) * 1.12;
|
|
16
|
+
const pad = { l: 90, r: 40, t: 160, b: 60 };
|
|
17
|
+
const px = (i) => b.x + pad.l + (i * (b.w - pad.l - pad.r)) / (pts.length - 1);
|
|
18
|
+
const py = (v) => b.y + b.h - pad.b - (v / max) * (b.h - pad.t - pad.b);
|
|
19
|
+
const axes = { px, py, left: b.x + pad.l, width: b.w - pad.l - pad.r };
|
|
20
|
+
grid(d, { ...axes, max });
|
|
21
|
+
pts.forEach((p, i) => out.push(text(p.label, {
|
|
22
|
+
x: px(i),
|
|
23
|
+
y: b.y + b.h - pad.b + 40,
|
|
24
|
+
size: 24,
|
|
25
|
+
fill: t.muted,
|
|
26
|
+
anchor: 'middle'
|
|
27
|
+
})));
|
|
28
|
+
const path = pts.map((p, i) => `${i ? 'L' : 'M'}${px(i)},${py(p.value)}`).join('');
|
|
29
|
+
out.push(`<defs><linearGradient id="area" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="${t.accent}" stop-opacity="0.45"/><stop offset="1" stop-color="${t.accent}" stop-opacity="0"/></linearGradient></defs>`);
|
|
30
|
+
out.push(`<path d="${path}L${px(pts.length - 1)},${py(0)}L${px(0)},${py(0)}Z" fill="url(#area)"/>`);
|
|
31
|
+
out.push(`<path d="${path}" fill="none" stroke="${t.accent}" stroke-width="6" stroke-linejoin="round"/>`);
|
|
32
|
+
pts.forEach((p, i) => {
|
|
33
|
+
out.push(`<circle cx="${px(i)}" cy="${py(p.value)}" r="8" fill="${t.bg}" stroke="${t.accent}" stroke-width="5"/>`);
|
|
34
|
+
if (p.note) {
|
|
35
|
+
out.push(...note(d, { p, i, axes }));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
callout(d, { x: b.x + pad.l, y: b.y + 100, w: 380 });
|
|
39
|
+
}
|
|
40
|
+
function note({ spec, t, b, fmt }, { p, i, axes: { px, py } }) {
|
|
41
|
+
const pts = spec.data;
|
|
42
|
+
const x = px(i);
|
|
43
|
+
const anchor = i > pts.length * 0.6 ? 'end' : 'start';
|
|
44
|
+
const lines = wrap(p.note ?? '', { width: 300, size: 22 });
|
|
45
|
+
const tx = anchor === 'end' ? x - 12 : x + 12;
|
|
46
|
+
const x1 = anchor === 'end' ? tx - 300 : tx + 300;
|
|
47
|
+
const under = pts.filter((_, j) => (px(j) - tx) * (px(j) - x1) <= 0 || j === i + (anchor === 'end' ? -1 : 1));
|
|
48
|
+
const peak = Math.min(...under.map((q) => py(q.value)), py(p.value));
|
|
49
|
+
const top = Math.max(b.y, peak - 50 - lines.length * 28 - 34);
|
|
50
|
+
return [
|
|
51
|
+
`<rect x="${x - 1}" y="${top + 12}" width="2" height="${py(p.value) - top - 22}" fill="${t.muted}"/>`,
|
|
52
|
+
text(fmt(p.value), { x: tx, y: top + 30, size: 30, weight: 800, fill: t.text, anchor }),
|
|
53
|
+
...lines.map((l, j) => text(l, { x: tx, y: top + 62 + j * 28, size: 22, fill: t.muted, anchor }))
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
export function column(d) {
|
|
57
|
+
const { out, spec, t, b, fmt } = d;
|
|
58
|
+
const pts = spec.data;
|
|
59
|
+
const hl = spec.highlight ?? pts[pts.length - 1].label;
|
|
60
|
+
const max = ceiling(pts.map((p) => p.value)) * 1.12;
|
|
61
|
+
const top = b.y + (spec.callout ? 190 : 20);
|
|
62
|
+
const base = b.y + b.h - 60;
|
|
63
|
+
const slot = b.w / pts.length;
|
|
64
|
+
const bw = Math.min(140, slot * 0.64);
|
|
65
|
+
pts.forEach((p, i) => {
|
|
66
|
+
const on = p.label === hl;
|
|
67
|
+
const h = (Math.max(0, p.value) / max) * (base - top);
|
|
68
|
+
const x = b.x + i * slot + (slot - bw) / 2;
|
|
69
|
+
out.push(`<rect x="${x}" y="${base - h}" width="${bw}" height="${h}" rx="6" fill="${on ? t.accent : t.base}"/>`);
|
|
70
|
+
out.push(text(fmt(p.value), {
|
|
71
|
+
x: x + bw / 2,
|
|
72
|
+
y: base - h - 16,
|
|
73
|
+
size: 28,
|
|
74
|
+
weight: 800,
|
|
75
|
+
fill: on ? t.accent : t.text,
|
|
76
|
+
anchor: 'middle'
|
|
77
|
+
}));
|
|
78
|
+
out.push(text(p.label, { x: x + bw / 2, y: base + 40, size: 24, fill: t.muted, anchor: 'middle' }));
|
|
79
|
+
});
|
|
80
|
+
out.push(`<rect x="${b.x}" y="${base}" width="${b.w}" height="2" fill="${t.faint}"/>`);
|
|
81
|
+
callout(d, { x: b.x, y: b.y + 100, w: 420 });
|
|
82
|
+
}
|
|
83
|
+
export function multiLine(d) {
|
|
84
|
+
const { out, spec, t, b, fmt } = d;
|
|
85
|
+
const labels = [...new Set(spec.data.map((p) => p.label))];
|
|
86
|
+
const groups = [...new Set(spec.data.map((p) => p.group ?? ''))];
|
|
87
|
+
const max = ceiling(spec.data.map((p) => p.value)) * 1.12;
|
|
88
|
+
const pad = { l: 90, r: 230, t: 40, b: 60 };
|
|
89
|
+
const px = (i) => b.x + pad.l + (i * (b.w - pad.l - pad.r)) / Math.max(1, labels.length - 1);
|
|
90
|
+
const py = (v) => b.y + b.h - pad.b - (v / max) * (b.h - pad.t - pad.b);
|
|
91
|
+
const color = (g, i) => g === spec.highlight ? t.accent : t.palette[(i + 1) % t.palette.length];
|
|
92
|
+
const ends = [];
|
|
93
|
+
grid(d, { px, py, left: b.x + pad.l, width: b.w - pad.l - pad.r, max });
|
|
94
|
+
labels.forEach((l, i) => out.push(text(l, { x: px(i), y: b.y + b.h - pad.b + 40, size: 24, fill: t.muted, anchor: 'middle' })));
|
|
95
|
+
groups.forEach((g, gi) => {
|
|
96
|
+
const pts = labels.flatMap((l, i) => {
|
|
97
|
+
const p = spec.data.find((q) => (q.group ?? '') === g && q.label === l);
|
|
98
|
+
return p ? [{ i, p }] : [];
|
|
99
|
+
});
|
|
100
|
+
const path = pts.map(({ i, p }, k) => `${k ? 'L' : 'M'}${px(i)},${py(p.value)}`).join('');
|
|
101
|
+
const on = g === spec.highlight;
|
|
102
|
+
const last = pts[pts.length - 1];
|
|
103
|
+
out.push(`<path d="${path}" fill="none" stroke="${color(g, gi)}" stroke-width="${on ? 8 : 5}" stroke-linejoin="round" stroke-linecap="round"/>`);
|
|
104
|
+
out.push(`<circle cx="${px(last.i)}" cy="${py(last.p.value)}" r="8" fill="${color(g, gi)}"/>`);
|
|
105
|
+
ends.push({ y: py(last.p.value), x: px(last.i), g, v: last.p.value, c: color(g, gi) });
|
|
106
|
+
});
|
|
107
|
+
dodge(ends, { gap: 56, lo: b.y, hi: b.y + b.h - pad.b });
|
|
108
|
+
for (const e of ends) {
|
|
109
|
+
out.push(text(e.g, { x: e.x + 20, y: e.y + 2, size: 24, weight: 800, fill: e.c }));
|
|
110
|
+
out.push(text(fmt(e.v), { x: e.x + 20, y: e.y + 28, size: 22, fill: t.muted }));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function formatter(chart) {
|
|
2
|
+
return (v, decimals = chart.decimals) => (v < 0 ? '-' : '') +
|
|
3
|
+
(chart.prefix ?? '') +
|
|
4
|
+
Math.abs(v).toLocaleString('en-US', {
|
|
5
|
+
minimumFractionDigits: decimals,
|
|
6
|
+
maximumFractionDigits: decimals
|
|
7
|
+
}) +
|
|
8
|
+
(chart.suffix ?? '');
|
|
9
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Format } from './format.ts';
|
|
2
|
+
import type { Rect } from './geometry.ts';
|
|
3
|
+
import type { Chart } from './spec.ts';
|
|
4
|
+
import type { Theme } from './themes.ts';
|
|
5
|
+
export declare const W = 1080;
|
|
6
|
+
export declare const H = 1350;
|
|
7
|
+
export declare const M = 72;
|
|
8
|
+
export interface Draw {
|
|
9
|
+
out: string[];
|
|
10
|
+
spec: Chart;
|
|
11
|
+
t: Theme;
|
|
12
|
+
b: Rect;
|
|
13
|
+
fmt: Format;
|
|
14
|
+
}
|
|
15
|
+
export interface Mark {
|
|
16
|
+
href: string;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
export interface Frame {
|
|
21
|
+
out: string[];
|
|
22
|
+
box: Rect;
|
|
23
|
+
}
|
|
24
|
+
export declare function frame(spec: Chart, { t, mark }: Framing): Frame;
|
|
25
|
+
export declare function svg(t: Theme, parts: ReadonlyArray<string>): string;
|
|
26
|
+
export interface Framing {
|
|
27
|
+
t: Theme;
|
|
28
|
+
mark?: Mark | undefined;
|
|
29
|
+
}
|
|
30
|
+
export interface Place {
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
w: number;
|
|
34
|
+
}
|
|
35
|
+
export declare function callout({ out, spec, t }: Draw, { x, y, w }: Place): void;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { attr, text, wrap } from './svg.js';
|
|
2
|
+
export const W = 1080;
|
|
3
|
+
export const H = 1350;
|
|
4
|
+
export const M = 72;
|
|
5
|
+
const FONT = "'Helvetica Neue', Helvetica, Arial, sans-serif";
|
|
6
|
+
export function frame(spec, { t, mark }) {
|
|
7
|
+
const out = [];
|
|
8
|
+
const y = header(out, { spec, t });
|
|
9
|
+
const box = { x: M, y: y + 24, w: W - 2 * M, h: H - 150 - (y + 24) };
|
|
10
|
+
footer(out, { spec, t, mark });
|
|
11
|
+
return { out, box };
|
|
12
|
+
}
|
|
13
|
+
export function svg(t, parts) {
|
|
14
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" font-family="${FONT}">` +
|
|
15
|
+
`<rect width="${W}" height="${H}" fill="${t.bg}"/>` +
|
|
16
|
+
parts.join('') +
|
|
17
|
+
'</svg>');
|
|
18
|
+
}
|
|
19
|
+
function header(out, { spec, t }) {
|
|
20
|
+
let y = M + 56;
|
|
21
|
+
for (const line of wrap(spec.title, { width: W - 2 * M, size: 66 })) {
|
|
22
|
+
out.push(text(line, { x: M, y, size: 66, weight: 800, fill: t.text, spacing: -1 }));
|
|
23
|
+
y += 72;
|
|
24
|
+
}
|
|
25
|
+
y += 4;
|
|
26
|
+
for (const line of wrap(spec.subtitle, { width: W - 2 * M, size: 30 })) {
|
|
27
|
+
out.push(text(line, { x: M, y, size: 30, fill: t.muted }));
|
|
28
|
+
y += 40;
|
|
29
|
+
}
|
|
30
|
+
return y;
|
|
31
|
+
}
|
|
32
|
+
function footer(out, { spec, t, mark }) {
|
|
33
|
+
const y = H - 112;
|
|
34
|
+
const width = mark ? W - 2 * M - mark.width - 40 : W - 2 * M;
|
|
35
|
+
const lines = wrap('Source: ' + spec.source, { width, size: 20 }).slice(0, 2);
|
|
36
|
+
out.push(`<rect x="${M}" y="${y}" width="${W - 2 * M}" height="2" fill="${t.faint}"/>`);
|
|
37
|
+
lines.forEach((line, i) => out.push(text(line, { x: M, y: y + 44 + i * 28, size: 20, fill: t.muted })));
|
|
38
|
+
if (!mark) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
out.push(`<image href="${attr(mark.href)}" x="${W - M - mark.width}" y="${y + 46 - mark.height / 2}" width="${mark.width}" height="${mark.height}"/>`);
|
|
42
|
+
}
|
|
43
|
+
export function callout({ out, spec, t }, { x, y, w }) {
|
|
44
|
+
if (!spec.callout)
|
|
45
|
+
return;
|
|
46
|
+
out.push(text(spec.callout.value, {
|
|
47
|
+
x: x + w,
|
|
48
|
+
y,
|
|
49
|
+
size: 120,
|
|
50
|
+
weight: 800,
|
|
51
|
+
fill: t.accent,
|
|
52
|
+
anchor: 'end',
|
|
53
|
+
spacing: -3
|
|
54
|
+
}));
|
|
55
|
+
wrap(spec.callout.text, { width: w, size: 26 }).forEach((line, i) => out.push(text(line, { x: x + w, y: y + 46 + i * 34, size: 26, fill: t.text, anchor: 'end' })));
|
|
56
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface Point {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
export interface Circle extends Point {
|
|
6
|
+
r: number;
|
|
7
|
+
}
|
|
8
|
+
export interface Rect extends Point {
|
|
9
|
+
w: number;
|
|
10
|
+
h: number;
|
|
11
|
+
}
|
|
12
|
+
export interface Polar {
|
|
13
|
+
r: number;
|
|
14
|
+
deg: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function polar(c: Point, { r, deg }: Polar): [number, number];
|
|
17
|
+
export interface Ring {
|
|
18
|
+
outer: number;
|
|
19
|
+
inner: number;
|
|
20
|
+
from: number;
|
|
21
|
+
to: number;
|
|
22
|
+
}
|
|
23
|
+
export declare function arc(c: Point, { outer: R, inner: r, from: a0, to: a1 }: Ring): string;
|
|
24
|
+
export interface Sweep {
|
|
25
|
+
r: number;
|
|
26
|
+
from: number;
|
|
27
|
+
to: number;
|
|
28
|
+
}
|
|
29
|
+
export declare function stroke(c: Point, { r, from: a0, to: a1 }: Sweep): string;
|
|
30
|
+
export interface Span {
|
|
31
|
+
gap: number;
|
|
32
|
+
lo: number;
|
|
33
|
+
hi: number;
|
|
34
|
+
}
|
|
35
|
+
export interface Placed {
|
|
36
|
+
y: number;
|
|
37
|
+
}
|
|
38
|
+
export declare function dodge(items: Placed[], { gap, lo, hi }: Span): void;
|
|
39
|
+
export declare function largestRemainder(shares: ReadonlyArray<number>): number[];
|
|
40
|
+
export declare function pack(radii: ReadonlyArray<number>, gap: number): Circle[];
|
|
41
|
+
export interface Bounds {
|
|
42
|
+
x0: number;
|
|
43
|
+
x1: number;
|
|
44
|
+
y0: number;
|
|
45
|
+
y1: number;
|
|
46
|
+
}
|
|
47
|
+
export declare function bounds(cs: ReadonlyArray<Circle>): Bounds;
|
|
48
|
+
export declare function squarify(values: ReadonlyArray<number>, rect: Rect): Rect[];
|
|
49
|
+
export declare function ceiling(values: ReadonlyArray<number>): number;
|
|
50
|
+
export declare function ticks(max: number): number[];
|