@motionscript/charts 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/dist/bar-chart/bar-chart.d.ts +286 -0
- package/dist/bar-chart/bar-chart.d.ts.map +1 -0
- package/dist/bar-chart/bar-chart.js +610 -0
- package/dist/bar-chart/bar-chart.js.map +1 -0
- package/dist/bar-chart/category-axis.d.ts +77 -0
- package/dist/bar-chart/category-axis.d.ts.map +1 -0
- package/dist/bar-chart/category-axis.js +141 -0
- package/dist/bar-chart/category-axis.js.map +1 -0
- package/dist/bar-chart/chart-bars.d.ts +149 -0
- package/dist/bar-chart/chart-bars.d.ts.map +1 -0
- package/dist/bar-chart/chart-bars.js +236 -0
- package/dist/bar-chart/chart-bars.js.map +1 -0
- package/dist/bar-chart/index.d.ts +16 -0
- package/dist/bar-chart/index.d.ts.map +1 -0
- package/dist/bar-chart/index.js +12 -0
- package/dist/bar-chart/index.js.map +1 -0
- package/dist/bar-chart/shared.d.ts +146 -0
- package/dist/bar-chart/shared.d.ts.map +1 -0
- package/dist/bar-chart/shared.js +182 -0
- package/dist/bar-chart/shared.js.map +1 -0
- package/dist/browser/chunks/chunk-ZMBOBHYL.js +2 -0
- package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +12 -0
- package/dist/engine.d.ts +27 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +27 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/chart-border.d.ts +28 -0
- package/dist/kit/chart-border.d.ts.map +1 -0
- package/dist/kit/chart-border.js +45 -0
- package/dist/kit/chart-border.js.map +1 -0
- package/dist/kit/chart-canvas.d.ts +56 -0
- package/dist/kit/chart-canvas.d.ts.map +1 -0
- package/dist/kit/chart-canvas.js +125 -0
- package/dist/kit/chart-canvas.js.map +1 -0
- package/dist/kit/chart-legend.d.ts +126 -0
- package/dist/kit/chart-legend.d.ts.map +1 -0
- package/dist/kit/chart-legend.js +212 -0
- package/dist/kit/chart-legend.js.map +1 -0
- package/dist/kit/shared.d.ts +251 -0
- package/dist/kit/shared.d.ts.map +1 -0
- package/dist/kit/shared.js +401 -0
- package/dist/kit/shared.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +99 -0
- package/dist/kit/vertical-axis.d.ts.map +1 -0
- package/dist/kit/vertical-axis.js +176 -0
- package/dist/kit/vertical-axis.js.map +1 -0
- package/dist/line-chart/chart-line.d.ts +91 -0
- package/dist/line-chart/chart-line.d.ts.map +1 -0
- package/dist/line-chart/chart-line.js +156 -0
- package/dist/line-chart/chart-line.js.map +1 -0
- package/dist/line-chart/chart-region.d.ts +47 -0
- package/dist/line-chart/chart-region.d.ts.map +1 -0
- package/dist/line-chart/chart-region.js +101 -0
- package/dist/line-chart/chart-region.js.map +1 -0
- package/dist/line-chart/horizontal-axis.d.ts +85 -0
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -0
- package/dist/line-chart/horizontal-axis.js +158 -0
- package/dist/line-chart/horizontal-axis.js.map +1 -0
- package/dist/line-chart/index.d.ts +22 -0
- package/dist/line-chart/index.d.ts.map +1 -0
- package/dist/line-chart/index.js +19 -0
- package/dist/line-chart/index.js.map +1 -0
- package/dist/line-chart/line-chart.d.ts +347 -0
- package/dist/line-chart/line-chart.d.ts.map +1 -0
- package/dist/line-chart/line-chart.js +701 -0
- package/dist/line-chart/line-chart.js.map +1 -0
- package/dist/line-chart/shared.d.ts +171 -0
- package/dist/line-chart/shared.d.ts.map +1 -0
- package/dist/line-chart/shared.js +270 -0
- package/dist/line-chart/shared.js.map +1 -0
- package/dist/line-chart/x-scale.d.ts +84 -0
- package/dist/line-chart/x-scale.d.ts.map +1 -0
- package/dist/line-chart/x-scale.js +302 -0
- package/dist/line-chart/x-scale.js.map +1 -0
- package/dist/nodes.d.ts +20 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +20 -0
- package/dist/nodes.js.map +1 -0
- package/dist/pie-chart/index.d.ts +18 -0
- package/dist/pie-chart/index.d.ts.map +1 -0
- package/dist/pie-chart/index.js +14 -0
- package/dist/pie-chart/index.js.map +1 -0
- package/dist/pie-chart/pie-chart.d.ts +170 -0
- package/dist/pie-chart/pie-chart.d.ts.map +1 -0
- package/dist/pie-chart/pie-chart.js +291 -0
- package/dist/pie-chart/pie-chart.js.map +1 -0
- package/dist/pie-chart/pie-legend.d.ts +60 -0
- package/dist/pie-chart/pie-legend.d.ts.map +1 -0
- package/dist/pie-chart/pie-legend.js +131 -0
- package/dist/pie-chart/pie-legend.js.map +1 -0
- package/dist/pie-chart/pie-ring.d.ts +266 -0
- package/dist/pie-chart/pie-ring.d.ts.map +1 -0
- package/dist/pie-chart/pie-ring.js +708 -0
- package/dist/pie-chart/pie-ring.js.map +1 -0
- package/dist/pie-chart/shared.d.ts +210 -0
- package/dist/pie-chart/shared.d.ts.map +1 -0
- package/dist/pie-chart/shared.js +240 -0
- package/dist/pie-chart/shared.js.map +1 -0
- package/package.json +69 -3
- package/registry.json +23 -0
- package/src/bar-chart/bar-chart.ts +804 -0
- package/src/bar-chart/category-axis.ts +177 -0
- package/src/bar-chart/chart-bars.ts +310 -0
- package/src/bar-chart/index.ts +31 -0
- package/src/bar-chart/shared.ts +354 -0
- package/src/engine.ts +26 -0
- package/src/index.ts +5 -0
- package/src/kit/chart-border.ts +61 -0
- package/src/kit/chart-canvas.ts +157 -0
- package/src/kit/chart-legend.ts +250 -0
- package/src/kit/shared.ts +675 -0
- package/src/kit/vertical-axis.ts +224 -0
- package/src/line-chart/chart-line.ts +208 -0
- package/src/line-chart/chart-region.ts +135 -0
- package/src/line-chart/horizontal-axis.ts +202 -0
- package/src/line-chart/index.ts +39 -0
- package/src/line-chart/line-chart.ts +894 -0
- package/src/line-chart/shared.ts +459 -0
- package/src/line-chart/x-scale.ts +367 -0
- package/src/nodes.ts +20 -0
- package/src/pie-chart/index.ts +37 -0
- package/src/pie-chart/pie-chart.ts +374 -0
- package/src/pie-chart/pie-legend.ts +159 -0
- package/src/pie-chart/pie-ring.ts +903 -0
- package/src/pie-chart/shared.ts +490 -0
- package/README.md +0 -4
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Rect,
|
|
3
|
+
Text,
|
|
4
|
+
command,
|
|
5
|
+
easeOut,
|
|
6
|
+
property,
|
|
7
|
+
textOps,
|
|
8
|
+
type CommandArgs,
|
|
9
|
+
type EasingFunction,
|
|
10
|
+
type Fill,
|
|
11
|
+
type NodeConfig,
|
|
12
|
+
type RectProps,
|
|
13
|
+
} from "@motionscript/core"
|
|
14
|
+
|
|
15
|
+
import { drive, hold, together, type Seekable } from "@motionscript/core/component"
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
DEFAULT_LEGEND,
|
|
19
|
+
SPOTLIGHT_DIM,
|
|
20
|
+
type ChartSeries,
|
|
21
|
+
type LegendConfig,
|
|
22
|
+
} from "./shared"
|
|
23
|
+
|
|
24
|
+
export interface ChartLegendProps extends RectProps {
|
|
25
|
+
/**
|
|
26
|
+
* One entry per series: legend `label`, its `enabled` default, and — through
|
|
27
|
+
* {@link ChartLegendProps.swatches} — the colour its chip is painted. A series
|
|
28
|
+
* that starts enabled (the default) is added to the row up front; one that
|
|
29
|
+
* starts `false` is hidden until a later {@link ChartLegend.showSeries}.
|
|
30
|
+
*/
|
|
31
|
+
series: ChartSeries[]
|
|
32
|
+
/**
|
|
33
|
+
* The swatch colour for each series, by index — resolved by the chart, whose
|
|
34
|
+
* business it is whether a series' colour comes out of a stroke palette (a
|
|
35
|
+
* line) or a fill palette (a bar). Shorter than the series list means the
|
|
36
|
+
* missing entries fall back to white.
|
|
37
|
+
*/
|
|
38
|
+
swatches: Fill[]
|
|
39
|
+
/** Padding, caption style, gaps, and swatch geometry — see {@link LegendConfig}. */
|
|
40
|
+
legend: LegendConfig
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The legend of a chart: a **horizontal-flow row of entries**, one per series, laid out
|
|
45
|
+
* left-to-right with the theme's `itemGap`. Each entry pairs the series label
|
|
46
|
+
* with a rounded colour swatch; only the swatch carries the series colour, the
|
|
47
|
+
* caption reads in the legend's neutral ink.
|
|
48
|
+
*
|
|
49
|
+
* {@link showSeries}/{@link hideSeries} flip a series on or off — showing one
|
|
50
|
+
* adds its entry back into the row at its stable series position and grows it
|
|
51
|
+
* in, hiding one shrinks it away and removes it. So the row only ever holds the
|
|
52
|
+
* currently-enabled series, in series order, and closes the gap when one leaves.
|
|
53
|
+
*
|
|
54
|
+
* This node *is* the row, so the parent places it (centred below the chart) and
|
|
55
|
+
* it hugs its entries.
|
|
56
|
+
*/
|
|
57
|
+
export class ChartLegend extends Rect {
|
|
58
|
+
@property({ default: () => [] as ChartSeries[] }) declare series: ChartSeries[]
|
|
59
|
+
@property({ default: () => [] as Fill[] }) declare swatches: Fill[]
|
|
60
|
+
@property({ default: DEFAULT_LEGEND }) declare legend: LegendConfig
|
|
61
|
+
|
|
62
|
+
/** One entry per series. **Every** entry is a child — see {@link reveal}. */
|
|
63
|
+
private items: Rect[] = []
|
|
64
|
+
/**
|
|
65
|
+
* How far each entry is shown, `0`–`1`. Drives its width, opacity and gap
|
|
66
|
+
* together, so `0` is an entry that takes no room and `1` is one at its
|
|
67
|
+
* natural size.
|
|
68
|
+
*
|
|
69
|
+
* This replaces adding and removing the entry from the row, which is what
|
|
70
|
+
* `showSeries`/`hideSeries` used to do. That could not be a command: a command
|
|
71
|
+
* is asked what it looks like at a time, and "is this node a child" is not a
|
|
72
|
+
* value a time can produce — the tree either contains it or it doesn't, and
|
|
73
|
+
* getting there means having run the add. Collapsed-but-present is the same
|
|
74
|
+
* picture reached by a route that *is* a function of `t`.
|
|
75
|
+
*
|
|
76
|
+
* A plain array rather than reactive props because the count is the series
|
|
77
|
+
* count, which is data. The entries' props are bound to it, so writing one
|
|
78
|
+
* still moves the layout.
|
|
79
|
+
*/
|
|
80
|
+
private reveal: number[] = []
|
|
81
|
+
/**
|
|
82
|
+
* Each entry's natural width, last time it was fully shown.
|
|
83
|
+
*
|
|
84
|
+
* A collapse needs numeric endpoints — `lerpSizeInput` snaps between a token
|
|
85
|
+
* and a number rather than interpolating — and `"hug"` is not a number until
|
|
86
|
+
* something measures it. Refreshed on every frame an entry is at full reveal
|
|
87
|
+
* (see {@link widthFor}), so it is current by the time a hide starts, and it
|
|
88
|
+
* tracks a relabelled series or a restyled legend without being told.
|
|
89
|
+
*/
|
|
90
|
+
private natural: number[] = []
|
|
91
|
+
|
|
92
|
+
constructor(props?: NodeConfig<ChartLegend, ChartLegendProps>) {
|
|
93
|
+
// `flow` after the spread, with the rest of the geometry: this class *is*
|
|
94
|
+
// the row, so the direction is its own answer and not the caller's. It has
|
|
95
|
+
// to be stated — a `Rect` given no flow lays its children out `freeform`,
|
|
96
|
+
// which stacks every entry on the same spot. One series looks perfect that
|
|
97
|
+
// way and three look like one smeared caption, so the failure only shows up
|
|
98
|
+
// on the charts that most need a legend.
|
|
99
|
+
super({
|
|
100
|
+
...(props as RectProps),
|
|
101
|
+
flow: "horizontal",
|
|
102
|
+
align: "center",
|
|
103
|
+
width: "hug",
|
|
104
|
+
height: "hug",
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
// Structure is one entry per series, so it's composed here from the resolved
|
|
108
|
+
// props super() applied. Fixed row geometry last, so it wins over super().
|
|
109
|
+
this.set({ gap: this.legend.itemGap, padding: this.legend.padding })
|
|
110
|
+
|
|
111
|
+
this.reveal = this.series.map((s) => ((s.enabled ?? true) ? 1 : 0))
|
|
112
|
+
this.natural = this.series.map(() => 0)
|
|
113
|
+
this.items = this.series.map((s, i) => this.buildItem(s, i))
|
|
114
|
+
|
|
115
|
+
// All of them, in series order. Order is now fixed rather than depending on
|
|
116
|
+
// which siblings happen to be shown, so an entry always comes back where it
|
|
117
|
+
// belongs and `insertionIndex` has nothing left to compute.
|
|
118
|
+
for (const item of this.items) this.add(item)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* An entry's width for its current reveal.
|
|
123
|
+
*
|
|
124
|
+
* `"hug"` at full reveal rather than the pinned number, so a shown entry stays
|
|
125
|
+
* responsive to its own content; the number only takes over while it is
|
|
126
|
+
* collapsing, which is the only time a token cannot express it.
|
|
127
|
+
*
|
|
128
|
+
* Records the natural width as a side effect, which is the honest place for it:
|
|
129
|
+
* this runs inside a binding, so it is already reading the layout every frame,
|
|
130
|
+
* and the value it wants is the one the layout just produced. It writes a plain
|
|
131
|
+
* array rather than a signal, so nothing is invalidated by it.
|
|
132
|
+
*/
|
|
133
|
+
private widthFor(index: number): number | "hug" {
|
|
134
|
+
const shown = this.reveal[index] ?? 0
|
|
135
|
+
if (shown >= 1) {
|
|
136
|
+
const measured = this.items[index]?.measuredWidth ?? 0
|
|
137
|
+
if (measured > 0) this.natural[index] = measured
|
|
138
|
+
return "hug"
|
|
139
|
+
}
|
|
140
|
+
return shown * (this.natural[index] ?? 0)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** One entry: the series caption beside its colour swatch. */
|
|
144
|
+
private buildItem(series: ChartSeries, index: number): Rect {
|
|
145
|
+
const marker = this.legend.markerStyle
|
|
146
|
+
const item = new Rect({ flow: "horizontal",
|
|
147
|
+
gap: 12,
|
|
148
|
+
align: "center",
|
|
149
|
+
// Bound to the reveal, so showing and hiding is a number moving rather
|
|
150
|
+
// than a child arriving and leaving.
|
|
151
|
+
width: () => this.widthFor(index),
|
|
152
|
+
opacity: () => this.reveal[index] ?? 0,
|
|
153
|
+
height: "hug",
|
|
154
|
+
children: [
|
|
155
|
+
new Text({
|
|
156
|
+
...textOps.styleProps(this.legend.textStyle),
|
|
157
|
+
text: series.label,
|
|
158
|
+
}),
|
|
159
|
+
new Rect({
|
|
160
|
+
width: marker.width,
|
|
161
|
+
height: marker.height,
|
|
162
|
+
cornerRadius: marker.borderRadius,
|
|
163
|
+
fill: this.swatches[index] ?? "white",
|
|
164
|
+
stroke: marker.stroke,
|
|
165
|
+
shadow: marker.shadow,
|
|
166
|
+
}),
|
|
167
|
+
],
|
|
168
|
+
})
|
|
169
|
+
// `gapScale` is a `Node` prop rather than a container one, so it is bound here
|
|
170
|
+
// instead of in the config. It tracks the reveal so the flanking gap closes
|
|
171
|
+
// with the entry rather than snapping when it reaches zero.
|
|
172
|
+
item.set({ gapScale: () => this.reveal[index] ?? 0 })
|
|
173
|
+
return item
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** True if series `index` is showing at all — see {@link reveal}. */
|
|
177
|
+
isEnabled(index: number): boolean {
|
|
178
|
+
return (this.reveal[index] ?? 0) > 0
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Grow series `index`'s entry back in at its series-order position, the
|
|
183
|
+
* siblings sliding over to make room.
|
|
184
|
+
*
|
|
185
|
+
* A reveal tween rather than an animated `addChildAt`. The entry never left —
|
|
186
|
+
* see {@link reveal} — so there is no add to have happened, and the whole
|
|
187
|
+
* thing is a function of `t`.
|
|
188
|
+
*/
|
|
189
|
+
@command()
|
|
190
|
+
showSeries(args: CommandArgs<{ index: number }>): Seekable {
|
|
191
|
+
const { index = 0 } = args.data ?? {}
|
|
192
|
+
return this.revealTo(index, 1, args.duration ?? 0.35, args.easing ?? easeOut())
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Shrink series `index`'s entry away, the gap closing behind it — the mirror
|
|
197
|
+
* of {@link showSeries}.
|
|
198
|
+
*/
|
|
199
|
+
@command()
|
|
200
|
+
hideSeries(args: CommandArgs<{ index: number }>): Seekable {
|
|
201
|
+
const { index = 0 } = args.data ?? {}
|
|
202
|
+
return this.revealTo(index, 0, args.duration ?? 0.35, args.easing ?? easeOut())
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Ramp one entry's reveal to `target`.
|
|
207
|
+
*
|
|
208
|
+
* `from` is read when the command is built, which for a driven scene is with
|
|
209
|
+
* the node in the state its own start time implies — so a hide that follows a
|
|
210
|
+
* show starts where the show left it rather than at whatever the node happens
|
|
211
|
+
* to hold now.
|
|
212
|
+
*/
|
|
213
|
+
private revealTo(
|
|
214
|
+
index: number,
|
|
215
|
+
target: number,
|
|
216
|
+
duration: number,
|
|
217
|
+
easing: EasingFunction
|
|
218
|
+
): Seekable {
|
|
219
|
+
if (this.items[index] === undefined) return hold(duration)
|
|
220
|
+
const from = this.reveal[index] ?? 0
|
|
221
|
+
return drive(duration, (t) => {
|
|
222
|
+
this.reveal[index] = from + (target - from) * easing(t)
|
|
223
|
+
})
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Dim every entry but series `index` to {@link SPOTLIGHT_DIM} and bring that
|
|
228
|
+
* one back to full — the legend half of a chart's `spotlightSeries`. Entries
|
|
229
|
+
* stay in place; only their opacity moves, so the row doesn't reflow.
|
|
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
|
+
}
|