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,52 @@
|
|
|
1
|
+
import { type TooltipOptions } from '../features/tooltip.js';
|
|
2
|
+
import type { Axis, Feature, Options } from '../types.js';
|
|
3
|
+
import { type Build, type EasyChart } from './controller.js';
|
|
4
|
+
import type { AreaProps, Axes, BarProps, Base, BoxPlotProps, CandlestickProps, ComboProps, FunnelProps, HeatmapProps, HistogramProps, Input, LineProps, PieProps, RadarProps, RadialBarProps, SankeyProps, ScatterProps, SunburstProps, TreemapProps, XAxisProps, YAxisProps } from './types.js';
|
|
5
|
+
export declare const defined: <T extends object>(o: T) => T;
|
|
6
|
+
export declare const on: <T extends object>(v: boolean | T | undefined) => T | false;
|
|
7
|
+
export declare const sizeOf: (p: Base) => {
|
|
8
|
+
width: number | undefined;
|
|
9
|
+
height: number | undefined;
|
|
10
|
+
margin: Partial<import("../types.js").Margin> | undefined;
|
|
11
|
+
description: string | undefined;
|
|
12
|
+
};
|
|
13
|
+
export declare const featuresOf: (p: Base & Pick<Axes, 'zoom'>, listed: number) => {
|
|
14
|
+
tooltip: boolean;
|
|
15
|
+
legend: false | import("../index.js").LegendOptions;
|
|
16
|
+
zoom: false | import("../index.js").ZoomOptions;
|
|
17
|
+
animate: false | import("../index.js").AnimateOptions;
|
|
18
|
+
};
|
|
19
|
+
export declare const install: (f: ReturnType<typeof featuresOf>, tip: TooltipOptions) => Feature[];
|
|
20
|
+
export declare const linear: (a: YAxisProps | undefined, label: string) => Axis;
|
|
21
|
+
export declare const dates: (a: XAxisProps | undefined, label: string, utc: boolean) => Axis;
|
|
22
|
+
export declare const given: (p: object, k: string) => boolean;
|
|
23
|
+
export declare function noAxes(p: object, name: string): void;
|
|
24
|
+
type Kinds<D> = {
|
|
25
|
+
line: LineProps<D>;
|
|
26
|
+
area: AreaProps<D>;
|
|
27
|
+
bar: BarProps<D>;
|
|
28
|
+
scatter: ScatterProps<D>;
|
|
29
|
+
pie: PieProps<D>;
|
|
30
|
+
funnel: FunnelProps<D>;
|
|
31
|
+
radialBar: RadialBarProps<D>;
|
|
32
|
+
radar: RadarProps<D>;
|
|
33
|
+
heatmap: HeatmapProps<D>;
|
|
34
|
+
histogram: HistogramProps<D>;
|
|
35
|
+
boxPlot: BoxPlotProps<D>;
|
|
36
|
+
candlestick: CandlestickProps<D>;
|
|
37
|
+
combo: ComboProps<D>;
|
|
38
|
+
sankey: SankeyProps<D>;
|
|
39
|
+
treemap: TreemapProps;
|
|
40
|
+
sunburst: SunburstProps;
|
|
41
|
+
};
|
|
42
|
+
type Supplied<K extends keyof Kinds<never>, D = Input | undefined> = D extends unknown ? Kinds<D>[K] : never;
|
|
43
|
+
type Later<K extends keyof Kinds<never>> = Omit<EasyChart<never>, 'update'> & {
|
|
44
|
+
update<D extends Input | undefined = undefined>(props: Partial<Kinds<D>[K]> & {
|
|
45
|
+
data?: D | null;
|
|
46
|
+
}): void;
|
|
47
|
+
};
|
|
48
|
+
type ChartOf<K extends keyof Kinds<never>, D> = [D] extends [undefined | readonly never[]] ? K extends 'treemap' | 'sunburst' ? EasyChart<Kinds<D>[K]> : Later<K> : EasyChart<Kinds<D>[K]>;
|
|
49
|
+
export declare function easyChart<K extends keyof Kinds<never>>(build: Build<Supplied<K>>): (<D extends Input | undefined = undefined>(el: HTMLElement, props: Kinds<D>[K]) => ChartOf<K, D>) & {
|
|
50
|
+
spec: <D extends Input | undefined = undefined>(props: Kinds<D>[K]) => Options;
|
|
51
|
+
};
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
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 { time } from '../scales/time.js';
|
|
6
|
+
import { dark, light } from '../theme.js';
|
|
7
|
+
import { create, PASSED, } from './controller.js';
|
|
8
|
+
import { source } from './source.js';
|
|
9
|
+
export const defined = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
10
|
+
export const on = (v) => v === true ? {} : v || false;
|
|
11
|
+
export const sizeOf = (p) => defined({ width: p.width, height: p.height, margin: p.margin, description: p.description });
|
|
12
|
+
export const featuresOf = (p, listed) => ({
|
|
13
|
+
tooltip: p.tooltip !== false,
|
|
14
|
+
legend: p.legend !== false && listed >= 2 && (typeof p.legend === 'object' ? p.legend : {}),
|
|
15
|
+
zoom: on(p.zoom),
|
|
16
|
+
animate: on(p.animate),
|
|
17
|
+
});
|
|
18
|
+
export const install = (f, tip) => [
|
|
19
|
+
f.tooltip && tooltip(tip),
|
|
20
|
+
f.legend && legend(f.legend),
|
|
21
|
+
f.zoom && zoom(f.zoom),
|
|
22
|
+
f.animate && animate(f.animate),
|
|
23
|
+
].filter((x) => !!x);
|
|
24
|
+
export const linear = (a, label) => defined({
|
|
25
|
+
domain: a?.domain ?? 'nice',
|
|
26
|
+
label: a?.label ?? label,
|
|
27
|
+
step: a?.step,
|
|
28
|
+
format: a?.format,
|
|
29
|
+
fit: a?.fit,
|
|
30
|
+
});
|
|
31
|
+
export const dates = (a, label, utc) => time({
|
|
32
|
+
label: a?.label ?? label,
|
|
33
|
+
utc,
|
|
34
|
+
format: a?.format,
|
|
35
|
+
fit: a?.fit,
|
|
36
|
+
...(Array.isArray(a?.domain) && { domain: a.domain }),
|
|
37
|
+
...(a?.unit && { unit: a.unit, every: a.every }),
|
|
38
|
+
});
|
|
39
|
+
export const given = (p, k) => p[k] !== undefined;
|
|
40
|
+
export function noAxes(p, name) {
|
|
41
|
+
for (const k of ['xAxis', 'yAxis', 'zoom'])
|
|
42
|
+
if (given(p, k))
|
|
43
|
+
throw new Error(`${name}: ${k} does not apply, as there are no axes`);
|
|
44
|
+
}
|
|
45
|
+
const themeFor = (t) => t === 'light' ? light : t === 'dark' ? dark : t === 'auto' ? undefined : t;
|
|
46
|
+
/** The options plot() would get, without an element: a theme of 'auto' builds with the light theme and leaves it out. */
|
|
47
|
+
function specOf(build, p) {
|
|
48
|
+
const theme = themeFor(p.theme);
|
|
49
|
+
const context = {
|
|
50
|
+
source: source(p.data),
|
|
51
|
+
theme: theme ?? light,
|
|
52
|
+
memo: (_name, _deps, make) => make(),
|
|
53
|
+
};
|
|
54
|
+
const b = build(p, context);
|
|
55
|
+
const passed = Object.fromEntries([...PASSED].filter(k => p[k] !== undefined).map(k => [k, p[k]]));
|
|
56
|
+
const given = !theme && b.options.theme === light ? undefined : b.options.theme;
|
|
57
|
+
return defined({ ...b.options, theme: given, ...passed, use: b.use() });
|
|
58
|
+
}
|
|
59
|
+
export function easyChart(build) {
|
|
60
|
+
return Object.assign((el, props) => create(el, props, build), {
|
|
61
|
+
spec: (props) => specOf(build, props),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CandlestickProps } from './types.js';
|
|
2
|
+
export declare const candlestickChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: CandlestickProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
3
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<CandlestickProps<D_1>> & {
|
|
4
|
+
data?: D_1 | null | undefined;
|
|
5
|
+
}): void;
|
|
6
|
+
} : import("./controller.js").EasyChart<CandlestickProps<D>>) & {
|
|
7
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: CandlestickProps<D>) => import("../types.js").Options;
|
|
8
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { candlestick } from '../marks/candlestick.js';
|
|
2
|
+
import { band } from '../scales/band.js';
|
|
3
|
+
import { dates, defined, easyChart, featuresOf, given, install, linear, sizeOf } from './build.js';
|
|
4
|
+
import { colorsFor, DAY, formatNumber, namesOf, spacing } from './defaults.js';
|
|
5
|
+
import { reader } from './fields.js';
|
|
6
|
+
const NAME = 'candlestickChart';
|
|
7
|
+
const PRICES = ['open', 'high', 'low', 'close'];
|
|
8
|
+
const LISTED = 'open, high, low and close';
|
|
9
|
+
function pick(s, p, utc) {
|
|
10
|
+
const { kinds, fail, check, 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 candles', `rows or columns with fields for x, ${LISTED}`);
|
|
15
|
+
const named = PRICES.map(k => [k, p[k]]);
|
|
16
|
+
check([['x', p.x], ...named], PRICES);
|
|
17
|
+
const unnamed = s.fields().filter(f => f !== p.x && !named.some(([, g]) => g === f));
|
|
18
|
+
const priced = PRICES.map((role) => {
|
|
19
|
+
const field = p[role];
|
|
20
|
+
if (field !== undefined)
|
|
21
|
+
return [role, field];
|
|
22
|
+
const found = unnamed.filter(f => f.toLowerCase() === role);
|
|
23
|
+
if (found.length !== 1)
|
|
24
|
+
fail(found.length
|
|
25
|
+
? `${found.map(f => `"${f}"`).join(', ')} are all called ${role}, so it cannot be guessed`
|
|
26
|
+
: `there is no field called ${role}`, `${LISTED} as fields of numbers`);
|
|
27
|
+
return [role, found[0]];
|
|
28
|
+
});
|
|
29
|
+
const pool = check([['x', p.x], ...priced], PRICES);
|
|
30
|
+
const first = (k) => pool.find(f => kinds.get(f) === k);
|
|
31
|
+
const x = p.x ??
|
|
32
|
+
first('time') ??
|
|
33
|
+
first('text') ??
|
|
34
|
+
fail('there is no field of dates or text for x', 'x as the field of where each candle stands');
|
|
35
|
+
const prices = Object.fromEntries(priced.map(([k, f]) => [k, numeric(f, k).values]));
|
|
36
|
+
const { kind, values, categories } = read(x, 'the accessor x', 'x', { utc });
|
|
37
|
+
const name = namesOf(kind, categories, [values], utc);
|
|
38
|
+
const at = kind === 'text' ? Float64Array.from(values, (v, i) => (Number.isFinite(v) ? i : Number.NaN)) : values;
|
|
39
|
+
const nameAt = (row) => (Number.isFinite(values[row]) ? name(values[row]) : '');
|
|
40
|
+
return {
|
|
41
|
+
kind,
|
|
42
|
+
label: typeof x === 'string' ? x : '',
|
|
43
|
+
x: at,
|
|
44
|
+
...(kind === 'text'
|
|
45
|
+
? { width: 0.6, categories: Array.from(at, (_, i) => nameAt(i)) }
|
|
46
|
+
: spacing(at, kind === 'time' ? DAY : 1)),
|
|
47
|
+
nameAt,
|
|
48
|
+
prices,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function xAxis({ kind, label, lone, categories }, a, utc) {
|
|
52
|
+
if (categories)
|
|
53
|
+
return band({ label: a?.label ?? label, categories });
|
|
54
|
+
const shown = lone && !Array.isArray(a?.domain) ? { ...a, domain: lone } : a;
|
|
55
|
+
return kind === 'number' ? linear(shown, label) : dates(shown, label, utc);
|
|
56
|
+
}
|
|
57
|
+
const build = (p, { source: s, theme, memo }) => {
|
|
58
|
+
if (s.length)
|
|
59
|
+
for (const k of ['y', 'series', 'label', 'value'])
|
|
60
|
+
if (given(p, k))
|
|
61
|
+
throw new Error(`${NAME}: use x, ${LISTED}, not ${k}`);
|
|
62
|
+
const utc = p.xAxis?.utc ?? true;
|
|
63
|
+
const picked = memo('fields', [s, p.x, ...PRICES.map(k => p[k]), utc], () => pick(s, p, utc));
|
|
64
|
+
const color = colorsFor(theme, p.colors, 1)[0];
|
|
65
|
+
const marks = memo('marks', [picked, color, p.up, p.down], () => picked
|
|
66
|
+
? [
|
|
67
|
+
candlestick(defined({
|
|
68
|
+
key: 'candlestick',
|
|
69
|
+
label: 'price',
|
|
70
|
+
x: picked.x,
|
|
71
|
+
...picked.prices,
|
|
72
|
+
width: picked.width,
|
|
73
|
+
color,
|
|
74
|
+
up: p.up,
|
|
75
|
+
down: p.down,
|
|
76
|
+
})),
|
|
77
|
+
]
|
|
78
|
+
: []);
|
|
79
|
+
const features = featuresOf(p, 1);
|
|
80
|
+
const text = { picked, value: p.yAxis?.format ?? formatNumber };
|
|
81
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
82
|
+
ref.current = text;
|
|
83
|
+
return {
|
|
84
|
+
options: {
|
|
85
|
+
...sizeOf(p),
|
|
86
|
+
theme,
|
|
87
|
+
x: picked ? xAxis(picked, p.xAxis, utc) : linear(p.xAxis, ''),
|
|
88
|
+
y: linear(p.yAxis, ''),
|
|
89
|
+
marks,
|
|
90
|
+
},
|
|
91
|
+
features,
|
|
92
|
+
use: () => {
|
|
93
|
+
ref.current = text;
|
|
94
|
+
return install(features, {
|
|
95
|
+
mode: 'nearest',
|
|
96
|
+
text: (_label, _x, _y, row) => {
|
|
97
|
+
const { picked: shown, value } = ref.current;
|
|
98
|
+
const numbers = PRICES.map(k => `${k} ${value(shown.prices[k][row])}`);
|
|
99
|
+
return `${shown.nameAt(row)} · ${numbers.join(', ')}`;
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
export const candlestickChart = easyChart(build);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ComboProps } from './types.js';
|
|
2
|
+
export declare const comboChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: ComboProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
3
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<ComboProps<D_1>> & {
|
|
4
|
+
data?: D_1 | null | undefined;
|
|
5
|
+
}): void;
|
|
6
|
+
} : import("./controller.js").EasyChart<ComboProps<D>>) & {
|
|
7
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: ComboProps<D>) => import("../types.js").Options;
|
|
8
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { easyChart, given } from './build.js';
|
|
2
|
+
import { list, reader } from './fields.js';
|
|
3
|
+
import { xy } from './xy.js';
|
|
4
|
+
const NAME = 'comboChart';
|
|
5
|
+
const ROLES = ['lines', 'bars', 'y2Lines', 'y2Bars'];
|
|
6
|
+
const LISTED = 'lines, bars, y2Lines and y2Bars';
|
|
7
|
+
function check(s, p) {
|
|
8
|
+
if (!s.length)
|
|
9
|
+
return;
|
|
10
|
+
const { fail } = reader(s, NAME);
|
|
11
|
+
if (s.shape)
|
|
12
|
+
fail('a list of numbers or pairs has no fields to name', `rows or columns with fields for ${LISTED}`);
|
|
13
|
+
const named = ROLES.flatMap(k => list(p[k]).map(f => [k, f]));
|
|
14
|
+
if (!named.length)
|
|
15
|
+
fail(`none of ${LISTED} is given`, 'at least one of them as a field of numbers');
|
|
16
|
+
const seen = new Map();
|
|
17
|
+
for (const [role, f] of named) {
|
|
18
|
+
if (typeof f !== 'string')
|
|
19
|
+
continue;
|
|
20
|
+
const other = seen.get(f);
|
|
21
|
+
if (other === role)
|
|
22
|
+
fail(`"${f}" is named twice in ${role}`, 'it once');
|
|
23
|
+
if (other)
|
|
24
|
+
fail(`"${f}" is named in both ${other} and ${role}`, 'it in one of them');
|
|
25
|
+
seen.set(f, role);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const drawn = xy(NAME, 'combo');
|
|
29
|
+
const build = (p, context) => {
|
|
30
|
+
const { source: s, memo } = context;
|
|
31
|
+
if (s.length)
|
|
32
|
+
for (const k of ['y', 'y2', 'series', 'errorBars'])
|
|
33
|
+
if (given(p, k))
|
|
34
|
+
throw new Error(`${NAME}: use ${LISTED}, not ${k}`);
|
|
35
|
+
memo('checked', [s, ...ROLES.flatMap(k => [k, ...list(p[k])])], () => check(s, p));
|
|
36
|
+
const roles = ROLES.flatMap(k => list(p[k]).map(() => k));
|
|
37
|
+
return drawn({
|
|
38
|
+
...p,
|
|
39
|
+
y: [...list(p.lines), ...list(p.bars)],
|
|
40
|
+
y2: [...list(p.y2Lines), ...list(p.y2Bars)],
|
|
41
|
+
roles,
|
|
42
|
+
barred: roles.map(k => k === 'bars' || k === 'y2Bars'),
|
|
43
|
+
series: undefined,
|
|
44
|
+
errorBars: undefined,
|
|
45
|
+
curve: undefined,
|
|
46
|
+
join: undefined,
|
|
47
|
+
fill: undefined,
|
|
48
|
+
}, context);
|
|
49
|
+
};
|
|
50
|
+
export const comboChart = easyChart(build);
|
|
@@ -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 { Axes, Base } from './types.js';
|
|
5
|
+
export type ControllerProps = Base & Pick<Axes, 'view' | 'onViewChange'> & {
|
|
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', 'y2']) {
|
|
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,20 @@
|
|
|
1
|
+
import { type Theme } from '../theme.js';
|
|
2
|
+
import type { Col, Span } from '../types.js';
|
|
3
|
+
import type { Kind } from './source.js';
|
|
4
|
+
export declare const DAY = 86400000;
|
|
5
|
+
export declare const formatNumber: (v: number) => string;
|
|
6
|
+
export declare function timeTitle(columns: readonly Col[], utc: boolean, distinct?: boolean): (t: number) => string;
|
|
7
|
+
export declare const namesOf: (kind: Kind | 'index', categories: readonly string[] | undefined, columns: readonly Col[], utc: boolean) => ((v: number) => string);
|
|
8
|
+
export declare const bandTitle: (categories: readonly string[], x: number) => string;
|
|
9
|
+
/** 0.6 of the smallest gap between distinct x, or of fallback where there is none. */
|
|
10
|
+
export declare function spacing(x: Col, fallback: number): {
|
|
11
|
+
width: number;
|
|
12
|
+
lone?: Span;
|
|
13
|
+
};
|
|
14
|
+
export type ThemeProp = 'auto' | 'light' | 'dark' | Theme | undefined;
|
|
15
|
+
/** 'auto' reads the element's computed color-scheme; onChange fires when a 'light dark' page flips. */
|
|
16
|
+
export declare function themeOf(el: Element, prop: ThemeProp, onChange: (theme: Theme) => void): {
|
|
17
|
+
theme: Theme;
|
|
18
|
+
dispose(): void;
|
|
19
|
+
};
|
|
20
|
+
export declare function colorsFor(theme: Theme | undefined, colors: readonly string[] | undefined, n: number): string[];
|