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,133 @@
|
|
|
1
|
+
import { glLayer } from '../gl.js';
|
|
2
|
+
import { base, ends } from './common.js';
|
|
3
|
+
import { focused, gpuRows } from './rects.js';
|
|
4
|
+
// Per candle: the body's left and right, the wick's top and bottom, the body's top and bottom.
|
|
5
|
+
const EDGES = 6;
|
|
6
|
+
export function candlestick(o) {
|
|
7
|
+
const { x, open, high, low, close, color, alpha = 1, width = 0.6 } = o;
|
|
8
|
+
const n = x.length;
|
|
9
|
+
const inks = [o.up ?? color, o.down ?? color];
|
|
10
|
+
const falls = (i) => close[i] < open[i];
|
|
11
|
+
const [first, last] = ends(x);
|
|
12
|
+
let sides;
|
|
13
|
+
// In device pixels; a row with a number missing has none.
|
|
14
|
+
const bounds = ({ ox, kx, oy, ky }) => {
|
|
15
|
+
const edges = new Float64Array(n * EDGES).fill(NaN);
|
|
16
|
+
const y = (v) => Math.round(oy - v * ky);
|
|
17
|
+
for (let i = 0; i < n; i++) {
|
|
18
|
+
if (![x, open, high, low, close].every(c => Number.isFinite(c[i])))
|
|
19
|
+
continue;
|
|
20
|
+
const mid = ox + x[i] * kx;
|
|
21
|
+
const l = Math.round(mid - (width * kx) / 2);
|
|
22
|
+
const top = y(Math.max(open[i], close[i]));
|
|
23
|
+
edges.set([
|
|
24
|
+
l,
|
|
25
|
+
Math.max(Math.round(mid + (width * kx) / 2), l + 1),
|
|
26
|
+
y(high[i]),
|
|
27
|
+
y(low[i]),
|
|
28
|
+
top,
|
|
29
|
+
Math.max(y(Math.min(open[i], close[i])), top + 1),
|
|
30
|
+
], i * EDGES);
|
|
31
|
+
}
|
|
32
|
+
return edges;
|
|
33
|
+
};
|
|
34
|
+
// The whole candle answers, wick included, and from 3 CSS pixels beside it: a candle is often a pixel or two wide.
|
|
35
|
+
const hit = ({ dpr, left, top, width: plotWidth, height }, bounded) => (px, py, reach) => {
|
|
36
|
+
const [hx, hy] = [px * dpr, py * dpr];
|
|
37
|
+
if (hx < left || hx > left + plotWidth || hy < top || hy > top + height)
|
|
38
|
+
return null;
|
|
39
|
+
const edges = bounded();
|
|
40
|
+
let [row, gap] = [-1, 3 * dpr];
|
|
41
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
42
|
+
const j = i * EDGES;
|
|
43
|
+
if (!(hy >= edges[j + 2] && hy < Math.max(edges[j + 3], edges[j + 2] + 1)))
|
|
44
|
+
continue;
|
|
45
|
+
const beside = Math.max(edges[j] - hx, hx - edges[j + 1], 0);
|
|
46
|
+
if (beside < gap || (beside === gap && row < 0))
|
|
47
|
+
[row, gap] = [i, beside];
|
|
48
|
+
}
|
|
49
|
+
return row < 0 ? null : { x: x[row], y: close[row], distance: reach, row, color: inks[+falls(row)] };
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
...base('candlestick', o, [x, [first - width / 2, last + width / 2]], [low, high]),
|
|
53
|
+
prepare(view) {
|
|
54
|
+
const { ctx, dpr } = view;
|
|
55
|
+
const edges = bounds(view);
|
|
56
|
+
const wick = Math.max(1, Math.round(dpr));
|
|
57
|
+
// The rising candles and the falling ones, each one filled shape.
|
|
58
|
+
const paths = (rows) => {
|
|
59
|
+
const made = [new Path2D(), new Path2D()];
|
|
60
|
+
for (let i = 0; i < n; i++) {
|
|
61
|
+
const j = i * EDGES;
|
|
62
|
+
const [l, r] = [edges[j], edges[j + 1]];
|
|
63
|
+
if (!rows(i) || Number.isNaN(l))
|
|
64
|
+
continue;
|
|
65
|
+
const path = made[+falls(i)];
|
|
66
|
+
path.rect(Math.round((l + r - wick) / 2), edges[j + 2], wick, Math.max(edges[j + 3] - edges[j + 2], 1));
|
|
67
|
+
path.rect(l, edges[j + 4], r - l, edges[j + 5] - edges[j + 4]);
|
|
68
|
+
}
|
|
69
|
+
return made;
|
|
70
|
+
};
|
|
71
|
+
const all = paths(() => true);
|
|
72
|
+
let split;
|
|
73
|
+
const draw = (made, k) => {
|
|
74
|
+
ctx.globalAlpha = alpha * k;
|
|
75
|
+
made.forEach((path, at) => {
|
|
76
|
+
ctx.fillStyle = inks[at];
|
|
77
|
+
ctx.fill(path);
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
paint(a, row, rest = 1) {
|
|
82
|
+
if (row === undefined || row >= n)
|
|
83
|
+
return draw(all, a);
|
|
84
|
+
if (split?.row !== row)
|
|
85
|
+
split = { row, one: paths(i => i === row), others: paths(i => i !== row) };
|
|
86
|
+
draw(split.others, a * rest);
|
|
87
|
+
draw(split.one, a);
|
|
88
|
+
},
|
|
89
|
+
hit: hit(view, () => edges),
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
gl: {
|
|
93
|
+
layer: glLayer,
|
|
94
|
+
under: true,
|
|
95
|
+
prepare(view, { gl }) {
|
|
96
|
+
// The painter takes one colour, so each direction is its own rows, with the other's left out as gaps.
|
|
97
|
+
sides ??= [false, true].map(fall => {
|
|
98
|
+
const opened = new Float64Array(n);
|
|
99
|
+
for (let i = 0; i < n; i++)
|
|
100
|
+
opened[i] = falls(i) === fall ? open[i] : NaN;
|
|
101
|
+
return gpuRows(new WeakMap(), [x], [high, low, close, opened]);
|
|
102
|
+
});
|
|
103
|
+
const pens = sides.map((rows, at) => rows.on(view, gl, inks[at]));
|
|
104
|
+
const wick = Math.max(1, Math.round(view.dpr));
|
|
105
|
+
// Columns: x, then high, low, close and open, which is last so that its gaps do not set the rows' offset.
|
|
106
|
+
// ponytail: a candle whose open equals its close shows only its wick here; give the body a pixel if it matters.
|
|
107
|
+
const parts = [
|
|
108
|
+
{ xa: 0, xb: 0, ya: 4, yb: 3, pad: [Math.max((width * view.kx) / 2, 0.5), 0], snap: true },
|
|
109
|
+
{ xa: 0, xb: 0, ya: 1, yb: 2, pad: [wick / 2, 0], shift: [(wick % 2) / 2, 0], snap: true },
|
|
110
|
+
];
|
|
111
|
+
let edges;
|
|
112
|
+
return {
|
|
113
|
+
paint(a, row, rest = 1) {
|
|
114
|
+
for (const { draw, once } of pens) {
|
|
115
|
+
once(() => {
|
|
116
|
+
for (const [strength, ...run] of focused(n, row, rest)) {
|
|
117
|
+
for (const part of parts)
|
|
118
|
+
draw(part, alpha * a * strength, ...run);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
hit: hit(view, () => (edges ??= bounds(view))),
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
release(layer) {
|
|
128
|
+
if (layer)
|
|
129
|
+
for (const rows of sides ?? [])
|
|
130
|
+
rows.release(layer.gl);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { Col } from '../types.js';
|
|
2
|
+
export type Common = {
|
|
3
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
4
|
+
key?: string;
|
|
5
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
6
|
+
label?: string;
|
|
7
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
8
|
+
group?: string;
|
|
9
|
+
/** A CSS colour. */
|
|
10
|
+
color: string;
|
|
11
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
12
|
+
alpha?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Draws the mark against the chart's second y axis, `y2`, which then fits it; 'y' when left out.
|
|
15
|
+
* A stack holds the marks of one `yAxis`, also on a chart with no `y2`, where all are drawn on `y`.
|
|
16
|
+
*/
|
|
17
|
+
yAxis?: 'y' | 'y2';
|
|
18
|
+
};
|
|
19
|
+
export declare const base: (type: string, o: Common, x: Col[], y: Col[]) => {
|
|
20
|
+
type: string;
|
|
21
|
+
key: string | undefined;
|
|
22
|
+
label: string | undefined;
|
|
23
|
+
group: string | undefined;
|
|
24
|
+
color: string;
|
|
25
|
+
options: Common;
|
|
26
|
+
yAxis: "y" | "y2" | undefined;
|
|
27
|
+
points: number;
|
|
28
|
+
channels: {
|
|
29
|
+
x: Col[];
|
|
30
|
+
y: Col[];
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
/** A stack's key: one id on the two y axes is two stacks, as values on two scales do not add up. */
|
|
34
|
+
export declare const stackKey: (type: string, o: {
|
|
35
|
+
yAxis?: 'y' | 'y2';
|
|
36
|
+
stack?: string;
|
|
37
|
+
}) => string;
|
|
38
|
+
/** The least and the greatest finite x, or Infinity and -Infinity when there is none. */
|
|
39
|
+
export declare function ends(x: Col): [number, number];
|
|
40
|
+
/** 'smooth' curves through the points where they are further apart than a pixel, and is 'linear' where they are not. */
|
|
41
|
+
export type Curve = 'linear' | 'step' | 'smooth';
|
|
42
|
+
/**
|
|
43
|
+
* A curve on from the path's current point through n points, forwards or backwards, that never
|
|
44
|
+
* goes above or below the points it joins: its lean at each is the harmonic mean of the leans
|
|
45
|
+
* either side of it, and level where they turn.
|
|
46
|
+
*/
|
|
47
|
+
export declare function smooth(path: Path2D, xs: ArrayLike<number>, ys: ArrayLike<number>, n: number, back?: boolean): void;
|
|
48
|
+
export declare const TURN: number;
|
|
49
|
+
/** How far round the clock from twelve the point (dx, dy) from the centre is, from 0 up to 1. */
|
|
50
|
+
/**
|
|
51
|
+
* Whether level text w by h, centred at (x, y), lies inside the part of a ring round (cx, cy)
|
|
52
|
+
* between two radii and two shares of the turn: its nearest and furthest points between the
|
|
53
|
+
* radii, and its corners between the two edges.
|
|
54
|
+
*/
|
|
55
|
+
export declare function inRing([cx, cy, inner, outer, from, to]: readonly number[], x: number, y: number, w: number, h: number): boolean;
|
|
56
|
+
export declare const turnAt: (dx: number, dy: number) => number;
|
|
57
|
+
/**
|
|
58
|
+
* One min, max and last y per device-pixel column. x must be ascending.
|
|
59
|
+
* `first` (each column's first y) and `at` (its exact x) are kept only with `straight`;
|
|
60
|
+
* without it they alias `lo` and must not be read.
|
|
61
|
+
* A y that is not finite once projected, or a value of `also` at the same row that is not, ends a
|
|
62
|
+
* run: `runs` holds each run's first column, the column after its last and how many values it
|
|
63
|
+
* kept, and a pixel column a run ends in is split. A run also ends after a row in `cuts`, and is
|
|
64
|
+
* marked as following one when nothing else broke it since.
|
|
65
|
+
*/
|
|
66
|
+
export declare function columns(x: Col, y: Col, ox: number, kx: number, oy: number, ky: number, straight?: boolean, also?: Col, [from, to]?: [number, number], cuts?: Set<number>): {
|
|
67
|
+
n: number;
|
|
68
|
+
col: Int32Array<ArrayBuffer>;
|
|
69
|
+
lo: Float64Array<ArrayBuffer>;
|
|
70
|
+
hi: Float64Array<ArrayBuffer>;
|
|
71
|
+
last: Float64Array<ArrayBuffer>;
|
|
72
|
+
first: Float64Array<ArrayBuffer>;
|
|
73
|
+
at: Float64Array<ArrayBuffer>;
|
|
74
|
+
runs: [number, number, number, boolean][];
|
|
75
|
+
};
|
|
76
|
+
/** Runs of columns() put together where each follows a cut, for `join` to draw as one. */
|
|
77
|
+
export declare function chains<T extends [number, number, number, boolean]>(runs: T[]): T[][];
|
|
78
|
+
/**
|
|
79
|
+
* x and ys cut to the rows where every y is finite, and the kept rows a gap follows: the curve is
|
|
80
|
+
* drawn straight from each of those to the next.
|
|
81
|
+
*/
|
|
82
|
+
export declare function joined<T extends Col[]>(x: Col, ...ys: T): [[Col, ...T], Set<number>];
|
|
83
|
+
/** How many rows of an ascending x are at or below v. */
|
|
84
|
+
export declare function upTo(x: Col, v: number): number;
|
|
85
|
+
/**
|
|
86
|
+
* The rows of an ascending x that a curve drawn between the device pixels lo and hi reads: those
|
|
87
|
+
* between them and `keep` pixel columns of rows beyond each, so no column is cut in two.
|
|
88
|
+
*/
|
|
89
|
+
export declare function span(x: Col, ox: number, kx: number, lo: number, hi: number, keep: number): [number, number];
|
|
90
|
+
/** The least and greatest f(value) over the rows where f of every column is finite. */
|
|
91
|
+
export declare function extent(ys: Col[], f?: (v: number) => number): [number, number];
|
|
92
|
+
/**
|
|
93
|
+
* The least and greatest oy - y * ky over the rows of ys, from their extent: when an end of it
|
|
94
|
+
* overflows once projected, the rows are scanned again for the ones that do not.
|
|
95
|
+
*/
|
|
96
|
+
export declare function reach([lo, hi]: [number, number], ys: Col[], oy: number, ky: number): [number, number];
|
|
97
|
+
/**
|
|
98
|
+
* y at v as the curve draws it: the held sample of a step, else the line or smooth() between
|
|
99
|
+
* samples, and the line from a row in `cuts` to the next. A row where y, or `also` like columns(), is not
|
|
100
|
+
* finite breaks the curve.
|
|
101
|
+
*/
|
|
102
|
+
export declare function sampleAt(x: Col, y: Col, v: number, curve: Curve, also?: Col, cuts?: Set<number>): number | undefined;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
export const base = (type, o, x, y) => ({
|
|
2
|
+
type,
|
|
3
|
+
key: o.key,
|
|
4
|
+
label: o.label,
|
|
5
|
+
group: o.group ?? o.label,
|
|
6
|
+
color: o.color,
|
|
7
|
+
options: o,
|
|
8
|
+
yAxis: o.yAxis,
|
|
9
|
+
points: x[0]?.length ?? 0,
|
|
10
|
+
channels: { x, y },
|
|
11
|
+
});
|
|
12
|
+
/** A stack's key: one id on the two y axes is two stacks, as values on two scales do not add up. */
|
|
13
|
+
export const stackKey = (type, o) => `${type}:stack:${o.yAxis ?? 'y'}:${o.stack}`;
|
|
14
|
+
/** The least and the greatest finite x, or Infinity and -Infinity when there is none. */
|
|
15
|
+
export function ends(x) {
|
|
16
|
+
let [first, last] = [Infinity, -Infinity];
|
|
17
|
+
for (let i = 0; i < x.length; i++) {
|
|
18
|
+
if (x[i] < first && x[i] !== -Infinity)
|
|
19
|
+
first = x[i];
|
|
20
|
+
if (x[i] > last && x[i] !== Infinity)
|
|
21
|
+
last = x[i];
|
|
22
|
+
}
|
|
23
|
+
return [first, last];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A curve on from the path's current point through n points, forwards or backwards, that never
|
|
27
|
+
* goes above or below the points it joins: its lean at each is the harmonic mean of the leans
|
|
28
|
+
* either side of it, and level where they turn.
|
|
29
|
+
*/
|
|
30
|
+
export function smooth(path, xs, ys, n, back = false) {
|
|
31
|
+
const at = (k) => (back ? n - 1 - k : k);
|
|
32
|
+
const rise = (k) => (ys[at(k + 1)] - ys[at(k)]) / (xs[at(k + 1)] - xs[at(k)] || 1);
|
|
33
|
+
const lean = (k) => {
|
|
34
|
+
if (k === 0 || k === n - 1)
|
|
35
|
+
return rise(Math.min(k, n - 2));
|
|
36
|
+
const [a, b] = [rise(k - 1), rise(k)];
|
|
37
|
+
return a * b > 0 ? (2 * a * b) / (a + b) : 0;
|
|
38
|
+
};
|
|
39
|
+
for (let k = 1; k < n; k++) {
|
|
40
|
+
const [x0, y0, x1, y1] = [xs[at(k - 1)], ys[at(k - 1)], xs[at(k)], ys[at(k)]];
|
|
41
|
+
const third = (x1 - x0) / 3;
|
|
42
|
+
path.bezierCurveTo(x0 + third, y0 + lean(k - 1) * third, x1 - third, y1 - lean(k) * third, x1, y1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export const TURN = 2 * Math.PI;
|
|
46
|
+
/** How far round the clock from twelve the point (dx, dy) from the centre is, from 0 up to 1. */
|
|
47
|
+
/**
|
|
48
|
+
* Whether level text w by h, centred at (x, y), lies inside the part of a ring round (cx, cy)
|
|
49
|
+
* between two radii and two shares of the turn: its nearest and furthest points between the
|
|
50
|
+
* radii, and its corners between the two edges.
|
|
51
|
+
*/
|
|
52
|
+
export function inRing([cx, cy, inner, outer, from, to], x, y, w, h) {
|
|
53
|
+
const [l, t, r, b] = [x - w / 2 - cx, y - h / 2 - cy, x + w / 2 - cx, y + h / 2 - cy];
|
|
54
|
+
const near = Math.hypot(Math.max(l, 0, -r), Math.max(t, 0, -b));
|
|
55
|
+
const far = Math.hypot(Math.max(-l, r), Math.max(-t, b));
|
|
56
|
+
if (near < inner || far > outer)
|
|
57
|
+
return false;
|
|
58
|
+
if (to - from >= 1)
|
|
59
|
+
return true;
|
|
60
|
+
return [
|
|
61
|
+
[l, t],
|
|
62
|
+
[r, t],
|
|
63
|
+
[r, b],
|
|
64
|
+
[l, b],
|
|
65
|
+
].every(([px, py]) => {
|
|
66
|
+
const share = turnAt(px, py);
|
|
67
|
+
return share >= from && share < to;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
export const turnAt = (dx, dy) => ((((Math.atan2(dy, dx) + TURN / 4) / TURN) % 1) + 1) % 1;
|
|
71
|
+
/**
|
|
72
|
+
* One min, max and last y per device-pixel column. x must be ascending.
|
|
73
|
+
* `first` (each column's first y) and `at` (its exact x) are kept only with `straight`;
|
|
74
|
+
* without it they alias `lo` and must not be read.
|
|
75
|
+
* A y that is not finite once projected, or a value of `also` at the same row that is not, ends a
|
|
76
|
+
* run: `runs` holds each run's first column, the column after its last and how many values it
|
|
77
|
+
* kept, and a pixel column a run ends in is split. A run also ends after a row in `cuts`, and is
|
|
78
|
+
* marked as following one when nothing else broke it since.
|
|
79
|
+
*/
|
|
80
|
+
export function columns(x, y, ox, kx, oy, ky, straight = false, also, [from, to] = [0, x.length], cuts) {
|
|
81
|
+
const n = to - from;
|
|
82
|
+
const col = new Int32Array(n);
|
|
83
|
+
const lo = new Float64Array(n);
|
|
84
|
+
const hi = new Float64Array(n);
|
|
85
|
+
const last = new Float64Array(n);
|
|
86
|
+
const first = straight ? new Float64Array(n) : lo;
|
|
87
|
+
const at = straight ? new Float64Array(n) : lo;
|
|
88
|
+
const runs = [];
|
|
89
|
+
let cut = true;
|
|
90
|
+
let bridged = false;
|
|
91
|
+
let k = -1;
|
|
92
|
+
for (let i = from; i < to; i++) {
|
|
93
|
+
const p = ox + x[i] * kx;
|
|
94
|
+
const c = Math.round(p);
|
|
95
|
+
const v = oy - y[i] * ky;
|
|
96
|
+
if (!Number.isFinite(v) || (also && !Number.isFinite(oy - also[i] * ky))) {
|
|
97
|
+
cut = true;
|
|
98
|
+
bridged = false;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (cut)
|
|
102
|
+
runs.push([k + 1, k + 1, 0, bridged]);
|
|
103
|
+
const run = runs[runs.length - 1];
|
|
104
|
+
run[2]++;
|
|
105
|
+
if (cut || c !== col[k]) {
|
|
106
|
+
k++;
|
|
107
|
+
run[1] = k + 1;
|
|
108
|
+
cut = false;
|
|
109
|
+
col[k] = c;
|
|
110
|
+
at[k] = p;
|
|
111
|
+
first[k] = lo[k] = hi[k] = v;
|
|
112
|
+
}
|
|
113
|
+
else if (v < lo[k])
|
|
114
|
+
lo[k] = v;
|
|
115
|
+
else if (v > hi[k])
|
|
116
|
+
hi[k] = v;
|
|
117
|
+
last[k] = v;
|
|
118
|
+
if (cuts?.has(i))
|
|
119
|
+
cut = bridged = true;
|
|
120
|
+
}
|
|
121
|
+
return { n: k + 1, col, lo, hi, last, first, at, runs };
|
|
122
|
+
}
|
|
123
|
+
/** Runs of columns() put together where each follows a cut, for `join` to draw as one. */
|
|
124
|
+
export function chains(runs) {
|
|
125
|
+
const out = [];
|
|
126
|
+
for (const run of runs) {
|
|
127
|
+
if (run[3] && out.length)
|
|
128
|
+
out[out.length - 1].push(run);
|
|
129
|
+
else
|
|
130
|
+
out.push([run]);
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* x and ys cut to the rows where every y is finite, and the kept rows a gap follows: the curve is
|
|
136
|
+
* drawn straight from each of those to the next.
|
|
137
|
+
*/
|
|
138
|
+
export function joined(x, ...ys) {
|
|
139
|
+
const keep = [];
|
|
140
|
+
for (let i = 0; i < x.length; i++)
|
|
141
|
+
if (ys.every(y => Number.isFinite(y[i])))
|
|
142
|
+
keep.push(i);
|
|
143
|
+
const cuts = new Set();
|
|
144
|
+
for (let k = 1; k < keep.length; k++)
|
|
145
|
+
if (keep[k] !== keep[k - 1] + 1)
|
|
146
|
+
cuts.add(k - 1);
|
|
147
|
+
if (keep.length === x.length)
|
|
148
|
+
return [[x, ...ys], cuts];
|
|
149
|
+
const cut = (c) => Float64Array.from(keep, i => c[i]);
|
|
150
|
+
return [[cut(x), ...ys.map(cut)], cuts];
|
|
151
|
+
}
|
|
152
|
+
/** How many rows of an ascending x are at or below v. */
|
|
153
|
+
export function upTo(x, v) {
|
|
154
|
+
let a = 0;
|
|
155
|
+
let b = x.length;
|
|
156
|
+
while (a < b) {
|
|
157
|
+
const mid = (a + b) >> 1;
|
|
158
|
+
if (x[mid] <= v)
|
|
159
|
+
a = mid + 1;
|
|
160
|
+
else
|
|
161
|
+
b = mid;
|
|
162
|
+
}
|
|
163
|
+
return a;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The rows of an ascending x that a curve drawn between the device pixels lo and hi reads: those
|
|
167
|
+
* between them and `keep` pixel columns of rows beyond each, so no column is cut in two.
|
|
168
|
+
*/
|
|
169
|
+
export function span(x, ox, kx, lo, hi, keep) {
|
|
170
|
+
const column = (i) => Math.round(ox + x[i] * kx);
|
|
171
|
+
let [a, b] = [upTo(x, (lo - ox) / kx), upTo(x, (hi - ox) / kx)];
|
|
172
|
+
if (a > b)
|
|
173
|
+
[a, b] = [b, a];
|
|
174
|
+
for (let k = 0; k < keep && a > 0; k++)
|
|
175
|
+
for (a--; a > 0 && column(a - 1) === column(a);)
|
|
176
|
+
a--;
|
|
177
|
+
for (let k = 0; k < keep && b < x.length; k++)
|
|
178
|
+
for (b++; b < x.length && column(b) === column(b - 1);)
|
|
179
|
+
b++;
|
|
180
|
+
return [a, b];
|
|
181
|
+
}
|
|
182
|
+
/** The least and greatest f(value) over the rows where f of every column is finite. */
|
|
183
|
+
export function extent(ys, f = (v) => v) {
|
|
184
|
+
let lo = Infinity;
|
|
185
|
+
let hi = -Infinity;
|
|
186
|
+
rows: for (let i = 0; i < ys[0].length; i++) {
|
|
187
|
+
for (const y of ys)
|
|
188
|
+
if (!Number.isFinite(f(y[i])))
|
|
189
|
+
continue rows;
|
|
190
|
+
for (const y of ys) {
|
|
191
|
+
const v = f(y[i]);
|
|
192
|
+
if (v < lo)
|
|
193
|
+
lo = v;
|
|
194
|
+
if (v > hi)
|
|
195
|
+
hi = v;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return [lo, hi];
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* The least and greatest oy - y * ky over the rows of ys, from their extent: when an end of it
|
|
202
|
+
* overflows once projected, the rows are scanned again for the ones that do not.
|
|
203
|
+
*/
|
|
204
|
+
export function reach([lo, hi], ys, oy, ky) {
|
|
205
|
+
const [a, b] = [oy - lo * ky, oy - hi * ky];
|
|
206
|
+
if (!Number.isFinite(a) || !Number.isFinite(b))
|
|
207
|
+
return extent(ys, v => oy - v * ky);
|
|
208
|
+
return a < b ? [a, b] : [b, a];
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* y at v as the curve draws it: the held sample of a step, else the line or smooth() between
|
|
212
|
+
* samples, and the line from a row in `cuts` to the next. A row where y, or `also` like columns(), is not
|
|
213
|
+
* finite breaks the curve.
|
|
214
|
+
*/
|
|
215
|
+
export function sampleAt(x, y, v, curve, also, cuts) {
|
|
216
|
+
const ok = (i) => Number.isFinite(y[i]) && (!also || Number.isFinite(also[i]));
|
|
217
|
+
const a = upTo(x, v);
|
|
218
|
+
if (!a)
|
|
219
|
+
return undefined;
|
|
220
|
+
const x0 = x[a - 1];
|
|
221
|
+
const y0 = y[a - 1];
|
|
222
|
+
if (!ok(a - 1))
|
|
223
|
+
return undefined;
|
|
224
|
+
if (x0 === v)
|
|
225
|
+
return y0;
|
|
226
|
+
if (a < x.length && !ok(a))
|
|
227
|
+
return undefined;
|
|
228
|
+
if (cuts?.has(a - 1))
|
|
229
|
+
curve = 'linear';
|
|
230
|
+
if (curve === 'step')
|
|
231
|
+
return y0;
|
|
232
|
+
if (a === x.length)
|
|
233
|
+
return undefined;
|
|
234
|
+
const [x1, y1] = [x[a], y[a]];
|
|
235
|
+
const t = (v - x0) / (x1 - x0);
|
|
236
|
+
if (curve !== 'smooth')
|
|
237
|
+
return y0 + (y1 - y0) * t;
|
|
238
|
+
const rise = (i) => i >= 0 && i + 1 < x.length && ok(i) && ok(i + 1) && !cuts?.has(i)
|
|
239
|
+
? (y[i + 1] - y[i]) / (x[i + 1] - x[i] || 1)
|
|
240
|
+
: undefined;
|
|
241
|
+
const lean = (i) => {
|
|
242
|
+
const [p, q] = [rise(i - 1), rise(i)];
|
|
243
|
+
if (p === undefined || q === undefined)
|
|
244
|
+
return (p ?? q);
|
|
245
|
+
return p * q > 0 ? (2 * p * q) / (p + q) : 0;
|
|
246
|
+
};
|
|
247
|
+
const [m0, m1] = [lean(a - 1) * (x1 - x0), lean(a) * (x1 - x0)];
|
|
248
|
+
const s = 1 - t;
|
|
249
|
+
return s * s * (1 + 2 * t) * y0 + s * s * t * m0 + t * t * (3 - 2 * t) * y1 - t * t * s * m1;
|
|
250
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
import type { Common } from './common.js';
|
|
3
|
+
/** A dot at each point, each with its own radius. */
|
|
4
|
+
export type DotsOptions = Common & {
|
|
5
|
+
/** The x of each point. */
|
|
6
|
+
x: Col;
|
|
7
|
+
/** The y of each point. */
|
|
8
|
+
y: Col;
|
|
9
|
+
/** The radius of each point in CSS pixels. */
|
|
10
|
+
r: Col;
|
|
11
|
+
/** Draws each point as a ring of its colour this many CSS pixels wide, always on the canvas. */
|
|
12
|
+
ring?: number;
|
|
13
|
+
/** The middle of a ring point, a CSS colour; the theme's surface when left out. */
|
|
14
|
+
fill?: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function dots(o: DotsOptions): Mark;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { glLayer, program, rgba } from '../gl.js';
|
|
2
|
+
import { base } from './common.js';
|
|
3
|
+
const TAU = 2 * Math.PI;
|
|
4
|
+
// Measured 2026-11-24: below this the canvas mounts sooner than WebGL can be set up, also at 4x CPU slowdown.
|
|
5
|
+
const GL_FROM = 20_000;
|
|
6
|
+
const origin = (c) => {
|
|
7
|
+
for (let i = 0; i < c.length; i++)
|
|
8
|
+
if (Number.isFinite(c[i]))
|
|
9
|
+
return c[i];
|
|
10
|
+
return 0;
|
|
11
|
+
};
|
|
12
|
+
const programs = new WeakMap();
|
|
13
|
+
function pointProgram(gl) {
|
|
14
|
+
const p = program(gl, 'in vec3 a;uniform vec4 m;uniform float dpr;out float size;void main(){gl_Position=vec4(a.x*m.x+m.y,a.y*m.z+m.w,0,1);size=a.z*2.0*dpr+1.0;gl_PointSize=size;}', 'precision mediump float;uniform vec4 color;uniform float hole;in float size;out vec4 o;void main(){float d=length(gl_PointCoord-0.5)*size;float al=clamp(size*0.5-d,0.0,1.0)*color.a;if(hole>0.0)al*=clamp(d-size*0.5+0.5+hole,0.0,1.0);if(al<=0.0)discard;o=vec4(color.rgb*al,al);}');
|
|
15
|
+
const a = gl.getAttribLocation(p, 'a');
|
|
16
|
+
gl.enableVertexAttribArray(a);
|
|
17
|
+
const uniform = (name) => gl.getUniformLocation(p, name);
|
|
18
|
+
return { p, a, m: uniform('m'), dpr: uniform('dpr'), hole: uniform('hole'), color: uniform('color') };
|
|
19
|
+
}
|
|
20
|
+
export function dots(o) {
|
|
21
|
+
const { x, y, r, color, alpha = 1 } = o;
|
|
22
|
+
const n = x.length;
|
|
23
|
+
const buffers = new WeakMap();
|
|
24
|
+
let reach = 0;
|
|
25
|
+
for (let i = 0; i < n; i++)
|
|
26
|
+
reach = Math.max(reach, r[i]);
|
|
27
|
+
const overhang = reach + (o.ring ?? 0) / 2;
|
|
28
|
+
// ponytail: linear scan per pointer move, fine at 100k points; a grid index when it shows in a profile.
|
|
29
|
+
const hit = ({ ox, kx, oy, ky, dpr, left, top, width, height }) => (px, py, max) => {
|
|
30
|
+
let best = max ** 2;
|
|
31
|
+
let at = -1;
|
|
32
|
+
for (let i = 0; i < n; i++) {
|
|
33
|
+
const cx = ox + x[i] * kx;
|
|
34
|
+
const cy = oy - y[i] * ky;
|
|
35
|
+
if (cx < left || cx > left + width || cy < top || cy > top + height)
|
|
36
|
+
continue;
|
|
37
|
+
const d = (cx / dpr - px) ** 2 + (cy / dpr - py) ** 2;
|
|
38
|
+
if (d < best)
|
|
39
|
+
[best, at] = [d, i];
|
|
40
|
+
}
|
|
41
|
+
return at < 0 ? null : { x: x[at], y: y[at], distance: Math.sqrt(best), row: at };
|
|
42
|
+
};
|
|
43
|
+
const prepareGl = (view, { gl }) => {
|
|
44
|
+
const u = programs.get(gl) ?? pointProgram(gl);
|
|
45
|
+
programs.set(gl, u);
|
|
46
|
+
// The offsets are subtracted in float64: a float32 attribute cannot hold e.g. millisecond timestamps.
|
|
47
|
+
// They come from the data, not the domain, so a new view changes only the uniform and never the buffer.
|
|
48
|
+
const x0 = origin(x);
|
|
49
|
+
const y0 = origin(y);
|
|
50
|
+
let buffer = buffers.get(gl);
|
|
51
|
+
if (!buffer) {
|
|
52
|
+
const data = new Float32Array(n * 3);
|
|
53
|
+
for (let i = 0; i < n; i++) {
|
|
54
|
+
data[3 * i] = x[i] - x0;
|
|
55
|
+
data[3 * i + 1] = y[i] - y0;
|
|
56
|
+
data[3 * i + 2] = Math.max(0, r[i]);
|
|
57
|
+
}
|
|
58
|
+
buffer = gl.createBuffer();
|
|
59
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
60
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
|
|
61
|
+
buffers.set(gl, buffer);
|
|
62
|
+
}
|
|
63
|
+
const { ctx, kx, ox, ky, oy, dpr } = view;
|
|
64
|
+
const [red, green, blue, opacity = 1] = rgba(ctx, color);
|
|
65
|
+
const ready = () => {
|
|
66
|
+
const { width: w, height: h } = gl.canvas;
|
|
67
|
+
gl.useProgram(u.p);
|
|
68
|
+
gl.uniform4f(u.m, (kx * 2) / w, ((ox + x0 * kx) * 2) / w - 1, (ky * 2) / h, 1 - ((oy - y0 * ky) * 2) / h);
|
|
69
|
+
gl.uniform1f(u.dpr, dpr);
|
|
70
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
71
|
+
gl.vertexAttribPointer(u.a, 3, gl.FLOAT, false, 0, 0);
|
|
72
|
+
};
|
|
73
|
+
const own = () => {
|
|
74
|
+
gl.uniform1f(u.hole, 0);
|
|
75
|
+
gl.uniform4f(u.color, red, green, blue, alpha * opacity);
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
// The alpha argument is unused: dense points saturate, so the layer's fade() dims a group instead.
|
|
79
|
+
paint() {
|
|
80
|
+
ready();
|
|
81
|
+
own();
|
|
82
|
+
gl.drawArrays(gl.POINTS, 0, n);
|
|
83
|
+
},
|
|
84
|
+
select(rows, outline) {
|
|
85
|
+
ready();
|
|
86
|
+
own();
|
|
87
|
+
for (const i of rows)
|
|
88
|
+
gl.drawArrays(gl.POINTS, i, 1);
|
|
89
|
+
const [r2, g2, b2, a2 = 1] = rgba(ctx, outline);
|
|
90
|
+
gl.uniform1f(u.hole, dpr);
|
|
91
|
+
gl.uniform4f(u.color, r2, g2, b2, a2);
|
|
92
|
+
for (const i of rows)
|
|
93
|
+
gl.drawArrays(gl.POINTS, i, 1);
|
|
94
|
+
},
|
|
95
|
+
hit: hit(view),
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
...base('dots', o, [x], [y]),
|
|
100
|
+
overhang,
|
|
101
|
+
rowsAlike: true,
|
|
102
|
+
prepare(view) {
|
|
103
|
+
const { ctx, dpr, ox, kx, oy, ky, theme, left, top, width, height } = view;
|
|
104
|
+
// A point further out than twice the overhang cannot reach the area its mark is cut to.
|
|
105
|
+
const out = 2 * overhang * dpr;
|
|
106
|
+
const circle = (path, i, grow = 0) => {
|
|
107
|
+
const px = ox + x[i] * kx;
|
|
108
|
+
const py = oy - y[i] * ky;
|
|
109
|
+
if (px < left - out || px > left + width + out || py < top - out || py > top + height + out)
|
|
110
|
+
return;
|
|
111
|
+
const radius = Math.max(0, r[i] * dpr + grow);
|
|
112
|
+
path.moveTo(px + radius, py);
|
|
113
|
+
path.arc(px, py, radius, 0, TAU);
|
|
114
|
+
};
|
|
115
|
+
const path = new Path2D();
|
|
116
|
+
for (let i = 0; i < n; i++)
|
|
117
|
+
circle(path, i);
|
|
118
|
+
const fill = (shape, a) => {
|
|
119
|
+
ctx.globalAlpha = alpha * a;
|
|
120
|
+
ctx.fillStyle = o.ring ? (o.fill ?? theme.surface) : color;
|
|
121
|
+
ctx.fill(shape);
|
|
122
|
+
if (!o.ring)
|
|
123
|
+
return;
|
|
124
|
+
ctx.strokeStyle = color;
|
|
125
|
+
ctx.lineWidth = o.ring * dpr;
|
|
126
|
+
ctx.stroke(shape);
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
paint: a => fill(path, a),
|
|
130
|
+
select(rows, outline) {
|
|
131
|
+
const some = new Path2D();
|
|
132
|
+
const edge = new Path2D();
|
|
133
|
+
for (const i of rows) {
|
|
134
|
+
circle(some, i);
|
|
135
|
+
circle(edge, i, ((o.ring ?? 0) / 2) * dpr);
|
|
136
|
+
}
|
|
137
|
+
fill(some, 1);
|
|
138
|
+
ctx.save();
|
|
139
|
+
ctx.clip(edge);
|
|
140
|
+
ctx.globalAlpha = 1;
|
|
141
|
+
ctx.strokeStyle = outline;
|
|
142
|
+
ctx.lineWidth = 2 * dpr;
|
|
143
|
+
ctx.stroke(edge);
|
|
144
|
+
ctx.restore();
|
|
145
|
+
},
|
|
146
|
+
hit: hit(view),
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
gl: o.ring ? undefined : { layer: glLayer, from: GL_FROM, prepare: prepareGl },
|
|
150
|
+
release(layer) {
|
|
151
|
+
const buffer = layer && buffers.get(layer.gl);
|
|
152
|
+
if (!buffer)
|
|
153
|
+
return;
|
|
154
|
+
layer.gl.deleteBuffer(buffer);
|
|
155
|
+
buffers.delete(layer.gl);
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
import type { Common } from './common.js';
|
|
3
|
+
/** A line from y1 to y2 at each x with a cap at both ends, or from x1 to x2 at each y. */
|
|
4
|
+
export type ErrorBarsOptions = Common & {
|
|
5
|
+
/** The line's width in CSS pixels. @default 1 */
|
|
6
|
+
width?: number;
|
|
7
|
+
/** Half the length of each cap, in CSS pixels. @default 4 */
|
|
8
|
+
cap?: number;
|
|
9
|
+
} & ({
|
|
10
|
+
/** Where each upright bar stands. */
|
|
11
|
+
x: Col;
|
|
12
|
+
/** One end of each upright bar. */
|
|
13
|
+
y1: Col;
|
|
14
|
+
/** The other end of each upright bar. */
|
|
15
|
+
y2: Col;
|
|
16
|
+
} | {
|
|
17
|
+
/** Where each level bar stands. */
|
|
18
|
+
y: Col;
|
|
19
|
+
/** One end of each level bar. */
|
|
20
|
+
x1: Col;
|
|
21
|
+
/** The other end of each level bar. */
|
|
22
|
+
x2: Col;
|
|
23
|
+
});
|
|
24
|
+
export declare function errorBars(o: ErrorBarsOptions): Mark;
|