swiftplot 0.0.0-stage → 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.
Files changed (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +327 -2
  3. package/dist/chart.d.ts +3 -0
  4. package/dist/chart.js +1226 -0
  5. package/dist/charts/controller.d.ts +32 -0
  6. package/dist/charts/controller.js +231 -0
  7. package/dist/charts/defaults.d.ts +12 -0
  8. package/dist/charts/defaults.js +60 -0
  9. package/dist/charts/fields.d.ts +34 -0
  10. package/dist/charts/fields.js +167 -0
  11. package/dist/charts/index.d.ts +6 -0
  12. package/dist/charts/index.js +3 -0
  13. package/dist/charts/mount.d.ts +5 -0
  14. package/dist/charts/mount.js +21 -0
  15. package/dist/charts/source.d.ts +24 -0
  16. package/dist/charts/source.js +225 -0
  17. package/dist/charts/types.d.ts +104 -0
  18. package/dist/charts/types.js +1 -0
  19. package/dist/charts/xy.d.ts +36 -0
  20. package/dist/charts/xy.js +220 -0
  21. package/dist/features/animate.d.ts +11 -0
  22. package/dist/features/animate.js +118 -0
  23. package/dist/features/brush.d.ts +13 -0
  24. package/dist/features/brush.js +127 -0
  25. package/dist/features/legend.d.ts +15 -0
  26. package/dist/features/legend.js +237 -0
  27. package/dist/features/pointer.d.ts +29 -0
  28. package/dist/features/pointer.js +23 -0
  29. package/dist/features/tooltip.d.ts +14 -0
  30. package/dist/features/tooltip.js +190 -0
  31. package/dist/features/zoom.d.ts +13 -0
  32. package/dist/features/zoom.js +228 -0
  33. package/dist/gl.d.ts +5 -0
  34. package/dist/gl.js +52 -0
  35. package/dist/index.d.ts +60 -0
  36. package/dist/index.js +31 -0
  37. package/dist/marks/arcs.d.ts +25 -0
  38. package/dist/marks/arcs.js +124 -0
  39. package/dist/marks/area.d.ts +24 -0
  40. package/dist/marks/area.js +106 -0
  41. package/dist/marks/bar.d.ts +44 -0
  42. package/dist/marks/bar.js +243 -0
  43. package/dist/marks/box.d.ts +27 -0
  44. package/dist/marks/box.js +188 -0
  45. package/dist/marks/common.d.ts +84 -0
  46. package/dist/marks/common.js +236 -0
  47. package/dist/marks/dots.d.ts +15 -0
  48. package/dist/marks/dots.js +156 -0
  49. package/dist/marks/errorBars.d.ts +19 -0
  50. package/dist/marks/errorBars.js +79 -0
  51. package/dist/marks/funnel.d.ts +25 -0
  52. package/dist/marks/funnel.js +121 -0
  53. package/dist/marks/heatmap.d.ts +19 -0
  54. package/dist/marks/heatmap.js +139 -0
  55. package/dist/marks/line.d.ts +19 -0
  56. package/dist/marks/line.js +75 -0
  57. package/dist/marks/radar.d.ts +22 -0
  58. package/dist/marks/radar.js +155 -0
  59. package/dist/marks/radialBars.d.ts +27 -0
  60. package/dist/marks/radialBars.js +111 -0
  61. package/dist/marks/rects.d.ts +43 -0
  62. package/dist/marks/rects.js +120 -0
  63. package/dist/marks/rule.d.ts +28 -0
  64. package/dist/marks/rule.js +80 -0
  65. package/dist/marks/sankey.d.ts +41 -0
  66. package/dist/marks/sankey.js +366 -0
  67. package/dist/marks/sunburst.d.ts +26 -0
  68. package/dist/marks/sunburst.js +185 -0
  69. package/dist/marks/ticks.d.ts +9 -0
  70. package/dist/marks/ticks.js +26 -0
  71. package/dist/marks/tree.d.ts +41 -0
  72. package/dist/marks/tree.js +110 -0
  73. package/dist/marks/treemap.d.ts +25 -0
  74. package/dist/marks/treemap.js +159 -0
  75. package/dist/paint.d.ts +30 -0
  76. package/dist/paint.js +66 -0
  77. package/dist/scale.d.ts +16 -0
  78. package/dist/scale.js +96 -0
  79. package/dist/scales/band.d.ts +9 -0
  80. package/dist/scales/band.js +23 -0
  81. package/dist/scales/bare.d.ts +3 -0
  82. package/dist/scales/bare.js +7 -0
  83. package/dist/scales/color.d.ts +18 -0
  84. package/dist/scales/color.js +21 -0
  85. package/dist/scales/log.d.ts +20 -0
  86. package/dist/scales/log.js +97 -0
  87. package/dist/scales/time.d.ts +30 -0
  88. package/dist/scales/time.js +181 -0
  89. package/dist/theme.d.ts +10 -0
  90. package/dist/theme.js +8 -0
  91. package/dist/transforms.d.ts +43 -0
  92. package/dist/transforms.js +118 -0
  93. package/dist/types.d.ts +434 -0
  94. package/dist/types.js +1 -0
  95. package/package.json +39 -3
@@ -0,0 +1,32 @@
1
+ import type { Theme } from '../theme.js';
2
+ import type { Chart, Feature, Options } from '../types.js';
3
+ import { type Source } from './source.js';
4
+ import type { Base } from './types.js';
5
+ export type ControllerProps = Base & {
6
+ data?: unknown;
7
+ };
8
+ export type Context = {
9
+ source: Source<never>;
10
+ theme: Theme;
11
+ /** make() runs again only when deps differ, element by element, from the last call under this name. */
12
+ memo: <T>(name: string, deps: readonly unknown[], make: () => T) => T;
13
+ };
14
+ type Passed = 'view' | 'hidden' | 'selected' | 'pointer' | 'onViewChange' | 'onHiddenChange' | 'onClick' | 'onPointerChange';
15
+ export type Built = {
16
+ options: Omit<Options, 'use' | Passed>;
17
+ /** Compared by value across updates: a change recreates the chart. */
18
+ features: unknown;
19
+ use(): Feature[];
20
+ };
21
+ export type Build<P> = (props: P, context: Context) => Built;
22
+ export type EasyChart<P> = {
23
+ update(props: Partial<P>): void;
24
+ destroy(): void;
25
+ readonly chart: Chart;
26
+ };
27
+ export declare const PASSED: Set<string>;
28
+ export declare const plain: (v: unknown) => v is Record<string, unknown>;
29
+ /** Plain objects and arrays by value, anything else (typed arrays, functions, themes) by identity. */
30
+ export declare const equal: (a: unknown, b: unknown) => boolean;
31
+ export declare function create<P extends ControllerProps>(el: HTMLElement, initial: P, build: Build<P>): EasyChart<P>;
32
+ export {};
@@ -0,0 +1,231 @@
1
+ import { plot } from '../chart.js';
2
+ import { themeOf } from './defaults.js';
3
+ import { source } from './source.js';
4
+ const STATE = ['view', 'hidden', 'selected', 'pointer'];
5
+ const CALLBACKS = ['onHiddenChange', 'onClick', 'onPointerChange'];
6
+ export const PASSED = new Set([...STATE, ...CALLBACKS, 'onViewChange']);
7
+ export const plain = (v) => typeof v === 'object' &&
8
+ v !== null &&
9
+ (Array.isArray(v) || Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);
10
+ /** Plain objects and arrays by value, anything else (typed arrays, functions, themes) by identity. */
11
+ export const equal = (a, b) => same(a, b, new Map());
12
+ function same(a, b, seen) {
13
+ if (Object.is(a, b))
14
+ return true;
15
+ if (!plain(a) || !plain(b) || Array.isArray(a) !== Array.isArray(b))
16
+ return false;
17
+ if (Array.isArray(a) && a.length !== b.length)
18
+ return false;
19
+ const pairs = seen.get(a) ?? seen.set(a, new Set()).get(a);
20
+ if (pairs.has(b))
21
+ return true;
22
+ pairs.add(b);
23
+ const keys = Object.keys(a);
24
+ return (keys.length === Object.keys(b).length && keys.every(k => Object.hasOwn(b, k) && same(a[k], b[k], seen)));
25
+ }
26
+ /** As the core keeps a mark: by key, type and options. */
27
+ const sameMarks = (a, b) => a.length === b.length &&
28
+ a.every((m, i) => m.key === b[i].key && m.type === b[i].type && equal(m.options, b[i].options));
29
+ const lengthsOf = (d) => d == null
30
+ ? []
31
+ : typeof d.length === 'number'
32
+ ? [d.length]
33
+ : Object.values(d).map(c => c?.length);
34
+ export function create(el, initial, build) {
35
+ let props = initial;
36
+ let read;
37
+ const readSource = (p) => {
38
+ const lengths = lengthsOf(p.data);
39
+ const bumped = read && read.version !== p.dataVersion;
40
+ if (read && !bumped && read.data === p.data && equal(read.lengths, lengths))
41
+ return read;
42
+ return {
43
+ data: p.data,
44
+ lengths,
45
+ version: p.dataVersion,
46
+ source: source(p.data, !!bumped || (!!read && !equal(read.lengths, lengths))),
47
+ };
48
+ };
49
+ const memos = new Map();
50
+ const memo = (name, deps, make) => {
51
+ const hit = memos.get(name);
52
+ if (hit && hit.deps.length === deps.length && hit.deps.every((d, i) => Object.is(d, deps[i])))
53
+ return hit.value;
54
+ const value = make();
55
+ memos.set(name, { deps, value });
56
+ return value;
57
+ };
58
+ let internals;
59
+ const keep = c => ((internals = c), { destroy() { } });
60
+ const windowed = {};
61
+ const remember = (v) => {
62
+ const whole = internals.domain();
63
+ for (const k of ['x', 'y']) {
64
+ const side = v[k];
65
+ const all = whole[k].map(e => internals.real(k, e));
66
+ if (side && String(side) !== String(all))
67
+ windowed[k] = [...side];
68
+ else
69
+ delete windowed[k];
70
+ }
71
+ };
72
+ const onViewChange = (v) => {
73
+ remember(v);
74
+ props.onViewChange?.(v);
75
+ };
76
+ const wanted = (p, b) => {
77
+ const w = { ...b.options, onViewChange };
78
+ for (const k of CALLBACKS)
79
+ w[k] = p[k];
80
+ for (const k of STATE)
81
+ if (p[k] !== undefined)
82
+ w[k] = p[k];
83
+ return w;
84
+ };
85
+ let theme;
86
+ const onTheme = (t) => {
87
+ theme = t;
88
+ apply(props, read);
89
+ };
90
+ let themed = themeOf(el, props.theme, onTheme);
91
+ theme = themed.theme;
92
+ let built;
93
+ let sent;
94
+ let chart;
95
+ try {
96
+ built = build(props, { source: (read = readSource(props)).source, theme, memo });
97
+ sent = wanted(props, built);
98
+ chart = plot(el, { ...sent, use: [...built.use(), keep] });
99
+ }
100
+ catch (e) {
101
+ themed.dispose();
102
+ throw e;
103
+ }
104
+ if (props.view)
105
+ remember(props.view);
106
+ let destroyed = false;
107
+ let live = true;
108
+ let held = [];
109
+ /** With r, the options are built again from it; without, only passed props changed. */
110
+ function apply(next, r) {
111
+ let b = built;
112
+ let t = themed;
113
+ let th = theme;
114
+ let uses;
115
+ if (r) {
116
+ if (next.theme !== props.theme)
117
+ th = (t = themeOf(el, next.theme, onTheme)).theme;
118
+ try {
119
+ b = build(next, { source: r.source, theme: th, memo });
120
+ if (!equal(b.features, built.features))
121
+ uses = b.use();
122
+ }
123
+ catch (e) {
124
+ if (t !== themed)
125
+ t.dispose();
126
+ throw e;
127
+ }
128
+ }
129
+ const want = wanted(next, b);
130
+ const hidden = live ? internals.hidden() : held;
131
+ const carried = (p) => ({
132
+ ...(p.view === undefined && Object.keys(windowed).length && { view: { ...windowed } }),
133
+ ...(p.hidden === undefined && hidden.length && { hidden: [...hidden] }),
134
+ });
135
+ const fail = (e) => {
136
+ if (t !== themed)
137
+ t.dispose();
138
+ held = hidden;
139
+ try {
140
+ chart = plot(el, { ...sent, ...carried(props), use: [...built.use(), keep] });
141
+ live = true;
142
+ }
143
+ catch { }
144
+ throw e;
145
+ };
146
+ const drop = () => {
147
+ if (!live)
148
+ return;
149
+ live = false;
150
+ chart.destroy();
151
+ };
152
+ const commit = (s) => {
153
+ props = next;
154
+ if (r)
155
+ read = r;
156
+ built = b;
157
+ theme = th;
158
+ if (t !== themed) {
159
+ themed.dispose();
160
+ themed = t;
161
+ }
162
+ sent = s;
163
+ };
164
+ if (!live)
165
+ uses ??= b.use();
166
+ if (uses) {
167
+ try {
168
+ drop();
169
+ chart = plot(el, { ...want, ...carried(next), use: [...uses, keep] });
170
+ live = true;
171
+ }
172
+ catch (e) {
173
+ fail(e);
174
+ }
175
+ commit(want);
176
+ if (next.view)
177
+ remember(next.view);
178
+ return;
179
+ }
180
+ // view and hidden come back from the live chart through carried(); selected and pointer only from sent.
181
+ const kept = { ...sent };
182
+ for (const k of ['view', 'hidden'])
183
+ if (!(k in want))
184
+ delete kept[k];
185
+ const partial = {};
186
+ for (const k of new Set([...Object.keys(kept), ...Object.keys(want)])) {
187
+ if (!(k in want) && STATE.includes(k))
188
+ continue;
189
+ const [was, now] = [kept[k], want[k]];
190
+ const same = k === 'marks' ? sameMarks(was, now) : equal(was, now);
191
+ if (!same)
192
+ partial[k] = now;
193
+ }
194
+ if (Object.keys(partial).length) {
195
+ try {
196
+ chart.update(partial);
197
+ }
198
+ catch (e) {
199
+ try {
200
+ drop();
201
+ }
202
+ catch { }
203
+ fail(e);
204
+ }
205
+ }
206
+ commit(Object.assign(kept, partial));
207
+ if (partial.view)
208
+ remember(partial.view);
209
+ }
210
+ return {
211
+ get chart() {
212
+ return chart;
213
+ },
214
+ update(partial) {
215
+ if (destroyed)
216
+ return;
217
+ const next = { ...props, ...partial };
218
+ const changed = Object.keys(partial).some(k => !PASSED.has(k) && props[k] !== next[k]);
219
+ const r = readSource(next);
220
+ apply(next, changed || r !== read ? r : undefined);
221
+ },
222
+ destroy() {
223
+ if (destroyed)
224
+ return;
225
+ destroyed = true;
226
+ themed.dispose();
227
+ if (live)
228
+ chart.destroy();
229
+ },
230
+ };
231
+ }
@@ -0,0 +1,12 @@
1
+ import { type Theme } from '../theme.js';
2
+ import type { Col } from '../types.js';
3
+ export declare const formatNumber: (v: number) => string;
4
+ export declare function timeTitle(columns: readonly Col[], utc: boolean): (t: number) => string;
5
+ export declare const bandTitle: (categories: readonly string[], x: number) => string;
6
+ export type ThemeProp = 'auto' | 'light' | 'dark' | Theme | undefined;
7
+ /** 'auto' reads the element's computed color-scheme; onChange fires when a 'light dark' page flips. */
8
+ export declare function themeOf(el: Element, prop: ThemeProp, onChange: (theme: Theme) => void): {
9
+ theme: Theme;
10
+ dispose(): void;
11
+ };
12
+ export declare function colorsFor(theme: Theme | undefined, colors: readonly string[] | undefined, n: number): string[];
@@ -0,0 +1,60 @@
1
+ import { palette, paletteDark } from '../paint.js';
2
+ import { dark, light } from '../theme.js';
3
+ const DAY = 864e5;
4
+ const numbers = new Intl.NumberFormat('en-US', { maximumFractionDigits: 2 });
5
+ export const formatNumber = (v) => numbers.format(v);
6
+ const pad = (n, w = 2) => String(n).padStart(w, '0');
7
+ export function timeTitle(columns, utc) {
8
+ const parts = (t) => {
9
+ const d = new Date(t);
10
+ return utc
11
+ ? [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes()]
12
+ : [d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes()];
13
+ };
14
+ const midnight = (t) => {
15
+ const d = new Date(t);
16
+ return utc
17
+ ? t % DAY === 0
18
+ : d.getHours() === 0 && d.getMinutes() === 0 && d.getSeconds() === 0 && d.getMilliseconds() === 0;
19
+ };
20
+ let dateOnly = true;
21
+ for (const xs of columns)
22
+ for (let i = 0; i < xs.length && dateOnly; i++) {
23
+ if (Number.isFinite(xs[i]))
24
+ dateOnly = midnight(xs[i]);
25
+ }
26
+ return t => {
27
+ const [y, mo, day, h, mi] = parts(t);
28
+ const date = `${y < 0 ? '-' : ''}${pad(Math.abs(y), 4)}-${pad(mo)}-${pad(day)}`;
29
+ return dateOnly ? date : `${date} ${pad(h)}:${pad(mi)}`;
30
+ };
31
+ }
32
+ export const bandTitle = (categories, x) => categories[Math.min(Math.max(Math.round(x), 0), categories.length - 1)];
33
+ /** 'auto' reads the element's computed color-scheme; onChange fires when a 'light dark' page flips. */
34
+ export function themeOf(el, prop, onChange) {
35
+ const none = () => { };
36
+ if (prop === 'light')
37
+ return { theme: light, dispose: none };
38
+ if (prop === 'dark')
39
+ return { theme: dark, dispose: none };
40
+ if (prop !== undefined && prop !== 'auto')
41
+ return { theme: prop, dispose: none };
42
+ const fixed = () => {
43
+ const words = getComputedStyle(el).colorScheme.split(/\s+/);
44
+ if (!words.includes('dark'))
45
+ return light;
46
+ return words.includes('light') ? undefined : dark;
47
+ };
48
+ const theme = fixed();
49
+ if (theme)
50
+ return { theme, dispose: none };
51
+ const mq = matchMedia('(prefers-color-scheme: dark)');
52
+ const resolve = () => fixed() ?? (mq.matches ? dark : light);
53
+ const listen = () => onChange(resolve());
54
+ mq.addEventListener('change', listen);
55
+ return { theme: resolve(), dispose: () => mq.removeEventListener('change', listen) };
56
+ }
57
+ export function colorsFor(theme, colors, n) {
58
+ const list = colors?.length ? colors : theme === dark ? paletteDark : palette;
59
+ return Array.from({ length: n }, (_, i) => list[i % list.length]);
60
+ }
@@ -0,0 +1,34 @@
1
+ import type { Col } from '../types.js';
2
+ import type { Accessor, Kind, Source } from './source.js';
3
+ type Field<R> = string | Accessor<R>;
4
+ export type FieldProps<R> = {
5
+ x?: Field<R>;
6
+ y?: Field<R> | readonly Field<R>[];
7
+ series?: Field<R>;
8
+ r?: number | Field<R>;
9
+ xAxis?: {
10
+ utc?: boolean;
11
+ };
12
+ };
13
+ export type Family = 'xy' | 'scatter';
14
+ export type Series = {
15
+ name: string;
16
+ x: Col;
17
+ y: Col;
18
+ r?: Col;
19
+ };
20
+ export type Assigned = {
21
+ x: {
22
+ label: string;
23
+ kind: Kind | 'index';
24
+ categories?: string[];
25
+ };
26
+ y: {
27
+ label: string;
28
+ };
29
+ ys: Series[];
30
+ /** With series and firstSeen: the distinct x across all series in order of first appearance. */
31
+ order?: number[];
32
+ };
33
+ export declare function assign<R>(s: Source<R>, props: FieldProps<R>, family: Family, name: string, firstSeen?: boolean): Assigned;
34
+ export {};
@@ -0,0 +1,167 @@
1
+ const quote = (f) => `"${f}"`;
2
+ export function assign(s, props, family, name, firstSeen = false) {
3
+ const p = s.shape === 'numbers'
4
+ ? { xAxis: props.xAxis, y: 'value' }
5
+ : s.shape === 'pairs'
6
+ ? { xAxis: props.xAxis, x: 'x', y: 'y' }
7
+ : props;
8
+ const own = (read) => {
9
+ try {
10
+ return read();
11
+ }
12
+ catch (e) {
13
+ throw new Error(`${name}: ${e.message}`, { cause: e });
14
+ }
15
+ };
16
+ const fields = own(() => s.fields());
17
+ const kinds = new Map(fields.map(f => [f, own(() => s.kind(f))]));
18
+ if (!s.length)
19
+ return { x: { label: '', kind: 'index' }, y: { label: '' }, ys: [] };
20
+ const unplottable = (f) => {
21
+ if (kinds.get(f))
22
+ return false;
23
+ try {
24
+ s.column(f);
25
+ return false;
26
+ }
27
+ catch {
28
+ return true;
29
+ }
30
+ };
31
+ const describe = (f) => kinds.get(f) ?? (unplottable(f) ? 'not plottable' : 'no values');
32
+ const fail = (why, pass, cause) => {
33
+ const list = fields.length
34
+ ? `The fields are ${fields.map(f => `${f} (${describe(f)})`).join(', ')}`
35
+ : 'There are no fields';
36
+ throw new Error(`${name}: ${why}. ${list}. Pass ${pass}.`, { cause });
37
+ };
38
+ const named = p.y === undefined ? [] : (Array.isArray(p.y) ? p.y : [p.y]);
39
+ const r = family === 'scatter' && typeof p.r !== 'number' ? p.r : undefined;
40
+ const roles = [
41
+ ['x', p.x],
42
+ ...named.map(f => ['y', f]),
43
+ ['series', p.series],
44
+ ['r', r],
45
+ ];
46
+ const names = roles.map(([, f]) => f).filter((f) => typeof f === 'string');
47
+ for (const [role, f] of roles) {
48
+ if (typeof f !== 'string')
49
+ continue;
50
+ if (!kinds.has(f)) {
51
+ // fields() lists only the keys of the first rows, so a named field is looked up in every row
52
+ const kind = (() => {
53
+ try {
54
+ return s.kind(f);
55
+ }
56
+ catch {
57
+ return null;
58
+ }
59
+ })();
60
+ if (kind === null || (!kind && !unplottable(f)))
61
+ fail(`there is no field ${quote(f)}`, `${role} as one of these fields`);
62
+ kinds.set(f, kind ?? undefined);
63
+ }
64
+ if (unplottable(f))
65
+ fail(`${quote(f)} holds values that are not numbers, dates or text`, `${role} as another field`);
66
+ const kind = kinds.get(f);
67
+ if ((role === 'y' || role === 'r') && kind && kind !== 'number')
68
+ fail(`${role} ${quote(f)} is ${kind}, not numbers`, `${role} as a field of numbers`);
69
+ }
70
+ const pool = fields.filter(f => kinds.get(f) && !names.includes(f));
71
+ const first = (k, not) => pool.find(f => kinds.get(f) === k && f !== not);
72
+ let x = p.x;
73
+ if (x === undefined && family === 'scatter')
74
+ x = first('time') ?? first('number');
75
+ else if (x === undefined) {
76
+ x = first('time') ?? first('text');
77
+ const numbers = pool.filter(f => kinds.get(f) === 'number');
78
+ if (x === undefined && numbers.length > (named.length ? 0 : 1))
79
+ x = numbers[0];
80
+ }
81
+ const xField = x;
82
+ const left = pool.filter(f => f !== xField && kinds.get(f) === 'number');
83
+ const ys = named.length ? named : family === 'scatter' ? left.slice(0, 1) : left;
84
+ if (family === 'scatter') {
85
+ if (named.length > 1)
86
+ fail('a scatter chart has one y', 'y as one field');
87
+ if (!ys.length || (x === undefined && !s.shape))
88
+ fail('a scatter chart needs two fields of numbers or dates', 'x and y as fields of numbers or dates');
89
+ }
90
+ else if (!ys.length) {
91
+ const asText = fields.filter(f => kinds.get(f) === 'text' &&
92
+ own(() => s.column(f)).categories.every(c => c.trim() !== '' && Number.isFinite(Number(c))));
93
+ const hint = asText.length
94
+ ? `; ${asText.map(quote).join(', ')} ${asText.length > 1 ? 'hold' : 'holds'} numbers as text: convert them with Number()`
95
+ : '';
96
+ fail(`there is no field of numbers for y${hint}`, 'y as a field of numbers');
97
+ }
98
+ const leftText = pool.find(f => f !== x && kinds.get(f) === 'text');
99
+ if (family === 'xy' && p.series === undefined && leftText !== undefined)
100
+ fail(`${quote(leftText)} is text, so the rows look like long data with one row per series and x`, `series: '${leftText}'`);
101
+ if (p.series !== undefined && ys.length > 1)
102
+ fail('with series there is one y', 'y as one field');
103
+ const read = (f, what, role, zone) => {
104
+ if (typeof f === 'string')
105
+ return own(() => s.column(f, zone));
106
+ try {
107
+ return s.column(f, zone);
108
+ }
109
+ catch (e) {
110
+ return fail(`${what} cannot be read: ${e.message}`, `${role} as another field`, e);
111
+ }
112
+ };
113
+ const numeric = (f, i, role) => {
114
+ const what = typeof f === 'string' ? `${role} ${quote(f)}` : `the accessor ${role}${i + 1}`;
115
+ const c = read(f, what, role);
116
+ if (c.kind !== 'number')
117
+ fail(`${what} is ${c.kind}, not numbers`, `${role} as a field of numbers`);
118
+ return c;
119
+ };
120
+ const yColumns = ys.map((f, i) => numeric(f, i, 'y').values);
121
+ const rColumn = r === undefined ? undefined : numeric(r, 0, 'r').values;
122
+ const xColumn = x === undefined ? undefined : read(xField, 'the accessor x', 'x', { utc: p.xAxis?.utc ?? true });
123
+ const xs = xColumn?.values ?? Float64Array.from({ length: s.length }, (_, i) => i);
124
+ const kept = [];
125
+ for (let i = 0; i < s.length; i++)
126
+ if (Number.isFinite(xs[i]) && (!rColumn || Number.isFinite(rColumn[i])))
127
+ kept.push(i);
128
+ const at = (c, rows) => (rows.length === s.length ? c : Float64Array.from(rows, i => c[i]));
129
+ const series = (label, rows, y) => ({
130
+ name: label,
131
+ x: at(xs, rows),
132
+ y: at(y, rows),
133
+ ...(rColumn && { r: at(rColumn, rows) }),
134
+ });
135
+ let out;
136
+ let order;
137
+ if (p.series === undefined)
138
+ out = ys.map((f, i) => series(typeof f === 'string' ? f : `y${i + 1}`, kept, yColumns[i]));
139
+ else {
140
+ const by = read(p.series, 'the accessor series', 'series');
141
+ const groups = new Map();
142
+ if (firstSeen)
143
+ order = new Set();
144
+ for (const i of kept) {
145
+ const v = by.values[i];
146
+ if (Number.isNaN(v))
147
+ continue;
148
+ order?.add(xs[i]);
149
+ const rows = groups.get(v);
150
+ if (rows)
151
+ rows.push(i);
152
+ else
153
+ groups.set(v, [i]);
154
+ }
155
+ out = [...groups].map(([v, rows]) => series(by.categories?.[v] ?? String(v), rows, yColumns[0]));
156
+ }
157
+ return {
158
+ x: {
159
+ label: typeof x === 'string' ? x : '',
160
+ kind: xColumn?.kind ?? 'index',
161
+ ...(xColumn?.categories && { categories: xColumn.categories }),
162
+ },
163
+ y: { label: ys.length === 1 && typeof ys[0] === 'string' ? ys[0] : '' },
164
+ ys: out,
165
+ ...(order && { order: [...order] }),
166
+ };
167
+ }
@@ -0,0 +1,6 @@
1
+ export { areaChart, barChart, lineChart, scatterChart } from './xy.js';
2
+ export { equal, plain } from './controller.js';
3
+ export type { EasyChart } from './controller.js';
4
+ export { mount } from './mount.js';
5
+ export type { Mount } from './mount.js';
6
+ export type { AreaProps as AreaChartProps, BarProps as BarChartProps, LineProps as LineChartProps, ScatterProps as ScatterChartProps, } from './types.js';
@@ -0,0 +1,3 @@
1
+ export { areaChart, barChart, lineChart, scatterChart } from './xy.js';
2
+ export { equal, plain } from './controller.js';
3
+ 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,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>;