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,247 @@
|
|
|
1
|
+
import { canonical, mapFor, region } from './places.js';
|
|
2
|
+
import { pick } from './pick.js';
|
|
3
|
+
import { MAX_ITEMS } from './schema.js';
|
|
4
|
+
const prefixes = {
|
|
5
|
+
dollars: '$',
|
|
6
|
+
euros: '€',
|
|
7
|
+
pounds: '£',
|
|
8
|
+
reais: 'R$'
|
|
9
|
+
};
|
|
10
|
+
const scales = { K: 1e3, M: 1e6, B: 1e9, T: 1e12 };
|
|
11
|
+
const multipliers = {
|
|
12
|
+
...scales,
|
|
13
|
+
thousands: 1e3,
|
|
14
|
+
millions: 1e6,
|
|
15
|
+
billions: 1e9,
|
|
16
|
+
trillions: 1e12
|
|
17
|
+
};
|
|
18
|
+
const scaleWords = {
|
|
19
|
+
ones: '',
|
|
20
|
+
thousands: 'K',
|
|
21
|
+
millions: 'M',
|
|
22
|
+
billions: 'B',
|
|
23
|
+
trillions: 'T'
|
|
24
|
+
};
|
|
25
|
+
const PARTS = [
|
|
26
|
+
'donut',
|
|
27
|
+
'waffle',
|
|
28
|
+
'treemap',
|
|
29
|
+
'bubbles',
|
|
30
|
+
'stacked',
|
|
31
|
+
'choropleth',
|
|
32
|
+
'bubble-map',
|
|
33
|
+
'tiles'
|
|
34
|
+
];
|
|
35
|
+
const MAP_TYPES = ['choropleth', 'bubble-map', 'tiles'];
|
|
36
|
+
const PERIOD = /^(\d{4}|q[1-4]|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i;
|
|
37
|
+
export function normalize(input, maps) {
|
|
38
|
+
if (input.data.length > MAX_ITEMS) {
|
|
39
|
+
throw new RangeError(`A chart takes at most ${MAX_ITEMS} items, got ${input.data.length}`);
|
|
40
|
+
}
|
|
41
|
+
const spec = arranged(finite(input), maps);
|
|
42
|
+
if (spec.data.length === 0) {
|
|
43
|
+
throw new RangeError('A change needs each item twice, once per moment, named in group');
|
|
44
|
+
}
|
|
45
|
+
const notes = new Map((spec.annotations ?? []).map((a) => [a.label, a.text]));
|
|
46
|
+
const raw = Math.max(...spec.data.map((d) => Math.abs(d.value)));
|
|
47
|
+
const named = spec.unit === 'percent' ? 1 : (multipliers[spec.scale ?? ''] ?? 1);
|
|
48
|
+
const given = raw >= 1e5 ? 1 : named;
|
|
49
|
+
const max = raw * given;
|
|
50
|
+
const rescale = max >= 1e4 ? largest(max) : 1;
|
|
51
|
+
const k = given / rescale;
|
|
52
|
+
const data = spec.data.map((d) => ({
|
|
53
|
+
...d,
|
|
54
|
+
value: d.value * k,
|
|
55
|
+
previous: d.previous === undefined ? undefined : d.previous * k,
|
|
56
|
+
note: notes.get(d.label) ?? d.note
|
|
57
|
+
}));
|
|
58
|
+
const names = new Set(data.flatMap((d) => [d.label, d.group]));
|
|
59
|
+
const highlight = names.has(spec.highlight) ? spec.highlight : undefined;
|
|
60
|
+
const callout = typeof spec.callout === 'string' ? derive(spec.callout, { data, highlight }) : spec.callout;
|
|
61
|
+
const scaled = given !== 1 || rescale !== 1
|
|
62
|
+
? (Object.entries(scales).find(([, s]) => s === rescale)?.[0] ?? '')
|
|
63
|
+
: undefined;
|
|
64
|
+
const scale = scaled ?? scaleWords[spec.scale ?? ''] ?? (spec.scale === 'none' ? '' : spec.scale);
|
|
65
|
+
const prefix = spec.unit ? (prefixes[spec.unit] ?? '') : spec.prefix;
|
|
66
|
+
const suffix = spec.unit ? unitSuffix(spec.unit, scale) : (scaled ?? spec.suffix);
|
|
67
|
+
const type = pick(spec, data);
|
|
68
|
+
return {
|
|
69
|
+
type,
|
|
70
|
+
shape: spec.shape,
|
|
71
|
+
title: spec.title,
|
|
72
|
+
subtitle: spec.subtitle,
|
|
73
|
+
source: spec.source,
|
|
74
|
+
data,
|
|
75
|
+
highlight,
|
|
76
|
+
callout: spec.callout === 'share' && !PARTS.includes(type) ? undefined : callout,
|
|
77
|
+
prefix,
|
|
78
|
+
suffix,
|
|
79
|
+
decimals: decimals(spec, { data, scaled }),
|
|
80
|
+
map: spec.map ?? 'world',
|
|
81
|
+
maps,
|
|
82
|
+
from: spec.from,
|
|
83
|
+
to: spec.to,
|
|
84
|
+
annotations: spec.annotations ?? []
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function largest(max) {
|
|
88
|
+
for (const s of [1e12, 1e9, 1e6]) {
|
|
89
|
+
if (max >= s)
|
|
90
|
+
return s;
|
|
91
|
+
}
|
|
92
|
+
return 1e3;
|
|
93
|
+
}
|
|
94
|
+
function finite(spec) {
|
|
95
|
+
const data = spec.data
|
|
96
|
+
.filter((d) => Number.isFinite(d.value))
|
|
97
|
+
.map((d) => ({ ...d, previous: Number.isFinite(d.previous) ? d.previous : undefined }));
|
|
98
|
+
if (data.length === 0) {
|
|
99
|
+
throw new RangeError('A chart needs at least one finite value');
|
|
100
|
+
}
|
|
101
|
+
return { ...spec, data };
|
|
102
|
+
}
|
|
103
|
+
function arranged(input, maps) {
|
|
104
|
+
let spec = unswap(input);
|
|
105
|
+
if (spec.type && MAP_TYPES.includes(spec.type)) {
|
|
106
|
+
spec = {
|
|
107
|
+
...spec,
|
|
108
|
+
shape: 'places',
|
|
109
|
+
type: undefined,
|
|
110
|
+
variant: spec.type === 'bubble-map' ? 'bubbles' : spec.variant
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (spec.shape === 'places') {
|
|
114
|
+
spec = maps ? places(spec, maps) : { ...spec, shape: 'ranking' };
|
|
115
|
+
}
|
|
116
|
+
if (spec.shape === 'change' && !spec.data.some((d) => d.previous !== undefined)) {
|
|
117
|
+
spec = pairs(spec);
|
|
118
|
+
}
|
|
119
|
+
return spec;
|
|
120
|
+
}
|
|
121
|
+
function unitSuffix(unit, scale) {
|
|
122
|
+
return unit === 'percent' ? '%' : (scale ?? '');
|
|
123
|
+
}
|
|
124
|
+
function decimals(spec, { data, scaled }) {
|
|
125
|
+
const top = Math.max(...data.map((d) => Math.abs(d.value)));
|
|
126
|
+
if (scaled === undefined) {
|
|
127
|
+
const fractional = data.some((d) => !Number.isInteger(d.value));
|
|
128
|
+
return spec.decimals ?? (fractional && top < 1000 ? 1 : 0);
|
|
129
|
+
}
|
|
130
|
+
if (top < 10) {
|
|
131
|
+
return 2;
|
|
132
|
+
}
|
|
133
|
+
return top < 100 ? 1 : 0;
|
|
134
|
+
}
|
|
135
|
+
function unswap(spec) {
|
|
136
|
+
if (spec.shape !== 'over-time')
|
|
137
|
+
return spec;
|
|
138
|
+
const groups = [...new Set(spec.data.map((d) => d.group).filter((g) => Boolean(g)))];
|
|
139
|
+
const swapped = groups.length >= 2 &&
|
|
140
|
+
groups.every((g) => PERIOD.test(g)) &&
|
|
141
|
+
!spec.data.some((d) => PERIOD.test(d.label));
|
|
142
|
+
if (!swapped) {
|
|
143
|
+
return spec;
|
|
144
|
+
}
|
|
145
|
+
if (groups.length === 2) {
|
|
146
|
+
return { ...spec, shape: 'change' };
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
...spec,
|
|
150
|
+
data: spec.data.map((d) => ({ ...d, label: d.group ?? d.label, group: d.label }))
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function places(spec, maps) {
|
|
154
|
+
const map = mapFor(maps, spec.data.map((d) => d.label));
|
|
155
|
+
const seen = new Set();
|
|
156
|
+
const data = [];
|
|
157
|
+
for (const d of spec.data) {
|
|
158
|
+
const label = canonical(maps, { map, label: d.label }) ?? d.label;
|
|
159
|
+
if (seen.has(label)) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
seen.add(label);
|
|
163
|
+
data.push({ ...d, label, region: region(maps, { map, label: d.label }) });
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
...spec,
|
|
167
|
+
map,
|
|
168
|
+
maps,
|
|
169
|
+
data,
|
|
170
|
+
highlight: canonical(maps, { map, label: spec.highlight }) ?? spec.highlight,
|
|
171
|
+
annotations: []
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function pairs(spec) {
|
|
175
|
+
const [from, to] = chronological([...new Set(spec.data.map((d) => d.group ?? ''))]);
|
|
176
|
+
const at = (label, moment) => spec.data.find((d) => d.label === label && (d.group ?? '') === moment)?.value;
|
|
177
|
+
const data = [];
|
|
178
|
+
for (const label of new Set(spec.data.map((d) => d.label))) {
|
|
179
|
+
const previous = at(label, from);
|
|
180
|
+
const value = at(label, to);
|
|
181
|
+
if (previous === undefined || value === undefined) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
data.push({ label, previous, value });
|
|
185
|
+
}
|
|
186
|
+
return { ...spec, from, to, data };
|
|
187
|
+
}
|
|
188
|
+
const MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
|
|
189
|
+
function when(moment) {
|
|
190
|
+
const year = /\d{4}/.exec(moment);
|
|
191
|
+
const quarter = /\bq([1-4])\b/i.exec(moment);
|
|
192
|
+
const month = MONTHS.findIndex((m) => new RegExp(`\\b${m}`, 'i').test(moment));
|
|
193
|
+
if (!year && !quarter && month === -1) {
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
const months = Number(year?.[0] ?? 0) * 12;
|
|
197
|
+
if (quarter) {
|
|
198
|
+
return months + (Number(quarter[1]) - 1) * 3;
|
|
199
|
+
}
|
|
200
|
+
return month === -1 ? months : months + month;
|
|
201
|
+
}
|
|
202
|
+
function chronological(moments) {
|
|
203
|
+
const timed = moments
|
|
204
|
+
.map((m) => ({ m, t: when(m) }))
|
|
205
|
+
.filter((x) => x.t !== undefined);
|
|
206
|
+
if (timed.length < moments.length) {
|
|
207
|
+
return [...moments];
|
|
208
|
+
}
|
|
209
|
+
return timed.sort((a, b) => a.t - b.t).map(({ m }) => m);
|
|
210
|
+
}
|
|
211
|
+
function derive(kind, { data, highlight }) {
|
|
212
|
+
const sorted = [...data].sort((a, b) => b.value - a.value);
|
|
213
|
+
if (kind === 'share') {
|
|
214
|
+
const total = data.reduce((s, d) => s + d.value, 0);
|
|
215
|
+
const d = data.find((x) => x.label === highlight) ?? sorted[0];
|
|
216
|
+
if (total <= 0) {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
return { value: Math.round((d.value / total) * 100) + '%', text: `of the total is ${d.label}` };
|
|
220
|
+
}
|
|
221
|
+
if (kind === 'growth') {
|
|
222
|
+
return growth(data);
|
|
223
|
+
}
|
|
224
|
+
if (kind === 'ratio') {
|
|
225
|
+
return ratio(sorted);
|
|
226
|
+
}
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
function growth(data) {
|
|
230
|
+
const [a, b] = [data[0], data[data.length - 1]];
|
|
231
|
+
if (a.value <= 0) {
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
const x = b.value / a.value;
|
|
235
|
+
const value = x >= 10 ? x.toFixed(0) + '×' : (x >= 1 ? '+' : '') + Math.round((x - 1) * 100) + '%';
|
|
236
|
+
return { value, text: `from ${a.label} to ${b.label}` };
|
|
237
|
+
}
|
|
238
|
+
function ratio(sorted) {
|
|
239
|
+
const [a, b] = sorted;
|
|
240
|
+
if (!b || b.value <= 0 || a.value / b.value < 1.5) {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
value: (a.value / b.value).toFixed(1) + '×',
|
|
245
|
+
text: `${a.label} compared with ${b.label}`
|
|
246
|
+
};
|
|
247
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { isTiles } from './places.js';
|
|
2
|
+
export function pick(spec, data) {
|
|
3
|
+
if (spec.type)
|
|
4
|
+
return spec.type;
|
|
5
|
+
const v = spec.variant;
|
|
6
|
+
const n = data.length;
|
|
7
|
+
if (spec.shape === 'places' && spec.maps) {
|
|
8
|
+
return placesChart(spec, spec.maps);
|
|
9
|
+
}
|
|
10
|
+
if (spec.shape === 'ranking') {
|
|
11
|
+
return v === 'radial' && n <= 10 ? 'radial' : 'ranked-bar';
|
|
12
|
+
}
|
|
13
|
+
if (spec.shape === 'sizes') {
|
|
14
|
+
return 'bubbles';
|
|
15
|
+
}
|
|
16
|
+
if (spec.shape === 'change') {
|
|
17
|
+
return data.some((d) => d.previous !== undefined) ? 'slope' : 'ranked-bar';
|
|
18
|
+
}
|
|
19
|
+
if (spec.shape === 'over-time') {
|
|
20
|
+
return overTime(spec, data);
|
|
21
|
+
}
|
|
22
|
+
return parts(spec, data);
|
|
23
|
+
}
|
|
24
|
+
function hasSeries(data) {
|
|
25
|
+
return (new Set(data.map((d) => d.group).filter(Boolean)).size > 1 &&
|
|
26
|
+
new Set(data.map((d) => d.label)).size < data.length);
|
|
27
|
+
}
|
|
28
|
+
function placesChart(spec, maps) {
|
|
29
|
+
if (isTiles(maps, spec.map ?? 'world'))
|
|
30
|
+
return 'tiles';
|
|
31
|
+
return spec.variant === 'bubbles' ? 'bubble-map' : 'choropleth';
|
|
32
|
+
}
|
|
33
|
+
function overTime(spec, data) {
|
|
34
|
+
if (hasSeries(data))
|
|
35
|
+
return 'multi-line';
|
|
36
|
+
if (spec.variant === 'column' || spec.variant === 'line')
|
|
37
|
+
return spec.variant;
|
|
38
|
+
return data.length <= 6 && !spec.annotations?.length ? 'column' : 'line';
|
|
39
|
+
}
|
|
40
|
+
function parts(spec, data) {
|
|
41
|
+
const v = spec.variant;
|
|
42
|
+
const n = data.length;
|
|
43
|
+
if (hasSeries(data)) {
|
|
44
|
+
return 'stacked';
|
|
45
|
+
}
|
|
46
|
+
if (v === 'stacked') {
|
|
47
|
+
return 'treemap';
|
|
48
|
+
}
|
|
49
|
+
if (v === 'waffle' && n <= 5) {
|
|
50
|
+
return 'waffle';
|
|
51
|
+
}
|
|
52
|
+
if (v === 'donut' && n <= 7) {
|
|
53
|
+
return 'donut';
|
|
54
|
+
}
|
|
55
|
+
if (v === 'treemap' || v === 'bubbles') {
|
|
56
|
+
return v;
|
|
57
|
+
}
|
|
58
|
+
if (n <= 2) {
|
|
59
|
+
return 'waffle';
|
|
60
|
+
}
|
|
61
|
+
return n <= 5 ? 'donut' : 'treemap';
|
|
62
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface GeoRegion {
|
|
2
|
+
name: string;
|
|
3
|
+
d: string;
|
|
4
|
+
cx: number;
|
|
5
|
+
cy: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Tile {
|
|
8
|
+
code: string;
|
|
9
|
+
name: string;
|
|
10
|
+
col: number;
|
|
11
|
+
row: number;
|
|
12
|
+
}
|
|
13
|
+
export interface Maps {
|
|
14
|
+
world: {
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
regions: ReadonlyArray<GeoRegion>;
|
|
18
|
+
aliases: Readonly<Record<string, ReadonlyArray<string>>>;
|
|
19
|
+
};
|
|
20
|
+
tiles: Readonly<Record<string, ReadonlyArray<Tile>>>;
|
|
21
|
+
}
|
|
22
|
+
export declare const isTiles: (maps: Maps, map: string) => boolean;
|
|
23
|
+
export declare function region(maps: Maps, { map, label }: Place): string | undefined;
|
|
24
|
+
export interface Place {
|
|
25
|
+
map: string;
|
|
26
|
+
label: string | undefined;
|
|
27
|
+
}
|
|
28
|
+
export declare function canonical(maps: Maps, place: Place): string | undefined;
|
|
29
|
+
export declare function regionCount(maps: Maps, map: string): number;
|
|
30
|
+
export declare function mapFor(maps: Maps, labels: ReadonlyArray<string>): string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const key = (s) => s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim();
|
|
2
|
+
const indexed = new WeakMap();
|
|
3
|
+
function names(maps) {
|
|
4
|
+
const known = indexed.get(maps);
|
|
5
|
+
if (known) {
|
|
6
|
+
return known;
|
|
7
|
+
}
|
|
8
|
+
const spell = (entries) => new Map(entries.map(([name, id]) => [key(name), id]));
|
|
9
|
+
const world = spell(maps.world.regions.flatMap((r) => [r.name, ...(maps.world.aliases[r.name] ?? [])].map((n) => [n, r.name])));
|
|
10
|
+
const tiles = new Map(Object.entries(maps.tiles).map(([map, grid]) => [
|
|
11
|
+
map,
|
|
12
|
+
spell(grid.flatMap((t) => [
|
|
13
|
+
[t.name, t.code],
|
|
14
|
+
[t.code, t.code]
|
|
15
|
+
]))
|
|
16
|
+
]));
|
|
17
|
+
const built = { world, tiles };
|
|
18
|
+
indexed.set(maps, built);
|
|
19
|
+
return built;
|
|
20
|
+
}
|
|
21
|
+
export const isTiles = (maps, map) => map in maps.tiles;
|
|
22
|
+
export function region(maps, { map, label }) {
|
|
23
|
+
const n = names(maps);
|
|
24
|
+
return (isTiles(maps, map) ? n.tiles.get(map) : n.world)?.get(key(label ?? ''));
|
|
25
|
+
}
|
|
26
|
+
export function canonical(maps, place) {
|
|
27
|
+
const id = region(maps, place);
|
|
28
|
+
if (!isTiles(maps, place.map)) {
|
|
29
|
+
return id;
|
|
30
|
+
}
|
|
31
|
+
return maps.tiles[place.map].find((t) => t.code === id)?.name;
|
|
32
|
+
}
|
|
33
|
+
export function regionCount(maps, map) {
|
|
34
|
+
return isTiles(maps, map) ? maps.tiles[map].length : maps.world.regions.length;
|
|
35
|
+
}
|
|
36
|
+
export function mapFor(maps, labels) {
|
|
37
|
+
const hits = (map) => labels.filter((label) => region(maps, { map, label })).length;
|
|
38
|
+
let best = 'world';
|
|
39
|
+
for (const map of Object.keys(maps.tiles)) {
|
|
40
|
+
if (hits(map) > hits(best)) {
|
|
41
|
+
best = map;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return best;
|
|
45
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Mark } from './frame.ts';
|
|
2
|
+
import type { Chart } from './spec.ts';
|
|
3
|
+
import { type ThemeName } from './themes.ts';
|
|
4
|
+
export interface RenderOptions {
|
|
5
|
+
theme: ThemeName;
|
|
6
|
+
mark?: Mark | undefined;
|
|
7
|
+
}
|
|
8
|
+
export declare function render(chart: Chart, { theme, mark }: RenderOptions): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { charts } from './charts/index.js';
|
|
2
|
+
import { formatter } from './format.js';
|
|
3
|
+
import { frame, svg } from './frame.js';
|
|
4
|
+
import { fixed } from './svg.js';
|
|
5
|
+
import { themes } from './themes.js';
|
|
6
|
+
export function render(chart, { theme, mark }) {
|
|
7
|
+
const t = themes[theme];
|
|
8
|
+
const { out: head, box } = frame(chart, { t, mark });
|
|
9
|
+
const out = [];
|
|
10
|
+
charts[chart.type]({ out, spec: chart, t, b: box, fmt: formatter(chart) });
|
|
11
|
+
return fixed(svg(t, [...head, ...out]));
|
|
12
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export declare const MAX_ITEMS = 30;
|
|
2
|
+
export declare const SHAPES: string[];
|
|
3
|
+
export declare const VARIANTS: string[];
|
|
4
|
+
export declare const UNITS: string[];
|
|
5
|
+
export declare const SCALES: string[];
|
|
6
|
+
export declare const chartSchema: {
|
|
7
|
+
type: string;
|
|
8
|
+
properties: {
|
|
9
|
+
shape: {
|
|
10
|
+
enum: string[];
|
|
11
|
+
};
|
|
12
|
+
variant: {
|
|
13
|
+
enum: string[];
|
|
14
|
+
};
|
|
15
|
+
title: {
|
|
16
|
+
type: string;
|
|
17
|
+
maxLength: number;
|
|
18
|
+
};
|
|
19
|
+
subtitle: {
|
|
20
|
+
type: string;
|
|
21
|
+
maxLength: number;
|
|
22
|
+
};
|
|
23
|
+
data: {
|
|
24
|
+
type: string;
|
|
25
|
+
items: {
|
|
26
|
+
type: string;
|
|
27
|
+
properties: {
|
|
28
|
+
label: {
|
|
29
|
+
type: string;
|
|
30
|
+
maxLength: number;
|
|
31
|
+
};
|
|
32
|
+
value: {
|
|
33
|
+
type: string;
|
|
34
|
+
};
|
|
35
|
+
group: {
|
|
36
|
+
type: string;
|
|
37
|
+
maxLength: number;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
required: string[];
|
|
41
|
+
additionalProperties: boolean;
|
|
42
|
+
};
|
|
43
|
+
minItems: number;
|
|
44
|
+
maxItems: number;
|
|
45
|
+
};
|
|
46
|
+
highlight: {
|
|
47
|
+
type: string;
|
|
48
|
+
maxLength: number;
|
|
49
|
+
};
|
|
50
|
+
scale: {
|
|
51
|
+
enum: string[];
|
|
52
|
+
};
|
|
53
|
+
unit: {
|
|
54
|
+
enum: string[];
|
|
55
|
+
};
|
|
56
|
+
annotations: {
|
|
57
|
+
type: string;
|
|
58
|
+
maxItems: number;
|
|
59
|
+
items: {
|
|
60
|
+
type: string;
|
|
61
|
+
properties: {
|
|
62
|
+
label: {
|
|
63
|
+
type: string;
|
|
64
|
+
maxLength: number;
|
|
65
|
+
};
|
|
66
|
+
text: {
|
|
67
|
+
type: string;
|
|
68
|
+
maxLength: number;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
required: string[];
|
|
72
|
+
additionalProperties: boolean;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
callout: {
|
|
76
|
+
enum: string[];
|
|
77
|
+
};
|
|
78
|
+
source: {
|
|
79
|
+
type: string;
|
|
80
|
+
maxLength: number;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
required: string[];
|
|
84
|
+
additionalProperties: boolean;
|
|
85
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export const MAX_ITEMS = 30;
|
|
2
|
+
const item = {
|
|
3
|
+
type: 'object',
|
|
4
|
+
properties: {
|
|
5
|
+
label: { type: 'string', maxLength: 32 },
|
|
6
|
+
value: { type: 'number' },
|
|
7
|
+
group: { type: 'string', maxLength: 24 }
|
|
8
|
+
},
|
|
9
|
+
required: ['label', 'value'],
|
|
10
|
+
additionalProperties: false
|
|
11
|
+
};
|
|
12
|
+
export const SHAPES = ['ranking', 'parts', 'over-time', 'change', 'sizes', 'places'];
|
|
13
|
+
export const VARIANTS = [
|
|
14
|
+
'bar',
|
|
15
|
+
'radial',
|
|
16
|
+
'donut',
|
|
17
|
+
'waffle',
|
|
18
|
+
'treemap',
|
|
19
|
+
'stacked',
|
|
20
|
+
'bubbles',
|
|
21
|
+
'line',
|
|
22
|
+
'column'
|
|
23
|
+
];
|
|
24
|
+
export const UNITS = ['number', 'percent', 'dollars', 'euros', 'pounds', 'reais'];
|
|
25
|
+
export const SCALES = ['ones', 'thousands', 'millions', 'billions', 'trillions'];
|
|
26
|
+
export const chartSchema = {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
shape: { enum: SHAPES },
|
|
30
|
+
variant: { enum: VARIANTS },
|
|
31
|
+
title: { type: 'string', maxLength: 64 },
|
|
32
|
+
subtitle: { type: 'string', maxLength: 110 },
|
|
33
|
+
data: { type: 'array', items: item, minItems: 2, maxItems: MAX_ITEMS },
|
|
34
|
+
highlight: { type: 'string', maxLength: 32 },
|
|
35
|
+
scale: { enum: SCALES },
|
|
36
|
+
unit: { enum: UNITS },
|
|
37
|
+
annotations: {
|
|
38
|
+
type: 'array',
|
|
39
|
+
maxItems: 2,
|
|
40
|
+
items: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
label: { type: 'string', maxLength: 32 },
|
|
44
|
+
text: { type: 'string', maxLength: 70 }
|
|
45
|
+
},
|
|
46
|
+
required: ['label', 'text'],
|
|
47
|
+
additionalProperties: false
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
callout: { enum: ['none', 'share', 'growth', 'ratio'] },
|
|
51
|
+
source: { type: 'string', maxLength: 120 }
|
|
52
|
+
},
|
|
53
|
+
required: [
|
|
54
|
+
'shape',
|
|
55
|
+
'title',
|
|
56
|
+
'subtitle',
|
|
57
|
+
'data',
|
|
58
|
+
'highlight',
|
|
59
|
+
'scale',
|
|
60
|
+
'unit',
|
|
61
|
+
'callout',
|
|
62
|
+
'source'
|
|
63
|
+
],
|
|
64
|
+
additionalProperties: false
|
|
65
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Maps } from './places.ts';
|
|
2
|
+
export type ChartType = 'ranked-bar' | 'bubbles' | 'treemap' | 'line' | 'column' | 'multi-line' | 'donut' | 'waffle' | 'stacked' | 'slope' | 'radial' | 'choropleth' | 'bubble-map' | 'tiles';
|
|
3
|
+
export interface Item {
|
|
4
|
+
label: string;
|
|
5
|
+
value: number;
|
|
6
|
+
group?: string | undefined;
|
|
7
|
+
previous?: number | undefined;
|
|
8
|
+
note?: string | undefined;
|
|
9
|
+
}
|
|
10
|
+
export interface Annotation {
|
|
11
|
+
label: string;
|
|
12
|
+
text: string;
|
|
13
|
+
}
|
|
14
|
+
export interface Callout {
|
|
15
|
+
value: string;
|
|
16
|
+
text: string;
|
|
17
|
+
}
|
|
18
|
+
export interface Spec {
|
|
19
|
+
shape?: string;
|
|
20
|
+
variant?: string;
|
|
21
|
+
type?: ChartType;
|
|
22
|
+
title: string;
|
|
23
|
+
subtitle: string;
|
|
24
|
+
data: ReadonlyArray<Item>;
|
|
25
|
+
highlight?: string | undefined;
|
|
26
|
+
scale?: string;
|
|
27
|
+
unit?: string;
|
|
28
|
+
prefix?: string;
|
|
29
|
+
suffix?: string;
|
|
30
|
+
decimals?: number;
|
|
31
|
+
annotations?: ReadonlyArray<Annotation>;
|
|
32
|
+
callout?: string | Callout;
|
|
33
|
+
source: string;
|
|
34
|
+
map?: string;
|
|
35
|
+
from?: string | undefined;
|
|
36
|
+
to?: string | undefined;
|
|
37
|
+
}
|
|
38
|
+
export interface Row extends Item {
|
|
39
|
+
region?: string | undefined;
|
|
40
|
+
}
|
|
41
|
+
export interface Chart {
|
|
42
|
+
type: ChartType;
|
|
43
|
+
shape?: string | undefined;
|
|
44
|
+
title: string;
|
|
45
|
+
subtitle: string;
|
|
46
|
+
source: string;
|
|
47
|
+
data: ReadonlyArray<Row>;
|
|
48
|
+
highlight: string | undefined;
|
|
49
|
+
callout: Callout | undefined;
|
|
50
|
+
prefix: string | undefined;
|
|
51
|
+
suffix: string | undefined;
|
|
52
|
+
decimals: number;
|
|
53
|
+
map: string;
|
|
54
|
+
maps: Maps | undefined;
|
|
55
|
+
from: string | undefined;
|
|
56
|
+
to: string | undefined;
|
|
57
|
+
annotations: ReadonlyArray<Annotation>;
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface TextStyle {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
size: number;
|
|
5
|
+
fill: string;
|
|
6
|
+
weight?: number;
|
|
7
|
+
anchor?: 'start' | 'middle' | 'end';
|
|
8
|
+
spacing?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function attr(value: string): string;
|
|
11
|
+
export declare function text(str: string | number, style: TextStyle): string;
|
|
12
|
+
export interface HaloStyle extends TextStyle {
|
|
13
|
+
stroke: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function halo(str: string, { stroke, ...style }: HaloStyle): string;
|
|
16
|
+
export interface Measure {
|
|
17
|
+
width: number;
|
|
18
|
+
size: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function wrap(str: string, { width, size }: Measure): string[];
|
|
21
|
+
export declare function fixed(svg: string): string;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export function attr(value) {
|
|
2
|
+
return value
|
|
3
|
+
.replace(/&/g, '&')
|
|
4
|
+
.replace(/"/g, '"')
|
|
5
|
+
.replace(/</g, '<')
|
|
6
|
+
.replace(/>/g, '>');
|
|
7
|
+
}
|
|
8
|
+
export function text(str, style) {
|
|
9
|
+
const { x, y, size, fill, weight = 400, anchor = 'start', spacing = 0 } = style;
|
|
10
|
+
const esc = String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
11
|
+
return `<text x="${x}" y="${y}" font-size="${size}" font-weight="${weight}" fill="${fill}" text-anchor="${anchor}" letter-spacing="${spacing}">${esc}</text>`;
|
|
12
|
+
}
|
|
13
|
+
export function halo(str, { stroke, ...style }) {
|
|
14
|
+
return text(str, style).replace('<text ', `<text stroke="${stroke}" stroke-width="6" stroke-linejoin="round" paint-order="stroke" `);
|
|
15
|
+
}
|
|
16
|
+
export function wrap(str, { width, size }) {
|
|
17
|
+
const per = Math.floor(width / (size * 0.54));
|
|
18
|
+
const lines = [];
|
|
19
|
+
let line = '';
|
|
20
|
+
for (const word of str.split(/\s+/)) {
|
|
21
|
+
if (line && (line + ' ' + word).length > per) {
|
|
22
|
+
lines.push(line);
|
|
23
|
+
line = word;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
line = line ? line + ' ' + word : word;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (line) {
|
|
30
|
+
lines.push(line);
|
|
31
|
+
}
|
|
32
|
+
return lines;
|
|
33
|
+
}
|
|
34
|
+
export function fixed(svg) {
|
|
35
|
+
return svg.replace(/<[^<>]+>/g, (tag) => tag.replace(/(?<![\w-])([\w-]+)="([^"]*)"/g, rounded));
|
|
36
|
+
}
|
|
37
|
+
function rounded(whole, name, value) {
|
|
38
|
+
if (name === 'href') {
|
|
39
|
+
return whole;
|
|
40
|
+
}
|
|
41
|
+
const short = value.replace(/-?(?<![\d.])\d+\.\d{3,}/g, (n) => String(Math.round(Number(n) * 100) / 100));
|
|
42
|
+
return `${name}="${short}"`;
|
|
43
|
+
}
|