@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
|
@@ -1,288 +1,219 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
15
|
-
* the
|
|
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
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
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
|
|
28
|
-
* summary in the hole.
|
|
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
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
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
|
|
44
|
-
*
|
|
45
|
-
*
|
|
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
|
-
|
|
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
|
-
centerCaption: this.centerCaption,
|
|
105
|
-
centerReveal: props?.showCenter === false ? 0 : 1,
|
|
106
|
-
});
|
|
107
|
-
const children = [this.ring];
|
|
108
|
-
if (showLegend) {
|
|
109
|
-
this.legend = new PieLegend({
|
|
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({
|
|
110
135
|
slices,
|
|
111
|
-
legend: () =>
|
|
112
|
-
dims: () =>
|
|
113
|
-
|
|
114
|
-
|
|
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
|
+
}));
|
|
115
143
|
}
|
|
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
|
-
const easing = args.easing ?? easeOut();
|
|
183
|
-
return this.tweenPops((i) => (i === index ? 0 : this.popAt(i)), duration, easing);
|
|
184
|
-
}
|
|
185
|
-
/** Move the focus to slice `index` alone, settling whatever else was out. */
|
|
186
|
-
selectOnlySlice(args) {
|
|
187
|
-
const { sliceIndex: index } = args.data;
|
|
188
|
-
const duration = args.duration ?? 0.45;
|
|
189
|
-
const easing = args.easing ?? easeOut();
|
|
190
|
-
return this.tweenPops((i) => (i === index ? 1 : 0), duration, easing);
|
|
191
|
-
}
|
|
192
|
-
/** Settle every slice flush and bring the whole chart back to full. */
|
|
193
|
-
clearSelection(args = {}) {
|
|
194
|
-
const duration = args.duration ?? 0.45;
|
|
195
|
-
const easing = args.easing ?? easeOut();
|
|
196
|
-
return this.tweenPops(() => 0, duration, easing);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Ease every slice's pop from where it is now to what `pop` asks for.
|
|
200
|
-
*
|
|
201
|
-
* Consumes its full duration even when nothing moves: in the studio a
|
|
202
|
-
* command's duration is what the timeline lays out and what its parallel
|
|
203
|
-
* siblings are timed against, so a no-op that took no frames would pull
|
|
204
|
-
* everything after it forward.
|
|
205
|
-
*
|
|
206
|
-
* `from` and the targets are read when the command is *built*, which for a
|
|
207
|
-
* driven scene is with the node in the state its own start time implies — so a
|
|
208
|
-
* select that follows another starts from where that one left the ring.
|
|
209
|
-
*/
|
|
210
|
-
tweenPops(pop, duration, easing) {
|
|
211
|
-
const from = this.data.map((_, i) => this.popAt(i));
|
|
212
|
-
const targets = this.data.map((_, i) => clampUnit(pop(i)));
|
|
213
|
-
if (from.every((value, i) => value === targets[i]))
|
|
214
|
-
return this.command(() => ({}), duration);
|
|
215
|
-
return this.command((t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }), duration, easing);
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
__decorate([
|
|
219
|
-
property({ default: () => [] })
|
|
220
|
-
], PieChart.prototype, "data", void 0);
|
|
221
|
-
__decorate([
|
|
222
|
-
property({ default: "" })
|
|
223
|
-
], PieChart.prototype, "categoryField", void 0);
|
|
224
|
-
__decorate([
|
|
225
|
-
property({ default: "" })
|
|
226
|
-
], PieChart.prototype, "valueField", void 0);
|
|
227
|
-
__decorate([
|
|
228
|
-
property({
|
|
229
|
-
default: DEFAULT_PIE_CHART_THEME,
|
|
230
|
-
mapper: resolvePieChartTheme,
|
|
231
|
-
tween: lerpPieChartTheme,
|
|
232
|
-
})
|
|
233
|
-
], PieChart.prototype, "theme", void 0);
|
|
234
|
-
__decorate([
|
|
235
|
-
property({ default: "percent" })
|
|
236
|
-
], PieChart.prototype, "labelMode", void 0);
|
|
237
|
-
__decorate([
|
|
238
|
-
property({ default: "leader" })
|
|
239
|
-
], PieChart.prototype, "labelPlacement", void 0);
|
|
240
|
-
__decorate([
|
|
241
|
-
property({ default: 40 })
|
|
242
|
-
], PieChart.prototype, "gap", void 0);
|
|
243
|
-
__decorate([
|
|
244
|
-
property({ default: "" })
|
|
245
|
-
], PieChart.prototype, "centerLabel", void 0);
|
|
246
|
-
__decorate([
|
|
247
|
-
property({ default: "" })
|
|
248
|
-
], PieChart.prototype, "centerCaption", void 0);
|
|
249
|
-
__decorate([
|
|
250
|
-
property({ default: () => [] })
|
|
251
|
-
], PieChart.prototype, "pops", void 0);
|
|
252
|
-
__decorate([
|
|
253
|
-
command()
|
|
254
|
-
], PieChart.prototype, "enter", null);
|
|
255
|
-
__decorate([
|
|
256
|
-
command()
|
|
257
|
-
], PieChart.prototype, "revealCenter", null);
|
|
258
|
-
__decorate([
|
|
259
|
-
command({ args: [{ key: "sliceIndex", kind: "number", default: 0, min: 0, step: 1 }] })
|
|
260
|
-
], PieChart.prototype, "selectSlice", null);
|
|
261
|
-
__decorate([
|
|
262
|
-
command({ args: [{ key: "sliceIndex", kind: "number", default: 0, min: 0, step: 1 }] })
|
|
263
|
-
], PieChart.prototype, "deselectSlice", null);
|
|
264
|
-
__decorate([
|
|
265
|
-
command({ args: [{ key: "sliceIndex", kind: "number", default: 0, min: 0, step: 1 }] })
|
|
266
|
-
], PieChart.prototype, "selectOnlySlice", null);
|
|
267
|
-
__decorate([
|
|
268
|
-
command()
|
|
269
|
-
], PieChart.prototype, "clearSelection", null);
|
|
270
|
-
PieChart = __decorate([
|
|
271
|
-
node({
|
|
272
|
-
key: "pieChart",
|
|
273
|
-
parentKey: "node",
|
|
274
|
-
forkable: true,
|
|
275
|
-
layout: {
|
|
276
|
-
children: "freeform",
|
|
277
|
-
defaultWidthMode: "fixed",
|
|
278
|
-
defaultHeightMode: "fixed",
|
|
279
|
-
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
|
+
},
|
|
280
210
|
},
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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),
|
|
284
216
|
},
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export { PieChart };
|
|
217
|
+
},
|
|
218
|
+
});
|
|
288
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;A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AID;IADC,OAAO,EAAE;8CAOT;AAlOU,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,CA+PpB","sourcesContent":["import {\n Rect,\n command,\n node,\n Node2D,\n easeOut,\n property,\n type Command,\n type CommandArgs,\n type DataRecord,\n type EasingFunction,\n type NodeConfig,\n type Node2DProps,\n} from \"@motionscript/sdk\"\n\nimport { clampUnit } from \"../kit/shared\"\nimport { PieLegend } from \"./pie-legend\"\nimport { PieRing } from \"./pie-ring\"\nimport {\n DEFAULT_PIE_CHART_THEME,\n lerpPieChartTheme,\n resolvePieChartTheme,\n resolvePieSlices,\n type PieChartConfig,\n type PieChartTheme,\n type PieLabelMode,\n type PieLabelPlacement,\n type PieSlice,\n} from \"./shared\"\nimport {\n asCommand,\n hold,\n sequence as sequenceOf,\n together,\n} from \"@motionscript/sdk/component\"\n\nexport interface PieChartProps extends Node2DProps {\n /** One row per slice — a plain record of field name to value. */\n data: DataRecord[]\n /** Key into each row for its slice caption, used on the labels and the legend. */\n categoryField: string\n /** Key into each row for its slice magnitude — the wedge's share of the ring. */\n valueField: string\n /**\n * The chart's visual theme — slice palette, ring geometry, label and centre\n * type, legend. Every branch is optional and deep-merged onto\n * {@link DEFAULT_PIE_CHART_THEME}.\n */\n theme?: PieChartTheme\n /** What the ring's labels say. */\n labelMode?: PieLabelMode\n /** Where they sit: inside their wedge, outside on a leader, or nowhere. */\n labelPlacement?: PieLabelPlacement\n /** Whether the legend column beside the ring is drawn at all. */\n showLegend?: boolean\n /** Gap between the ring and the legend, in px. */\n gap?: number\n /** Text set in the ring's hole — see {@link revealCenter}. */\n centerLabel?: string\n /** Smaller second line under {@link centerLabel}. */\n centerCaption?: string\n /**\n * Whether the hole's summary is drawn at rest. `true` (the default) is the\n * plain answer; a scene that wants the conclusion to land on a later beat sets\n * this `false` and calls {@link revealCenter} when it gets there.\n */\n showCenter?: boolean\n}\n\n/**\n * How {@link PieChart.enter} divides its duration: the wedges sweeping in, then\n * the labels and the legend arriving together. Shares of the whole, so they sum\n * to 1 — the entrance takes exactly the time it was given, for the reason spelt\n * out on `LineChart.enter`.\n */\nconst SWEEP_SHARE = 0.65\nconst LABEL_SHARE = 0.35\n\n/** What the selection commands move: the chart's own {@link PieChart.pops}. */\ntype SlicePops = { pops: number[] }\n\n/**\n * A pie or donut chart, composed from two independent parts rather than one\n * monolithic draw — the third of the chart family, built the same way as the\n * line and bar charts beside it:\n *\n * - {@link PieRing} — the wedges, each sized by its share of the total, with its\n * label set inside its own band or pinned outside on a leader line, and the\n * summary in the hole. The only part of the family that draws a path.\n * - {@link PieLegend} — a column of swatch-and-caption rows naming each slice.\n *\n * The two are laid out by a plain horizontal-flow {@link Rect}: the ring fills whatever the\n * hugging legend leaves, both centred, so the chart re-flows if its box changes.\n *\n * **One mark per row, not per column** — which is what makes this the odd one of\n * the three. A line or a bar chart plots *series*, so its colours belong to\n * columns and hiding one is a sensible thing to ask for. A pie plots rows, so its\n * palette is indexed by row and there is nothing to hide: dropping a slice would\n * silently re-cut every other wedge's share, which is a different chart rather\n * than the same chart with a series switched off. {@link selectSlice} and its\n * siblings are what a pie has instead — a slice swells out of the ring, its hole\n * edge staying put, and everything else dims behind it.\n *\n * **Resting state is the finished chart** — the same bargain the other two make:\n * the studio paints a node's Initial state on a paused canvas, so a chart that\n * only appeared once a command had run would be edited blind. {@link enter} arms\n * every part back to its pre-entrance state before animating. The one exception\n * is the hole's summary, which a scene can hold back with `showCenter: false`\n * because it is usually the chart's *conclusion* rather than part of its setup.\n */\n@node({\n key: \"pieChart\",\n parentKey: \"node\",\n forkable: true,\n layout: {\n children: \"freeform\",\n defaultWidthMode: \"fixed\",\n defaultHeightMode: \"fixed\",\n acceptsChildren: true,\n },\n seed: {\n width: 680,\n height: 480,\n },\n})\nexport class PieChart extends Node2D<PieChartProps> {\n @property({ default: () => [] as DataRecord[] }) declare data: DataRecord[]\n @property({ default: \"\" }) declare categoryField: string\n @property({ default: \"\" }) declare valueField: string\n @property({\n default: DEFAULT_PIE_CHART_THEME,\n mapper: resolvePieChartTheme,\n tween: lerpPieChartTheme,\n })\n declare theme: PieChartTheme\n @property({ default: \"percent\" }) declare labelMode: PieLabelMode\n @property({ default: \"leader\" }) declare labelPlacement: PieLabelPlacement\n @property({ default: 40 }) declare gap: number\n @property({ default: \"\" }) declare centerLabel: string\n @property({ default: \"\" }) declare centerCaption: string\n\n /**\n * How far each slice is currently pushed out of the ring, `0`–`1` per index.\n *\n * Held here rather than on the ring because two parts read it: the wedges grow\n * out by it, and the legend entries dim by it. One signal, one tween, and the\n * two can't drift.\n */\n @property({ default: () => [] as number[] }) declare pops: number[]\n\n // ---- Part handles (built in the constructor) --------------------------\n private ring: PieRing\n private legend: PieLegend | null = null\n\n /** `this.theme` read back through its *resolved* shape — see `LineChart.theme`. */\n private get resolvedTheme(): PieChartConfig {\n return this.theme as unknown as PieChartConfig\n }\n\n /** The slices the ring and the legend draw, resolved from the rows and the palette. */\n private get slices(): PieSlice[] {\n return resolvePieSlices(\n this.data,\n this.categoryField,\n this.valueField,\n this.resolvedTheme.sliceFills\n )\n }\n\n /**\n * How far each slice is dimmed by the current selection, one entry per slice —\n * 1 while nothing is selected, and for the selected slice itself; falling\n * toward the theme's `dimOpacity` for the rest in proportion to how far the\n * selection has grown.\n */\n private sliceDims(): number[] {\n let focus = 0\n for (let i = 0; i < this.data.length; i++) {\n focus = Math.max(focus, this.popAt(i))\n }\n const dim = this.resolvedTheme.spotlight.dimOpacity\n if (focus <= 0) return this.data.map(() => 1)\n return this.data.map(\n (_, i) => 1 - (1 - clampUnit(dim)) * focus * (1 - this.popAt(i))\n )\n }\n\n /** How far slice `index` is currently pushed out, in `[0, 1]`. */\n private popAt(index: number): number {\n return clampUnit(this.pops[index] ?? 0)\n }\n\n constructor(props?: NodeConfig<PieChart, PieChartProps>) {\n super(props)\n\n // The whole chart is composed from plain props/`data` — no context needed,\n // so the parts are built here (super() applied the props above). The slice\n // *structure* (one wedge and one legend row each) is read once, like every\n // other chart's series list; everything the parts draw with is passed as a\n // closure, so `chart.to({ theme })` genuinely re-paints the ring.\n const slices = this.slices\n const showLegend = props?.showLegend !== false\n this.pops = slices.map(() => 0)\n\n this.ring = new PieRing({\n width: \"fill\",\n height: \"fill\",\n slices,\n ring: () => this.resolvedTheme.ring,\n label: () => this.resolvedTheme.label,\n centerStyle: () => this.resolvedTheme.center,\n labelMode: this.labelMode,\n labelPlacement: this.labelPlacement,\n pops: () => this.pops,\n dimOpacity: () => this.resolvedTheme.spotlight.dimOpacity,\n centerLabel: this.centerLabel,\n centerCaption: this.centerCaption,\n centerReveal: props?.showCenter === false ? 0 : 1,\n })\n\n const children: Node2D[] = [this.ring]\n if (showLegend) {\n this.legend = new PieLegend({\n slices,\n legend: () => this.resolvedTheme.legend,\n dims: () => this.sliceDims(),\n })\n children.push(this.legend)\n }\n\n this.add(\n new Rect({ flow: \"horizontal\",\n width: \"fill\",\n height: \"fill\",\n gap: this.gap,\n align: \"center\",\n children,\n })\n )\n }\n\n // ---- Orchestrated entrance --------------------------------------------\n\n /**\n * Prime every part at its pre-entrance state — no wedges swept, no labels, the\n * legend shifted and transparent.\n *\n * Called by {@link enter} before its first frame so nothing flashes ahead of\n * its beat. Idempotent, and public so a scene can hide a chart ahead of time\n * without starting the entrance. The hole's summary is deliberately untouched:\n * it has its own signal, so a scene that set it up to land later keeps that.\n */\n arm(): void {\n this.ring.set({ growth: 0, labelReveal: 0 })\n this.legend?.arm()\n }\n\n /**\n * Animate the whole chart in: the wedges sweep in clockwise from the theme's\n * `startAngle`, then the labels fade up and the legend staggers in together.\n *\n * `duration` is the **whole** entrance; the phases are shares of it.\n */\n @command()\n enter(\n args: CommandArgs<Record<string, never>> & { duration: number }\n ): Command<Record<string, never>> {\n const duration = args.duration\n const easing = args.easing ?? easeOut()\n this.arm()\n\n return asCommand(\n sequenceOf(\n this.ring.sweep({ duration: duration * SWEEP_SHARE, easing }),\n together(\n this.ring.revealLabels({ duration: duration * LABEL_SHARE, easing }),\n this.legend\n ? this.legend.enter({ duration: duration * LABEL_SHARE, easing })\n : hold(duration * LABEL_SHARE)\n )\n ),\n duration\n )\n }\n\n /**\n * Fade the hole's summary up.\n *\n * Held out of {@link enter} on purpose: what sits in a donut's hole is usually\n * the chart's conclusion, so the scene lands it on the beat it belongs to\n * rather than giving it away with the first frame. Pair it with\n * `showCenter: false`, which is what leaves the hole clear to begin with.\n */\n @command()\n revealCenter(\n args: CommandArgs<Record<string, never>> & { duration?: number } = {}\n ): Command<{ centerReveal: number }> {\n return this.ring.revealCenter({ duration: args.duration ?? 0.6, easing: args.easing ?? easeOut() })\n }\n\n // ---- Selection ---------------------------------------------------------\n\n /** True when slice `index` is currently grown out past the ring's rim. */\n isSelected(index: number): boolean {\n return this.popAt(index) > 0.5\n }\n\n /**\n * Grow slice `index` out past the ring's rim and dim the rest of the chart\n * behind it, leaving any other selected slice where it is.\n */\n @command({ args: [{ key: \"sliceIndex\", kind: \"number\", default: 0, min: 0, step: 1 }] })\n selectSlice(\n args: CommandArgs<{ sliceIndex: number }> & { duration?: number }\n ): Command<SlicePops> {\n const { sliceIndex: index } = args.data as { sliceIndex: number }\n const duration = args.duration ?? 0.45\n const easing = args.easing ?? easeOut()\n return this.tweenPops((i) => (i === index ? 1 : this.popAt(i)), duration, easing)\n }\n\n /** Settle slice `index` back flush with the ring. */\n @command({ args: [{ key: \"sliceIndex\", kind: \"number\", default: 0, min: 0, step: 1 }] })\n deselectSlice(\n args: CommandArgs<{ sliceIndex: number }> & { duration?: number }\n ): Command<SlicePops> {\n const { sliceIndex: index } = args.data as { sliceIndex: number }\n const duration = args.duration ?? 0.45\n const easing = args.easing ?? easeOut()\n return this.tweenPops((i) => (i === index ? 0 : this.popAt(i)), duration, easing)\n }\n\n /** Move the focus to slice `index` alone, settling whatever else was out. */\n @command({ args: [{ key: \"sliceIndex\", kind: \"number\", default: 0, min: 0, step: 1 }] })\n selectOnlySlice(\n args: CommandArgs<{ sliceIndex: number }> & { duration?: number }\n ): Command<SlicePops> {\n const { sliceIndex: index } = args.data as { sliceIndex: number }\n const duration = args.duration ?? 0.45\n const easing = args.easing ?? easeOut()\n return this.tweenPops((i) => (i === index ? 1 : 0), duration, easing)\n }\n\n /** Settle every slice flush and bring the whole chart back to full. */\n @command()\n clearSelection(\n args: CommandArgs<Record<string, never>> & { duration?: number } = {}\n ): Command<SlicePops> {\n const duration = args.duration ?? 0.45\n const easing = args.easing ?? easeOut()\n return this.tweenPops(() => 0, duration, easing)\n }\n\n /**\n * Ease every slice's pop from where it is now to what `pop` asks for.\n *\n * Consumes its full duration even when nothing moves: in the studio a\n * command's duration is what the timeline lays out and what its parallel\n * siblings are timed against, so a no-op that took no frames would pull\n * everything after it forward.\n *\n * `from` and the targets are read when the command is *built*, which for a\n * driven scene is with the node in the state its own start time implies — so a\n * select that follows another starts from where that one left the ring.\n */\n private tweenPops(\n pop: (index: number) => number,\n duration: number,\n easing: EasingFunction\n ): Command<SlicePops> {\n const from = this.data.map((_, i) => this.popAt(i))\n const targets = this.data.map((_, i) => clampUnit(pop(i)))\n if (from.every((value, i) => value === targets[i])) return this.command<SlicePops>(() => ({}), duration)\n\n return this.command<SlicePops>(\n (t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }),\n duration,\n easing\n )\n }\n}\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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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"]}
|