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

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 (146) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +30 -16
  3. package/dist/bar-chart/bar-chart.d.ts +133 -242
  4. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  5. package/dist/bar-chart/bar-chart.js +378 -569
  6. package/dist/bar-chart/bar-chart.js.map +1 -1
  7. package/dist/bar-chart/category-axis.d.ts +35 -41
  8. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  9. package/dist/bar-chart/category-axis.js +93 -115
  10. package/dist/bar-chart/category-axis.js.map +1 -1
  11. package/dist/bar-chart/chart-bars.d.ts +50 -70
  12. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  13. package/dist/bar-chart/chart-bars.js +156 -210
  14. package/dist/bar-chart/chart-bars.js.map +1 -1
  15. package/dist/bar-chart/index.d.ts +2 -2
  16. package/dist/bar-chart/index.d.ts.map +1 -1
  17. package/dist/bar-chart/index.js +1 -1
  18. package/dist/bar-chart/index.js.map +1 -1
  19. package/dist/bar-chart/shared.d.ts +3 -3
  20. package/dist/bar-chart/shared.d.ts.map +1 -1
  21. package/dist/bar-chart/shared.js +3 -3
  22. package/dist/bar-chart/shared.js.map +1 -1
  23. package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
  24. package/dist/browser/chunks/chunk-XROBIRQK.js.map +7 -0
  25. package/dist/browser/index.js +1 -1
  26. package/dist/browser/index.js.map +3 -3
  27. package/dist/browser/kit.js +1 -1
  28. package/dist/browser/manifest.json +1 -1
  29. package/dist/engine.d.ts +8 -6
  30. package/dist/engine.d.ts.map +1 -1
  31. package/dist/engine.js +8 -6
  32. package/dist/engine.js.map +1 -1
  33. package/dist/kit/cartesian.d.ts +44 -0
  34. package/dist/kit/cartesian.d.ts.map +1 -0
  35. package/dist/kit/cartesian.js +59 -0
  36. package/dist/kit/cartesian.js.map +1 -0
  37. package/dist/kit/chart-border.d.ts +10 -11
  38. package/dist/kit/chart-border.d.ts.map +1 -1
  39. package/dist/kit/chart-border.js +15 -32
  40. package/dist/kit/chart-border.js.map +1 -1
  41. package/dist/kit/chart-canvas.d.ts +32 -21
  42. package/dist/kit/chart-canvas.d.ts.map +1 -1
  43. package/dist/kit/chart-canvas.js +33 -90
  44. package/dist/kit/chart-canvas.js.map +1 -1
  45. package/dist/kit/chart-legend.d.ts +123 -64
  46. package/dist/kit/chart-legend.d.ts.map +1 -1
  47. package/dist/kit/chart-legend.js +129 -154
  48. package/dist/kit/chart-legend.js.map +1 -1
  49. package/dist/kit/entries.d.ts +17 -0
  50. package/dist/kit/entries.d.ts.map +1 -0
  51. package/dist/kit/entries.js +19 -0
  52. package/dist/kit/entries.js.map +1 -0
  53. package/dist/kit/motion.d.ts +48 -0
  54. package/dist/kit/motion.d.ts.map +1 -0
  55. package/dist/kit/motion.js +63 -0
  56. package/dist/kit/motion.js.map +1 -0
  57. package/dist/kit/paints.d.ts +10 -0
  58. package/dist/kit/paints.d.ts.map +1 -0
  59. package/dist/kit/paints.js +12 -0
  60. package/dist/kit/paints.js.map +1 -0
  61. package/dist/kit/shared.d.ts +31 -3
  62. package/dist/kit/shared.d.ts.map +1 -1
  63. package/dist/kit/shared.js +42 -3
  64. package/dist/kit/shared.js.map +1 -1
  65. package/dist/kit/vertical-axis.d.ts +48 -61
  66. package/dist/kit/vertical-axis.d.ts.map +1 -1
  67. package/dist/kit/vertical-axis.js +97 -132
  68. package/dist/kit/vertical-axis.js.map +1 -1
  69. package/dist/line-chart/chart-line.d.ts +60 -77
  70. package/dist/line-chart/chart-line.d.ts.map +1 -1
  71. package/dist/line-chart/chart-line.js +100 -121
  72. package/dist/line-chart/chart-line.js.map +1 -1
  73. package/dist/line-chart/chart-region.d.ts +32 -33
  74. package/dist/line-chart/chart-region.d.ts.map +1 -1
  75. package/dist/line-chart/chart-region.js +31 -65
  76. package/dist/line-chart/chart-region.js.map +1 -1
  77. package/dist/line-chart/horizontal-axis.d.ts +58 -64
  78. package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
  79. package/dist/line-chart/horizontal-axis.js +94 -123
  80. package/dist/line-chart/horizontal-axis.js.map +1 -1
  81. package/dist/line-chart/index.d.ts +2 -2
  82. package/dist/line-chart/index.d.ts.map +1 -1
  83. package/dist/line-chart/index.js +1 -1
  84. package/dist/line-chart/index.js.map +1 -1
  85. package/dist/line-chart/line-chart.d.ts +156 -319
  86. package/dist/line-chart/line-chart.d.ts.map +1 -1
  87. package/dist/line-chart/line-chart.js +403 -663
  88. package/dist/line-chart/line-chart.js.map +1 -1
  89. package/dist/line-chart/shared.d.ts +3 -3
  90. package/dist/line-chart/shared.d.ts.map +1 -1
  91. package/dist/line-chart/shared.js +3 -3
  92. package/dist/line-chart/shared.js.map +1 -1
  93. package/dist/nodes.d.ts +6 -9
  94. package/dist/nodes.d.ts.map +1 -1
  95. package/dist/nodes.js +7 -9
  96. package/dist/nodes.js.map +1 -1
  97. package/dist/pie-chart/index.d.ts +2 -2
  98. package/dist/pie-chart/index.d.ts.map +1 -1
  99. package/dist/pie-chart/index.js +1 -1
  100. package/dist/pie-chart/index.js.map +1 -1
  101. package/dist/pie-chart/pie-chart.d.ts +108 -132
  102. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  103. package/dist/pie-chart/pie-chart.js +200 -271
  104. package/dist/pie-chart/pie-chart.js.map +1 -1
  105. package/dist/pie-chart/pie-legend.d.ts +84 -31
  106. package/dist/pie-chart/pie-legend.d.ts.map +1 -1
  107. package/dist/pie-chart/pie-legend.js +93 -107
  108. package/dist/pie-chart/pie-legend.js.map +1 -1
  109. package/dist/pie-chart/pie-ring.d.ts +160 -131
  110. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  111. package/dist/pie-chart/pie-ring.js +333 -391
  112. package/dist/pie-chart/pie-ring.js.map +1 -1
  113. package/dist/pie-chart/shared.d.ts +2 -2
  114. package/dist/pie-chart/shared.d.ts.map +1 -1
  115. package/dist/pie-chart/shared.js +2 -2
  116. package/dist/pie-chart/shared.js.map +1 -1
  117. package/package.json +4 -4
  118. package/src/bar-chart/bar-chart.ts +415 -701
  119. package/src/bar-chart/category-axis.ts +95 -110
  120. package/src/bar-chart/chart-bars.ts +178 -200
  121. package/src/bar-chart/index.ts +2 -2
  122. package/src/bar-chart/shared.ts +3 -3
  123. package/src/engine.ts +8 -6
  124. package/src/kit/cartesian.ts +81 -0
  125. package/src/kit/chart-border.ts +22 -33
  126. package/src/kit/chart-canvas.ts +44 -79
  127. package/src/kit/chart-legend.ts +173 -189
  128. package/src/kit/entries.ts +20 -0
  129. package/src/kit/motion.ts +90 -0
  130. package/src/kit/paints.ts +15 -0
  131. package/src/kit/shared.ts +52 -3
  132. package/src/kit/vertical-axis.ts +110 -140
  133. package/src/line-chart/chart-line.ts +102 -154
  134. package/src/line-chart/chart-region.ts +35 -80
  135. package/src/line-chart/horizontal-axis.ts +101 -152
  136. package/src/line-chart/index.ts +2 -2
  137. package/src/line-chart/line-chart.ts +449 -825
  138. package/src/line-chart/shared.ts +3 -3
  139. package/src/nodes.ts +8 -10
  140. package/src/pie-chart/index.ts +2 -2
  141. package/src/pie-chart/pie-chart.ts +221 -311
  142. package/src/pie-chart/pie-legend.ts +124 -114
  143. package/src/pie-chart/pie-ring.ts +386 -473
  144. package/src/pie-chart/shared.ts +2 -2
  145. package/dist/browser/chunks/chunk-5ZPBV6BU.js +0 -2
  146. package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +0 -7
@@ -1,22 +1,36 @@
1
1
  import {
2
+ Flow,
3
+ Paint,
2
4
  Rect,
5
+ Shapes,
3
6
  Text,
4
- command,
5
- easeOut,
6
- property,
7
+ Types,
8
+ component2d,
7
9
  textOps,
8
- type CommandArgs,
9
- type EasingFunction,
10
10
  type Fill,
11
- type MeasureContext2D,
12
- type NodeConfig,
11
+ type NodeOf,
13
12
  type RectProps,
14
- type Size2D,
15
- type SizeConstraints,
13
+ // Named so the row's emitted type reaches the Rect surface it spreads through the sdk, not core.
14
+ type Alignment as _Alignment,
15
+ type CornerRadiusResolved as _CornerRadiusResolved,
16
+ type CornerStyleResolved as _CornerStyleResolved,
17
+ type FillResolved as _FillResolved,
18
+ type FlowMode as _FlowMode,
19
+ type FlowResolved as _FlowResolved,
20
+ type GapSize as _GapSize,
21
+ type Node2D as _Node2D,
22
+ type Node2DProps as _Node2DProps,
23
+ type RectCornerRadius as _RectCornerRadius,
24
+ type RectCornerStyle as _RectCornerStyle,
25
+ type Shadow as _Shadow,
26
+ type ShadowResolved as _ShadowResolved,
27
+ type Stroke as _Stroke,
28
+ type StrokeResolved as _StrokeResolved,
29
+ type Vector2 as _Vector2,
16
30
  } from "@motionscript/sdk"
17
31
 
18
- import { drive, hold, together, type Seekable } from "@motionscript/sdk/component"
19
-
32
+ import { fresh } from "./entries"
33
+ import { frameEase } from "./motion"
20
34
  import {
21
35
  DEFAULT_LEGEND,
22
36
  SPOTLIGHT_DIM,
@@ -29,7 +43,7 @@ export interface ChartLegendProps extends RectProps {
29
43
  * One entry per series: legend `label`, its `enabled` default, and — through
30
44
  * {@link ChartLegendProps.swatches} — the colour its chip is painted. A series
31
45
  * that starts enabled (the default) is added to the row up front; one that
32
- * starts `false` is hidden until a later {@link ChartLegend.showSeries}.
46
+ * starts `false` is hidden until a later `showSeries`.
33
47
  */
34
48
  series: ChartSeries[]
35
49
  /**
@@ -43,25 +57,40 @@ export interface ChartLegendProps extends RectProps {
43
57
  legend: LegendConfig
44
58
  }
45
59
 
60
+ /** A row's entries are a `Rect`'s: paint, the flow of its children, its corners. */
61
+ const row = { ...Paint.state, ...Flow.state, ...Shapes.corners }
62
+
46
63
  /**
47
- * One legend entry. It hugs its caption and swatch; `shown` scales that width from
48
- * nothing to the whole of it.
64
+ * One legend entry. It hugs its caption and swatch; `shown` scales that width
65
+ * from nothing to the whole of it.
49
66
  *
50
67
  * The width is taken from the content on every measure, at any reveal, so a frame
51
68
  * never depends on whether the entry was once seen fully shown. A collapse needs a
52
69
  * number to scale, and `"hug"` is not one until something measures it — this is
53
70
  * the something.
54
71
  */
55
- class LegendEntry extends Rect {
56
- constructor(props: NodeConfig<Rect, RectProps>, private readonly _shown: () => number) {
57
- super(props)
58
- }
72
+ const LegendEntry = component2d({
73
+ key: "legendEntry",
74
+ state: { ...row, shown: Types.number(1) },
75
+ commands: { ...Paint.commands },
76
+ measure: (measurer, state, children) => {
77
+ const size = Flow.measure(measurer, state, children, Paint.inset(state))
78
+ return state.shown >= 1 ? size : { ...size, width: state.shown * size.width }
79
+ },
80
+ layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),
81
+ render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),
82
+ })
59
83
 
60
- override measure(constraints: SizeConstraints, scope: MeasureContext2D): Partial<Size2D> {
61
- const size = super.measure(constraints, scope)
62
- const shown = this._shown()
63
- return shown >= 1 ? size : { ...size, width: shown * (size.width ?? 0) }
64
- }
84
+ /**
85
+ * How far each entry is shown, written whole (a document, a `set`) or one
86
+ * entry at a time (a command, as `{ [index]: reveal }`), which leaves the others
87
+ * as the frame left them — so two series moving at once each move their own.
88
+ */
89
+ function revealOf(value: readonly number[] | Readonly<Record<number, number>>, previous?: number[]): number[] {
90
+ if (Array.isArray(value)) return value as number[]
91
+ const next = [...(previous ?? [])]
92
+ for (const [index, reveal] of Object.entries(value)) next[Number(index)] = reveal
93
+ return next
65
94
  }
66
95
 
67
96
  /**
@@ -70,176 +99,131 @@ class LegendEntry extends Rect {
70
99
  * with a rounded colour swatch; only the swatch carries the series colour, the
71
100
  * caption reads in the legend's neutral ink.
72
101
  *
73
- * {@link showSeries}/{@link hideSeries} flip a series on or off — showing one
74
- * adds its entry back into the row at its stable series position and grows it
75
- * in, hiding one shrinks it away and removes it. So the row only ever holds the
76
- * currently-enabled series, in series order, and closes the gap when one leaves.
102
+ * `showSeries`/`hideSeries` flip a series on or off — showing one grows its
103
+ * entry back in at its stable series position, hiding one shrinks it away. So
104
+ * the row only ever shows the currently-enabled series, in series order, and
105
+ * closes the gap when one leaves.
77
106
  *
78
107
  * This node *is* the row, so the parent places it (centred below the chart) and
79
108
  * it hugs its entries.
80
109
  */
81
- export class ChartLegend extends Rect {
82
- @property({ default: () => [] as ChartSeries[] }) declare series: ChartSeries[]
83
- @property({ default: () => [] as Fill[] }) declare swatches: Fill[]
84
- @property({ default: DEFAULT_LEGEND }) declare legend: LegendConfig
85
-
86
- /** One entry per series. **Every** entry is a child — see {@link reveal}. */
87
- private items: LegendEntry[] = []
88
- /**
89
- * How far each entry is shown, `0`–`1`. Drives its width, opacity and gap
90
- * together, so `0` is an entry that takes no room and `1` is one at its
91
- * natural size.
92
- *
93
- * This replaces adding and removing the entry from the row, which is what
94
- * `showSeries`/`hideSeries` used to do. That could not be a command: a command
95
- * is asked what it looks like at a time, and "is this node a child" is not a
96
- * value a time can produce — the tree either contains it or it doesn't, and
97
- * getting there means having run the add. Collapsed-but-present is the same
98
- * picture reached by a route that *is* a function of `t`.
99
- *
100
- * One array in one slot, because the count is the series count, which is data.
101
- * It is replaced rather than mutated: a write is a slot write, so the timeline
102
- * sees it and restores it to the authored reveal before the command starts
103
- * and after a seek back. The entries' props are bound to it, so writing it
104
- * still moves the layout.
105
- */
106
- @property({ default: () => [] as number[] }) private declare reveal: number[]
107
-
108
- constructor(props?: NodeConfig<ChartLegend, ChartLegendProps>) {
109
- // `flow` after the spread, with the rest of the geometry: this class *is*
110
- // the row, so the direction is its own answer and not the caller's. It has
111
- // to be stated — a `Rect` given no flow lays its children out `freeform`,
112
- // which stacks every entry on the same spot. One series looks perfect that
113
- // way and three look like one smeared caption, so the failure only shows up
114
- // on the charts that most need a legend.
115
- super({
116
- ...(props as RectProps),
117
- flow: "horizontal",
118
- align: "center",
119
- width: "hug",
120
- height: "hug",
121
- })
122
-
123
- // Structure is one entry per series, so it's composed here from the resolved
124
- // props super() applied. Fixed row geometry last, so it wins over super().
125
- this.set({ gap: this.legend.itemGap, padding: this.legend.padding })
126
-
127
- this.reveal = this.series.map((s) => ((s.enabled ?? true) ? 1 : 0))
128
- this.items = this.series.map((s, i) => this.buildItem(s, i))
129
-
130
- // All of them, in series order. Order is now fixed rather than depending on
131
- // which siblings happen to be shown, so an entry always comes back where it
132
- // belongs and `insertionIndex` has nothing left to compute.
133
- for (const item of this.items) this.add(item)
134
- }
135
-
136
- /** One entry: the series caption beside its colour swatch. */
137
- private buildItem(series: ChartSeries, index: number): LegendEntry {
138
- const marker = this.legend.markerStyle
139
- const item = new LegendEntry({ flow: "horizontal",
140
- gap: 12,
141
- align: "center",
142
- // Bound to the reveal, so showing and hiding is a number moving rather
143
- // than a child arriving and leaving. The width follows it in `measure`.
144
- width: "hug",
145
- opacity: () => this.reveal[index] ?? 0,
146
- height: "hug",
147
- children: [
148
- new Text({
149
- ...textOps.styleProps(this.legend.textStyle),
150
- text: series.label,
151
- }),
152
- new Rect({
153
- width: marker.width,
154
- height: marker.height,
155
- cornerRadius: marker.borderRadius,
156
- fill: this.swatches[index] ?? "white",
157
- stroke: marker.stroke,
158
- shadow: marker.shadow,
159
- }),
160
- ],
161
- }, () => this.reveal[index] ?? 0)
162
- // `gapScale` is a `Node` prop rather than a container one, so it is bound here
163
- // instead of in the config. It tracks the reveal so the flanking gap closes
164
- // with the entry rather than snapping when it reaches zero.
165
- item.set({ gapScale: () => this.reveal[index] ?? 0 })
166
- return item
167
- }
168
-
169
- /** True if series `index` is showing at all — see {@link reveal}. */
170
- isEnabled(index: number): boolean {
171
- return (this.reveal[index] ?? 0) > 0
172
- }
173
-
174
- /**
175
- * Grow series `index`'s entry back in at its series-order position, the
176
- * siblings sliding over to make room.
177
- *
178
- * A reveal tween rather than an animated `addChildAt`. The entry never left —
179
- * see {@link reveal} — so there is no add to have happened, and the whole
180
- * thing is a function of `t`.
181
- */
182
- @command()
183
- showSeries(args: CommandArgs<{ index: number }>): Seekable {
184
- const { index = 0 } = args.data ?? {}
185
- return this.revealTo(index, 1, args.duration ?? 0.35, args.easing ?? easeOut())
186
- }
187
-
188
- /**
189
- * Shrink series `index`'s entry away, the gap closing behind it — the mirror
190
- * of {@link showSeries}.
191
- */
192
- @command()
193
- hideSeries(args: CommandArgs<{ index: number }>): Seekable {
194
- const { index = 0 } = args.data ?? {}
195
- return this.revealTo(index, 0, args.duration ?? 0.35, args.easing ?? easeOut())
196
- }
110
+ export const ChartLegend = component2d({
111
+ key: "chartLegend",
112
+ state: {
113
+ ...row,
114
+ series: fresh<ChartSeries[]>(() => []),
115
+ swatches: fresh<Fill[]>(() => []),
116
+ legend: Types.custom<LegendConfig>(DEFAULT_LEGEND),
117
+ /**
118
+ * How far each entry is shown, `0`–`1`. Drives its width, opacity and gap
119
+ * together, so `0` is an entry that takes no room and `1` is one at its
120
+ * natural size.
121
+ *
122
+ * This replaces adding and removing the entry from the row: a command is
123
+ * asked what it looks like at a time, and "is this node a child" is not a
124
+ * value a time can produce. Collapsed-but-present is the same picture reached
125
+ * by a route that *is* a function of `t`.
126
+ */
127
+ reveal: fresh<number[], number[] | Readonly<Record<number, number>>>(() => [], { resolve: revealOf }),
128
+ },
129
+ // This node *is* the row, so the direction is its own answer. It has to be
130
+ // stated — a `Rect` given no flow lays its children out `freeform`, which
131
+ // stacks every entry on the same spot. The gap and padding are the theme's.
132
+ defaults: {
133
+ flow: "horizontal",
134
+ width: "hug",
135
+ height: "hug",
136
+ gap: (state) => state.legend.itemGap,
137
+ padding: (state) => state.legend.padding,
138
+ reveal: (state) => state.series.map((s) => ((s.enabled ?? true) ? 1 : 0)),
139
+ },
140
+ // One entry per series, in series order, every one a child for good: order is
141
+ // fixed rather than depending on which siblings happen to be shown, so an entry
142
+ // always comes back where it belongs.
143
+ children: (state) => {
144
+ const marker = state.legend.markerStyle
145
+ return state.series.map((series, index) =>
146
+ LegendEntry({
147
+ flow: "horizontal",
148
+ gap: 12,
149
+ align: "center",
150
+ width: "hug",
151
+ height: "hug",
152
+ // Bound to the reveal, so showing and hiding is a number moving rather
153
+ // than a child arriving and leaving. The width follows it in `measure`,
154
+ // and the flanking gap closes with the entry rather than snapping.
155
+ opacity: () => state.reveal[index] ?? 0,
156
+ gapScale: () => state.reveal[index] ?? 0,
157
+ shown: () => state.reveal[index] ?? 0,
158
+ children: [
159
+ Text({ ...textOps.styleProps(state.legend.textStyle), text: series.label }),
160
+ Rect({
161
+ width: marker.width,
162
+ height: marker.height,
163
+ cornerRadius: marker.borderRadius,
164
+ fill: state.swatches[index] ?? "white",
165
+ stroke: marker.stroke,
166
+ shadow: marker.shadow,
167
+ }),
168
+ ],
169
+ })
170
+ )
171
+ },
172
+ commands: {
173
+ ...Paint.commands,
174
+ /**
175
+ * Grow series `index`'s entry back in at its series-order position, the
176
+ * siblings sliding over to make room. A reveal tween rather than an animated
177
+ * `addChildAt`: the entry never left, so the whole thing is a function of `t`.
178
+ */
179
+ showSeries: {
180
+ args: { index: Types.number(0, { min: 0, step: 1 }) },
181
+ // An index that is not a series takes its slot and moves nothing.
182
+ run: (timeline, state) => (timeline.args.index in state.series
183
+ ? timeline.animate(revealFrame(state, timeline.args.index, 1, frameEase(timeline)))
184
+ : timeline.wait(timeline.duration)),
185
+ },
186
+ /** Shrink series `index`'s entry away, the gap closing behind it — the mirror of `showSeries`. */
187
+ hideSeries: {
188
+ args: { index: Types.number(0, { min: 0, step: 1 }) },
189
+ run: (timeline, state) => (timeline.args.index in state.series
190
+ ? timeline.animate(revealFrame(state, timeline.args.index, 0, frameEase(timeline)))
191
+ : timeline.wait(timeline.duration)),
192
+ },
193
+ /**
194
+ * Dim every entry but series `index` to {@link SPOTLIGHT_DIM} and bring that
195
+ * one back to full — the legend half of a chart's `spotlightSeries`. Entries
196
+ * stay in place; only their opacity moves, so the row doesn't reflow. Only
197
+ * the entries shown when it is built are touched: a hidden series' entry is
198
+ * left as `hideSeries` left it.
199
+ */
200
+ spotlightSeries: {
201
+ args: { index: Types.number(0, { min: 0, step: 1 }) },
202
+ run: (timeline, state, children) =>
203
+ timeline.together(
204
+ children.flatMap((entry, i) =>
205
+ (state.reveal[i] ?? 0) > 0 ? [entry.to({ opacity: i === timeline.args.index ? 1 : SPOTLIGHT_DIM })] : []
206
+ )
207
+ ),
208
+ },
209
+ },
210
+ measure: (measurer, state, children) => Flow.measure(measurer, state, children, Paint.inset(state)),
211
+ layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),
212
+ render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),
213
+ })
197
214
 
198
- /**
199
- * Ramp one entry's reveal to `target`.
200
- *
201
- * `from` is read when the command is built, which for a driven scene is with
202
- * the node in the state its own start time implies — so a hide that follows a
203
- * show starts where the show left it rather than at whatever the node happens
204
- * to hold now.
205
- */
206
- private revealTo(
207
- index: number,
208
- target: number,
209
- duration: number,
210
- easing: EasingFunction
211
- ): Seekable {
212
- if (this.items[index] === undefined) return hold(duration)
213
- const from = this.reveal[index] ?? 0
214
- return drive(duration, (t) => {
215
- const next = [...this.reveal]
216
- next[index] = from + (target - from) * easing(t)
217
- this.reveal = next
218
- })
219
- }
215
+ /**
216
+ * One entry's reveal ramping to `target` from where the command finds it — so a
217
+ * hide that follows a show starts where the show left it.
218
+ */
219
+ function revealFrame(state: { readonly reveal: readonly number[] }, index: number, target: number, ease: (t: number) => number) {
220
+ const from = state.reveal[index] ?? 0
221
+ return (t: number) => ({ reveal: { [index]: from + (target - from) * ease(t) } })
222
+ }
220
223
 
221
- /**
222
- * Dim every entry but series `index` to {@link SPOTLIGHT_DIM} and bring that
223
- * one back to full — the legend half of a chart's `spotlightSeries`. Entries
224
- * stay in place; only their opacity moves, so the row doesn't reflow.
225
- *
226
- * Only the currently-shown entries are touched — a hidden series' entry is
227
- * left as {@link hideSeries} left it.
228
- */
229
- @command()
230
- spotlightSeries(args: CommandArgs<{ index: number }>): Seekable {
231
- const { index = 0 } = args.data ?? {}
232
- const duration = args.duration ?? 0.35
233
- const easing = args.easing ?? easeOut()
234
- // Opacity only, on entries that are already in the row — no membership
235
- // changes, so unlike {@link showSeries} this is a plain function of `t`.
236
- return together(
237
- ...this.items
238
- .map((item, i) => ({ item, i }))
239
- .filter(({ i }) => this.isEnabled(i))
240
- .map(({ item, i }) =>
241
- item.to({ data: { opacity: i === index ? 1 : SPOTLIGHT_DIM }, duration, easing })
242
- )
243
- )
244
- }
224
+ /** Whether series `index` is showing at all on `legend`. */
225
+ export function legendShows(legend: { readonly reveal: readonly number[] }, index: number): boolean {
226
+ return (legend.reveal[index] ?? 0) > 0
245
227
  }
228
+
229
+ export type ChartLegend = NodeOf<typeof ChartLegend>
@@ -0,0 +1,20 @@
1
+ import { Types, type Element, type Node2D, type Type } from "@motionscript/sdk"
2
+
3
+ /**
4
+ * An entry made fresh for every node: each node gets its own array or box
5
+ * rather than one shared literal, and a host is told no default for it.
6
+ */
7
+ export function fresh<T, In = T>(make: () => T, options?: { readonly resolve?: (value: In) => T; readonly kind?: string }): Type<In, T> {
8
+ // A function default is a factory: the component calls it once per node.
9
+ return Types.custom<In, T>(make as unknown as In, options)
10
+ }
11
+
12
+ /**
13
+ * Nodes a `children` builder made with `new`, as the elements it returns. A
14
+ * part built that way can be read in a binding — its own box, or its parent's,
15
+ * which is the component it was built for — where a call would hand back only
16
+ * the element.
17
+ */
18
+ export function asParts(nodes: readonly Node2D[]): readonly Element[] {
19
+ return nodes as unknown as readonly Element[]
20
+ }
@@ -0,0 +1,90 @@
1
+ import { Types, easeOut, type EasingFunction, type Piece, type Timeline } from "@motionscript/sdk"
2
+
3
+ const identity: EasingFunction = (t) => t
4
+
5
+ /** Any component's timeline: these helpers only build pieces, which every timeline builds alike. */
6
+ type AnyTimeline = Timeline<any, any>
7
+
8
+ /**
9
+ * The ease a part's own frames apply to `t`: none when its command was given
10
+ * one, which already eases every frame the command builds, and otherwise the
11
+ * ease-out every chart command defaults to.
12
+ */
13
+ export function frameEase(timeline: { readonly easing: EasingFunction | undefined }): EasingFunction {
14
+ return timeline.easing ? identity : easeOut()
15
+ }
16
+
17
+ /** A piece of a command placed at its own time: from `at` seconds, for `duration` seconds. */
18
+ export interface Beat {
19
+ readonly at: number
20
+ readonly duration: number
21
+ readonly piece: Piece
22
+ }
23
+
24
+ /**
25
+ * `beats` at their own offsets over the command, each eased on its own and held
26
+ * at its last frame once it is done. A beat ahead of the playhead writes nothing,
27
+ * so whatever stands before it is written by a piece ahead of these. Beats that
28
+ * run past the command are played faster to end with it.
29
+ */
30
+ export function beats(timeline: AnyTimeline, parts: readonly Beat[]): Piece {
31
+ const total = timeline.duration
32
+ const natural = parts.reduce((end, part) => Math.max(end, part.at + part.duration), 0)
33
+ const k = natural > total && natural > 0 ? total / natural : 1
34
+ return timeline.together(
35
+ parts.map(({ at, duration, piece }) => {
36
+ const start = at * k
37
+ const length = duration * k
38
+ return timeline.sequence([timeline.wait(start), piece, timeline.wait(Math.max(0, total - start - length))])
39
+ })
40
+ )
41
+ }
42
+
43
+ /** How far one piece of a staggered entrance has come: its opacity, and how far it still sits off its place. */
44
+ export interface Arrival {
45
+ readonly opacity: number
46
+ readonly shift: number
47
+ }
48
+
49
+ /** Each piece's arrival, by key. An unlisted piece has landed. */
50
+ export type Arrivals = Readonly<Record<string, Arrival>>
51
+
52
+ export const LANDED: Arrival = { opacity: 1, shift: 0 }
53
+
54
+ /**
55
+ * A part's private record of its pieces' arrivals. A write merges into it, so
56
+ * beats placed apart each write their own piece and leave the others as the
57
+ * frame left them.
58
+ */
59
+ export function arrivalsEntry() {
60
+ return Types.custom<Arrivals>({}, { private: true, resolve: (next: Arrivals, previous?: Arrivals) => ({ ...previous, ...next }) })
61
+ }
62
+
63
+ export function arrivalOf(arrivals: Arrivals, key: string | number): Arrival {
64
+ return arrivals[key] ?? LANDED
65
+ }
66
+
67
+ /** Every piece in `keys` armed at `from`: the first frame of their entrance. */
68
+ export function armed(keys: readonly (string | number)[], from: Arrival): Arrivals {
69
+ const record: Record<string, Arrival> = {}
70
+ for (const key of keys) record[key] = from
71
+ return record
72
+ }
73
+
74
+ /**
75
+ * A piece arriving from `from` over its beat: a part armed at `from` tweened to
76
+ * `{ opacity: 1, [axis]: 0 }`, eased by `ease`.
77
+ */
78
+ export function arrival(timeline: AnyTimeline, key: string | number, from: Arrival, ease: EasingFunction): Piece {
79
+ return timeline.animate((t) => {
80
+ const e = ease(t)
81
+ return {
82
+ arrivals: {
83
+ [key]: {
84
+ opacity: from.opacity + (LANDED.opacity - from.opacity) * e,
85
+ shift: from.shift + (LANDED.shift - from.shift) * e,
86
+ },
87
+ },
88
+ }
89
+ })
90
+ }
@@ -0,0 +1,15 @@
1
+ import type { FillResolved, Loader, ShadowResolved, StrokeResolved } from "@motionscript/sdk"
2
+
3
+ /** Paints a chart part holds in entries of its own, which no paint entry loads for it. */
4
+ export interface PartPaints {
5
+ readonly fills?: readonly (readonly FillResolved[])[]
6
+ readonly strokes?: readonly (readonly StrokeResolved[])[]
7
+ readonly shadows?: readonly (readonly ShadowResolved[])[]
8
+ }
9
+
10
+ /** Declare `paints` from a component's `load`. */
11
+ export function loadPaints(loader: Loader, paints: PartPaints): void {
12
+ for (const fill of paints.fills ?? []) loader.fill(fill)
13
+ for (const strokes of paints.strokes ?? []) for (const stroke of strokes) loader.fill(stroke.fill)
14
+ for (const shadows of paints.shadows ?? []) for (const shadow of shadows) loader.fill(shadow.fill)
15
+ }
package/src/kit/shared.ts CHANGED
@@ -13,7 +13,7 @@
13
13
  * within one app.
14
14
  *
15
15
  * Kept renderer-agnostic (no `Node` imports) so a host's own inspector
16
- * mappings can build a partial theme without pulling the node classes in.
16
+ * mappings can build a partial theme without pulling the components in.
17
17
  *
18
18
  * **Two conventions run through every resolver and lerp below**, inherited from
19
19
  * the shape system's `resolveStroke`/`resolveShadow`:
@@ -101,7 +101,7 @@ export interface AxisDomainConfig {
101
101
  }
102
102
 
103
103
  /**
104
- * An axis domain's @property mapper: merges the author's partial input onto
104
+ * An axis domain entry's resolve: merges the author's partial input onto
105
105
  * `previous`, so a later partial `.set({ yAxis: {...} })` patches
106
106
  * `label`/`range`/`ticks` independently. `range`/`ticks` staying `undefined`
107
107
  * after the merge is the auto-derive signal the scale getters read.
@@ -119,7 +119,7 @@ export function resolveAxisDomain(
119
119
  }
120
120
 
121
121
  /**
122
- * An axis domain's @property tween: interpolates `range`/`ticks` numerically
122
+ * An axis domain entry's lerp: interpolates `range`/`ticks` numerically
123
123
  * (pairwise by index for `ticks`), so a `to` command that restates a range
124
124
  * rescales the plot smoothly. `label`, and either field when one side is in
125
125
  * auto-derive mode, step at the midpoint.
@@ -188,6 +188,55 @@ export function axisTweenTarget(
188
188
  return target
189
189
  }
190
190
 
191
+ /**
192
+ * An axis domain part-way through a tween that leaves or reaches a `range` or
193
+ * `ticks` left to the data: both ends as stored, and how far along. Only the
194
+ * chart knows what an auto side draws as, so {@link settleAxisDomain} resolves
195
+ * it there. Its own fields are the plain {@link lerpAxisDomain} of its ends, so
196
+ * a mapper merging onto one still reads a domain.
197
+ */
198
+ export interface AxisDomainBlend extends AxisDomainConfig {
199
+ readonly blend: { readonly from: AxisDomainConfig; readonly to: AxisDomainConfig; readonly t: number }
200
+ }
201
+
202
+ function blendOf(domain: AxisDomainConfig): AxisDomainBlend["blend"] | undefined {
203
+ return (domain as Partial<AxisDomainBlend>).blend
204
+ }
205
+
206
+ /**
207
+ * A chart's axis entry tween: what `to({ yAxis: { range } })` moves through.
208
+ * Between two explicit domains it is {@link lerpAxisDomain}, and a restated
209
+ * `range` without `ticks` spreads explicit ticks across it, keeping their count.
210
+ * When either end leaves `range` or `ticks` to the data, the value between is an
211
+ * {@link AxisDomainBlend}, which the chart draws as the blend of what each end
212
+ * draws: a rescale from an auto axis is continuous, not a cut at the midpoint.
213
+ */
214
+ export function lerpAxisEntry(from: AxisDomainConfig, to: AxisDomainConfig, t: number): AxisDomainConfig {
215
+ // The mapper merged the request onto `from`, so a field it left unstated is `from`'s own, by identity.
216
+ const rangeMoved = to.range !== from.range
217
+ const target = rangeMoved && to.range && to.ticks && to.ticks === from.ticks
218
+ ? { ...to, ticks: markerValues(to.range, Math.max(2, to.ticks.length) - 1) }
219
+ : to
220
+ const blended = blendOf(from) !== undefined
221
+ const moved = blended || rangeMoved || target.ticks !== from.ticks
222
+ const auto = blended || from.range === undefined || from.ticks === undefined || target.range === undefined || target.ticks === undefined
223
+ if (!moved || !auto) return lerpAxisDomain(from, target, t)
224
+ if (t === 0) return from
225
+ if (t === 1) return target
226
+ const blend: AxisDomainBlend = { ...lerpAxisDomain(from, target, t), blend: { from, to: target, t } }
227
+ return blend
228
+ }
229
+
230
+ /**
231
+ * `domain` as the chart draws it: `drawn` resolves a plain domain's auto `range`
232
+ * and `ticks` off the data, and a blend is the lerp of what its two ends draw.
233
+ */
234
+ export function settleAxisDomain(domain: AxisDomainConfig, drawn: (domain: AxisDomainConfig) => AxisDomainConfig): AxisDomainConfig {
235
+ const blend = blendOf(domain)
236
+ if (!blend) return drawn(domain)
237
+ return lerpAxisDomain(settleAxisDomain(blend.from, drawn), settleAxisDomain(blend.to, drawn), blend.t)
238
+ }
239
+
191
240
  /**
192
241
  * The low-level colour tokens the default themes are built from — a dark plot
193
242
  * panel with warm-grey axis furniture.