swiftplot 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +324 -3
- package/dist/chart.d.ts +3 -0
- package/dist/chart.js +1226 -0
- package/dist/charts/controller.d.ts +32 -0
- package/dist/charts/controller.js +231 -0
- package/dist/charts/defaults.d.ts +12 -0
- package/dist/charts/defaults.js +60 -0
- package/dist/charts/fields.d.ts +34 -0
- package/dist/charts/fields.js +167 -0
- package/dist/charts/index.d.ts +6 -0
- package/dist/charts/index.js +3 -0
- package/dist/charts/mount.d.ts +5 -0
- package/dist/charts/mount.js +21 -0
- package/dist/charts/source.d.ts +24 -0
- package/dist/charts/source.js +225 -0
- package/dist/charts/types.d.ts +104 -0
- package/dist/charts/types.js +1 -0
- package/dist/charts/xy.d.ts +36 -0
- package/dist/charts/xy.js +220 -0
- package/dist/features/animate.d.ts +11 -0
- package/dist/features/animate.js +118 -0
- package/dist/features/brush.d.ts +13 -0
- package/dist/features/brush.js +127 -0
- package/dist/features/legend.d.ts +15 -0
- package/dist/features/legend.js +237 -0
- package/dist/features/pointer.d.ts +29 -0
- package/dist/features/pointer.js +23 -0
- package/dist/features/tooltip.d.ts +14 -0
- package/dist/features/tooltip.js +190 -0
- package/dist/features/zoom.d.ts +13 -0
- package/dist/features/zoom.js +228 -0
- package/dist/gl.d.ts +5 -0
- package/dist/gl.js +52 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +31 -0
- package/dist/marks/arcs.d.ts +25 -0
- package/dist/marks/arcs.js +124 -0
- package/dist/marks/area.d.ts +24 -0
- package/dist/marks/area.js +106 -0
- package/dist/marks/bar.d.ts +44 -0
- package/dist/marks/bar.js +243 -0
- package/dist/marks/box.d.ts +27 -0
- package/dist/marks/box.js +188 -0
- package/dist/marks/common.d.ts +84 -0
- package/dist/marks/common.js +236 -0
- package/dist/marks/dots.d.ts +15 -0
- package/dist/marks/dots.js +156 -0
- package/dist/marks/errorBars.d.ts +19 -0
- package/dist/marks/errorBars.js +79 -0
- package/dist/marks/funnel.d.ts +25 -0
- package/dist/marks/funnel.js +121 -0
- package/dist/marks/heatmap.d.ts +19 -0
- package/dist/marks/heatmap.js +139 -0
- package/dist/marks/line.d.ts +19 -0
- package/dist/marks/line.js +75 -0
- package/dist/marks/radar.d.ts +22 -0
- package/dist/marks/radar.js +155 -0
- package/dist/marks/radialBars.d.ts +27 -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 +28 -0
- package/dist/marks/rule.js +80 -0
- package/dist/marks/sankey.d.ts +41 -0
- package/dist/marks/sankey.js +366 -0
- package/dist/marks/sunburst.d.ts +26 -0
- package/dist/marks/sunburst.js +185 -0
- package/dist/marks/ticks.d.ts +9 -0
- package/dist/marks/ticks.js +26 -0
- package/dist/marks/tree.d.ts +41 -0
- package/dist/marks/tree.js +110 -0
- package/dist/marks/treemap.d.ts +25 -0
- package/dist/marks/treemap.js +159 -0
- package/dist/paint.d.ts +30 -0
- package/dist/paint.js +66 -0
- package/dist/scale.d.ts +16 -0
- package/dist/scale.js +96 -0
- package/dist/scales/band.d.ts +9 -0
- package/dist/scales/band.js +23 -0
- package/dist/scales/bare.d.ts +3 -0
- package/dist/scales/bare.js +7 -0
- package/dist/scales/color.d.ts +18 -0
- package/dist/scales/color.js +21 -0
- package/dist/scales/log.d.ts +20 -0
- package/dist/scales/log.js +97 -0
- package/dist/scales/time.d.ts +30 -0
- package/dist/scales/time.js +181 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +8 -0
- package/dist/transforms.d.ts +43 -0
- package/dist/transforms.js +118 -0
- package/dist/types.d.ts +434 -0
- package/dist/types.js +1 -0
- package/package.json +31 -6
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { make as moment } from '../scales/time.js';
|
|
2
|
+
const FIELD_SCAN = 100;
|
|
3
|
+
const numeric = (v) => ArrayBuffer.isView(v) &&
|
|
4
|
+
!(v instanceof DataView) &&
|
|
5
|
+
!(v instanceof BigInt64Array) &&
|
|
6
|
+
!(v instanceof BigUint64Array);
|
|
7
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
8
|
+
const missing = (v) => v == null || v === '' || (typeof v === 'number' && !Number.isFinite(v));
|
|
9
|
+
const infinite = (c) => {
|
|
10
|
+
for (let i = 0; i < c.length; i++)
|
|
11
|
+
if (c[i] === Infinity || c[i] === -Infinity)
|
|
12
|
+
return true;
|
|
13
|
+
return false;
|
|
14
|
+
};
|
|
15
|
+
const ISO = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(\.\d+)?)?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
16
|
+
const DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
17
|
+
const leap = (y) => y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0);
|
|
18
|
+
/** undefined when s is not ISO 8601, NaN when it names an impossible date */
|
|
19
|
+
function iso(s, utc) {
|
|
20
|
+
const m = ISO.exec(s);
|
|
21
|
+
if (!m)
|
|
22
|
+
return undefined;
|
|
23
|
+
const [y, mo, d, h, mi, sec] = [+m[1], +m[2], +m[3], +(m[4] ?? 0), +(m[5] ?? 0), +(m[6] ?? 0)];
|
|
24
|
+
const ms = m[7] ? Number(m[7].slice(1, 4).padEnd(3, '0')) : 0;
|
|
25
|
+
if (mo < 1 ||
|
|
26
|
+
mo > 12 ||
|
|
27
|
+
d < 1 ||
|
|
28
|
+
d > DAYS[mo - 1] + (mo === 2 && leap(y) ? 1 : 0) ||
|
|
29
|
+
h > 23 ||
|
|
30
|
+
mi > 59 ||
|
|
31
|
+
sec > 59)
|
|
32
|
+
return Number.NaN;
|
|
33
|
+
const zone = m[8];
|
|
34
|
+
if (!zone && !utc)
|
|
35
|
+
return moment([y, mo - 1, d, h, mi, sec], false) + ms;
|
|
36
|
+
const t = new Date(0).setUTCFullYear(y, mo - 1, d) + ((h * 60 + mi) * 60 + sec) * 1000 + ms;
|
|
37
|
+
if (zone === 'Z' || !zone)
|
|
38
|
+
return t;
|
|
39
|
+
const [oh, om] = [Number(zone.slice(1, 3)), Number(zone.slice(4))];
|
|
40
|
+
if (oh > 23 || om > 59)
|
|
41
|
+
return Number.NaN;
|
|
42
|
+
const offset = oh * 60 + om;
|
|
43
|
+
return t - (zone[0] === '-' ? -offset : offset) * 60_000;
|
|
44
|
+
}
|
|
45
|
+
function kindOf(v) {
|
|
46
|
+
if (missing(v))
|
|
47
|
+
return undefined;
|
|
48
|
+
if (typeof v === 'number')
|
|
49
|
+
return 'number';
|
|
50
|
+
if (typeof v === 'string') {
|
|
51
|
+
const t = iso(v, true);
|
|
52
|
+
return t === undefined ? 'text' : Number.isNaN(t) ? undefined : 'time';
|
|
53
|
+
}
|
|
54
|
+
if (v instanceof Date)
|
|
55
|
+
return Number.isNaN(v.getTime()) ? undefined : 'time';
|
|
56
|
+
return 'unplottable';
|
|
57
|
+
}
|
|
58
|
+
/** fresh gives borrowed typed arrays new views, as the core keeps a mark whose columns are the same objects. */
|
|
59
|
+
export function source(data, fresh = false) {
|
|
60
|
+
if (data == null)
|
|
61
|
+
return make(0, [], () => () => undefined);
|
|
62
|
+
if (Array.isArray(data))
|
|
63
|
+
return array(data);
|
|
64
|
+
if (typeof data.length === 'number')
|
|
65
|
+
return numbers(data, fresh);
|
|
66
|
+
return columns(data, fresh);
|
|
67
|
+
}
|
|
68
|
+
function array(data) {
|
|
69
|
+
const first = data.find(d => d != null);
|
|
70
|
+
if (!data.length)
|
|
71
|
+
return source(null);
|
|
72
|
+
if (first === undefined || typeof first === 'number')
|
|
73
|
+
return numbers(data);
|
|
74
|
+
if (Array.isArray(first)) {
|
|
75
|
+
const at = (k) => (i) => {
|
|
76
|
+
const p = data[i];
|
|
77
|
+
if (!Array.isArray(p) || p.length !== 2)
|
|
78
|
+
throw new Error(`Row ${i} is not an [x, y] pair: every row must hold exactly two values`);
|
|
79
|
+
return p[k];
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
...make(data.length, ['x', 'y'], f => {
|
|
83
|
+
if (typeof f === 'function')
|
|
84
|
+
throw new Error('Accessors work on row objects only');
|
|
85
|
+
return f === 'x' ? at(0) : f === 'y' ? at(1) : unknown(f, ['x', 'y']);
|
|
86
|
+
}),
|
|
87
|
+
shape: 'pairs',
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const row = (i) => {
|
|
91
|
+
const r = data[i];
|
|
92
|
+
if (!isObject(r))
|
|
93
|
+
throw new Error(`Row ${i} is neither an object nor an [x, y] pair`);
|
|
94
|
+
return r;
|
|
95
|
+
};
|
|
96
|
+
let names;
|
|
97
|
+
const fields = () => {
|
|
98
|
+
if (names)
|
|
99
|
+
return names;
|
|
100
|
+
const found = new Set();
|
|
101
|
+
for (let i = 0; i < Math.min(data.length, FIELD_SCAN); i++)
|
|
102
|
+
for (const k of Object.keys(row(i)))
|
|
103
|
+
found.add(k);
|
|
104
|
+
return (names = [...found]);
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
...make(data.length, [], f => typeof f === 'function'
|
|
108
|
+
? i => f(row(i), i)
|
|
109
|
+
: i => {
|
|
110
|
+
const r = row(i);
|
|
111
|
+
return Object.hasOwn(r, f) ? r[f] : undefined;
|
|
112
|
+
}),
|
|
113
|
+
fields,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function columns(data, fresh) {
|
|
117
|
+
const names = Object.keys(data);
|
|
118
|
+
const length = names.length ? (data[names[0]]?.length ?? 0) : 0;
|
|
119
|
+
const get = (f) => {
|
|
120
|
+
const c = data[f];
|
|
121
|
+
if (!c || typeof c.length !== 'number')
|
|
122
|
+
throw new Error(`Field "${f}" is not an array`);
|
|
123
|
+
if (c.length !== length)
|
|
124
|
+
throw new Error(`Columns differ in length: "${names[0]}" has ${length} values, "${f}" has ${c.length}`);
|
|
125
|
+
return c;
|
|
126
|
+
};
|
|
127
|
+
return make(length, names, f => {
|
|
128
|
+
if (typeof f === 'function')
|
|
129
|
+
throw new Error('Accessors work on row objects only; name a field of the columns object');
|
|
130
|
+
if (!Object.hasOwn(data, f))
|
|
131
|
+
return unknown(f, names);
|
|
132
|
+
const c = get(f);
|
|
133
|
+
return i => c[i];
|
|
134
|
+
}, f => {
|
|
135
|
+
const c = Object.hasOwn(data, f) && numeric(data[f]) ? get(f) : undefined;
|
|
136
|
+
return c;
|
|
137
|
+
}, fresh);
|
|
138
|
+
}
|
|
139
|
+
function numbers(data, fresh = false) {
|
|
140
|
+
const read = i => data[i];
|
|
141
|
+
return {
|
|
142
|
+
...make(data.length, ['value'], f => {
|
|
143
|
+
if (typeof f === 'function')
|
|
144
|
+
throw new Error('Accessors work on row objects only');
|
|
145
|
+
return f === 'value' ? read : unknown(f, ['value']);
|
|
146
|
+
}, f => (f === 'value' && numeric(data) ? data : undefined), fresh),
|
|
147
|
+
shape: 'numbers',
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function unknown(field, names) {
|
|
151
|
+
throw new Error(`No field "${field}"; the fields are ${names.map(n => `"${n}"`).join(', ')}`);
|
|
152
|
+
}
|
|
153
|
+
function make(length, names, reader, borrow = () => undefined, fresh = false) {
|
|
154
|
+
const cache = new Map();
|
|
155
|
+
const scan = (read) => {
|
|
156
|
+
for (let i = 0; i < length; i++) {
|
|
157
|
+
const k = kindOf(read(i));
|
|
158
|
+
if (k)
|
|
159
|
+
return k;
|
|
160
|
+
}
|
|
161
|
+
return undefined;
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
length,
|
|
165
|
+
fields: () => names,
|
|
166
|
+
kind(field) {
|
|
167
|
+
const k = scan(reader(field));
|
|
168
|
+
return k === 'unplottable' ? undefined : k;
|
|
169
|
+
},
|
|
170
|
+
column(field, zone) {
|
|
171
|
+
const utc = zone?.utc ?? true;
|
|
172
|
+
const at = (typeof field === 'string' && cache.get(field)) || {};
|
|
173
|
+
const hit = utc ? at.utc : at.local;
|
|
174
|
+
if (hit)
|
|
175
|
+
return hit;
|
|
176
|
+
const own = typeof field === 'string' ? borrow(field) : undefined;
|
|
177
|
+
const usable = own && !infinite(own);
|
|
178
|
+
const c = usable
|
|
179
|
+
? { kind: 'number', values: fresh ? own.subarray(0) : own }
|
|
180
|
+
: convert(field, reader(field), length, scan, utc);
|
|
181
|
+
if (c.kind !== 'time')
|
|
182
|
+
at.utc = at.local = c;
|
|
183
|
+
else if (utc)
|
|
184
|
+
at.utc = c;
|
|
185
|
+
else
|
|
186
|
+
at.local = c;
|
|
187
|
+
// an accessor is often a new inline function on each update, so caching it by identity would leak
|
|
188
|
+
if (typeof field === 'string')
|
|
189
|
+
cache.set(field, at);
|
|
190
|
+
return c;
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function convert(field, read, length, scan, utc) {
|
|
195
|
+
const raw = Array.from({ length }, (_, i) => read(i));
|
|
196
|
+
const kind = scan(i => raw[i]) ?? 'number';
|
|
197
|
+
if (kind === 'unplottable') {
|
|
198
|
+
const name = typeof field === 'string' ? `Field "${field}"` : 'The accessor';
|
|
199
|
+
throw new Error(`${name} holds values that are not numbers, dates or text, and cannot be plotted`);
|
|
200
|
+
}
|
|
201
|
+
const values = new Float64Array(length).fill(Number.NaN);
|
|
202
|
+
if (kind === 'text') {
|
|
203
|
+
const index = new Map();
|
|
204
|
+
raw.forEach((v, i) => {
|
|
205
|
+
if (typeof v !== 'string' || kindOf(v) !== 'text')
|
|
206
|
+
return;
|
|
207
|
+
let k = index.get(v);
|
|
208
|
+
if (k === undefined)
|
|
209
|
+
index.set(v, (k = index.size));
|
|
210
|
+
values[i] = k;
|
|
211
|
+
});
|
|
212
|
+
return { kind, values, categories: [...index.keys()] };
|
|
213
|
+
}
|
|
214
|
+
raw.forEach((v, i) => {
|
|
215
|
+
if (kind === 'number') {
|
|
216
|
+
if (typeof v === 'number' && Number.isFinite(v))
|
|
217
|
+
values[i] = v;
|
|
218
|
+
}
|
|
219
|
+
else if (v instanceof Date)
|
|
220
|
+
values[i] = v.getTime();
|
|
221
|
+
else if (typeof v === 'string')
|
|
222
|
+
values[i] = iso(v, utc) ?? Number.NaN;
|
|
223
|
+
});
|
|
224
|
+
return { kind, values };
|
|
225
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { AnimateOptions } from '../features/animate.js';
|
|
2
|
+
import type { LegendOptions } from '../features/legend.js';
|
|
3
|
+
import type { ZoomOptions } from '../features/zoom.js';
|
|
4
|
+
import type { LineOptions } from '../marks/line.js';
|
|
5
|
+
import type { TimeOptions } from '../scales/time.js';
|
|
6
|
+
import type { Theme } from '../theme.js';
|
|
7
|
+
import type { Axis, Options } from '../types.js';
|
|
8
|
+
import type { Accessor, Data } from './source.js';
|
|
9
|
+
export type YAxisProps = {
|
|
10
|
+
label?: string;
|
|
11
|
+
domain?: Axis['domain'];
|
|
12
|
+
step?: number;
|
|
13
|
+
format?: (v: number) => string;
|
|
14
|
+
fit?: Axis['fit'];
|
|
15
|
+
};
|
|
16
|
+
export type XAxisProps = YAxisProps & {
|
|
17
|
+
utc?: boolean;
|
|
18
|
+
unit?: TimeOptions['unit'];
|
|
19
|
+
every?: number;
|
|
20
|
+
};
|
|
21
|
+
export type Base = {
|
|
22
|
+
dataVersion?: number;
|
|
23
|
+
xAxis?: XAxisProps;
|
|
24
|
+
yAxis?: YAxisProps;
|
|
25
|
+
colors?: readonly string[];
|
|
26
|
+
theme?: 'auto' | 'light' | 'dark' | Theme;
|
|
27
|
+
tooltip?: boolean;
|
|
28
|
+
legend?: boolean | LegendOptions;
|
|
29
|
+
zoom?: boolean | ZoomOptions;
|
|
30
|
+
animate?: boolean | AnimateOptions;
|
|
31
|
+
width?: number;
|
|
32
|
+
height?: number;
|
|
33
|
+
margin?: Options['margin'];
|
|
34
|
+
description?: string;
|
|
35
|
+
view?: Options['view'];
|
|
36
|
+
onViewChange?: Options['onViewChange'];
|
|
37
|
+
hidden?: string[];
|
|
38
|
+
onHiddenChange?: Options['onHiddenChange'];
|
|
39
|
+
selected?: Options['selected'];
|
|
40
|
+
onClick?: Options['onClick'];
|
|
41
|
+
pointer?: Options['pointer'];
|
|
42
|
+
onPointerChange?: Options['onPointerChange'];
|
|
43
|
+
};
|
|
44
|
+
type Name<T> = keyof T & string;
|
|
45
|
+
type OneOrMore<T> = T | readonly T[];
|
|
46
|
+
type NumberKey<R> = {
|
|
47
|
+
[K in Name<R>]: NonNullable<R[K]> extends number ? K : never;
|
|
48
|
+
}[Name<R>];
|
|
49
|
+
type NumberColumn<C> = {
|
|
50
|
+
[K in Name<C>]: C[K] extends ArrayLike<infer V> ? (NonNullable<V> extends number ? K : never) : never;
|
|
51
|
+
}[Name<C>];
|
|
52
|
+
export type Field<R> = Name<R> | Accessor<R>;
|
|
53
|
+
export type YField<R> = NumberKey<R> | Accessor<R>;
|
|
54
|
+
/** The pairs tuple lets an array literal of pairs be inferred as tuples rather than as rows. */
|
|
55
|
+
export type Input = Data | readonly (readonly [unknown, unknown])[];
|
|
56
|
+
export type RowProps<R> = Base & {
|
|
57
|
+
data?: readonly R[] | null;
|
|
58
|
+
x?: Field<R>;
|
|
59
|
+
y?: OneOrMore<YField<R>>;
|
|
60
|
+
series?: Field<R>;
|
|
61
|
+
};
|
|
62
|
+
export type ColumnProps<C> = Base & {
|
|
63
|
+
data?: C | null;
|
|
64
|
+
x?: Name<C>;
|
|
65
|
+
y?: OneOrMore<NumberColumn<C>>;
|
|
66
|
+
series?: Name<C>;
|
|
67
|
+
};
|
|
68
|
+
type Positional = {
|
|
69
|
+
x?: never;
|
|
70
|
+
y?: never;
|
|
71
|
+
series?: never;
|
|
72
|
+
};
|
|
73
|
+
export type NumberProps = Base & Positional & {
|
|
74
|
+
data?: ArrayLike<number | null | undefined> | null;
|
|
75
|
+
};
|
|
76
|
+
export type PairProps = Base & Positional & {
|
|
77
|
+
data?: readonly (readonly unknown[])[] | null;
|
|
78
|
+
};
|
|
79
|
+
/** Before any data is given the fields cannot be checked. */
|
|
80
|
+
type LooseProps = Base & {
|
|
81
|
+
data?: null;
|
|
82
|
+
x?: string;
|
|
83
|
+
y?: OneOrMore<string>;
|
|
84
|
+
series?: string;
|
|
85
|
+
};
|
|
86
|
+
export type Props<D> = {
|
|
87
|
+
data?: D | null;
|
|
88
|
+
} & (0 extends 1 & D ? LooseProps : [D] extends [ArrayLike<number | null | undefined>] ? NumberProps : [D] extends [readonly (readonly unknown[])[]] ? PairProps : [D] extends [readonly (infer R)[]] ? RowProps<R> : [D] extends [Record<string, ArrayLike<unknown>>] ? ColumnProps<D> : LooseProps);
|
|
89
|
+
type Curve = {
|
|
90
|
+
curve?: LineOptions['curve'];
|
|
91
|
+
join?: boolean;
|
|
92
|
+
};
|
|
93
|
+
export type LineProps<D> = Props<D> & Curve;
|
|
94
|
+
export type AreaProps<D> = Props<D> & Curve & {
|
|
95
|
+
stack?: boolean;
|
|
96
|
+
};
|
|
97
|
+
export type BarProps<D> = Props<D> & {
|
|
98
|
+
stack?: boolean;
|
|
99
|
+
};
|
|
100
|
+
export type ScatterProps<D> = Props<D> & {
|
|
101
|
+
y?: Exclude<Props<D>['y'], readonly unknown[]>;
|
|
102
|
+
r?: number | NonNullable<Exclude<Props<D>['y'], readonly unknown[]>>;
|
|
103
|
+
};
|
|
104
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Options } from '../types.js';
|
|
2
|
+
import { type Build, type EasyChart } from './controller.js';
|
|
3
|
+
import { type FieldProps } from './fields.js';
|
|
4
|
+
import type { AreaProps, BarProps, Base, Input, LineProps, ScatterProps } from './types.js';
|
|
5
|
+
type XyProps = Base & FieldProps<never> & {
|
|
6
|
+
data?: unknown;
|
|
7
|
+
curve?: LineProps<undefined>['curve'];
|
|
8
|
+
join?: boolean;
|
|
9
|
+
stack?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare function xy(name: string, kind?: 'line' | 'area' | 'bar' | 'scatter'): Build<XyProps>;
|
|
12
|
+
type Kinds<D> = {
|
|
13
|
+
line: LineProps<D>;
|
|
14
|
+
area: AreaProps<D>;
|
|
15
|
+
bar: BarProps<D>;
|
|
16
|
+
scatter: ScatterProps<D>;
|
|
17
|
+
};
|
|
18
|
+
type Later<K extends keyof Kinds<never>> = Omit<EasyChart<never>, 'update'> & {
|
|
19
|
+
update<D extends Input | undefined = undefined>(props: Partial<Kinds<D>[K]> & {
|
|
20
|
+
data?: D | null;
|
|
21
|
+
}): void;
|
|
22
|
+
};
|
|
23
|
+
type ChartOf<K extends keyof Kinds<never>, D> = [D] extends [undefined | readonly never[]] ? Later<K> : EasyChart<Kinds<D>[K]>;
|
|
24
|
+
export declare const lineChart: (<D extends Input | undefined = undefined>(el: HTMLElement, props: LineProps<D>) => ChartOf<"line", D>) & {
|
|
25
|
+
spec: <D extends Input | undefined = undefined>(props: LineProps<D>) => Options;
|
|
26
|
+
};
|
|
27
|
+
export declare const areaChart: (<D extends Input | undefined = undefined>(el: HTMLElement, props: AreaProps<D>) => ChartOf<"area", D>) & {
|
|
28
|
+
spec: <D extends Input | undefined = undefined>(props: AreaProps<D>) => Options;
|
|
29
|
+
};
|
|
30
|
+
export declare const barChart: (<D extends Input | undefined = undefined>(el: HTMLElement, props: BarProps<D>) => ChartOf<"bar", D>) & {
|
|
31
|
+
spec: <D extends Input | undefined = undefined>(props: BarProps<D>) => Options;
|
|
32
|
+
};
|
|
33
|
+
export declare const scatterChart: (<D extends Input | undefined = undefined>(el: HTMLElement, props: ScatterProps<D>) => ChartOf<"scatter", D>) & {
|
|
34
|
+
spec: <D extends Input | undefined = undefined>(props: ScatterProps<D>) => Options;
|
|
35
|
+
};
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { animate } from '../features/animate.js';
|
|
2
|
+
import { legend } from '../features/legend.js';
|
|
3
|
+
import { tooltip } from '../features/tooltip.js';
|
|
4
|
+
import { zoom } from '../features/zoom.js';
|
|
5
|
+
import { area } from '../marks/area.js';
|
|
6
|
+
import { bar } from '../marks/bar.js';
|
|
7
|
+
import { dots } from '../marks/dots.js';
|
|
8
|
+
import { line } from '../marks/line.js';
|
|
9
|
+
import { band } from '../scales/band.js';
|
|
10
|
+
import { time } from '../scales/time.js';
|
|
11
|
+
import { dark, light } from '../theme.js';
|
|
12
|
+
import { create, PASSED } from './controller.js';
|
|
13
|
+
import { bandTitle, colorsFor, formatNumber, timeTitle } from './defaults.js';
|
|
14
|
+
import { assign } from './fields.js';
|
|
15
|
+
import { source } from './source.js';
|
|
16
|
+
const defined = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
17
|
+
const on = (v) => (v === true ? {} : v || false);
|
|
18
|
+
const ascending = (x) => {
|
|
19
|
+
for (let i = 1; i < x.length; i++)
|
|
20
|
+
if (x[i] < x[i - 1])
|
|
21
|
+
return false;
|
|
22
|
+
return true;
|
|
23
|
+
};
|
|
24
|
+
/** Line and area need ascending x; equal x keep their order. */
|
|
25
|
+
function sorted(ys) {
|
|
26
|
+
const orders = new Map();
|
|
27
|
+
const order = (x) => {
|
|
28
|
+
if (!orders.has(x))
|
|
29
|
+
orders.set(x, ascending(x) ? undefined : Array.from(x, (_, i) => i).sort((a, b) => x[a] - x[b] || a - b));
|
|
30
|
+
return orders.get(x);
|
|
31
|
+
};
|
|
32
|
+
const xs = new Map();
|
|
33
|
+
return ys.map(s => {
|
|
34
|
+
const o = order(s.x);
|
|
35
|
+
if (!o)
|
|
36
|
+
return s;
|
|
37
|
+
if (!xs.has(s.x))
|
|
38
|
+
xs.set(s.x, Float64Array.from(o, i => s.x[i]));
|
|
39
|
+
return { ...s, x: xs.get(s.x), y: Float64Array.from(o, i => s.y[i]) };
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** Each series' y at the slot of its x in order, NaN where it has no row, the last row winning. */
|
|
43
|
+
function laid(ys, order, x) {
|
|
44
|
+
const at = new Map(Array.from(order, (v, i) => [v, i]));
|
|
45
|
+
return ys.map(s => {
|
|
46
|
+
const y = new Float64Array(order.length).fill(Number.NaN);
|
|
47
|
+
for (let i = 0; i < s.x.length; i++)
|
|
48
|
+
y[at.get(s.x[i])] = s.y[i];
|
|
49
|
+
return { ...s, x, y };
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** The core stacks row by row, so long series are laid over the sorted union of their x. */
|
|
53
|
+
function aligned(ys) {
|
|
54
|
+
const x = Float64Array.from(new Set(ys.flatMap(s => Array.from(s.x)))).sort();
|
|
55
|
+
return laid(ys, x, x);
|
|
56
|
+
}
|
|
57
|
+
/** Wide data is one category per row; long data one per distinct x in order of first appearance, the last row winning. */
|
|
58
|
+
function categorical({ x: ax, ys, order: seen }, utc, long) {
|
|
59
|
+
const name = ax.kind === 'text'
|
|
60
|
+
? (v) => ax.categories[v]
|
|
61
|
+
: ax.kind === 'time'
|
|
62
|
+
? timeTitle(ys.map(s => s.x), utc)
|
|
63
|
+
: String;
|
|
64
|
+
if (!long) {
|
|
65
|
+
const xs = ys[0]?.x ?? [];
|
|
66
|
+
const index = Float64Array.from(xs, (_, i) => i);
|
|
67
|
+
return { categories: Array.from(xs, name), ys: ys.map(s => ({ ...s, x: index })) };
|
|
68
|
+
}
|
|
69
|
+
const order = seen ?? [];
|
|
70
|
+
return {
|
|
71
|
+
categories: order.map(name),
|
|
72
|
+
ys: laid(ys, order, Float64Array.from(order, (_, i) => i)),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export function xy(name, kind = 'line') {
|
|
76
|
+
return (p, { source: s, theme, memo }) => {
|
|
77
|
+
const utc = kind === 'bar' || (p.xAxis?.utc ?? true);
|
|
78
|
+
const scatter = kind === 'scatter';
|
|
79
|
+
const rField = typeof p.r === 'number' ? undefined : p.r;
|
|
80
|
+
const assigned = memo('fields', [s, p.x, ...[p.y ?? []].flat(), p.series, rField, utc], () => assign(s, { ...p, xAxis: { utc } }, scatter ? 'scatter' : 'xy', name, kind === 'bar'));
|
|
81
|
+
const stacked = kind !== 'line' && !scatter && !!p.stack;
|
|
82
|
+
const bars = kind === 'bar'
|
|
83
|
+
? memo('bars', [assigned], () => categorical(assigned, utc, p.series !== undefined))
|
|
84
|
+
: undefined;
|
|
85
|
+
const ys = bars?.ys ??
|
|
86
|
+
(scatter ? assigned.ys : undefined) ??
|
|
87
|
+
memo('sorted', [assigned, stacked], () => stacked && p.series !== undefined ? aligned(assigned.ys) : sorted(assigned.ys));
|
|
88
|
+
const { x: ax, y: ay } = assigned;
|
|
89
|
+
const xa = p.xAxis ?? {};
|
|
90
|
+
const ya = p.yAxis ?? {};
|
|
91
|
+
const label = xa.label ?? ax.label;
|
|
92
|
+
const categories = ax.categories ?? [];
|
|
93
|
+
const x = bars
|
|
94
|
+
? band({ label, categories: bars.categories })
|
|
95
|
+
: ax.kind === 'text'
|
|
96
|
+
? band({ label, categories })
|
|
97
|
+
: ax.kind === 'time'
|
|
98
|
+
? time({
|
|
99
|
+
label,
|
|
100
|
+
utc,
|
|
101
|
+
format: xa.format,
|
|
102
|
+
fit: xa.fit,
|
|
103
|
+
...(Array.isArray(xa.domain) && { domain: xa.domain }),
|
|
104
|
+
...(xa.unit && { unit: xa.unit, every: xa.every }),
|
|
105
|
+
})
|
|
106
|
+
: defined({ domain: xa.domain ?? 'nice', label, step: xa.step, format: xa.format, fit: xa.fit });
|
|
107
|
+
const y = defined({
|
|
108
|
+
domain: ya.domain ?? 'nice',
|
|
109
|
+
label: ya.label ?? ay.label,
|
|
110
|
+
step: ya.step,
|
|
111
|
+
format: ya.format,
|
|
112
|
+
fit: ya.fit,
|
|
113
|
+
});
|
|
114
|
+
const colors = colorsFor(theme, p.colors, ys.length);
|
|
115
|
+
const radius = typeof p.r === 'number' ? p.r : 4;
|
|
116
|
+
const radii = scatter
|
|
117
|
+
? memo('radii', [ys, radius], () => ys.map(r => r.r ?? new Float64Array(r.x.length).fill(radius)))
|
|
118
|
+
: undefined;
|
|
119
|
+
const marks = memo('marks', [ys, stacked, p.curve, p.join, radii, ...colors], () => ys.map((r, i) => {
|
|
120
|
+
if (scatter)
|
|
121
|
+
return dots({ key: r.name, label: r.name, x: r.x, y: r.y, r: radii[i], color: colors[i] });
|
|
122
|
+
const o = {
|
|
123
|
+
key: r.name,
|
|
124
|
+
label: r.name,
|
|
125
|
+
x: r.x,
|
|
126
|
+
y: r.y,
|
|
127
|
+
color: colors[i],
|
|
128
|
+
curve: p.curve,
|
|
129
|
+
join: p.join,
|
|
130
|
+
};
|
|
131
|
+
if (kind === 'line')
|
|
132
|
+
return line(o);
|
|
133
|
+
if (kind === 'bar')
|
|
134
|
+
return bar({
|
|
135
|
+
key: r.name,
|
|
136
|
+
label: r.name,
|
|
137
|
+
x: r.x,
|
|
138
|
+
y: r.y,
|
|
139
|
+
color: colors[i],
|
|
140
|
+
...(stacked ? { stack: 'stack' } : { dodge: 'dodge' }),
|
|
141
|
+
});
|
|
142
|
+
return area(defined({
|
|
143
|
+
...o,
|
|
144
|
+
stack: stacked ? 'stack' : undefined,
|
|
145
|
+
alpha: !stacked && ys.length >= 2 ? 0.5 : undefined,
|
|
146
|
+
}));
|
|
147
|
+
}));
|
|
148
|
+
const text = {
|
|
149
|
+
x: (v) => (!bars && ax.kind !== 'text' && xa.format ? xa.format : text.title)(v),
|
|
150
|
+
title: bars
|
|
151
|
+
? v => bandTitle(bars.categories, v)
|
|
152
|
+
: ax.kind === 'text'
|
|
153
|
+
? v => bandTitle(categories, v)
|
|
154
|
+
: ax.kind === 'time' && p.tooltip !== false
|
|
155
|
+
? memo('timeTitle', [ys, utc], () => timeTitle([...new Set(ys.map(r => r.x))], utc))
|
|
156
|
+
: formatNumber,
|
|
157
|
+
value: ya.format ?? formatNumber,
|
|
158
|
+
};
|
|
159
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
160
|
+
const showLegend = p.legend !== false && ys.length >= 2;
|
|
161
|
+
const features = {
|
|
162
|
+
tooltip: p.tooltip !== false,
|
|
163
|
+
legend: showLegend && (typeof p.legend === 'object' ? p.legend : {}),
|
|
164
|
+
zoom: on(p.zoom),
|
|
165
|
+
animate: on(p.animate),
|
|
166
|
+
};
|
|
167
|
+
ref.current = text;
|
|
168
|
+
return {
|
|
169
|
+
options: {
|
|
170
|
+
...defined({ width: p.width, height: p.height, margin: p.margin, description: p.description }),
|
|
171
|
+
theme,
|
|
172
|
+
x,
|
|
173
|
+
y,
|
|
174
|
+
marks,
|
|
175
|
+
},
|
|
176
|
+
features,
|
|
177
|
+
use: () => {
|
|
178
|
+
ref.current = text;
|
|
179
|
+
return [
|
|
180
|
+
features.tooltip &&
|
|
181
|
+
tooltip(scatter
|
|
182
|
+
? {
|
|
183
|
+
mode: 'nearest',
|
|
184
|
+
text: (label, x, y) => `${label}: ${ref.current.x(x)}, ${ref.current.value(y)}`,
|
|
185
|
+
}
|
|
186
|
+
: bars
|
|
187
|
+
? {
|
|
188
|
+
mode: 'nearest',
|
|
189
|
+
text: (label, _x, v, row) => `${ref.current.title(row)} · ${label}: ${ref.current.value(v)}`,
|
|
190
|
+
}
|
|
191
|
+
: { mode: 'x', title: v => ref.current.title(v), value: v => ref.current.value(v) }),
|
|
192
|
+
features.legend && legend(features.legend),
|
|
193
|
+
features.zoom && zoom(features.zoom),
|
|
194
|
+
features.animate && animate(features.animate),
|
|
195
|
+
].filter((f) => !!f);
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
const themeFor = (t) => t === 'light' ? light : t === 'dark' ? dark : t === 'auto' ? undefined : t;
|
|
201
|
+
/** The options plot() would get, without an element: a theme of 'auto' gives no theme and the light palette. */
|
|
202
|
+
function specOf(build, p) {
|
|
203
|
+
const context = {
|
|
204
|
+
source: source(p.data),
|
|
205
|
+
theme: themeFor(p.theme),
|
|
206
|
+
memo: (_name, _deps, make) => make(),
|
|
207
|
+
};
|
|
208
|
+
const b = build(p, context);
|
|
209
|
+
const passed = Object.fromEntries([...PASSED].filter(k => p[k] !== undefined).map(k => [k, p[k]]));
|
|
210
|
+
return defined({ ...b.options, ...passed, use: b.use() });
|
|
211
|
+
}
|
|
212
|
+
function easyChart(build) {
|
|
213
|
+
return Object.assign((el, props) => create(el, props, build), {
|
|
214
|
+
spec: (props) => specOf(build, props),
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
export const lineChart = easyChart(xy('lineChart'));
|
|
218
|
+
export const areaChart = easyChart(xy('areaChart', 'area'));
|
|
219
|
+
export const barChart = easyChart(xy('barChart', 'bar'));
|
|
220
|
+
export const scatterChart = easyChart(xy('scatterChart', 'scatter'));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Feature } from '../types.js';
|
|
2
|
+
export type AnimateOptions = {
|
|
3
|
+
/** Milliseconds; nothing animates when it is 0 or not finite. */
|
|
4
|
+
duration?: number;
|
|
5
|
+
/**
|
|
6
|
+
* How marks appear on the first paint: uncovered (the default), from the left, from the top where a
|
|
7
|
+
* mark asks for it, or round the clock in a polar frame; or all at once from transparent.
|
|
8
|
+
*/
|
|
9
|
+
enter?: 'reveal' | 'fade';
|
|
10
|
+
};
|
|
11
|
+
export declare function animate({ duration, enter }?: AnimateOptions): Feature;
|