swiftplot 0.0.0-stage → 0.1.1
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 +327 -2
- package/dist/chart.d.ts +3 -0
- package/dist/chart.js +1226 -0
- package/dist/charts/controller.d.ts +32 -0
- package/dist/charts/controller.js +231 -0
- package/dist/charts/defaults.d.ts +12 -0
- package/dist/charts/defaults.js +60 -0
- package/dist/charts/fields.d.ts +34 -0
- package/dist/charts/fields.js +167 -0
- package/dist/charts/index.d.ts +6 -0
- package/dist/charts/index.js +3 -0
- package/dist/charts/mount.d.ts +5 -0
- package/dist/charts/mount.js +21 -0
- package/dist/charts/source.d.ts +24 -0
- package/dist/charts/source.js +225 -0
- package/dist/charts/types.d.ts +104 -0
- package/dist/charts/types.js +1 -0
- package/dist/charts/xy.d.ts +36 -0
- package/dist/charts/xy.js +220 -0
- package/dist/features/animate.d.ts +11 -0
- package/dist/features/animate.js +118 -0
- package/dist/features/brush.d.ts +13 -0
- package/dist/features/brush.js +127 -0
- package/dist/features/legend.d.ts +15 -0
- package/dist/features/legend.js +237 -0
- package/dist/features/pointer.d.ts +29 -0
- package/dist/features/pointer.js +23 -0
- package/dist/features/tooltip.d.ts +14 -0
- package/dist/features/tooltip.js +190 -0
- package/dist/features/zoom.d.ts +13 -0
- package/dist/features/zoom.js +228 -0
- package/dist/gl.d.ts +5 -0
- package/dist/gl.js +52 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +31 -0
- package/dist/marks/arcs.d.ts +25 -0
- package/dist/marks/arcs.js +124 -0
- package/dist/marks/area.d.ts +24 -0
- package/dist/marks/area.js +106 -0
- package/dist/marks/bar.d.ts +44 -0
- package/dist/marks/bar.js +243 -0
- package/dist/marks/box.d.ts +27 -0
- package/dist/marks/box.js +188 -0
- package/dist/marks/common.d.ts +84 -0
- package/dist/marks/common.js +236 -0
- package/dist/marks/dots.d.ts +15 -0
- package/dist/marks/dots.js +156 -0
- package/dist/marks/errorBars.d.ts +19 -0
- package/dist/marks/errorBars.js +79 -0
- package/dist/marks/funnel.d.ts +25 -0
- package/dist/marks/funnel.js +121 -0
- package/dist/marks/heatmap.d.ts +19 -0
- package/dist/marks/heatmap.js +139 -0
- package/dist/marks/line.d.ts +19 -0
- package/dist/marks/line.js +75 -0
- package/dist/marks/radar.d.ts +22 -0
- package/dist/marks/radar.js +155 -0
- package/dist/marks/radialBars.d.ts +27 -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 +28 -0
- package/dist/marks/rule.js +80 -0
- package/dist/marks/sankey.d.ts +41 -0
- package/dist/marks/sankey.js +366 -0
- package/dist/marks/sunburst.d.ts +26 -0
- package/dist/marks/sunburst.js +185 -0
- package/dist/marks/ticks.d.ts +9 -0
- package/dist/marks/ticks.js +26 -0
- package/dist/marks/tree.d.ts +41 -0
- package/dist/marks/tree.js +110 -0
- package/dist/marks/treemap.d.ts +25 -0
- package/dist/marks/treemap.js +159 -0
- package/dist/paint.d.ts +30 -0
- package/dist/paint.js +66 -0
- package/dist/scale.d.ts +16 -0
- package/dist/scale.js +96 -0
- package/dist/scales/band.d.ts +9 -0
- package/dist/scales/band.js +23 -0
- package/dist/scales/bare.d.ts +3 -0
- package/dist/scales/bare.js +7 -0
- package/dist/scales/color.d.ts +18 -0
- package/dist/scales/color.js +21 -0
- package/dist/scales/log.d.ts +20 -0
- package/dist/scales/log.js +97 -0
- package/dist/scales/time.d.ts +30 -0
- package/dist/scales/time.js +181 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +8 -0
- package/dist/transforms.d.ts +43 -0
- package/dist/transforms.js +118 -0
- package/dist/types.d.ts +434 -0
- package/dist/types.js +1 -0
- package/package.json +39 -3
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
import type { Common } from './common.js';
|
|
3
|
+
/**
|
|
4
|
+
* A line from y1 to y2 at each x with a cap at both ends, or from x1 to x2 at each y.
|
|
5
|
+
* `cap` is half the cap's length and `width` the line width, both in CSS pixels.
|
|
6
|
+
*/
|
|
7
|
+
export type ErrorBarsOptions = Common & {
|
|
8
|
+
width?: number;
|
|
9
|
+
cap?: number;
|
|
10
|
+
} & ({
|
|
11
|
+
x: Col;
|
|
12
|
+
y1: Col;
|
|
13
|
+
y2: Col;
|
|
14
|
+
} | {
|
|
15
|
+
y: Col;
|
|
16
|
+
x1: Col;
|
|
17
|
+
x2: Col;
|
|
18
|
+
});
|
|
19
|
+
export declare function errorBars(o: ErrorBarsOptions): Mark;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { glLayer } from '../gl.js';
|
|
2
|
+
import { base } from './common.js';
|
|
3
|
+
import { gpuRows } from './rects.js';
|
|
4
|
+
export function errorBars(o) {
|
|
5
|
+
const { color, alpha = 1, width = 1, cap = 4 } = o;
|
|
6
|
+
const upright = 'x' in o;
|
|
7
|
+
// `at` is the position the bar stands on; `from` and `to` are its ends along the other axis.
|
|
8
|
+
const [at, from, to] = upright ? [o.x, o.y1, o.y2] : [o.y, o.x1, o.x2];
|
|
9
|
+
const rows = upright ? gpuRows(new WeakMap(), [at], [from, to]) : gpuRows(new WeakMap(), [from, to], [at]);
|
|
10
|
+
return {
|
|
11
|
+
...base('errorBars', o, upright ? [at] : [from, to], upright ? [from, to] : [at]),
|
|
12
|
+
oneShape: true,
|
|
13
|
+
overhang: Math.max(cap, width / 2),
|
|
14
|
+
prepare({ ctx, dpr, ox, kx, oy, ky }) {
|
|
15
|
+
const c = cap * dpr;
|
|
16
|
+
const path = new Path2D();
|
|
17
|
+
for (let i = 0; i < at.length; i++) {
|
|
18
|
+
// Canvas ignores a moveTo with a NaN in it, and the next lineTo would then join two rows.
|
|
19
|
+
if (!Number.isFinite(at[i]) || !Number.isFinite(from[i]) || !Number.isFinite(to[i]))
|
|
20
|
+
continue;
|
|
21
|
+
// A stem between the two ends, and a cap across each end.
|
|
22
|
+
if (upright) {
|
|
23
|
+
const px = ox + at[i] * kx;
|
|
24
|
+
const [a, b] = [oy - from[i] * ky, oy - to[i] * ky];
|
|
25
|
+
path.moveTo(px, a);
|
|
26
|
+
path.lineTo(px, b);
|
|
27
|
+
path.moveTo(px - c, a);
|
|
28
|
+
path.lineTo(px + c, a);
|
|
29
|
+
path.moveTo(px - c, b);
|
|
30
|
+
path.lineTo(px + c, b);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const py = oy - at[i] * ky;
|
|
34
|
+
const [a, b] = [ox + from[i] * kx, ox + to[i] * kx];
|
|
35
|
+
path.moveTo(a, py);
|
|
36
|
+
path.lineTo(b, py);
|
|
37
|
+
path.moveTo(a, py - c);
|
|
38
|
+
path.lineTo(a, py + c);
|
|
39
|
+
path.moveTo(b, py - c);
|
|
40
|
+
path.lineTo(b, py + c);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
paint(a) {
|
|
45
|
+
ctx.globalAlpha = alpha * a;
|
|
46
|
+
ctx.lineWidth = width * dpr;
|
|
47
|
+
ctx.strokeStyle = color;
|
|
48
|
+
ctx.stroke(path);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
gl: {
|
|
53
|
+
layer: glLayer,
|
|
54
|
+
under: true,
|
|
55
|
+
prepare(view, { gl }) {
|
|
56
|
+
const { draw, once } = rows.on(view, gl, color, Math.max(cap, width / 2));
|
|
57
|
+
// A line keeps a device pixel of width and is fainter instead, or it would fall between pixels.
|
|
58
|
+
const [half, c] = [Math.max(width * view.dpr, 1) / 2, cap * view.dpr];
|
|
59
|
+
const thin = Math.min(width * view.dpr, 1);
|
|
60
|
+
const parts = upright
|
|
61
|
+
? [
|
|
62
|
+
{ xa: 0, xb: 0, ya: 1, yb: 2, pad: [half, 0] },
|
|
63
|
+
{ xa: 0, xb: 0, ya: 1, yb: 1, pad: [c, half] },
|
|
64
|
+
{ xa: 0, xb: 0, ya: 2, yb: 2, pad: [c, half] },
|
|
65
|
+
]
|
|
66
|
+
: [
|
|
67
|
+
{ xa: 0, xb: 1, ya: 2, yb: 2, pad: [0, half] },
|
|
68
|
+
{ xa: 0, xb: 0, ya: 2, yb: 2, pad: [half, c] },
|
|
69
|
+
{ xa: 1, xb: 1, ya: 2, yb: 2, pad: [half, c] },
|
|
70
|
+
];
|
|
71
|
+
return { paint: a => once(() => parts.forEach(part => draw(part, alpha * a * thin))) };
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
release(layer) {
|
|
75
|
+
if (layer)
|
|
76
|
+
rows.release(layer.gl);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Gradient } from '../paint.js';
|
|
2
|
+
import type { Col, Mark } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Stages of a funnel from top to bottom, for a blank frame. All stages are equally tall and each
|
|
5
|
+
* is as wide as its value against the largest; it narrows to the width of the stage below. A
|
|
6
|
+
* value that is negative or not a number counts as 0. Colours repeat when there are fewer than
|
|
7
|
+
* stages. `labels` name the stages for a legend.
|
|
8
|
+
*/
|
|
9
|
+
export type FunnelOptions = {
|
|
10
|
+
values: Col;
|
|
11
|
+
colors: string[];
|
|
12
|
+
labels?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* Text on each stage: true for its name and its share of the first stage, or your own. It is
|
|
15
|
+
* written inside the stage in white, or beside it where it does not fit.
|
|
16
|
+
*/
|
|
17
|
+
text?: true | ((row: number, share: number) => string);
|
|
18
|
+
/** One blend over the whole funnel in place of a colour per stage; `colors` still name the stages in a legend. */
|
|
19
|
+
fill?: Gradient;
|
|
20
|
+
alpha?: number;
|
|
21
|
+
key?: string;
|
|
22
|
+
label?: string;
|
|
23
|
+
group?: string;
|
|
24
|
+
};
|
|
25
|
+
export declare function funnel(o: FunnelOptions): Mark;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { painter } from '../paint.js';
|
|
2
|
+
export function funnel(o) {
|
|
3
|
+
const { values, colors, alpha = 1 } = o;
|
|
4
|
+
const n = values.length;
|
|
5
|
+
const worth = (i) => (values[i] > 0 && Number.isFinite(values[i]) ? values[i] : 0);
|
|
6
|
+
let most = 0;
|
|
7
|
+
for (let i = 0; i < n; i++)
|
|
8
|
+
most = Math.max(most, worth(i));
|
|
9
|
+
// Each stage's width as a share of the widest, and its value as a share of the first stage's.
|
|
10
|
+
const wide = Float64Array.from({ length: n }, (_, i) => worth(i) / (most || 1));
|
|
11
|
+
const share = (i) => (worth(0) ? worth(i) / worth(0) : 0);
|
|
12
|
+
const shade = (i) => colors[i % colors.length] ?? '#888';
|
|
13
|
+
const said = o.text === true
|
|
14
|
+
? (i, s) => `${o.labels?.[i] ?? ''} ${Math.round(s * 100)}%`.trim()
|
|
15
|
+
: o.text;
|
|
16
|
+
return {
|
|
17
|
+
type: 'funnel',
|
|
18
|
+
key: o.key,
|
|
19
|
+
label: o.label,
|
|
20
|
+
group: o.group ?? o.label,
|
|
21
|
+
color: shade(0),
|
|
22
|
+
options: o,
|
|
23
|
+
points: n,
|
|
24
|
+
channels: { x: [], y: [] },
|
|
25
|
+
reveal: 'down',
|
|
26
|
+
entries: o.labels?.slice(0, n).map((label, i) => ({ label, color: shade(i) })),
|
|
27
|
+
prepare({ ctx, dpr, left, top, width, height }) {
|
|
28
|
+
const cx = left + width / 2;
|
|
29
|
+
const tall = height / (n || 1);
|
|
30
|
+
// A pixel of space under each stage but the last.
|
|
31
|
+
const gap = Math.min(dpr, tall / 4);
|
|
32
|
+
const half = (i) => (wide[Math.min(i, n - 1)] * width) / 2;
|
|
33
|
+
// The sides are one smooth curve through the stage widths that never bulges past them:
|
|
34
|
+
// its lean where two stages meet is the harmonic mean of the leans either side, and
|
|
35
|
+
// upright where the widths turn. A stage runs from the top of its band to the gap under
|
|
36
|
+
// it. Paint, hit and the room for text all read this.
|
|
37
|
+
const drop = (i) => (i < 0 || i >= n ? 0 : half(i + 1) - half(i));
|
|
38
|
+
const lean = (i) => {
|
|
39
|
+
const [a, b] = [drop(i - 1), drop(i)];
|
|
40
|
+
if (!i)
|
|
41
|
+
return (3 * b - lean(1)) / 2;
|
|
42
|
+
return a * b > 0 ? (2 * a * b) / (a + b) : 0;
|
|
43
|
+
};
|
|
44
|
+
const span = (i) => [top + i * tall, top + (i + 1) * tall - (i < n - 1 ? gap : 0)];
|
|
45
|
+
const reach = (i, y) => {
|
|
46
|
+
const [y0, y1] = span(i);
|
|
47
|
+
const u = Math.min(1, Math.max(0, (y - y0) / (y1 - y0 || 1)));
|
|
48
|
+
const [uu, back] = [u * u, 1 - u];
|
|
49
|
+
return (half(i) * (1 + 2 * u) * back * back +
|
|
50
|
+
lean(i) * u * back * back +
|
|
51
|
+
half(i + 1) * uu * (3 - 2 * u) -
|
|
52
|
+
lean(i + 1) * uu * back);
|
|
53
|
+
};
|
|
54
|
+
// Stages less than a device pixel tall are drawn several to a straight-sided band a pixel
|
|
55
|
+
// tall, in its first stage's colour: more shapes than pixels cost time and show nothing.
|
|
56
|
+
const per = tall && tall < 1 ? Math.ceil(1 / tall) : 1;
|
|
57
|
+
const stages = [];
|
|
58
|
+
for (let i = 0; i < n; i += per) {
|
|
59
|
+
const to = Math.min(i + per, n);
|
|
60
|
+
const [y0, y1] = [span(i)[0], span(to - 1)[1]];
|
|
61
|
+
const path = new Path2D();
|
|
62
|
+
stages.push({ path, from: i, to });
|
|
63
|
+
path.moveTo(cx - half(i), y0);
|
|
64
|
+
path.lineTo(cx + half(i), y0);
|
|
65
|
+
if (per > 1) {
|
|
66
|
+
path.lineTo(cx + half(to), y1);
|
|
67
|
+
path.lineTo(cx - half(to), y1);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
// Control points a third and two thirds of the way down make the curve exactly reach().
|
|
71
|
+
const [a, b] = [y0 + (y1 - y0) / 3, y1 - (y1 - y0) / 3];
|
|
72
|
+
const [out, into] = [half(i) + lean(i) / 3, half(i + 1) - lean(i + 1) / 3];
|
|
73
|
+
path.bezierCurveTo(cx + out, a, cx + into, b, cx + half(i + 1), y1);
|
|
74
|
+
path.lineTo(cx - half(i + 1), y1);
|
|
75
|
+
path.bezierCurveTo(cx - into, b, cx - out, a, cx - half(i), y0);
|
|
76
|
+
path.closePath();
|
|
77
|
+
}
|
|
78
|
+
const ink = o.fill && painter(ctx, o.fill, shade(0))([left, top, left + width, top + height]);
|
|
79
|
+
const labels = [];
|
|
80
|
+
for (let i = 0; said && i < n && most; i++) {
|
|
81
|
+
const [y0, y1] = span(i);
|
|
82
|
+
const y = (y0 + y1) / 2;
|
|
83
|
+
if (y1 - y0 < 16 * dpr)
|
|
84
|
+
continue;
|
|
85
|
+
const text = said(i, share(i));
|
|
86
|
+
if (!text)
|
|
87
|
+
continue;
|
|
88
|
+
// The stage's half-width at the top and at the bottom of the text. Inside where even the
|
|
89
|
+
// narrower leaves room; otherwise to the right of the wider, clear of the slanted side.
|
|
90
|
+
const [above, below] = [reach(i, y - 8 * dpr), reach(i, y + 8 * dpr)];
|
|
91
|
+
if (text.length * 7 * dpr <= 2 * Math.min(above, below) * 0.9) {
|
|
92
|
+
labels.push({ x: cx, y, text, color: '#fff', priority: n - i });
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
labels.push({ x: cx + Math.max(above, below) + 8 * dpr, y, text, align: 'start', priority: n - i });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
labels,
|
|
100
|
+
paint(k, row, rest = 1) {
|
|
101
|
+
for (const { path, from, to } of stages) {
|
|
102
|
+
ctx.globalAlpha = alpha * k * (row === undefined || (row >= from && row < to) ? 1 : rest);
|
|
103
|
+
ctx.fillStyle = ink || shade(from);
|
|
104
|
+
ctx.fill(path);
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
// The stage whose band the pointer is in, if it is within the stage's slanted sides.
|
|
108
|
+
hit(px, py, max) {
|
|
109
|
+
const [hx, hy] = [px * dpr, py * dpr];
|
|
110
|
+
const row = Math.floor((hy - top) / tall);
|
|
111
|
+
if (!(row >= 0 && row < n) || !most || hy >= span(row)[1])
|
|
112
|
+
return null;
|
|
113
|
+
const across = reach(row, hy);
|
|
114
|
+
if (Math.abs(hx - cx) > across || !across)
|
|
115
|
+
return null;
|
|
116
|
+
return { x: 0.5, y: 1 - (row + 0.5) / n, distance: max, row };
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ColorScale } from '../scales/color.js';
|
|
2
|
+
import type { Col, Mark } from '../types.js';
|
|
3
|
+
import type { Common } from './common.js';
|
|
4
|
+
/**
|
|
5
|
+
* A grid of cells coloured by value. `values` runs along x first: the cell in column c of row r
|
|
6
|
+
* is values[r * columns + c], and row 0 is at the bottom. By default cell (c, r) is centred on
|
|
7
|
+
* x = c and y = r, which is where a band axis puts its categories; `x` and `y` give the grid's
|
|
8
|
+
* edges instead, from the first cell's to the last one's, in either order. A cell whose value is
|
|
9
|
+
* not finite is left empty. Where several cells share a pixel, the pixel shows and answers for
|
|
10
|
+
* the largest of them, so a peak is never lost.
|
|
11
|
+
*/
|
|
12
|
+
export type HeatmapOptions = Omit<Common, 'color'> & {
|
|
13
|
+
values: Col;
|
|
14
|
+
columns: number;
|
|
15
|
+
x?: [number, number];
|
|
16
|
+
y?: [number, number];
|
|
17
|
+
color?: ColorScale;
|
|
18
|
+
};
|
|
19
|
+
export declare function heatmap(o: HeatmapOptions): Mark;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { extent } from '../scale.js';
|
|
2
|
+
import { sequential } from '../scales/color.js';
|
|
3
|
+
export function heatmap(o) {
|
|
4
|
+
const { values, alpha = 1, color = sequential() } = o;
|
|
5
|
+
const columns = Number.isFinite(o.columns) ? Math.max(1, Math.floor(o.columns)) : 1;
|
|
6
|
+
const rows = Math.ceil(values.length / columns);
|
|
7
|
+
const [x0, x1] = o.x ?? [-0.5, columns - 0.5];
|
|
8
|
+
const [y0, y1] = o.y ?? [-0.5, rows - 0.5];
|
|
9
|
+
// Found when first asked for, by the legend or the first paint, whichever comes first.
|
|
10
|
+
let found;
|
|
11
|
+
const domain = () => (found ??= color.domain ?? extent([values]));
|
|
12
|
+
return {
|
|
13
|
+
type: 'heatmap',
|
|
14
|
+
key: o.key,
|
|
15
|
+
label: o.label,
|
|
16
|
+
group: o.group ?? o.label,
|
|
17
|
+
color: color.stops.at(-1),
|
|
18
|
+
options: o,
|
|
19
|
+
points: values.length,
|
|
20
|
+
channels: { x: [[x0], [x1]], y: [[y0], [y1]] },
|
|
21
|
+
ramp() {
|
|
22
|
+
const [lo, hi] = domain();
|
|
23
|
+
return { table: color.table, lo, hi };
|
|
24
|
+
},
|
|
25
|
+
prepare({ ctx, dpr, ox, kx, oy, ky, left, top, width, height }) {
|
|
26
|
+
// Column c starts at ax + c * bx in device pixels, row r at ay + r * by.
|
|
27
|
+
const [ax, bx] = [ox + x0 * kx, ((x1 - x0) / columns) * kx];
|
|
28
|
+
const [ay, by] = [oy - y0 * ky, (-(y1 - y0) / (rows || 1)) * ky];
|
|
29
|
+
// Only the cells that reach into the plot are drawn, however large the grid is.
|
|
30
|
+
const seen = (a, b, n, from, size) => {
|
|
31
|
+
const [p, q] = b ? [(from - a) / b, (from + size - a) / b] : [0, -1];
|
|
32
|
+
return [
|
|
33
|
+
Math.max(0, Math.floor(Math.min(p, q))),
|
|
34
|
+
Math.min(n - 1, Math.ceil(Math.max(p, q)) - 1),
|
|
35
|
+
];
|
|
36
|
+
};
|
|
37
|
+
const [c0, c1] = seen(ax, bx, columns, left, width);
|
|
38
|
+
const [r0, r1] = seen(ay, by, rows, top, height);
|
|
39
|
+
const some = c1 >= c0 && r1 >= r0;
|
|
40
|
+
// Where those cells lie, and the picture that is drawn over it. Along an axis where cells are
|
|
41
|
+
// at least a pixel, that is a pixel per cell, stretched. Where they are smaller it is exactly
|
|
42
|
+
// the device pixels they cover, drawn one for one: a stretch there could skip a pixel's peak.
|
|
43
|
+
const along = (a, b, lo, hi) => {
|
|
44
|
+
const [start, size, cells] = [
|
|
45
|
+
Math.min(a + lo * b, a + (hi + 1) * b),
|
|
46
|
+
Math.abs((hi + 1 - lo) * b),
|
|
47
|
+
hi - lo + 1,
|
|
48
|
+
];
|
|
49
|
+
if (size >= cells)
|
|
50
|
+
return [start, size, Math.max(1, cells)];
|
|
51
|
+
const first = Math.floor(start);
|
|
52
|
+
const pixels = Math.max(1, Math.ceil(start + size) - first);
|
|
53
|
+
return [first, pixels, pixels];
|
|
54
|
+
};
|
|
55
|
+
const [dl, dw, w] = along(ax, bx, c0, c1);
|
|
56
|
+
const [dt, dh, h] = along(ay, by, r0, r1);
|
|
57
|
+
const pixel = (at, start, size, n) => Math.max(0, Math.min(n - 1, Math.floor(((at - start) / size) * n)));
|
|
58
|
+
// For each pixel of the picture, the cell it shows: the one with the largest value there.
|
|
59
|
+
const shows = new Int32Array(some ? w * h : 0).fill(-1);
|
|
60
|
+
for (let r = r0; some && r <= r1; r++) {
|
|
61
|
+
const ty = pixel(ay + (r + 0.5) * by, dt, dh, h) * w;
|
|
62
|
+
for (let c = c0, i = r * columns + c0; c <= c1 && i < values.length; c++, i++) {
|
|
63
|
+
const v = values[i];
|
|
64
|
+
if (!Number.isFinite(v))
|
|
65
|
+
continue;
|
|
66
|
+
const t = ty + pixel(ax + (c + 0.5) * bx, dl, dw, w);
|
|
67
|
+
if (shows[t] < 0 || v > values[shows[t]])
|
|
68
|
+
shows[t] = i;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
let picture;
|
|
72
|
+
if (some) {
|
|
73
|
+
const [lo, hi] = domain();
|
|
74
|
+
picture = new OffscreenCanvas(w, h);
|
|
75
|
+
const c = picture.getContext('2d');
|
|
76
|
+
const image = c.createImageData(w, h);
|
|
77
|
+
const pixels = new Uint32Array(image.data.buffer);
|
|
78
|
+
// Halved before subtracting only where the plain difference overflows: for a tiny domain
|
|
79
|
+
// the halves would both round to zero.
|
|
80
|
+
const [span, half] = [hi - lo, hi / 2 - lo / 2];
|
|
81
|
+
shows.forEach((i, p) => {
|
|
82
|
+
if (i < 0)
|
|
83
|
+
return;
|
|
84
|
+
const v = values[i];
|
|
85
|
+
const t = !span ? 0 : Number.isFinite(span) ? (v - lo) / span : (v / 2 - lo / 2) / half;
|
|
86
|
+
pixels[p] = color.table[Math.round(Math.max(0, Math.min(1, t)) * 255)];
|
|
87
|
+
});
|
|
88
|
+
c.putImageData(image, 0, 0);
|
|
89
|
+
}
|
|
90
|
+
const draw = (k) => {
|
|
91
|
+
ctx.globalAlpha = alpha * k;
|
|
92
|
+
ctx.imageSmoothingEnabled = false;
|
|
93
|
+
ctx.drawImage(picture, dl, dt, dw, dh);
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
paint(k, row, rest = 1) {
|
|
97
|
+
if (!picture)
|
|
98
|
+
return;
|
|
99
|
+
const c = (row ?? -1) % columns;
|
|
100
|
+
const r = Math.floor((row ?? -1) / columns);
|
|
101
|
+
if (row === undefined || c < c0 || c > c1 || r < r0 || r > r1)
|
|
102
|
+
return draw(k);
|
|
103
|
+
// The pixel of the picture that holds the focused cell, at full strength; the rest dimmed.
|
|
104
|
+
const one = new Path2D();
|
|
105
|
+
one.rect(dl + (pixel(ax + (c + 0.5) * bx, dl, dw, w) * dw) / w, dt + (pixel(ay + (r + 0.5) * by, dt, dh, h) * dh) / h, dw / w, dh / h);
|
|
106
|
+
const around = new Path2D(one);
|
|
107
|
+
around.rect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
108
|
+
for (const [clip, strength] of [
|
|
109
|
+
[one, k],
|
|
110
|
+
[around, k * rest],
|
|
111
|
+
]) {
|
|
112
|
+
ctx.save();
|
|
113
|
+
ctx.clip(clip, 'evenodd');
|
|
114
|
+
draw(strength);
|
|
115
|
+
ctx.restore();
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
// The pixel of the picture under the pointer answers with the cell it shows. A filled
|
|
119
|
+
// shape, so the weakest hit.
|
|
120
|
+
hit(px, py, reach) {
|
|
121
|
+
const [hx, hy] = [px * dpr, py * dpr];
|
|
122
|
+
if (hx < left || hx > left + width || hy < top || hy > top + height)
|
|
123
|
+
return null;
|
|
124
|
+
if (!picture || hx < dl || hx > dl + dw || hy < dt || hy > dt + dh)
|
|
125
|
+
return null;
|
|
126
|
+
const row = shows[pixel(hy, dt, dh, h) * w + pixel(hx, dl, dw, w)];
|
|
127
|
+
if (row < 0)
|
|
128
|
+
return null;
|
|
129
|
+
return {
|
|
130
|
+
x: x0 + (((row % columns) + 0.5) / columns) * (x1 - x0),
|
|
131
|
+
y: y0 + ((Math.floor(row / columns) + 0.5) / rows) * (y1 - y0),
|
|
132
|
+
distance: reach,
|
|
133
|
+
row,
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
import { type Paint } from '../paint.js';
|
|
3
|
+
import type { Common, Curve } from './common.js';
|
|
4
|
+
/**
|
|
5
|
+
* x must be ascending. `stroke` draws the line with something other than its colour: a blend
|
|
6
|
+
* across the plot area, say. `fill` shades what is under the line, down to the bottom of the plot
|
|
7
|
+
* area: `true` for a faint fade of the line's colour, or a paint of your own. `join` draws it straight
|
|
8
|
+
* across a value that is not finite, where it would otherwise leave a gap.
|
|
9
|
+
*/
|
|
10
|
+
export type LineOptions = Common & {
|
|
11
|
+
x: Col;
|
|
12
|
+
y: Col;
|
|
13
|
+
curve?: Curve;
|
|
14
|
+
width?: number;
|
|
15
|
+
stroke?: Paint;
|
|
16
|
+
fill?: Paint | boolean;
|
|
17
|
+
join?: boolean;
|
|
18
|
+
};
|
|
19
|
+
export declare function line(o: LineOptions): Mark;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { gradient, painter } from '../paint.js';
|
|
2
|
+
import { base, chains, columns, extent, joined, reach, sampleAt, smooth, span } from './common.js';
|
|
3
|
+
export function line(o) {
|
|
4
|
+
const { color, curve = 'linear', width = 1.5, alpha = 1 } = o;
|
|
5
|
+
const [[x, y], cuts] = o.join ? joined(o.x, o.y) : [[o.x, o.y]];
|
|
6
|
+
const end = o.join && !Number.isFinite(o.y[o.y.length - 1]) ? x[x.length - 1] : Infinity;
|
|
7
|
+
const step = curve === 'step';
|
|
8
|
+
const ys = o.fill ? extent([y]) : undefined;
|
|
9
|
+
return {
|
|
10
|
+
...base('line', o, [o.x], [o.y]),
|
|
11
|
+
oneShape: true,
|
|
12
|
+
overhang: width / 2,
|
|
13
|
+
prepare({ ctx, dpr, ox, kx, oy, ky, left, top, width: wide, height }) {
|
|
14
|
+
const rows = span(x, ox, kx, left - width * dpr, left + wide + width * dpr, curve === 'smooth' ? 2 : 1);
|
|
15
|
+
const c = columns(x, y, ox, kx, oy, ky, !step || o.join, undefined, rows, cuts);
|
|
16
|
+
const cx = step ? c.col : c.at;
|
|
17
|
+
const trace = (path, [a, b, kept], on = false) => {
|
|
18
|
+
const go = on ? 'lineTo' : 'moveTo';
|
|
19
|
+
if (curve === 'smooth' && kept === b - a) {
|
|
20
|
+
path[go](c.at[a], c.first[a]);
|
|
21
|
+
smooth(path, c.at.subarray(a, b), c.first.subarray(a, b), b - a);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
for (let i = a; i < b; i++) {
|
|
25
|
+
const px = cx[i];
|
|
26
|
+
if (i === a)
|
|
27
|
+
path[go](px, step && !on ? c.last[i] : c.first[i]);
|
|
28
|
+
else
|
|
29
|
+
path.lineTo(px, step ? c.last[i - 1] : c.first[i]);
|
|
30
|
+
if (c.lo[i] !== c.hi[i]) {
|
|
31
|
+
path.lineTo(px, c.lo[i]);
|
|
32
|
+
path.lineTo(px, c.hi[i]);
|
|
33
|
+
}
|
|
34
|
+
path.lineTo(px, c.last[i]);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const path = new Path2D();
|
|
38
|
+
const groups = chains(c.runs);
|
|
39
|
+
for (const runs of groups)
|
|
40
|
+
runs.forEach((run, i) => trace(path, run, i > 0));
|
|
41
|
+
const ink = painter(ctx, o.stroke, color)([left, top, left + wide, top + height]);
|
|
42
|
+
let under;
|
|
43
|
+
let shade = color;
|
|
44
|
+
if (o.fill && c.n) {
|
|
45
|
+
const floor = top + height;
|
|
46
|
+
under = new Path2D();
|
|
47
|
+
for (const runs of groups) {
|
|
48
|
+
runs.forEach((run, i) => trace(under, run, i > 0));
|
|
49
|
+
under.lineTo(cx[runs[runs.length - 1][1] - 1], floor);
|
|
50
|
+
under.lineTo(cx[runs[0][0]], floor);
|
|
51
|
+
under.closePath();
|
|
52
|
+
}
|
|
53
|
+
const high = Math.min(floor, reach(ys, [y], oy, ky)[0]);
|
|
54
|
+
shade = painter(ctx, o.fill === true ? gradient(0.18, 0) : o.fill, color)([left, high, left + wide, floor]);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
paint(a) {
|
|
58
|
+
ctx.globalAlpha = alpha * a;
|
|
59
|
+
if (under) {
|
|
60
|
+
ctx.fillStyle = shade;
|
|
61
|
+
ctx.fill(under);
|
|
62
|
+
}
|
|
63
|
+
ctx.lineWidth = width * dpr;
|
|
64
|
+
ctx.strokeStyle = ink;
|
|
65
|
+
ctx.stroke(path);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
sampleAtX: (v, straight) =>
|
|
70
|
+
// A step holds its last value past the end, which a trailing gap left out by `join` must not.
|
|
71
|
+
v > end
|
|
72
|
+
? undefined
|
|
73
|
+
: sampleAt(x, y, v, straight && curve === 'smooth' ? 'linear' : curve, undefined, cuts),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Col, Mark } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A radar chart for a polar frame: `marks: radar({ axes, series })`. It gives one mark for the
|
|
4
|
+
* rings, the spokes and their names, then one per series, so series are named, dimmed and hit
|
|
5
|
+
* like any other marks, and a hit's row is the spoke. Spokes start at twelve o'clock and run
|
|
6
|
+
* clockwise. The rim stands for `max`, or for the largest value rounded up to a whole step. A
|
|
7
|
+
* value that is negative or not a number sits at the centre, and one above `max` on the rim.
|
|
8
|
+
*/
|
|
9
|
+
export type RadarOptions = {
|
|
10
|
+
axes: string[];
|
|
11
|
+
series: {
|
|
12
|
+
values: Col;
|
|
13
|
+
color: string;
|
|
14
|
+
label?: string;
|
|
15
|
+
group?: string;
|
|
16
|
+
key?: string;
|
|
17
|
+
}[];
|
|
18
|
+
max?: number;
|
|
19
|
+
/** The opacity of a series' filling; 0.15 by default. */
|
|
20
|
+
fill?: number;
|
|
21
|
+
};
|
|
22
|
+
export declare function radar(o: RadarOptions): Mark[];
|