swiftplot 0.1.1 → 0.4.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/README.md +86 -61
- package/dist/chart.d.ts +4 -0
- package/dist/chart.js +172 -89
- package/dist/charts/build.d.ts +53 -0
- package/dist/charts/build.js +69 -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 +2 -2
- package/dist/charts/controller.js +2 -2
- package/dist/charts/defaults.d.ts +10 -2
- package/dist/charts/defaults.js +67 -6
- package/dist/charts/distribution.d.ts +15 -0
- package/dist/charts/distribution.js +178 -0
- package/dist/charts/fields.d.ts +33 -3
- package/dist/charts/fields.js +150 -66
- 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 +8 -1
- package/dist/charts/index.js +7 -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/types.d.ts +564 -23
- package/dist/charts/xy.d.ts +35 -25
- package/dist/charts/xy.js +327 -126
- package/dist/features/animate.d.ts +10 -1
- package/dist/features/animate.js +3 -3
- package/dist/features/brush.d.ts +3 -1
- package/dist/features/brush.js +2 -1
- package/dist/features/legend.d.ts +1 -3
- package/dist/features/legend.js +4 -5
- package/dist/features/tooltip.d.ts +10 -2
- package/dist/features/tooltip.js +20 -15
- package/dist/features/zoom.d.ts +1 -0
- package/dist/features/zoom.js +22 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/marks/arcs.d.ts +10 -4
- package/dist/marks/arcs.js +2 -1
- package/dist/marks/area.d.ts +9 -8
- package/dist/marks/area.js +2 -2
- package/dist/marks/bar.d.ts +18 -10
- package/dist/marks/bar.js +5 -5
- package/dist/marks/box.d.ts +7 -1
- package/dist/marks/box.js +2 -8
- package/dist/marks/candlestick.d.ts +22 -0
- package/dist/marks/candlestick.js +133 -0
- package/dist/marks/common.d.ts +18 -0
- package/dist/marks/common.js +14 -0
- package/dist/marks/dots.d.ts +6 -5
- package/dist/marks/dots.js +20 -5
- package/dist/marks/errorBars.d.ts +9 -4
- package/dist/marks/errorBars.js +21 -1
- package/dist/marks/funnel.d.ts +8 -3
- package/dist/marks/funnel.js +2 -1
- package/dist/marks/heatmap.d.ts +8 -7
- package/dist/marks/line.d.ts +8 -6
- package/dist/marks/radar.d.ts +9 -2
- package/dist/marks/radar.js +9 -2
- package/dist/marks/radialBars.d.ts +12 -5
- package/dist/marks/rects.js +15 -5
- package/dist/marks/rule.d.ts +19 -0
- package/dist/marks/rule.js +4 -1
- package/dist/marks/sankey.d.ts +14 -2
- package/dist/marks/sankey.js +4 -1
- package/dist/marks/sunburst.d.ts +5 -0
- package/dist/marks/sunburst.js +2 -1
- package/dist/marks/ticks.d.ts +4 -1
- package/dist/marks/tree.d.ts +14 -6
- package/dist/marks/tree.js +6 -3
- package/dist/marks/treemap.d.ts +5 -0
- package/dist/marks/treemap.js +7 -2
- package/dist/paint.d.ts +6 -1
- package/dist/paint.js +6 -1
- package/dist/scale.d.ts +1 -1
- package/dist/scale.js +4 -3
- package/dist/scales/band.d.ts +3 -0
- package/dist/scales/band.js +5 -1
- package/dist/scales/bare.d.ts +4 -1
- package/dist/scales/bare.js +4 -1
- package/dist/scales/color.d.ts +2 -0
- package/dist/scales/log.d.ts +8 -1
- package/dist/scales/log.js +3 -2
- package/dist/scales/time.d.ts +7 -0
- package/dist/scales/time.js +3 -1
- package/dist/text.d.ts +8 -0
- package/dist/text.js +33 -0
- package/dist/transforms.d.ts +11 -7
- package/dist/transforms.js +8 -7
- package/dist/types.d.ts +44 -15
- package/package.json +2 -2
|
@@ -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);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Theme } from '../theme.js';
|
|
2
2
|
import type { Chart, Feature, Options } from '../types.js';
|
|
3
3
|
import { type Source } from './source.js';
|
|
4
|
-
import type { Base } from './types.js';
|
|
5
|
-
export type ControllerProps = Base & {
|
|
4
|
+
import type { Axes, Base } from './types.js';
|
|
5
|
+
export type ControllerProps = Base & Pick<Axes, 'view' | 'onViewChange'> & {
|
|
6
6
|
data?: unknown;
|
|
7
7
|
};
|
|
8
8
|
export type Context = {
|
|
@@ -60,9 +60,9 @@ export function create(el, initial, build) {
|
|
|
60
60
|
const windowed = {};
|
|
61
61
|
const remember = (v) => {
|
|
62
62
|
const whole = internals.domain();
|
|
63
|
-
for (const k of ['x', 'y']) {
|
|
63
|
+
for (const k of ['x', 'y', 'y2']) {
|
|
64
64
|
const side = v[k];
|
|
65
|
-
const all = whole[k]
|
|
65
|
+
const all = whole[k]?.map(e => internals.real(k, e));
|
|
66
66
|
if (side && String(side) !== String(all))
|
|
67
67
|
windowed[k] = [...side];
|
|
68
68
|
else
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { type Theme } from '../theme.js';
|
|
2
|
-
import type { Col } from '../types.js';
|
|
2
|
+
import type { Col, Span } from '../types.js';
|
|
3
|
+
import type { Kind } from './source.js';
|
|
4
|
+
export declare const DAY = 86400000;
|
|
3
5
|
export declare const formatNumber: (v: number) => string;
|
|
4
|
-
export declare function timeTitle(columns: readonly Col[], utc: boolean): (t: 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);
|
|
5
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
|
+
};
|
|
6
14
|
export type ThemeProp = 'auto' | 'light' | 'dark' | Theme | undefined;
|
|
7
15
|
/** 'auto' reads the element's computed color-scheme; onChange fires when a 'light dark' page flips. */
|
|
8
16
|
export declare function themeOf(el: Element, prop: ThemeProp, onChange: (theme: Theme) => void): {
|
package/dist/charts/defaults.js
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
import { palette, paletteDark } from '../paint.js';
|
|
2
2
|
import { dark, light } from '../theme.js';
|
|
3
|
-
const DAY = 864e5;
|
|
3
|
+
export const DAY = 864e5;
|
|
4
4
|
const numbers = new Intl.NumberFormat('en-US', { maximumFractionDigits: 2 });
|
|
5
5
|
export const formatNumber = (v) => numbers.format(v);
|
|
6
6
|
const pad = (n, w = 2) => String(n).padStart(w, '0');
|
|
7
|
-
export function timeTitle(columns, utc) {
|
|
7
|
+
export function timeTitle(columns, utc, distinct = false) {
|
|
8
8
|
const parts = (t) => {
|
|
9
9
|
const d = new Date(t);
|
|
10
10
|
return utc
|
|
11
|
-
? [
|
|
12
|
-
|
|
11
|
+
? [
|
|
12
|
+
d.getUTCFullYear(),
|
|
13
|
+
d.getUTCMonth() + 1,
|
|
14
|
+
d.getUTCDate(),
|
|
15
|
+
d.getUTCHours(),
|
|
16
|
+
d.getUTCMinutes(),
|
|
17
|
+
d.getUTCSeconds(),
|
|
18
|
+
d.getUTCMilliseconds(),
|
|
19
|
+
]
|
|
20
|
+
: [
|
|
21
|
+
d.getFullYear(),
|
|
22
|
+
d.getMonth() + 1,
|
|
23
|
+
d.getDate(),
|
|
24
|
+
d.getHours(),
|
|
25
|
+
d.getMinutes(),
|
|
26
|
+
d.getSeconds(),
|
|
27
|
+
d.getMilliseconds(),
|
|
28
|
+
];
|
|
13
29
|
};
|
|
14
30
|
const midnight = (t) => {
|
|
15
31
|
const d = new Date(t);
|
|
@@ -23,13 +39,58 @@ export function timeTitle(columns, utc) {
|
|
|
23
39
|
if (Number.isFinite(xs[i]))
|
|
24
40
|
dateOnly = midnight(xs[i]);
|
|
25
41
|
}
|
|
42
|
+
const times = new Set();
|
|
43
|
+
if (distinct && !(dateOnly && utc))
|
|
44
|
+
for (const xs of columns)
|
|
45
|
+
for (let i = 0; i < xs.length; i++)
|
|
46
|
+
if (Number.isFinite(xs[i]))
|
|
47
|
+
times.add(xs[i]);
|
|
48
|
+
const collide = (unit) => new Set(Array.from(times, t => Math.floor(t / unit))).size < times.size;
|
|
49
|
+
const width = !collide(6e4) ? 5 : !collide(1e3) ? 8 : 12;
|
|
50
|
+
const unit = dateOnly ? DAY : width === 5 ? 6e4 : width === 8 ? 1e3 : 1;
|
|
51
|
+
const walls = new Map();
|
|
52
|
+
let zoned = false;
|
|
53
|
+
if (!utc)
|
|
54
|
+
for (const t of times) {
|
|
55
|
+
const offset = new Date(t).getTimezoneOffset();
|
|
56
|
+
const wall = Math.floor((t - offset * 6e4) / unit);
|
|
57
|
+
zoned ||= (walls.get(wall) ?? offset) !== offset;
|
|
58
|
+
walls.set(wall, offset);
|
|
59
|
+
}
|
|
60
|
+
const zone = (t) => {
|
|
61
|
+
if (!zoned)
|
|
62
|
+
return '';
|
|
63
|
+
const offset = -new Date(t).getTimezoneOffset();
|
|
64
|
+
const size = Math.abs(offset);
|
|
65
|
+
return ` UTC${offset < 0 ? '-' : '+'}${pad(Math.floor(size / 60))}:${pad(Math.floor(size % 60))}`;
|
|
66
|
+
};
|
|
26
67
|
return t => {
|
|
27
|
-
const [y, mo, day, h, mi] = parts(t);
|
|
68
|
+
const [y, mo, day, h, mi, s, ms] = parts(t);
|
|
28
69
|
const date = `${y < 0 ? '-' : ''}${pad(Math.abs(y), 4)}-${pad(mo)}-${pad(day)}`;
|
|
29
|
-
|
|
70
|
+
const clock = dateOnly ? '' : ` ${`${pad(h)}:${pad(mi)}:${pad(s)}.${pad(ms, 3)}`.slice(0, width)}`;
|
|
71
|
+
return `${date}${clock}${zone(t)}`;
|
|
30
72
|
};
|
|
31
73
|
}
|
|
74
|
+
export const namesOf = (kind, categories, columns, utc) => kind === 'text' ? v => categories[v] : kind === 'time' ? timeTitle(columns, utc, true) : String;
|
|
32
75
|
export const bandTitle = (categories, x) => categories[Math.min(Math.max(Math.round(x), 0), categories.length - 1)];
|
|
76
|
+
/** 0.6 of the smallest gap between distinct x, or of fallback where there is none. */
|
|
77
|
+
export function spacing(x, fallback) {
|
|
78
|
+
const at = Float64Array.from(x).sort();
|
|
79
|
+
let gap = Infinity;
|
|
80
|
+
for (let i = 1; i < at.length; i++) {
|
|
81
|
+
const d = at[i] - at[i - 1];
|
|
82
|
+
if (d > 0 && d < gap)
|
|
83
|
+
gap = d;
|
|
84
|
+
}
|
|
85
|
+
return gap < Infinity
|
|
86
|
+
? { width: gap * 0.6 }
|
|
87
|
+
: {
|
|
88
|
+
width: fallback * 0.6,
|
|
89
|
+
// A candle or bar alone would otherwise fill the axis, whose extent is its width.
|
|
90
|
+
...(at.length &&
|
|
91
|
+
Number.isFinite(at[0]) && { lone: [at[0] - fallback / 2, at[0] + fallback / 2] }),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
33
94
|
/** 'auto' reads the element's computed color-scheme; onChange fires when a 'light dark' page flips. */
|
|
34
95
|
export function themeOf(el, prop, onChange) {
|
|
35
96
|
const none = () => { };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BoxPlotProps, HistogramProps } from './types.js';
|
|
2
|
+
export declare const histogramChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: HistogramProps<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<HistogramProps<D_1>> & {
|
|
4
|
+
data?: D_1 | null | undefined;
|
|
5
|
+
}): void;
|
|
6
|
+
} : import("./controller.js").EasyChart<HistogramProps<D>>) & {
|
|
7
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: HistogramProps<D>) => import("../types.js").Options;
|
|
8
|
+
};
|
|
9
|
+
export declare const boxPlotChart: (<D extends import("./types.js").Input | undefined = undefined>(el: HTMLElement, props: BoxPlotProps<D>) => [D] extends [readonly never[] | undefined] ? Omit<import("./controller.js").EasyChart<never>, "update"> & {
|
|
10
|
+
update<D_1 extends import("./types.js").Input | undefined = undefined>(props: Partial<BoxPlotProps<D_1>> & {
|
|
11
|
+
data?: D_1 | null | undefined;
|
|
12
|
+
}): void;
|
|
13
|
+
} : import("./controller.js").EasyChart<BoxPlotProps<D>>) & {
|
|
14
|
+
spec: <D extends import("./types.js").Input | undefined = undefined>(props: BoxPlotProps<D>) => import("../types.js").Options;
|
|
15
|
+
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { bar } from '../marks/bar.js';
|
|
2
|
+
import { box } from '../marks/box.js';
|
|
3
|
+
import { band } from '../scales/band.js';
|
|
4
|
+
import { bin, fiveNumbers } from '../transforms.js';
|
|
5
|
+
import { defined, easyChart, featuresOf, given, install, linear, sizeOf } from './build.js';
|
|
6
|
+
import { colorsFor, formatNumber, namesOf } from './defaults.js';
|
|
7
|
+
import { reader } from './fields.js';
|
|
8
|
+
const HISTOGRAM = 'histogramChart';
|
|
9
|
+
const MAX_BINS = 1_000_000;
|
|
10
|
+
/** Two decimals at most, or as many as tell the edges of the narrowest bin apart, in full past the 20 all engines take. */
|
|
11
|
+
function edgeFormat({ x1, x2 }) {
|
|
12
|
+
let narrowest = Infinity;
|
|
13
|
+
for (let i = 0; i < x1.length; i++) {
|
|
14
|
+
const w = x2[i] - x1[i];
|
|
15
|
+
if (w > 0 && w < narrowest)
|
|
16
|
+
narrowest = w;
|
|
17
|
+
}
|
|
18
|
+
const digits = Math.max(Math.ceil(-Math.log10(narrowest)) + 1, 2);
|
|
19
|
+
if (digits > 20)
|
|
20
|
+
return String;
|
|
21
|
+
const f = new Intl.NumberFormat('en-US', { maximumFractionDigits: digits });
|
|
22
|
+
return (v) => f.format(v);
|
|
23
|
+
}
|
|
24
|
+
function count(s, named, by) {
|
|
25
|
+
const { fail, check, sole, numeric } = reader(s, HISTOGRAM);
|
|
26
|
+
if (!s.length)
|
|
27
|
+
return undefined;
|
|
28
|
+
if (s.shape === 'pairs')
|
|
29
|
+
fail('a list of pairs has no one list of values', 'numbers, or rows or columns with a field of numbers for x');
|
|
30
|
+
if (typeof by === 'number' && by > MAX_BINS)
|
|
31
|
+
throw new Error(`${HISTOGRAM}: ${formatNumber(by)} bins are over the limit of ${formatNumber(MAX_BINS)}`);
|
|
32
|
+
const given = s.shape ? 'value' : named;
|
|
33
|
+
const x = sole('x', given, check([['x', given]], ['x']));
|
|
34
|
+
const bins = bin(numeric(x, 'x').values, by);
|
|
35
|
+
return { label: typeof x === 'string' ? x : '', bins, edge: edgeFormat(bins) };
|
|
36
|
+
}
|
|
37
|
+
const buildHistogram = (p, { source: s, theme, memo }) => {
|
|
38
|
+
if (s.length)
|
|
39
|
+
for (const k of ['y', 'label', 'value'])
|
|
40
|
+
if (given(p, k))
|
|
41
|
+
throw new Error(`${HISTOGRAM}: use x for the values counted, not ${k}`);
|
|
42
|
+
const { bins: by } = p;
|
|
43
|
+
const picked = memo('fields', [s, p.x, typeof by, ...(typeof by === 'object' ? Array.from(by) : [by])], () => count(s, p.x, by));
|
|
44
|
+
const features = featuresOf(p, 1);
|
|
45
|
+
const text = { picked, x: p.xAxis?.format ?? picked?.edge, y: p.yAxis?.format ?? formatNumber };
|
|
46
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
47
|
+
ref.current = text;
|
|
48
|
+
return {
|
|
49
|
+
options: {
|
|
50
|
+
...sizeOf(p),
|
|
51
|
+
theme,
|
|
52
|
+
x: linear(p.xAxis, picked?.label ?? ''),
|
|
53
|
+
y: linear(p.yAxis, picked ? 'count' : ''),
|
|
54
|
+
marks: picked
|
|
55
|
+
? [
|
|
56
|
+
bar({
|
|
57
|
+
key: 'histogram',
|
|
58
|
+
label: 'count',
|
|
59
|
+
...picked.bins,
|
|
60
|
+
color: colorsFor(theme, p.colors, 1)[0],
|
|
61
|
+
gap: 1,
|
|
62
|
+
}),
|
|
63
|
+
]
|
|
64
|
+
: [],
|
|
65
|
+
},
|
|
66
|
+
features,
|
|
67
|
+
use: () => {
|
|
68
|
+
ref.current = text;
|
|
69
|
+
return install(features, {
|
|
70
|
+
mode: 'nearest',
|
|
71
|
+
text: (label, _x, v, row) => {
|
|
72
|
+
const { picked: shown, x, y } = ref.current;
|
|
73
|
+
const { x1, x2 } = shown.bins;
|
|
74
|
+
return `${x(x1[row])} – ${x(x2[row])} · ${label}: ${y(v)}`;
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const BOX_PLOT = 'boxPlotChart';
|
|
81
|
+
function boxes(s, props) {
|
|
82
|
+
const { kinds, fail, check, sole, read, numeric } = reader(s, BOX_PLOT);
|
|
83
|
+
if (!s.length)
|
|
84
|
+
return undefined;
|
|
85
|
+
const p = s.shape === 'numbers' ? { y: 'value' } : s.shape === 'pairs' ? { x: 'x', y: 'y' } : props;
|
|
86
|
+
const pool = check([
|
|
87
|
+
['x', p.x ?? undefined],
|
|
88
|
+
['y', p.y],
|
|
89
|
+
], ['y']);
|
|
90
|
+
const y = sole('y', p.y, pool);
|
|
91
|
+
const { values } = numeric(y, 'y');
|
|
92
|
+
let x = p.x;
|
|
93
|
+
if (x === undefined) {
|
|
94
|
+
const texts = pool.filter(f => kinds.get(f) === 'text');
|
|
95
|
+
const pass = 'or x: null for one box';
|
|
96
|
+
if (texts.length > 1)
|
|
97
|
+
fail(`${texts.map(f => `"${f}"`).join(', ')} are all fields of text, so x cannot be guessed`, `x as one of them, ${pass}`);
|
|
98
|
+
x = texts[0];
|
|
99
|
+
if (x !== undefined) {
|
|
100
|
+
const c = read(x, '', 'x');
|
|
101
|
+
if (c.categories.length === c.values.length - Array.from(c.values).filter(Number.isNaN).length)
|
|
102
|
+
fail(`no value of "${x}" repeats, so every box would hold one row`, `x as the field of groups, ${pass}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const groups = new Map();
|
|
106
|
+
let categories = [''];
|
|
107
|
+
if (x != null) {
|
|
108
|
+
const by = read(x, 'the accessor x', 'x');
|
|
109
|
+
for (let i = 0; i < s.length; i++) {
|
|
110
|
+
const g = by.values[i];
|
|
111
|
+
if (!Number.isFinite(g))
|
|
112
|
+
continue;
|
|
113
|
+
const group = groups.get(g);
|
|
114
|
+
if (group)
|
|
115
|
+
group.push(values[i]);
|
|
116
|
+
else
|
|
117
|
+
groups.set(g, [values[i]]);
|
|
118
|
+
}
|
|
119
|
+
const name = namesOf(by.kind, by.categories, [by.values], true);
|
|
120
|
+
categories = Array.from(groups.keys(), name);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
x: typeof x === 'string' ? x : '',
|
|
124
|
+
y: typeof y === 'string' ? y : '',
|
|
125
|
+
label: typeof y === 'string' ? y : 'y',
|
|
126
|
+
categories,
|
|
127
|
+
at: Float64Array.from(categories, (_, i) => i),
|
|
128
|
+
five: fiveNumbers(x == null ? [values] : [...groups.values()]),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const buildBoxPlot = (p, { source: s, theme, memo }) => {
|
|
132
|
+
if (s.length) {
|
|
133
|
+
if (given(p, 'label') || given(p, 'value'))
|
|
134
|
+
throw new Error(`${BOX_PLOT}: use x and y, not label and value`);
|
|
135
|
+
if (given(p, 'zoom'))
|
|
136
|
+
throw new Error(`${BOX_PLOT}: zoom does not apply to boxes`);
|
|
137
|
+
}
|
|
138
|
+
const picked = memo('fields', [s, p.x, p.y], () => boxes(s, p));
|
|
139
|
+
const features = featuresOf({ ...p, zoom: false }, 1);
|
|
140
|
+
const text = { picked, value: p.yAxis?.format ?? formatNumber };
|
|
141
|
+
const ref = memo('tooltip', [], () => ({}));
|
|
142
|
+
ref.current = text;
|
|
143
|
+
return {
|
|
144
|
+
options: {
|
|
145
|
+
...sizeOf(p),
|
|
146
|
+
theme,
|
|
147
|
+
x: band({ label: p.xAxis?.label ?? picked?.x ?? '', categories: picked?.categories ?? [] }),
|
|
148
|
+
y: linear(p.yAxis, picked?.y ?? ''),
|
|
149
|
+
marks: picked
|
|
150
|
+
? [
|
|
151
|
+
box(defined({
|
|
152
|
+
key: 'box',
|
|
153
|
+
label: picked.label,
|
|
154
|
+
x: picked.at,
|
|
155
|
+
...picked.five,
|
|
156
|
+
color: colorsFor(theme, p.colors, 1)[0],
|
|
157
|
+
fill: p.fill,
|
|
158
|
+
})),
|
|
159
|
+
]
|
|
160
|
+
: [],
|
|
161
|
+
},
|
|
162
|
+
features,
|
|
163
|
+
use: () => {
|
|
164
|
+
ref.current = text;
|
|
165
|
+
return install(features, {
|
|
166
|
+
mode: 'nearest',
|
|
167
|
+
text: (label, _x, _y, row) => {
|
|
168
|
+
const { picked: shown, value } = ref.current;
|
|
169
|
+
const group = shown.categories[row];
|
|
170
|
+
const numbers = Object.entries(shown.five).map(([k, v]) => `${k} ${value(v[row])}`);
|
|
171
|
+
return `${group ? `${group} · ` : ''}${label}: ${numbers.join(', ')}`;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
export const histogramChart = easyChart(buildHistogram);
|
|
178
|
+
export const boxPlotChart = easyChart(buildBoxPlot);
|
package/dist/charts/fields.d.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import type { Col } from '../types.js';
|
|
2
|
-
import type { Accessor, Kind, Source } from './source.js';
|
|
3
|
-
type Field<R> = string | Accessor<R>;
|
|
2
|
+
import type { Accessor, Column, Kind, Source } from './source.js';
|
|
3
|
+
export type Field<R> = string | Accessor<R>;
|
|
4
4
|
export type FieldProps<R> = {
|
|
5
5
|
x?: Field<R>;
|
|
6
6
|
y?: Field<R> | readonly Field<R>[];
|
|
7
|
+
y2?: Field<R> | readonly Field<R>[];
|
|
7
8
|
series?: Field<R>;
|
|
8
9
|
r?: number | Field<R>;
|
|
10
|
+
errorBars?: {
|
|
11
|
+
low: Field<R>;
|
|
12
|
+
high: Field<R>;
|
|
13
|
+
axis?: 'x' | 'y';
|
|
14
|
+
};
|
|
9
15
|
xAxis?: {
|
|
10
16
|
utc?: boolean;
|
|
11
17
|
};
|
|
18
|
+
/** The prop each field of y and then of y2 came from, where it is not those; nothing is then guessed for y. */
|
|
19
|
+
roles?: readonly string[];
|
|
12
20
|
};
|
|
13
21
|
export type Family = 'xy' | 'scatter';
|
|
14
22
|
export type Series = {
|
|
@@ -16,6 +24,9 @@ export type Series = {
|
|
|
16
24
|
x: Col;
|
|
17
25
|
y: Col;
|
|
18
26
|
r?: Col;
|
|
27
|
+
low?: Col;
|
|
28
|
+
high?: Col;
|
|
29
|
+
yAxis?: 'y2';
|
|
19
30
|
};
|
|
20
31
|
export type Assigned = {
|
|
21
32
|
x: {
|
|
@@ -25,10 +36,29 @@ export type Assigned = {
|
|
|
25
36
|
};
|
|
26
37
|
y: {
|
|
27
38
|
label: string;
|
|
39
|
+
categories?: string[];
|
|
40
|
+
};
|
|
41
|
+
y2?: {
|
|
42
|
+
label: string;
|
|
28
43
|
};
|
|
29
44
|
ys: Series[];
|
|
45
|
+
/** The error bars run along x: low and high are values on the x axis. */
|
|
46
|
+
lying?: true;
|
|
47
|
+
/** Without series, where some row has no place on x: the rows of the data that the rows of ys are. */
|
|
48
|
+
rows?: number[];
|
|
30
49
|
/** With series and firstSeen: the distinct x across all series in order of first appearance. */
|
|
31
50
|
order?: number[];
|
|
32
51
|
};
|
|
52
|
+
export declare const list: <T>(f: T | readonly T[] | undefined) => readonly T[];
|
|
53
|
+
export declare function reader<R>(s: Source<R>, name: string): {
|
|
54
|
+
kinds: Map<string, Kind | undefined>;
|
|
55
|
+
fail: (why: string, pass: string, cause?: unknown) => never;
|
|
56
|
+
check: (roles: readonly (readonly [string, Field<R> | undefined])[], numberRoles: readonly string[]) => string[];
|
|
57
|
+
noNumbers: (role: string) => never;
|
|
58
|
+
sole: (role: string, named: Field<R> | undefined, pool: readonly string[]) => Field<R>;
|
|
59
|
+
read: (f: Field<R>, what: string, role: string, zone?: {
|
|
60
|
+
utc: boolean;
|
|
61
|
+
}) => Column;
|
|
62
|
+
numeric: (f: Field<R>, role: string, nth?: string, orText?: boolean) => Column;
|
|
63
|
+
};
|
|
33
64
|
export declare function assign<R>(s: Source<R>, props: FieldProps<R>, family: Family, name: string, firstSeen?: boolean): Assigned;
|
|
34
|
-
export {};
|