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,27 @@
|
|
|
1
|
+
import type { Axis, Col, Span } from '../types.js';
|
|
2
|
+
export type LogAxis = Axis & {
|
|
3
|
+
/** The logarithm's base. */
|
|
4
|
+
base: number;
|
|
5
|
+
/** A real value's logarithm, the space the marks' columns are in; one at or below zero becomes NaN. */
|
|
6
|
+
to(real: number, axis: Axis): number;
|
|
7
|
+
/** The inverse of `to`: the base raised to a value in the marks' space. */
|
|
8
|
+
from(v: number, axis: Axis): number;
|
|
9
|
+
/** The values in the axis's space, to give a mark; one at or below zero becomes NaN, a gap. */
|
|
10
|
+
col(values: readonly number[]): number[];
|
|
11
|
+
col(values: ArrayBufferView & Col): Float64Array;
|
|
12
|
+
col(values: Col): number[] | Float64Array;
|
|
13
|
+
};
|
|
14
|
+
export type LogOptions = {
|
|
15
|
+
/** The axis title; an empty string shows none. */
|
|
16
|
+
label: string;
|
|
17
|
+
/** The logarithm's base: above zero, finite and not 1. @default 10 */
|
|
18
|
+
base?: number;
|
|
19
|
+
/** Real values, both above zero; 'nice' widens the extent of the data to whole powers of the base. @default 'nice' */
|
|
20
|
+
domain?: Span | 'nice' | 'data';
|
|
21
|
+
/** The text of each tick, from its real value. @default String */
|
|
22
|
+
format?: Axis['format'];
|
|
23
|
+
/** Which marks the extent is taken from, as on any axis. @default 'shown' */
|
|
24
|
+
fit?: Axis['fit'];
|
|
25
|
+
};
|
|
26
|
+
/** An axis whose marks are given columns in log space: `y.col(values)`, or `y.to(v, y)` for one value. */
|
|
27
|
+
export declare function log({ label, base, domain, format, fit }: LogOptions): LogAxis;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { fits, scale as linear, spaced } from '../scale.js';
|
|
2
|
+
import { widest as widestOf } from '../text.js';
|
|
3
|
+
// log10 and log2 are exact at their own powers; another base's division can land a hair off one.
|
|
4
|
+
const lg = (v, base) => {
|
|
5
|
+
if (base === 10)
|
|
6
|
+
return Math.log10(v);
|
|
7
|
+
if (base === 2)
|
|
8
|
+
return Math.log2(v);
|
|
9
|
+
const r = Math.log(v) / Math.log(base);
|
|
10
|
+
return base ** Math.round(r) === v ? Math.round(r) : r;
|
|
11
|
+
};
|
|
12
|
+
const baseOf = (axis) => axis.base;
|
|
13
|
+
const to = (v, axis) => (v > 0 ? lg(v, baseOf(axis)) : NaN);
|
|
14
|
+
const from = (v, axis) => baseOf(axis) ** v;
|
|
15
|
+
function col(values) {
|
|
16
|
+
return Array.isArray(values) ? values.map(v => to(v, this)) : Float64Array.from(values, v => to(v, this));
|
|
17
|
+
}
|
|
18
|
+
function resolve(axis, extent, room) {
|
|
19
|
+
const base = baseOf(axis);
|
|
20
|
+
const format = axis.format ?? String;
|
|
21
|
+
let [lo, hi] = typeof axis.domain === 'string' ? extent() : axis.domain;
|
|
22
|
+
if (lo === hi)
|
|
23
|
+
[lo, hi] = [lo - 0.5, hi + 0.5];
|
|
24
|
+
const up = lo <= hi;
|
|
25
|
+
if (axis.domain === 'nice')
|
|
26
|
+
[lo, hi] = up ? [Math.floor(lo), Math.ceil(hi)] : [Math.ceil(lo), Math.floor(hi)];
|
|
27
|
+
// A base below 1, or a reversed domain, runs downward in log space; the ticks are found over it upward.
|
|
28
|
+
const [a, b] = up ? [lo, hi] : [hi, lo];
|
|
29
|
+
const tick = (k, times = 1) => {
|
|
30
|
+
const v = +(times * base ** k).toPrecision(15);
|
|
31
|
+
return { value: times === 1 ? k : lg(v, base), label: format(v) };
|
|
32
|
+
};
|
|
33
|
+
const powers = [];
|
|
34
|
+
const all = [];
|
|
35
|
+
const stride = Math.max(1, Math.ceil((b - a) / 3000));
|
|
36
|
+
const first = Math.floor(a + 1e-9);
|
|
37
|
+
const count = Math.min(3002, Math.floor((b + 1e-9 - first) / stride) + 1);
|
|
38
|
+
for (let i = 0, last = NaN; i < count; i++) {
|
|
39
|
+
const k = first + i * stride;
|
|
40
|
+
if (k === last)
|
|
41
|
+
continue;
|
|
42
|
+
last = k;
|
|
43
|
+
for (const times of base === 10 && stride === 1 ? [1, 2, 5] : [1]) {
|
|
44
|
+
const t = tick(k, times);
|
|
45
|
+
if (t.value < a - 1e-9 || t.value > b + 1e-9)
|
|
46
|
+
continue;
|
|
47
|
+
all.push(t);
|
|
48
|
+
if (times === 1)
|
|
49
|
+
powers.push(t);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const widest = (ts) => widestOf(ts.map(t => t.label));
|
|
53
|
+
const ordered = (ticks) => ({
|
|
54
|
+
lo,
|
|
55
|
+
hi,
|
|
56
|
+
ticks: ticks.sort((p, q) => (up ? p.value - q.value : q.value - p.value)),
|
|
57
|
+
});
|
|
58
|
+
if (room && all.length > powers.length && all.length > 1 && fits(all.length, widest(all), room))
|
|
59
|
+
return ordered(all);
|
|
60
|
+
if (powers.length > 1)
|
|
61
|
+
return ordered(spaced(powers, widest(powers), room));
|
|
62
|
+
// A window inside one power of the base offers its power, its 1-2-5 ticks, then the linear ticks of its
|
|
63
|
+
// real values, and keeps each whose label clears those already kept where they sit in log space.
|
|
64
|
+
const real = linear([Math.min(base ** a, base ** b), Math.max(base ** a, base ** b)], [0, 1], undefined, axis);
|
|
65
|
+
const offered = [
|
|
66
|
+
...powers,
|
|
67
|
+
...all,
|
|
68
|
+
...real.ticks.flatMap(v => (v > 0 ? [{ value: lg(v, base), label: format(v) }] : [])),
|
|
69
|
+
];
|
|
70
|
+
const gap = room
|
|
71
|
+
? ((room.horizontal ? widest(offered) + 16 : 22) / Math.max(room.length, 1)) * (b - a)
|
|
72
|
+
: 1e-9;
|
|
73
|
+
const kept = [];
|
|
74
|
+
for (const t of offered)
|
|
75
|
+
if (kept.every(k => Math.abs(k.value - t.value) >= gap))
|
|
76
|
+
kept.push(t);
|
|
77
|
+
return ordered(kept);
|
|
78
|
+
}
|
|
79
|
+
/** An axis whose marks are given columns in log space: `y.col(values)`, or `y.to(v, y)` for one value. */
|
|
80
|
+
export function log({ label, base = 10, domain = 'nice', format, fit }) {
|
|
81
|
+
if (!(base > 0 && base !== 1 && Number.isFinite(base)))
|
|
82
|
+
throw new RangeError(`log() base ${base} must be above zero and not 1`);
|
|
83
|
+
if (typeof domain !== 'string')
|
|
84
|
+
for (const bound of domain)
|
|
85
|
+
if (!(bound > 0 && bound < Infinity))
|
|
86
|
+
throw new RangeError(`log() domain bound ${bound} must be above zero`);
|
|
87
|
+
return {
|
|
88
|
+
domain: typeof domain === 'string' ? domain : [lg(domain[0], base), lg(domain[1], base)],
|
|
89
|
+
label,
|
|
90
|
+
base,
|
|
91
|
+
format,
|
|
92
|
+
fit,
|
|
93
|
+
to,
|
|
94
|
+
from,
|
|
95
|
+
col: col,
|
|
96
|
+
scale: resolve,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Axis } from '../types.js';
|
|
2
|
+
declare const UNITS: readonly ['year', 'month', 'day', 'hour', 'minute', 'second'];
|
|
3
|
+
export type TimeAxis = Axis & {
|
|
4
|
+
/** Whether tick boundaries and labels are in UTC rather than the browser's zone. */
|
|
5
|
+
utc: boolean;
|
|
6
|
+
/** Ticks every this many units. */
|
|
7
|
+
every?: number;
|
|
8
|
+
/** The unit whose boundaries the ticks fall on. */
|
|
9
|
+
unit?: (typeof UNITS)[number];
|
|
10
|
+
};
|
|
11
|
+
export type TimeOptions = {
|
|
12
|
+
/** The axis title; an empty string shows none. */
|
|
13
|
+
label: string;
|
|
14
|
+
/** Millisecond timestamps; the extent of the data when omitted. */
|
|
15
|
+
domain?: [number, number];
|
|
16
|
+
/** Tick boundaries and labels in UTC (the default) or in the browser's zone. */
|
|
17
|
+
utc?: boolean;
|
|
18
|
+
/** The text of each tick, from its timestamp; English month names and a 24-hour clock when left out. */
|
|
19
|
+
format?: Axis['format'];
|
|
20
|
+
/** Which marks the extent is taken from, as on any axis. @default 'shown' */
|
|
21
|
+
fit?: Axis['fit'];
|
|
22
|
+
} & ({
|
|
23
|
+
/**
|
|
24
|
+
* Ticks on the boundaries of this unit. Left out, the step is chosen for the span shown. A unit
|
|
25
|
+
* that would give 990 ticks or more across what is shown gives way to that chosen step.
|
|
26
|
+
*/
|
|
27
|
+
unit: (typeof UNITS)[number];
|
|
28
|
+
/** Ticks every this many units, counted from the start of the next larger unit; 1 when left out. */
|
|
29
|
+
every?: number;
|
|
30
|
+
} | {
|
|
31
|
+
unit?: undefined;
|
|
32
|
+
every?: undefined;
|
|
33
|
+
});
|
|
34
|
+
export declare const make: ([y, mo, d, h, mi, s]: number[], utc: boolean) => number;
|
|
35
|
+
/** An axis of millisecond timestamps, ticked on calendar boundaries and labelled as dates and times. */
|
|
36
|
+
export declare const time: ({ label, domain, utc, every, unit, format, fit }: TimeOptions) => TimeAxis;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { niceStep, spaced, thin } from '../scale.js';
|
|
2
|
+
import { estimated, textWidth, widest as widestOf } from '../text.js';
|
|
3
|
+
const UNITS = ['year', 'month', 'day', 'hour', 'minute', 'second'];
|
|
4
|
+
const MONTHS = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
|
|
5
|
+
const S = 1e3;
|
|
6
|
+
const MIN = 6e4;
|
|
7
|
+
const H = 36e5;
|
|
8
|
+
const D = 864e5;
|
|
9
|
+
const Y = 365 * D;
|
|
10
|
+
const UNIT_MS = [Y, 30 * D, D, H, MIN, S];
|
|
11
|
+
// [field, count, approximate ms], field indexing [year, month, day, hour, minute, second].
|
|
12
|
+
// ponytail: nothing finer than a second.
|
|
13
|
+
const STEPS = [
|
|
14
|
+
[5, 1, S],
|
|
15
|
+
[5, 5, 5 * S],
|
|
16
|
+
[5, 15, 15 * S],
|
|
17
|
+
[5, 30, 30 * S],
|
|
18
|
+
[4, 1, MIN],
|
|
19
|
+
[4, 5, 5 * MIN],
|
|
20
|
+
[4, 15, 15 * MIN],
|
|
21
|
+
[4, 30, 30 * MIN],
|
|
22
|
+
[3, 1, H],
|
|
23
|
+
[3, 3, 3 * H],
|
|
24
|
+
[3, 6, 6 * H],
|
|
25
|
+
[3, 12, 12 * H],
|
|
26
|
+
[2, 1, D],
|
|
27
|
+
[2, 2, 2 * D],
|
|
28
|
+
[2, 7, 7 * D],
|
|
29
|
+
[1, 1, 30 * D],
|
|
30
|
+
[1, 3, 91 * D],
|
|
31
|
+
[1, 6, 182 * D],
|
|
32
|
+
];
|
|
33
|
+
const read = (t, utc) => {
|
|
34
|
+
const d = new Date(t);
|
|
35
|
+
return utc
|
|
36
|
+
? [
|
|
37
|
+
d.getUTCFullYear(),
|
|
38
|
+
d.getUTCMonth(),
|
|
39
|
+
d.getUTCDate(),
|
|
40
|
+
d.getUTCHours(),
|
|
41
|
+
d.getUTCMinutes(),
|
|
42
|
+
d.getUTCSeconds(),
|
|
43
|
+
]
|
|
44
|
+
: [d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds()];
|
|
45
|
+
};
|
|
46
|
+
// Date.UTC and new Date(y, ...) read the years 0 to 99 as 1900 to 1999; the setters do not.
|
|
47
|
+
export const make = ([y, mo, d, h, mi, s], utc) => {
|
|
48
|
+
const t = new Date(0);
|
|
49
|
+
if (utc) {
|
|
50
|
+
t.setUTCFullYear(y, mo, d);
|
|
51
|
+
t.setUTCHours(h, mi, s, 0);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
t.setFullYear(y, mo, d);
|
|
55
|
+
t.setHours(h, mi, s, 0);
|
|
56
|
+
}
|
|
57
|
+
return t.getTime();
|
|
58
|
+
};
|
|
59
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
60
|
+
// English month names and a 24-hour clock unless the axis has a format.
|
|
61
|
+
function label([y, mo, d, h, mi, s], field) {
|
|
62
|
+
const day = `${MONTHS[mo]} ${d}`;
|
|
63
|
+
if (field === 0)
|
|
64
|
+
return String(y);
|
|
65
|
+
if (field === 1)
|
|
66
|
+
return mo ? MONTHS[mo] : String(y);
|
|
67
|
+
if (field === 2)
|
|
68
|
+
return day;
|
|
69
|
+
if (field === 5)
|
|
70
|
+
return `${pad(h)}:${pad(mi)}:${pad(s)}`;
|
|
71
|
+
return h || mi ? `${pad(h)}:${pad(mi)}` : day;
|
|
72
|
+
}
|
|
73
|
+
function resolve(axis, extent, room) {
|
|
74
|
+
const { lo, hi, ticks, fixed, keep } = stepped(axis, typeof axis.domain === 'string' ? extent() : axis.domain, room);
|
|
75
|
+
const widest = widestOf(ticks.map(t => t.label), axis.format ? textWidth : estimated);
|
|
76
|
+
return {
|
|
77
|
+
lo,
|
|
78
|
+
hi,
|
|
79
|
+
ticks: fixed ? spaced(ticks, widest, room, (t, _, k) => keep(t.value, k)) : thin(ticks, widest, room),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function stepped(axis, [lo, hi], room) {
|
|
83
|
+
if (lo === hi)
|
|
84
|
+
[lo, hi] = [lo - S, hi + S];
|
|
85
|
+
const span = hi - lo;
|
|
86
|
+
// A time label is about seven characters, so one fits every 64 px along x.
|
|
87
|
+
const max = room ? Math.max(1, Math.min(10, Math.floor(room.length / (room.horizontal ? 64 : 22)))) : 10;
|
|
88
|
+
const unit = UNITS.indexOf(axis.unit);
|
|
89
|
+
const n = Math.floor(axis.every ?? 1);
|
|
90
|
+
const every = Number.isSafeInteger(n) && n >= 1 ? n : 1;
|
|
91
|
+
// A unit too fine for the 1000 ticks counted below would leave ticks at the low end only.
|
|
92
|
+
const fixed = unit >= 0 && span / (every * UNIT_MS[unit]) < 990;
|
|
93
|
+
const [field, count] = fixed
|
|
94
|
+
? [unit, every]
|
|
95
|
+
: (STEPS.find(s => span / s[2] <= max) ?? [0, Math.max(1, niceStep(span / Y, max))]);
|
|
96
|
+
const text = (p, t) => axis.format?.(t) ?? label(p, field);
|
|
97
|
+
const { utc } = axis;
|
|
98
|
+
const ticks = [];
|
|
99
|
+
const from = field === 2 ? 1 : 0;
|
|
100
|
+
// Thinned to every k-th step: within each period of the next larger unit, the multiples of k at least k
|
|
101
|
+
// steps before the next period. When k steps outlast the shortest period, only the first of every m-th
|
|
102
|
+
// period counted from 1970, m periods being at least k steps. Either way the pick depends on the tick alone.
|
|
103
|
+
const shortest = [Infinity, 12, 28, 24, 60, 60][field];
|
|
104
|
+
const keep = (t, k) => {
|
|
105
|
+
if (k === 1)
|
|
106
|
+
return true;
|
|
107
|
+
const p = read(t, utc);
|
|
108
|
+
const index = (p[field] - from) / count;
|
|
109
|
+
if (k * count > shortest) {
|
|
110
|
+
const day = make([p[0], p[1], p[2], 0, 0, 0], true) / D;
|
|
111
|
+
const period = [0, p[0], p[0] * 12 + p[1], day, day * 24 + p[3], (day * 24 + p[3]) * 60 + p[4]][field];
|
|
112
|
+
return index === 0 && period % Math.ceil((k * count) / shortest) === 0;
|
|
113
|
+
}
|
|
114
|
+
const end = [Infinity, 12, read(make([p[0], p[1] + 1, 0, 0, 0, 0], utc), utc)[2] + 1, 24, 60, 60][field];
|
|
115
|
+
return index % k === 0 && end - p[field] >= k * count;
|
|
116
|
+
};
|
|
117
|
+
const floor = (p) => {
|
|
118
|
+
for (let i = field + 1; i < 6; i++)
|
|
119
|
+
p[i] = i === 2 ? 1 : 0;
|
|
120
|
+
p[field] = from + Math.floor((p[field] - from) / count) * count;
|
|
121
|
+
return p;
|
|
122
|
+
};
|
|
123
|
+
if (field > 2) {
|
|
124
|
+
// Fixed steps from a boundary of the local clock, so the hour a clock change repeats keeps both
|
|
125
|
+
// of its occurrences; a step that lands off a boundary has crossed a change and is realigned.
|
|
126
|
+
const step = count * [H, MIN, S][field - 3];
|
|
127
|
+
let t = make(floor(read(lo, utc)), utc);
|
|
128
|
+
if (lo - t >= step)
|
|
129
|
+
t += Math.floor((lo - t) / step) * step;
|
|
130
|
+
const aligned = (p) => p[field] % count === 0 && p.every((v, i) => i <= field || !v);
|
|
131
|
+
for (let n = 0; t <= hi && n < 1000; n++) {
|
|
132
|
+
// A boundary the clock skips comes back from make() moved off it, and is not a tick.
|
|
133
|
+
const here = read(t, utc);
|
|
134
|
+
if (t >= lo && aligned(here))
|
|
135
|
+
ticks.push({ value: t, label: text(here, t) });
|
|
136
|
+
const p = read(t + step, utc);
|
|
137
|
+
let next = aligned(p) ? t + step : make(floor(p), utc);
|
|
138
|
+
if (next <= t) {
|
|
139
|
+
p[field] += count;
|
|
140
|
+
next = make(p, utc);
|
|
141
|
+
}
|
|
142
|
+
t = next;
|
|
143
|
+
}
|
|
144
|
+
return { lo, hi, ticks, fixed, keep };
|
|
145
|
+
}
|
|
146
|
+
// Each step goes through Date, which is what keeps months and leap years right.
|
|
147
|
+
let p = floor(read(lo, utc));
|
|
148
|
+
if (field === 2 && count === 7 && !fixed) {
|
|
149
|
+
const d = new Date(make(p, utc));
|
|
150
|
+
p[2] -= ((utc ? d.getUTCDay() : d.getDay()) + 6) % 7;
|
|
151
|
+
p = read(make(p, utc), utc);
|
|
152
|
+
}
|
|
153
|
+
for (let t = make(p, utc), n = 0; t <= hi && n < 1000; n++) {
|
|
154
|
+
// Steps of days restart with each month and of months with each year, so they stay on the same
|
|
155
|
+
// days and months; a day closer than a step to the next month's first is dropped.
|
|
156
|
+
const monthly = field === 2 && (fixed || count !== 7);
|
|
157
|
+
const [year, month] = p;
|
|
158
|
+
const ahead = [...p];
|
|
159
|
+
ahead[field] += count;
|
|
160
|
+
const next = make(ahead, utc);
|
|
161
|
+
const first = monthly ? make([year, month + 1, 1, 0, 0, 0], utc) : Infinity;
|
|
162
|
+
if (t >= lo && !(p[2] !== 1 && next > first))
|
|
163
|
+
ticks.push({ value: t, label: text(p, t) });
|
|
164
|
+
p = read(Math.min(next, first), utc);
|
|
165
|
+
// Where a zone skips midnight the boundary lands at 01:00; the next one must not inherit that hour.
|
|
166
|
+
p[3] = p[4] = p[5] = 0;
|
|
167
|
+
if (field === 1 && p[0] !== year)
|
|
168
|
+
p[1] = 0;
|
|
169
|
+
t = make(p, utc);
|
|
170
|
+
}
|
|
171
|
+
return { lo, hi, ticks, fixed, keep };
|
|
172
|
+
}
|
|
173
|
+
/** An axis of millisecond timestamps, ticked on calendar boundaries and labelled as dates and times. */
|
|
174
|
+
export const time = ({ label, domain, utc = true, every, unit, format, fit }) => ({
|
|
175
|
+
domain: domain ?? 'data',
|
|
176
|
+
label,
|
|
177
|
+
utc,
|
|
178
|
+
every,
|
|
179
|
+
unit,
|
|
180
|
+
format,
|
|
181
|
+
fit,
|
|
182
|
+
scale: resolve,
|
|
183
|
+
});
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const FONT = "12px system-ui,sans-serif";
|
|
2
|
+
export declare const estimated: (s: string) => number;
|
|
3
|
+
/**
|
|
4
|
+
* A label's width in CSS pixels. Plain numbers are estimated, as the first measurement on a page costs
|
|
5
|
+
* about 3 ms (13 ms at 4x CPU slowdown, measured 2026-11-25), which a chart of numbers alone should not pay.
|
|
6
|
+
*/
|
|
7
|
+
export declare function textWidth(s: string): number;
|
|
8
|
+
export declare const widest: (labels: Iterable<string>, width?: typeof textWidth) => number;
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export const FONT = '12px system-ui,sans-serif';
|
|
2
|
+
const PLAIN = /^[\d\s.,:%+−-]*$/;
|
|
3
|
+
const widths = new Map();
|
|
4
|
+
let pen;
|
|
5
|
+
export const estimated = (s) => s.length * 7;
|
|
6
|
+
/**
|
|
7
|
+
* A label's width in CSS pixels. Plain numbers are estimated, as the first measurement on a page costs
|
|
8
|
+
* about 3 ms (13 ms at 4x CPU slowdown, measured 2026-11-25), which a chart of numbers alone should not pay.
|
|
9
|
+
*/
|
|
10
|
+
export function textWidth(s) {
|
|
11
|
+
if (PLAIN.test(s))
|
|
12
|
+
return estimated(s);
|
|
13
|
+
let w = widths.get(s);
|
|
14
|
+
if (w !== undefined)
|
|
15
|
+
return w;
|
|
16
|
+
if (pen === undefined) {
|
|
17
|
+
pen = typeof document === 'undefined' ? null : document.createElement('canvas').getContext('2d');
|
|
18
|
+
if (pen)
|
|
19
|
+
pen.font = FONT;
|
|
20
|
+
}
|
|
21
|
+
if (!pen)
|
|
22
|
+
return estimated(s);
|
|
23
|
+
if (widths.size >= 4096)
|
|
24
|
+
widths.clear();
|
|
25
|
+
widths.set(s, (w = pen.measureText(s).width));
|
|
26
|
+
return w;
|
|
27
|
+
}
|
|
28
|
+
export const widest = (labels, width = textWidth) => {
|
|
29
|
+
let w = 0;
|
|
30
|
+
for (const l of labels)
|
|
31
|
+
w = Math.max(w, width(l));
|
|
32
|
+
return w;
|
|
33
|
+
};
|
package/dist/theme.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The chart's own colours: text, tick marks and muted text, grid lines, and the tooltip's box. */
|
|
2
|
+
export type Theme = {
|
|
3
|
+
text: string;
|
|
4
|
+
muted: string;
|
|
5
|
+
grid: string;
|
|
6
|
+
surface: string;
|
|
7
|
+
border: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const light: Theme;
|
|
10
|
+
export declare const dark: Theme;
|
package/dist/theme.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Col } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Counts values per bin, as the bounds of one bar per bin. A bin holds its lower edge and the last bin
|
|
4
|
+
* also its upper one; NaN, infinite values and values outside the edges are not counted.
|
|
5
|
+
* @param values The values to count.
|
|
6
|
+
* @param by The bin edges in ascending order, or how many equal bins span the finite values, rounded and at least 1; Sturges' rule when left out.
|
|
7
|
+
*/
|
|
8
|
+
export declare function bin(values: Col, by?: number | Col): {
|
|
9
|
+
x1: Float64Array<ArrayBufferLike>;
|
|
10
|
+
x2: Float64Array<ArrayBufferLike>;
|
|
11
|
+
y1: Float64Array<ArrayBuffer>;
|
|
12
|
+
y2: Float64Array<ArrayBuffer>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Stacks series that share their x column, row by row: each gets the y1 and y2 of its layer.
|
|
16
|
+
* Negative values stack downwards from zero, apart from the positive ones; NaN counts as zero.
|
|
17
|
+
* @param series The y column of each layer, stacked in this order.
|
|
18
|
+
*/
|
|
19
|
+
export declare function stack(series: Col[]): {
|
|
20
|
+
y1: Float64Array<ArrayBuffer>;
|
|
21
|
+
y2: Float64Array<ArrayBuffer>;
|
|
22
|
+
}[];
|
|
23
|
+
/**
|
|
24
|
+
* The two bounds of one bar per category on a band axis, where category i sits at i.
|
|
25
|
+
* @param at A count for categories 0 to n - 1, or the indices themselves.
|
|
26
|
+
*/
|
|
27
|
+
export declare function bands(at: number | Col, { slot, of, width, }?: {
|
|
28
|
+
/** Which of the `of` slots in each category these bars take, for grouped bars. @default 0 */
|
|
29
|
+
slot?: number;
|
|
30
|
+
/** How many slots each category is split into. @default 1 */
|
|
31
|
+
of?: number;
|
|
32
|
+
/** The share of the category that the bars of all slots take together. @default 0.8 */
|
|
33
|
+
width?: number;
|
|
34
|
+
}): [Float64Array, Float64Array];
|
|
35
|
+
/**
|
|
36
|
+
* The five numbers a box plot draws, for each group of values: minimum, quartiles and maximum.
|
|
37
|
+
* Quartiles are interpolated between the two nearest values; NaN and infinite values are left out,
|
|
38
|
+
* and a group with nothing left gives NaN, which draws nothing.
|
|
39
|
+
* @param groups The values of each group, one box per group.
|
|
40
|
+
*/
|
|
41
|
+
export declare function fiveNumbers(groups: Col[]): {
|
|
42
|
+
min: Float64Array<ArrayBuffer>;
|
|
43
|
+
q1: Float64Array<ArrayBuffer>;
|
|
44
|
+
median: Float64Array<ArrayBuffer>;
|
|
45
|
+
q3: Float64Array<ArrayBuffer>;
|
|
46
|
+
max: Float64Array<ArrayBuffer>;
|
|
47
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Counts values per bin, as the bounds of one bar per bin. A bin holds its lower edge and the last bin
|
|
3
|
+
* also its upper one; NaN, infinite values and values outside the edges are not counted.
|
|
4
|
+
* @param values The values to count.
|
|
5
|
+
* @param by The bin edges in ascending order, or how many equal bins span the finite values, rounded and at least 1; Sturges' rule when left out.
|
|
6
|
+
*/
|
|
7
|
+
export function bin(values, by) {
|
|
8
|
+
let edges;
|
|
9
|
+
if (typeof by === 'object')
|
|
10
|
+
edges = Float64Array.from(by);
|
|
11
|
+
else {
|
|
12
|
+
let lo = Infinity;
|
|
13
|
+
let hi = -Infinity;
|
|
14
|
+
let finite = 0;
|
|
15
|
+
for (let i = 0; i < values.length; i++) {
|
|
16
|
+
const v = values[i];
|
|
17
|
+
if (!Number.isFinite(v))
|
|
18
|
+
continue;
|
|
19
|
+
finite++;
|
|
20
|
+
if (v < lo)
|
|
21
|
+
lo = v;
|
|
22
|
+
if (v > hi)
|
|
23
|
+
hi = v;
|
|
24
|
+
}
|
|
25
|
+
if (!finite)
|
|
26
|
+
[lo, hi] = [0, 1];
|
|
27
|
+
if (lo === hi) {
|
|
28
|
+
const pad = Math.abs(lo) / 2 || 0.5;
|
|
29
|
+
lo = Math.max(lo - pad, -Number.MAX_VALUE);
|
|
30
|
+
hi = Math.min(hi + pad, Number.MAX_VALUE);
|
|
31
|
+
}
|
|
32
|
+
const wanted = by ?? Math.ceil(Math.log2(finite || 1)) + 1;
|
|
33
|
+
const count = Number.isFinite(wanted) ? Math.max(1, Math.round(wanted)) : 1;
|
|
34
|
+
// Weighted, not lo + (hi - lo) * t: that overflows for a huge extent and can miss hi by a rounding step.
|
|
35
|
+
edges = Float64Array.from({ length: count + 1 }, (_, i) => lo * (1 - i / count) + hi * (i / count));
|
|
36
|
+
}
|
|
37
|
+
const n = Math.max(edges.length - 1, 0);
|
|
38
|
+
const y2 = new Float64Array(n);
|
|
39
|
+
for (let i = 0; i < values.length; i++) {
|
|
40
|
+
const v = values[i];
|
|
41
|
+
if (!Number.isFinite(v))
|
|
42
|
+
continue;
|
|
43
|
+
let a = 0;
|
|
44
|
+
let b = n + 1;
|
|
45
|
+
while (a < b) {
|
|
46
|
+
const mid = (a + b) >> 1;
|
|
47
|
+
if (edges[mid] <= v)
|
|
48
|
+
a = mid + 1;
|
|
49
|
+
else
|
|
50
|
+
b = mid;
|
|
51
|
+
}
|
|
52
|
+
const at = v === edges[n] ? n - 1 : a - 1;
|
|
53
|
+
if (at >= 0 && at < n)
|
|
54
|
+
y2[at]++;
|
|
55
|
+
}
|
|
56
|
+
return { x1: edges.subarray(0, n), x2: edges.subarray(1), y1: new Float64Array(n), y2 };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Stacks series that share their x column, row by row: each gets the y1 and y2 of its layer.
|
|
60
|
+
* Negative values stack downwards from zero, apart from the positive ones; NaN counts as zero.
|
|
61
|
+
* @param series The y column of each layer, stacked in this order.
|
|
62
|
+
*/
|
|
63
|
+
export function stack(series) {
|
|
64
|
+
const n = series[0]?.length ?? 0;
|
|
65
|
+
const up = new Float64Array(n);
|
|
66
|
+
const down = new Float64Array(n);
|
|
67
|
+
return series.map(s => {
|
|
68
|
+
const y1 = new Float64Array(n);
|
|
69
|
+
const y2 = new Float64Array(n);
|
|
70
|
+
for (let i = 0; i < n; i++) {
|
|
71
|
+
const v = s[i] || 0;
|
|
72
|
+
const base = v < 0 ? down : up;
|
|
73
|
+
y1[i] = base[i];
|
|
74
|
+
y2[i] = base[i] += v;
|
|
75
|
+
}
|
|
76
|
+
return { y1, y2 };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The two bounds of one bar per category on a band axis, where category i sits at i.
|
|
81
|
+
* @param at A count for categories 0 to n - 1, or the indices themselves.
|
|
82
|
+
*/
|
|
83
|
+
export function bands(at, { slot = 0, of = 1, width = 0.8, } = {}) {
|
|
84
|
+
const n = typeof at === 'number' ? at : at.length;
|
|
85
|
+
const lo = new Float64Array(n);
|
|
86
|
+
const hi = new Float64Array(n);
|
|
87
|
+
const each = width / of;
|
|
88
|
+
const offset = slot * each - width / 2;
|
|
89
|
+
for (let i = 0; i < n; i++) {
|
|
90
|
+
lo[i] = (typeof at === 'number' ? i : at[i]) + offset;
|
|
91
|
+
hi[i] = lo[i] + each;
|
|
92
|
+
}
|
|
93
|
+
return [lo, hi];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The five numbers a box plot draws, for each group of values: minimum, quartiles and maximum.
|
|
97
|
+
* Quartiles are interpolated between the two nearest values; NaN and infinite values are left out,
|
|
98
|
+
* and a group with nothing left gives NaN, which draws nothing.
|
|
99
|
+
* @param groups The values of each group, one box per group.
|
|
100
|
+
*/
|
|
101
|
+
export function fiveNumbers(groups) {
|
|
102
|
+
const column = () => new Float64Array(groups.length);
|
|
103
|
+
const [min, q1, median, q3, max] = [column(), column(), column(), column(), column()];
|
|
104
|
+
groups.forEach((group, g) => {
|
|
105
|
+
const v = Float64Array.from(group).filter(Number.isFinite).sort();
|
|
106
|
+
const quantile = (p) => {
|
|
107
|
+
const at = (v.length - 1) * p;
|
|
108
|
+
const [a, b, t] = [v[Math.floor(at)], v[Math.ceil(at)], at % 1];
|
|
109
|
+
// The difference overflows for huge values of opposite sign; the weighted sum does not.
|
|
110
|
+
return Number.isFinite(b - a) ? a + (b - a) * t : a * (1 - t) + b * t;
|
|
111
|
+
};
|
|
112
|
+
min[g] = v[0] ?? NaN;
|
|
113
|
+
q1[g] = quantile(0.25);
|
|
114
|
+
median[g] = quantile(0.5);
|
|
115
|
+
q3[g] = quantile(0.75);
|
|
116
|
+
max[g] = v.at(-1) ?? NaN;
|
|
117
|
+
});
|
|
118
|
+
return { min, q1, median, q3, max };
|
|
119
|
+
}
|