swiftplot 0.1.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 (95) hide show
  1. package/README.md +71 -47
  2. package/dist/chart.d.ts +4 -0
  3. package/dist/chart.js +171 -86
  4. package/dist/charts/build.d.ts +52 -0
  5. package/dist/charts/build.js +63 -0
  6. package/dist/charts/candlestick.d.ts +8 -0
  7. package/dist/charts/candlestick.js +105 -0
  8. package/dist/charts/combo.d.ts +8 -0
  9. package/dist/charts/combo.js +50 -0
  10. package/dist/charts/controller.d.ts +2 -2
  11. package/dist/charts/controller.js +2 -2
  12. package/dist/charts/defaults.d.ts +10 -2
  13. package/dist/charts/defaults.js +67 -6
  14. package/dist/charts/distribution.d.ts +15 -0
  15. package/dist/charts/distribution.js +178 -0
  16. package/dist/charts/fields.d.ts +33 -3
  17. package/dist/charts/fields.js +150 -66
  18. package/dist/charts/flow.d.ts +15 -0
  19. package/dist/charts/flow.js +189 -0
  20. package/dist/charts/heatmap.d.ts +9 -0
  21. package/dist/charts/heatmap.js +104 -0
  22. package/dist/charts/index.d.ts +8 -1
  23. package/dist/charts/index.js +7 -0
  24. package/dist/charts/parts.d.ts +23 -0
  25. package/dist/charts/parts.js +66 -0
  26. package/dist/charts/radar.d.ts +7 -0
  27. package/dist/charts/radar.js +77 -0
  28. package/dist/charts/types.d.ts +562 -23
  29. package/dist/charts/xy.d.ts +35 -25
  30. package/dist/charts/xy.js +327 -126
  31. package/dist/features/animate.d.ts +10 -1
  32. package/dist/features/animate.js +3 -3
  33. package/dist/features/brush.d.ts +3 -1
  34. package/dist/features/brush.js +2 -1
  35. package/dist/features/legend.d.ts +1 -3
  36. package/dist/features/legend.js +4 -5
  37. package/dist/features/tooltip.d.ts +10 -2
  38. package/dist/features/tooltip.js +12 -10
  39. package/dist/features/zoom.d.ts +1 -0
  40. package/dist/features/zoom.js +22 -4
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.js +1 -0
  43. package/dist/marks/arcs.d.ts +10 -4
  44. package/dist/marks/arcs.js +2 -1
  45. package/dist/marks/area.d.ts +9 -8
  46. package/dist/marks/area.js +2 -2
  47. package/dist/marks/bar.d.ts +18 -10
  48. package/dist/marks/bar.js +5 -5
  49. package/dist/marks/box.d.ts +7 -1
  50. package/dist/marks/box.js +2 -8
  51. package/dist/marks/candlestick.d.ts +22 -0
  52. package/dist/marks/candlestick.js +133 -0
  53. package/dist/marks/common.d.ts +18 -0
  54. package/dist/marks/common.js +14 -0
  55. package/dist/marks/dots.d.ts +6 -5
  56. package/dist/marks/dots.js +3 -1
  57. package/dist/marks/errorBars.d.ts +9 -4
  58. package/dist/marks/errorBars.js +21 -1
  59. package/dist/marks/funnel.d.ts +8 -3
  60. package/dist/marks/funnel.js +2 -1
  61. package/dist/marks/heatmap.d.ts +8 -7
  62. package/dist/marks/line.d.ts +8 -6
  63. package/dist/marks/radar.d.ts +9 -2
  64. package/dist/marks/radar.js +9 -2
  65. package/dist/marks/radialBars.d.ts +12 -5
  66. package/dist/marks/rule.d.ts +19 -0
  67. package/dist/marks/rule.js +4 -1
  68. package/dist/marks/sankey.d.ts +14 -2
  69. package/dist/marks/sankey.js +4 -1
  70. package/dist/marks/sunburst.d.ts +5 -0
  71. package/dist/marks/sunburst.js +2 -1
  72. package/dist/marks/ticks.d.ts +4 -1
  73. package/dist/marks/tree.d.ts +14 -6
  74. package/dist/marks/tree.js +6 -3
  75. package/dist/marks/treemap.d.ts +5 -0
  76. package/dist/marks/treemap.js +7 -2
  77. package/dist/paint.d.ts +6 -1
  78. package/dist/paint.js +6 -1
  79. package/dist/scale.d.ts +1 -1
  80. package/dist/scale.js +4 -3
  81. package/dist/scales/band.d.ts +3 -0
  82. package/dist/scales/band.js +5 -1
  83. package/dist/scales/bare.d.ts +4 -1
  84. package/dist/scales/bare.js +4 -1
  85. package/dist/scales/color.d.ts +2 -0
  86. package/dist/scales/log.d.ts +8 -1
  87. package/dist/scales/log.js +3 -2
  88. package/dist/scales/time.d.ts +7 -0
  89. package/dist/scales/time.js +3 -1
  90. package/dist/text.d.ts +8 -0
  91. package/dist/text.js +33 -0
  92. package/dist/transforms.d.ts +11 -7
  93. package/dist/transforms.js +8 -7
  94. package/dist/types.d.ts +44 -13
  95. package/package.json +2 -2
@@ -10,6 +10,7 @@ import { type TreeColumns } from './tree.js';
10
10
  * ponytail: at most the 200 largest nodes ask for a label, as in treemap().
11
11
  */
12
12
  export type SunburstOptions = TreeColumns & {
13
+ /** A colour for each root, or for each child of a single root, repeated when there are fewer. */
13
14
  colors: string[];
14
15
  /** Text on each node that has room: its name by default, your own, or none. */
15
16
  text?: false | ((row: number) => string);
@@ -18,9 +19,13 @@ export type SunburstOptions = TreeColumns & {
18
19
  * place of a lighter step for each ring.
19
20
  */
20
21
  shade?: number;
22
+ /** The mark's opacity, from 0 to 1. @default 1 */
21
23
  alpha?: number;
24
+ /** Pairs the mark with the one it replaces in update(), to keep or morph its shape; its place in `marks` if left out. */
22
25
  key?: string;
26
+ /** The mark's name in a legend, a tooltip and the chart's description. */
23
27
  label?: string;
28
+ /** Marks of one group are focused, dimmed and hidden together; the label when left out. */
24
29
  group?: string;
25
30
  };
26
31
  export declare function sunburst(o: SunburstOptions): Mark;
@@ -1,4 +1,5 @@
1
1
  import { rgba } from '../gl.js';
2
+ import { textWidth } from '../text.js';
2
3
  import { inRing, TURN, turnAt } from './common.js';
3
4
  import { first, grow } from './tree.js';
4
5
  export function sunburst(o) {
@@ -116,7 +117,7 @@ export function sunburst(o) {
116
117
  for (const i of o.text === false || !outer ? [] : largest) {
117
118
  const text = o.text ? o.text(i) : named(i);
118
119
  const [x, y] = anchor(i);
119
- if (!text || !inRing([cx, cy, ...reach(i), from[i], to[i]], x, y, text.length * 7 * dpr, 16 * dpr))
120
+ if (!text || !inRing([cx, cy, ...reach(i), from[i], to[i]], x, y, textWidth(text) * dpr, 16 * dpr))
120
121
  continue;
121
122
  labels.push({ x, y, text, color: '#fff', priority: size[i] / 2 ** depth[i] });
122
123
  }
@@ -1,9 +1,12 @@
1
1
  import type { Col, Mark } from '../types.js';
2
2
  import type { Common } from './common.js';
3
- /** A vertical tick of `size` CSS pixels above and below each point. */
3
+ /** A vertical tick at each point. */
4
4
  export type TicksOptions = Common & {
5
+ /** The x of each tick. */
5
6
  x: Col;
7
+ /** The y each tick is centred on. */
6
8
  y: Col;
9
+ /** How far each tick reaches above and below its point, in CSS pixels. */
7
10
  size: number;
8
11
  };
9
12
  export declare function ticks(o: TicksOptions): Mark;
@@ -1,21 +1,29 @@
1
1
  import type { Col } from '../types.js';
2
2
  /**
3
- * A hierarchy in columns: `parent` holds the place in `nodes` of each node's parent, negative
4
- * for a root. Only the values of nodes without children count; a node with children is as large
5
- * as they are together, so a parent's own share needs a child of its own. A value that is
6
- * negative or not a number counts as 0.
3
+ * A hierarchy in columns, one row per node. A node with children is as large as they are together,
4
+ * so a parent's own share needs a child of its own.
7
5
  */
8
6
  export type TreeColumns = {
7
+ /** The nodes' names. */
9
8
  nodes: string[];
9
+ /** The place in `nodes` of each node's parent, negative for a root. */
10
10
  parent: Col;
11
+ /** Counts only for a node without children; one negative or not a number counts as 0. */
11
12
  value: Col;
12
13
  };
14
+ /** A node of a tree, with its children inside it. */
13
15
  export type Nested = {
16
+ /** The node's name. */
14
17
  name: string;
18
+ /** The node's size; only a node without children counts. @default 0 */
15
19
  value?: number;
20
+ /** The nodes under this one. */
16
21
  children?: Nested[];
17
22
  };
18
- /** The columns of a nested tree, or of several. Nodes that share a name stay separate nodes. */
23
+ /**
24
+ * The columns of a nested tree, or of several. Nodes that share a name stay separate nodes.
25
+ * @param tree A root, or a list of roots.
26
+ */
19
27
  export declare function fromTree(tree: Nested | Nested[]): {
20
28
  nodes: string[];
21
29
  parent: number[];
@@ -26,7 +34,7 @@ export declare function first(list: number[], count: number, before: (a: number,
26
34
  /**
27
35
  * What both tree marks lay out. Sizes are shares of the largest value, so that sums of them
28
36
  * cannot overflow. `tint` numbers the branches that get a colour each: the roots, or the
29
- * children of a single root, which itself has -1. Nothing here recurses, so a tree may be as
37
+ * children of a single root, which itself has -1 unless it is the only node. Nothing here recurses, so a tree may be as
30
38
  * deep as it likes. A parent out of range, a node that is its own parent and a cycle throw.
31
39
  */
32
40
  export declare function grow({ nodes, parent, value }: TreeColumns, who: string): {
@@ -1,4 +1,7 @@
1
- /** The columns of a nested tree, or of several. Nodes that share a name stay separate nodes. */
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
+ */
2
5
  export function fromTree(tree) {
3
6
  const [nodes, parent, value] = [[], [], []];
4
7
  // An object met again under itself would never end; the same object in two branches is fine.
@@ -48,7 +51,7 @@ export function first(list, count, before) {
48
51
  /**
49
52
  * What both tree marks lay out. Sizes are shares of the largest value, so that sums of them
50
53
  * cannot overflow. `tint` numbers the branches that get a colour each: the roots, or the
51
- * children of a single root, which itself has -1. Nothing here recurses, so a tree may be as
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
52
55
  * deep as it likes. A parent out of range, a node that is its own parent and a cycle throw.
53
56
  */
54
57
  export function grow({ nodes, parent, value }, who) {
@@ -70,7 +73,7 @@ export function grow({ nodes, parent, value }, who) {
70
73
  const depth = new Int32Array(n);
71
74
  const tint = new Int32Array(n);
72
75
  const order = [...roots];
73
- roots.forEach((root, k) => (tint[root] = roots.length === 1 ? -1 : k));
76
+ roots.forEach((root, k) => (tint[root] = roots.length === 1 && n > 1 ? -1 : k));
74
77
  for (let done = 0; done < order.length; done++) {
75
78
  const node = order[done];
76
79
  kids[node].forEach((kid, k) => {
@@ -12,14 +12,19 @@ import { type TreeColumns } from './tree.js';
12
12
  * label with every other; an index of kept labels when more are wanted.
13
13
  */
14
14
  export type TreemapOptions = TreeColumns & {
15
+ /** A colour for each root, or for each child of a single root, repeated when there are fewer. */
15
16
  colors: string[];
16
17
  /** Text in each rectangle that has room: the node's name by default, your own, or none. */
17
18
  text?: false | ((row: number) => string);
18
19
  /** Each branch starts at this opacity at the top of its room and reaches its full colour at the bottom. */
19
20
  shade?: number;
21
+ /** The mark's opacity, from 0 to 1. @default 1 */
20
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. */
21
24
  key?: string;
25
+ /** The mark's name in a legend, a tooltip and the chart's description. */
22
26
  label?: string;
27
+ /** Marks of one group are focused, dimmed and hidden together; the label when left out. */
23
28
  group?: string;
24
29
  };
25
30
  export declare function treemap(o: TreemapOptions): Mark;
@@ -1,3 +1,4 @@
1
+ import { textWidth } from '../text.js';
1
2
  import { gradient, painter } from '../paint.js';
2
3
  import { first, grow } from './tree.js';
3
4
  export function treemap(o) {
@@ -84,7 +85,11 @@ export function treemap(o) {
84
85
  const sheetOf = (i) => (o.shade === undefined ? shade(i) : tint[i]);
85
86
  for (const i of shown) {
86
87
  if (!sheets.has(sheetOf(i))) {
87
- const branch = tint[i] < 0 ? roots[0] : roots.length === 1 ? kids[roots[0]][tint[i]] : roots[tint[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]];
88
93
  const [x, y, w, h] = room.subarray(branch * 4, branch * 4 + 4);
89
94
  const ink = painter(ctx, o.shade === undefined ? undefined : gradient(o.shade, 1), shade(i))([x, y, x + w, y + h]);
90
95
  sheets.set(sheetOf(i), { path: new Path2D(), ink });
@@ -95,7 +100,7 @@ export function treemap(o) {
95
100
  for (const i of o.text === false ? [] : largest) {
96
101
  const text = o.text ? o.text(i) : named(i);
97
102
  const [x, y, w, h] = box(i);
98
- if (!text || (text.length * 7 + 8) * dpr > w || 20 * dpr > h)
103
+ if (!text || (textWidth(text) + 8) * dpr > w || 20 * dpr > h)
99
104
  continue;
100
105
  labels.push({
101
106
  x: x + 4 * dpr,
package/dist/paint.d.ts CHANGED
@@ -13,7 +13,12 @@ export type Gradient = {
13
13
  };
14
14
  /** What a shape is filled or a line drawn with: a CSS colour, or two blended across the mark. */
15
15
  export type Paint = string | Gradient;
16
- /** A blend of two stops in a direction on the screen: gradient(0.4, 0) fades a mark's own colour out downwards. */
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
+ */
17
22
  export declare const gradient: (from: string | number, to: string | number, toward?: Gradient['toward']) => Gradient;
18
23
  /** The brand's blend, cyan to violet, left to right. */
19
24
  export declare const brand: Gradient;
package/dist/paint.js CHANGED
@@ -24,7 +24,12 @@ export const paletteDark = [
24
24
  '#059669',
25
25
  '#6366f1',
26
26
  ];
27
- /** A blend of two stops in a direction on the screen: gradient(0.4, 0) fades a mark's own colour out downwards. */
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
+ */
28
33
  export const gradient = (from, to, toward = 'down') => ({ from, to, toward });
29
34
  /** The brand's blend, cyan to violet, left to right. */
30
35
  export const brand = gradient('#06b6d4', '#8b5cf6', 'right');
package/dist/scale.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { Axis, Col, Room } from './types.js';
2
2
  /** The 1, 2 or 5 times a power of ten that cuts the span into at most `max` steps. */
3
3
  export declare function niceStep(span: number, max?: number): number;
4
4
  export declare function extent(cols: Col[]): [number, number];
5
- /** Whether labels of this many characters fit side by side (x) or stacked (y) along the axis. */
5
+ /** Whether this many labels, the widest so many CSS pixels wide, fit side by side (x) or stacked (y) along the axis. */
6
6
  export declare const fits: (count: number, widest: number, room: Room) => boolean;
7
7
  /** At most the ticks whose labels fit: when even two collide, only the first is kept. */
8
8
  export declare const thin: <T>(ticks: T[], widest: number, room?: Room) => T[];
package/dist/scale.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { textWidth, widest as widestOf } from './text.js';
1
2
  /** The 1, 2 or 5 times a power of ten that cuts the span into at most `max` steps. */
2
3
  export function niceStep(span, max = 10) {
3
4
  const mag = 10 ** Math.floor(Math.log10(span / max));
@@ -18,8 +19,8 @@ export function extent(cols) {
18
19
  }
19
20
  return lo > hi ? [0, 1] : [lo, hi];
20
21
  }
21
- /** Whether labels of this many characters fit side by side (x) or stacked (y) along the axis. */
22
- export const fits = (count, widest, room) => count * (room.horizontal ? widest * 7 + 16 : 22) <= Math.max(room.length, 1);
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);
23
24
  /** At most the ticks whose labels fit: when even two collide, only the first is kept. */
24
25
  export const thin = (ticks, widest, room) => room && ticks.length > 1 && !fits(ticks.length, widest, room) ? ticks.slice(0, 1) : ticks;
25
26
  /** The ticks `keep` picks for the least k whose labels fit, every k-th by default; the first when it picks none. */
@@ -76,7 +77,7 @@ export function scale(domain, of, room, { step: given, format = String } = {}) {
76
77
  }
77
78
  return ticks;
78
79
  };
79
- const widest = () => Math.max(...ticks.map(v => format(v).length));
80
+ const widest = () => widestOf(ticks.map(format), room?.horizontal ? textWidth : () => 0);
80
81
  let ticks = at(step);
81
82
  if (fixed)
82
83
  return {
@@ -1,9 +1,12 @@
1
1
  import type { Axis } from '../types.js';
2
2
  export type BandAxis = Axis & {
3
+ /** The categories in order; category i sits at i. */
3
4
  categories: readonly string[];
4
5
  };
5
6
  /** Category i sits at x = i and owns the band from i - 0.5 to i + 0.5. */
6
7
  export declare const band: (o: {
8
+ /** The axis title; an empty string shows none. */
7
9
  label: string;
10
+ /** The categories in order, each labelled where there is room. */
8
11
  categories: readonly string[];
9
12
  }) => BandAxis;
@@ -1,3 +1,4 @@
1
+ import { widest } from '../text.js';
1
2
  function resolve({ categories, domain }, _extent, room) {
2
3
  const n = categories.length;
3
4
  const [first, last] = typeof domain === 'string'
@@ -7,7 +8,10 @@ function resolve({ categories, domain }, _extent, room) {
7
8
  // Every category is labelled when they fit; otherwise every second, third and so on.
8
9
  let every = Math.ceil(shown.length / 20) || 1;
9
10
  if (room) {
10
- const each = room.horizontal ? shown.reduce((w, c) => Math.max(w, c.length), 0) * 7 + 16 : 22;
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;
11
15
  every = Math.ceil(shown.length / Math.max(1, Math.floor(room.length / each))) || 1;
12
16
  }
13
17
  const ticks = [];
@@ -1,3 +1,6 @@
1
1
  import type { Axis } from '../types.js';
2
- /** An axis that places the marks and shows nothing itself: no ticks, no grid lines and no label. */
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
+ */
3
6
  export declare const bare: (domain?: Axis['domain']) => Axis;
@@ -3,5 +3,8 @@ function resolve(axis, extent) {
3
3
  const { lo, hi } = scale(axis.domain, typeof axis.domain === 'string' ? extent() : axis.domain);
4
4
  return { lo, hi, ticks: [] };
5
5
  }
6
- /** An axis that places the marks and shows nothing itself: no ticks, no grid lines and no label. */
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
+ */
7
10
  export const bare = (domain = 'data') => ({ domain, label: '', scale: resolve });
@@ -13,6 +13,8 @@ export type ColorScale = {
13
13
  * browser's is; a perceptual blend or a byte-order check when one of them matters.
14
14
  */
15
15
  export declare function sequential(o?: {
16
+ /** The colours to run through, as #rgb or #rrggbb. @default ['#f7fbff', '#6baed6', '#08306b'] */
16
17
  stops?: string[];
18
+ /** The values that get the first and the last colour; the extent of the data when left out. */
17
19
  domain?: [number, number];
18
20
  }): ColorScale;
@@ -1,7 +1,10 @@
1
1
  import type { Axis, Col, Span } from '../types.js';
2
2
  export type LogAxis = Axis & {
3
+ /** The logarithm's base. */
3
4
  base: number;
5
+ /** A real value's logarithm, the space the marks' columns are in; one at or below zero becomes NaN. */
4
6
  to(real: number, axis: Axis): number;
7
+ /** The inverse of `to`: the base raised to a value in the marks' space. */
5
8
  from(v: number, axis: Axis): number;
6
9
  /** The values in the axis's space, to give a mark; one at or below zero becomes NaN, a gap. */
7
10
  col(values: readonly number[]): number[];
@@ -9,11 +12,15 @@ export type LogAxis = Axis & {
9
12
  col(values: Col): number[] | Float64Array;
10
13
  };
11
14
  export type LogOptions = {
15
+ /** The axis title; an empty string shows none. */
12
16
  label: string;
17
+ /** The logarithm's base: above zero, finite and not 1. @default 10 */
13
18
  base?: number;
14
- /** Real values, both above zero; 'nice' widens the extent of the data to whole powers of the base. */
19
+ /** Real values, both above zero; 'nice' widens the extent of the data to whole powers of the base. @default 'nice' */
15
20
  domain?: Span | 'nice' | 'data';
21
+ /** The text of each tick, from its real value. @default String */
16
22
  format?: Axis['format'];
23
+ /** Which marks the extent is taken from, as on any axis. @default 'shown' */
17
24
  fit?: Axis['fit'];
18
25
  };
19
26
  /** An axis whose marks are given columns in log space: `y.col(values)`, or `y.to(v, y)` for one value. */
@@ -1,4 +1,5 @@
1
1
  import { fits, scale as linear, spaced } from '../scale.js';
2
+ import { widest as widestOf } from '../text.js';
2
3
  // log10 and log2 are exact at their own powers; another base's division can land a hair off one.
3
4
  const lg = (v, base) => {
4
5
  if (base === 10)
@@ -48,7 +49,7 @@ function resolve(axis, extent, room) {
48
49
  powers.push(t);
49
50
  }
50
51
  }
51
- const widest = (ts) => ts.reduce((w, t) => Math.max(w, t.label.length), 0);
52
+ const widest = (ts) => widestOf(ts.map(t => t.label));
52
53
  const ordered = (ticks) => ({
53
54
  lo,
54
55
  hi,
@@ -67,7 +68,7 @@ function resolve(axis, extent, room) {
67
68
  ...real.ticks.flatMap(v => (v > 0 ? [{ value: lg(v, base), label: format(v) }] : [])),
68
69
  ];
69
70
  const gap = room
70
- ? ((room.horizontal ? widest(offered) * 7 + 16 : 22) / Math.max(room.length, 1)) * (b - a)
71
+ ? ((room.horizontal ? widest(offered) + 16 : 22) / Math.max(room.length, 1)) * (b - a)
71
72
  : 1e-9;
72
73
  const kept = [];
73
74
  for (const t of offered)
@@ -1,17 +1,23 @@
1
1
  import type { Axis } from '../types.js';
2
2
  declare const UNITS: readonly ['year', 'month', 'day', 'hour', 'minute', 'second'];
3
3
  export type TimeAxis = Axis & {
4
+ /** Whether tick boundaries and labels are in UTC rather than the browser's zone. */
4
5
  utc: boolean;
6
+ /** Ticks every this many units. */
5
7
  every?: number;
8
+ /** The unit whose boundaries the ticks fall on. */
6
9
  unit?: (typeof UNITS)[number];
7
10
  };
8
11
  export type TimeOptions = {
12
+ /** The axis title; an empty string shows none. */
9
13
  label: string;
10
14
  /** Millisecond timestamps; the extent of the data when omitted. */
11
15
  domain?: [number, number];
12
16
  /** Tick boundaries and labels in UTC (the default) or in the browser's zone. */
13
17
  utc?: boolean;
18
+ /** The text of each tick, from its timestamp; English month names and a 24-hour clock when left out. */
14
19
  format?: Axis['format'];
20
+ /** Which marks the extent is taken from, as on any axis. @default 'shown' */
15
21
  fit?: Axis['fit'];
16
22
  } & ({
17
23
  /**
@@ -26,5 +32,6 @@ export type TimeOptions = {
26
32
  every?: undefined;
27
33
  });
28
34
  export declare const make: ([y, mo, d, h, mi, s]: number[], utc: boolean) => number;
35
+ /** An axis of millisecond timestamps, ticked on calendar boundaries and labelled as dates and times. */
29
36
  export declare const time: ({ label, domain, utc, every, unit, format, fit }: TimeOptions) => TimeAxis;
30
37
  export {};
@@ -1,4 +1,5 @@
1
1
  import { niceStep, spaced, thin } from '../scale.js';
2
+ import { estimated, textWidth, widest as widestOf } from '../text.js';
2
3
  const UNITS = ['year', 'month', 'day', 'hour', 'minute', 'second'];
3
4
  const MONTHS = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
4
5
  const S = 1e3;
@@ -71,7 +72,7 @@ function label([y, mo, d, h, mi, s], field) {
71
72
  }
72
73
  function resolve(axis, extent, room) {
73
74
  const { lo, hi, ticks, fixed, keep } = stepped(axis, typeof axis.domain === 'string' ? extent() : axis.domain, room);
74
- const widest = ticks.reduce((w, t) => Math.max(w, t.label.length), 0);
75
+ const widest = widestOf(ticks.map(t => t.label), axis.format ? textWidth : estimated);
75
76
  return {
76
77
  lo,
77
78
  hi,
@@ -169,6 +170,7 @@ function stepped(axis, [lo, hi], room) {
169
170
  }
170
171
  return { lo, hi, ticks, fixed, keep };
171
172
  }
173
+ /** An axis of millisecond timestamps, ticked on calendar boundaries and labelled as dates and times. */
172
174
  export const time = ({ label, domain, utc = true, every, unit, format, fit }) => ({
173
175
  domain: domain ?? 'data',
174
176
  label,
package/dist/text.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export declare const FONT = "12px system-ui,sans-serif";
2
+ export declare const estimated: (s: string) => number;
3
+ /**
4
+ * A label's width in CSS pixels. Plain numbers are estimated, as the first measurement on a page costs
5
+ * about 3 ms (13 ms at 4x CPU slowdown, measured 2026-11-25), which a chart of numbers alone should not pay.
6
+ */
7
+ export declare function textWidth(s: string): number;
8
+ export declare const widest: (labels: Iterable<string>, width?: typeof textWidth) => number;
package/dist/text.js ADDED
@@ -0,0 +1,33 @@
1
+ export const FONT = '12px system-ui,sans-serif';
2
+ const PLAIN = /^[\d\s.,:%+−-]*$/;
3
+ const widths = new Map();
4
+ let pen;
5
+ export const estimated = (s) => s.length * 7;
6
+ /**
7
+ * A label's width in CSS pixels. Plain numbers are estimated, as the first measurement on a page costs
8
+ * about 3 ms (13 ms at 4x CPU slowdown, measured 2026-11-25), which a chart of numbers alone should not pay.
9
+ */
10
+ export function textWidth(s) {
11
+ if (PLAIN.test(s))
12
+ return estimated(s);
13
+ let w = widths.get(s);
14
+ if (w !== undefined)
15
+ return w;
16
+ if (pen === undefined) {
17
+ pen = typeof document === 'undefined' ? null : document.createElement('canvas').getContext('2d');
18
+ if (pen)
19
+ pen.font = FONT;
20
+ }
21
+ if (!pen)
22
+ return estimated(s);
23
+ if (widths.size >= 4096)
24
+ widths.clear();
25
+ widths.set(s, (w = pen.measureText(s).width));
26
+ return w;
27
+ }
28
+ export const widest = (labels, width = textWidth) => {
29
+ let w = 0;
30
+ for (const l of labels)
31
+ w = Math.max(w, width(l));
32
+ return w;
33
+ };
@@ -1,9 +1,9 @@
1
1
  import type { Col } from './types.js';
2
2
  /**
3
- * Counts values per bin, as the bounds of one bar per bin. `by` is the bin edges in ascending
4
- * order, or the number of equal bins across the extent of the finite values (rounded to a whole
5
- * number of at least 1, and 1 when it is not finite; Sturges' rule by default). A bin holds its lower edge and the last bin
3
+ * Counts values per bin, as the bounds of one bar per bin. A bin holds its lower edge and the last bin
6
4
  * also its upper one; NaN, infinite values and values outside the edges are not counted.
5
+ * @param values The values to count.
6
+ * @param by The bin edges in ascending order, or how many equal bins span the finite values, rounded and at least 1; Sturges' rule when left out.
7
7
  */
8
8
  export declare function bin(values: Col, by?: number | Col): {
9
9
  x1: Float64Array<ArrayBufferLike>;
@@ -14,25 +14,29 @@ export declare function bin(values: Col, by?: number | Col): {
14
14
  /**
15
15
  * Stacks series that share their x column, row by row: each gets the y1 and y2 of its layer.
16
16
  * Negative values stack downwards from zero, apart from the positive ones; NaN counts as zero.
17
+ * @param series The y column of each layer, stacked in this order.
17
18
  */
18
19
  export declare function stack(series: Col[]): {
19
20
  y1: Float64Array<ArrayBuffer>;
20
21
  y2: Float64Array<ArrayBuffer>;
21
22
  }[];
22
23
  /**
23
- * The two bounds of one bar per category on a band axis, where category i sits at i: a count for
24
- * categories 0 to n - 1, or the indices themselves. `width` is the share of the category that the
25
- * bars of all slots take together; grouped bars pass their own `slot` out of `of`.
24
+ * The two bounds of one bar per category on a band axis, where category i sits at i.
25
+ * @param at A count for categories 0 to n - 1, or the indices themselves.
26
26
  */
27
- export declare function bands(at: number | Col, { slot, of, width }?: {
27
+ export declare function bands(at: number | Col, { slot, of, width, }?: {
28
+ /** Which of the `of` slots in each category these bars take, for grouped bars. @default 0 */
28
29
  slot?: number;
30
+ /** How many slots each category is split into. @default 1 */
29
31
  of?: number;
32
+ /** The share of the category that the bars of all slots take together. @default 0.8 */
30
33
  width?: number;
31
34
  }): [Float64Array, Float64Array];
32
35
  /**
33
36
  * The five numbers a box plot draws, for each group of values: minimum, quartiles and maximum.
34
37
  * Quartiles are interpolated between the two nearest values; NaN and infinite values are left out,
35
38
  * and a group with nothing left gives NaN, which draws nothing.
39
+ * @param groups The values of each group, one box per group.
36
40
  */
37
41
  export declare function fiveNumbers(groups: Col[]): {
38
42
  min: Float64Array<ArrayBuffer>;
@@ -1,8 +1,8 @@
1
1
  /**
2
- * Counts values per bin, as the bounds of one bar per bin. `by` is the bin edges in ascending
3
- * order, or the number of equal bins across the extent of the finite values (rounded to a whole
4
- * number of at least 1, and 1 when it is not finite; Sturges' rule by default). A bin holds its lower edge and the last bin
2
+ * Counts values per bin, as the bounds of one bar per bin. A bin holds its lower edge and the last bin
5
3
  * also its upper one; NaN, infinite values and values outside the edges are not counted.
4
+ * @param values The values to count.
5
+ * @param by The bin edges in ascending order, or how many equal bins span the finite values, rounded and at least 1; Sturges' rule when left out.
6
6
  */
7
7
  export function bin(values, by) {
8
8
  let edges;
@@ -58,6 +58,7 @@ export function bin(values, by) {
58
58
  /**
59
59
  * Stacks series that share their x column, row by row: each gets the y1 and y2 of its layer.
60
60
  * Negative values stack downwards from zero, apart from the positive ones; NaN counts as zero.
61
+ * @param series The y column of each layer, stacked in this order.
61
62
  */
62
63
  export function stack(series) {
63
64
  const n = series[0]?.length ?? 0;
@@ -76,11 +77,10 @@ export function stack(series) {
76
77
  });
77
78
  }
78
79
  /**
79
- * The two bounds of one bar per category on a band axis, where category i sits at i: a count for
80
- * categories 0 to n - 1, or the indices themselves. `width` is the share of the category that the
81
- * bars of all slots take together; grouped bars pass their own `slot` out of `of`.
80
+ * The two bounds of one bar per category on a band axis, where category i sits at i.
81
+ * @param at A count for categories 0 to n - 1, or the indices themselves.
82
82
  */
83
- export function bands(at, { slot = 0, of = 1, width = 0.8 } = {}) {
83
+ export function bands(at, { slot = 0, of = 1, width = 0.8, } = {}) {
84
84
  const n = typeof at === 'number' ? at : at.length;
85
85
  const lo = new Float64Array(n);
86
86
  const hi = new Float64Array(n);
@@ -96,6 +96,7 @@ export function bands(at, { slot = 0, of = 1, width = 0.8 } = {}) {
96
96
  * The five numbers a box plot draws, for each group of values: minimum, quartiles and maximum.
97
97
  * Quartiles are interpolated between the two nearest values; NaN and infinite values are left out,
98
98
  * and a group with nothing left gives NaN, which draws nothing.
99
+ * @param groups The values of each group, one box per group.
99
100
  */
100
101
  export function fiveNumbers(groups) {
101
102
  const column = () => new Float64Array(groups.length);