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,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The columns of a nested tree, or of several. Nodes that share a name stay separate nodes.
|
|
3
|
+
* @param tree A root, or a list of roots.
|
|
4
|
+
*/
|
|
5
|
+
export function fromTree(tree) {
|
|
6
|
+
const [nodes, parent, value] = [[], [], []];
|
|
7
|
+
// An object met again under itself would never end; the same object in two branches is fine.
|
|
8
|
+
const under = new Set();
|
|
9
|
+
const todo = [tree]
|
|
10
|
+
.flat()
|
|
11
|
+
.map((root) => [root, -1])
|
|
12
|
+
.reverse();
|
|
13
|
+
for (let next = todo.pop(); next; next = todo.pop()) {
|
|
14
|
+
const [node, above, leave] = next;
|
|
15
|
+
if (leave) {
|
|
16
|
+
under.delete(node);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (under.has(node))
|
|
20
|
+
throw new Error(`fromTree: ${node.name} is among its own children`);
|
|
21
|
+
under.add(node);
|
|
22
|
+
const at = nodes.push(node.name) - 1;
|
|
23
|
+
parent.push(above);
|
|
24
|
+
value.push(node.value ?? 0);
|
|
25
|
+
todo.push([node, above, true]);
|
|
26
|
+
for (const child of [...(node.children ?? [])].reverse())
|
|
27
|
+
todo.push([child, at]);
|
|
28
|
+
}
|
|
29
|
+
return { nodes, parent, value };
|
|
30
|
+
}
|
|
31
|
+
/** The `count` first of `list` in the order `before` gives, without sorting all of it. */
|
|
32
|
+
export function first(list, count, before) {
|
|
33
|
+
const best = [];
|
|
34
|
+
for (const i of list) {
|
|
35
|
+
if (best.length === count && before(i, best[count - 1]) > 0)
|
|
36
|
+
continue;
|
|
37
|
+
let [at, end] = [0, best.length];
|
|
38
|
+
while (at < end) {
|
|
39
|
+
const mid = (at + end) >> 1;
|
|
40
|
+
if (before(i, best[mid]) < 0)
|
|
41
|
+
end = mid;
|
|
42
|
+
else
|
|
43
|
+
at = mid + 1;
|
|
44
|
+
}
|
|
45
|
+
best.splice(at, 0, i);
|
|
46
|
+
if (best.length > count)
|
|
47
|
+
best.pop();
|
|
48
|
+
}
|
|
49
|
+
return best;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* What both tree marks lay out. Sizes are shares of the largest value, so that sums of them
|
|
53
|
+
* cannot overflow. `tint` numbers the branches that get a colour each: the roots, or the
|
|
54
|
+
* children of a single root, which itself has -1 unless it is the only node. Nothing here recurses, so a tree may be as
|
|
55
|
+
* deep as it likes. A parent out of range, a node that is its own parent and a cycle throw.
|
|
56
|
+
*/
|
|
57
|
+
export function grow({ nodes, parent, value }, who) {
|
|
58
|
+
const n = nodes.length;
|
|
59
|
+
const named = (i) => nodes[i] ?? `#${i}`;
|
|
60
|
+
const kids = nodes.map(() => []);
|
|
61
|
+
const roots = [];
|
|
62
|
+
for (let i = 0; i < n; i++) {
|
|
63
|
+
const p = parent[i];
|
|
64
|
+
if (p < 0)
|
|
65
|
+
roots.push(i);
|
|
66
|
+
else if (!Number.isInteger(p) || p >= n)
|
|
67
|
+
throw new Error(`${who}: ${named(i)} has parent ${p}, and there are ${n} nodes`);
|
|
68
|
+
else if (p === i)
|
|
69
|
+
throw new Error(`${who}: ${named(i)} is its own parent`);
|
|
70
|
+
else
|
|
71
|
+
kids[p].push(i);
|
|
72
|
+
}
|
|
73
|
+
const depth = new Int32Array(n);
|
|
74
|
+
const tint = new Int32Array(n);
|
|
75
|
+
const order = [...roots];
|
|
76
|
+
roots.forEach((root, k) => (tint[root] = roots.length === 1 && n > 1 ? -1 : k));
|
|
77
|
+
for (let done = 0; done < order.length; done++) {
|
|
78
|
+
const node = order[done];
|
|
79
|
+
kids[node].forEach((kid, k) => {
|
|
80
|
+
depth[kid] = depth[node] + 1;
|
|
81
|
+
tint[kid] = tint[node] < 0 ? k : tint[node];
|
|
82
|
+
order.push(kid);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (order.length < n) {
|
|
86
|
+
// A node no root leads to has a parent no root leads to; walking up must come round.
|
|
87
|
+
const reached = new Set(order);
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
let node = parent.length ? Array.from({ length: n }, (_, i) => i).find(i => !reached.has(i)) : 0;
|
|
90
|
+
while (!seen.has(node)) {
|
|
91
|
+
seen.add(node);
|
|
92
|
+
node = parent[node];
|
|
93
|
+
}
|
|
94
|
+
const circle = [node];
|
|
95
|
+
for (let next = parent[node]; next !== node; next = parent[next])
|
|
96
|
+
circle.unshift(next);
|
|
97
|
+
throw new Error(`${who}: the parents go round in a circle through ${circle.map(named).join(', ')}`);
|
|
98
|
+
}
|
|
99
|
+
const worth = (i) => (value[i] > 0 && Number.isFinite(value[i]) ? value[i] : 0);
|
|
100
|
+
let most = 0;
|
|
101
|
+
for (let i = 0; i < n; i++)
|
|
102
|
+
if (!kids[i].length)
|
|
103
|
+
most = Math.max(most, worth(i));
|
|
104
|
+
const size = new Float64Array(n);
|
|
105
|
+
for (let k = n - 1; k >= 0; k--) {
|
|
106
|
+
const node = order[k];
|
|
107
|
+
if (!kids[node].length)
|
|
108
|
+
size[node] = worth(node) / (most || 1);
|
|
109
|
+
if (parent[node] >= 0)
|
|
110
|
+
size[parent[node]] += size[node];
|
|
111
|
+
}
|
|
112
|
+
return { n, kids, roots, order, depth, tint, size, named };
|
|
113
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Mark } from '../types.js';
|
|
2
|
+
import { type TreeColumns } from './tree.js';
|
|
3
|
+
/**
|
|
4
|
+
* Rectangles as large as their values, packed as near to square as they go, for a blank frame.
|
|
5
|
+
* Only nodes without children are drawn, inside the room of their parents; see TreeColumns for
|
|
6
|
+
* the input and fromTree() for nested objects. A parent with more children than it has pixels is
|
|
7
|
+
* drawn as one rectangle, and is then the row a hit reports. Each root has a colour, or each child of a single
|
|
8
|
+
* root, and everything below it shares that colour. A hit's row is the node's place in `nodes`.
|
|
9
|
+
* ponytail: a pointer move looks through the siblings at each level, which is fine for
|
|
10
|
+
* thousands; the rows the packing made would index them when a level holds many more.
|
|
11
|
+
* ponytail: at most the 200 largest rectangles ask for a label, since the chart compares every
|
|
12
|
+
* label with every other; an index of kept labels when more are wanted.
|
|
13
|
+
*/
|
|
14
|
+
export type TreemapOptions = TreeColumns & {
|
|
15
|
+
/** A colour for each root, or for each child of a single root, repeated when there are fewer. */
|
|
16
|
+
colors: string[];
|
|
17
|
+
/** Text in each rectangle that has room: the node's name by default, your own, or none. */
|
|
18
|
+
text?: false | ((row: number) => string);
|
|
19
|
+
/** Each branch starts at this opacity at the top of its room and reaches its full colour at the bottom. */
|
|
20
|
+
shade?: number;
|
|
21
|
+
/** The mark's opacity, from 0 to 1. @default 1 */
|
|
22
|
+
alpha?: number;
|
|
23
|
+
/** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
|
|
24
|
+
key?: string;
|
|
25
|
+
/** The mark's name in a legend, a tooltip and the chart's description. */
|
|
26
|
+
label?: string;
|
|
27
|
+
/** Marks of one group are focused, dimmed and hidden together; the label when left out. */
|
|
28
|
+
group?: string;
|
|
29
|
+
};
|
|
30
|
+
export declare function treemap(o: TreemapOptions): Mark;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { textWidth } from '../text.js';
|
|
2
|
+
import { gradient, painter } from '../paint.js';
|
|
3
|
+
import { first, grow } from './tree.js';
|
|
4
|
+
export function treemap(o) {
|
|
5
|
+
const { colors, alpha = 1 } = o;
|
|
6
|
+
const { n, kids, roots, order, tint, size, named } = grow(o, 'treemap');
|
|
7
|
+
const shade = (i) => colors[tint[i] % colors.length] ?? '#888';
|
|
8
|
+
const sorted = (list) => list.filter(i => size[i] > 0).sort((a, b) => size[b] - size[a] || a - b);
|
|
9
|
+
const leaves = order.filter(i => !kids[i].length && size[i] > 0);
|
|
10
|
+
const largest = first(leaves, 200, (a, b) => size[b] - size[a] || a - b);
|
|
11
|
+
return {
|
|
12
|
+
type: 'treemap',
|
|
13
|
+
key: o.key,
|
|
14
|
+
label: o.label,
|
|
15
|
+
group: o.group ?? o.label,
|
|
16
|
+
color: colors[0] ?? '#888',
|
|
17
|
+
options: o,
|
|
18
|
+
points: n,
|
|
19
|
+
channels: { x: [], y: [] },
|
|
20
|
+
prepare({ ctx, dpr, left, top, width, height }) {
|
|
21
|
+
// Each node's room as x, y, width and height; a node of no size has none.
|
|
22
|
+
const room = new Float64Array(n * 4);
|
|
23
|
+
// Rows of rectangles along the shorter side of what is left, each row closed when one
|
|
24
|
+
// more rectangle would make its worst rectangle less square.
|
|
25
|
+
const pack = (list, x, y, w, h) => {
|
|
26
|
+
const total = list.reduce((sum, i) => sum + size[i], 0);
|
|
27
|
+
if (!(w > 0 && h > 0 && total > 0))
|
|
28
|
+
return;
|
|
29
|
+
const scale = (w * h) / total;
|
|
30
|
+
for (let start = 0; start < list.length;) {
|
|
31
|
+
const short = Math.min(w, h);
|
|
32
|
+
let [end, sum, least, most, worst] = [start, 0, Infinity, 0, Infinity];
|
|
33
|
+
for (; end < list.length; end++) {
|
|
34
|
+
const area = size[list[end]] * scale;
|
|
35
|
+
const [all, lo, hi] = [sum + area, Math.min(least, area), Math.max(most, area)];
|
|
36
|
+
const badly = Math.max((short * short * hi) / (all * all), (all * all) / (short * short * lo));
|
|
37
|
+
if (end > start && badly > worst)
|
|
38
|
+
break;
|
|
39
|
+
[sum, least, most, worst] = [all, lo, hi, badly];
|
|
40
|
+
}
|
|
41
|
+
const thick = short > 0 ? sum / short : 0;
|
|
42
|
+
let along = 0;
|
|
43
|
+
for (let k = start; k < end; k++) {
|
|
44
|
+
const long = thick ? (size[list[k]] * scale) / thick : 0;
|
|
45
|
+
room.set(w >= h ? [x, y + along, thick, long] : [x + along, y, long, thick], list[k] * 4);
|
|
46
|
+
along += long;
|
|
47
|
+
}
|
|
48
|
+
if (w >= h)
|
|
49
|
+
[x, w] = [x + thick, Math.max(0, w - thick)];
|
|
50
|
+
else
|
|
51
|
+
[y, h] = [y + thick, Math.max(0, h - thick)];
|
|
52
|
+
start = end;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
pack(sorted(roots), left, top, width, height);
|
|
56
|
+
// A parent keeps a pixel round its children, which is what sets branches apart; an only
|
|
57
|
+
// child has no branch beside it and takes all the room, however long the chain.
|
|
58
|
+
// A parent whose children would average under a device pixel each is drawn, and hit, as
|
|
59
|
+
// one rectangle itself: more rectangles than pixels cost time and show nothing.
|
|
60
|
+
const solid = new Uint8Array(n);
|
|
61
|
+
const shown = [];
|
|
62
|
+
for (const node of order) {
|
|
63
|
+
const [x, y, w, h] = room.subarray(node * 4, node * 4 + 4);
|
|
64
|
+
if (!(w > 0 && h > 0))
|
|
65
|
+
continue;
|
|
66
|
+
if (!kids[node].length || (w * h < kids[node].length && tint[node] >= 0)) {
|
|
67
|
+
solid[node] = 1;
|
|
68
|
+
shown.push(node);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const some = sorted(kids[node]);
|
|
72
|
+
const [px, py] = some.length > 1 ? [Math.min(dpr, w / 4), Math.min(dpr, h / 4)] : [0, 0];
|
|
73
|
+
pack(some, x + px, y + py, w - 2 * px, h - 2 * py);
|
|
74
|
+
}
|
|
75
|
+
// What is painted and hit of a leaf: its room less half a pixel all round, or less where
|
|
76
|
+
// that would leave nothing.
|
|
77
|
+
const box = (i) => {
|
|
78
|
+
const [x, y, w, h] = room.subarray(i * 4, i * 4 + 4);
|
|
79
|
+
const [gx, gy] = [Math.min(dpr / 2, w / 4), Math.min(dpr / 2, h / 4)];
|
|
80
|
+
return [x + gx, y + gy, w - 2 * gx, h - 2 * gy];
|
|
81
|
+
};
|
|
82
|
+
// One shape per colour, since no two rectangles overlap: a fill each would cost far more.
|
|
83
|
+
// With a shade each branch has its own paint, lighter at the top of the branch's room.
|
|
84
|
+
const sheets = new Map();
|
|
85
|
+
const sheetOf = (i) => (o.shade === undefined ? shade(i) : tint[i]);
|
|
86
|
+
for (const i of shown) {
|
|
87
|
+
if (!sheets.has(sheetOf(i))) {
|
|
88
|
+
const branch = tint[i] < 0
|
|
89
|
+
? roots[0]
|
|
90
|
+
: roots.length === 1 && n > 1
|
|
91
|
+
? kids[roots[0]][tint[i]]
|
|
92
|
+
: roots[tint[i]];
|
|
93
|
+
const [x, y, w, h] = room.subarray(branch * 4, branch * 4 + 4);
|
|
94
|
+
const ink = painter(ctx, o.shade === undefined ? undefined : gradient(o.shade, 1), shade(i))([x, y, x + w, y + h]);
|
|
95
|
+
sheets.set(sheetOf(i), { path: new Path2D(), ink });
|
|
96
|
+
}
|
|
97
|
+
sheets.get(sheetOf(i)).path.rect(...box(i));
|
|
98
|
+
}
|
|
99
|
+
const labels = [];
|
|
100
|
+
for (const i of o.text === false ? [] : largest) {
|
|
101
|
+
const text = o.text ? o.text(i) : named(i);
|
|
102
|
+
const [x, y, w, h] = box(i);
|
|
103
|
+
if (!text || (textWidth(text) + 8) * dpr > w || 20 * dpr > h)
|
|
104
|
+
continue;
|
|
105
|
+
labels.push({
|
|
106
|
+
x: x + 4 * dpr,
|
|
107
|
+
y: y + 10 * dpr,
|
|
108
|
+
text,
|
|
109
|
+
align: 'start',
|
|
110
|
+
color: '#fff',
|
|
111
|
+
priority: size[i],
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
labels,
|
|
116
|
+
paint(k, row, rest = 1) {
|
|
117
|
+
ctx.save();
|
|
118
|
+
if (row !== undefined && solid[row]) {
|
|
119
|
+
ctx.globalAlpha = alpha * k;
|
|
120
|
+
ctx.fillStyle = sheets.get(sheetOf(row))?.ink ?? shade(row);
|
|
121
|
+
ctx.fillRect(...box(row));
|
|
122
|
+
// The rest is drawn through a hole where that rectangle is, so it is not painted twice.
|
|
123
|
+
const around = new Path2D();
|
|
124
|
+
around.rect(...box(row));
|
|
125
|
+
around.rect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
126
|
+
ctx.clip(around, 'evenodd');
|
|
127
|
+
}
|
|
128
|
+
ctx.globalAlpha = alpha * k * (row === undefined ? 1 : rest);
|
|
129
|
+
for (const { path, ink } of sheets.values()) {
|
|
130
|
+
ctx.fillStyle = ink;
|
|
131
|
+
ctx.fill(path);
|
|
132
|
+
}
|
|
133
|
+
ctx.restore();
|
|
134
|
+
},
|
|
135
|
+
// Down from the roots through the rooms the pointer is in, to a leaf's painted box.
|
|
136
|
+
hit(px, py, max) {
|
|
137
|
+
const [hx, hy] = [px * dpr, py * dpr];
|
|
138
|
+
const holds = (i) => {
|
|
139
|
+
const k = i * 4;
|
|
140
|
+
return (hx >= room[k] &&
|
|
141
|
+
hx < room[k] + room[k + 2] &&
|
|
142
|
+
hy >= room[k + 1] &&
|
|
143
|
+
hy < room[k + 1] + room[k + 3]);
|
|
144
|
+
};
|
|
145
|
+
let node = roots.find(holds);
|
|
146
|
+
while (node !== undefined && !solid[node])
|
|
147
|
+
node = kids[node].find(holds);
|
|
148
|
+
if (node === undefined)
|
|
149
|
+
return null;
|
|
150
|
+
const [x, y, w, h] = box(node);
|
|
151
|
+
if (hx < x || hx >= x + w || hy < y || hy >= y + h)
|
|
152
|
+
return null;
|
|
153
|
+
return {
|
|
154
|
+
x: (x + w / 2 - left) / width,
|
|
155
|
+
y: 1 - (y + h / 2 - top) / height,
|
|
156
|
+
distance: max,
|
|
157
|
+
row: node,
|
|
158
|
+
color: shade(node),
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
package/dist/paint.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Eight colours for eight series, to be given out in this order: the order is what keeps
|
|
3
|
+
* neighbours apart for colour-blind readers, and each has a contrast of 3:1 on white.
|
|
4
|
+
*/
|
|
5
|
+
export declare const palette: string[];
|
|
6
|
+
/** The same eight hues in the same order, each with a contrast of 3:1 on `dark.surface`. */
|
|
7
|
+
export declare const paletteDark: string[];
|
|
8
|
+
/** A stop is a CSS colour, or a number for the mark's own colour at that opacity. */
|
|
9
|
+
export type Gradient = {
|
|
10
|
+
from: string | number;
|
|
11
|
+
to: string | number;
|
|
12
|
+
toward: 'down' | 'up' | 'right' | 'left';
|
|
13
|
+
};
|
|
14
|
+
/** What a shape is filled or a line drawn with: a CSS colour, or two blended across the mark. */
|
|
15
|
+
export type Paint = string | Gradient;
|
|
16
|
+
/**
|
|
17
|
+
* A blend of two stops in a direction on the screen: gradient(0.4, 0) fades a mark's own colour out downwards.
|
|
18
|
+
* @param from The first stop: a CSS colour, or a number for the mark's own colour at that opacity.
|
|
19
|
+
* @param to The second stop, given as `from` is.
|
|
20
|
+
* @param toward The way the blend runs on the screen, from `from` to `to`; 'down' when left out.
|
|
21
|
+
*/
|
|
22
|
+
export declare const gradient: (from: string | number, to: string | number, toward?: Gradient['toward']) => Gradient;
|
|
23
|
+
/** The brand's blend, cyan to violet, left to right. */
|
|
24
|
+
export declare const brand: Gradient;
|
|
25
|
+
/**
|
|
26
|
+
* The two stops as red, green, blue and opacity from 0 to 1, in the order the direction runs on
|
|
27
|
+
* screen; `based` keeps them as given, for a blend that runs to a baseline whichever way that lies.
|
|
28
|
+
*/
|
|
29
|
+
export declare function stops(ctx: CanvasRenderingContext2D, g: Gradient, color: string, based?: boolean): number[][];
|
|
30
|
+
/**
|
|
31
|
+
* What to fill or stroke with on the canvas, given a box of device pixels: left, top, right and
|
|
32
|
+
* bottom. With `turn`, the level of a baseline, an upright blend starts at the box's far end, or
|
|
33
|
+
* at both ends when the baseline is between them, and reaches its second stop at the baseline.
|
|
34
|
+
*/
|
|
35
|
+
export declare function painter(ctx: CanvasRenderingContext2D, paint: Paint | undefined, color: string): ([l, t, r, b]: number[], turn?: number) => string | CanvasGradient;
|
package/dist/paint.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { rgba } from './gl.js';
|
|
2
|
+
/**
|
|
3
|
+
* Eight colours for eight series, to be given out in this order: the order is what keeps
|
|
4
|
+
* neighbours apart for colour-blind readers, and each has a contrast of 3:1 on white.
|
|
5
|
+
*/
|
|
6
|
+
export const palette = [
|
|
7
|
+
'#0891b2',
|
|
8
|
+
'#7c3aed',
|
|
9
|
+
'#db2777',
|
|
10
|
+
'#2563eb',
|
|
11
|
+
'#d97706',
|
|
12
|
+
'#c026d3',
|
|
13
|
+
'#059669',
|
|
14
|
+
'#4f46e5',
|
|
15
|
+
];
|
|
16
|
+
/** The same eight hues in the same order, each with a contrast of 3:1 on `dark.surface`. */
|
|
17
|
+
export const paletteDark = [
|
|
18
|
+
'#0891b2',
|
|
19
|
+
'#8b5cf6',
|
|
20
|
+
'#ec4899',
|
|
21
|
+
'#3b82f6',
|
|
22
|
+
'#d97706',
|
|
23
|
+
'#d946ef',
|
|
24
|
+
'#059669',
|
|
25
|
+
'#6366f1',
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* A blend of two stops in a direction on the screen: gradient(0.4, 0) fades a mark's own colour out downwards.
|
|
29
|
+
* @param from The first stop: a CSS colour, or a number for the mark's own colour at that opacity.
|
|
30
|
+
* @param to The second stop, given as `from` is.
|
|
31
|
+
* @param toward The way the blend runs on the screen, from `from` to `to`; 'down' when left out.
|
|
32
|
+
*/
|
|
33
|
+
export const gradient = (from, to, toward = 'down') => ({ from, to, toward });
|
|
34
|
+
/** The brand's blend, cyan to violet, left to right. */
|
|
35
|
+
export const brand = gradient('#06b6d4', '#8b5cf6', 'right');
|
|
36
|
+
/**
|
|
37
|
+
* The two stops as red, green, blue and opacity from 0 to 1, in the order the direction runs on
|
|
38
|
+
* screen; `based` keeps them as given, for a blend that runs to a baseline whichever way that lies.
|
|
39
|
+
*/
|
|
40
|
+
export function stops(ctx, g, color, based = false) {
|
|
41
|
+
const own = rgba(ctx, color);
|
|
42
|
+
const both = [g.from, g.to].map(stop => typeof stop === 'number' ? [own[0], own[1], own[2], (own[3] ?? 1) * stop] : rgba(ctx, stop));
|
|
43
|
+
return !based && (g.toward === 'up' || g.toward === 'left') ? both.reverse() : both;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What to fill or stroke with on the canvas, given a box of device pixels: left, top, right and
|
|
47
|
+
* bottom. With `turn`, the level of a baseline, an upright blend starts at the box's far end, or
|
|
48
|
+
* at both ends when the baseline is between them, and reaches its second stop at the baseline.
|
|
49
|
+
*/
|
|
50
|
+
export function painter(ctx, paint, color) {
|
|
51
|
+
if (paint === undefined || typeof paint === 'string')
|
|
52
|
+
return () => paint ?? color;
|
|
53
|
+
const upright = paint.toward === 'down' || paint.toward === 'up';
|
|
54
|
+
const [plain, based] = [false, true].map(given => stops(ctx, paint, color, given).map(([red, green, blue, a]) => `rgba(${Math.round(red * 255)},${Math.round(green * 255)},${Math.round(blue * 255)},${a})`));
|
|
55
|
+
return ([l, t, r, b], turn) => {
|
|
56
|
+
// A box that is not a number, such as a bar at zero on a log axis, draws nothing, and a gradient for it would throw.
|
|
57
|
+
if (!Number.isFinite(upright ? t + b : l + r))
|
|
58
|
+
return plain[0];
|
|
59
|
+
const made = upright ? ctx.createLinearGradient(0, t, 0, b) : ctx.createLinearGradient(l, 0, r, 0);
|
|
60
|
+
if (upright && turn !== undefined) {
|
|
61
|
+
made.addColorStop(0, based[0]);
|
|
62
|
+
made.addColorStop(Math.min(1, Math.max(0, (turn - t) / (b - t || 1))), based[1]);
|
|
63
|
+
made.addColorStop(1, based[0]);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
made.addColorStop(0, plain[0]);
|
|
67
|
+
made.addColorStop(1, plain[1]);
|
|
68
|
+
}
|
|
69
|
+
return made;
|
|
70
|
+
};
|
|
71
|
+
}
|
package/dist/scale.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Axis, Col, Room } from './types.js';
|
|
2
|
+
/** The 1, 2 or 5 times a power of ten that cuts the span into at most `max` steps. */
|
|
3
|
+
export declare function niceStep(span: number, max?: number): number;
|
|
4
|
+
export declare function extent(cols: Col[]): [number, number];
|
|
5
|
+
/** Whether this many labels, the widest so many CSS pixels wide, fit side by side (x) or stacked (y) along the axis. */
|
|
6
|
+
export declare const fits: (count: number, widest: number, room: Room) => boolean;
|
|
7
|
+
/** At most the ticks whose labels fit: when even two collide, only the first is kept. */
|
|
8
|
+
export declare const thin: <T>(ticks: T[], widest: number, room?: Room) => T[];
|
|
9
|
+
/** The ticks `keep` picks for the least k whose labels fit, every k-th by default; the first when it picks none. */
|
|
10
|
+
export declare const spaced: <T>(ticks: T[], widest: number, room?: Room, keep?: (t: T, i: number, k: number) => boolean) => T[];
|
|
11
|
+
/** `of` is the extent of the data, used when the domain is 'nice' or 'data'. */
|
|
12
|
+
export declare function scale(domain: Axis['domain'], of: [number, number], room?: Room, { step: given, format }?: Pick<Axis, 'step' | 'format'>): {
|
|
13
|
+
lo: number;
|
|
14
|
+
hi: number;
|
|
15
|
+
ticks: number[];
|
|
16
|
+
};
|
package/dist/scale.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { textWidth, widest as widestOf } from './text.js';
|
|
2
|
+
/** The 1, 2 or 5 times a power of ten that cuts the span into at most `max` steps. */
|
|
3
|
+
export function niceStep(span, max = 10) {
|
|
4
|
+
const mag = 10 ** Math.floor(Math.log10(span / max));
|
|
5
|
+
return mag * ([1, 2, 5].find(k => span / (mag * k) <= max) ?? 10);
|
|
6
|
+
}
|
|
7
|
+
export function extent(cols) {
|
|
8
|
+
let lo = Infinity;
|
|
9
|
+
let hi = -Infinity;
|
|
10
|
+
for (const c of cols) {
|
|
11
|
+
for (let i = 0; i < c.length; i++) {
|
|
12
|
+
// NaN fails both comparisons; an infinite value must not become a bound either.
|
|
13
|
+
const v = c[i];
|
|
14
|
+
if (v < lo && v !== -Infinity)
|
|
15
|
+
lo = v;
|
|
16
|
+
if (v > hi && v !== Infinity)
|
|
17
|
+
hi = v;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return lo > hi ? [0, 1] : [lo, hi];
|
|
21
|
+
}
|
|
22
|
+
/** Whether this many labels, the widest so many CSS pixels wide, fit side by side (x) or stacked (y) along the axis. */
|
|
23
|
+
export const fits = (count, widest, room) => count * (room.horizontal ? widest + 16 : 22) <= Math.max(room.length, 1);
|
|
24
|
+
/** At most the ticks whose labels fit: when even two collide, only the first is kept. */
|
|
25
|
+
export const thin = (ticks, widest, room) => room && ticks.length > 1 && !fits(ticks.length, widest, room) ? ticks.slice(0, 1) : ticks;
|
|
26
|
+
/** The ticks `keep` picks for the least k whose labels fit, every k-th by default; the first when it picks none. */
|
|
27
|
+
export const spaced = (ticks, widest, room, keep = (_, i, k) => i % k === 0) => {
|
|
28
|
+
let k = 1;
|
|
29
|
+
while (room && k < ticks.length && !fits(Math.ceil(ticks.length / k), widest, room))
|
|
30
|
+
k++;
|
|
31
|
+
const kept = ticks.filter((t, i) => keep(t, i, k));
|
|
32
|
+
return kept.length ? kept : ticks.slice(0, 1);
|
|
33
|
+
};
|
|
34
|
+
/** `of` is the extent of the data, used when the domain is 'nice' or 'data'. */
|
|
35
|
+
export function scale(domain, of, room, { step: given, format = String } = {}) {
|
|
36
|
+
let [lo, hi] = typeof domain === 'string' ? of : domain;
|
|
37
|
+
if (lo === hi) {
|
|
38
|
+
const pad = Math.abs(lo) / 2 || 0.5;
|
|
39
|
+
lo -= pad;
|
|
40
|
+
hi += pad;
|
|
41
|
+
}
|
|
42
|
+
// The bounds are rounded to this step whatever the room, so they hold still when the chart is resized.
|
|
43
|
+
// A span too small to divide is its own step, and a bound rounded past the largest number stays on it.
|
|
44
|
+
// A step too fine for the 1000 ticks counted below would leave ticks at the low end only.
|
|
45
|
+
const fixed = Number.isFinite(given) && given > 0 && (hi - lo) / given < 990;
|
|
46
|
+
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
|
+
// Cleaning to 15 digits is undone where it moves a value by more than float error, so it never merges multiples.
|
|
49
|
+
const clean = (v, tick) => {
|
|
50
|
+
const c = +v.toPrecision(15);
|
|
51
|
+
return Math.abs(c - v) <= tick * 1e-9 ? c : v;
|
|
52
|
+
};
|
|
53
|
+
// The nearest multiple, or the next one outward when it falls inside v: the quotient's own rounding can
|
|
54
|
+
// land either side, so only the cleaned bound is tested. Where no multiple is representable, v is kept.
|
|
55
|
+
const multiple = (v, out) => {
|
|
56
|
+
const n = Math.round(v / step);
|
|
57
|
+
for (const k of [n, n + out]) {
|
|
58
|
+
const b = clean(held(k * step), step);
|
|
59
|
+
if ((b - v) * out >= 0)
|
|
60
|
+
return b;
|
|
61
|
+
}
|
|
62
|
+
return v;
|
|
63
|
+
};
|
|
64
|
+
if (domain === 'nice')
|
|
65
|
+
[lo, hi] = [multiple(lo, -1), multiple(hi, 1)];
|
|
66
|
+
const at = (tick) => {
|
|
67
|
+
const digits = Math.max(0, -Math.floor(Math.log10(tick)));
|
|
68
|
+
const first = Math.ceil(lo / tick - 1e-9);
|
|
69
|
+
const ticks = [];
|
|
70
|
+
// Counted, not run until past hi: beyond 2^53 a step no longer changes the value, and a loop
|
|
71
|
+
// on the value would never end. Equal neighbours are what such a step leaves behind.
|
|
72
|
+
for (let k = 0; k < 1000 && (first + k) * tick <= hi + tick * 1e-9; k++) {
|
|
73
|
+
// toFixed takes at most 100 decimals; a smaller tick is cleaned to 15 significant digits.
|
|
74
|
+
const v = fixed || digits > 100 ? clean((first + k) * tick, tick) : +((first + k) * tick).toFixed(digits);
|
|
75
|
+
if (v !== ticks.at(-1))
|
|
76
|
+
ticks.push(v);
|
|
77
|
+
}
|
|
78
|
+
return ticks;
|
|
79
|
+
};
|
|
80
|
+
const widest = () => widestOf(ticks.map(format), room?.horizontal ? textWidth : () => 0);
|
|
81
|
+
let ticks = at(step);
|
|
82
|
+
if (fixed)
|
|
83
|
+
return {
|
|
84
|
+
lo,
|
|
85
|
+
hi,
|
|
86
|
+
ticks: spaced(ticks, ticks.length ? widest() : 0, room, (v, _, k) => Math.round(v / step) % k === 0),
|
|
87
|
+
};
|
|
88
|
+
// Only the ticks thin out, to as few as it takes for their labels not to collide.
|
|
89
|
+
for (let max = 9; room && max > 0 && ticks.length > 1 && !fits(ticks.length, widest(), room); max--) {
|
|
90
|
+
// A coarser step can fall between the bounds and hit nothing; the last ticks found are kept.
|
|
91
|
+
const coarser = at(Math.max(step, niceStep(hi - lo, max)));
|
|
92
|
+
if (!coarser.length)
|
|
93
|
+
break;
|
|
94
|
+
ticks = coarser;
|
|
95
|
+
}
|
|
96
|
+
return { lo, hi, ticks: thin(ticks, ticks.length ? widest() : 0, room) };
|
|
97
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Axis } from '../types.js';
|
|
2
|
+
export type BandAxis = Axis & {
|
|
3
|
+
/** The categories in order; category i sits at i. */
|
|
4
|
+
categories: readonly string[];
|
|
5
|
+
};
|
|
6
|
+
/** Category i sits at x = i and owns the band from i - 0.5 to i + 0.5. */
|
|
7
|
+
export declare const band: (o: {
|
|
8
|
+
/** The axis title; an empty string shows none. */
|
|
9
|
+
label: string;
|
|
10
|
+
/** The categories in order, each labelled where there is room. */
|
|
11
|
+
categories: readonly string[];
|
|
12
|
+
}) => BandAxis;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { widest } from '../text.js';
|
|
2
|
+
function resolve({ categories, domain }, _extent, room) {
|
|
3
|
+
const n = categories.length;
|
|
4
|
+
const [first, last] = typeof domain === 'string'
|
|
5
|
+
? [0, n - 1]
|
|
6
|
+
: [Math.max(0, Math.ceil(domain[0])), Math.min(n - 1, Math.floor(domain[1]))];
|
|
7
|
+
const shown = categories.slice(first, last + 1);
|
|
8
|
+
// Every category is labelled when they fit; otherwise every second, third and so on.
|
|
9
|
+
let every = Math.ceil(shown.length / 20) || 1;
|
|
10
|
+
if (room) {
|
|
11
|
+
// ponytail: only the 64 longest names by character count are measured, so a shorter name of
|
|
12
|
+
// wider glyphs among more than that is missed; measure them all if that ever shows.
|
|
13
|
+
const longest = shown.length > 64 ? shown.toSorted((a, b) => b.length - a.length).slice(0, 64) : shown;
|
|
14
|
+
const each = room.horizontal ? widest(longest) + 16 : 22;
|
|
15
|
+
every = Math.ceil(shown.length / Math.max(1, Math.floor(room.length / each))) || 1;
|
|
16
|
+
}
|
|
17
|
+
const ticks = [];
|
|
18
|
+
for (let value = Math.ceil(first / every) * every; value <= last; value += every)
|
|
19
|
+
ticks.push({ value, label: categories[value] });
|
|
20
|
+
return { lo: -0.5, hi: Math.max(n, 1) - 0.5, ticks };
|
|
21
|
+
}
|
|
22
|
+
/** Category i sits at x = i and owns the band from i - 0.5 to i + 0.5. */
|
|
23
|
+
export const band = (o) => ({
|
|
24
|
+
domain: 'data',
|
|
25
|
+
...o,
|
|
26
|
+
scale: resolve,
|
|
27
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Axis } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* An axis that places the marks and shows nothing itself: no ticks, no grid lines and no label.
|
|
4
|
+
* @param domain 'data' (the default) fits the marks exactly, 'nice' widens that to whole steps; or [lo, hi].
|
|
5
|
+
*/
|
|
6
|
+
export declare const bare: (domain?: Axis['domain']) => Axis;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { scale } from '../scale.js';
|
|
2
|
+
function resolve(axis, extent) {
|
|
3
|
+
const { lo, hi } = scale(axis.domain, typeof axis.domain === 'string' ? extent() : axis.domain);
|
|
4
|
+
return { lo, hi, ticks: [] };
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* An axis that places the marks and shows nothing itself: no ticks, no grid lines and no label.
|
|
8
|
+
* @param domain 'data' (the default) fits the marks exactly, 'nice' widens that to whole steps; or [lo, hi].
|
|
9
|
+
*/
|
|
10
|
+
export const bare = (domain = 'data') => ({ domain, label: '', scale: resolve });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** A colour for each value of a domain, as a table of 256 pixels to index into. */
|
|
2
|
+
export type ColorScale = {
|
|
3
|
+
/** The values that get the first and the last colour; the extent of the data when left out. */
|
|
4
|
+
domain?: [number, number];
|
|
5
|
+
/** The first and the last stop, as given. */
|
|
6
|
+
stops: string[];
|
|
7
|
+
/** Pixels as ImageData holds them when read through a Uint32Array. */
|
|
8
|
+
table: Uint32Array;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Colours running through the stops, which are #rgb or #rrggbb, in equal steps.
|
|
12
|
+
* ponytail: blended in sRGB, and the pixel layout assumes a little-endian machine, as every
|
|
13
|
+
* browser's is; a perceptual blend or a byte-order check when one of them matters.
|
|
14
|
+
*/
|
|
15
|
+
export declare function sequential(o?: {
|
|
16
|
+
/** The colours to run through, as #rgb or #rrggbb. @default ['#f7fbff', '#6baed6', '#08306b'] */
|
|
17
|
+
stops?: string[];
|
|
18
|
+
/** The values that get the first and the last colour; the extent of the data when left out. */
|
|
19
|
+
domain?: [number, number];
|
|
20
|
+
}): ColorScale;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const channels = (hex) => {
|
|
2
|
+
const h = hex.length === 4 ? hex.replace(/[^#]/g, c => c + c) : hex;
|
|
3
|
+
return [1, 3, 5].map(i => parseInt(h.slice(i, i + 2), 16));
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Colours running through the stops, which are #rgb or #rrggbb, in equal steps.
|
|
7
|
+
* ponytail: blended in sRGB, and the pixel layout assumes a little-endian machine, as every
|
|
8
|
+
* browser's is; a perceptual blend or a byte-order check when one of them matters.
|
|
9
|
+
*/
|
|
10
|
+
export function sequential(o = {}) {
|
|
11
|
+
const stops = o.stops?.length ? o.stops : ['#f7fbff', '#6baed6', '#08306b'];
|
|
12
|
+
const rgb = stops.map(channels);
|
|
13
|
+
const table = new Uint32Array(256);
|
|
14
|
+
for (let i = 0; i < 256; i++) {
|
|
15
|
+
const at = (i / 255) * (rgb.length - 1);
|
|
16
|
+
const [a, b] = [rgb[Math.floor(at)], rgb[Math.ceil(at)]];
|
|
17
|
+
const [r, g, bl] = a.map((v, k) => Math.round(v + (b[k] - v) * (at % 1)));
|
|
18
|
+
table[i] = (255 << 24) | (bl << 16) | (g << 8) | r;
|
|
19
|
+
}
|
|
20
|
+
return { domain: o.domain, stops, table };
|
|
21
|
+
}
|