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.
Files changed (115) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +348 -3
  3. package/dist/chart.d.ts +7 -0
  4. package/dist/chart.js +1311 -0
  5. package/dist/charts/build.d.ts +52 -0
  6. package/dist/charts/build.js +63 -0
  7. package/dist/charts/candlestick.d.ts +8 -0
  8. package/dist/charts/candlestick.js +105 -0
  9. package/dist/charts/combo.d.ts +8 -0
  10. package/dist/charts/combo.js +50 -0
  11. package/dist/charts/controller.d.ts +32 -0
  12. package/dist/charts/controller.js +231 -0
  13. package/dist/charts/defaults.d.ts +20 -0
  14. package/dist/charts/defaults.js +121 -0
  15. package/dist/charts/distribution.d.ts +15 -0
  16. package/dist/charts/distribution.js +178 -0
  17. package/dist/charts/fields.d.ts +64 -0
  18. package/dist/charts/fields.js +251 -0
  19. package/dist/charts/flow.d.ts +15 -0
  20. package/dist/charts/flow.js +189 -0
  21. package/dist/charts/heatmap.d.ts +9 -0
  22. package/dist/charts/heatmap.js +104 -0
  23. package/dist/charts/index.d.ts +13 -0
  24. package/dist/charts/index.js +10 -0
  25. package/dist/charts/mount.d.ts +5 -0
  26. package/dist/charts/mount.js +21 -0
  27. package/dist/charts/parts.d.ts +23 -0
  28. package/dist/charts/parts.js +66 -0
  29. package/dist/charts/radar.d.ts +7 -0
  30. package/dist/charts/radar.js +77 -0
  31. package/dist/charts/source.d.ts +24 -0
  32. package/dist/charts/source.js +225 -0
  33. package/dist/charts/types.d.ts +643 -0
  34. package/dist/charts/types.js +1 -0
  35. package/dist/charts/xy.d.ts +46 -0
  36. package/dist/charts/xy.js +421 -0
  37. package/dist/features/animate.d.ts +20 -0
  38. package/dist/features/animate.js +118 -0
  39. package/dist/features/brush.d.ts +15 -0
  40. package/dist/features/brush.js +128 -0
  41. package/dist/features/legend.d.ts +13 -0
  42. package/dist/features/legend.js +236 -0
  43. package/dist/features/pointer.d.ts +29 -0
  44. package/dist/features/pointer.js +23 -0
  45. package/dist/features/tooltip.d.ts +22 -0
  46. package/dist/features/tooltip.js +192 -0
  47. package/dist/features/zoom.d.ts +14 -0
  48. package/dist/features/zoom.js +246 -0
  49. package/dist/gl.d.ts +5 -0
  50. package/dist/gl.js +52 -0
  51. package/dist/index.d.ts +62 -0
  52. package/dist/index.js +32 -0
  53. package/dist/marks/arcs.d.ts +31 -0
  54. package/dist/marks/arcs.js +125 -0
  55. package/dist/marks/area.d.ts +25 -0
  56. package/dist/marks/area.js +106 -0
  57. package/dist/marks/bar.d.ts +52 -0
  58. package/dist/marks/bar.js +243 -0
  59. package/dist/marks/box.d.ts +33 -0
  60. package/dist/marks/box.js +182 -0
  61. package/dist/marks/candlestick.d.ts +22 -0
  62. package/dist/marks/candlestick.js +133 -0
  63. package/dist/marks/common.d.ts +102 -0
  64. package/dist/marks/common.js +250 -0
  65. package/dist/marks/dots.d.ts +16 -0
  66. package/dist/marks/dots.js +158 -0
  67. package/dist/marks/errorBars.d.ts +24 -0
  68. package/dist/marks/errorBars.js +99 -0
  69. package/dist/marks/funnel.d.ts +30 -0
  70. package/dist/marks/funnel.js +122 -0
  71. package/dist/marks/heatmap.d.ts +20 -0
  72. package/dist/marks/heatmap.js +139 -0
  73. package/dist/marks/line.d.ts +21 -0
  74. package/dist/marks/line.js +75 -0
  75. package/dist/marks/radar.d.ts +29 -0
  76. package/dist/marks/radar.js +162 -0
  77. package/dist/marks/radialBars.d.ts +34 -0
  78. package/dist/marks/radialBars.js +111 -0
  79. package/dist/marks/rects.d.ts +43 -0
  80. package/dist/marks/rects.js +120 -0
  81. package/dist/marks/rule.d.ts +47 -0
  82. package/dist/marks/rule.js +83 -0
  83. package/dist/marks/sankey.d.ts +53 -0
  84. package/dist/marks/sankey.js +369 -0
  85. package/dist/marks/sunburst.d.ts +31 -0
  86. package/dist/marks/sunburst.js +186 -0
  87. package/dist/marks/ticks.d.ts +12 -0
  88. package/dist/marks/ticks.js +26 -0
  89. package/dist/marks/tree.d.ts +49 -0
  90. package/dist/marks/tree.js +113 -0
  91. package/dist/marks/treemap.d.ts +30 -0
  92. package/dist/marks/treemap.js +164 -0
  93. package/dist/paint.d.ts +35 -0
  94. package/dist/paint.js +71 -0
  95. package/dist/scale.d.ts +16 -0
  96. package/dist/scale.js +97 -0
  97. package/dist/scales/band.d.ts +12 -0
  98. package/dist/scales/band.js +27 -0
  99. package/dist/scales/bare.d.ts +6 -0
  100. package/dist/scales/bare.js +10 -0
  101. package/dist/scales/color.d.ts +20 -0
  102. package/dist/scales/color.js +21 -0
  103. package/dist/scales/log.d.ts +27 -0
  104. package/dist/scales/log.js +98 -0
  105. package/dist/scales/time.d.ts +37 -0
  106. package/dist/scales/time.js +183 -0
  107. package/dist/text.d.ts +8 -0
  108. package/dist/text.js +33 -0
  109. package/dist/theme.d.ts +10 -0
  110. package/dist/theme.js +8 -0
  111. package/dist/transforms.d.ts +47 -0
  112. package/dist/transforms.js +119 -0
  113. package/dist/types.d.ts +465 -0
  114. package/dist/types.js +1 -0
  115. package/package.json +31 -6
@@ -0,0 +1,99 @@
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
+ const isWhole = (i) => Number.isFinite(at[i]) && Number.isFinite(from[i]) && Number.isFinite(to[i]);
11
+ let read;
12
+ const wholeRows = () => {
13
+ if (read)
14
+ return read;
15
+ const copy = (c) => Float64Array.from(at, (_, i) => (isWhole(i) ? c[i] : Number.NaN));
16
+ let partial = false;
17
+ for (let i = 0; i < at.length && !partial; i++)
18
+ partial = !isWhole(i) && (Number.isFinite(at[i]) || Number.isFinite(from[i]) || Number.isFinite(to[i]));
19
+ const [places, low, high] = partial ? [copy(at), copy(from), copy(to)] : [at, from, to];
20
+ return (read = { places: [places], ends: [low, high] });
21
+ };
22
+ return {
23
+ ...base('errorBars', o, upright ? [at] : [from, to], upright ? [from, to] : [at]),
24
+ channels: {
25
+ get x() {
26
+ return upright ? wholeRows().places : wholeRows().ends;
27
+ },
28
+ get y() {
29
+ return upright ? wholeRows().ends : wholeRows().places;
30
+ },
31
+ },
32
+ oneShape: true,
33
+ overhang: Math.max(cap, width / 2),
34
+ prepare({ ctx, dpr, ox, kx, oy, ky }) {
35
+ const c = cap * dpr;
36
+ const path = new Path2D();
37
+ for (let i = 0; i < at.length; i++) {
38
+ // Canvas ignores a moveTo with a NaN in it, and the next lineTo would then join two rows.
39
+ if (!isWhole(i))
40
+ continue;
41
+ // A stem between the two ends, and a cap across each end.
42
+ if (upright) {
43
+ const px = ox + at[i] * kx;
44
+ const [a, b] = [oy - from[i] * ky, oy - to[i] * ky];
45
+ path.moveTo(px, a);
46
+ path.lineTo(px, b);
47
+ path.moveTo(px - c, a);
48
+ path.lineTo(px + c, a);
49
+ path.moveTo(px - c, b);
50
+ path.lineTo(px + c, b);
51
+ }
52
+ else {
53
+ const py = oy - at[i] * ky;
54
+ const [a, b] = [ox + from[i] * kx, ox + to[i] * kx];
55
+ path.moveTo(a, py);
56
+ path.lineTo(b, py);
57
+ path.moveTo(a, py - c);
58
+ path.lineTo(a, py + c);
59
+ path.moveTo(b, py - c);
60
+ path.lineTo(b, py + c);
61
+ }
62
+ }
63
+ return {
64
+ paint(a) {
65
+ ctx.globalAlpha = alpha * a;
66
+ ctx.lineWidth = width * dpr;
67
+ ctx.strokeStyle = color;
68
+ ctx.stroke(path);
69
+ },
70
+ };
71
+ },
72
+ gl: {
73
+ layer: glLayer,
74
+ under: true,
75
+ prepare(view, { gl }) {
76
+ const { draw, once } = rows.on(view, gl, color, Math.max(cap, width / 2));
77
+ // A line keeps a device pixel of width and is fainter instead, or it would fall between pixels.
78
+ const [half, c] = [Math.max(width * view.dpr, 1) / 2, cap * view.dpr];
79
+ const thin = Math.min(width * view.dpr, 1);
80
+ const parts = upright
81
+ ? [
82
+ { xa: 0, xb: 0, ya: 1, yb: 2, pad: [half, 0] },
83
+ { xa: 0, xb: 0, ya: 1, yb: 1, pad: [c, half] },
84
+ { xa: 0, xb: 0, ya: 2, yb: 2, pad: [c, half] },
85
+ ]
86
+ : [
87
+ { xa: 0, xb: 1, ya: 2, yb: 2, pad: [0, half] },
88
+ { xa: 0, xb: 0, ya: 2, yb: 2, pad: [half, c] },
89
+ { xa: 1, xb: 1, ya: 2, yb: 2, pad: [half, c] },
90
+ ];
91
+ return { paint: a => once(() => parts.forEach(part => draw(part, alpha * a * thin))) };
92
+ },
93
+ },
94
+ release(layer) {
95
+ if (layer)
96
+ rows.release(layer.gl);
97
+ },
98
+ };
99
+ }
@@ -0,0 +1,30 @@
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.
6
+ */
7
+ export type FunnelOptions = {
8
+ /** Each stage's value; one that is negative or not a number counts as 0. */
9
+ values: Col;
10
+ /** The stages' colours, repeated when there are fewer than stages. */
11
+ colors: string[];
12
+ /** The stages' names, for a legend, a tooltip and `text`. */
13
+ labels?: string[];
14
+ /**
15
+ * Text on each stage: true for its name and its share of the first stage, or your own. It is
16
+ * written inside the stage in white, or beside it where it does not fit.
17
+ */
18
+ text?: true | ((row: number, share: number) => string);
19
+ /** One blend over the whole funnel in place of a colour per stage; `colors` still name the stages in a legend. */
20
+ fill?: Gradient;
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 funnel(o: FunnelOptions): Mark;
@@ -0,0 +1,122 @@
1
+ import { painter } from '../paint.js';
2
+ import { textWidth } from '../text.js';
3
+ export function funnel(o) {
4
+ const { values, colors, alpha = 1 } = o;
5
+ const n = values.length;
6
+ const worth = (i) => (values[i] > 0 && Number.isFinite(values[i]) ? values[i] : 0);
7
+ let most = 0;
8
+ for (let i = 0; i < n; i++)
9
+ most = Math.max(most, worth(i));
10
+ // Each stage's width as a share of the widest, and its value as a share of the first stage's.
11
+ const wide = Float64Array.from({ length: n }, (_, i) => worth(i) / (most || 1));
12
+ const share = (i) => (worth(0) ? worth(i) / worth(0) : 0);
13
+ const shade = (i) => colors[i % colors.length] ?? '#888';
14
+ const said = o.text === true
15
+ ? (i, s) => `${o.labels?.[i] ?? ''} ${Math.round(s * 100)}%`.trim()
16
+ : o.text;
17
+ return {
18
+ type: 'funnel',
19
+ key: o.key,
20
+ label: o.label,
21
+ group: o.group ?? o.label,
22
+ color: shade(0),
23
+ options: o,
24
+ points: n,
25
+ channels: { x: [], y: [] },
26
+ reveal: 'down',
27
+ entries: o.labels?.slice(0, n).map((label, i) => ({ label, color: shade(i) })),
28
+ prepare({ ctx, dpr, left, top, width, height }) {
29
+ const cx = left + width / 2;
30
+ const tall = height / (n || 1);
31
+ // A pixel of space under each stage but the last.
32
+ const gap = Math.min(dpr, tall / 4);
33
+ const half = (i) => (wide[Math.min(i, n - 1)] * width) / 2;
34
+ // The sides are one smooth curve through the stage widths that never bulges past them:
35
+ // its lean where two stages meet is the harmonic mean of the leans either side, and
36
+ // upright where the widths turn. A stage runs from the top of its band to the gap under
37
+ // it. Paint, hit and the room for text all read this.
38
+ const drop = (i) => (i < 0 || i >= n ? 0 : half(i + 1) - half(i));
39
+ const lean = (i) => {
40
+ const [a, b] = [drop(i - 1), drop(i)];
41
+ if (!i)
42
+ return (3 * b - lean(1)) / 2;
43
+ return a * b > 0 ? (2 * a * b) / (a + b) : 0;
44
+ };
45
+ const span = (i) => [top + i * tall, top + (i + 1) * tall - (i < n - 1 ? gap : 0)];
46
+ const reach = (i, y) => {
47
+ const [y0, y1] = span(i);
48
+ const u = Math.min(1, Math.max(0, (y - y0) / (y1 - y0 || 1)));
49
+ const [uu, back] = [u * u, 1 - u];
50
+ return (half(i) * (1 + 2 * u) * back * back +
51
+ lean(i) * u * back * back +
52
+ half(i + 1) * uu * (3 - 2 * u) -
53
+ lean(i + 1) * uu * back);
54
+ };
55
+ // Stages less than a device pixel tall are drawn several to a straight-sided band a pixel
56
+ // tall, in its first stage's colour: more shapes than pixels cost time and show nothing.
57
+ const per = tall && tall < 1 ? Math.ceil(1 / tall) : 1;
58
+ const stages = [];
59
+ for (let i = 0; i < n; i += per) {
60
+ const to = Math.min(i + per, n);
61
+ const [y0, y1] = [span(i)[0], span(to - 1)[1]];
62
+ const path = new Path2D();
63
+ stages.push({ path, from: i, to });
64
+ path.moveTo(cx - half(i), y0);
65
+ path.lineTo(cx + half(i), y0);
66
+ if (per > 1) {
67
+ path.lineTo(cx + half(to), y1);
68
+ path.lineTo(cx - half(to), y1);
69
+ continue;
70
+ }
71
+ // Control points a third and two thirds of the way down make the curve exactly reach().
72
+ const [a, b] = [y0 + (y1 - y0) / 3, y1 - (y1 - y0) / 3];
73
+ const [out, into] = [half(i) + lean(i) / 3, half(i + 1) - lean(i + 1) / 3];
74
+ path.bezierCurveTo(cx + out, a, cx + into, b, cx + half(i + 1), y1);
75
+ path.lineTo(cx - half(i + 1), y1);
76
+ path.bezierCurveTo(cx - into, b, cx - out, a, cx - half(i), y0);
77
+ path.closePath();
78
+ }
79
+ const ink = o.fill && painter(ctx, o.fill, shade(0))([left, top, left + width, top + height]);
80
+ const labels = [];
81
+ for (let i = 0; said && i < n && most; i++) {
82
+ const [y0, y1] = span(i);
83
+ const y = (y0 + y1) / 2;
84
+ if (y1 - y0 < 16 * dpr)
85
+ continue;
86
+ const text = said(i, share(i));
87
+ if (!text)
88
+ continue;
89
+ // The stage's half-width at the top and at the bottom of the text. Inside where even the
90
+ // narrower leaves room; otherwise to the right of the wider, clear of the slanted side.
91
+ const [above, below] = [reach(i, y - 8 * dpr), reach(i, y + 8 * dpr)];
92
+ if (textWidth(text) * dpr <= 2 * Math.min(above, below) * 0.9) {
93
+ labels.push({ x: cx, y, text, color: '#fff', priority: n - i });
94
+ }
95
+ else {
96
+ labels.push({ x: cx + Math.max(above, below) + 8 * dpr, y, text, align: 'start', priority: n - i });
97
+ }
98
+ }
99
+ return {
100
+ labels,
101
+ paint(k, row, rest = 1) {
102
+ for (const { path, from, to } of stages) {
103
+ ctx.globalAlpha = alpha * k * (row === undefined || (row >= from && row < to) ? 1 : rest);
104
+ ctx.fillStyle = ink || shade(from);
105
+ ctx.fill(path);
106
+ }
107
+ },
108
+ // The stage whose band the pointer is in, if it is within the stage's slanted sides.
109
+ hit(px, py, max) {
110
+ const [hx, hy] = [px * dpr, py * dpr];
111
+ const row = Math.floor((hy - top) / tall);
112
+ if (!(row >= 0 && row < n) || !most || hy >= span(row)[1])
113
+ return null;
114
+ const across = reach(row, hy);
115
+ if (Math.abs(hx - cx) > across || !across)
116
+ return null;
117
+ return { x: 0.5, y: 1 - (row + 0.5) / n, distance: max, row };
118
+ },
119
+ };
120
+ },
121
+ };
122
+ }
@@ -0,0 +1,20 @@
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. A cell whose value is not finite is left empty. Where several
6
+ * cells share a pixel, the pixel shows and answers for the largest of them, so a peak is never lost.
7
+ */
8
+ export type HeatmapOptions = Omit<Common, 'color' | 'yAxis'> & {
9
+ /** The cells along x first: column c of row r is values[r * columns + c], and row 0 lies at the first y edge. */
10
+ values: Col;
11
+ /** How many cells a row holds, rounded down and at least 1. */
12
+ columns: number;
13
+ /** The grid's x edges, in either order; left out, column c is centred on x = c, as a band axis places it. */
14
+ x?: [number, number];
15
+ /** The grid's y edges, in either order; left out, row r is centred on y = r, as a band axis places it. */
16
+ y?: [number, number];
17
+ /** The colours the values run through. @default sequential() */
18
+ color?: ColorScale;
19
+ };
20
+ 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,21 @@
1
+ import type { Col, Mark } from '../types.js';
2
+ import { type Paint } from '../paint.js';
3
+ import type { Common, Curve } from './common.js';
4
+ /** A line through points in ascending x; a y that is not finite leaves a gap unless `join` is set. */
5
+ export type LineOptions = Common & {
6
+ /** The x of each point, ascending. */
7
+ x: Col;
8
+ /** The y of each point; one that is not finite leaves a gap. */
9
+ y: Col;
10
+ /** How the line runs from point to point: straight, in steps, or smoothly. @default 'linear' */
11
+ curve?: Curve;
12
+ /** The line's width in CSS pixels. @default 1.5 */
13
+ width?: number;
14
+ /** Draws the line with something other than its colour: a blend across the plot area, say. */
15
+ stroke?: Paint;
16
+ /** Shades down to the bottom of the plot: `true` for a faint fade of the line's colour, or a paint. */
17
+ fill?: Paint | boolean;
18
+ /** Draws the line straight across a value that is not finite, where it would leave a gap. */
19
+ join?: boolean;
20
+ };
21
+ 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,29 @@
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. A value that is negative or not a number sits at the centre.
7
+ */
8
+ export type RadarOptions = {
9
+ /** The spokes' names; a series has a value for each, in this order. */
10
+ axes: string[];
11
+ /** The series, each drawn as a mark of its own with its values, colour, label, group and key. */
12
+ series: {
13
+ /** The series' value on each spoke. */
14
+ values: Col;
15
+ /** A CSS colour. */
16
+ color: string;
17
+ /** The series' name in a legend, a tooltip and the chart's description. */
18
+ label?: string;
19
+ /** Series of one group are focused, dimmed and hidden together; the label when left out. */
20
+ group?: string;
21
+ /** Pairs the series with the one it replaces in update(), to keep or morph its shape. */
22
+ key?: string;
23
+ }[];
24
+ /** What the rim stands for, capping larger values; the largest rounded up to a whole step when left out. */
25
+ max?: number;
26
+ /** The opacity of a series' filling; 0.15 by default. */
27
+ fill?: number;
28
+ };
29
+ export declare function radar(o: RadarOptions): Mark[];