@motionscript/charts 0.1.0-alpha.3 → 0.1.0-alpha.6
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.
- package/CHANGELOG.md +63 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +99 -0
- package/dist/bar-chart/bar-chart.d.ts +93 -231
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +378 -565
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +24 -41
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +93 -115
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +36 -70
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +156 -210
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +2 -2
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +1 -1
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +6 -6
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +6 -6
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
- package/dist/browser/chunks/chunk-XROBIRQK.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +3 -2
- package/dist/engine.d.ts +8 -6
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +8 -6
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/cartesian.d.ts +44 -0
- package/dist/kit/cartesian.d.ts.map +1 -0
- package/dist/kit/cartesian.js +59 -0
- package/dist/kit/cartesian.js.map +1 -0
- package/dist/kit/chart-border.d.ts +8 -11
- package/dist/kit/chart-border.d.ts.map +1 -1
- package/dist/kit/chart-border.js +15 -32
- package/dist/kit/chart-border.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +23 -21
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +33 -90
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +79 -88
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +137 -186
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.d.ts +17 -0
- package/dist/kit/entries.d.ts.map +1 -0
- package/dist/kit/entries.js +19 -0
- package/dist/kit/entries.js.map +1 -0
- package/dist/kit/motion.d.ts +48 -0
- package/dist/kit/motion.d.ts.map +1 -0
- package/dist/kit/motion.js +63 -0
- package/dist/kit/motion.js.map +1 -0
- package/dist/kit/paints.d.ts +10 -0
- package/dist/kit/paints.d.ts.map +1 -0
- package/dist/kit/paints.js +12 -0
- package/dist/kit/paints.js.map +1 -0
- package/dist/kit/shared.d.ts +59 -8
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +105 -10
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +36 -61
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +97 -132
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +27 -53
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +89 -120
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +14 -14
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +26 -64
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +32 -56
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +85 -123
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +2 -2
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +1 -1
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +111 -311
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +403 -639
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +6 -6
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +22 -7
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +1 -1
- package/dist/line-chart/x-scale.js +1 -1
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.d.ts +6 -9
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +7 -9
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +2 -2
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +77 -134
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +199 -268
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +54 -31
- package/dist/pie-chart/pie-legend.d.ts.map +1 -1
- package/dist/pie-chart/pie-legend.js +93 -107
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +142 -122
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +333 -361
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +3 -3
- package/dist/pie-chart/shared.js +3 -3
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +6 -5
- package/registry.json +23 -23
- package/src/bar-chart/bar-chart.ts +534 -814
- package/src/bar-chart/category-axis.ts +162 -177
- package/src/bar-chart/chart-bars.ts +288 -310
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +28 -26
- package/src/index.ts +5 -5
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +50 -61
- package/src/kit/chart-canvas.ts +122 -157
- package/src/kit/chart-legend.ts +229 -250
- package/src/kit/entries.ts +20 -0
- package/src/kit/motion.ts +90 -0
- package/src/kit/paints.ts +15 -0
- package/src/kit/shared.ts +782 -675
- package/src/kit/vertical-axis.ts +194 -224
- package/src/line-chart/chart-line.ts +181 -208
- package/src/line-chart/chart-region.ts +106 -135
- package/src/line-chart/horizontal-axis.ts +166 -202
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +585 -929
- package/src/line-chart/shared.ts +473 -459
- package/src/line-chart/x-scale.ts +367 -367
- package/src/nodes.ts +18 -20
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +293 -381
- package/src/pie-chart/pie-legend.ts +169 -159
- package/src/pie-chart/pie-ring.ts +848 -903
- package/src/pie-chart/shared.ts +490 -490
- package/dist/browser/chunks/chunk-RMHQ6GYU.js +0 -2
- package/dist/browser/chunks/chunk-RMHQ6GYU.js.map +0 -7
package/src/kit/chart-legend.ts
CHANGED
|
@@ -1,250 +1,229 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
type Fill,
|
|
11
|
-
type
|
|
12
|
-
type RectProps,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
type
|
|
21
|
-
type
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
*
|
|
231
|
-
* Only the currently-shown entries are touched — a hidden series' entry is
|
|
232
|
-
* left as {@link hideSeries} left it.
|
|
233
|
-
*/
|
|
234
|
-
@command()
|
|
235
|
-
spotlightSeries(args: CommandArgs<{ index: number }>): Seekable {
|
|
236
|
-
const { index = 0 } = args.data ?? {}
|
|
237
|
-
const duration = args.duration ?? 0.35
|
|
238
|
-
const easing = args.easing ?? easeOut()
|
|
239
|
-
// Opacity only, on entries that are already in the row — no membership
|
|
240
|
-
// changes, so unlike {@link showSeries} this is a plain function of `t`.
|
|
241
|
-
return together(
|
|
242
|
-
...this.items
|
|
243
|
-
.map((item, i) => ({ item, i }))
|
|
244
|
-
.filter(({ i }) => this.isEnabled(i))
|
|
245
|
-
.map(({ item, i }) =>
|
|
246
|
-
item.to({ data: { opacity: i === index ? 1 : SPOTLIGHT_DIM }, duration, easing })
|
|
247
|
-
)
|
|
248
|
-
)
|
|
249
|
-
}
|
|
250
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Flow,
|
|
3
|
+
Paint,
|
|
4
|
+
Rect,
|
|
5
|
+
Shapes,
|
|
6
|
+
Text,
|
|
7
|
+
Types,
|
|
8
|
+
component2d,
|
|
9
|
+
textOps,
|
|
10
|
+
type Fill,
|
|
11
|
+
type NodeOf,
|
|
12
|
+
type RectProps,
|
|
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,
|
|
30
|
+
} from "@motionscript/sdk"
|
|
31
|
+
|
|
32
|
+
import { fresh } from "./entries"
|
|
33
|
+
import { frameEase } from "./motion"
|
|
34
|
+
import {
|
|
35
|
+
DEFAULT_LEGEND,
|
|
36
|
+
SPOTLIGHT_DIM,
|
|
37
|
+
type ChartSeries,
|
|
38
|
+
type LegendConfig,
|
|
39
|
+
} from "./shared"
|
|
40
|
+
|
|
41
|
+
export interface ChartLegendProps extends RectProps {
|
|
42
|
+
/**
|
|
43
|
+
* One entry per series: legend `label`, its `enabled` default, and — through
|
|
44
|
+
* {@link ChartLegendProps.swatches} — the colour its chip is painted. A series
|
|
45
|
+
* that starts enabled (the default) is added to the row up front; one that
|
|
46
|
+
* starts `false` is hidden until a later `showSeries`.
|
|
47
|
+
*/
|
|
48
|
+
series: ChartSeries[]
|
|
49
|
+
/**
|
|
50
|
+
* The swatch colour for each series, by index — resolved by the chart, whose
|
|
51
|
+
* business it is whether a series' colour comes out of a stroke palette (a
|
|
52
|
+
* line) or a fill palette (a bar). Shorter than the series list means the
|
|
53
|
+
* missing entries fall back to white.
|
|
54
|
+
*/
|
|
55
|
+
swatches: Fill[]
|
|
56
|
+
/** Padding, caption style, gaps, and swatch geometry — see {@link LegendConfig}. */
|
|
57
|
+
legend: LegendConfig
|
|
58
|
+
}
|
|
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
|
+
|
|
63
|
+
/**
|
|
64
|
+
* One legend entry. It hugs its caption and swatch; `shown` scales that width
|
|
65
|
+
* from nothing to the whole of it.
|
|
66
|
+
*
|
|
67
|
+
* The width is taken from the content on every measure, at any reveal, so a frame
|
|
68
|
+
* never depends on whether the entry was once seen fully shown. A collapse needs a
|
|
69
|
+
* number to scale, and `"hug"` is not one until something measures it — this is
|
|
70
|
+
* the something.
|
|
71
|
+
*/
|
|
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
|
+
})
|
|
83
|
+
|
|
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
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The legend of a chart: a **horizontal-flow row of entries**, one per series, laid out
|
|
98
|
+
* left-to-right with the theme's `itemGap`. Each entry pairs the series label
|
|
99
|
+
* with a rounded colour swatch; only the swatch carries the series colour, the
|
|
100
|
+
* caption reads in the legend's neutral ink.
|
|
101
|
+
*
|
|
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.
|
|
106
|
+
*
|
|
107
|
+
* This node *is* the row, so the parent places it (centred below the chart) and
|
|
108
|
+
* it hugs its entries.
|
|
109
|
+
*/
|
|
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
|
+
})
|
|
214
|
+
|
|
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
|
+
}
|
|
223
|
+
|
|
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
|
|
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
|
+
}
|