swiftplot 0.5.2 → 0.6.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 +8 -7
- package/dist/chart.d.ts +2 -1
- package/dist/chart.js +112 -16
- package/dist/charts/build.d.ts +2 -2
- package/dist/charts/build.js +24 -1
- package/dist/charts/candlestick.js +3 -0
- package/dist/charts/controller.d.ts +1 -1
- package/dist/charts/controller.js +14 -3
- package/dist/charts/defaults.d.ts +5 -1
- package/dist/charts/defaults.js +54 -20
- package/dist/charts/distribution.js +20 -5
- package/dist/charts/parts.js +1 -1
- package/dist/charts/types.d.ts +32 -7
- package/dist/charts/xy.d.ts +1 -0
- package/dist/charts/xy.js +81 -17
- package/dist/color.d.ts +5 -3
- package/dist/color.js +6 -4
- package/dist/features/brush.js +7 -2
- package/dist/features/pointer.d.ts +14 -1
- package/dist/features/pointer.js +50 -0
- package/dist/features/tooltip.d.ts +2 -2
- package/dist/features/tooltip.js +54 -14
- package/dist/features/zoom.d.ts +4 -0
- package/dist/features/zoom.js +11 -3
- package/dist/gl.d.ts +1 -1
- package/dist/gl.js +3 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/marks/arcs.d.ts +5 -0
- package/dist/marks/arcs.js +21 -14
- package/dist/scale.d.ts +4 -2
- package/dist/scale.js +30 -9
- package/dist/scales/band.js +2 -0
- package/dist/scales/bare.d.ts +2 -1
- package/dist/scales/bare.js +3 -3
- package/dist/scales/log.js +3 -1
- package/dist/scales/time.js +2 -1
- package/dist/schema.json +1 -0
- package/dist/transforms.d.ts +6 -0
- package/dist/transforms.js +13 -0
- package/dist/types.d.ts +14 -3
- package/package.json +4 -3
package/dist/features/zoom.d.ts
CHANGED
|
@@ -10,5 +10,9 @@ export type ZoomOptions = {
|
|
|
10
10
|
* is in use. 'pan' takes every mouse drag, so a brush() beside it gets none.
|
|
11
11
|
*/
|
|
12
12
|
drag?: 'pan' | 'shift' | false;
|
|
13
|
+
/** The narrowest x span the wheel, a pinch and `brush({ zoom })` reach, in axis units: milliseconds on a time axis. */
|
|
14
|
+
min?: number;
|
|
15
|
+
/** The widest x span the wheel and a pinch zoom out to, in axis units. A view already wider, such as the whole domain, is left as it is. */
|
|
16
|
+
max?: number;
|
|
13
17
|
};
|
|
14
18
|
export declare function zoom(z?: ZoomOptions): Feature;
|
package/dist/features/zoom.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { locate as where, settle as release, SLOP_PX, strayed, unselect } from './pointer.js';
|
|
1
|
+
import { limited, locate as where, narrowest, settle as release, SLOP_PX, strayed, unselect, } from './pointer.js';
|
|
2
2
|
// How far a drag that pans both axes must move up or down before it pans y: a hand does not move level.
|
|
3
3
|
const LEVEL_PX = 8;
|
|
4
4
|
const DOUBLE_TAP_MS = 300;
|
|
@@ -6,11 +6,16 @@ const DOUBLE_TAP_PX = 20;
|
|
|
6
6
|
const TAP_MS = 500;
|
|
7
7
|
export function zoom(z = {}) {
|
|
8
8
|
const sides = ['x', 'y'].filter(k => (z.axes ?? 'x').includes(k));
|
|
9
|
+
if (z.min !== undefined && z.max !== undefined && z.min > z.max)
|
|
10
|
+
throw new Error(`zoom() min ${z.min} is above max ${z.max}`);
|
|
9
11
|
return chart => {
|
|
10
12
|
const { root, drag, doc, win } = chart;
|
|
11
13
|
const locate = (e) => where(chart, e);
|
|
12
14
|
if (z.drag !== false)
|
|
13
15
|
drag.pan = z.drag === 'pan' ? 'plain' : 'shift';
|
|
16
|
+
if (z.min !== undefined && sides.includes('x'))
|
|
17
|
+
narrowest.set(chart, z.min);
|
|
18
|
+
const limit = (s, span, k, at, a) => s === 'x' ? limited(chart, span, k, z.min, z.max, at, a) : k;
|
|
14
19
|
let pending;
|
|
15
20
|
const shown = () => chart.fit(pending === undefined ? {} : pending);
|
|
16
21
|
let frame = 0;
|
|
@@ -35,11 +40,12 @@ export function zoom(z = {}) {
|
|
|
35
40
|
if (!over || !e.deltaY)
|
|
36
41
|
return;
|
|
37
42
|
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? chart.size().height : 1;
|
|
38
|
-
const
|
|
43
|
+
const zoomed = Math.exp(e.deltaY * unit * 0.002);
|
|
39
44
|
const base = shown();
|
|
40
45
|
const want = { ...pending };
|
|
41
46
|
for (const s of sides) {
|
|
42
47
|
const [lo, hi] = base[s];
|
|
48
|
+
const k = limit(s, base[s], zoomed, at[s]);
|
|
43
49
|
const a = lo + at[s] * (hi - lo);
|
|
44
50
|
const around = (t) => [a - (a - lo) * t, a + (hi - a) * t];
|
|
45
51
|
const step = around(k);
|
|
@@ -178,11 +184,12 @@ export function zoom(z = {}) {
|
|
|
178
184
|
if (!pinch)
|
|
179
185
|
return;
|
|
180
186
|
const { mid, d } = span(touches.get(pinch.a), touches.get(pinch.b));
|
|
181
|
-
const
|
|
187
|
+
const zoomed = d && pinch.d ? pinch.d / d : 1;
|
|
182
188
|
const want = {};
|
|
183
189
|
for (const s of sides) {
|
|
184
190
|
const [lo, hi] = pinch.view[s];
|
|
185
191
|
const under = lo + pinch.mid[s] * (hi - lo);
|
|
192
|
+
const k = limit(s, pinch.view[s], zoomed, mid[s], under);
|
|
186
193
|
const [flo, fhi] = chart.fit({ [s]: [lo, lo + (hi - lo) * k] })[s];
|
|
187
194
|
const width = fhi - flo;
|
|
188
195
|
const start = under - mid[s] * width;
|
|
@@ -237,6 +244,7 @@ export function zoom(z = {}) {
|
|
|
237
244
|
win.cancelAnimationFrame(frame);
|
|
238
245
|
win.clearTimeout(settling);
|
|
239
246
|
drag.pan = drag.active = false;
|
|
247
|
+
narrowest.delete(chart);
|
|
240
248
|
root.removeEventListener('wheel', wheel);
|
|
241
249
|
root.removeEventListener('pointerdown', down);
|
|
242
250
|
root.removeEventListener('dblclick', reset);
|
package/dist/gl.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { GlLayer } from './types.js';
|
|
2
2
|
export declare function program(gl: WebGL2RenderingContext, vertex: string, fragment: string): WebGLProgram;
|
|
3
3
|
export declare function glLayer(canvas: HTMLCanvasElement): GlLayer | null;
|
|
4
|
-
/** A colour for addColorStop, which takes no CSS variable
|
|
4
|
+
/** A colour for addColorStop, which takes no CSS variable or `light-dark()`. */
|
|
5
5
|
export declare function stop(ctx: CanvasRenderingContext2D, color: string): string;
|
|
6
6
|
/** A CSS colour as red, green, blue and opacity from 0 to 1. */
|
|
7
7
|
export declare function rgba(ctx: CanvasRenderingContext2D, color: string): number[];
|
package/dist/gl.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { placed } from './color.js';
|
|
1
2
|
export function program(gl, vertex, fragment) {
|
|
2
3
|
const p = gl.createProgram();
|
|
3
4
|
for (const [type, src] of [
|
|
@@ -38,9 +39,9 @@ export function glLayer(canvas) {
|
|
|
38
39
|
},
|
|
39
40
|
};
|
|
40
41
|
}
|
|
41
|
-
/** A colour for addColorStop, which takes no CSS variable
|
|
42
|
+
/** A colour for addColorStop, which takes no CSS variable or `light-dark()`. */
|
|
42
43
|
export function stop(ctx, color) {
|
|
43
|
-
if (!color
|
|
44
|
+
if (!placed(color))
|
|
44
45
|
return color;
|
|
45
46
|
ctx.fillStyle = color;
|
|
46
47
|
return ctx.fillStyle;
|
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export type { LogAxis, LogOptions } from './scales/log.js';
|
|
|
33
33
|
export { time } from './scales/time.js';
|
|
34
34
|
export { dark, light } from './theme.js';
|
|
35
35
|
export type { Theme } from './theme.js';
|
|
36
|
-
export { bands, bin, fiveNumbers, stack } from './transforms.js';
|
|
36
|
+
export { bands, bin, cumulate, fiveNumbers, stack } from './transforms.js';
|
|
37
37
|
export type { AnimateOptions } from './features/animate.js';
|
|
38
38
|
export type { BrushOptions } from './features/brush.js';
|
|
39
39
|
export type { LegendOptions } from './features/legend.js';
|
package/dist/index.js
CHANGED
|
@@ -29,4 +29,4 @@ export { sequential } from './scales/color.js';
|
|
|
29
29
|
export { log } from './scales/log.js';
|
|
30
30
|
export { time } from './scales/time.js';
|
|
31
31
|
export { dark, light } from './theme.js';
|
|
32
|
-
export { bands, bin, fiveNumbers, stack } from './transforms.js';
|
|
32
|
+
export { bands, bin, cumulate, fiveNumbers, stack } from './transforms.js';
|
package/dist/marks/arcs.d.ts
CHANGED
|
@@ -14,6 +14,11 @@ export type ArcsOptions = {
|
|
|
14
14
|
inner?: number;
|
|
15
15
|
/** Text on each slice that has room for it: true for its share as a percentage, or your own. */
|
|
16
16
|
text?: true | ((row: number, share: number) => string);
|
|
17
|
+
/**
|
|
18
|
+
* Text in the middle, or a function of the total of the values drawn. It is kept over any slice
|
|
19
|
+
* label it would cover, and with `inner` 0 it is drawn over the slices.
|
|
20
|
+
*/
|
|
21
|
+
center?: string | ((total: number) => string);
|
|
17
22
|
/**
|
|
18
23
|
* Each slice starts at this opacity and reaches its full colour at its end, clockwise. Where
|
|
19
24
|
* the browser has no conic gradient the slices stay solid.
|
package/dist/marks/arcs.js
CHANGED
|
@@ -11,9 +11,12 @@ export function arcs(o) {
|
|
|
11
11
|
let most = 0;
|
|
12
12
|
for (let i = 0; i < n; i++)
|
|
13
13
|
most = Math.max(most, worth(i));
|
|
14
|
-
let total = 0;
|
|
15
|
-
for (let i = 0; i < n; i++)
|
|
16
|
-
|
|
14
|
+
let [total, sum] = [0, 0];
|
|
15
|
+
for (let i = 0; i < n; i++) {
|
|
16
|
+
const w = worth(i);
|
|
17
|
+
ends[i] = total += w / (most || 1);
|
|
18
|
+
sum += w;
|
|
19
|
+
}
|
|
17
20
|
for (let i = 0; i < n; i++)
|
|
18
21
|
ends[i] /= total || 1;
|
|
19
22
|
let wide = 0;
|
|
@@ -73,17 +76,21 @@ export function arcs(o) {
|
|
|
73
76
|
};
|
|
74
77
|
const said = o.text === true ? (_, share) => `${Math.round(share * 100)}%` : o.text;
|
|
75
78
|
const within = (i, x, y, w, h) => inRing([cx, cy, hole, outer, ends[i - 1] ?? 0, ends[i]], x, y, w, h);
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
const middle = outer ? (typeof o.center === 'function' ? o.center(sum) : o.center) : '';
|
|
80
|
+
const labels = [
|
|
81
|
+
...(middle ? [{ x: cx, y: cy, text: middle, priority: Infinity }] : []),
|
|
82
|
+
...(!said
|
|
83
|
+
? []
|
|
84
|
+
: Array.from(ends, (end, i) => {
|
|
85
|
+
const share = end - (ends[i - 1] ?? 0);
|
|
86
|
+
// A slice narrower at the rim than one character has no room, so it is not asked.
|
|
87
|
+
const text = share * TURN * outer >= 7 * dpr ? said(i, share) : '';
|
|
88
|
+
const [x, y] = anchor(i);
|
|
89
|
+
if (!text || !within(i, x, y, textWidth(text) * dpr, 16 * dpr))
|
|
90
|
+
return undefined;
|
|
91
|
+
return { x, y, text, color: '#fff', priority: share };
|
|
92
|
+
}).filter(l => l !== undefined)),
|
|
93
|
+
];
|
|
87
94
|
return {
|
|
88
95
|
labels,
|
|
89
96
|
paint(k, row, rest = 1) {
|
package/dist/scale.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Axis, Col, Room } from './types.js';
|
|
1
|
+
import type { Axis, Col, Room, Span } from './types.js';
|
|
2
2
|
/** The 1, 2 or 5 times a power of ten that cuts the span into at most `max` steps. */
|
|
3
3
|
export declare function niceStep(span: number, max?: number): number;
|
|
4
4
|
export declare function extent(cols: Col[]): [number, number];
|
|
@@ -8,7 +8,9 @@ export declare const fits: (count: number, widest: number, room: Room) => boolea
|
|
|
8
8
|
export declare const thin: <T>(ticks: T[], widest: number, room?: Room) => T[];
|
|
9
9
|
/** The ticks `keep` picks for the least k whose labels fit, every k-th by default; the first when it picks none. */
|
|
10
10
|
export declare const spaced: <T>(ticks: T[], widest: number, room?: Room, keep?: (t: T, i: number, k: number) => boolean) => T[];
|
|
11
|
-
|
|
11
|
+
export declare const fixed: (domain: Axis['domain']) => domain is Span;
|
|
12
|
+
export declare function closed(domain: Axis['domain'] | undefined, who: string, what: string): asserts domain is Span | 'nice' | 'data' | undefined;
|
|
13
|
+
/** `of` is the extent of the data, used for each end of the domain that is 'nice' or 'data'. */
|
|
12
14
|
export declare function scale(domain: Axis['domain'], of: [number, number], room?: Room, { step: given, format }?: Pick<Axis, 'step' | 'format'>): {
|
|
13
15
|
lo: number;
|
|
14
16
|
hi: number;
|
package/dist/scale.js
CHANGED
|
@@ -31,20 +31,39 @@ export const spaced = (ticks, widest, room, keep = (_, i, k) => i % k === 0) =>
|
|
|
31
31
|
const kept = ticks.filter((t, i) => keep(t, i, k));
|
|
32
32
|
return kept.length ? kept : ticks.slice(0, 1);
|
|
33
33
|
};
|
|
34
|
-
|
|
34
|
+
export const fixed = (domain) => typeof domain !== 'string' && typeof domain[0] === 'number' && typeof domain[1] === 'number';
|
|
35
|
+
export function closed(domain, who, what) {
|
|
36
|
+
if (Array.isArray(domain) && !fixed(domain))
|
|
37
|
+
throw new Error(`${who} with one open end does not apply to ${what}`);
|
|
38
|
+
}
|
|
39
|
+
/** `of` is the extent of the data, used for each end of the domain that is 'nice' or 'data'. */
|
|
35
40
|
export function scale(domain, of, room, { step: given, format = String } = {}) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
const [first, last] = typeof domain === 'string' ? [domain, domain] : domain;
|
|
42
|
+
const held = (v) => Math.max(-Number.MAX_VALUE, Math.min(Number.MAX_VALUE, v));
|
|
43
|
+
let lo = typeof first === 'number' ? first : of[0];
|
|
44
|
+
let hi = typeof last === 'number' ? last : of[1];
|
|
45
|
+
if (typeof first === typeof last) {
|
|
46
|
+
if (lo === hi) {
|
|
47
|
+
const pad = Math.abs(lo) / 2 || 0.5;
|
|
48
|
+
lo -= pad;
|
|
49
|
+
hi += pad;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else if (lo >= hi) {
|
|
53
|
+
// Data at or past the fixed end: the open end moves away from it, so the axis does not invert.
|
|
54
|
+
if (typeof first === 'number')
|
|
55
|
+
hi = held(lo + (Math.abs(lo) / 2 || 0.5));
|
|
56
|
+
else
|
|
57
|
+
lo = held(hi - (Math.abs(hi) / 2 || 0.5));
|
|
58
|
+
// A fixed end at ±MAX_VALUE has no room past it, so it moves inward by half instead.
|
|
59
|
+
if (lo === hi)
|
|
60
|
+
[lo, hi] = typeof first === 'number' ? [lo / 2, hi] : [lo, hi / 2];
|
|
41
61
|
}
|
|
42
62
|
// The bounds are rounded to this step whatever the room, so they hold still when the chart is resized.
|
|
43
63
|
// A span too small to divide is its own step, and a bound rounded past the largest number stays on it.
|
|
44
64
|
// A step too fine for the 1000 ticks counted below would leave ticks at the low end only.
|
|
45
65
|
const fixed = Number.isFinite(given) && given > 0 && (hi - lo) / given < 990;
|
|
46
66
|
const step = fixed ? given : niceStep(hi - lo) || hi - lo;
|
|
47
|
-
const held = (v) => Math.max(-Number.MAX_VALUE, Math.min(Number.MAX_VALUE, v));
|
|
48
67
|
// Cleaning to 15 digits is undone where it moves a value by more than float error, so it never merges multiples.
|
|
49
68
|
const clean = (v, tick) => {
|
|
50
69
|
const c = +v.toPrecision(15);
|
|
@@ -61,8 +80,10 @@ export function scale(domain, of, room, { step: given, format = String } = {}) {
|
|
|
61
80
|
}
|
|
62
81
|
return v;
|
|
63
82
|
};
|
|
64
|
-
if (
|
|
65
|
-
|
|
83
|
+
if (first === 'nice')
|
|
84
|
+
lo = multiple(lo, -1);
|
|
85
|
+
if (last === 'nice')
|
|
86
|
+
hi = multiple(hi, 1);
|
|
66
87
|
const at = (tick) => {
|
|
67
88
|
const digits = Math.max(0, -Math.floor(Math.log10(tick)));
|
|
68
89
|
const first = Math.ceil(lo / tick - 1e-9);
|
package/dist/scales/band.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { closed } from '../scale.js';
|
|
1
2
|
import { widest } from '../text.js';
|
|
2
3
|
function resolve({ categories, domain }, _extent, room) {
|
|
4
|
+
closed(domain, 'band() domain', 'categories');
|
|
3
5
|
const n = categories.length;
|
|
4
6
|
const [first, last] = typeof domain === 'string'
|
|
5
7
|
? [0, n - 1]
|
package/dist/scales/bare.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Axis } from '../types.js';
|
|
1
|
+
import type { Axis, Resolved } from '../types.js';
|
|
2
|
+
export declare function resolve(axis: Axis, extent: () => [number, number]): Resolved;
|
|
2
3
|
/**
|
|
3
4
|
* An axis that places the marks and shows nothing itself: no ticks, no grid lines and no label.
|
|
4
5
|
* @param domain 'data' (the default) fits the marks exactly, 'nice' widens that to whole steps; or [lo, hi].
|
package/dist/scales/bare.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { scale } from '../scale.js';
|
|
2
|
-
function resolve(axis, extent) {
|
|
3
|
-
const { lo, hi } = scale(axis.domain,
|
|
1
|
+
import { fixed, scale } from '../scale.js';
|
|
2
|
+
export function resolve(axis, extent) {
|
|
3
|
+
const { lo, hi } = scale(axis.domain, fixed(axis.domain) ? axis.domain : extent());
|
|
4
4
|
return { lo, hi, ticks: [] };
|
|
5
5
|
}
|
|
6
6
|
/**
|
package/dist/scales/log.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fits, scale as linear, spaced } from '../scale.js';
|
|
1
|
+
import { closed, fits, scale as linear, spaced } from '../scale.js';
|
|
2
2
|
import { widest as widestOf } from '../text.js';
|
|
3
3
|
// log10 and log2 are exact at their own powers; another base's division can land a hair off one.
|
|
4
4
|
const lg = (v, base) => {
|
|
@@ -18,6 +18,7 @@ function col(values) {
|
|
|
18
18
|
function resolve(axis, extent, room) {
|
|
19
19
|
const base = baseOf(axis);
|
|
20
20
|
const format = axis.format ?? String;
|
|
21
|
+
closed(axis.domain, 'log() domain', 'a log scale');
|
|
21
22
|
let [lo, hi] = typeof axis.domain === 'string' ? extent() : axis.domain;
|
|
22
23
|
if (lo === hi)
|
|
23
24
|
[lo, hi] = [lo - 0.5, hi + 0.5];
|
|
@@ -80,6 +81,7 @@ function resolve(axis, extent, room) {
|
|
|
80
81
|
export function log({ label, base = 10, domain = 'nice', format, fit }) {
|
|
81
82
|
if (!(base > 0 && base !== 1 && Number.isFinite(base)))
|
|
82
83
|
throw new RangeError(`log() base ${base} must be above zero and not 1`);
|
|
84
|
+
closed(domain, 'log() domain', 'a log scale');
|
|
83
85
|
if (typeof domain !== 'string')
|
|
84
86
|
for (const bound of domain)
|
|
85
87
|
if (!(bound > 0 && bound < Infinity))
|
package/dist/scales/time.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { niceStep, spaced, thin } from '../scale.js';
|
|
1
|
+
import { closed, niceStep, spaced, thin } from '../scale.js';
|
|
2
2
|
import { estimated, textWidth, widest as widestOf } from '../text.js';
|
|
3
3
|
const UNITS = ['year', 'month', 'day', 'hour', 'minute', 'second'];
|
|
4
4
|
const MONTHS = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
|
|
@@ -71,6 +71,7 @@ function label([y, mo, d, h, mi, s], field) {
|
|
|
71
71
|
return h || mi ? `${pad(h)}:${pad(mi)}` : day;
|
|
72
72
|
}
|
|
73
73
|
function resolve(axis, extent, room) {
|
|
74
|
+
closed(axis.domain, 'time() domain', 'dates');
|
|
74
75
|
const { lo, hi, ticks, fixed, keep } = stepped(axis, typeof axis.domain === 'string' ? extent() : axis.domain, room);
|
|
75
76
|
const widest = widestOf(ticks.map(t => t.label), axis.format ? textWidth : estimated);
|
|
76
77
|
return {
|