swiftplot 0.1.1 → 0.4.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 (96) hide show
  1. package/README.md +86 -61
  2. package/dist/chart.d.ts +4 -0
  3. package/dist/chart.js +172 -89
  4. package/dist/charts/build.d.ts +53 -0
  5. package/dist/charts/build.js +69 -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 +564 -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 +20 -15
  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 +20 -5
  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/rects.js +15 -5
  67. package/dist/marks/rule.d.ts +19 -0
  68. package/dist/marks/rule.js +4 -1
  69. package/dist/marks/sankey.d.ts +14 -2
  70. package/dist/marks/sankey.js +4 -1
  71. package/dist/marks/sunburst.d.ts +5 -0
  72. package/dist/marks/sunburst.js +2 -1
  73. package/dist/marks/ticks.d.ts +4 -1
  74. package/dist/marks/tree.d.ts +14 -6
  75. package/dist/marks/tree.js +6 -3
  76. package/dist/marks/treemap.d.ts +5 -0
  77. package/dist/marks/treemap.js +7 -2
  78. package/dist/paint.d.ts +6 -1
  79. package/dist/paint.js +6 -1
  80. package/dist/scale.d.ts +1 -1
  81. package/dist/scale.js +4 -3
  82. package/dist/scales/band.d.ts +3 -0
  83. package/dist/scales/band.js +5 -1
  84. package/dist/scales/bare.d.ts +4 -1
  85. package/dist/scales/bare.js +4 -1
  86. package/dist/scales/color.d.ts +2 -0
  87. package/dist/scales/log.d.ts +8 -1
  88. package/dist/scales/log.js +3 -2
  89. package/dist/scales/time.d.ts +7 -0
  90. package/dist/scales/time.js +3 -1
  91. package/dist/text.d.ts +8 -0
  92. package/dist/text.js +33 -0
  93. package/dist/transforms.d.ts +11 -7
  94. package/dist/transforms.js +8 -7
  95. package/dist/types.d.ts +44 -15
  96. package/package.json +2 -2
@@ -1,5 +1,7 @@
1
+ // A move falls back to the cross-fade when one repaint of the chart takes longer than this, in ms.
2
+ const REPAINT_BUDGET = 6;
1
3
  /**
2
- * Marks are revealed from the left (or fade in) on the first paint. When the data changes they
4
+ * Marks are uncovered (or fade in) on the first paint. When the data changes they
3
5
  * move to their new shape if every changed mark can (bars, row for row) and cross-fade otherwise.
4
6
  * The entrance and the cross-fade blend two pictures of the chart, so a frame costs the same
5
7
  * whatever the data size; a move repaints the chart each frame.
@@ -7,8 +9,6 @@
7
9
  * while updates arrive less than `duration` ms apart.
8
10
  * ponytail: marks on WebGL take part in the entrance, but swap at once when the data changes.
9
11
  */
10
- // A move falls back to the cross-fade when one repaint of the chart takes longer than this, in ms.
11
- const REPAINT_BUDGET = 6;
12
12
  export function animate({ duration = 300, enter = 'reveal' } = {}) {
13
13
  if (!Number.isFinite(duration) || duration <= 0)
14
14
  return () => ({ destroy() { } });
@@ -1,13 +1,15 @@
1
1
  import type { Feature, Span } from '../types.js';
2
+ /** A drag over the plot marks out a range on x, y or both, which is reported on release and, with `zoom`, shown. */
2
3
  export type BrushOptions = {
3
4
  /** 'x' when left out: the rectangle spans the plot's height and the range its whole y window. */
4
5
  axes?: 'x' | 'y' | 'xy';
5
6
  /** Show the range brushed once it is reported, on the brushed axes, and drop the rectangle. */
6
7
  zoom?: boolean;
7
- /** The range brushed, in axis values, on release; null once a range reported is cleared. */
8
+ /** The range brushed, in axis values, on release, with y2 when the chart has a second axis; null once a range reported is cleared. */
8
9
  onSelect?(range: {
9
10
  x: Span;
10
11
  y: Span;
12
+ y2?: Span;
11
13
  } | null): void;
12
14
  };
13
15
  export declare function brush(b?: BrushOptions): Feature;
@@ -103,8 +103,9 @@ export function brush(b = {}) {
103
103
  x: bx ? asc(chart.xValue(kept.left), chart.xValue(kept.right)) : view.x,
104
104
  y: by ? asc(chart.yValue(kept.bottom), chart.yValue(kept.top)) : view.y,
105
105
  };
106
+ const y2 = view.y2 && (by ? asc(chart.yValue(kept.bottom, 'y2'), chart.yValue(kept.top, 'y2')) : view.y2);
106
107
  reported = !b.zoom;
107
- b.onSelect?.({ x: span('x', picked.x), y: span('y', picked.y) });
108
+ b.onSelect?.({ x: span('x', picked.x), y: span('y', picked.y), ...(y2 && { y2: span('y2', y2) }) });
108
109
  if (!b.zoom || gone)
109
110
  return;
110
111
  clear();
@@ -8,8 +8,6 @@ export type LegendOptions = {
8
8
  /**
9
9
  * One entry per group of labelled marks, in rows above the plot; pointing at one focuses its group,
10
10
  * and with `toggle` (the default) a click or Enter or Space hides or shows it.
11
- * ponytail: entry widths are estimated at 7 px a character, because measuring text (DOM or canvas)
12
- * costs about 16 ms at 4x CPU slowdown before the first paint. A row that turns out wider is cut off,
13
- * and a label wider than the plot ends in an ellipsis, titled once layout shows it cut.
11
+ * A label wider than the plot ends in an ellipsis, titled once layout shows it cut.
14
12
  */
15
13
  export declare function legend({ toggle, rows: cap }?: LegendOptions): Feature;
@@ -1,3 +1,4 @@
1
+ import { textWidth } from '../text.js';
1
2
  const ROW = 20;
2
3
  const SWATCH = 16;
3
4
  const GAP = 14;
@@ -12,9 +13,7 @@ function ends(lo, hi) {
12
13
  /**
13
14
  * One entry per group of labelled marks, in rows above the plot; pointing at one focuses its group,
14
15
  * and with `toggle` (the default) a click or Enter or Space hides or shows it.
15
- * ponytail: entry widths are estimated at 7 px a character, because measuring text (DOM or canvas)
16
- * costs about 16 ms at 4x CPU slowdown before the first paint. A row that turns out wider is cut off,
17
- * and a label wider than the plot ends in an ellipsis, titled once layout shows it cut.
16
+ * A label wider than the plot ends in an ellipsis, titled once layout shows it cut.
18
17
  */
19
18
  export function legend({ toggle = true, rows: cap } = {}) {
20
19
  return chart => {
@@ -61,7 +60,7 @@ export function legend({ toggle = true, rows: cap } = {}) {
61
60
  : ramps.length;
62
61
  const rows = [[]];
63
62
  const most = Math.max(1, Math.floor(cap ?? budget - kept) || 1);
64
- const wide = (e) => SWATCH + e.label.length * 7;
63
+ const wide = (e) => SWATCH + textWidth(e.label);
65
64
  const more = (n) => `+${n} more`;
66
65
  let used = 0;
67
66
  let rest = [];
@@ -91,7 +90,7 @@ export function legend({ toggle = true, rows: cap } = {}) {
91
90
  ...strips.splice(kept).map(s => s.label ?? s.ends.join('–')),
92
91
  ];
93
92
  const last = rows.at(-1);
94
- while (unlisted.length && last.length > 1 && used + GAP + more(unlisted.length).length * 7 > width) {
93
+ while (unlisted.length && last.length > 1 && used + GAP + textWidth(more(unlisted.length)) > width) {
95
94
  const out = last.pop();
96
95
  unlisted.unshift(out.label);
97
96
  used -= GAP + wide(out);
@@ -1,14 +1,22 @@
1
1
  import type { Feature } from '../types.js';
2
+ /** A tooltip at the pointer: a row for each visible, labelled line or area at its x, or the text for what is nearest it. */
2
3
  export type TooltipOptions =
3
- /** One row per labelled mark that can be sampled at the pointer's x, with a crosshair; a band gives its thickness. */
4
+ /** A row for each labelled line or area not hidden, at the pointer's x, with a crosshair; a band gives its thickness. */
4
5
  {
6
+ /** Shows each labelled line and area that is not hidden, at the pointer's x. */
5
7
  mode: 'x';
8
+ /** The first line of the tooltip, for the x at the pointer. */
6
9
  title: (x: number) => string;
7
- value: (y: number) => string;
10
+ /** A mark's value as text, after its label, for its y on the axis it is drawn against. */
11
+ value: (y: number, axis: 'y' | 'y2') => string;
12
+ /** The x to read the title and the marks at, such as the nearest row's, from the one at the pointer, both in the marks' space. */
13
+ snap?: (x: number) => number;
8
14
  }
9
15
  /** x and y are where the mark says it was hit, y being the row's own value where it has one; row is its row there. */
10
16
  | {
17
+ /** Shows what is nearest the pointer, within 20 CSS pixels. */
11
18
  mode: 'nearest';
19
+ /** The tooltip's text, from the label of the mark or part hit, its x and y, and its row. */
12
20
  text: (label: string, x: number, y: number, row?: number) => string;
13
21
  };
14
22
  export declare function tooltip(t: TooltipOptions): Feature;
@@ -69,50 +69,55 @@ export function tooltip(t) {
69
69
  // A mark made of named parts, such as slices, is spoken for by the part that was hit.
70
70
  const part = hit.row === undefined ? undefined : hit.mark.entries?.[hit.row];
71
71
  const { label = '', color } = part ?? hit.mark;
72
- const text = t.text(label, chart.real('x', hit.x), chart.real('y', hit.value ?? hit.y), hit.row);
73
- show(chart.xPx(hit.x), chart.yPx(hit.y), [{ text, color: hit.color ?? color }]);
72
+ const on = chart.yAxis(hit.mark);
73
+ const text = t.text(label, chart.real('x', hit.x), chart.real(on, hit.value ?? hit.y), hit.row);
74
+ show(chart.xPx(hit.x), chart.yPx(hit.y, on), [{ text, color: hit.color ?? color }]);
74
75
  return;
75
76
  }
76
77
  const m = chart.margin();
77
78
  const off = py !== undefined && (py < m.top || py > height - m.bottom);
78
79
  if (px < m.left || px > width - m.right || off)
79
80
  return hide();
80
- const v = chart.xValue(px);
81
+ const under = chart.xValue(px);
82
+ const v = t.snap?.(under) ?? under;
81
83
  const rows = [{ text: t.title(chart.real('x', v)) }];
82
- // A line within reach of the pointer is focused before the filled mark the pointer is in,
83
- // and of several filled marks the one drawn last.
84
+ // A line within reach of the pointer is focused before the filled mark the pointer is in, and of
85
+ // several filled marks the thinnest there, so each of overlapping areas has a part that picks it.
84
86
  let near = FOCUS_PX;
85
87
  let line;
86
88
  let fill;
89
+ let thin = Infinity;
87
90
  for (const k of chart.marks()) {
88
91
  if (!k.sampleAtX)
89
92
  continue;
90
93
  const s = k.sampleAtX(v);
94
+ const on = chart.yAxis(k);
91
95
  const value = typeof s === 'number'
92
- ? chart.real('y', s)
93
- : s &&
94
- (s.band ? chart.real('y', s.y2) - chart.real('y', s.y1) : chart.real('y', s.value ?? s.y2));
96
+ ? chart.real(on, s)
97
+ : s && (s.band ? chart.real(on, s.y2) - chart.real(on, s.y1) : chart.real(on, s.value ?? s.y2));
95
98
  if (k.label !== undefined) {
96
99
  rows.push({
97
- text: `${k.label}: ${value === undefined ? '–' : t.value(value)}`,
100
+ text: `${k.label}: ${value === undefined ? '–' : t.value(value, on)}`,
98
101
  color: k.color,
99
102
  });
100
103
  }
101
104
  if (s === undefined || k.group === undefined || py === undefined)
102
105
  continue;
103
106
  if (typeof s === 'number') {
104
- const gap = Math.abs(chart.yPx(s) - py);
107
+ const gap = Math.abs(chart.yPx(s, on) - py);
105
108
  if (gap < near)
106
109
  [near, line] = [gap, k.group];
107
110
  }
108
111
  else {
109
- const a = chart.yPx(s.y1);
110
- const b = chart.yPx(s.y2);
111
- if (a !== b && py >= Math.min(a, b) && py <= Math.max(a, b))
112
- fill = k.group;
112
+ const a = chart.yPx(s.y1, on);
113
+ const b = chart.yPx(s.y2, on);
114
+ const span = Math.abs(a - b);
115
+ if (span && py >= Math.min(a, b) && py <= Math.max(a, b) && span <= thin)
116
+ [thin, fill] = [span, k.group];
113
117
  }
114
118
  }
115
- const group = line ?? fill ?? null;
119
+ // A mark that answers the pointer where it is, such as a bar, is focused where neither applies.
120
+ const group = line ?? fill ?? (py === undefined ? undefined : chart.hit(px, py, 0)?.mark.group) ?? null;
116
121
  cross.style.display = '';
117
122
  cross.style.left = `${px}px`;
118
123
  cross.style.top = `${m.top}px`;
@@ -1,4 +1,5 @@
1
1
  import type { Feature } from '../types.js';
2
+ /** Zooms with the wheel (ctrl or cmd held, by default) or a pinch and pans with a drag, on x, y or both; a double-click on the plot shows the whole of each zoomed axis again. */
2
3
  export type ZoomOptions = {
3
4
  /** 'x' when left out. */
4
5
  axes?: 'x' | 'y' | 'xy';
@@ -1,4 +1,6 @@
1
1
  import { locate as where, settle as release, SLOP_PX, unselect } from './pointer.js';
2
+ // How far a drag that pans both axes must move up or down before it pans y: a hand does not move level.
3
+ const LEVEL_PX = 8;
2
4
  const DOUBLE_TAP_MS = 300;
3
5
  const DOUBLE_TAP_PX = 20;
4
6
  const TAP_MS = 500;
@@ -54,6 +56,7 @@ export function zoom(z = {}) {
54
56
  if (!still)
55
57
  ask(want);
56
58
  };
59
+ // `lift` is where a drag along both axes left the level: the y spans shown then, and the height it had moved.
57
60
  let from;
58
61
  let settling = 0;
59
62
  const down = (e) => {
@@ -80,10 +83,25 @@ export function zoom(z = {}) {
80
83
  return;
81
84
  drag.active = true;
82
85
  const want = {};
83
- for (const s of sides) {
84
- const [lo, hi] = from.view[s];
85
- const shift = s === 'x' ? (-dx * (hi - lo)) / from.w : (dy * (hi - lo)) / from.h;
86
- want[s] = [lo + shift, hi + shift];
86
+ if (sides.includes('x')) {
87
+ const [lo, hi] = from.view.x;
88
+ const shift = (-dx * (hi - lo)) / from.w;
89
+ want.x = [lo + shift, hi + shift];
90
+ }
91
+ if (sides.includes('y')) {
92
+ const both = sides.length === 2;
93
+ // Until then y is left out, so an axis fitted to the x window goes on fitting; from then on only the
94
+ // height beyond the threshold counts, from the spans shown at that moment, both y axes together.
95
+ if (both && !from.lift && Math.abs(dy) >= LEVEL_PX)
96
+ from.lift = { ...shown(), dy: Math.sign(dy) * LEVEL_PX };
97
+ const base = both ? from.lift : { ...from.view, y2: undefined, dy: 0 };
98
+ for (const k of ['y', 'y2']) {
99
+ const s = base?.[k];
100
+ if (!s)
101
+ continue;
102
+ const shift = ((dy - base.dy) * (s[1] - s[0])) / from.h;
103
+ want[k] = [s[0] + shift, s[1] + shift];
104
+ }
87
105
  }
88
106
  ask(want);
89
107
  };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export { arcs } from './marks/arcs.js';
8
8
  export { area } from './marks/area.js';
9
9
  export { bar } from './marks/bar.js';
10
10
  export { box } from './marks/box.js';
11
+ export { candlestick } from './marks/candlestick.js';
11
12
  export { dots } from './marks/dots.js';
12
13
  export { errorBars } from './marks/errorBars.js';
13
14
  export { funnel } from './marks/funnel.js';
@@ -42,6 +43,7 @@ export type { ArcsOptions } from './marks/arcs.js';
42
43
  export type { AreaOptions } from './marks/area.js';
43
44
  export type { BarOptions } from './marks/bar.js';
44
45
  export type { BoxOptions } from './marks/box.js';
46
+ export type { CandlestickOptions } from './marks/candlestick.js';
45
47
  export type { DotsOptions } from './marks/dots.js';
46
48
  export type { ErrorBarsOptions } from './marks/errorBars.js';
47
49
  export type { FunnelOptions } from './marks/funnel.js';
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ export { arcs } from './marks/arcs.js';
8
8
  export { area } from './marks/area.js';
9
9
  export { bar } from './marks/bar.js';
10
10
  export { box } from './marks/box.js';
11
+ export { candlestick } from './marks/candlestick.js';
11
12
  export { dots } from './marks/dots.js';
12
13
  export { errorBars } from './marks/errorBars.js';
13
14
  export { funnel } from './marks/funnel.js';
@@ -1,14 +1,16 @@
1
1
  import type { Col, Mark } from '../types.js';
2
2
  /**
3
- * Slices of a pie, or of a ring when `inner` (the hole's share of the radius) is above 0, for a
4
- * polar frame. They start at 12 o'clock and run clockwise, each as wide as its value's share of
5
- * the total; a value that is negative or not a number counts as 0. Colours repeat when there are
6
- * fewer than slices. `labels` name the slices for a legend.
3
+ * Slices of a pie, or of a ring, for a polar frame. They start at 12 o'clock and run clockwise,
4
+ * each as wide as its value's share of the total.
7
5
  */
8
6
  export type ArcsOptions = {
7
+ /** Each slice's value; one that is negative or not a number counts as 0. */
9
8
  values: Col;
9
+ /** The slices' colours, repeated when there are fewer than slices. */
10
10
  colors: string[];
11
+ /** The slices' names, for a legend and a tooltip. */
11
12
  labels?: string[];
13
+ /** The hole's share of the radius; above 0 the pie is a ring. @default 0 */
12
14
  inner?: number;
13
15
  /** Text on each slice that has room for it: true for its share as a percentage, or your own. */
14
16
  text?: true | ((row: number, share: number) => string);
@@ -17,9 +19,13 @@ export type ArcsOptions = {
17
19
  * the browser has no conic gradient the slices stay solid.
18
20
  */
19
21
  sweep?: number;
22
+ /** The mark's opacity, from 0 to 1. @default 1 */
20
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. */
21
25
  key?: string;
26
+ /** The mark's name in a legend, a tooltip and the chart's description. */
22
27
  label?: string;
28
+ /** Marks of one group are focused, dimmed and hidden together; the label when left out. */
23
29
  group?: string;
24
30
  };
25
31
  export declare function arcs(o: ArcsOptions): 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
  export function arcs(o) {
4
5
  const { values, colors, inner = 0, alpha = 1 } = o;
@@ -79,7 +80,7 @@ export function arcs(o) {
79
80
  // A slice narrower at the rim than one character has no room, so it is not asked.
80
81
  const text = share * TURN * outer >= 7 * dpr ? said(i, share) : '';
81
82
  const [x, y] = anchor(i);
82
- if (!text || !within(i, x, y, text.length * 7 * dpr, 16 * dpr))
83
+ if (!text || !within(i, x, y, textWidth(text) * dpr, 16 * dpr))
83
84
  return undefined;
84
85
  return { x, y, text, color: '#fff', priority: share };
85
86
  }).filter(l => l !== undefined);
@@ -1,24 +1,25 @@
1
1
  import type { Col, Mark } from '../types.js';
2
2
  import { type Paint } from '../paint.js';
3
3
  import type { Common, Curve } from './common.js';
4
- /**
5
- * Filled between 0 and y, or between y1 and y2. x must be ascending. `fill` fills it with
6
- * something other than its colour. An upright blend runs from the furthest the area reaches to
7
- * its baseline, on both sides of it and whichever side that is, so gradient(0.4, 0) fades the area
8
- * out where it meets the axis. Areas given the same `stack` id are stacked by the chart over the shown
9
- * ones, in the order they are listed; they must share their x. `join` fills it straight across a
10
- * value that is not finite, where it would otherwise leave a gap.
11
- */
4
+ /** Filled between 0 and y, or between y1 and y2. */
12
5
  export type AreaOptions = Common & {
6
+ /** The x of each point, ascending. */
13
7
  x: Col;
8
+ /** How the edges run from point to point: straight, in steps, or smoothly. @default 'linear' */
14
9
  curve?: Curve;
10
+ /** Fills it with something other than its colour; on an unstacked `y` area an upright blend runs from its far edge to its baseline. */
15
11
  fill?: Paint;
12
+ /** Fills straight across a value that is not finite, where it would leave a gap. */
16
13
  join?: boolean;
17
14
  } & ({
15
+ /** The top of the area, which is filled from 0; a value that is not finite leaves a gap. */
18
16
  y: Col;
17
+ /** Areas with the same id are stacked, in the order listed, over the shown ones; they must share x. */
19
18
  stack?: string;
20
19
  } | {
20
+ /** One edge of a band, filled across to y2; a value that is not finite leaves a gap. */
21
21
  y1: Col;
22
+ /** The band's other edge. */
22
23
  y2: Col;
23
24
  });
24
25
  export declare function area(o: AreaOptions): Mark;
@@ -1,6 +1,6 @@
1
1
  import { stack } from '../transforms.js';
2
2
  import { painter } from '../paint.js';
3
- import { base, chains, columns, extent, joined, reach, sampleAt, smooth, span } from './common.js';
3
+ import { base, chains, columns, extent, joined, reach, sampleAt, smooth, span, stackKey } from './common.js';
4
4
  const layers = (members) => stack(members.map(m => m.y)).map((laid, i) => {
5
5
  const { y: _, stack: __, ...rest } = members[i];
6
6
  return area({ ...rest, ...laid });
@@ -11,7 +11,7 @@ export function area(o) {
11
11
  ...layers([o])[0],
12
12
  options: o,
13
13
  arrange: {
14
- key: `area:stack:${o.stack}`,
14
+ key: stackKey('area', o),
15
15
  lay: members => layers(members.map(k => k.options)),
16
16
  },
17
17
  };
@@ -3,41 +3,49 @@ import type { Col, Mark } from '../types.js';
3
3
  import type { Common } from './common.js';
4
4
  /**
5
5
  * One rectangle per row, from (x1, y1) to (x2, y2) in data units; either pair may be in any order.
6
- * A single `y` (or `x`) stands for a pair that starts at zero. `gap` takes so many CSS pixels off
7
- * the side of each bar, which sets touching bars such as a histogram's apart; a bar keeps a pixel
8
- * of its width. `radius` rounds the value end of a bar that starts at zero by so many CSS pixels,
9
- * and all the corners of one that does not; bars too narrow to
10
- * show it, and bars drawn by WebGL, stay square. `fill` fills the bars with something other than
11
- * their colour: a blend runs over each bar, and down a bar that stands on a baseline it runs from
12
- * the value end to the baseline. On the canvas blended bars are filled one at a time.
13
- * Bars given `x`, `y` and a `stack` or `dodge` id are laid out by the chart with the shown bars of
14
- * that id, in the order they are listed: stacked over one another, or side by side in the slots of
15
- * their category, `x` being the category's place on a band axis.
6
+ * A single `y` (or `x`) stands for a pair that starts at zero. On the canvas blended bars are
7
+ * filled one at a time.
16
8
  */
17
9
  export type BarOptions = Common & {
10
+ /** Fills the bars with something other than their colour; a blend runs over each bar, and an upright one on an unstacked `y` bar from its value end to the baseline. */
18
11
  fill?: Paint;
12
+ /** Rounds by so many CSS pixels the value end of a bar from zero, or every corner; WebGL bars stay square. */
19
13
  radius?: number;
14
+ /** Takes so many CSS pixels off the side of each bar, to set touching bars apart; a bar keeps a pixel. */
20
15
  gap?: number;
21
16
  } & ((({
17
+ /** One end of each bar along x, in data units. */
22
18
  x1: Col;
19
+ /** The other end of each bar along x. */
23
20
  x2: Col;
24
21
  } | {
22
+ /** Where each bar ends along x; it starts at zero. */
25
23
  x: Col;
26
24
  }) & ({
25
+ /** One end of each bar along y, in data units. */
27
26
  y1: Col;
27
+ /** The other end of each bar along y. */
28
28
  y2: Col;
29
29
  } | {
30
+ /** Where each bar ends along y; it starts at zero. */
30
31
  y: Col;
31
32
  }) & {
32
33
  stack?: never;
33
34
  dodge?: never;
35
+ width?: never;
34
36
  }) | ({
37
+ /** The place of each bar's category on a band axis. */
35
38
  x: Col;
39
+ /** Each bar's value. */
36
40
  y: Col;
41
+ /** How much of x the bars of one place take together, in data units. @default 0.8 */
42
+ width?: number;
37
43
  } & ({
44
+ /** Bars with the same id are stacked over one another, in the order listed, among those shown. */
38
45
  stack: string;
39
46
  dodge?: never;
40
47
  } | {
48
+ /** Bars with the same id stand side by side in their category, in the order listed, among those shown. */
41
49
  dodge: string;
42
50
  stack?: never;
43
51
  })));
package/dist/marks/bar.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { glLayer } from '../gl.js';
2
2
  import { painter, stops } from '../paint.js';
3
3
  import { bands, stack } from '../transforms.js';
4
- import { base } from './common.js';
4
+ import { base, stackKey } from './common.js';
5
5
  import { focused, gpuRows } from './rects.js';
6
6
  const arranged = (members) => {
7
7
  const stacked = members[0]?.stack !== undefined && stack(members.map(m => m.y));
8
8
  return members.map((m, i) => {
9
- const { x, y, stack: _, dodge: __, ...rest } = m;
10
- const [x1, x2] = bands(x, stacked ? {} : { slot: i, of: members.length });
9
+ const { x, y, stack: _, dodge: __, width, ...rest } = m;
10
+ const [x1, x2] = bands(x, stacked ? { width } : { slot: i, of: members.length, width });
11
11
  return stacked ? make({ ...rest, x1, x2, ...stacked[i] }, y) : make({ ...rest, x1, x2, y });
12
12
  });
13
13
  };
@@ -53,7 +53,7 @@ export function bar(o) {
53
53
  ...arranged([a])[0],
54
54
  options: o,
55
55
  arrange: {
56
- key: a.stack !== undefined ? `bar:stack:${a.stack}` : `bar:dodge:${a.dodge}`,
56
+ key: a.stack !== undefined ? stackKey('bar', a) : `bar:dodge:${a.dodge}`,
57
57
  lay: members => arranged(members.map(k => k.options)),
58
58
  },
59
59
  };
@@ -82,7 +82,7 @@ function make(o, value) {
82
82
  const up = y2[i] >= 0 ? [r, r, 0, 0] : [0, 0, r, r];
83
83
  const along = x2[i] >= 0 ? [0, r, r, 0] : [r, 0, 0, r];
84
84
  const grounded = (own.y1 ? own.y1[i] : 0) === 0;
85
- path.roundRect(l, t, w, h, grounded ? up : flat ? along : r);
85
+ path.roundRect(l, t, w, h, flat ? along : grounded ? up : r);
86
86
  };
87
87
  const shaped = (box, dpr) => {
88
88
  if (!corner)
@@ -3,17 +3,23 @@ import type { Col, Mark } from '../types.js';
3
3
  import type { Common } from './common.js';
4
4
  /**
5
5
  * A box from q1 to q3 with a line at the median and whiskers out to min and max, one per x.
6
- * `width` is the box's width in x units, so 0.6 on a band axis is 60% of the category.
7
6
  * ponytail: upright only, and the whiskers go to whatever min and max are given; outliers are
8
7
  * for the caller to leave out and draw with dots().
9
8
  */
10
9
  export type BoxOptions = Common & {
10
+ /** Where each box stands along x. */
11
11
  x: Col;
12
+ /** Where the lower whisker ends. */
12
13
  min: Col;
14
+ /** The bottom of the box, the first quartile. */
13
15
  q1: Col;
16
+ /** Where the line across the box is drawn. */
14
17
  median: Col;
18
+ /** The top of the box, the third quartile. */
15
19
  q3: Col;
20
+ /** Where the upper whisker ends. */
16
21
  max: Col;
22
+ /** The box's width in x units, so 0.6 on a band axis is 60% of the category. @default 0.6 */
17
23
  width?: number;
18
24
  /** What the boxes are filled with: a colour, drawn faint, or a blend down each box, drawn as given. */
19
25
  fill?: Paint;
package/dist/marks/box.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { glLayer } from '../gl.js';
2
2
  import { painter, stops } from '../paint.js';
3
- import { base } from './common.js';
3
+ import { base, ends } from './common.js';
4
4
  import { focused, gpuRows } from './rects.js';
5
5
  export function box(o) {
6
6
  const { x, min, q1, median, q3, max, color, alpha = 1, width = 0.6 } = o;
@@ -8,13 +8,7 @@ export function box(o) {
8
8
  const [corner, heavy, faint] = [o.radius ?? 0, o.medianWidth ?? 1.5, o.whiskers ?? 1];
9
9
  const blended = typeof o.fill === 'object' ? o.fill : undefined;
10
10
  // The axis must make room for the boxes, not only for their centres.
11
- let [first, last] = [Infinity, -Infinity];
12
- for (let i = 0; i < n; i++) {
13
- if (x[i] < first && x[i] !== -Infinity)
14
- first = x[i];
15
- if (x[i] > last && x[i] !== Infinity)
16
- last = x[i];
17
- }
11
+ const [first, last] = ends(x);
18
12
  const rows = gpuRows(new WeakMap(), [x], [min, q1, median, q3, max]);
19
13
  // Each box's left, top, right and bottom in device pixels; a row with a number missing has none.
20
14
  const bounds = ({ ox, kx, oy, ky }) => {
@@ -0,0 +1,22 @@
1
+ import type { Col, Mark } from '../types.js';
2
+ import type { Common } from './common.js';
3
+ /** A candle per x: a wick from low to high and a body from open to close. */
4
+ export type CandlestickOptions = Common & {
5
+ /** Where each candle stands along x. */
6
+ x: Col;
7
+ /** One end of the body. */
8
+ open: Col;
9
+ /** The top of the wick. */
10
+ high: Col;
11
+ /** The bottom of the wick. */
12
+ low: Col;
13
+ /** The other end of the body; below `open`, the candle takes the `down` colour. */
14
+ close: Col;
15
+ /** The body's width in x units, so 0.6 on a band axis is 60% of the category. @default 0.6 */
16
+ width?: number;
17
+ /** The colour of a candle whose close is not below its open; `color` unless given. */
18
+ up?: string;
19
+ /** The colour of a candle whose close is below its open; `color` unless given. */
20
+ down?: string;
21
+ };
22
+ export declare function candlestick(o: CandlestickOptions): Mark;