swiftplot 0.0.1 → 0.3.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 +348 -3
- package/dist/chart.d.ts +7 -0
- package/dist/chart.js +1311 -0
- package/dist/charts/build.d.ts +52 -0
- package/dist/charts/build.js +63 -0
- package/dist/charts/candlestick.d.ts +8 -0
- package/dist/charts/candlestick.js +105 -0
- package/dist/charts/combo.d.ts +8 -0
- package/dist/charts/combo.js +50 -0
- package/dist/charts/controller.d.ts +32 -0
- package/dist/charts/controller.js +231 -0
- package/dist/charts/defaults.d.ts +20 -0
- package/dist/charts/defaults.js +121 -0
- package/dist/charts/distribution.d.ts +15 -0
- package/dist/charts/distribution.js +178 -0
- package/dist/charts/fields.d.ts +64 -0
- package/dist/charts/fields.js +251 -0
- package/dist/charts/flow.d.ts +15 -0
- package/dist/charts/flow.js +189 -0
- package/dist/charts/heatmap.d.ts +9 -0
- package/dist/charts/heatmap.js +104 -0
- package/dist/charts/index.d.ts +13 -0
- package/dist/charts/index.js +10 -0
- package/dist/charts/mount.d.ts +5 -0
- package/dist/charts/mount.js +21 -0
- package/dist/charts/parts.d.ts +23 -0
- package/dist/charts/parts.js +66 -0
- package/dist/charts/radar.d.ts +7 -0
- package/dist/charts/radar.js +77 -0
- package/dist/charts/source.d.ts +24 -0
- package/dist/charts/source.js +225 -0
- package/dist/charts/types.d.ts +643 -0
- package/dist/charts/types.js +1 -0
- package/dist/charts/xy.d.ts +46 -0
- package/dist/charts/xy.js +421 -0
- package/dist/features/animate.d.ts +20 -0
- package/dist/features/animate.js +118 -0
- package/dist/features/brush.d.ts +15 -0
- package/dist/features/brush.js +128 -0
- package/dist/features/legend.d.ts +13 -0
- package/dist/features/legend.js +236 -0
- package/dist/features/pointer.d.ts +29 -0
- package/dist/features/pointer.js +23 -0
- package/dist/features/tooltip.d.ts +22 -0
- package/dist/features/tooltip.js +192 -0
- package/dist/features/zoom.d.ts +14 -0
- package/dist/features/zoom.js +246 -0
- package/dist/gl.d.ts +5 -0
- package/dist/gl.js +52 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +32 -0
- package/dist/marks/arcs.d.ts +31 -0
- package/dist/marks/arcs.js +125 -0
- package/dist/marks/area.d.ts +25 -0
- package/dist/marks/area.js +106 -0
- package/dist/marks/bar.d.ts +52 -0
- package/dist/marks/bar.js +243 -0
- package/dist/marks/box.d.ts +33 -0
- package/dist/marks/box.js +182 -0
- package/dist/marks/candlestick.d.ts +22 -0
- package/dist/marks/candlestick.js +133 -0
- package/dist/marks/common.d.ts +102 -0
- package/dist/marks/common.js +250 -0
- package/dist/marks/dots.d.ts +16 -0
- package/dist/marks/dots.js +158 -0
- package/dist/marks/errorBars.d.ts +24 -0
- package/dist/marks/errorBars.js +99 -0
- package/dist/marks/funnel.d.ts +30 -0
- package/dist/marks/funnel.js +122 -0
- package/dist/marks/heatmap.d.ts +20 -0
- package/dist/marks/heatmap.js +139 -0
- package/dist/marks/line.d.ts +21 -0
- package/dist/marks/line.js +75 -0
- package/dist/marks/radar.d.ts +29 -0
- package/dist/marks/radar.js +162 -0
- package/dist/marks/radialBars.d.ts +34 -0
- package/dist/marks/radialBars.js +111 -0
- package/dist/marks/rects.d.ts +43 -0
- package/dist/marks/rects.js +120 -0
- package/dist/marks/rule.d.ts +47 -0
- package/dist/marks/rule.js +83 -0
- package/dist/marks/sankey.d.ts +53 -0
- package/dist/marks/sankey.js +369 -0
- package/dist/marks/sunburst.d.ts +31 -0
- package/dist/marks/sunburst.js +186 -0
- package/dist/marks/ticks.d.ts +12 -0
- package/dist/marks/ticks.js +26 -0
- package/dist/marks/tree.d.ts +49 -0
- package/dist/marks/tree.js +113 -0
- package/dist/marks/treemap.d.ts +30 -0
- package/dist/marks/treemap.js +164 -0
- package/dist/paint.d.ts +35 -0
- package/dist/paint.js +71 -0
- package/dist/scale.d.ts +16 -0
- package/dist/scale.js +97 -0
- package/dist/scales/band.d.ts +12 -0
- package/dist/scales/band.js +27 -0
- package/dist/scales/bare.d.ts +6 -0
- package/dist/scales/bare.js +10 -0
- package/dist/scales/color.d.ts +20 -0
- package/dist/scales/color.js +21 -0
- package/dist/scales/log.d.ts +27 -0
- package/dist/scales/log.js +98 -0
- package/dist/scales/time.d.ts +37 -0
- package/dist/scales/time.js +183 -0
- package/dist/text.d.ts +8 -0
- package/dist/text.js +33 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +8 -0
- package/dist/transforms.d.ts +47 -0
- package/dist/transforms.js +119 -0
- package/dist/types.d.ts +465 -0
- package/dist/types.js +1 -0
- package/package.json +31 -6
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { fromLinks, sankey } from '../marks/sankey.js';
|
|
2
|
+
import { sunburst } from '../marks/sunburst.js';
|
|
3
|
+
import { fromTree } from '../marks/tree.js';
|
|
4
|
+
import { treemap } from '../marks/treemap.js';
|
|
5
|
+
import { defined, easyChart, featuresOf, given, install, noAxes, sizeOf } from './build.js';
|
|
6
|
+
import { colorsFor, formatNumber, namesOf } from './defaults.js';
|
|
7
|
+
import { reader } from './fields.js';
|
|
8
|
+
const SANKEY = 'sankeyChart';
|
|
9
|
+
const worth = (v) => (v > 0 && Number.isFinite(v) ? v : 0);
|
|
10
|
+
// Only finite sizes are added up, so a total that is not finite is a sum past the largest number.
|
|
11
|
+
const formatTotal = (v) => (Number.isFinite(v) ? formatNumber(v) : 'over 1.79e+308');
|
|
12
|
+
const hidden = (text) => (text === false ? false : undefined);
|
|
13
|
+
/** Runs a step of the core, whose errors start with its own name, under the chart's. */
|
|
14
|
+
function under(name, step) {
|
|
15
|
+
try {
|
|
16
|
+
return step();
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
throw new Error(`${name}: ${e.message.replace(/^\w+: /, '')}`, { cause: e });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function links(s, p) {
|
|
23
|
+
const { kinds, fail, check, sole, read, numeric } = reader(s, SANKEY);
|
|
24
|
+
if (!s.length)
|
|
25
|
+
return undefined;
|
|
26
|
+
if (s.shape)
|
|
27
|
+
fail('a list of numbers or pairs has no links', 'rows or columns with fields for source, target and value');
|
|
28
|
+
const pool = check([
|
|
29
|
+
['source', p.source],
|
|
30
|
+
['target', p.target],
|
|
31
|
+
['value', p.value],
|
|
32
|
+
], ['value']);
|
|
33
|
+
const named = pool.filter(f => kinds.get(f) === 'text');
|
|
34
|
+
const end = (role) => p[role] ??
|
|
35
|
+
named.shift() ??
|
|
36
|
+
fail(`there is no field of text left for ${role}`, `${role} as a field of the nodes' names`);
|
|
37
|
+
const from = end('source');
|
|
38
|
+
const to = end('target');
|
|
39
|
+
const value = sole('value', p.value, pool);
|
|
40
|
+
const { values } = numeric(value, 'value');
|
|
41
|
+
const ends = [
|
|
42
|
+
read(from, 'the accessor source', 'source'),
|
|
43
|
+
read(to, 'the accessor target', 'target'),
|
|
44
|
+
];
|
|
45
|
+
const namer = ({ kind, categories }) => namesOf(kind, categories, ends.filter(c => c.kind === kind).map(c => c.values), true);
|
|
46
|
+
const [source, target] = ends;
|
|
47
|
+
const [sourceName, targetName] = [namer(source), namer(target)];
|
|
48
|
+
const list = [];
|
|
49
|
+
for (let i = 0; i < s.length; i++) {
|
|
50
|
+
const [a, b] = [source.values[i], target.values[i]];
|
|
51
|
+
if (Number.isFinite(a) && Number.isFinite(b))
|
|
52
|
+
list.push({ source: sourceName(a), target: targetName(b), value: values[i] });
|
|
53
|
+
}
|
|
54
|
+
const flow = fromLinks(list);
|
|
55
|
+
const [out, into] = [new Float64Array(flow.nodes.length), new Float64Array(flow.nodes.length)];
|
|
56
|
+
flow.value.forEach((v, i) => {
|
|
57
|
+
out[flow.source[i]] += worth(v);
|
|
58
|
+
into[flow.target[i]] += worth(v);
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
...flow,
|
|
62
|
+
label: typeof value === 'string' ? value : 'value',
|
|
63
|
+
totals: out.map((v, i) => Math.max(v, into[i])),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const sankeyBuild = (p, { source: s, theme, memo }) => {
|
|
67
|
+
if (s.length) {
|
|
68
|
+
for (const k of ['x', 'y', 'label', 'series'])
|
|
69
|
+
if (given(p, k))
|
|
70
|
+
throw new Error(`${SANKEY}: use source, target and value, not ${k}`);
|
|
71
|
+
noAxes(p, SANKEY);
|
|
72
|
+
}
|
|
73
|
+
const picked = memo('fields', [s, p.source, p.target, p.value], () => links(s, p));
|
|
74
|
+
const colors = colorsFor(theme, p.colors, picked?.nodes.length ?? 0);
|
|
75
|
+
const marks = memo('marks', [picked, p.blend, hidden(p.text), ...colors], () => {
|
|
76
|
+
if (!picked)
|
|
77
|
+
return [];
|
|
78
|
+
const { nodes, source, target, value, label } = picked;
|
|
79
|
+
return under(SANKEY, () => [
|
|
80
|
+
sankey(defined({
|
|
81
|
+
key: 'sankey',
|
|
82
|
+
label,
|
|
83
|
+
nodes,
|
|
84
|
+
source,
|
|
85
|
+
target,
|
|
86
|
+
value,
|
|
87
|
+
colors,
|
|
88
|
+
blend: p.blend,
|
|
89
|
+
text: hidden(p.text),
|
|
90
|
+
})),
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
93
|
+
const features = featuresOf({ ...p, zoom: false }, 0);
|
|
94
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
95
|
+
ref.current = picked;
|
|
96
|
+
return {
|
|
97
|
+
options: { ...sizeOf(p), theme, frame: 'blank', marks },
|
|
98
|
+
features,
|
|
99
|
+
use: () => {
|
|
100
|
+
ref.current = picked;
|
|
101
|
+
return install(features, {
|
|
102
|
+
mode: 'nearest',
|
|
103
|
+
text: (_label, _x, _y, row) => {
|
|
104
|
+
const { nodes, source, target, value, totals } = ref.current;
|
|
105
|
+
const link = row - nodes.length;
|
|
106
|
+
return link < 0
|
|
107
|
+
? `${nodes[row]}: ${formatTotal(totals[row])}`
|
|
108
|
+
: `${nodes[source[link]]} → ${nodes[target[link]]}: ${formatNumber(worth(value[link]))}`;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
function grown(data, name) {
|
|
115
|
+
if (!data || !Object.keys(data).length)
|
|
116
|
+
return undefined;
|
|
117
|
+
const tree = under(name, () => {
|
|
118
|
+
const bad = (cause) => new Error('data must be a root { name, value, children } or a list of roots', { cause });
|
|
119
|
+
try {
|
|
120
|
+
const made = fromTree(data);
|
|
121
|
+
if (made.nodes.some(n => typeof n !== 'string'))
|
|
122
|
+
throw bad();
|
|
123
|
+
return made;
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
throw e instanceof TypeError ? bad(e) : e;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
const { nodes, parent, value } = tree;
|
|
130
|
+
const totals = new Float64Array(nodes.length);
|
|
131
|
+
const leaf = new Uint8Array(nodes.length).fill(1);
|
|
132
|
+
for (const at of parent)
|
|
133
|
+
if (at >= 0)
|
|
134
|
+
leaf[at] = 0;
|
|
135
|
+
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
136
|
+
if (leaf[i])
|
|
137
|
+
totals[i] = worth(value[i]);
|
|
138
|
+
if (parent[i] >= 0)
|
|
139
|
+
totals[parent[i]] += totals[i];
|
|
140
|
+
}
|
|
141
|
+
const below = (at) => parent.filter(up => up === at).length;
|
|
142
|
+
const roots = below(-1);
|
|
143
|
+
return { ...tree, totals, branches: roots > 1 ? roots : below(0) || 1 };
|
|
144
|
+
}
|
|
145
|
+
function trees(kind, frame, draw) {
|
|
146
|
+
const name = `${kind}Chart`;
|
|
147
|
+
return (p, { source: s, theme, memo }) => {
|
|
148
|
+
// The source stands for the data: a new one is made when the data or its version changes.
|
|
149
|
+
const tree = memo('tree', [s], () => grown(p.data, name));
|
|
150
|
+
if (tree) {
|
|
151
|
+
for (const k of ['x', 'y', 'series', 'label', 'value', 'source', 'target'])
|
|
152
|
+
if (given(p, k))
|
|
153
|
+
throw new Error(`${name}: ${k} does not apply, as data is a tree of { name, value, children }`);
|
|
154
|
+
noAxes(p, name);
|
|
155
|
+
}
|
|
156
|
+
const colors = colorsFor(theme, p.colors, tree?.branches ?? 0);
|
|
157
|
+
const marks = memo('marks', [tree, p.shade, hidden(p.text), ...colors], () => {
|
|
158
|
+
if (!tree)
|
|
159
|
+
return [];
|
|
160
|
+
const { nodes, parent, value } = tree;
|
|
161
|
+
return [
|
|
162
|
+
draw(defined({ key: kind, nodes, parent, value, colors, shade: p.shade, text: hidden(p.text) })),
|
|
163
|
+
];
|
|
164
|
+
});
|
|
165
|
+
const features = featuresOf({ ...p, zoom: false }, 0);
|
|
166
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
167
|
+
ref.current = tree;
|
|
168
|
+
return {
|
|
169
|
+
options: { ...sizeOf(p), theme, frame, marks },
|
|
170
|
+
features,
|
|
171
|
+
use: () => {
|
|
172
|
+
ref.current = tree;
|
|
173
|
+
return install(features, {
|
|
174
|
+
mode: 'nearest',
|
|
175
|
+
text: (_label, _x, _y, row) => {
|
|
176
|
+
const { nodes, parent, totals } = ref.current;
|
|
177
|
+
const path = [];
|
|
178
|
+
for (let i = row; i >= 0; i = parent[i])
|
|
179
|
+
path.push(nodes[i]);
|
|
180
|
+
return `${path.reverse().join(' · ')}: ${formatTotal(totals[row])}`;
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
export const sankeyChart = easyChart(sankeyBuild);
|
|
188
|
+
export const treemapChart = easyChart(trees('treemap', 'blank', treemap));
|
|
189
|
+
export const sunburstChart = easyChart(trees('sunburst', 'polar', sunburst));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HeatmapProps } from './types.js';
|
|
2
|
+
export declare const fits: (columns: number, rows: number) => boolean;
|
|
3
|
+
export declare const heatmapChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: HeatmapProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
4
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<HeatmapProps<D_1>> & {
|
|
5
|
+
data?: D_1 | null | undefined;
|
|
6
|
+
}): void;
|
|
7
|
+
} : import("./controller.js").EasyChart<HeatmapProps<D>>) & {
|
|
8
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: HeatmapProps<D>) => import("../types.js").Options;
|
|
9
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { heatmap } from '../marks/heatmap.js';
|
|
2
|
+
import { band } from '../scales/band.js';
|
|
3
|
+
import { defined, easyChart, featuresOf, given, install, sizeOf } from './build.js';
|
|
4
|
+
import { formatNumber, namesOf } from './defaults.js';
|
|
5
|
+
import { reader } from './fields.js';
|
|
6
|
+
const NAME = 'heatmapChart';
|
|
7
|
+
const MAX_CELLS = 10_000_000;
|
|
8
|
+
export const fits = (columns, rows) => columns * rows <= MAX_CELLS;
|
|
9
|
+
function pick(s, p) {
|
|
10
|
+
const { kinds, fail, check, sole, read, numeric } = reader(s, NAME);
|
|
11
|
+
if (!s.length)
|
|
12
|
+
return undefined;
|
|
13
|
+
if (s.shape)
|
|
14
|
+
fail('a list of numbers or pairs has no cells', 'rows or columns with fields for x, y and value');
|
|
15
|
+
const pool = check([
|
|
16
|
+
['x', p.x],
|
|
17
|
+
['y', p.y],
|
|
18
|
+
['value', p.value],
|
|
19
|
+
], ['value']);
|
|
20
|
+
const named = pool.filter(f => kinds.get(f) === 'text' || kinds.get(f) === 'time');
|
|
21
|
+
const side = (role) => p[role] ??
|
|
22
|
+
named.shift() ??
|
|
23
|
+
fail(`there is no field of text or dates left for ${role}`, `${role} as a field of the cells' places`);
|
|
24
|
+
const x = side('x');
|
|
25
|
+
const y = side('y');
|
|
26
|
+
const value = sole('value', p.value, pool);
|
|
27
|
+
const { values } = numeric(value, 'value');
|
|
28
|
+
const xs = read(x, 'the accessor x', 'x');
|
|
29
|
+
const ys = read(y, 'the accessor y', 'y');
|
|
30
|
+
const kept = Array.from({ length: s.length }, (_, i) => i).filter(i => Number.isFinite(xs.values[i]) && Number.isFinite(ys.values[i]));
|
|
31
|
+
const lay = (f, { kind, values: at, categories }) => {
|
|
32
|
+
const order = [...new Set(kept.map(i => at[i]))];
|
|
33
|
+
if (kind !== 'text')
|
|
34
|
+
order.sort((a, b) => a - b);
|
|
35
|
+
return {
|
|
36
|
+
label: typeof f === 'string' ? f : '',
|
|
37
|
+
categories: order.map(namesOf(kind, categories, [order], true)),
|
|
38
|
+
slots: new Map(order.map((v, i) => [v, i])),
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const { slots: columns, ...across } = lay(x, xs);
|
|
42
|
+
const { slots: rows, ...up } = lay(y, ys);
|
|
43
|
+
if (!fits(columns.size, rows.size))
|
|
44
|
+
fail(`a grid of ${formatNumber(columns.size)} columns by ${formatNumber(rows.size)} rows is over the limit of ${formatNumber(MAX_CELLS)} cells`, 'x and y as fields with fewer distinct values');
|
|
45
|
+
const cells = new Float64Array(columns.size * rows.size).fill(Number.NaN);
|
|
46
|
+
for (const i of kept)
|
|
47
|
+
cells[rows.get(ys.values[i]) * columns.size + columns.get(xs.values[i])] = values[i];
|
|
48
|
+
return {
|
|
49
|
+
x: across,
|
|
50
|
+
y: up,
|
|
51
|
+
label: typeof value === 'string' ? value : 'value',
|
|
52
|
+
values: cells,
|
|
53
|
+
some: cells.some(Number.isFinite),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const build = (p, { source: s, theme, memo }) => {
|
|
57
|
+
if (s.length) {
|
|
58
|
+
if (given(p, 'label'))
|
|
59
|
+
throw new Error(`${NAME}: use x, y and value, not label and value`);
|
|
60
|
+
if (given(p, 'zoom'))
|
|
61
|
+
throw new Error(`${NAME}: zoom does not apply to a grid of cells`);
|
|
62
|
+
}
|
|
63
|
+
const picked = memo('fields', [s, p.x, p.y, p.value], () => pick(s, p));
|
|
64
|
+
const { color: scale } = p;
|
|
65
|
+
// By value, not identity: a scale made inline is new on every render.
|
|
66
|
+
const color = memo('color', scale ? [...(scale.domain ?? []), ...scale.stops, ...scale.table] : [], () => scale);
|
|
67
|
+
const axis = (side, label) => band({ label: label ?? side?.label ?? '', categories: side?.categories ?? [] });
|
|
68
|
+
const features = featuresOf({ ...p, zoom: false }, picked?.some ? 2 : 0);
|
|
69
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
70
|
+
ref.current = picked;
|
|
71
|
+
return {
|
|
72
|
+
options: {
|
|
73
|
+
...sizeOf(p),
|
|
74
|
+
theme,
|
|
75
|
+
x: axis(picked?.x, p.xAxis?.label),
|
|
76
|
+
y: axis(picked?.y, p.yAxis?.label),
|
|
77
|
+
marks: picked
|
|
78
|
+
? [
|
|
79
|
+
heatmap(defined({
|
|
80
|
+
key: 'heatmap',
|
|
81
|
+
label: picked.label,
|
|
82
|
+
values: picked.values,
|
|
83
|
+
columns: picked.x.categories.length,
|
|
84
|
+
color,
|
|
85
|
+
})),
|
|
86
|
+
]
|
|
87
|
+
: [],
|
|
88
|
+
},
|
|
89
|
+
features,
|
|
90
|
+
use: () => {
|
|
91
|
+
ref.current = picked;
|
|
92
|
+
return install(features, {
|
|
93
|
+
mode: 'nearest',
|
|
94
|
+
text: (label, _x, _y, row) => {
|
|
95
|
+
const { x, y, values } = ref.current;
|
|
96
|
+
const n = x.categories.length;
|
|
97
|
+
const [column, line] = [x.categories[row % n], y.categories[Math.floor(row / n)]];
|
|
98
|
+
return `${column} · ${line} · ${label}: ${formatNumber(values[row])}`;
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
export const heatmapChart = easyChart(build);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { candlestickChart } from './candlestick.js';
|
|
2
|
+
export { comboChart } from './combo.js';
|
|
3
|
+
export { boxPlotChart, histogramChart } from './distribution.js';
|
|
4
|
+
export { sankeyChart, sunburstChart, treemapChart } from './flow.js';
|
|
5
|
+
export { heatmapChart } from './heatmap.js';
|
|
6
|
+
export { funnelChart, pieChart, radialBarChart } from './parts.js';
|
|
7
|
+
export { radarChart } from './radar.js';
|
|
8
|
+
export { areaChart, barChart, lineChart, scatterChart } from './xy.js';
|
|
9
|
+
export { equal, plain } from './controller.js';
|
|
10
|
+
export type { EasyChart } from './controller.js';
|
|
11
|
+
export { mount } from './mount.js';
|
|
12
|
+
export type { Mount } from './mount.js';
|
|
13
|
+
export type { AreaProps as AreaChartProps, BarProps as BarChartProps, BoxPlotProps as BoxPlotChartProps, CandlestickProps as CandlestickChartProps, ComboProps as ComboChartProps, FunnelProps as FunnelChartProps, HeatmapProps as HeatmapChartProps, HistogramProps as HistogramChartProps, LineProps as LineChartProps, PieProps as PieChartProps, RadarProps as RadarChartProps, RadialBarProps as RadialBarChartProps, SankeyProps as SankeyChartProps, ScatterProps as ScatterChartProps, SunburstProps as SunburstChartProps, TreemapProps as TreemapChartProps, } from './types.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { candlestickChart } from './candlestick.js';
|
|
2
|
+
export { comboChart } from './combo.js';
|
|
3
|
+
export { boxPlotChart, histogramChart } from './distribution.js';
|
|
4
|
+
export { sankeyChart, sunburstChart, treemapChart } from './flow.js';
|
|
5
|
+
export { heatmapChart } from './heatmap.js';
|
|
6
|
+
export { funnelChart, pieChart, radialBarChart } from './parts.js';
|
|
7
|
+
export { radarChart } from './radar.js';
|
|
8
|
+
export { areaChart, barChart, lineChart, scatterChart } from './xy.js';
|
|
9
|
+
export { equal, plain } from './controller.js';
|
|
10
|
+
export { mount } from './mount.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Options } from '../types.js';
|
|
2
|
+
import { type EasyChart } from './controller.js';
|
|
3
|
+
export type Mount<P> = (el: HTMLElement, props: P) => EasyChart<P>;
|
|
4
|
+
/** plot() behind the easy charts' controller: an update sends the core only the options that changed. */
|
|
5
|
+
export declare const mount: Mount<Options>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { light } from '../theme.js';
|
|
2
|
+
import { create, PASSED } from './controller.js';
|
|
3
|
+
/** plot() behind the easy charts' controller: an update sends the core only the options that changed. */
|
|
4
|
+
export const mount = (el, options) => {
|
|
5
|
+
const use = options.use ?? [];
|
|
6
|
+
const c = create(el, { ...options, theme: options.theme ?? light }, props => {
|
|
7
|
+
const { use: _, ...rest } = props;
|
|
8
|
+
for (const k of PASSED)
|
|
9
|
+
delete rest[k];
|
|
10
|
+
return { options: rest, features: null, use: () => use };
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
get chart() {
|
|
14
|
+
return c.chart;
|
|
15
|
+
},
|
|
16
|
+
update(partial) {
|
|
17
|
+
c.update('theme' in partial ? { ...partial, theme: partial.theme ?? light } : partial);
|
|
18
|
+
},
|
|
19
|
+
destroy: () => c.destroy(),
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Options } from '../types.js';
|
|
2
|
+
import type { FunnelProps, PieProps, RadialBarProps } from './types.js';
|
|
3
|
+
export declare const pieChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: PieProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
4
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<PieProps<D_1>> & {
|
|
5
|
+
data?: D_1 | null | undefined;
|
|
6
|
+
}): void;
|
|
7
|
+
} : import("./controller.js").EasyChart<PieProps<D>>) & {
|
|
8
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: PieProps<D>) => Options;
|
|
9
|
+
};
|
|
10
|
+
export declare const funnelChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: FunnelProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
11
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<FunnelProps<D_1>> & {
|
|
12
|
+
data?: D_1 | null | undefined;
|
|
13
|
+
}): void;
|
|
14
|
+
} : import("./controller.js").EasyChart<FunnelProps<D>>) & {
|
|
15
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: FunnelProps<D>) => Options;
|
|
16
|
+
};
|
|
17
|
+
export declare const radialBarChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: RadialBarProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
18
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<RadialBarProps<D_1>> & {
|
|
19
|
+
data?: D_1 | null | undefined;
|
|
20
|
+
}): void;
|
|
21
|
+
} : import("./controller.js").EasyChart<RadialBarProps<D>>) & {
|
|
22
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: RadialBarProps<D>) => Options;
|
|
23
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { arcs } from '../marks/arcs.js';
|
|
2
|
+
import { funnel } from '../marks/funnel.js';
|
|
3
|
+
import { radialBars } from '../marks/radialBars.js';
|
|
4
|
+
import { defined, easyChart, featuresOf, given, install, noAxes, sizeOf } from './build.js';
|
|
5
|
+
import { colorsFor, formatNumber, namesOf } from './defaults.js';
|
|
6
|
+
import { reader } from './fields.js';
|
|
7
|
+
function pick(s, p, name) {
|
|
8
|
+
const { kinds, check, sole, read, numeric } = reader(s, name);
|
|
9
|
+
if (!s.length)
|
|
10
|
+
return undefined;
|
|
11
|
+
const given = s.shape === 'numbers' ? { value: 'value' } : s.shape === 'pairs' ? { label: 'x', value: 'y' } : p;
|
|
12
|
+
const pool = check([
|
|
13
|
+
['label', given.label],
|
|
14
|
+
['value', given.value],
|
|
15
|
+
], ['value']);
|
|
16
|
+
const label = given.label ?? pool.find(f => kinds.get(f) === 'text');
|
|
17
|
+
const value = sole('value', given.value, pool);
|
|
18
|
+
const { values } = numeric(value, 'value');
|
|
19
|
+
const named = label === undefined ? undefined : read(label, 'the accessor label', 'label');
|
|
20
|
+
const nameOf = named && namesOf(named.kind, named.categories, [named.values], true);
|
|
21
|
+
return {
|
|
22
|
+
label: typeof value === 'string' ? value : 'value',
|
|
23
|
+
values,
|
|
24
|
+
...(named && { labels: Array.from(named.values, v => (Number.isFinite(v) ? nameOf(v) : '')) }),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function reject(p, name) {
|
|
28
|
+
if (given(p, 'x') || given(p, 'y'))
|
|
29
|
+
throw new Error(`${name}: use label and value, not x and y`);
|
|
30
|
+
noAxes(p, name);
|
|
31
|
+
}
|
|
32
|
+
function parts(kind, frame, draw) {
|
|
33
|
+
const name = `${kind}Chart`;
|
|
34
|
+
return (p, { source: s, theme, memo }) => {
|
|
35
|
+
if (s.length)
|
|
36
|
+
reject(p, name);
|
|
37
|
+
const picked = memo('fields', [s, p.label, p.value], () => pick(s, p, name));
|
|
38
|
+
const features = featuresOf({ ...p, zoom: false }, picked?.labels?.length ?? 0);
|
|
39
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
40
|
+
ref.current = picked;
|
|
41
|
+
return {
|
|
42
|
+
options: {
|
|
43
|
+
...sizeOf(p),
|
|
44
|
+
theme,
|
|
45
|
+
frame,
|
|
46
|
+
marks: picked
|
|
47
|
+
? [
|
|
48
|
+
draw(defined({ key: kind, ...picked, colors: colorsFor(theme, p.colors, picked.values.length) }), p),
|
|
49
|
+
]
|
|
50
|
+
: [],
|
|
51
|
+
},
|
|
52
|
+
features,
|
|
53
|
+
use: () => {
|
|
54
|
+
ref.current = picked;
|
|
55
|
+
return install(features, {
|
|
56
|
+
mode: 'nearest',
|
|
57
|
+
text: (label, _x, _y, row) => `${label || ref.current.label}: ${formatNumber(ref.current.values[row])}`,
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const shown = (text) => (text === false ? undefined : true);
|
|
64
|
+
export const pieChart = easyChart(parts('pie', 'polar', (part, p) => arcs(defined({ ...part, inner: p.inner, text: shown(p.text), sweep: p.sweep }))));
|
|
65
|
+
export const funnelChart = easyChart(parts('funnel', 'blank', (part, p) => funnel(defined({ ...part, text: shown(p.text), fill: p.fill }))));
|
|
66
|
+
export const radialBarChart = easyChart(parts('radialBar', 'polar', (part, p) => radialBars(defined({ ...part, max: p.max, sweep: p.sweep, inner: p.inner }))));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const radarChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: import("./types.js").RadarProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
2
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<import("./types.js").RadarProps<D_1>> & {
|
|
3
|
+
data?: D_1 | null | undefined;
|
|
4
|
+
}): void;
|
|
5
|
+
} : import("./controller.js").EasyChart<import("./types.js").RadarProps<D>>) & {
|
|
6
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: import("./types.js").RadarProps<D>) => import("../types.js").Options;
|
|
7
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { radar } from '../marks/radar.js';
|
|
2
|
+
import { defined, easyChart, featuresOf, given, install, noAxes, sizeOf } from './build.js';
|
|
3
|
+
import { colorsFor, formatNumber, namesOf } from './defaults.js';
|
|
4
|
+
import { list, reader } from './fields.js';
|
|
5
|
+
const NAME = 'radarChart';
|
|
6
|
+
function pick(s, props) {
|
|
7
|
+
const { kinds, check, noNumbers, read, numeric } = reader(s, NAME);
|
|
8
|
+
if (!s.length)
|
|
9
|
+
return { axes: [], shapes: [] };
|
|
10
|
+
const p = s.shape === 'numbers' ? { y: 'value' } : s.shape === 'pairs' ? { x: 'x', y: 'y' } : props;
|
|
11
|
+
const named = list(p.y ?? undefined);
|
|
12
|
+
const pool = check([['x', p.x], ...named.map(f => ['y', f])], ['y']);
|
|
13
|
+
const x = p.x ?? pool.find(f => kinds.get(f) === 'text');
|
|
14
|
+
const ys = named.length ? named : pool.filter(f => kinds.get(f) === 'number');
|
|
15
|
+
if (!ys.length)
|
|
16
|
+
noNumbers('y');
|
|
17
|
+
const spokes = x === undefined ? undefined : read(x, 'the accessor x', 'x');
|
|
18
|
+
const nameOf = spokes && namesOf(spokes.kind, spokes.categories, [spokes.values], true);
|
|
19
|
+
const ids = new Set(ys);
|
|
20
|
+
const seen = new Set();
|
|
21
|
+
const idOf = (f, label) => {
|
|
22
|
+
let id = label;
|
|
23
|
+
if (typeof f !== 'string' || seen.has(f))
|
|
24
|
+
for (let n = 2; ids.has(id); n++)
|
|
25
|
+
id = `${label} (${n})`;
|
|
26
|
+
ids.add(id);
|
|
27
|
+
seen.add(f);
|
|
28
|
+
return id;
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
axes: Array.from({ length: s.length }, (_, i) => nameOf && Number.isFinite(spokes.values[i]) ? nameOf(spokes.values[i]) : ''),
|
|
32
|
+
shapes: ys.map((f, i) => {
|
|
33
|
+
const label = typeof f === 'string' ? f : `y${i + 1}`;
|
|
34
|
+
return { id: idOf(f, label), label, values: numeric(f, 'y', `${i + 1}`).values };
|
|
35
|
+
}),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const build = (p, { source: s, theme, memo }) => {
|
|
39
|
+
if (s.length) {
|
|
40
|
+
if (given(p, 'label') || given(p, 'value'))
|
|
41
|
+
throw new Error(`${NAME}: use x and y, not label and value`);
|
|
42
|
+
noAxes(p, NAME);
|
|
43
|
+
}
|
|
44
|
+
const picked = memo('fields', [s, p.x, ...list(p.y ?? undefined)], () => pick(s, p));
|
|
45
|
+
const { axes, shapes } = picked;
|
|
46
|
+
const colors = colorsFor(theme, p.colors, shapes.length);
|
|
47
|
+
const marks = memo('marks', [picked, p.max, p.fill, ...colors], () => {
|
|
48
|
+
if (!shapes.length)
|
|
49
|
+
return [];
|
|
50
|
+
const series = shapes.map(({ id, ...r }, i) => ({
|
|
51
|
+
...r,
|
|
52
|
+
key: `shape:${id}`,
|
|
53
|
+
group: id,
|
|
54
|
+
color: colors[i],
|
|
55
|
+
}));
|
|
56
|
+
const [grid, ...rest] = radar(defined({ axes, series, max: p.max, fill: p.fill }));
|
|
57
|
+
return [{ ...grid, key: 'radar-grid' }, ...rest];
|
|
58
|
+
});
|
|
59
|
+
const features = featuresOf({ ...p, zoom: false }, shapes.length);
|
|
60
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
61
|
+
ref.current = picked;
|
|
62
|
+
return {
|
|
63
|
+
options: { ...sizeOf(p), theme, frame: 'polar', marks },
|
|
64
|
+
features,
|
|
65
|
+
use: () => {
|
|
66
|
+
ref.current = picked;
|
|
67
|
+
return install(features, {
|
|
68
|
+
mode: 'nearest',
|
|
69
|
+
text: (label, _x, v, row) => {
|
|
70
|
+
const spoke = ref.current.axes[row];
|
|
71
|
+
return `${spoke ? `${spoke} · ` : ''}${label}: ${Number.isFinite(v) ? formatNumber(v) : '–'}`;
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
export const radarChart = easyChart(build);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Col } from '../types.js';
|
|
2
|
+
export type Kind = 'number' | 'time' | 'text';
|
|
3
|
+
export type Column = {
|
|
4
|
+
kind: Kind;
|
|
5
|
+
values: Col;
|
|
6
|
+
categories?: string[];
|
|
7
|
+
};
|
|
8
|
+
export type Accessor<R> = (row: R, index: number) => unknown;
|
|
9
|
+
export type Zone = {
|
|
10
|
+
utc: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type Source<R = unknown> = {
|
|
13
|
+
length: number;
|
|
14
|
+
fields(): string[];
|
|
15
|
+
/** undefined for a field that is unplottable or holds only missing values */
|
|
16
|
+
kind(field: string): Kind | undefined;
|
|
17
|
+
/** zone applies to ISO strings without an offset; utc is the default */
|
|
18
|
+
column(field: string | Accessor<R>, zone?: Zone): Column;
|
|
19
|
+
/** set for the positional shapes, whose fields are fixed and never guessed */
|
|
20
|
+
shape?: 'numbers' | 'pairs';
|
|
21
|
+
};
|
|
22
|
+
export type Data<R = unknown> = readonly R[] | ArrayLike<number> | Record<string, ArrayLike<unknown>>;
|
|
23
|
+
/** fresh gives borrowed typed arrays new views, as the core keeps a mark whose columns are the same objects. */
|
|
24
|
+
export declare function source<R>(data: Data<R> | null | undefined, fresh?: boolean): Source<R>;
|