cseen 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +169 -2
- package/dist/all.d.ts +9 -0
- package/dist/all.js +11 -0
- package/dist/all.js.map +1 -0
- package/dist/anim/mount.d.ts +69 -0
- package/dist/anim/mount.js +234 -0
- package/dist/anim/mount.js.map +1 -0
- package/dist/anim/orbit.d.ts +85 -0
- package/dist/anim/orbit.js +198 -0
- package/dist/anim/orbit.js.map +1 -0
- package/dist/anim/timeline.d.ts +287 -0
- package/dist/anim/timeline.js +379 -0
- package/dist/anim/timeline.js.map +1 -0
- package/dist/charts/charts.d.ts +216 -0
- package/dist/charts/charts.js +570 -0
- package/dist/charts/charts.js.map +1 -0
- package/dist/charts/index.d.ts +2 -0
- package/dist/charts/index.js +2 -0
- package/dist/charts/index.js.map +1 -0
- package/dist/construct/construct.d.ts +73 -0
- package/dist/construct/construct.js +96 -0
- package/dist/construct/construct.js.map +1 -0
- package/dist/construct/geom.d.ts +91 -0
- package/dist/construct/geom.js +118 -0
- package/dist/construct/geom.js.map +1 -0
- package/dist/core/animatable.d.ts +19 -0
- package/dist/core/animatable.js +41 -0
- package/dist/core/animatable.js.map +1 -0
- package/dist/core/camera.d.ts +48 -0
- package/dist/core/camera.js +95 -0
- package/dist/core/camera.js.map +1 -0
- package/dist/core/math.d.ts +39 -0
- package/dist/core/math.js +104 -0
- package/dist/core/math.js.map +1 -0
- package/dist/core/nodes.d.ts +387 -0
- package/dist/core/nodes.js +418 -0
- package/dist/core/nodes.js.map +1 -0
- package/dist/core/render.d.ts +98 -0
- package/dist/core/render.js +386 -0
- package/dist/core/render.js.map +1 -0
- package/dist/core/scene.d.ts +96 -0
- package/dist/core/scene.js +104 -0
- package/dist/core/scene.js.map +1 -0
- package/dist/core/sort.d.ts +40 -0
- package/dist/core/sort.js +181 -0
- package/dist/core/sort.js.map +1 -0
- package/dist/core/split.d.ts +47 -0
- package/dist/core/split.js +169 -0
- package/dist/core/split.js.map +1 -0
- package/dist/core/svg-import.d.ts +16 -0
- package/dist/core/svg-import.js +53 -0
- package/dist/core/svg-import.js.map +1 -0
- package/dist/cseen.css +106 -0
- package/dist/devices/devices.d.ts +307 -0
- package/dist/devices/devices.js +327 -0
- package/dist/devices/devices.js.map +1 -0
- package/dist/devices/index.d.ts +6 -0
- package/dist/devices/index.js +4 -0
- package/dist/devices/index.js.map +1 -0
- package/dist/devices/keyboard.d.ts +86 -0
- package/dist/devices/keyboard.js +186 -0
- package/dist/devices/keyboard.js.map +1 -0
- package/dist/devices/peripherals.d.ts +103 -0
- package/dist/devices/peripherals.js +171 -0
- package/dist/devices/peripherals.js.map +1 -0
- package/dist/draw/heads.d.ts +11 -0
- package/dist/draw/heads.js +26 -0
- package/dist/draw/heads.js.map +1 -0
- package/dist/draw/link.d.ts +76 -0
- package/dist/draw/link.js +102 -0
- package/dist/draw/link.js.map +1 -0
- package/dist/draw/overlap.d.ts +50 -0
- package/dist/draw/overlap.js +80 -0
- package/dist/draw/overlap.js.map +1 -0
- package/dist/draw/pen.d.ts +41 -0
- package/dist/draw/pen.js +57 -0
- package/dist/draw/pen.js.map +1 -0
- package/dist/draw/shapes.d.ts +206 -0
- package/dist/draw/shapes.js +225 -0
- package/dist/draw/shapes.js.map +1 -0
- package/dist/draw/svgplane.d.ts +74 -0
- package/dist/draw/svgplane.js +97 -0
- package/dist/draw/svgplane.js.map +1 -0
- package/dist/draw/text.d.ts +111 -0
- package/dist/draw/text.js +204 -0
- package/dist/draw/text.js.map +1 -0
- package/dist/draw/words.d.ts +29 -0
- package/dist/draw/words.js +30 -0
- package/dist/draw/words.js.map +1 -0
- package/dist/flow/flow.d.ts +206 -0
- package/dist/flow/flow.js +408 -0
- package/dist/flow/flow.js.map +1 -0
- package/dist/flow/index.d.ts +4 -0
- package/dist/flow/index.js +3 -0
- package/dist/flow/index.js.map +1 -0
- package/dist/flow/layout.d.ts +66 -0
- package/dist/flow/layout.js +353 -0
- package/dist/flow/layout.js.map +1 -0
- package/dist/flow/sequence.d.ts +114 -0
- package/dist/flow/sequence.js +309 -0
- package/dist/flow/sequence.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/math/index.d.ts +2 -0
- package/dist/math/index.js +2 -0
- package/dist/math/index.js.map +1 -0
- package/dist/math/math.d.ts +238 -0
- package/dist/math/math.js +421 -0
- package/dist/math/math.js.map +1 -0
- package/dist/optics/index.d.ts +2 -0
- package/dist/optics/index.js +2 -0
- package/dist/optics/index.js.map +1 -0
- package/dist/optics/optics.d.ts +94 -0
- package/dist/optics/optics.js +173 -0
- package/dist/optics/optics.js.map +1 -0
- package/dist/ui/index.d.ts +2 -0
- package/dist/ui/index.js +2 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/ui/ui.d.ts +166 -0
- package/dist/ui/ui.js +255 -0
- package/dist/ui/ui.js.map +1 -0
- package/dist/uml/factory.d.ts +15 -0
- package/dist/uml/factory.js +16 -0
- package/dist/uml/factory.js.map +1 -0
- package/dist/uml/index.d.ts +3 -0
- package/dist/uml/index.js +3 -0
- package/dist/uml/index.js.map +1 -0
- package/dist/uml/uml.d.ts +187 -0
- package/dist/uml/uml.js +599 -0
- package/dist/uml/uml.js.map +1 -0
- package/dist/waves/index.d.ts +2 -0
- package/dist/waves/index.js +2 -0
- package/dist/waves/index.js.map +1 -0
- package/dist/waves/waves.d.ts +93 -0
- package/dist/waves/waves.js +206 -0
- package/dist/waves/waves.js.map +1 -0
- package/package.json +85 -4
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { SvgPlane, colourOf, esc, clamp, r1, nice, niceTicks, easeOut, hashId, INK, FAINT, GRID, BG } from "../draw/svgplane.js";
|
|
2
|
+
export { niceTicks };
|
|
3
|
+
/* ------------------------------------------------------------------ small helpers */
|
|
4
|
+
/** item i of n, revealed by a shared `grow` from 0 to 1, with each item a little after the last */
|
|
5
|
+
const staged = (grow, i, n) => easeOut(grow * 1.5 - (0.5 * i) / Math.max(1, n - 1));
|
|
6
|
+
/** the default number format: whole thousands as `k`, millions as `M`, otherwise at most two decimals */
|
|
7
|
+
const fmt = (v) => (Math.abs(v) >= 1e6 ? `${+(v / 1e6).toFixed(1)}M` : Math.abs(v) >= 1e4 ? `${+(v / 1e3).toFixed(1)}k` : `${+v.toFixed(2)}`);
|
|
8
|
+
/** a smooth path through points that never overshoots (monotone cubic) */
|
|
9
|
+
function smooth(p) {
|
|
10
|
+
const n = p.length;
|
|
11
|
+
if (n < 3)
|
|
12
|
+
return p.map((q, i) => `${i ? 'L' : 'M'}${r1(q[0])} ${r1(q[1])}`).join('');
|
|
13
|
+
const d = p.slice(1).map((q, i) => (q[1] - p[i][1]) / ((q[0] - p[i][0]) || 1));
|
|
14
|
+
const m = p.map((_, i) => (i === 0 ? d[0] : i === n - 1 ? d[n - 2] : d[i - 1] * d[i] <= 0 ? 0 : (d[i - 1] + d[i]) / 2));
|
|
15
|
+
for (let i = 0; i < n - 1; i++) {
|
|
16
|
+
if (d[i] === 0) {
|
|
17
|
+
m[i] = 0;
|
|
18
|
+
m[i + 1] = 0;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const a = m[i] / d[i], b = m[i + 1] / d[i], s = a * a + b * b;
|
|
22
|
+
if (s > 9) {
|
|
23
|
+
const t = 3 / Math.sqrt(s);
|
|
24
|
+
m[i] = t * a * d[i];
|
|
25
|
+
m[i + 1] = t * b * d[i];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let s = `M${r1(p[0][0])} ${r1(p[0][1])}`;
|
|
29
|
+
for (let i = 0; i < n - 1; i++) {
|
|
30
|
+
const h = (p[i + 1][0] - p[i][0]) / 3;
|
|
31
|
+
s += `C${r1(p[i][0] + h)} ${r1(p[i][1] + m[i] * h)} ${r1(p[i + 1][0] - h)} ${r1(p[i + 1][1] - m[i + 1] * h)} ${r1(p[i + 1][0])} ${r1(p[i + 1][1])}`;
|
|
32
|
+
}
|
|
33
|
+
return s;
|
|
34
|
+
}
|
|
35
|
+
/** a path that moves along, then up or down, so each value holds until the next point */
|
|
36
|
+
const stepPath = (p) => p.map((q, i) => (i ? `H${r1(q[0])}V${r1(q[1])}` : `M${r1(q[0])} ${r1(q[1])}`)).join('');
|
|
37
|
+
/** the path of one line through its points in the chosen curve style */
|
|
38
|
+
const linePath = (p, curve) => (curve === 'smooth' ? smooth(p) : curve === 'step' ? stepPath(p) : p.map((q, i) => `${i ? 'L' : 'M'}${r1(q[0])} ${r1(q[1])}`).join(''));
|
|
39
|
+
/** The name of the i-th series when you gave none, as a spreadsheet names columns: A to Z, then AA, AB and so on. */
|
|
40
|
+
export function seriesName(i) {
|
|
41
|
+
let n = Math.max(0, Math.floor(i)) + 1, s = '';
|
|
42
|
+
for (; n > 0; n = Math.floor((n - 1) / 26))
|
|
43
|
+
s = String.fromCharCode(65 + ((n - 1) % 26)) + s;
|
|
44
|
+
return s;
|
|
45
|
+
}
|
|
46
|
+
/** The table behind any form of data: the labels along the axis and one series per colour. */
|
|
47
|
+
export function toTable(data, labels) {
|
|
48
|
+
if (!data)
|
|
49
|
+
return { labels: labels ?? [], series: [] };
|
|
50
|
+
const lab = (n) => labels ?? Array.from({ length: n }, (_, i) => String(i + 1));
|
|
51
|
+
if (Array.isArray(data)) {
|
|
52
|
+
if (!data.length)
|
|
53
|
+
return { labels: [], series: [] };
|
|
54
|
+
const first = data[0];
|
|
55
|
+
if (typeof first === 'number')
|
|
56
|
+
return { labels: lab(data.length), series: [{ name: '', values: data }] };
|
|
57
|
+
if (Array.isArray(first) && typeof first[0] === 'string')
|
|
58
|
+
return { labels: data.map((d) => d[0]), series: [{ name: '', values: data.map((d) => d[1]) }] };
|
|
59
|
+
if (Array.isArray(first))
|
|
60
|
+
return { labels: lab(first.length), series: data.map((v, i) => ({ name: seriesName(i), values: v })) };
|
|
61
|
+
return { labels: data.map((d) => d.label), series: [{ name: '', values: data.map((d) => d.value) }] };
|
|
62
|
+
}
|
|
63
|
+
const obj = data;
|
|
64
|
+
if (Array.isArray(obj.series)) {
|
|
65
|
+
const series = obj.series;
|
|
66
|
+
return { labels: obj.labels ?? lab(series[0]?.values.length ?? 0), series };
|
|
67
|
+
}
|
|
68
|
+
const map = data, names = Object.keys(map);
|
|
69
|
+
return { labels: lab(map[names[0]]?.length ?? 0), series: names.map((name) => ({ name, values: map[name] })) };
|
|
70
|
+
}
|
|
71
|
+
/* ------------------------------------------------------------------ the chart */
|
|
72
|
+
/**
|
|
73
|
+
* A chart drawn on a plane in the scene: flat on the page, standing up, or on a screen (`device.put`). It takes the
|
|
74
|
+
* theme's colours and updates with them. `grow` (0 to 1) reveals it, and `data` (the numbers flattened) can be
|
|
75
|
+
* tweened to morph from one dataset to another: `tween(c, { data: c.flatten(next) })`.
|
|
76
|
+
*/
|
|
77
|
+
export class Chart extends SvgPlane {
|
|
78
|
+
table0;
|
|
79
|
+
uid;
|
|
80
|
+
constructor(o = {}) {
|
|
81
|
+
super(o, [4, 2.5]);
|
|
82
|
+
this.table0 = o.matrix
|
|
83
|
+
? { labels: o.colLabels ?? (o.matrix[0] ?? []).map((_, i) => String(i + 1)), series: o.matrix.map((values, i) => ({ name: o.rowLabels?.[i] ?? String(i + 1), values })) }
|
|
84
|
+
: toTable(o.data, o.labels);
|
|
85
|
+
this.props.grow = 1;
|
|
86
|
+
this.props.data = this.flatten(this.table0);
|
|
87
|
+
this.uid = `szc${Math.abs(hashId(this.id))}`;
|
|
88
|
+
}
|
|
89
|
+
/** all the numbers of a table in one list: what the `data` property holds, and what you tween to */
|
|
90
|
+
flatten(t) {
|
|
91
|
+
return toTable(t, this.o.labels).series.flatMap((x) => x.values);
|
|
92
|
+
}
|
|
93
|
+
/** the current table: the original shape with the numbers from the `data` property, which may be mid-tween. A value that is not a finite number counts as 0, so one bad entry cannot spoil the whole drawing. */
|
|
94
|
+
table() {
|
|
95
|
+
const flat = this.props.data;
|
|
96
|
+
let k = 0;
|
|
97
|
+
return { labels: this.table0.labels, series: this.table0.series.map((s) => ({ ...s, values: s.values.map((v) => { const x = flat[k++] ?? v; return Number.isFinite(x) ? x : 0; }) })) };
|
|
98
|
+
}
|
|
99
|
+
draw(W, H) { return draw(this, W, H); }
|
|
100
|
+
/** the id the line charts give their reveal clip path, unique to this chart. @internal */ get clipId() { return this.uid; }
|
|
101
|
+
}
|
|
102
|
+
/** A chart. `chart({ type: 'bar', data: [3, 5, 2] })`, or use the shortcuts `bars`, `lines`, `pie`... */
|
|
103
|
+
export const chart = (o = {}) => new Chart(o);
|
|
104
|
+
/** makes a named factory that is `chart` with the type fixed */
|
|
105
|
+
const shortcut = (type) => (o = {}) => new Chart({ ...o, type });
|
|
106
|
+
/**
|
|
107
|
+
* A bar chart. `data` can be numbers, several series, label and value pairs, or a map of series.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* const s = new Scene({ view: 'front' });
|
|
111
|
+
* s.add(bars({ data: [12, 19, 7, 15, 22, 9], labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'], title: 'Signups', labelValues: true }));
|
|
112
|
+
*/
|
|
113
|
+
export const bars = shortcut('bar');
|
|
114
|
+
/**
|
|
115
|
+
* A line chart. Several series are drawn together, joined linearly, smoothly or in steps.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* const s = new Scene({ view: 'front' });
|
|
119
|
+
* s.add(lines({ data: { Latency: [220, 180, 240, 190, 150, 170], Errors: [90, 110, 80, 60, 70, 40] }, curve: 'smooth' }));
|
|
120
|
+
*/
|
|
121
|
+
export const lines = shortcut('line');
|
|
122
|
+
/**
|
|
123
|
+
* An area chart: overlapping filled areas, or stacked with `stack: true`.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* const s = new Scene({ view: 'front' });
|
|
127
|
+
* s.add(areas({ data: { A: [3, 5, 4, 7, 6, 8], B: [2, 3, 5, 4, 6, 5] }, stack: true }));
|
|
128
|
+
*/
|
|
129
|
+
export const areas = shortcut('area');
|
|
130
|
+
/**
|
|
131
|
+
* A scatter plot. Points can carry a size `r` (bubbles), a value `v` for colour, and a series `s`.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* const s = new Scene({ view: 'front' });
|
|
135
|
+
* s.add(scatter({ points: [{ x: 1, y: 2 }, { x: 2, y: 3.5 }, { x: 3, y: 3 }, { x: 4, y: 5.2 }, { x: 5, y: 4.8 }], title: 'Scatter' }));
|
|
136
|
+
*/
|
|
137
|
+
export const scatter = shortcut('scatter');
|
|
138
|
+
/**
|
|
139
|
+
* A pie chart of the shares in `data`.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* const s = new Scene({ view: 'front' });
|
|
143
|
+
* s.add(pie({ data: [['Chrome', 62], ['Safari', 20], ['Firefox', 8], ['Other', 10]], labelValues: true, legend: 'right' }));
|
|
144
|
+
*/
|
|
145
|
+
export const pie = shortcut('pie');
|
|
146
|
+
/**
|
|
147
|
+
* A pie with a hole. Takes the same options as `pie`.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* const s = new Scene({ view: 'front' });
|
|
151
|
+
* s.add(donut({ data: [['Done', 70], ['Open', 20], ['Blocked', 10]], labelValues: true }));
|
|
152
|
+
*/
|
|
153
|
+
export const donut = shortcut('donut');
|
|
154
|
+
/**
|
|
155
|
+
* A histogram of raw `values`, binned into `bins` bars.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* const s = new Scene({ view: 'front' });
|
|
159
|
+
* s.add(histogram({ values: [1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 7, 7, 8], bins: 6, title: 'Histogram' }));
|
|
160
|
+
*/
|
|
161
|
+
export const histogram = shortcut('histogram');
|
|
162
|
+
/**
|
|
163
|
+
* A matrix coloured through the theme's sequential ramp, or a diverging ramp for values around zero.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* const s = new Scene({ view: 'front' });
|
|
167
|
+
* s.add(heatmap({ matrix: [[1, 3, 5, 2], [4, 6, 9, 3], [7, 2, 4, 8]], rowLabels: ['Mon', 'Tue', 'Wed'], colLabels: ['9', '10', '11', '12'], labelValues: true }));
|
|
168
|
+
*/
|
|
169
|
+
export const heatmap = shortcut('heatmap');
|
|
170
|
+
/**
|
|
171
|
+
* A radar chart: one spoke per label, one polygon per series.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* const s = new Scene({ view: 'front' });
|
|
175
|
+
* s.add(radar({ data: { Alpha: [8, 6, 7, 5, 9], Beta: [5, 8, 4, 9, 6] }, labels: ['A', 'B', 'C', 'D', 'E'] }));
|
|
176
|
+
*/
|
|
177
|
+
export const radar = shortcut('radar');
|
|
178
|
+
/**
|
|
179
|
+
* A small line chart with no axes, the size of a word.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* const s = new Scene({ view: 'front' });
|
|
183
|
+
* s.add(sparkline({ data: [3, 5, 4, 8, 6, 9, 7, 11], size: [4, 1.4] }));
|
|
184
|
+
*/
|
|
185
|
+
export const sparkline = shortcut('sparkline');
|
|
186
|
+
/* ------------------------------------------------------------------ colour */
|
|
187
|
+
/** the colour of item i of n, following the palette option. `names` lets `highlight` pick by name; `explicit` is the series' own colour. */
|
|
188
|
+
function paint(o, i, n, names, explicit) {
|
|
189
|
+
if (n === 1 && !explicit && o.highlight === undefined && o.palette === undefined)
|
|
190
|
+
return 'var(--c-s1)';
|
|
191
|
+
const hi = o.highlight === undefined ? null : (Array.isArray(o.highlight) ? o.highlight : [o.highlight]);
|
|
192
|
+
if (hi)
|
|
193
|
+
return hi.some((h) => h === i || h === names[i]) ? 'var(--c-accent)' : 'color-mix(in oklab, var(--c-faint) 70%, var(--c-bg))';
|
|
194
|
+
if (explicit)
|
|
195
|
+
return colourOf(explicit);
|
|
196
|
+
const p = o.palette ?? 'categorical', t = n <= 1 ? 1 : i / (n - 1);
|
|
197
|
+
if (typeof p === 'function')
|
|
198
|
+
return colourOf(p(i, n));
|
|
199
|
+
if (Array.isArray(p))
|
|
200
|
+
return colourOf(p[i % p.length]);
|
|
201
|
+
if (p === 'sequential')
|
|
202
|
+
return `color-mix(in oklab, var(--c-hi) ${Math.round(28 + 72 * t)}%, var(--c-lo))`;
|
|
203
|
+
if (p === 'mono')
|
|
204
|
+
return `color-mix(in oklab, var(--c-accent) ${Math.round(100 - 60 * t)}%, var(--c-bg))`;
|
|
205
|
+
if (p === 'diverging')
|
|
206
|
+
return t < 0.5 ? `color-mix(in oklab, var(--c-s2) ${Math.round((1 - 2 * t) * 100)}%, var(--c-lo))` : `color-mix(in oklab, var(--c-hi) ${Math.round((2 * t - 1) * 100)}%, var(--c-lo))`;
|
|
207
|
+
// categorical: the theme's eight, then the same eight softened
|
|
208
|
+
const k = i % 8, c = `var(--c-s${k + 1})`;
|
|
209
|
+
return i < 8 ? c : `color-mix(in oklab, ${c} 65%, var(--c-bg))`;
|
|
210
|
+
}
|
|
211
|
+
/** a value from 0 to 1 as a colour on the ramp, for heatmaps and value-coloured points */
|
|
212
|
+
function ramp(o, t) {
|
|
213
|
+
if (o.palette === 'diverging')
|
|
214
|
+
return paint({ ...o, highlight: undefined }, Math.round(clamp(t) * 100), 101, []);
|
|
215
|
+
return `color-mix(in oklab, var(--c-hi) ${Math.round(clamp(t) * 100)}%, var(--c-lo))`;
|
|
216
|
+
}
|
|
217
|
+
/* ------------------------------------------------------------------ drawing */
|
|
218
|
+
/** Lays out the margins, title, axes and legend of a chart, hands the plot rectangle to the drawer for its type, and adds the legend. W and H are in svg units. */
|
|
219
|
+
function draw(c, W, H) {
|
|
220
|
+
const o = c.o, type = o.type ?? 'bar', fs = Math.max(6, Math.min(W, H) * 0.046), grow = clamp(c.props.grow);
|
|
221
|
+
const t = c.table(), nS = t.series.length, nC = t.labels.length;
|
|
222
|
+
const names = type === 'pie' || type === 'donut' || (nS === 1 && type !== 'radar' && type !== 'heatmap') ? t.labels : t.series.map((s) => s.name);
|
|
223
|
+
const axesOpt = o.axes === false ? { x: false, y: false, grid: false } : typeof o.axes === 'object' ? { x: true, y: true, grid: true, ...o.axes } : { x: true, y: true, grid: true };
|
|
224
|
+
const bare = type === 'sparkline' || type === 'pie' || type === 'donut' || type === 'radar' || type === 'heatmap';
|
|
225
|
+
const showAxes = !bare && o.axes !== false;
|
|
226
|
+
const valFmt = typeof o.labelValues === 'function' ? o.labelValues : o.format ?? fmt;
|
|
227
|
+
const pad = (o.pad ?? 0.08) * 100;
|
|
228
|
+
const legendPos = o.legend === false ? null : o.legend === true || o.legend === undefined ? (((type === 'pie' || type === 'donut') ? nC > 1 : nS > 1 && type !== 'heatmap' && type !== 'scatter') || o.legend === true ? 'top' : null) : o.legend;
|
|
229
|
+
let top = pad + (o.title ? fs * 2 : 0), bottom = pad, left = pad, right = pad;
|
|
230
|
+
let svg = '';
|
|
231
|
+
if (o.title)
|
|
232
|
+
svg += `<text x="${pad}" y="${pad + fs * 1.15}" font-size="${fs * 1.2}" font-weight="700" fill="${INK}">${esc(o.title)}</text>`;
|
|
233
|
+
if (legendPos === 'top')
|
|
234
|
+
top += fs * 1.9;
|
|
235
|
+
if (legendPos === 'bottom')
|
|
236
|
+
bottom += fs * 1.9;
|
|
237
|
+
const legendItems = (type === 'pie' || type === 'donut' || nS === 1 ? t.labels : t.series.map((s) => s.name)).map((name, i, all) => ({ name, colour: paint(o, i, all.length, all, t.series[type === 'pie' || type === 'donut' || nS === 1 ? 0 : i]?.color && nS > 1 ? t.series[i].color : undefined) }));
|
|
238
|
+
if (legendPos === 'right' && legendItems.length)
|
|
239
|
+
right += Math.max(...legendItems.map((l) => l.name.length)) * fs * 0.6 + fs * 2.4;
|
|
240
|
+
// the plot rectangle, after leaving room for tick labels
|
|
241
|
+
const horizontal = !!o.horizontal && (type === 'bar');
|
|
242
|
+
const stacked = !!o.stack || !!o.normalize;
|
|
243
|
+
let lo = 0, hi = 1, ticks = [];
|
|
244
|
+
if (type === 'bar' || type === 'line' || type === 'area' || type === 'sparkline' || type === 'histogram') {
|
|
245
|
+
const vals = type === 'histogram' ? histogramBins(o, t).counts : null;
|
|
246
|
+
const colMax = (j) => stacked ? t.series.reduce((a, s) => a + Math.max(0, s.values[j] ?? 0), 0) : Math.max(...t.series.map((s) => s.values[j] ?? 0));
|
|
247
|
+
const dmax = vals ? Math.max(...vals) : o.normalize ? 100 : Math.max(...Array.from({ length: nC }, (_, j) => colMax(j)), 0);
|
|
248
|
+
const dmin = vals ? 0 : Math.min(0, ...t.series.flatMap((s) => s.values));
|
|
249
|
+
const every = t.series.flatMap((s) => s.values);
|
|
250
|
+
lo = o.yMin ?? (type === 'line' || type === 'sparkline' ? (every.length ? Math.min(...every) : 0) : dmin);
|
|
251
|
+
hi = o.yMax ?? dmax;
|
|
252
|
+
if (type === 'line' && o.yMin === undefined) {
|
|
253
|
+
const span = hi - lo || 1;
|
|
254
|
+
lo = lo - span * 0.08;
|
|
255
|
+
}
|
|
256
|
+
if (type !== 'sparkline') {
|
|
257
|
+
ticks = niceTicks(lo, hi, 5);
|
|
258
|
+
if (o.yMin === undefined)
|
|
259
|
+
lo = Math.min(lo, ticks[0]);
|
|
260
|
+
if (o.yMax === undefined)
|
|
261
|
+
hi = Math.max(hi, ticks[ticks.length - 1]);
|
|
262
|
+
if (type !== 'line')
|
|
263
|
+
lo = Math.min(lo, ticks[0]);
|
|
264
|
+
}
|
|
265
|
+
if (hi <= lo)
|
|
266
|
+
hi = lo + 1;
|
|
267
|
+
}
|
|
268
|
+
const catLabelW = horizontal ? Math.max(...t.labels.map((l) => l.length), 1) * fs * 0.6 + fs * 0.8 : 0;
|
|
269
|
+
const valLabelW = Math.max(...ticks.map((v) => valFmt(v).length), 1) * fs * 0.6 + fs * 0.8;
|
|
270
|
+
if (showAxes) {
|
|
271
|
+
left += horizontal ? catLabelW : valLabelW;
|
|
272
|
+
bottom += fs * (axesOpt.xLabel ? 3.6 : 2.2);
|
|
273
|
+
if (axesOpt.yLabel)
|
|
274
|
+
left += fs * 1.2;
|
|
275
|
+
}
|
|
276
|
+
const pw = W - left - right, ph = H - top - bottom, px = left, py = top;
|
|
277
|
+
const body = (() => {
|
|
278
|
+
switch (type) {
|
|
279
|
+
case 'bar': return bars_(c, t, o, names, px, py, pw, ph, lo, hi, ticks, fs, grow, horizontal, stacked, valFmt, showAxes, axesOpt);
|
|
280
|
+
case 'line':
|
|
281
|
+
case 'area':
|
|
282
|
+
case 'sparkline': return lines_(c, t, o, names, px, py, pw, ph, lo, hi, ticks, fs, grow, stacked, valFmt, showAxes, axesOpt, type);
|
|
283
|
+
case 'histogram': return hist_(t, o, px, py, pw, ph, lo, hi, ticks, fs, grow, valFmt, showAxes, axesOpt);
|
|
284
|
+
case 'scatter': return scatter_(o, px, py, pw, ph, fs, grow, valFmt, axesOpt, W, H, top, bottom, left, right);
|
|
285
|
+
case 'pie':
|
|
286
|
+
case 'donut': return pie_(t, o, names, W, H, top, bottom, left, right, fs, grow, valFmt, type === 'donut');
|
|
287
|
+
case 'heatmap': return heat_(t, o, px, py, pw, ph, fs, grow, valFmt);
|
|
288
|
+
case 'radar': return radar_(t, o, names, px, py, pw, ph, fs, grow);
|
|
289
|
+
}
|
|
290
|
+
})();
|
|
291
|
+
svg += body;
|
|
292
|
+
// legend
|
|
293
|
+
if (legendPos) {
|
|
294
|
+
let x = legendPos === 'right' ? W - right + fs : left, y = legendPos === 'bottom' ? H - pad - fs * 0.5 : legendPos === 'top' ? pad + (o.title ? fs * 2 : 0) + fs * 1.1 : top + fs;
|
|
295
|
+
for (const l of legendItems) {
|
|
296
|
+
svg += `<rect x="${r1(x)}" y="${r1(y - fs * 0.75)}" width="${fs * 0.9}" height="${fs * 0.9}" rx="${fs * 0.2}" fill="${l.colour}"/><text x="${r1(x + fs * 1.35)}" y="${r1(y)}" font-size="${fs * 0.95}" fill="${INK}">${esc(l.name)}</text>`;
|
|
297
|
+
if (legendPos === 'right')
|
|
298
|
+
y += fs * 1.6;
|
|
299
|
+
else
|
|
300
|
+
x += l.name.length * fs * 0.6 * 0.95 + fs * 2.6;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return svg;
|
|
304
|
+
}
|
|
305
|
+
/** the y axis, grid lines and tick labels for a chart whose values run up the page */
|
|
306
|
+
function yAxis(px, py, pw, ph, lo, hi, ticks, fs, a, f) {
|
|
307
|
+
const Y = (v) => py + ph - ((v - lo) / (hi - lo)) * ph;
|
|
308
|
+
let s = '';
|
|
309
|
+
for (const v of ticks) {
|
|
310
|
+
if (v < lo - 1e-9 || v > hi + 1e-9)
|
|
311
|
+
continue;
|
|
312
|
+
if (a.grid)
|
|
313
|
+
s += `<line x1="${px}" y1="${r1(Y(v))}" x2="${px + pw}" y2="${r1(Y(v))}" stroke="${GRID}" stroke-width="${v === 0 ? 1.2 : 0.7}"/>`;
|
|
314
|
+
if (a.y)
|
|
315
|
+
s += `<text x="${px - fs * 0.6}" y="${r1(Y(v) + fs * 0.33)}" text-anchor="end" font-size="${fs * 0.9}" fill="${FAINT}">${esc(f(v))}</text>`;
|
|
316
|
+
}
|
|
317
|
+
if (a.yLabel)
|
|
318
|
+
s += `<text transform="translate(${fs * 0.9} ${py + ph / 2}) rotate(-90)" text-anchor="middle" font-size="${fs * 0.95}" fill="${FAINT}">${esc(a.yLabel)}</text>`;
|
|
319
|
+
return s;
|
|
320
|
+
}
|
|
321
|
+
/** the category labels under a bar or line chart, thinned out so they do not collide, the x axis line and its title. `bw` is the room for one label. */
|
|
322
|
+
function xLabels(labels, xs, y, fs, bw, a, px, pw, py, ph) {
|
|
323
|
+
let s = '';
|
|
324
|
+
const maxLen = Math.max(1, ...labels.map((l) => l.length)), fits = maxLen * fs * 0.6 * 0.9 <= bw * 1.05, every = fits ? 1 : Math.ceil((maxLen * fs * 0.6 * 0.9) / Math.max(1, bw));
|
|
325
|
+
labels.forEach((l, i) => { if (i % every === 0)
|
|
326
|
+
s += `<text x="${r1(xs[i])}" y="${r1(y + fs * 1.3)}" text-anchor="middle" font-size="${fs * 0.9}" fill="${FAINT}">${esc(l)}</text>`; });
|
|
327
|
+
if (a.xLabel)
|
|
328
|
+
s += `<text x="${px + pw / 2}" y="${r1(y + fs * 3.2)}" text-anchor="middle" font-size="${fs * 0.95}" fill="${FAINT}">${esc(a.xLabel)}</text>`;
|
|
329
|
+
return s + `<line x1="${px}" y1="${py + ph}" x2="${px + pw}" y2="${py + ph}" stroke="${INK}" stroke-opacity=".55" stroke-width="1"/>`;
|
|
330
|
+
}
|
|
331
|
+
/** bars, vertical or horizontal, grouped or stacked, grown in by `grow` */
|
|
332
|
+
function bars_(c, t, o, names, px, py, pw, ph, lo, hi, ticks, fs, grow, horizontal, stacked, f, showAxes, a) {
|
|
333
|
+
const nC = t.labels.length, nS = t.series.length;
|
|
334
|
+
const single = nS === 1;
|
|
335
|
+
let s = '';
|
|
336
|
+
if (horizontal) {
|
|
337
|
+
// categories run down the page, values run across
|
|
338
|
+
const X = (v) => px + ((v - lo) / (hi - lo)) * pw, band = ph / nC, group = band * 0.72, bh = stacked || single ? group : group / nS;
|
|
339
|
+
if (showAxes)
|
|
340
|
+
for (const v of ticks) {
|
|
341
|
+
if (v < lo || v > hi)
|
|
342
|
+
continue;
|
|
343
|
+
if (a.grid)
|
|
344
|
+
s += `<line x1="${r1(X(v))}" y1="${py}" x2="${r1(X(v))}" y2="${py + ph}" stroke="${GRID}" stroke-width="0.7"/>`;
|
|
345
|
+
if (a.x)
|
|
346
|
+
s += `<text x="${r1(X(v))}" y="${py + ph + fs * 1.3}" text-anchor="middle" font-size="${fs * 0.9}" fill="${FAINT}">${esc(f(v))}</text>`;
|
|
347
|
+
}
|
|
348
|
+
for (let j = 0; j < nC; j++) {
|
|
349
|
+
const cy = py + band * (j + 0.5), g = staged(grow, j, nC);
|
|
350
|
+
if (showAxes && a.y)
|
|
351
|
+
s += `<text x="${px - fs * 0.6}" y="${r1(cy + fs * 0.33)}" text-anchor="end" font-size="${fs * 0.9}" fill="${FAINT}">${esc(t.labels[j])}</text>`;
|
|
352
|
+
let acc = 0;
|
|
353
|
+
t.series.forEach((se, i) => {
|
|
354
|
+
const raw = se.values[j] ?? 0, v = o.normalize ? (raw / (t.series.reduce((q, z) => q + (z.values[j] ?? 0), 0) || 1)) * 100 : raw;
|
|
355
|
+
const x0 = stacked ? X(lo + acc) : X(Math.max(lo, 0)), x1 = stacked ? X(lo + acc + v * g) : X(Math.max(lo, 0) + (v - Math.max(lo, 0)) * g);
|
|
356
|
+
const y = stacked || single ? cy - bh / 2 : cy - group / 2 + i * bh;
|
|
357
|
+
if (stacked)
|
|
358
|
+
acc += v;
|
|
359
|
+
s += `<rect x="${r1(Math.min(x0, x1))}" y="${r1(y)}" width="${r1(Math.abs(x1 - x0))}" height="${r1(bh * 0.92)}" rx="${r1(Math.min(3, bh * 0.2))}" fill="${paint(o, single && (o.palette !== undefined || o.highlight !== undefined) ? j : single ? 0 : i, single && (o.palette !== undefined || o.highlight !== undefined) ? nC : single ? 1 : nS, single ? t.labels : names, se.color)}"/>`;
|
|
360
|
+
if (o.labelValues && g > 0.95)
|
|
361
|
+
s += `<text x="${r1(x1 + fs * 0.4)}" y="${r1(y + bh * 0.5 + fs * 0.33)}" font-size="${fs * 0.85}" fill="${INK}">${esc(f(v))}</text>`;
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
return s;
|
|
365
|
+
}
|
|
366
|
+
const Y = (v) => py + ph - ((v - lo) / (hi - lo)) * ph, band = pw / nC, group = band * 0.74, bw = stacked || single ? group : group / nS;
|
|
367
|
+
if (showAxes)
|
|
368
|
+
s += yAxis(px, py, pw, ph, lo, hi, ticks, fs, a, f);
|
|
369
|
+
const xs = [];
|
|
370
|
+
for (let j = 0; j < nC; j++) {
|
|
371
|
+
const cx = px + band * (j + 0.5), g = staged(grow, j, nC);
|
|
372
|
+
xs.push(cx);
|
|
373
|
+
let acc = 0;
|
|
374
|
+
const base = Math.max(lo, 0);
|
|
375
|
+
t.series.forEach((se, i) => {
|
|
376
|
+
const raw = se.values[j] ?? 0, v = o.normalize ? (raw / (t.series.reduce((q, z) => q + (z.values[j] ?? 0), 0) || 1)) * 100 : raw;
|
|
377
|
+
const x = stacked || single ? cx - bw / 2 : cx - group / 2 + i * bw;
|
|
378
|
+
const y0 = stacked ? Y(acc) : Y(base), y1 = stacked ? Y(acc + v * g) : Y(base + (v - base) * g);
|
|
379
|
+
if (stacked)
|
|
380
|
+
acc += v;
|
|
381
|
+
s += `<rect x="${r1(x)}" y="${r1(Math.min(y0, y1))}" width="${r1(bw * 0.92)}" height="${r1(Math.abs(y1 - y0))}" rx="${r1(Math.min(3, bw * 0.16))}" fill="${paint(o, single && (o.palette !== undefined || o.highlight !== undefined) ? j : single ? 0 : i, single && (o.palette !== undefined || o.highlight !== undefined) ? nC : single ? 1 : nS, single ? t.labels : names, se.color)}"/>`;
|
|
382
|
+
if (o.labelValues && g > 0.95)
|
|
383
|
+
s += `<text x="${r1(x + bw * 0.46)}" y="${r1(Math.min(y0, y1) - fs * 0.4)}" text-anchor="middle" font-size="${fs * 0.85}" fill="${INK}">${esc(f(v))}</text>`;
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
if (showAxes && a.x)
|
|
387
|
+
s += xLabels(t.labels, xs, py + ph, fs, band, a, px, pw, py, ph);
|
|
388
|
+
return s;
|
|
389
|
+
}
|
|
390
|
+
/** lines, areas and sparklines. A clip path that widens with `grow` draws them in from the left. */
|
|
391
|
+
function lines_(c, t, o, names, px, py, pw, ph, lo, hi, ticks, fs, grow, stacked, f, showAxes, a, type) {
|
|
392
|
+
const nC = t.labels.length, nS = t.series.length, curve = o.curve ?? 'linear';
|
|
393
|
+
const X = (j) => px + (nC <= 1 ? pw / 2 : (pw * j) / (nC - 1)), Y = (v) => py + ph - ((v - lo) / (hi - lo)) * ph;
|
|
394
|
+
const spark = type === 'sparkline', area = type === 'area' || !!o.fill, id = `${c.clipId}`;
|
|
395
|
+
let s = '';
|
|
396
|
+
if (showAxes)
|
|
397
|
+
s += yAxis(px, py, pw, ph, lo, hi, ticks, fs, a, f);
|
|
398
|
+
// a sparkline fits its own range, with no axis to keep honest
|
|
399
|
+
if (spark) {
|
|
400
|
+
const v = t.series[0]?.values ?? [];
|
|
401
|
+
if (!v.length)
|
|
402
|
+
return '';
|
|
403
|
+
lo = Math.min(...v);
|
|
404
|
+
hi = Math.max(...v);
|
|
405
|
+
if (hi === lo)
|
|
406
|
+
hi = lo + 1;
|
|
407
|
+
}
|
|
408
|
+
const SY = spark ? (v) => py + ph * 0.1 + (ph * 0.8) * (1 - (v - lo) / (hi - lo)) : Y;
|
|
409
|
+
// the reveal: a clip that grows from the left
|
|
410
|
+
s += `<clipPath id="${id}"><rect x="${px - 4}" y="${py - 8}" width="${r1((pw + 8) * easeOut(grow))}" height="${ph + 16}"/></clipPath><g clip-path="url(#${id})">`;
|
|
411
|
+
const base = Math.max(lo, Math.min(0, hi));
|
|
412
|
+
const cum = Array.from({ length: nC }, () => 0);
|
|
413
|
+
// with `normalize` every column is drawn as shares of its own total, as the bars do
|
|
414
|
+
const totals = Array.from({ length: nC }, (_, j) => t.series.reduce((a, q) => a + (q.values[j] ?? 0), 0));
|
|
415
|
+
for (let i = 0; i < nS; i++) {
|
|
416
|
+
const se = t.series[i], colour = paint(o, nS === 1 ? 0 : i, nS, names, se.color);
|
|
417
|
+
const bottom = stacked ? cum.slice() : null;
|
|
418
|
+
const pts = se.values.map((v, j) => { const u = o.normalize ? (v / (totals[j] || 1)) * 100 : v; if (stacked)
|
|
419
|
+
cum[j] += u; return [X(j), SY(stacked ? cum[j] : u)]; });
|
|
420
|
+
if (area) {
|
|
421
|
+
const lower = bottom ? bottom.map((v, j) => [X(j), SY(v)]).reverse() : [[X(nC - 1), SY(base)], [X(0), SY(base)]];
|
|
422
|
+
s += `<path d="${linePath(pts, curve)}L${lower.map((q) => `${r1(q[0])} ${r1(q[1])}`).join('L')}Z" fill="${colour}" fill-opacity="${stacked ? 0.55 : 0.2}"/>`;
|
|
423
|
+
}
|
|
424
|
+
s += `<path d="${linePath(pts, curve)}" fill="none" stroke="${colour}" stroke-width="${spark ? 1.6 : 2}" stroke-linecap="round" stroke-linejoin="round"/>`;
|
|
425
|
+
if (o.dots !== false && !spark && nC <= 40)
|
|
426
|
+
s += pts.map((q) => `<circle cx="${r1(q[0])}" cy="${r1(q[1])}" r="${2.6}" fill="${BG}" stroke="${colour}" stroke-width="1.6"/>`).join('');
|
|
427
|
+
if (spark) {
|
|
428
|
+
const q = pts[pts.length - 1];
|
|
429
|
+
s += `<circle cx="${r1(q[0])}" cy="${r1(q[1])}" r="2.8" fill="${colour}"/>`;
|
|
430
|
+
}
|
|
431
|
+
if (o.labelValues && !spark)
|
|
432
|
+
s += pts.map((q, j) => `<text x="${r1(q[0])}" y="${r1(q[1] - fs * 0.7)}" text-anchor="middle" font-size="${fs * 0.8}" fill="${INK}">${esc(f(se.values[j]))}</text>`).join('');
|
|
433
|
+
}
|
|
434
|
+
s += '</g>';
|
|
435
|
+
if (showAxes && a.x)
|
|
436
|
+
s += xLabels(t.labels, Array.from({ length: nC }, (_, j) => X(j)), py + ph, fs, pw / Math.max(1, nC - 1), a, px, pw, py, ph);
|
|
437
|
+
return s;
|
|
438
|
+
}
|
|
439
|
+
/** the bins of a histogram: edges on round numbers, and how many values fall in each (a value on an edge goes to the bin above; the highest to the last). Non-finite values are ignored. */
|
|
440
|
+
function histogramBins(o, t) {
|
|
441
|
+
const vals = (o.values ?? t.series[0]?.values ?? []).filter(Number.isFinite);
|
|
442
|
+
if (!vals.length)
|
|
443
|
+
return { edges: [0, 1], counts: [0] };
|
|
444
|
+
const mn = Math.min(...vals), mx = Math.max(...vals), n = o.bins ?? Math.max(4, Math.min(24, Math.ceil(Math.log2(vals.length) + 1)));
|
|
445
|
+
const step = nice((mx - mn || 1) / n, true), a = Math.floor(mn / step) * step, nb = Math.max(1, Math.ceil((mx - a) / step + 1e-9));
|
|
446
|
+
const edges = Array.from({ length: nb + 1 }, (_, i) => a + i * step), counts = new Array(nb).fill(0);
|
|
447
|
+
for (const v of vals)
|
|
448
|
+
counts[Math.min(nb - 1, Math.floor((v - a) / step))]++;
|
|
449
|
+
return { edges, counts };
|
|
450
|
+
}
|
|
451
|
+
/** the bars of a histogram */
|
|
452
|
+
function hist_(t, o, px, py, pw, ph, lo, hi, ticks, fs, grow, f, showAxes, a) {
|
|
453
|
+
const { edges, counts } = histogramBins(o, t), nb = counts.length, bw = pw / nb, Y = (v) => py + ph - ((v - lo) / (hi - lo)) * ph;
|
|
454
|
+
let s = showAxes ? yAxis(px, py, pw, ph, lo, hi, ticks, fs, a, f) : '';
|
|
455
|
+
counts.forEach((n, j) => {
|
|
456
|
+
const g = staged(grow, j, nb), y = Y(n * g);
|
|
457
|
+
s += `<rect x="${r1(px + j * bw + 1)}" y="${r1(y)}" width="${r1(bw - 2)}" height="${r1(Y(0) - y)}" rx="2" fill="${paint(o, 0, 1, [])}"/>`;
|
|
458
|
+
if (o.labelValues && n && g > 0.95)
|
|
459
|
+
s += `<text x="${r1(px + (j + 0.5) * bw)}" y="${r1(y - fs * 0.4)}" text-anchor="middle" font-size="${fs * 0.8}" fill="${INK}">${n}</text>`;
|
|
460
|
+
});
|
|
461
|
+
if (showAxes && a.x) {
|
|
462
|
+
const every = Math.ceil((f(edges[0]).length * fs * 0.6 * 1.2) / bw);
|
|
463
|
+
edges.forEach((e, j) => { if (j % every === 0)
|
|
464
|
+
s += `<text x="${r1(px + j * bw)}" y="${r1(py + ph + fs * 1.3)}" text-anchor="middle" font-size="${fs * 0.9}" fill="${FAINT}">${esc(f(e))}</text>`; });
|
|
465
|
+
s += `<line x1="${px}" y1="${py + ph}" x2="${px + pw}" y2="${py + ph}" stroke="${INK}" stroke-opacity=".55"/>`;
|
|
466
|
+
if (a.xLabel)
|
|
467
|
+
s += `<text x="${px + pw / 2}" y="${r1(py + ph + fs * 3.2)}" text-anchor="middle" font-size="${fs * 0.95}" fill="${FAINT}">${esc(a.xLabel)}</text>`;
|
|
468
|
+
}
|
|
469
|
+
return s;
|
|
470
|
+
}
|
|
471
|
+
/** a scatter plot or bubble chart. It lays out its own axes, because both are numeric and the plot rectangle from `draw` only fits a category axis. */
|
|
472
|
+
function scatter_(o, px, py, pw, ph, fs, grow, f, a, W, H, top, bottom, left, right) {
|
|
473
|
+
const pts = (o.points ?? []).filter((p) => Number.isFinite(p.x) && Number.isFinite(p.y)), xs = pts.length ? pts.map((p) => p.x) : [0, 1], ys = pts.length ? pts.map((p) => p.y) : [0, 1];
|
|
474
|
+
const xt = niceTicks(Math.min(...xs), Math.max(...xs), 6), yt = niceTicks(o.yMin ?? Math.min(...ys), o.yMax ?? Math.max(...ys), 5);
|
|
475
|
+
const x0 = xt[0], x1 = xt[xt.length - 1], y0 = yt[0], y1 = yt[yt.length - 1];
|
|
476
|
+
const showY = o.axes !== false, lw = Math.max(...yt.map((v) => f(v).length)) * fs * 0.6 + fs * 0.8;
|
|
477
|
+
const L = showY ? left - pad(o) + lw : left, B = H - bottom; // `bottom` already holds the room for the tick labels and the x label
|
|
478
|
+
const X = (v) => L + ((v - x0) / (x1 - x0 || 1)) * (W - right - L), Y = (v) => top + (1 - (v - y0) / (y1 - y0 || 1)) * (B - top);
|
|
479
|
+
let s = '';
|
|
480
|
+
if (showY) {
|
|
481
|
+
for (const v of yt) {
|
|
482
|
+
if (a.grid)
|
|
483
|
+
s += `<line x1="${L}" y1="${r1(Y(v))}" x2="${W - right}" y2="${r1(Y(v))}" stroke="${GRID}" stroke-width="0.7"/>`;
|
|
484
|
+
if (a.y)
|
|
485
|
+
s += `<text x="${L - fs * 0.6}" y="${r1(Y(v) + fs * 0.33)}" text-anchor="end" font-size="${fs * 0.9}" fill="${FAINT}">${esc(f(v))}</text>`;
|
|
486
|
+
}
|
|
487
|
+
for (const v of xt) {
|
|
488
|
+
if (a.grid)
|
|
489
|
+
s += `<line x1="${r1(X(v))}" y1="${top}" x2="${r1(X(v))}" y2="${B}" stroke="${GRID}" stroke-width="0.7"/>`;
|
|
490
|
+
if (a.x)
|
|
491
|
+
s += `<text x="${r1(X(v))}" y="${r1(B + fs * 1.3)}" text-anchor="middle" font-size="${fs * 0.9}" fill="${FAINT}">${esc(f(v))}</text>`;
|
|
492
|
+
}
|
|
493
|
+
s += `<line x1="${L}" y1="${B}" x2="${W - right}" y2="${B}" stroke="${INK}" stroke-opacity=".55"/><line x1="${L}" y1="${top}" x2="${L}" y2="${B}" stroke="${INK}" stroke-opacity=".55"/>`;
|
|
494
|
+
if (a.xLabel)
|
|
495
|
+
s += `<text x="${(L + W - right) / 2}" y="${r1(B + fs * 3.2)}" text-anchor="middle" font-size="${fs * 0.95}" fill="${FAINT}">${esc(a.xLabel)}</text>`;
|
|
496
|
+
if (a.yLabel)
|
|
497
|
+
s += `<text transform="translate(${fs * 0.9} ${(top + B) / 2}) rotate(-90)" text-anchor="middle" font-size="${fs * 0.95}" fill="${FAINT}">${esc(a.yLabel)}</text>`;
|
|
498
|
+
}
|
|
499
|
+
const vs = pts.map((p) => p.v).filter((v) => v !== undefined), vmin = Math.min(...vs), vmax = Math.max(...vs);
|
|
500
|
+
const rs = pts.map((p) => p.r).filter((v) => v !== undefined), rmax = Math.max(...rs, 1e-9), maxR = o.maxR ?? 11;
|
|
501
|
+
const seriesNames = [...new Set(pts.map((p) => String(p.s ?? '')))];
|
|
502
|
+
pts.forEach((p, i) => {
|
|
503
|
+
const g = staged(grow, i, pts.length), si = Math.max(0, seriesNames.indexOf(String(p.s ?? '')));
|
|
504
|
+
const colour = p.v !== undefined ? ramp(o, 0.22 + 0.78 * ((p.v - vmin) / (vmax - vmin || 1))) : paint(o, si, seriesNames.length, seriesNames);
|
|
505
|
+
const r = (p.r !== undefined ? 3 + (maxR - 3) * Math.sqrt(p.r / rmax) : 4) * g;
|
|
506
|
+
s += `<circle cx="${r1(X(p.x))}" cy="${r1(Y(p.y))}" r="${r1(r)}" fill="${colour}" fill-opacity="${p.r !== undefined ? 0.7 : 0.9}" stroke="${BG}" stroke-width="0.8"/>`;
|
|
507
|
+
});
|
|
508
|
+
return s;
|
|
509
|
+
}
|
|
510
|
+
/** the margin of the chart in svg units */
|
|
511
|
+
const pad = (o) => (o.pad ?? 0.08) * 100;
|
|
512
|
+
/** a pie or donut, swept in clockwise from the top by `grow` */
|
|
513
|
+
function pie_(t, o, names, W, H, top, bottom, left, right, fs, grow, f, donut) {
|
|
514
|
+
const vals = t.series[0]?.values.map((v) => Math.max(0, v)) ?? [], total = vals.reduce((a, b) => a + b, 0) || 1;
|
|
515
|
+
const cx = (left + W - right) / 2, cy = (top + H - bottom) / 2, R = Math.min(W - left - right, H - top - bottom) / 2 - (o.labelValues ? fs * 1.6 : 4);
|
|
516
|
+
const hole = donut ? (o.hole ?? 0.58) * R : 0, sweepMax = 2 * Math.PI * easeOut(grow);
|
|
517
|
+
let a = -Math.PI / 2, s = '';
|
|
518
|
+
vals.forEach((v, i) => {
|
|
519
|
+
const a0 = a, span = (v / total) * 2 * Math.PI, a1 = Math.min(a + span, -Math.PI / 2 + sweepMax);
|
|
520
|
+
a += span;
|
|
521
|
+
if (a1 <= a0 + 1e-6)
|
|
522
|
+
return;
|
|
523
|
+
const big = a1 - a0 > Math.PI ? 1 : 0, P = (r, ang) => `${r1(cx + r * Math.cos(ang))} ${r1(cy + r * Math.sin(ang))}`;
|
|
524
|
+
const d = vals.length === 1 && a1 - a0 > 2 * Math.PI - 1e-3
|
|
525
|
+
? `M${P(R, 0)}A${R} ${R} 0 1 1 ${P(R, Math.PI)}A${R} ${R} 0 1 1 ${P(R, 0)}${hole ? `M${P(hole, 0)}A${hole} ${hole} 0 1 0 ${P(hole, Math.PI)}A${hole} ${hole} 0 1 0 ${P(hole, 0)}` : ''}Z`
|
|
526
|
+
: hole ? `M${P(R, a0)}A${R} ${R} 0 ${big} 1 ${P(R, a1)}L${P(hole, a1)}A${hole} ${hole} 0 ${big} 0 ${P(hole, a0)}Z` : `M${r1(cx)} ${r1(cy)}L${P(R, a0)}A${R} ${R} 0 ${big} 1 ${P(R, a1)}Z`;
|
|
527
|
+
s += `<path d="${d}" fill="${paint(o, i, vals.length, names)}" stroke="${BG}" stroke-width="1.6" stroke-linejoin="round" fill-rule="evenodd"/>`;
|
|
528
|
+
if (o.labelValues && a1 >= a0 + span - 1e-6) {
|
|
529
|
+
const m = (a0 + a1) / 2, pct = f === fmt ? `${Math.round((v / total) * 100)}%` : f(v);
|
|
530
|
+
s += `<text x="${r1(cx + (R + fs * 0.9) * Math.cos(m))}" y="${r1(cy + (R + fs * 0.9) * Math.sin(m) + fs * 0.3)}" text-anchor="${Math.cos(m) > 0.2 ? 'start' : Math.cos(m) < -0.2 ? 'end' : 'middle'}" font-size="${fs * 0.85}" fill="${INK}">${esc(pct)}</text>`;
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
return s;
|
|
534
|
+
}
|
|
535
|
+
/** a grid of cells, one row per series, coloured along the ramp between the lowest and highest value */
|
|
536
|
+
function heat_(t, o, px, py, pw, ph, fs, grow, f) {
|
|
537
|
+
const nR = t.series.length, nC = t.labels.length, all = t.series.flatMap((x) => x.values), mn = o.yMin ?? Math.min(...all), mx = o.yMax ?? Math.max(...all);
|
|
538
|
+
const rowW = Math.max(...t.series.map((r) => r.name.length), 1) * fs * 0.6 + fs * 0.8, x0 = px + rowW, cw = (pw - rowW) / nC, y0 = py + fs * 1.6, ch = (ph - fs * 1.6) / nR;
|
|
539
|
+
let s = '';
|
|
540
|
+
t.labels.forEach((l, j) => { s += `<text x="${r1(x0 + (j + 0.5) * cw)}" y="${r1(py + fs * 0.9)}" text-anchor="middle" font-size="${fs * 0.9}" fill="${FAINT}">${esc(l)}</text>`; });
|
|
541
|
+
t.series.forEach((r, i) => {
|
|
542
|
+
s += `<text x="${r1(x0 - fs * 0.5)}" y="${r1(y0 + (i + 0.5) * ch + fs * 0.33)}" text-anchor="end" font-size="${fs * 0.9}" fill="${FAINT}">${esc(r.name)}</text>`;
|
|
543
|
+
r.values.forEach((v, j) => {
|
|
544
|
+
const k = i * nC + j, g = staged(grow, k, nR * nC), tt = (v - mn) / (mx - mn || 1);
|
|
545
|
+
s += `<rect x="${r1(x0 + j * cw + 1)}" y="${r1(y0 + i * ch + 1)}" width="${r1(cw - 2)}" height="${r1(ch - 2)}" rx="${r1(Math.min(3, cw * 0.1))}" fill="${ramp(o, tt)}" fill-opacity="${r1(g * 100) / 100}"/>`;
|
|
546
|
+
if (o.labelValues && g > 0.9)
|
|
547
|
+
s += `<text x="${r1(x0 + (j + 0.5) * cw)}" y="${r1(y0 + (i + 0.5) * ch + fs * 0.3)}" text-anchor="middle" font-size="${fs * 0.8}" fill="${tt > 0.6 ? BG : INK}">${esc(f(v))}</text>`;
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
return s;
|
|
551
|
+
}
|
|
552
|
+
/** a radar chart: four grid rings, a spoke per label and a polygon per series, scaled to the highest value */
|
|
553
|
+
function radar_(t, o, names, px, py, pw, ph, fs, grow) {
|
|
554
|
+
const n = t.labels.length, cx = px + pw / 2, cy = py + ph / 2, R = Math.min(pw, ph) / 2 - fs * 1.8, all = t.series.flatMap((s) => s.values), mx = o.yMax ?? Math.max(...all, 1e-9);
|
|
555
|
+
const P = (i, r) => [cx + r * Math.sin((2 * Math.PI * i) / n), cy - r * Math.cos((2 * Math.PI * i) / n)];
|
|
556
|
+
let s = '';
|
|
557
|
+
for (let k = 1; k <= 4; k++)
|
|
558
|
+
s += `<path d="${Array.from({ length: n }, (_, i) => `${i ? 'L' : 'M'}${P(i, (R * k) / 4).map(r1).join(' ')}`).join('')}Z" fill="none" stroke="${GRID}" stroke-width="0.8"/>`;
|
|
559
|
+
t.labels.forEach((l, i) => {
|
|
560
|
+
const [x, y] = P(i, R), [lx, ly] = P(i, R + fs * 1.2);
|
|
561
|
+
s += `<line x1="${r1(cx)}" y1="${r1(cy)}" x2="${r1(x)}" y2="${r1(y)}" stroke="${GRID}" stroke-width="0.8"/><text x="${r1(lx)}" y="${r1(ly + fs * 0.3)}" text-anchor="${Math.sin((2 * Math.PI * i) / n) > 0.3 ? 'start' : Math.sin((2 * Math.PI * i) / n) < -0.3 ? 'end' : 'middle'}" font-size="${fs * 0.9}" fill="${FAINT}">${esc(l)}</text>`;
|
|
562
|
+
});
|
|
563
|
+
const g = easeOut(grow);
|
|
564
|
+
t.series.forEach((se, i) => {
|
|
565
|
+
const colour = paint(o, i, t.series.length, names, se.color), pts = se.values.map((v, j) => P(j, (R * Math.max(0, v) * g) / mx));
|
|
566
|
+
s += `<path d="${pts.map((q, j) => `${j ? 'L' : 'M'}${q.map(r1).join(' ')}`).join('')}Z" fill="${colour}" fill-opacity="0.2" stroke="${colour}" stroke-width="1.8" stroke-linejoin="round"/>` + pts.map((q) => `<circle cx="${r1(q[0])}" cy="${r1(q[1])}" r="2.4" fill="${colour}"/>`).join('');
|
|
567
|
+
});
|
|
568
|
+
return s;
|
|
569
|
+
}
|
|
570
|
+
//# sourceMappingURL=charts.js.map
|