@motionscript/charts 0.1.0-alpha.0 → 0.1.0-alpha.5

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 (124) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +85 -0
  5. package/dist/bar-chart/bar-chart.d.ts +33 -19
  6. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  7. package/dist/bar-chart/bar-chart.js +68 -64
  8. package/dist/bar-chart/bar-chart.js.map +1 -1
  9. package/dist/bar-chart/category-axis.d.ts +2 -2
  10. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  11. package/dist/bar-chart/category-axis.js +2 -2
  12. package/dist/bar-chart/category-axis.js.map +1 -1
  13. package/dist/bar-chart/chart-bars.d.ts +1 -1
  14. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  15. package/dist/bar-chart/chart-bars.js +2 -2
  16. package/dist/bar-chart/chart-bars.js.map +1 -1
  17. package/dist/bar-chart/index.js.map +1 -1
  18. package/dist/bar-chart/shared.d.ts +4 -4
  19. package/dist/bar-chart/shared.d.ts.map +1 -1
  20. package/dist/bar-chart/shared.js +4 -4
  21. package/dist/bar-chart/shared.js.map +1 -1
  22. package/dist/browser/chunks/chunk-5ZPBV6BU.js +2 -0
  23. package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +7 -0
  24. package/dist/browser/index.js +1 -1
  25. package/dist/browser/index.js.map +3 -3
  26. package/dist/browser/kit.js +1 -1
  27. package/dist/browser/manifest.json +4 -3
  28. package/dist/engine.d.ts +1 -1
  29. package/dist/engine.d.ts.map +1 -1
  30. package/dist/engine.js +1 -1
  31. package/dist/engine.js.map +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/kit/chart-border.d.ts +1 -1
  34. package/dist/kit/chart-border.d.ts.map +1 -1
  35. package/dist/kit/chart-border.js +2 -2
  36. package/dist/kit/chart-border.js.map +1 -1
  37. package/dist/kit/chart-canvas.d.ts +1 -1
  38. package/dist/kit/chart-canvas.d.ts.map +1 -1
  39. package/dist/kit/chart-canvas.js +2 -2
  40. package/dist/kit/chart-canvas.js.map +1 -1
  41. package/dist/kit/chart-legend.d.ts +6 -27
  42. package/dist/kit/chart-legend.d.ts.map +1 -1
  43. package/dist/kit/chart-legend.js +33 -57
  44. package/dist/kit/chart-legend.js.map +1 -1
  45. package/dist/kit/shared.d.ts +31 -8
  46. package/dist/kit/shared.d.ts.map +1 -1
  47. package/dist/kit/shared.js +67 -11
  48. package/dist/kit/shared.js.map +1 -1
  49. package/dist/kit/vertical-axis.d.ts +2 -2
  50. package/dist/kit/vertical-axis.d.ts.map +1 -1
  51. package/dist/kit/vertical-axis.js +2 -2
  52. package/dist/kit/vertical-axis.js.map +1 -1
  53. package/dist/line-chart/chart-line.d.ts +3 -3
  54. package/dist/line-chart/chart-line.d.ts.map +1 -1
  55. package/dist/line-chart/chart-line.js +2 -2
  56. package/dist/line-chart/chart-line.js.map +1 -1
  57. package/dist/line-chart/chart-region.d.ts +1 -1
  58. package/dist/line-chart/chart-region.d.ts.map +1 -1
  59. package/dist/line-chart/chart-region.js +2 -2
  60. package/dist/line-chart/chart-region.js.map +1 -1
  61. package/dist/line-chart/horizontal-axis.d.ts +2 -2
  62. package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
  63. package/dist/line-chart/horizontal-axis.js +2 -2
  64. package/dist/line-chart/horizontal-axis.js.map +1 -1
  65. package/dist/line-chart/index.js.map +1 -1
  66. package/dist/line-chart/line-chart.d.ts +25 -9
  67. package/dist/line-chart/line-chart.d.ts.map +1 -1
  68. package/dist/line-chart/line-chart.js +66 -43
  69. package/dist/line-chart/line-chart.js.map +1 -1
  70. package/dist/line-chart/shared.d.ts +4 -4
  71. package/dist/line-chart/shared.d.ts.map +1 -1
  72. package/dist/line-chart/shared.js +20 -5
  73. package/dist/line-chart/shared.js.map +1 -1
  74. package/dist/line-chart/x-scale.d.ts +2 -2
  75. package/dist/line-chart/x-scale.d.ts.map +1 -1
  76. package/dist/line-chart/x-scale.js +2 -2
  77. package/dist/line-chart/x-scale.js.map +1 -1
  78. package/dist/nodes.js.map +1 -1
  79. package/dist/pie-chart/index.js.map +1 -1
  80. package/dist/pie-chart/pie-chart.d.ts +16 -10
  81. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  82. package/dist/pie-chart/pie-chart.js +15 -16
  83. package/dist/pie-chart/pie-chart.js.map +1 -1
  84. package/dist/pie-chart/pie-legend.d.ts +2 -2
  85. package/dist/pie-chart/pie-legend.d.ts.map +1 -1
  86. package/dist/pie-chart/pie-legend.js +6 -6
  87. package/dist/pie-chart/pie-legend.js.map +1 -1
  88. package/dist/pie-chart/pie-ring.d.ts +14 -5
  89. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  90. package/dist/pie-chart/pie-ring.js +45 -15
  91. package/dist/pie-chart/pie-ring.js.map +1 -1
  92. package/dist/pie-chart/shared.d.ts +4 -4
  93. package/dist/pie-chart/shared.d.ts.map +1 -1
  94. package/dist/pie-chart/shared.js +4 -4
  95. package/dist/pie-chart/shared.js.map +1 -1
  96. package/package.json +6 -4
  97. package/registry.json +23 -23
  98. package/src/bar-chart/bar-chart.ts +820 -804
  99. package/src/bar-chart/category-axis.ts +177 -177
  100. package/src/bar-chart/chart-bars.ts +310 -310
  101. package/src/bar-chart/index.ts +31 -31
  102. package/src/bar-chart/shared.ts +354 -354
  103. package/src/engine.ts +26 -26
  104. package/src/index.ts +5 -5
  105. package/src/kit/chart-border.ts +61 -61
  106. package/src/kit/chart-canvas.ts +157 -157
  107. package/src/kit/chart-legend.ts +245 -250
  108. package/src/kit/shared.ts +733 -675
  109. package/src/kit/vertical-axis.ts +224 -224
  110. package/src/line-chart/chart-line.ts +208 -208
  111. package/src/line-chart/chart-region.ts +135 -135
  112. package/src/line-chart/horizontal-axis.ts +202 -202
  113. package/src/line-chart/index.ts +39 -39
  114. package/src/line-chart/line-chart.ts +961 -894
  115. package/src/line-chart/shared.ts +473 -459
  116. package/src/line-chart/x-scale.ts +367 -367
  117. package/src/nodes.ts +20 -20
  118. package/src/pie-chart/index.ts +37 -37
  119. package/src/pie-chart/pie-chart.ts +383 -374
  120. package/src/pie-chart/pie-legend.ts +159 -159
  121. package/src/pie-chart/pie-ring.ts +935 -903
  122. package/src/pie-chart/shared.ts +490 -490
  123. package/dist/browser/chunks/chunk-ZMBOBHYL.js +0 -2
  124. package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +0 -7
@@ -1,135 +1,135 @@
1
- import {
2
- type AssetScope,
3
- Graphics2D,
4
- Node2D,
5
- fillOps,
6
- strokeOps,
7
- property,
8
- type Fill,
9
- type FillResolved,
10
- type Node2DProps,
11
- type RenderContext2D,
12
- type Stroke,
13
- type StrokeResolved,
14
- } from "@motionscript/core"
15
-
16
- import {
17
- DEFAULT_CHART_THEME,
18
- EMPTY_REGION_BOX,
19
- hasRegion,
20
- type RegionBox,
21
- } from "./shared"
22
- import { declarePaints } from "@motionscript/core/component"
23
-
24
- export interface ChartRegionProps extends Node2DProps {
25
- /**
26
- * The highlighted slice, already resolved into this node's local pixel space
27
- * (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box
28
- * draws nothing.
29
- */
30
- box: RegionBox
31
- /** Dashed rule marking each *bounded* edge of the box. */
32
- stroke: Stroke
33
- /** Wash filling the box, under the series lines. */
34
- fill: Fill
35
- /**
36
- * Reveal of the annotation in `[0, 1]`: 0 hides it, 1 draws it fully. Driven
37
- * by the chart's own `regionReveal`, so the rules and the series' dimming move
38
- * as one.
39
- */
40
- reveal: number
41
- }
42
-
43
- /**
44
- * The **region call-out** of a {@link LineChart}: a washed rectangle over the
45
- * plot with a dashed rule down each edge the author actually bounded. Sized and
46
- * positioned entirely from the pre-mapped {@link ChartRegionProps.box} rather
47
- * than its own `layoutBounds` — the box already carries the plot geometry.
48
- *
49
- * It draws only the *marking*; fading the series outside the region is
50
- * {@link ChartLine}'s job (it clips its own polyline against the same box), so
51
- * the two can't disagree about where the boundary is.
52
- *
53
- * Only bounded edges get a rule: a region given just an `x` range spans the full
54
- * plot height, so its top and bottom edges are the plot's own border, not a data
55
- * bound — drawing dashes there would assert a limit the author didn't set.
56
- */
57
- export class ChartRegion extends Node2D<ChartRegionProps> {
58
- @property({ default: () => EMPTY_REGION_BOX }) declare box: RegionBox
59
- @property({
60
- default: DEFAULT_CHART_THEME.region.stroke,
61
- mapper: strokeOps.resolve,
62
- tween: strokeOps.lerp,
63
- })
64
- declare stroke: Stroke
65
- @property({
66
- default: DEFAULT_CHART_THEME.region.fill,
67
- mapper: fillOps.resolve,
68
- tween: fillOps.lerp,
69
- })
70
- declare fill: Fill
71
- @property({ default: 0 }) declare reveal: number
72
-
73
- /**
74
- * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
75
- * paint props it carries — see {@link declarePaints}.
76
- */
77
- override declareAssets(assets: AssetScope): void {
78
- super.declareAssets(assets)
79
- declarePaints(this, assets)
80
- }
81
-
82
- protected renderSelf(ctx: RenderContext2D): void {
83
- const box = this.box
84
- if (!hasRegion(box)) return
85
-
86
- const r = Math.max(0, Math.min(1, this.reveal))
87
- if (r <= 0) return
88
-
89
- const width = box.right - box.left
90
- const height = box.top - box.bottom
91
- const cx = (box.left + box.right) / 2
92
- const cy = (box.bottom + box.top) / 2
93
-
94
- // Wash first, so the rules sit over their own tint.
95
- const layers = this.fill as FillResolved[]
96
- if (layers.length > 0) {
97
- ctx.draw(
98
- new Graphics2D()
99
- .rect({ x: cx, y: cy, width, height })
100
- .fill(this.fill)
101
- .opacity(r)
102
- )
103
- }
104
-
105
- // One rule per bounded edge, growing from the region's centre outward as
106
- // `reveal` climbs — the same "trace it in" beat as the series lines.
107
- const stroke = this.stroke as StrokeResolved[]
108
- if (stroke.length === 0) return
109
-
110
- // One `draw` per rule rather than four line ops under a single paint:
111
- // shapes chained before a `stroke()` are combined into one surface, and
112
- // stroking a union of disjoint open paths doesn't paint.
113
- const rule = (points: { x: number; y: number }[]) =>
114
- ctx.draw(new Graphics2D().line({ points }).stroke(this.stroke).opacity(r))
115
-
116
- if (box.xBounded) {
117
- const half = (height / 2) * r
118
- for (const x of [box.left, box.right]) {
119
- rule([
120
- { x, y: cy - half },
121
- { x, y: cy + half },
122
- ])
123
- }
124
- }
125
- if (box.yBounded) {
126
- const half = (width / 2) * r
127
- for (const y of [box.bottom, box.top]) {
128
- rule([
129
- { x: cx - half, y },
130
- { x: cx + half, y },
131
- ])
132
- }
133
- }
134
- }
135
- }
1
+ import {
2
+ type AssetScope,
3
+ Graphics2D,
4
+ Node2D,
5
+ fillOps,
6
+ strokeOps,
7
+ property,
8
+ type Fill,
9
+ type FillResolved,
10
+ type Node2DProps,
11
+ type RenderContext2D,
12
+ type Stroke,
13
+ type StrokeResolved,
14
+ } from "@motionscript/sdk"
15
+
16
+ import {
17
+ DEFAULT_CHART_THEME,
18
+ EMPTY_REGION_BOX,
19
+ hasRegion,
20
+ type RegionBox,
21
+ } from "./shared"
22
+ import { declarePaints } from "@motionscript/sdk/component"
23
+
24
+ export interface ChartRegionProps extends Node2DProps {
25
+ /**
26
+ * The highlighted slice, already resolved into this node's local pixel space
27
+ * (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box
28
+ * draws nothing.
29
+ */
30
+ box: RegionBox
31
+ /** Dashed rule marking each *bounded* edge of the box. */
32
+ stroke: Stroke
33
+ /** Wash filling the box, under the series lines. */
34
+ fill: Fill
35
+ /**
36
+ * Reveal of the annotation in `[0, 1]`: 0 hides it, 1 draws it fully. Driven
37
+ * by the chart's own `regionReveal`, so the rules and the series' dimming move
38
+ * as one.
39
+ */
40
+ reveal: number
41
+ }
42
+
43
+ /**
44
+ * The **region call-out** of a {@link LineChart}: a washed rectangle over the
45
+ * plot with a dashed rule down each edge the author actually bounded. Sized and
46
+ * positioned entirely from the pre-mapped {@link ChartRegionProps.box} rather
47
+ * than its own `layoutBounds` — the box already carries the plot geometry.
48
+ *
49
+ * It draws only the *marking*; fading the series outside the region is
50
+ * {@link ChartLine}'s job (it clips its own polyline against the same box), so
51
+ * the two can't disagree about where the boundary is.
52
+ *
53
+ * Only bounded edges get a rule: a region given just an `x` range spans the full
54
+ * plot height, so its top and bottom edges are the plot's own border, not a data
55
+ * bound — drawing dashes there would assert a limit the author didn't set.
56
+ */
57
+ export class ChartRegion extends Node2D<ChartRegionProps> {
58
+ @property({ default: () => EMPTY_REGION_BOX }) declare box: RegionBox
59
+ @property({
60
+ default: DEFAULT_CHART_THEME.region.stroke,
61
+ mapper: strokeOps.resolve,
62
+ tween: strokeOps.lerp,
63
+ })
64
+ declare stroke: Stroke
65
+ @property({
66
+ default: DEFAULT_CHART_THEME.region.fill,
67
+ mapper: fillOps.resolve,
68
+ tween: fillOps.lerp,
69
+ })
70
+ declare fill: Fill
71
+ @property({ default: 0 }) declare reveal: number
72
+
73
+ /**
74
+ * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
75
+ * paint props it carries — see {@link declarePaints}.
76
+ */
77
+ override declareAssets(assets: AssetScope): void {
78
+ super.declareAssets(assets)
79
+ declarePaints(this, assets)
80
+ }
81
+
82
+ protected renderSelf(ctx: RenderContext2D): void {
83
+ const box = this.box
84
+ if (!hasRegion(box)) return
85
+
86
+ const r = Math.max(0, Math.min(1, this.reveal))
87
+ if (r <= 0) return
88
+
89
+ const width = box.right - box.left
90
+ const height = box.top - box.bottom
91
+ const cx = (box.left + box.right) / 2
92
+ const cy = (box.bottom + box.top) / 2
93
+
94
+ // Wash first, so the rules sit over their own tint.
95
+ const layers = this.fill as FillResolved[]
96
+ if (layers.length > 0) {
97
+ ctx.draw(
98
+ new Graphics2D()
99
+ .rect({ x: cx, y: cy, width, height })
100
+ .fill(this.fill)
101
+ .opacity(r)
102
+ )
103
+ }
104
+
105
+ // One rule per bounded edge, growing from the region's centre outward as
106
+ // `reveal` climbs — the same "trace it in" beat as the series lines.
107
+ const stroke = this.stroke as StrokeResolved[]
108
+ if (stroke.length === 0) return
109
+
110
+ // One `draw` per rule rather than four line ops under a single paint:
111
+ // shapes chained before a `stroke()` are combined into one surface, and
112
+ // stroking a union of disjoint open paths doesn't paint.
113
+ const rule = (points: { x: number; y: number }[]) =>
114
+ ctx.draw(new Graphics2D().line({ points }).stroke(this.stroke).opacity(r))
115
+
116
+ if (box.xBounded) {
117
+ const half = (height / 2) * r
118
+ for (const x of [box.left, box.right]) {
119
+ rule([
120
+ { x, y: cy - half },
121
+ { x, y: cy + half },
122
+ ])
123
+ }
124
+ }
125
+ if (box.yBounded) {
126
+ const half = (width / 2) * r
127
+ for (const y of [box.bottom, box.top]) {
128
+ rule([
129
+ { x: cx - half, y },
130
+ { x: cx + half, y },
131
+ ])
132
+ }
133
+ }
134
+ }
135
+ }
@@ -1,202 +1,202 @@
1
- import {
2
- command,
3
- Node2D,
4
- Rect,
5
- Text,
6
- easeOut,
7
- property,
8
- insetsOps,
9
- textOps,
10
- type CommandArgs,
11
- type NodeConfig,
12
- type Node2DProps,
13
- } from "@motionscript/core";
14
-
15
- import {
16
- DEFAULT_X_AXIS,
17
- enterBudget,
18
- formatMarker,
19
- type AxisConfig,
20
- type Range,
21
- } from "../kit/shared";
22
- import { at, compose, type Seekable } from "@motionscript/core/component"
23
-
24
- export interface HorizontalAxisProps extends Node2DProps {
25
- /** `[min, max]` of the horizontal scale the markers are positioned within. */
26
- range: Range;
27
- /** Tick values (in data units) to draw along the axis. */
28
- markers: number[];
29
- /** Marker/caption text style, grid stroke, and gutters — see {@link AxisConfig}. */
30
- axis: AxisConfig;
31
- /**
32
- * Caption drawn centred in the gutter beneath the marker row. Empty string
33
- * (the default) hides it.
34
- */
35
- label: string;
36
- }
37
-
38
- /** How far a marker starts above its slot when it enters, in px. */
39
- const SLIDE = 24;
40
-
41
- /** How a tick value is captioned — see {@link HorizontalAxis.labelFor}. */
42
- export type MarkerLabel = (value: number) => string;
43
-
44
- /**
45
- * The horizontal axis of a {@link LineChart}: a **row of {@link Text} nodes**,
46
- * one per marker value, running left-to-right. Pure layout — the mirror of the
47
- * kit's `VerticalAxis`, including its bound (not snapshotted) marker text and
48
- * positions, and its landed resting state.
49
- *
50
- * Markers are always placed **by value** across the range, whatever they say:
51
- * `2026-03` sits three months along, not three slots along. That is what keeps
52
- * it distinct from a bar chart's bottom axis, which centres a caption on each
53
- * slot and has no scale at all — the two only look alike. What the values are
54
- * captioned *as* is the chart's to decide; see {@link labelFor}.
55
- *
56
- * {@link enter} staggers the markers in **left-to-right**: each starts lifted a
57
- * little above its slot and transparent, then fades and slides down into place.
58
- */
59
- export class HorizontalAxis extends Node2D<HorizontalAxisProps> {
60
- @property({ default: () => [0, 1] as Range }) declare range: Range;
61
- @property({ default: () => [] as number[] }) declare markers: number[];
62
- @property({ default: DEFAULT_X_AXIS }) declare axis: AxisConfig;
63
- @property({ default: "" }) declare label: string;
64
-
65
- /**
66
- * How a tick value is captioned. Defaults to the numeric formatting the
67
- * vertical axis uses; a chart whose x column names months or categories
68
- * replaces it (see `x-scale.ts`).
69
- *
70
- * A plain field rather than a `@property`, because it is a **policy, not a
71
- * value**: there is nothing to tween between two formatters, and a signal
72
- * holding a function is a signal every `set`/`to` on this node would have to
73
- * know not to touch. Read inside each marker's `text` binding, so replacing it
74
- * before the first frame is enough — which is when the chart does it.
75
- */
76
- labelFor: MarkerLabel = formatMarker;
77
-
78
- /** One text node per marker, ordered left → right, for the staggered enter. */
79
- private markerNodes: Text[] = [];
80
- /** The caption node, or null when `label` is empty. */
81
- private labelNode: Text | null = null;
82
-
83
- /** The live tick values, left → right — re-read every frame, not snapshotted. */
84
- private get sortedMarkers(): number[] {
85
- return [...this.markers].sort((a, b) => a - b);
86
- }
87
-
88
- private valueAt(index: number): number | undefined {
89
- return this.sortedMarkers[index];
90
- }
91
-
92
- /** Tick `i`'s fraction across the live `range`, 0 at the left edge. */
93
- private fracAt(index: number): number {
94
- const v = this.valueAt(index);
95
- if (v === undefined) return 0;
96
- const [min, max] = this.range;
97
- return max <= min ? 0 : (v - min) / (max - min);
98
- }
99
-
100
- constructor(props?: NodeConfig<HorizontalAxis, HorizontalAxisProps>) {
101
- super(props);
102
-
103
- const tickPad = insetsOps.resolve(this.axis.tickPadding).top;
104
- const titlePad = insetsOps.resolve(this.axis.titlePadding).top;
105
-
106
- this.markerNodes = this.markers.map((_, i) => {
107
- const marker: Text = new Text({
108
- ...textOps.styleProps(this.axis.labelStyle),
109
- text: () => {
110
- const v = this.valueAt(i);
111
- return v === undefined ? "" : this.labelFor(v);
112
- },
113
- x: () => {
114
- const width = this.layoutBounds.width;
115
- const centred = (this.fracAt(i) - 0.5) * width;
116
- // Held inside the axis's right edge, which is also the *chart's*: the
117
- // last marker is centred on the last grid line, so half of `2026-06`
118
- // — or of `3300` — would be drawn outside the node's box and outside
119
- // the rectangle the editor draws around it. Only the right edge is
120
- // held, because the left one has the vertical axis's whole gutter
121
- // beyond it, and a marker overhanging into that is inside the chart.
122
- const limit = width / 2 - marker.layoutBounds.width / 2;
123
- return Math.min(centred, limit);
124
- },
125
- });
126
- return marker;
127
- });
128
-
129
- const children: Node2D[] = [
130
- new Rect({
131
- width: "fill",
132
- height: "hug",
133
- padding: { top: tickPad },
134
- children: this.markerNodes,
135
- }),
136
- ];
137
- if (this.label !== "") {
138
- this.labelNode = new Text({
139
- ...textOps.styleProps(this.axis.titleStyle),
140
- text: this.label,
141
- });
142
- children.push(this.labelNode);
143
- }
144
-
145
- this.add(
146
- new Rect({ flow: "vertical", gap: titlePad, height: "hug", width: "fill", children }),
147
- );
148
- }
149
-
150
- /**
151
- * Primes every marker (and the caption) at its pre-entrance state: lifted
152
- * above its slot and transparent. Idempotent — see {@link VerticalAxis.arm}.
153
- */
154
- arm(slide = SLIDE): void {
155
- for (const marker of this.markerNodes) marker.set({ opacity: 0, y: slide });
156
- this.labelNode?.set({ opacity: 0, y: slide });
157
- }
158
-
159
- /**
160
- * Stagger the markers in left to right, then slide the caption in after them.
161
- *
162
- * A {@link Seekable} rather than a generator. The offsets were always static —
163
- * `i * step` per marker — but expressed as `wait` inside a generator they only
164
- * existed while it ran, so knowing what the axis looked like partway through
165
- * meant running it from zero. Placed explicitly, the same stagger can be asked
166
- * for a time, which is what lets a chart scene scrub in constant time.
167
- *
168
- * `duration` is the **whole** entrance — see {@link VerticalAxis.enter} and
169
- * {@link enterBudget}.
170
- */
171
- @command()
172
- enter(
173
- args: CommandArgs<{ stagger?: number; slide?: number }> & { duration: number },
174
- ): Seekable {
175
- const { stagger, slide = SLIDE } = args.data ?? {};
176
- const duration = args.duration;
177
- const easing = args.easing ?? easeOut();
178
- const nodes = this.markerNodes;
179
- if (nodes.length === 0) return compose([]);
180
- const { step, per, caption } = enterBudget(
181
- duration,
182
- nodes.length,
183
- this.labelNode !== null,
184
- stagger,
185
- );
186
-
187
- this.arm(slide);
188
-
189
- // Left marker leads: markerNodes is already left → right.
190
- const markers = nodes.map((node, i) =>
191
- at(i * step, node.to({ data: { opacity: 1, y: 0 }, duration: per, easing })),
192
- );
193
- const lastMarkerEnds = nodes.length === 0 ? 0 : (nodes.length - 1) * step + per;
194
-
195
- return compose([
196
- ...markers,
197
- ...(this.labelNode
198
- ? [at(lastMarkerEnds, this.labelNode.to({ data: { opacity: 1, y: 0 }, duration: caption, easing }))]
199
- : []),
200
- ]);
201
- }
202
- }
1
+ import {
2
+ command,
3
+ Node2D,
4
+ Rect,
5
+ Text,
6
+ easeOut,
7
+ property,
8
+ insetsOps,
9
+ textOps,
10
+ type CommandArgs,
11
+ type NodeConfig,
12
+ type Node2DProps,
13
+ } from "@motionscript/sdk";
14
+
15
+ import {
16
+ DEFAULT_X_AXIS,
17
+ enterBudget,
18
+ formatMarker,
19
+ type AxisConfig,
20
+ type Range,
21
+ } from "../kit/shared";
22
+ import { at, compose, type Seekable } from "@motionscript/sdk/component"
23
+
24
+ export interface HorizontalAxisProps extends Node2DProps {
25
+ /** `[min, max]` of the horizontal scale the markers are positioned within. */
26
+ range: Range;
27
+ /** Tick values (in data units) to draw along the axis. */
28
+ markers: number[];
29
+ /** Marker/caption text style, grid stroke, and gutters — see {@link AxisConfig}. */
30
+ axis: AxisConfig;
31
+ /**
32
+ * Caption drawn centred in the gutter beneath the marker row. Empty string
33
+ * (the default) hides it.
34
+ */
35
+ label: string;
36
+ }
37
+
38
+ /** How far a marker starts above its slot when it enters, in px. */
39
+ const SLIDE = 24;
40
+
41
+ /** How a tick value is captioned — see {@link HorizontalAxis.labelFor}. */
42
+ export type MarkerLabel = (value: number) => string;
43
+
44
+ /**
45
+ * The horizontal axis of a {@link LineChart}: a **row of {@link Text} nodes**,
46
+ * one per marker value, running left-to-right. Pure layout — the mirror of the
47
+ * kit's `VerticalAxis`, including its bound (not snapshotted) marker text and
48
+ * positions, and its landed resting state.
49
+ *
50
+ * Markers are always placed **by value** across the range, whatever they say:
51
+ * `2026-03` sits three months along, not three slots along. That is what keeps
52
+ * it distinct from a bar chart's bottom axis, which centres a caption on each
53
+ * slot and has no scale at all — the two only look alike. What the values are
54
+ * captioned *as* is the chart's to decide; see {@link labelFor}.
55
+ *
56
+ * {@link enter} staggers the markers in **left-to-right**: each starts lifted a
57
+ * little above its slot and transparent, then fades and slides down into place.
58
+ */
59
+ export class HorizontalAxis extends Node2D<HorizontalAxisProps> {
60
+ @property({ default: () => [0, 1] as Range }) declare range: Range;
61
+ @property({ default: () => [] as number[] }) declare markers: number[];
62
+ @property({ default: DEFAULT_X_AXIS }) declare axis: AxisConfig;
63
+ @property({ default: "" }) declare label: string;
64
+
65
+ /**
66
+ * How a tick value is captioned. Defaults to the numeric formatting the
67
+ * vertical axis uses; a chart whose x column names months or categories
68
+ * replaces it (see `x-scale.ts`).
69
+ *
70
+ * A plain field rather than a `@property`, because it is a **policy, not a
71
+ * value**: there is nothing to tween between two formatters, and a signal
72
+ * holding a function is a signal every `set`/`to` on this node would have to
73
+ * know not to touch. Read inside each marker's `text` binding, so replacing it
74
+ * before the first frame is enough — which is when the chart does it.
75
+ */
76
+ labelFor: MarkerLabel = formatMarker;
77
+
78
+ /** One text node per marker, ordered left → right, for the staggered enter. */
79
+ private markerNodes: Text[] = [];
80
+ /** The caption node, or null when `label` is empty. */
81
+ private labelNode: Text | null = null;
82
+
83
+ /** The live tick values, left → right — re-read every frame, not snapshotted. */
84
+ private get sortedMarkers(): number[] {
85
+ return [...this.markers].sort((a, b) => a - b);
86
+ }
87
+
88
+ private valueAt(index: number): number | undefined {
89
+ return this.sortedMarkers[index];
90
+ }
91
+
92
+ /** Tick `i`'s fraction across the live `range`, 0 at the left edge. */
93
+ private fracAt(index: number): number {
94
+ const v = this.valueAt(index);
95
+ if (v === undefined) return 0;
96
+ const [min, max] = this.range;
97
+ return max <= min ? 0 : (v - min) / (max - min);
98
+ }
99
+
100
+ constructor(props?: NodeConfig<HorizontalAxis, HorizontalAxisProps>) {
101
+ super(props);
102
+
103
+ const tickPad = insetsOps.resolve(this.axis.tickPadding).top;
104
+ const titlePad = insetsOps.resolve(this.axis.titlePadding).top;
105
+
106
+ this.markerNodes = this.markers.map((_, i) => {
107
+ const marker: Text = new Text({
108
+ ...textOps.styleProps(this.axis.labelStyle),
109
+ text: () => {
110
+ const v = this.valueAt(i);
111
+ return v === undefined ? "" : this.labelFor(v);
112
+ },
113
+ x: () => {
114
+ const width = this.layoutBounds.width;
115
+ const centred = (this.fracAt(i) - 0.5) * width;
116
+ // Held inside the axis's right edge, which is also the *chart's*: the
117
+ // last marker is centred on the last grid line, so half of `2026-06`
118
+ // — or of `3300` — would be drawn outside the node's box and outside
119
+ // the rectangle the editor draws around it. Only the right edge is
120
+ // held, because the left one has the vertical axis's whole gutter
121
+ // beyond it, and a marker overhanging into that is inside the chart.
122
+ const limit = width / 2 - marker.layoutBounds.width / 2;
123
+ return Math.min(centred, limit);
124
+ },
125
+ });
126
+ return marker;
127
+ });
128
+
129
+ const children: Node2D[] = [
130
+ new Rect({
131
+ width: "fill",
132
+ height: "hug",
133
+ padding: { top: tickPad },
134
+ children: this.markerNodes,
135
+ }),
136
+ ];
137
+ if (this.label !== "") {
138
+ this.labelNode = new Text({
139
+ ...textOps.styleProps(this.axis.titleStyle),
140
+ text: this.label,
141
+ });
142
+ children.push(this.labelNode);
143
+ }
144
+
145
+ this.add(
146
+ new Rect({ flow: "vertical", gap: titlePad, height: "hug", width: "fill", children }),
147
+ );
148
+ }
149
+
150
+ /**
151
+ * Primes every marker (and the caption) at its pre-entrance state: lifted
152
+ * above its slot and transparent. Idempotent — see {@link VerticalAxis.arm}.
153
+ */
154
+ arm(slide = SLIDE): void {
155
+ for (const marker of this.markerNodes) marker.set({ opacity: 0, y: slide });
156
+ this.labelNode?.set({ opacity: 0, y: slide });
157
+ }
158
+
159
+ /**
160
+ * Stagger the markers in left to right, then slide the caption in after them.
161
+ *
162
+ * A {@link Seekable} rather than a generator. The offsets were always static —
163
+ * `i * step` per marker — but expressed as `wait` inside a generator they only
164
+ * existed while it ran, so knowing what the axis looked like partway through
165
+ * meant running it from zero. Placed explicitly, the same stagger can be asked
166
+ * for a time, which is what lets a chart scene scrub in constant time.
167
+ *
168
+ * `duration` is the **whole** entrance — see {@link VerticalAxis.enter} and
169
+ * {@link enterBudget}.
170
+ */
171
+ @command()
172
+ enter(
173
+ args: CommandArgs<{ stagger?: number; slide?: number }> & { duration: number },
174
+ ): Seekable {
175
+ const { stagger, slide = SLIDE } = args.data ?? {};
176
+ const duration = args.duration;
177
+ const easing = args.easing ?? easeOut();
178
+ const nodes = this.markerNodes;
179
+ if (nodes.length === 0) return compose([]);
180
+ const { step, per, caption } = enterBudget(
181
+ duration,
182
+ nodes.length,
183
+ this.labelNode !== null,
184
+ stagger,
185
+ );
186
+
187
+ this.arm(slide);
188
+
189
+ // Left marker leads: markerNodes is already left → right.
190
+ const markers = nodes.map((node, i) =>
191
+ at(i * step, node.to({ data: { opacity: 1, y: 0 }, duration: per, easing })),
192
+ );
193
+ const lastMarkerEnds = nodes.length === 0 ? 0 : (nodes.length - 1) * step + per;
194
+
195
+ return compose([
196
+ ...markers,
197
+ ...(this.labelNode
198
+ ? [at(lastMarkerEnds, this.labelNode.to({ data: { opacity: 1, y: 0 }, duration: caption, easing }))]
199
+ : []),
200
+ ]);
201
+ }
202
+ }