@motionscript/charts 0.1.0-alpha.5 → 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.
Files changed (141) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +30 -16
  3. package/dist/bar-chart/bar-chart.d.ts +93 -246
  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 +24 -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 +36 -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 +8 -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 +23 -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 +79 -67
  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 +36 -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 +25 -51
  70. package/dist/line-chart/chart-line.d.ts.map +1 -1
  71. package/dist/line-chart/chart-line.js +89 -120
  72. package/dist/line-chart/chart-line.js.map +1 -1
  73. package/dist/line-chart/chart-region.d.ts +14 -14
  74. package/dist/line-chart/chart-region.d.ts.map +1 -1
  75. package/dist/line-chart/chart-region.js +26 -64
  76. package/dist/line-chart/chart-region.js.map +1 -1
  77. package/dist/line-chart/horizontal-axis.d.ts +32 -56
  78. package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
  79. package/dist/line-chart/horizontal-axis.js +85 -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 +111 -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 +77 -134
  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 +54 -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 +142 -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/package.json +4 -4
  114. package/src/bar-chart/bar-chart.ts +415 -701
  115. package/src/bar-chart/category-axis.ts +95 -110
  116. package/src/bar-chart/chart-bars.ts +178 -200
  117. package/src/bar-chart/index.ts +2 -2
  118. package/src/bar-chart/shared.ts +3 -3
  119. package/src/engine.ts +8 -6
  120. package/src/kit/cartesian.ts +81 -0
  121. package/src/kit/chart-border.ts +22 -33
  122. package/src/kit/chart-canvas.ts +44 -79
  123. package/src/kit/chart-legend.ts +173 -189
  124. package/src/kit/entries.ts +20 -0
  125. package/src/kit/motion.ts +90 -0
  126. package/src/kit/paints.ts +15 -0
  127. package/src/kit/shared.ts +52 -3
  128. package/src/kit/vertical-axis.ts +110 -140
  129. package/src/line-chart/chart-line.ts +95 -122
  130. package/src/line-chart/chart-region.ts +30 -59
  131. package/src/line-chart/horizontal-axis.ts +99 -135
  132. package/src/line-chart/index.ts +2 -2
  133. package/src/line-chart/line-chart.ts +449 -825
  134. package/src/line-chart/shared.ts +3 -3
  135. package/src/nodes.ts +8 -10
  136. package/src/pie-chart/index.ts +2 -2
  137. package/src/pie-chart/pie-chart.ts +221 -311
  138. package/src/pie-chart/pie-legend.ts +124 -114
  139. package/src/pie-chart/pie-ring.ts +386 -473
  140. package/dist/browser/chunks/chunk-5ZPBV6BU.js +0 -2
  141. package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +0 -7
@@ -1,290 +1,219 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { Rect, command, node, Node2D, easeOut, property, } from "@motionscript/sdk";
1
+ import { Rect, Types, component2d, easeOut, } from "@motionscript/sdk";
2
+ import { asParts, fresh } from "../kit/entries.js";
3
+ import { beats } from "../kit/motion.js";
8
4
  import { clampUnit } from "../kit/shared.js";
9
5
  import { PieLegend } from "./pie-legend.js";
10
6
  import { PieRing } from "./pie-ring.js";
11
7
  import { DEFAULT_PIE_CHART_THEME, lerpPieChartTheme, resolvePieChartTheme, resolvePieSlices, } from "./shared.js";
12
- import { asCommand, hold, sequence as sequenceOf, together, } from "@motionscript/sdk/component";
13
8
  /**
14
- * How {@link PieChart.enter} divides its duration: the wedges sweeping in, then
15
- * the labels and the legend arriving together. Shares of the whole, so they sum
16
- * to 1 — the entrance takes exactly the time it was given, for the reason spelt
17
- * out on `LineChart.enter`.
9
+ * How `enter` divides its duration: the wedges sweeping in, then the labels and
10
+ * the legend arriving together. Shares of the whole, so they sum to 1.
18
11
  */
19
12
  const SWEEP_SHARE = 0.65;
20
13
  const LABEL_SHARE = 0.35;
14
+ const SLICE_ARG = Types.number(0, { min: 0, step: 1 });
15
+ /** The slices the ring and the legend draw, resolved from the rows and the palette. */
16
+ function slicesOf(s) {
17
+ return resolvePieSlices(s.data, s.categoryField, s.valueField, s.theme.sliceFills);
18
+ }
19
+ /** How far slice `index` is pushed out, in `[0, 1]`. */
20
+ function popAt(s, index) {
21
+ return clampUnit(s.pops[index] ?? 0);
22
+ }
23
+ /** Whether slice `index` is grown out past the ring's rim: past half way, so a slice mid-swell is not yet counted. */
24
+ export function isSliceSelected(s, index) {
25
+ return popAt(s, index) > 0.5;
26
+ }
21
27
  /**
22
- * A pie or donut chart, composed from two independent parts rather than one
23
- * monolithic draw — the third of the chart family, built the same way as the
24
- * line and bar charts beside it:
28
+ * How far each slice is dimmed by the selection, one entry per slice: 1 while
29
+ * nothing is out, and for the selected slice itself; falling toward the theme's
30
+ * `dimOpacity` for the rest in proportion to how far the selection has grown.
31
+ */
32
+ function sliceDims(s) {
33
+ let focus = 0;
34
+ for (let i = 0; i < s.data.length; i++)
35
+ focus = Math.max(focus, popAt(s, i));
36
+ const dim = s.theme.spotlight.dimOpacity;
37
+ if (focus <= 0)
38
+ return s.data.map(() => 1);
39
+ return s.data.map((_, i) => 1 - (1 - clampUnit(dim)) * focus * (1 - popAt(s, i)));
40
+ }
41
+ /**
42
+ * Every slice's pop eased from where it is to what `pop` asks for. A move that
43
+ * changes nothing still takes its slot, writing nothing.
44
+ */
45
+ function popping(timeline, s, pop) {
46
+ const from = s.data.map((_, i) => popAt(s, i));
47
+ const targets = s.data.map((_, i) => clampUnit(pop(i)));
48
+ if (from.every((value, i) => value === targets[i]))
49
+ return timeline.wait(timeline.duration);
50
+ return timeline.animate((t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }));
51
+ }
52
+ /** The ring and the legend, found through the chart's children: the last row of them is the one it built. */
53
+ function partsOf(children) {
54
+ for (let i = children.length - 1; i >= 0; i--) {
55
+ const row = children[i];
56
+ const ring = Rect.is(row) ? row.children.find(PieRing.is) : undefined;
57
+ if (ring)
58
+ return { ring, legend: row.children.find(PieLegend.is) ?? null };
59
+ }
60
+ return null;
61
+ }
62
+ /**
63
+ * A pie or donut chart, composed from two parts laid out by a horizontal-flow
64
+ * {@link Rect} — the ring filling whatever the hugging legend leaves, both
65
+ * centred:
25
66
  *
26
67
  * - {@link PieRing} — the wedges, each sized by its share of the total, with its
27
- * label set inside its own band or pinned outside on a leader line, and the
28
- * summary in the hole. The only part of the family that draws a path.
68
+ * label inside its own band or pinned outside on a leader line, and the
69
+ * summary in the hole.
29
70
  * - {@link PieLegend} — a column of swatch-and-caption rows naming each slice.
30
71
  *
31
- * The two are laid out by a plain horizontal-flow {@link Rect}: the ring fills whatever the
32
- * hugging legend leaves, both centred, so the chart re-flows if its box changes.
33
- *
34
- * **One mark per row, not per column** — which is what makes this the odd one of
35
- * the three. A line or a bar chart plots *series*, so its colours belong to
36
- * columns and hiding one is a sensible thing to ask for. A pie plots rows, so its
37
- * palette is indexed by row and there is nothing to hide: dropping a slice would
38
- * silently re-cut every other wedge's share, which is a different chart rather
39
- * than the same chart with a series switched off. {@link selectSlice} and its
40
- * siblings are what a pie has instead — a slice swells out of the ring, its hole
41
- * edge staying put, and everything else dims behind it.
72
+ * **One mark per row, not per column**: a pie's palette is indexed by row and
73
+ * there is nothing to hide, since dropping a slice would re-cut every other
74
+ * wedge. `selectSlice` and its siblings are what a pie has instead — a slice
75
+ * swells out of the ring and everything else dims behind it.
42
76
  *
43
- * **Resting state is the finished chart** — the same bargain the other two make:
44
- * an editor paints a node's Initial state on a paused canvas, so a chart that
45
- * only appeared once a command had run would be edited blind. {@link enter} arms
46
- * every part back to its pre-entrance state before animating. The one exception
47
- * is the hole's summary, which a scene can hold back with `showCenter: false`
48
- * because it is usually the chart's *conclusion* rather than part of its setup.
77
+ * **Resting state is the finished chart**; `enter` arms every part on its first
78
+ * frame. The one exception is the hole's summary, which a scene can hold back
79
+ * with `showCenter: false`, because it is usually the chart's *conclusion*.
49
80
  */
50
- let PieChart = class PieChart extends Node2D {
51
- // ---- Part handles (built in the constructor) --------------------------
52
- ring;
53
- legend = null;
54
- /** `this.theme` read back through its *resolved* shape — see `LineChart.theme`. */
55
- get resolvedTheme() {
56
- return this.theme;
57
- }
58
- /** The slices the ring and the legend draw, resolved from the rows and the palette. */
59
- get slices() {
60
- return resolvePieSlices(this.data, this.categoryField, this.valueField, this.resolvedTheme.sliceFills);
61
- }
62
- /**
63
- * How far each slice is dimmed by the current selection, one entry per slice —
64
- * 1 while nothing is selected, and for the selected slice itself; falling
65
- * toward the theme's `dimOpacity` for the rest in proportion to how far the
66
- * selection has grown.
67
- */
68
- sliceDims() {
69
- let focus = 0;
70
- for (let i = 0; i < this.data.length; i++) {
71
- focus = Math.max(focus, this.popAt(i));
72
- }
73
- const dim = this.resolvedTheme.spotlight.dimOpacity;
74
- if (focus <= 0)
75
- return this.data.map(() => 1);
76
- return this.data.map((_, i) => 1 - (1 - clampUnit(dim)) * focus * (1 - this.popAt(i)));
77
- }
78
- /** How far slice `index` is currently pushed out, in `[0, 1]`. */
79
- popAt(index) {
80
- return clampUnit(this.pops[index] ?? 0);
81
- }
82
- constructor(props) {
83
- super(props);
84
- // The whole chart is composed from plain props/`data` — no context needed,
85
- // so the parts are built here (super() applied the props above). The slice
86
- // *structure* (one label and one legend row per row of `data`) is read once,
87
- // like every other chart's series list; everything the parts draw with,
88
- // including the slices themselves and the paint they take from the palette,
89
- // is passed as a closure, so `chart.to({ theme })` genuinely re-paints the
90
- // ring and the swatches and a `set` of `data` re-cuts the wedges.
91
- const slices = this.slices;
92
- const showLegend = props?.showLegend !== false;
93
- this.pops = slices.map(() => 0);
94
- this.ring = new PieRing({
95
- width: "fill",
96
- height: "fill",
97
- slices: () => this.slices,
98
- ring: () => this.resolvedTheme.ring,
99
- label: () => this.resolvedTheme.label,
100
- centerStyle: () => this.resolvedTheme.center,
101
- labelMode: this.labelMode,
102
- labelPlacement: this.labelPlacement,
103
- pops: () => this.pops,
104
- dimOpacity: () => this.resolvedTheme.spotlight.dimOpacity,
105
- centerLabel: this.centerLabel,
106
- centerCaption: this.centerCaption,
107
- centerReveal: props?.showCenter === false ? 0 : 1,
108
- });
109
- const children = [this.ring];
110
- if (showLegend) {
111
- this.legend = new PieLegend({
112
- slices: () => this.slices,
113
- legend: () => this.resolvedTheme.legend,
114
- dims: () => this.sliceDims(),
115
- });
116
- children.push(this.legend);
81
+ export const PieChart = component2d({
82
+ key: "pieChart",
83
+ state: {
84
+ data: fresh(() => []),
85
+ categoryField: "",
86
+ valueField: "",
87
+ theme: Types.custom(DEFAULT_PIE_CHART_THEME, { resolve: resolvePieChartTheme, lerp: lerpPieChartTheme }),
88
+ labelMode: Types.custom("percent"),
89
+ labelPlacement: Types.custom("leader"),
90
+ gap: 40,
91
+ centerLabel: "",
92
+ centerCaption: "",
93
+ /**
94
+ * How far each slice is pushed out of the ring, `0`–`1` per index. Held here
95
+ * rather than on the ring because two parts read it: the wedges grow out by
96
+ * it, and the legend entries dim by it. Seeded flush, one to a row.
97
+ */
98
+ pops: fresh(() => []),
99
+ /** Read as the chart is built: the legend is a part the chart builds or doesn't. */
100
+ showLegend: Types.boolean(true, { animatable: false }),
101
+ /** Read as the chart is built: whether the hole's summary is drawn at rest. */
102
+ showCenter: Types.boolean(true, { animatable: false }),
103
+ },
104
+ defaults: {
105
+ // Its parts fill it; a node made of them would hug them.
106
+ width: "fill",
107
+ height: "fill",
108
+ pops: (state) => slicesOf(state).map(() => 0),
109
+ },
110
+ // The slice *structure* (one label and one legend row per row) is read once;
111
+ // the slices themselves, their paint and the selection are bound, so a theme
112
+ // tween re-paints and a `set` of `data` re-cuts the wedges.
113
+ children: (state) => {
114
+ const theme = () => state.theme;
115
+ const slices = () => slicesOf(state);
116
+ const parts = [
117
+ new PieRing({
118
+ width: "fill",
119
+ height: "fill",
120
+ slices,
121
+ ring: () => theme().ring,
122
+ label: () => theme().label,
123
+ centerStyle: () => theme().center,
124
+ labelMode: state.labelMode,
125
+ labelPlacement: state.labelPlacement,
126
+ pops: () => state.pops,
127
+ dimOpacity: () => theme().spotlight.dimOpacity,
128
+ centerLabel: state.centerLabel,
129
+ centerCaption: state.centerCaption,
130
+ centerReveal: state.showCenter === false ? 0 : 1,
131
+ }),
132
+ ];
133
+ if (state.showLegend !== false) {
134
+ parts.push(new PieLegend({
135
+ slices,
136
+ legend: () => theme().legend,
137
+ dims: () => sliceDims(state),
138
+ // The column's spacing is the theme's as the chart was built, like the
139
+ // entries' type and chips.
140
+ gap: state.theme.legend.itemGap / 3,
141
+ padding: state.theme.legend.padding,
142
+ }));
117
143
  }
118
- this.add(new Rect({ flow: "horizontal",
119
- width: "fill",
120
- height: "fill",
121
- gap: this.gap,
122
- align: "center",
123
- children,
124
- }));
125
- }
126
- // ---- Orchestrated entrance --------------------------------------------
127
- /**
128
- * Prime every part at its pre-entrance state — no wedges swept, no labels, the
129
- * legend shifted and transparent.
130
- *
131
- * Called by {@link enter} before its first frame so nothing flashes ahead of
132
- * its beat. Idempotent, and public so a scene can hide a chart ahead of time
133
- * without starting the entrance. The hole's summary is deliberately untouched:
134
- * it has its own signal, so a scene that set it up to land later keeps that.
135
- */
136
- arm() {
137
- this.ring.set({ growth: 0, labelReveal: 0 });
138
- this.legend?.arm();
139
- }
140
- /**
141
- * Animate the whole chart in: the wedges sweep in clockwise from the theme's
142
- * `startAngle`, then the labels fade up and the legend staggers in together.
143
- *
144
- * `duration` is the **whole** entrance; the phases are shares of it.
145
- */
146
- enter(args) {
147
- const duration = args.duration;
148
- const easing = args.easing ?? easeOut();
149
- this.arm();
150
- return asCommand(sequenceOf(this.ring.sweep({ duration: duration * SWEEP_SHARE, easing }), together(this.ring.revealLabels({ duration: duration * LABEL_SHARE, easing }), this.legend
151
- ? this.legend.enter({ duration: duration * LABEL_SHARE, easing })
152
- : hold(duration * LABEL_SHARE))), duration);
153
- }
154
- /**
155
- * Fade the hole's summary up.
156
- *
157
- * Held out of {@link enter} on purpose: what sits in a donut's hole is usually
158
- * the chart's conclusion, so the scene lands it on the beat it belongs to
159
- * rather than giving it away with the first frame. Pair it with
160
- * `showCenter: false`, which is what leaves the hole clear to begin with.
161
- */
162
- revealCenter(args = {}) {
163
- return this.ring.revealCenter({ duration: args.duration ?? 0.6, easing: args.easing ?? easeOut() });
164
- }
165
- // ---- Selection ---------------------------------------------------------
166
- /** True when slice `index` is currently grown out past the ring's rim. */
167
- isSelected(index) {
168
- return this.popAt(index) > 0.5;
169
- }
170
- /**
171
- * Grow slice `index` out past the ring's rim and dim the rest of the chart
172
- * behind it, leaving any other selected slice where it is.
173
- */
174
- selectSlice(args) {
175
- const { sliceIndex: index } = args.data;
176
- const duration = args.duration ?? 0.45;
177
- const easing = args.easing ?? easeOut();
178
- return this.tweenPops((i) => (i === index ? 1 : this.popAt(i)), duration, easing);
179
- }
180
- /** Settle slice `index` back flush with the ring. */
181
- deselectSlice(args) {
182
- const { sliceIndex: index } = args.data;
183
- const duration = args.duration ?? 0.45;
184
- const easing = args.easing ?? easeOut();
185
- return this.tweenPops((i) => (i === index ? 0 : this.popAt(i)), duration, easing);
186
- }
187
- /** Move the focus to slice `index` alone, settling whatever else was out. */
188
- selectOnlySlice(args) {
189
- const { sliceIndex: index } = args.data;
190
- const duration = args.duration ?? 0.45;
191
- const easing = args.easing ?? easeOut();
192
- return this.tweenPops((i) => (i === index ? 1 : 0), duration, easing);
193
- }
194
- /** Settle every slice flush and bring the whole chart back to full. */
195
- clearSelection(args = {}) {
196
- const duration = args.duration ?? 0.45;
197
- const easing = args.easing ?? easeOut();
198
- return this.tweenPops(() => 0, duration, easing);
199
- }
200
- /**
201
- * Ease every slice's pop from where it is now to what `pop` asks for.
202
- *
203
- * Consumes its full duration even when nothing moves: in an editor a
204
- * command's duration is what the timeline lays out and what its parallel
205
- * siblings are timed against, so a no-op that took no frames would pull
206
- * everything after it forward.
207
- *
208
- * `from` and the targets are read when the command is *built*, which for a
209
- * driven scene is with the node in the state its own start time implies — so a
210
- * select that follows another starts from where that one left the ring.
211
- */
212
- tweenPops(pop, duration, easing) {
213
- const from = this.data.map((_, i) => this.popAt(i));
214
- const targets = this.data.map((_, i) => clampUnit(pop(i)));
215
- if (from.every((value, i) => value === targets[i]))
216
- return this.command(() => ({}), duration);
217
- return this.command((t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }), duration, easing);
218
- }
219
- };
220
- __decorate([
221
- property({ default: () => [] })
222
- ], PieChart.prototype, "data", void 0);
223
- __decorate([
224
- property({ default: "" })
225
- ], PieChart.prototype, "categoryField", void 0);
226
- __decorate([
227
- property({ default: "" })
228
- ], PieChart.prototype, "valueField", void 0);
229
- __decorate([
230
- property({
231
- default: DEFAULT_PIE_CHART_THEME,
232
- mapper: resolvePieChartTheme,
233
- tween: lerpPieChartTheme,
234
- })
235
- ], PieChart.prototype, "theme", void 0);
236
- __decorate([
237
- property({ default: "percent" })
238
- ], PieChart.prototype, "labelMode", void 0);
239
- __decorate([
240
- property({ default: "leader" })
241
- ], PieChart.prototype, "labelPlacement", void 0);
242
- __decorate([
243
- property({ default: 40 })
244
- ], PieChart.prototype, "gap", void 0);
245
- __decorate([
246
- property({ default: "" })
247
- ], PieChart.prototype, "centerLabel", void 0);
248
- __decorate([
249
- property({ default: "" })
250
- ], PieChart.prototype, "centerCaption", void 0);
251
- __decorate([
252
- property({ default: () => [] })
253
- ], PieChart.prototype, "pops", void 0);
254
- __decorate([
255
- command()
256
- ], PieChart.prototype, "enter", null);
257
- __decorate([
258
- command()
259
- ], PieChart.prototype, "revealCenter", null);
260
- __decorate([
261
- command({ args: [{ key: "sliceIndex", kind: "number", default: 0, min: 0, step: 1 }] })
262
- ], PieChart.prototype, "selectSlice", null);
263
- __decorate([
264
- command({ args: [{ key: "sliceIndex", kind: "number", default: 0, min: 0, step: 1 }] })
265
- ], PieChart.prototype, "deselectSlice", null);
266
- __decorate([
267
- command({ args: [{ key: "sliceIndex", kind: "number", default: 0, min: 0, step: 1 }] })
268
- ], PieChart.prototype, "selectOnlySlice", null);
269
- __decorate([
270
- command()
271
- ], PieChart.prototype, "clearSelection", null);
272
- PieChart = __decorate([
273
- node({
274
- key: "pieChart",
275
- parentKey: "node",
276
- forkable: true,
277
- layout: {
278
- children: "freeform",
279
- defaultWidthMode: "fixed",
280
- defaultHeightMode: "fixed",
281
- acceptsChildren: true,
144
+ const row = new Rect({ flow: "horizontal", width: "fill", height: "fill", gap: state.gap, align: "center", children: parts });
145
+ return asParts([...state.children, row]);
146
+ },
147
+ commands: {
148
+ /**
149
+ * Animate the whole chart in: the wedges sweep in clockwise from the theme's
150
+ * `startAngle`, then the labels fade up and the legend staggers in together.
151
+ * Its first frame arms the wedges, the labels and the legend; the hole's
152
+ * summary keeps its own signal. `duration` is the **whole** entrance.
153
+ */
154
+ enter: {
155
+ args: {},
156
+ easing: easeOut(),
157
+ run: (timeline, _state, children) => {
158
+ const parts = partsOf(children);
159
+ if (!parts)
160
+ return timeline.wait(timeline.duration);
161
+ const { ring, legend } = parts;
162
+ const sweep = timeline.duration * SWEEP_SHARE;
163
+ const labels = timeline.duration * LABEL_SHARE;
164
+ const at0 = (piece) => ({ at: 0, duration: 0, piece });
165
+ return beats(timeline, [
166
+ at0(ring.to({ growth: 0, labelReveal: 0 })),
167
+ ...(legend ? [at0(legend.run("arm"))] : []),
168
+ { at: 0, duration: sweep, piece: ring.run("sweep") },
169
+ { at: sweep, duration: labels, piece: ring.run("revealLabels") },
170
+ ...(legend ? [{ at: sweep, duration: labels, piece: legend.run("enter") }] : []),
171
+ ]);
172
+ },
173
+ },
174
+ /**
175
+ * Fade the hole's summary up. Held out of `enter` on purpose: what sits in a
176
+ * donut's hole is usually the chart's conclusion, so the scene lands it on
177
+ * its own beat. Pair it with `showCenter: false`.
178
+ */
179
+ revealCenter: {
180
+ args: {},
181
+ easing: easeOut(),
182
+ run: (timeline, _state, children) => partsOf(children)?.ring.run("revealCenter") ?? timeline.wait(timeline.duration),
183
+ },
184
+ /** Grow slice `index` out past the ring's rim and dim the rest, leaving any other selected slice where it is. */
185
+ selectSlice: {
186
+ args: { sliceIndex: SLICE_ARG },
187
+ easing: easeOut(),
188
+ run: (timeline, state) => {
189
+ const index = timeline.args.sliceIndex;
190
+ return popping(timeline, state, (i) => (i === index ? 1 : popAt(state, i)));
191
+ },
192
+ },
193
+ /** Settle slice `index` back flush with the ring. */
194
+ deselectSlice: {
195
+ args: { sliceIndex: SLICE_ARG },
196
+ easing: easeOut(),
197
+ run: (timeline, state) => {
198
+ const index = timeline.args.sliceIndex;
199
+ return popping(timeline, state, (i) => (i === index ? 0 : popAt(state, i)));
200
+ },
201
+ },
202
+ /** Move the focus to slice `index` alone, settling whatever else was out. */
203
+ selectOnlySlice: {
204
+ args: { sliceIndex: SLICE_ARG },
205
+ easing: easeOut(),
206
+ run: (timeline, state) => {
207
+ const index = timeline.args.sliceIndex;
208
+ return popping(timeline, state, (i) => (i === index ? 1 : 0));
209
+ },
282
210
  },
283
- seed: {
284
- width: 680,
285
- height: 480,
211
+ /** Settle every slice flush and bring the whole chart back to full. */
212
+ clearSelection: {
213
+ args: {},
214
+ easing: easeOut(),
215
+ run: (timeline, state) => popping(timeline, state, () => 0),
286
216
  },
287
- })
288
- ], PieChart);
289
- export { PieChart };
217
+ },
218
+ });
290
219
  //# sourceMappingURL=pie-chart.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"pie-chart.js","sourceRoot":"","sources":["../../src/pie-chart/pie-chart.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,MAAM,EACN,OAAO,EACP,QAAQ,GAOT,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EACL,uBAAuB,EACvB,iBAAiB,EACjB,oBAAoB,EACpB,gBAAgB,GAMjB,MAAM,UAAU,CAAA;AACjB,OAAO,EACL,SAAS,EACT,IAAI,EACJ,QAAQ,IAAI,UAAU,EACtB,QAAQ,GACT,MAAM,6BAA6B,CAAA;AAmCpC;;;;;GAKG;AACH,MAAM,WAAW,GAAG,IAAI,CAAA;AACxB,MAAM,WAAW,GAAG,IAAI,CAAA;AAKxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAgBI,IAAM,QAAQ,GAAd,MAAM,QAAS,SAAQ,MAAqB;IAyBjD,0EAA0E;IAClE,IAAI,CAAS;IACb,MAAM,GAAqB,IAAI,CAAA;IAEvC,mFAAmF;IACnF,IAAY,aAAa;QACvB,OAAO,IAAI,CAAC,KAAkC,CAAA;IAChD,CAAC;IAED,uFAAuF;IACvF,IAAY,MAAM;QAChB,OAAO,gBAAgB,CACrB,IAAI,CAAC,IAAI,EACT,IAAI,CAAC,aAAa,EAClB,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,aAAa,CAAC,UAAU,CAC9B,CAAA;IACH,CAAC;IAED;;;;;OAKG;IACK,SAAS;Q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AE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;2CAQvF;AAID;IADC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;6CAQvF;AAID;IADC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;+CAQvF;AAID;IADC,OAAO,EAAE;8CAOT;AApOU,QAAQ;IAfpB,IAAI,CAAC;QACJ,GAAG,EAAE,UAAU;QACf,SAAS,EAAE,MAAM;QACjB,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE;YACN,QAAQ,EAAE,UAAU;YACpB,gBAAgB,EAAE,OAAO;YACzB,iBAAiB,EAAE,OAAO;YAC1B,eAAe,EAAE,IAAI;SACtB;QACD,IAAI,EAAE;YACJ,KAAK,EAAE,GAAG;YACV,MAAM,EAAE,GAAG;SACZ;KACF,CAAC;GACW,QAAQ,CAiQpB","sourcesContent":["import {\r\n Rect,\r\n command,\r\n node,\r\n Node2D,\r\n easeOut,\r\n property,\r\n type Command,\r\n type CommandArgs,\r\n type DataRecord,\r\n type EasingFunction,\r\n type NodeConfig,\r\n type Node2DProps,\r\n} from \"@motionscript/sdk\"\r\n\r\nimport { clampUnit } from \"../kit/shared\"\r\nimport { PieLegend } from \"./pie-legend\"\r\nimport { PieRing } from \"./pie-ring\"\r\nimport {\r\n DEFAULT_PIE_CHART_THEME,\r\n lerpPieChartTheme,\r\n resolvePieChartTheme,\r\n resolvePieSlices,\r\n type PieChartConfig,\r\n type PieChartTheme,\r\n type PieLabelMode,\r\n type PieLabelPlacement,\r\n type PieSlice,\r\n} from \"./shared\"\r\nimport {\r\n asCommand,\r\n hold,\r\n sequence as sequenceOf,\r\n together,\r\n} from \"@motionscript/sdk/component\"\r\n\r\nexport interface PieChartProps extends Node2DProps {\r\n /** One row per slice — a plain record of field name to value. */\r\n data: DataRecord[]\r\n /** Key into each row for its slice caption, used on the labels and the legend. */\r\n categoryField: string\r\n /** Key into each row for its slice magnitude — the wedge's share of the ring. */\r\n valueField: string\r\n /**\r\n * The chart's visual theme — slice palette, ring geometry, label and centre\r\n * type, legend. Every branch is optional and deep-merged onto\r\n * {@link DEFAULT_PIE_CHART_THEME}.\r\n */\r\n theme?: PieChartTheme\r\n /** What the ring's labels say. */\r\n labelMode?: PieLabelMode\r\n /** Where they sit: inside their wedge, outside on a leader, or nowhere. */\r\n labelPlacement?: PieLabelPlacement\r\n /** Whether the legend column beside the ring is drawn at all. */\r\n showLegend?: boolean\r\n /** Gap between the ring and the legend, in px. */\r\n gap?: number\r\n /** Text set in the ring's hole — see {@link revealCenter}. */\r\n centerLabel?: string\r\n /** Smaller second line under {@link centerLabel}. */\r\n centerCaption?: string\r\n /**\r\n * Whether the hole's summary is drawn at rest. `true` (the default) is the\r\n * plain answer; a scene that wants the conclusion to land on a later beat sets\r\n * this `false` and calls {@link revealCenter} when it gets there.\r\n */\r\n showCenter?: boolean\r\n}\r\n\r\n/**\r\n * How {@link PieChart.enter} divides its duration: the wedges sweeping in, then\r\n * the labels and the legend arriving together. Shares of the whole, so they sum\r\n * to 1 — the entrance takes exactly the time it was given, for the reason spelt\r\n * out on `LineChart.enter`.\r\n */\r\nconst SWEEP_SHARE = 0.65\r\nconst LABEL_SHARE = 0.35\r\n\r\n/** What the selection commands move: the chart's own {@link PieChart.pops}. */\r\ntype SlicePops = { pops: number[] }\r\n\r\n/**\r\n * A pie or donut chart, composed from two independent parts rather than one\r\n * monolithic draw — the third of the chart family, built the same way as the\r\n * line and bar charts beside it:\r\n *\r\n * - {@link PieRing} — the wedges, each sized by its share of the total, with its\r\n * label set inside its own band or pinned outside on a leader line, and the\r\n * summary in the hole. The only part of the family that draws a path.\r\n * - {@link PieLegend} — a column of swatch-and-caption rows naming each slice.\r\n *\r\n * The two are laid out by a plain horizontal-flow {@link Rect}: the ring fills whatever the\r\n * hugging legend leaves, both centred, so the chart re-flows if its box changes.\r\n *\r\n * **One mark per row, not per column** — which is what makes this the odd one of\r\n * the three. A line or a bar chart plots *series*, so its colours belong to\r\n * columns and hiding one is a sensible thing to ask for. A pie plots rows, so its\r\n * palette is indexed by row and there is nothing to hide: dropping a slice would\r\n * silently re-cut every other wedge's share, which is a different chart rather\r\n * than the same chart with a series switched off. {@link selectSlice} and its\r\n * siblings are what a pie has instead — a slice swells out of the ring, its hole\r\n * edge staying put, and everything else dims behind it.\r\n *\r\n * **Resting state is the finished chart** — the same bargain the other two make:\r\n * an editor paints a node's Initial state on a paused canvas, so a chart that\r\n * only appeared once a command had run would be edited blind. {@link enter} arms\r\n * every part back to its pre-entrance state before animating. The one exception\r\n * is the hole's summary, which a scene can hold back with `showCenter: false`\r\n * because it is usually the chart's *conclusion* rather than part of its setup.\r\n */\r\n@node({\r\n key: \"pieChart\",\r\n parentKey: \"node\",\r\n forkable: true,\r\n layout: {\r\n children: \"freeform\",\r\n defaultWidthMode: \"fixed\",\r\n defaultHeightMode: \"fixed\",\r\n acceptsChildren: true,\r\n },\r\n seed: {\r\n width: 680,\r\n height: 480,\r\n },\r\n})\r\nexport class PieChart extends Node2D<PieChartProps> {\r\n @property({ default: () => [] as DataRecord[] }) declare data: DataRecord[]\r\n @property({ default: \"\" }) declare categoryField: string\r\n @property({ default: \"\" }) declare valueField: string\r\n @property({\r\n default: DEFAULT_PIE_CHART_THEME,\r\n mapper: resolvePieChartTheme,\r\n tween: lerpPieChartTheme,\r\n })\r\n declare theme: PieChartTheme\r\n @property({ default: \"percent\" }) declare labelMode: PieLabelMode\r\n @property({ default: \"leader\" }) declare labelPlacement: PieLabelPlacement\r\n @property({ default: 40 }) declare gap: number\r\n @property({ default: \"\" }) declare centerLabel: string\r\n @property({ default: \"\" }) declare centerCaption: string\r\n\r\n /**\r\n * How far each slice is currently pushed out of the ring, `0`–`1` per index.\r\n *\r\n * Held here rather than on the ring because two parts read it: the wedges grow\r\n * out by it, and the legend entries dim by it. One signal, one tween, and the\r\n * two can't drift.\r\n */\r\n @property({ default: () => [] as number[] }) declare pops: number[]\r\n\r\n // ---- Part handles (built in the constructor) --------------------------\r\n private ring: PieRing\r\n private legend: PieLegend | null = null\r\n\r\n /** `this.theme` read back through its *resolved* shape — see `LineChart.theme`. */\r\n private get resolvedTheme(): PieChartConfig {\r\n return this.theme as unknown as PieChartConfig\r\n }\r\n\r\n /** The slices the ring and the legend draw, resolved from the rows and the palette. */\r\n private get slices(): PieSlice[] {\r\n return resolvePieSlices(\r\n this.data,\r\n this.categoryField,\r\n this.valueField,\r\n this.resolvedTheme.sliceFills\r\n )\r\n }\r\n\r\n /**\r\n * How far each slice is dimmed by the current selection, one entry per slice —\r\n * 1 while nothing is selected, and for the selected slice itself; falling\r\n * toward the theme's `dimOpacity` for the rest in proportion to how far the\r\n * selection has grown.\r\n */\r\n private sliceDims(): number[] {\r\n let focus = 0\r\n for (let i = 0; i < this.data.length; i++) {\r\n focus = Math.max(focus, this.popAt(i))\r\n }\r\n const dim = this.resolvedTheme.spotlight.dimOpacity\r\n if (focus <= 0) return this.data.map(() => 1)\r\n return this.data.map(\r\n (_, i) => 1 - (1 - clampUnit(dim)) * focus * (1 - this.popAt(i))\r\n )\r\n }\r\n\r\n /** How far slice `index` is currently pushed out, in `[0, 1]`. */\r\n private popAt(index: number): number {\r\n return clampUnit(this.pops[index] ?? 0)\r\n }\r\n\r\n constructor(props?: NodeConfig<PieChart, PieChartProps>) {\r\n super(props)\r\n\r\n // The whole chart is composed from plain props/`data` — no context needed,\r\n // so the parts are built here (super() applied the props above). The slice\r\n // *structure* (one label and one legend row per row of `data`) is read once,\r\n // like every other chart's series list; everything the parts draw with,\r\n // including the slices themselves and the paint they take from the palette,\r\n // is passed as a closure, so `chart.to({ theme })` genuinely re-paints the\r\n // ring and the swatches and a `set` of `data` re-cuts the wedges.\r\n const slices = this.slices\r\n const showLegend = props?.showLegend !== false\r\n this.pops = slices.map(() => 0)\r\n\r\n this.ring = new PieRing({\r\n width: \"fill\",\r\n height: \"fill\",\r\n slices: () => this.slices,\r\n ring: () => this.resolvedTheme.ring,\r\n label: () => this.resolvedTheme.label,\r\n centerStyle: () => this.resolvedTheme.center,\r\n labelMode: this.labelMode,\r\n labelPlacement: this.labelPlacement,\r\n pops: () => this.pops,\r\n dimOpacity: () => this.resolvedTheme.spotlight.dimOpacity,\r\n centerLabel: this.centerLabel,\r\n centerCaption: this.centerCaption,\r\n centerReveal: props?.showCenter === false ? 0 : 1,\r\n })\r\n\r\n const children: Node2D[] = [this.ring]\r\n if (showLegend) {\r\n this.legend = new PieLegend({\r\n slices: () => this.slices,\r\n legend: () => this.resolvedTheme.legend,\r\n dims: () => this.sliceDims(),\r\n })\r\n children.push(this.legend)\r\n }\r\n\r\n this.add(\r\n new Rect({ flow: \"horizontal\",\r\n width: \"fill\",\r\n height: \"fill\",\r\n gap: this.gap,\r\n align: \"center\",\r\n children,\r\n })\r\n )\r\n }\r\n\r\n // ---- Orchestrated entrance --------------------------------------------\r\n\r\n /**\r\n * Prime every part at its pre-entrance state — no wedges swept, no labels, the\r\n * legend shifted and transparent.\r\n *\r\n * Called by {@link enter} before its first frame so nothing flashes ahead of\r\n * its beat. Idempotent, and public so a scene can hide a chart ahead of time\r\n * without starting the entrance. The hole's summary is deliberately untouched:\r\n * it has its own signal, so a scene that set it up to land later keeps that.\r\n */\r\n arm(): void {\r\n this.ring.set({ growth: 0, labelReveal: 0 })\r\n this.legend?.arm()\r\n }\r\n\r\n /**\r\n * Animate the whole chart in: the wedges sweep in clockwise from the theme's\r\n * `startAngle`, then the labels fade up and the legend staggers in together.\r\n *\r\n * `duration` is the **whole** entrance; the phases are shares of it.\r\n */\r\n @command()\r\n enter(\r\n args: CommandArgs<Record<string, never>> & { duration: number }\r\n ): Command<Record<string, never>> {\r\n const duration = args.duration\r\n const easing = args.easing ?? easeOut()\r\n this.arm()\r\n\r\n return asCommand(\r\n sequenceOf(\r\n this.ring.sweep({ duration: duration * SWEEP_SHARE, easing }),\r\n together(\r\n this.ring.revealLabels({ duration: duration * LABEL_SHARE, easing }),\r\n this.legend\r\n ? this.legend.enter({ duration: duration * LABEL_SHARE, easing })\r\n : hold(duration * LABEL_SHARE)\r\n )\r\n ),\r\n duration\r\n )\r\n }\r\n\r\n /**\r\n * Fade the hole's summary up.\r\n *\r\n * Held out of {@link enter} on purpose: what sits in a donut's hole is usually\r\n * the chart's conclusion, so the scene lands it on the beat it belongs to\r\n * rather than giving it away with the first frame. Pair it with\r\n * `showCenter: false`, which is what leaves the hole clear to begin with.\r\n */\r\n @command()\r\n revealCenter(\r\n args: CommandArgs<Record<string, never>> & { duration?: number } = {}\r\n ): Command<{ centerReveal: number }> {\r\n return this.ring.revealCenter({ duration: args.duration ?? 0.6, easing: args.easing ?? easeOut() })\r\n }\r\n\r\n // ---- Selection ---------------------------------------------------------\r\n\r\n /** True when slice `index` is currently grown out past the ring's rim. */\r\n isSelected(index: number): boolean {\r\n return this.popAt(index) > 0.5\r\n }\r\n\r\n /**\r\n * Grow slice `index` out past the ring's rim and dim the rest of the chart\r\n * behind it, leaving any other selected slice where it is.\r\n */\r\n @command({ args: [{ key: \"sliceIndex\", kind: \"number\", default: 0, min: 0, step: 1 }] })\r\n selectSlice(\r\n args: CommandArgs<{ sliceIndex: number }> & { duration?: number }\r\n ): Command<SlicePops> {\r\n const { sliceIndex: index } = args.data as { sliceIndex: number }\r\n const duration = args.duration ?? 0.45\r\n const easing = args.easing ?? easeOut()\r\n return this.tweenPops((i) => (i === index ? 1 : this.popAt(i)), duration, easing)\r\n }\r\n\r\n /** Settle slice `index` back flush with the ring. */\r\n @command({ args: [{ key: \"sliceIndex\", kind: \"number\", default: 0, min: 0, step: 1 }] })\r\n deselectSlice(\r\n args: CommandArgs<{ sliceIndex: number }> & { duration?: number }\r\n ): Command<SlicePops> {\r\n const { sliceIndex: index } = args.data as { sliceIndex: number }\r\n const duration = args.duration ?? 0.45\r\n const easing = args.easing ?? easeOut()\r\n return this.tweenPops((i) => (i === index ? 0 : this.popAt(i)), duration, easing)\r\n }\r\n\r\n /** Move the focus to slice `index` alone, settling whatever else was out. */\r\n @command({ args: [{ key: \"sliceIndex\", kind: \"number\", default: 0, min: 0, step: 1 }] })\r\n selectOnlySlice(\r\n args: CommandArgs<{ sliceIndex: number }> & { duration?: number }\r\n ): Command<SlicePops> {\r\n const { sliceIndex: index } = args.data as { sliceIndex: number }\r\n const duration = args.duration ?? 0.45\r\n const easing = args.easing ?? easeOut()\r\n return this.tweenPops((i) => (i === index ? 1 : 0), duration, easing)\r\n }\r\n\r\n /** Settle every slice flush and bring the whole chart back to full. */\r\n @command()\r\n clearSelection(\r\n args: CommandArgs<Record<string, never>> & { duration?: number } = {}\r\n ): Command<SlicePops> {\r\n const duration = args.duration ?? 0.45\r\n const easing = args.easing ?? easeOut()\r\n return this.tweenPops(() => 0, duration, easing)\r\n }\r\n\r\n /**\r\n * Ease every slice's pop from where it is now to what `pop` asks for.\r\n *\r\n * Consumes its full duration even when nothing moves: in an editor a\r\n * command's duration is what the timeline lays out and what its parallel\r\n * siblings are timed against, so a no-op that took no frames would pull\r\n * everything after it forward.\r\n *\r\n * `from` and the targets are read when the command is *built*, which for a\r\n * driven scene is with the node in the state its own start time implies — so a\r\n * select that follows another starts from where that one left the ring.\r\n */\r\n private tweenPops(\r\n pop: (index: number) => number,\r\n duration: number,\r\n easing: EasingFunction\r\n ): Command<SlicePops> {\r\n const from = this.data.map((_, i) => this.popAt(i))\r\n const targets = this.data.map((_, i) => clampUnit(pop(i)))\r\n if (from.every((value, i) => value === targets[i])) return this.command<SlicePops>(() => ({}), duration)\r\n\r\n return this.command<SlicePops>(\r\n (t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }),\r\n duration,\r\n easing\r\n )\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"pie-chart.js","sourceRoot":"","sources":["../../src/pie-chart/pie-chart.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,KAAK,EACL,WAAW,EACX,OAAO,GAQR,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,KAAK,EAAa,MAAM,eAAe,CAAA;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACpC,OAAO,EACL,uBAAuB,EACvB,iBAAiB,EACjB,oBAAoB,EACpB,gBAAgB,GAMjB,MAAM,UAAU,CAAA;AA4CjB;;;GAGG;AACH,MAAM,WAAW,GAAG,IAAI,CAAA;AACxB,MAAM,WAAW,GAAG,IAAI,CAAA;AAExB,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAA;AAEtD,uFAAuF;AACvF,SAAS,QAAQ,CAAC,CAAiB;IACjC,OAAO,gBAAgB,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,aAAa,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;AACpF,CAAC;AAED,wDAAwD;AACxD,SAAS,KAAK,CAAC,CAAiB,EAAE,KAAa;IAC7C,OAAO,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;AACtC,CAAC;AAED,sHAAsH;AACtH,MAAM,UAAU,eAAe,CAAC,CAAiB,EAAE,KAAa;IAC9D,OAAO,KAAK,CA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,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;YAC7E,CAAC;SACF;QACD,6EAA6E;QAC7E,eAAe,EAAE;YACf,IAAI,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE;YAC/B,MAAM,EAAE,OAAO,EAAE;YACjB,GAAG,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE;gBACvB,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAA;gBACtC,OAAO,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;YAC/D,CAAC;SACF;QACD,uEAAuE;QACvE,cAAc,EAAE;YACd,IAAI,EAAE,EAAE;YACR,MAAM,EAAE,OAAO,EAAE;YACjB,GAAG,EAAE,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;SAC5D;KACF;CACF,CAAC,CAAA","sourcesContent":["import {\r\n Rect,\r\n Types,\r\n component2d,\r\n easeOut,\r\n type ChildView,\r\n type DataRecord,\r\n type NodeOf,\r\n // Named so this component's emitted type reaches `Node2D` through the sdk, not core.\r\n type Node2D,\r\n type Node2DProps,\r\n type Timeline,\r\n} from \"@motionscript/sdk\"\r\n\r\nimport { asParts, fresh } from \"../kit/entries\"\r\nimport { beats, type Beat } from \"../kit/motion\"\r\nimport { clampUnit } from \"../kit/shared\"\r\nimport { PieLegend } from \"./pie-legend\"\r\nimport { PieRing } from \"./pie-ring\"\r\nimport {\r\n DEFAULT_PIE_CHART_THEME,\r\n lerpPieChartTheme,\r\n resolvePieChartTheme,\r\n resolvePieSlices,\r\n type PieChartConfig,\r\n type PieChartTheme,\r\n type PieLabelMode,\r\n type PieLabelPlacement,\r\n type PieSlice,\r\n} from \"./shared\"\r\n\r\nexport interface PieChartProps extends Node2DProps {\r\n /** One row per slice — a plain record of field name to value. */\r\n data: DataRecord[]\r\n /** Key into each row for its slice caption, used on the labels and the legend. */\r\n categoryField: string\r\n /** Key into each row for its slice magnitude — the wedge's share of the ring. */\r\n valueField: string\r\n /**\r\n * The chart's visual theme — slice palette, ring geometry, label and centre\r\n * type, legend. Every branch is optional and deep-merged onto\r\n * {@link DEFAULT_PIE_CHART_THEME}.\r\n */\r\n theme?: PieChartTheme\r\n /** What the ring's labels say. */\r\n labelMode?: PieLabelMode\r\n /** Where they sit: inside their wedge, outside on a leader, or nowhere. */\r\n labelPlacement?: PieLabelPlacement\r\n /** Whether the legend column beside the ring is drawn at all. */\r\n showLegend?: boolean\r\n /** Gap between the ring and the legend, in px. */\r\n gap?: number\r\n /** Text set in the ring's hole — see `revealCenter`. */\r\n centerLabel?: string\r\n /** Smaller second line under {@link centerLabel}. */\r\n centerCaption?: string\r\n /**\r\n * Whether the hole's summary is drawn at rest. `true` (the default) is the\r\n * plain answer; a scene that wants the conclusion to land on a later beat sets\r\n * this `false` and runs `revealCenter` when it gets there.\r\n */\r\n showCenter?: boolean\r\n}\r\n\r\n/** What the selection reads: the rows, how they are cut, and the pops the commands move. */\r\nexport interface PieChartValues {\r\n readonly data: DataRecord[]\r\n readonly categoryField: string\r\n readonly valueField: string\r\n readonly theme: PieChartConfig\r\n readonly pops: number[]\r\n}\r\n\r\n/**\r\n * How `enter` divides its duration: the wedges sweeping in, then the labels and\r\n * the legend arriving together. Shares of the whole, so they sum to 1.\r\n */\r\nconst SWEEP_SHARE = 0.65\r\nconst LABEL_SHARE = 0.35\r\n\r\nconst SLICE_ARG = Types.number(0, { min: 0, step: 1 })\r\n\r\n/** The slices the ring and the legend draw, resolved from the rows and the palette. */\r\nfunction slicesOf(s: PieChartValues): PieSlice[] {\r\n return resolvePieSlices(s.data, s.categoryField, s.valueField, s.theme.sliceFills)\r\n}\r\n\r\n/** How far slice `index` is pushed out, in `[0, 1]`. */\r\nfunction popAt(s: PieChartValues, index: number): number {\r\n return clampUnit(s.pops[index] ?? 0)\r\n}\r\n\r\n/** Whether slice `index` is grown out past the ring's rim: past half way, so a slice mid-swell is not yet counted. */\r\nexport function isSliceSelected(s: PieChartValues, index: number): boolean {\r\n return popAt(s, index) > 0.5\r\n}\r\n\r\n/**\r\n * How far each slice is dimmed by the selection, one entry per slice: 1 while\r\n * nothing is out, and for the selected slice itself; falling toward the theme's\r\n * `dimOpacity` for the rest in proportion to how far the selection has grown.\r\n */\r\nfunction sliceDims(s: PieChartValues): number[] {\r\n let focus = 0\r\n for (let i = 0; i < s.data.length; i++) focus = Math.max(focus, popAt(s, i))\r\n const dim = s.theme.spotlight.dimOpacity\r\n if (focus <= 0) return s.data.map(() => 1)\r\n return s.data.map((_, i) => 1 - (1 - clampUnit(dim)) * focus * (1 - popAt(s, i)))\r\n}\r\n\r\ntype AnyTimeline = Timeline<any, any>\r\n\r\n/**\r\n * Every slice's pop eased from where it is to what `pop` asks for. A move that\r\n * changes nothing still takes its slot, writing nothing.\r\n */\r\nfunction popping(timeline: AnyTimeline, s: PieChartValues, pop: (index: number) => number) {\r\n const from = s.data.map((_, i) => popAt(s, i))\r\n const targets = s.data.map((_, i) => clampUnit(pop(i)))\r\n if (from.every((value, i) => value === targets[i])) return timeline.wait(timeline.duration)\r\n return timeline.animate((t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }))\r\n}\r\n\r\n/** The ring and the legend, found through the chart's children: the last row of them is the one it built. */\r\nfunction partsOf(children: readonly ChildView[]) {\r\n for (let i = children.length - 1; i >= 0; i--) {\r\n const row = children[i]\r\n const ring = Rect.is(row) ? row.children.find(PieRing.is) : undefined\r\n if (ring) return { ring, legend: row.children.find(PieLegend.is) ?? null }\r\n }\r\n return null\r\n}\r\n\r\n/**\r\n * A pie or donut chart, composed from two parts laid out by a horizontal-flow\r\n * {@link Rect} — the ring filling whatever the hugging legend leaves, both\r\n * centred:\r\n *\r\n * - {@link PieRing} — the wedges, each sized by its share of the total, with its\r\n * label inside its own band or pinned outside on a leader line, and the\r\n * summary in the hole.\r\n * - {@link PieLegend} — a column of swatch-and-caption rows naming each slice.\r\n *\r\n * **One mark per row, not per column**: a pie's palette is indexed by row and\r\n * there is nothing to hide, since dropping a slice would re-cut every other\r\n * wedge. `selectSlice` and its siblings are what a pie has instead — a slice\r\n * swells out of the ring and everything else dims behind it.\r\n *\r\n * **Resting state is the finished chart**; `enter` arms every part on its first\r\n * frame. The one exception is the hole's summary, which a scene can hold back\r\n * with `showCenter: false`, because it is usually the chart's *conclusion*.\r\n */\r\nexport const PieChart = component2d({\r\n key: \"pieChart\",\r\n state: {\r\n data: fresh<DataRecord[]>(() => []),\r\n categoryField: \"\",\r\n valueField: \"\",\r\n theme: Types.custom<PieChartTheme, PieChartConfig>(DEFAULT_PIE_CHART_THEME, { resolve: resolvePieChartTheme, lerp: lerpPieChartTheme }),\r\n labelMode: Types.custom<PieLabelMode>(\"percent\"),\r\n labelPlacement: Types.custom<PieLabelPlacement>(\"leader\"),\r\n gap: 40,\r\n centerLabel: \"\",\r\n centerCaption: \"\",\r\n /**\r\n * How far each slice is pushed out of the ring, `0`–`1` per index. Held here\r\n * rather than on the ring because two parts read it: the wedges grow out by\r\n * it, and the legend entries dim by it. Seeded flush, one to a row.\r\n */\r\n pops: fresh<number[]>(() => []),\r\n /** Read as the chart is built: the legend is a part the chart builds or doesn't. */\r\n showLegend: Types.boolean(true, { animatable: false }),\r\n /** Read as the chart is built: whether the hole's summary is drawn at rest. */\r\n showCenter: Types.boolean(true, { animatable: false }),\r\n },\r\n defaults: {\r\n // Its parts fill it; a node made of them would hug them.\r\n width: \"fill\",\r\n height: \"fill\",\r\n pops: (state) => slicesOf(state).map(() => 0),\r\n },\r\n // The slice *structure* (one label and one legend row per row) is read once;\r\n // the slices themselves, their paint and the selection are bound, so a theme\r\n // tween re-paints and a `set` of `data` re-cuts the wedges.\r\n children: (state) => {\r\n const theme = () => state.theme\r\n const slices = () => slicesOf(state)\r\n const parts: Node2D[] = [\r\n new PieRing({\r\n width: \"fill\",\r\n height: \"fill\",\r\n slices,\r\n ring: () => theme().ring,\r\n label: () => theme().label,\r\n centerStyle: () => theme().center,\r\n labelMode: state.labelMode,\r\n labelPlacement: state.labelPlacement,\r\n pops: () => state.pops,\r\n dimOpacity: () => theme().spotlight.dimOpacity,\r\n centerLabel: state.centerLabel,\r\n centerCaption: state.centerCaption,\r\n centerReveal: state.showCenter === false ? 0 : 1,\r\n }) as unknown as Node2D,\r\n ]\r\n if (state.showLegend !== false) {\r\n parts.push(\r\n new PieLegend({\r\n slices,\r\n legend: () => theme().legend,\r\n dims: () => sliceDims(state),\r\n // The column's spacing is the theme's as the chart was built, like the\r\n // entries' type and chips.\r\n gap: state.theme.legend.itemGap / 3,\r\n padding: state.theme.legend.padding,\r\n }) as unknown as Node2D\r\n )\r\n }\r\n const row = new Rect({ flow: \"horizontal\", width: \"fill\", height: \"fill\", gap: state.gap, align: \"center\", children: parts })\r\n return asParts([...(state.children as unknown as Node2D[]), row as unknown as Node2D])\r\n },\r\n commands: {\r\n /**\r\n * Animate the whole chart in: the wedges sweep in clockwise from the theme's\r\n * `startAngle`, then the labels fade up and the legend staggers in together.\r\n * Its first frame arms the wedges, the labels and the legend; the hole's\r\n * summary keeps its own signal. `duration` is the **whole** entrance.\r\n */\r\n enter: {\r\n args: {},\r\n easing: easeOut(),\r\n run: (timeline, _state, children) => {\r\n const parts = partsOf(children)\r\n if (!parts) return timeline.wait(timeline.duration)\r\n const { ring, legend } = parts\r\n const sweep = timeline.duration * SWEEP_SHARE\r\n const labels = timeline.duration * LABEL_SHARE\r\n const at0 = (piece: Beat[\"piece\"]): Beat => ({ at: 0, duration: 0, piece })\r\n return beats(timeline, [\r\n at0(ring.to({ growth: 0, labelReveal: 0 })),\r\n ...(legend ? [at0(legend.run(\"arm\"))] : []),\r\n { at: 0, duration: sweep, piece: ring.run(\"sweep\") },\r\n { at: sweep, duration: labels, piece: ring.run(\"revealLabels\") },\r\n ...(legend ? [{ at: sweep, duration: labels, piece: legend.run(\"enter\") }] : []),\r\n ])\r\n },\r\n },\r\n /**\r\n * Fade the hole's summary up. Held out of `enter` on purpose: what sits in a\r\n * donut's hole is usually the chart's conclusion, so the scene lands it on\r\n * its own beat. Pair it with `showCenter: false`.\r\n */\r\n revealCenter: {\r\n args: {},\r\n easing: easeOut(),\r\n run: (timeline, _state, children) => partsOf(children)?.ring.run(\"revealCenter\") ?? timeline.wait(timeline.duration),\r\n },\r\n /** Grow slice `index` out past the ring's rim and dim the rest, leaving any other selected slice where it is. */\r\n selectSlice: {\r\n args: { sliceIndex: SLICE_ARG },\r\n easing: easeOut(),\r\n run: (timeline, state) => {\r\n const index = timeline.args.sliceIndex\r\n return popping(timeline, state, (i) => (i === index ? 1 : popAt(state, i)))\r\n },\r\n },\r\n /** Settle slice `index` back flush with the ring. */\r\n deselectSlice: {\r\n args: { sliceIndex: SLICE_ARG },\r\n easing: easeOut(),\r\n run: (timeline, state) => {\r\n const index = timeline.args.sliceIndex\r\n return popping(timeline, state, (i) => (i === index ? 0 : popAt(state, i)))\r\n },\r\n },\r\n /** Move the focus to slice `index` alone, settling whatever else was out. */\r\n selectOnlySlice: {\r\n args: { sliceIndex: SLICE_ARG },\r\n easing: easeOut(),\r\n run: (timeline, state) => {\r\n const index = timeline.args.sliceIndex\r\n return popping(timeline, state, (i) => (i === index ? 1 : 0))\r\n },\r\n },\r\n /** Settle every slice flush and bring the whole chart back to full. */\r\n clearSelection: {\r\n args: {},\r\n easing: easeOut(),\r\n run: (timeline, state) => popping(timeline, state, () => 0),\r\n },\r\n },\r\n})\r\n\r\nexport type PieChart = NodeOf<typeof PieChart>\r\n"]}