@motionscript/charts 0.1.0-alpha.0 → 0.1.0-alpha.10
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 +105 -0
- package/NOTICE +49 -0
- package/README.md +99 -0
- package/dist/bar-chart/bar-chart.d.ts +133 -228
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +386 -565
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +35 -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 +50 -70
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +155 -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 +7 -7
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +7 -7
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-TJNHLQGV.js +2 -0
- package/dist/browser/chunks/chunk-TJNHLQGV.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 +4 -3
- package/dist/engine.d.ts +10 -8
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +10 -8
- package/dist/engine.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 +10 -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 +32 -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 +123 -85
- 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 +61 -10
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +108 -13
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +48 -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 +60 -77
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +100 -121
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +32 -33
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +31 -65
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +58 -64
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +94 -123
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +3 -3
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +2 -2
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +156 -303
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +410 -638
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +7 -7
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +23 -8
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +4 -11
- package/dist/line-chart/x-scale.d.ts.map +1 -1
- package/dist/line-chart/x-scale.js +16 -5
- 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 +108 -126
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +202 -271
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +84 -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 +160 -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 +12 -6
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +14 -8
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +7 -4
- package/src/bar-chart/bar-chart.ts +424 -686
- package/src/bar-chart/category-axis.ts +96 -111
- package/src/bar-chart/chart-bars.ts +178 -201
- package/src/bar-chart/index.ts +2 -2
- package/src/bar-chart/shared.ts +7 -7
- package/src/engine.ts +10 -8
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +23 -34
- package/src/kit/chart-canvas.ts +45 -80
- package/src/kit/chart-legend.ts +185 -206
- 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 +120 -13
- package/src/kit/vertical-axis.ts +111 -141
- package/src/line-chart/chart-line.ts +103 -155
- package/src/line-chart/chart-region.ts +36 -81
- package/src/line-chart/horizontal-axis.ts +102 -153
- package/src/line-chart/index.ts +3 -3
- package/src/line-chart/line-chart.ts +463 -759
- package/src/line-chart/shared.ts +22 -8
- package/src/line-chart/x-scale.ts +20 -5
- package/src/nodes.ts +8 -10
- package/src/pie-chart/index.ts +2 -2
- package/src/pie-chart/pie-chart.ts +225 -303
- package/src/pie-chart/pie-legend.ts +125 -115
- package/src/pie-chart/pie-ring.ts +388 -443
- package/src/pie-chart/shared.ts +14 -8
- package/dist/browser/chunks/chunk-ZMBOBHYL.js +0 -2
- package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +0 -7
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Rect,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Node2D,
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
6
5
|
easeOut,
|
|
7
|
-
|
|
8
|
-
type CommandArgs,
|
|
6
|
+
type ChildView,
|
|
9
7
|
type DataRecord,
|
|
10
|
-
type
|
|
11
|
-
type
|
|
8
|
+
type NodeOf,
|
|
9
|
+
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
10
|
+
type Node2D,
|
|
12
11
|
type Node2DProps,
|
|
13
|
-
|
|
12
|
+
type Timeline,
|
|
13
|
+
} from "@motionscript/sdk"
|
|
14
14
|
|
|
15
|
+
import { asParts, fresh } from "../kit/entries"
|
|
16
|
+
import { beats, type Beat } from "../kit/motion"
|
|
15
17
|
import { clampUnit } from "../kit/shared"
|
|
16
18
|
import { PieLegend } from "./pie-legend"
|
|
17
19
|
import { PieRing } from "./pie-ring"
|
|
@@ -26,13 +28,6 @@ import {
|
|
|
26
28
|
type PieLabelPlacement,
|
|
27
29
|
type PieSlice,
|
|
28
30
|
} from "./shared"
|
|
29
|
-
import {
|
|
30
|
-
drive,
|
|
31
|
-
hold,
|
|
32
|
-
sequence as sequenceOf,
|
|
33
|
-
together,
|
|
34
|
-
type Seekable,
|
|
35
|
-
} from "@motionscript/core/component"
|
|
36
31
|
|
|
37
32
|
export interface PieChartProps extends Node2DProps {
|
|
38
33
|
/** One row per slice — a plain record of field name to value. */
|
|
@@ -55,320 +50,247 @@ export interface PieChartProps extends Node2DProps {
|
|
|
55
50
|
showLegend?: boolean
|
|
56
51
|
/** Gap between the ring and the legend, in px. */
|
|
57
52
|
gap?: number
|
|
58
|
-
/** Text set in the ring's hole — see
|
|
53
|
+
/** Text set in the ring's hole — see `revealCenter`. */
|
|
59
54
|
centerLabel?: string
|
|
60
55
|
/** Smaller second line under {@link centerLabel}. */
|
|
61
56
|
centerCaption?: string
|
|
62
57
|
/**
|
|
63
58
|
* Whether the hole's summary is drawn at rest. `true` (the default) is the
|
|
64
59
|
* plain answer; a scene that wants the conclusion to land on a later beat sets
|
|
65
|
-
* this `false` and
|
|
60
|
+
* this `false` and runs `revealCenter` when it gets there.
|
|
66
61
|
*/
|
|
67
62
|
showCenter?: boolean
|
|
68
63
|
}
|
|
69
64
|
|
|
65
|
+
/** What the selection reads: the rows, how they are cut, and the pops the commands move. */
|
|
66
|
+
export interface PieChartValues {
|
|
67
|
+
readonly data: DataRecord[]
|
|
68
|
+
readonly categoryField: string
|
|
69
|
+
readonly valueField: string
|
|
70
|
+
readonly theme: PieChartConfig
|
|
71
|
+
readonly pops: number[]
|
|
72
|
+
}
|
|
73
|
+
|
|
70
74
|
/**
|
|
71
|
-
* How
|
|
72
|
-
* the
|
|
73
|
-
* to 1 — the entrance takes exactly the time it was given, for the reason spelt
|
|
74
|
-
* out on `LineChart.enter`.
|
|
75
|
+
* How `enter` divides its duration: the wedges sweeping in, then the labels and
|
|
76
|
+
* the legend arriving together. Shares of the whole, so they sum to 1.
|
|
75
77
|
*/
|
|
76
78
|
const SWEEP_SHARE = 0.65
|
|
77
79
|
const LABEL_SHARE = 0.35
|
|
78
80
|
|
|
81
|
+
const SLICE_ARG = Types.number(0, { min: 0, step: 1 })
|
|
82
|
+
|
|
83
|
+
/** The slices the ring and the legend draw, resolved from the rows and the palette. */
|
|
84
|
+
function slicesOf(s: PieChartValues): PieSlice[] {
|
|
85
|
+
return resolvePieSlices(s.data, s.categoryField, s.valueField, s.theme.sliceFills)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** How far slice `index` is pushed out, in `[0, 1]`. */
|
|
89
|
+
function popAt(s: PieChartValues, index: number): number {
|
|
90
|
+
return clampUnit(s.pops[index] ?? 0)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Whether slice `index` is grown out past the ring's rim: past half way, so a slice mid-swell is not yet counted. */
|
|
94
|
+
export function isSliceSelected(s: PieChartValues, index: number): boolean {
|
|
95
|
+
return popAt(s, index) > 0.5
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* How far each slice is dimmed by the selection, one entry per slice: 1 while
|
|
100
|
+
* nothing is out, and for the selected slice itself; falling toward the theme's
|
|
101
|
+
* `dimOpacity` for the rest in proportion to how far the selection has grown.
|
|
102
|
+
*/
|
|
103
|
+
function sliceDims(s: PieChartValues): number[] {
|
|
104
|
+
let focus = 0
|
|
105
|
+
for (let i = 0; i < s.data.length; i++) focus = Math.max(focus, popAt(s, i))
|
|
106
|
+
const dim = s.theme.spotlight.dimOpacity
|
|
107
|
+
if (focus <= 0) return s.data.map(() => 1)
|
|
108
|
+
return s.data.map((_, i) => 1 - (1 - clampUnit(dim)) * focus * (1 - popAt(s, i)))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type AnyTimeline = Timeline<any, any>
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Every slice's pop eased from where it is to what `pop` asks for. A move that
|
|
115
|
+
* changes nothing still takes its slot, writing nothing.
|
|
116
|
+
*/
|
|
117
|
+
function popping(timeline: AnyTimeline, s: PieChartValues, pop: (index: number) => number) {
|
|
118
|
+
const from = s.data.map((_, i) => popAt(s, i))
|
|
119
|
+
const targets = s.data.map((_, i) => clampUnit(pop(i)))
|
|
120
|
+
if (from.every((value, i) => value === targets[i])) return timeline.wait(timeline.duration)
|
|
121
|
+
return timeline.animate((t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The ring and the legend, found through the chart's children: the last row of them is the one it built. */
|
|
125
|
+
function partsOf(children: readonly ChildView[]) {
|
|
126
|
+
for (let i = children.length - 1; i >= 0; i--) {
|
|
127
|
+
const row = children[i]
|
|
128
|
+
const ring = Rect.is(row) ? row.children.find(PieRing.is) : undefined
|
|
129
|
+
if (ring) return { ring, legend: row.children.find(PieLegend.is) ?? null }
|
|
130
|
+
}
|
|
131
|
+
return null
|
|
132
|
+
}
|
|
133
|
+
|
|
79
134
|
/**
|
|
80
|
-
* A pie or donut chart, composed from two
|
|
81
|
-
*
|
|
82
|
-
*
|
|
135
|
+
* A pie or donut chart, composed from two parts laid out by a horizontal-flow
|
|
136
|
+
* {@link Rect} — the ring filling whatever the hugging legend leaves, both
|
|
137
|
+
* centred:
|
|
83
138
|
*
|
|
84
139
|
* - {@link PieRing} — the wedges, each sized by its share of the total, with its
|
|
85
|
-
* label
|
|
86
|
-
* summary in the hole.
|
|
140
|
+
* label inside its own band or pinned outside on a leader line, and the
|
|
141
|
+
* summary in the hole.
|
|
87
142
|
* - {@link PieLegend} — a column of swatch-and-caption rows naming each slice.
|
|
88
143
|
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* the three. A line or a bar chart plots *series*, so its colours belong to
|
|
94
|
-
* columns and hiding one is a sensible thing to ask for. A pie plots rows, so its
|
|
95
|
-
* palette is indexed by row and there is nothing to hide: dropping a slice would
|
|
96
|
-
* silently re-cut every other wedge's share, which is a different chart rather
|
|
97
|
-
* than the same chart with a series switched off. {@link selectSlice} and its
|
|
98
|
-
* siblings are what a pie has instead — a slice swells out of the ring, its hole
|
|
99
|
-
* edge staying put, and everything else dims behind it.
|
|
144
|
+
* **One mark per row, not per column**: a pie's palette is indexed by row and
|
|
145
|
+
* there is nothing to hide, since dropping a slice would re-cut every other
|
|
146
|
+
* wedge. `selectSlice` and its siblings are what a pie has instead — a slice
|
|
147
|
+
* swells out of the ring and everything else dims behind it.
|
|
100
148
|
*
|
|
101
|
-
* **Resting state is the finished chart
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* every part back to its pre-entrance state before animating. The one exception
|
|
105
|
-
* is the hole's summary, which a scene can hold back with `showCenter: false`
|
|
106
|
-
* because it is usually the chart's *conclusion* rather than part of its setup.
|
|
149
|
+
* **Resting state is the finished chart**; `enter` arms every part on its first
|
|
150
|
+
* frame. The one exception is the hole's summary, which a scene can hold back
|
|
151
|
+
* with `showCenter: false`, because it is usually the chart's *conclusion*.
|
|
107
152
|
*/
|
|
108
|
-
|
|
153
|
+
export const PieChart = component2d({
|
|
109
154
|
key: "pieChart",
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
155
|
+
state: {
|
|
156
|
+
data: fresh<DataRecord[]>(() => []),
|
|
157
|
+
categoryField: "",
|
|
158
|
+
valueField: "",
|
|
159
|
+
theme: Types.custom<PieChartTheme, PieChartConfig>(DEFAULT_PIE_CHART_THEME, { resolve: resolvePieChartTheme, lerp: lerpPieChartTheme }),
|
|
160
|
+
labelMode: Types.custom<PieLabelMode>("percent"),
|
|
161
|
+
labelPlacement: Types.custom<PieLabelPlacement>("leader"),
|
|
162
|
+
gap: 40,
|
|
163
|
+
centerLabel: "",
|
|
164
|
+
centerCaption: "",
|
|
165
|
+
/**
|
|
166
|
+
* How far each slice is pushed out of the ring, `0`–`1` per index. Held here
|
|
167
|
+
* rather than on the ring because two parts read it: the wedges grow out by
|
|
168
|
+
* it, and the legend entries dim by it. Seeded flush, one to a row.
|
|
169
|
+
*/
|
|
170
|
+
pops: fresh<number[]>(() => []),
|
|
171
|
+
/** Read as the chart is built: the legend is a part the chart builds or doesn't. */
|
|
172
|
+
showLegend: Types.boolean(true, { animatable: false }),
|
|
173
|
+
/** Read as the chart is built: whether the hole's summary is drawn at rest. */
|
|
174
|
+
showCenter: Types.boolean(true, { animatable: false }),
|
|
117
175
|
},
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
176
|
+
defaults: {
|
|
177
|
+
// Its parts fill it; a node made of them would hug them.
|
|
178
|
+
width: "fill",
|
|
179
|
+
height: "fill",
|
|
180
|
+
pops: (state) => slicesOf(state).map(() => 0),
|
|
121
181
|
},
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
})
|
|
132
|
-
declare theme: PieChartTheme
|
|
133
|
-
@property({ default: "percent" }) declare labelMode: PieLabelMode
|
|
134
|
-
@property({ default: "leader" }) declare labelPlacement: PieLabelPlacement
|
|
135
|
-
@property({ default: 40 }) declare gap: number
|
|
136
|
-
@property({ default: "" }) declare centerLabel: string
|
|
137
|
-
@property({ default: "" }) declare centerCaption: string
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* How far each slice is currently pushed out of the ring, `0`–`1` per index.
|
|
141
|
-
*
|
|
142
|
-
* Held here rather than on the ring because two parts read it: the wedges grow
|
|
143
|
-
* out by it, and the legend entries dim by it. One signal, one tween, and the
|
|
144
|
-
* two can't drift.
|
|
145
|
-
*/
|
|
146
|
-
@property({ default: () => [] as number[] }) declare pops: number[]
|
|
147
|
-
|
|
148
|
-
// ---- Part handles (built in the constructor) --------------------------
|
|
149
|
-
private ring: PieRing
|
|
150
|
-
private legend: PieLegend | null = null
|
|
151
|
-
|
|
152
|
-
/** `this.theme` read back through its *resolved* shape — see `LineChart.theme`. */
|
|
153
|
-
private get resolvedTheme(): PieChartConfig {
|
|
154
|
-
return this.theme as unknown as PieChartConfig
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** The slices the ring and the legend draw, resolved from the rows and the palette. */
|
|
158
|
-
private get slices(): PieSlice[] {
|
|
159
|
-
return resolvePieSlices(
|
|
160
|
-
this.data,
|
|
161
|
-
this.categoryField,
|
|
162
|
-
this.valueField,
|
|
163
|
-
this.resolvedTheme.sliceFills
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* How far each slice is dimmed by the current selection, one entry per slice —
|
|
169
|
-
* 1 while nothing is selected, and for the selected slice itself; falling
|
|
170
|
-
* toward the theme's `dimOpacity` for the rest in proportion to how far the
|
|
171
|
-
* selection has grown.
|
|
172
|
-
*/
|
|
173
|
-
private sliceDims(): number[] {
|
|
174
|
-
let focus = 0
|
|
175
|
-
for (let i = 0; i < this.data.length; i++) {
|
|
176
|
-
focus = Math.max(focus, this.popAt(i))
|
|
177
|
-
}
|
|
178
|
-
const dim = this.resolvedTheme.spotlight.dimOpacity
|
|
179
|
-
if (focus <= 0) return this.data.map(() => 1)
|
|
180
|
-
return this.data.map(
|
|
181
|
-
(_, i) => 1 - (1 - clampUnit(dim)) * focus * (1 - this.popAt(i))
|
|
182
|
-
)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/** How far slice `index` is currently pushed out, in `[0, 1]`. */
|
|
186
|
-
private popAt(index: number): number {
|
|
187
|
-
return clampUnit(this.pops[index] ?? 0)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
constructor(props?: NodeConfig<PieChart, PieChartProps>) {
|
|
191
|
-
super(props)
|
|
192
|
-
|
|
193
|
-
// The whole chart is composed from plain props/`data` — no context needed,
|
|
194
|
-
// so the parts are built here (super() applied the props above). The slice
|
|
195
|
-
// *structure* (one wedge and one legend row each) is read once, like every
|
|
196
|
-
// other chart's series list; everything the parts draw with is passed as a
|
|
197
|
-
// closure, so `chart.to({ theme })` genuinely re-paints the ring.
|
|
198
|
-
const slices = this.slices
|
|
199
|
-
const showLegend = props?.showLegend !== false
|
|
200
|
-
this.pops = slices.map(() => 0)
|
|
201
|
-
|
|
202
|
-
this.ring = new PieRing({
|
|
203
|
-
width: "fill",
|
|
204
|
-
height: "fill",
|
|
205
|
-
slices,
|
|
206
|
-
ring: () => this.resolvedTheme.ring,
|
|
207
|
-
label: () => this.resolvedTheme.label,
|
|
208
|
-
centerStyle: () => this.resolvedTheme.center,
|
|
209
|
-
labelMode: this.labelMode,
|
|
210
|
-
labelPlacement: this.labelPlacement,
|
|
211
|
-
pops: () => this.pops,
|
|
212
|
-
dimOpacity: () => this.resolvedTheme.spotlight.dimOpacity,
|
|
213
|
-
centerLabel: this.centerLabel,
|
|
214
|
-
centerCaption: this.centerCaption,
|
|
215
|
-
centerReveal: props?.showCenter === false ? 0 : 1,
|
|
216
|
-
})
|
|
217
|
-
|
|
218
|
-
const children: Node2D[] = [this.ring]
|
|
219
|
-
if (showLegend) {
|
|
220
|
-
this.legend = new PieLegend({
|
|
221
|
-
slices,
|
|
222
|
-
legend: () => this.resolvedTheme.legend,
|
|
223
|
-
dims: () => this.sliceDims(),
|
|
224
|
-
})
|
|
225
|
-
children.push(this.legend)
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
this.add(
|
|
229
|
-
new Rect({ flow: "horizontal",
|
|
182
|
+
allowedChildren: [],
|
|
183
|
+
// The slice *structure* (one label and one legend row per row) is read once;
|
|
184
|
+
// the slices themselves, their paint and the selection are bound, so a theme
|
|
185
|
+
// tween re-paints and a `set` of `data` re-cuts the wedges.
|
|
186
|
+
children: (state) => {
|
|
187
|
+
const theme = () => state.theme
|
|
188
|
+
const slices = () => slicesOf(state)
|
|
189
|
+
const parts: Node2D[] = [
|
|
190
|
+
new PieRing({
|
|
230
191
|
width: "fill",
|
|
231
192
|
height: "fill",
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
* Animate the whole chart in: the wedges sweep in clockwise from the theme's
|
|
257
|
-
* `startAngle`, then the labels fade up and the legend staggers in together.
|
|
258
|
-
*
|
|
259
|
-
* `duration` is the **whole** entrance; the phases are shares of it.
|
|
260
|
-
*/
|
|
261
|
-
@command()
|
|
262
|
-
enter(
|
|
263
|
-
args: CommandArgs<Record<string, never>> & { duration: number }
|
|
264
|
-
): Seekable {
|
|
265
|
-
const duration = args.duration
|
|
266
|
-
const easing = args.easing ?? easeOut()
|
|
267
|
-
this.arm()
|
|
268
|
-
|
|
269
|
-
return sequenceOf(
|
|
270
|
-
this.ring.sweep({ duration: duration * SWEEP_SHARE, easing }),
|
|
271
|
-
together(
|
|
272
|
-
this.ring.revealLabels({ duration: duration * LABEL_SHARE, easing }),
|
|
273
|
-
this.legend
|
|
274
|
-
? this.legend.enter({ duration: duration * LABEL_SHARE, easing })
|
|
275
|
-
: hold(duration * LABEL_SHARE)
|
|
193
|
+
slices,
|
|
194
|
+
ring: () => theme().ring,
|
|
195
|
+
label: () => theme().label,
|
|
196
|
+
centerStyle: () => theme().center,
|
|
197
|
+
labelMode: state.labelMode,
|
|
198
|
+
labelPlacement: state.labelPlacement,
|
|
199
|
+
pops: () => state.pops,
|
|
200
|
+
dimOpacity: () => theme().spotlight.dimOpacity,
|
|
201
|
+
centerLabel: state.centerLabel,
|
|
202
|
+
centerCaption: state.centerCaption,
|
|
203
|
+
centerReveal: state.showCenter === false ? 0 : 1,
|
|
204
|
+
}) as unknown as Node2D,
|
|
205
|
+
]
|
|
206
|
+
if (state.showLegend !== false) {
|
|
207
|
+
parts.push(
|
|
208
|
+
new PieLegend({
|
|
209
|
+
slices,
|
|
210
|
+
legend: () => theme().legend,
|
|
211
|
+
dims: () => sliceDims(state),
|
|
212
|
+
// The column's spacing is the theme's as the chart was built, like the
|
|
213
|
+
// entries' type and chips.
|
|
214
|
+
gap: state.theme.legend.itemGap / 3,
|
|
215
|
+
padding: state.theme.legend.padding,
|
|
216
|
+
}) as unknown as Node2D
|
|
276
217
|
)
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
* everything after it forward.
|
|
355
|
-
*
|
|
356
|
-
* `from` and the targets are read when the command is *built*, which for a
|
|
357
|
-
* driven scene is with the node in the state its own start time implies — so a
|
|
358
|
-
* select that follows another starts from where that one left the ring.
|
|
359
|
-
*/
|
|
360
|
-
private tweenPops(
|
|
361
|
-
pop: (index: number) => number,
|
|
362
|
-
duration: number,
|
|
363
|
-
easing: EasingFunction
|
|
364
|
-
): Seekable {
|
|
365
|
-
const from = this.data.map((_, i) => this.popAt(i))
|
|
366
|
-
const targets = this.data.map((_, i) => clampUnit(pop(i)))
|
|
367
|
-
if (from.every((value, i) => value === targets[i])) return hold(duration)
|
|
218
|
+
}
|
|
219
|
+
const row = new Rect({ flow: "horizontal", width: "fill", height: "fill", gap: state.gap, align: "center", children: parts })
|
|
220
|
+
return asParts([row as unknown as Node2D])
|
|
221
|
+
},
|
|
222
|
+
commands: {
|
|
223
|
+
/**
|
|
224
|
+
* Animate the whole chart in: the wedges sweep in clockwise from the theme's
|
|
225
|
+
* `startAngle`, then the labels fade up and the legend staggers in together.
|
|
226
|
+
* Its first frame arms the wedges, the labels and the legend; the hole's
|
|
227
|
+
* summary keeps its own signal. `duration` is the **whole** entrance.
|
|
228
|
+
*/
|
|
229
|
+
enter: {
|
|
230
|
+
args: {},
|
|
231
|
+
// Held from the chart's arrival, so it never shows finished ahead of its draw.
|
|
232
|
+
entrance: true,
|
|
233
|
+
easing: easeOut(),
|
|
234
|
+
run: (timeline, _state, children) => {
|
|
235
|
+
const parts = partsOf(children)
|
|
236
|
+
if (!parts) return timeline.wait(timeline.duration)
|
|
237
|
+
const { ring, legend } = parts
|
|
238
|
+
const sweep = timeline.duration * SWEEP_SHARE
|
|
239
|
+
const labels = timeline.duration * LABEL_SHARE
|
|
240
|
+
const at0 = (piece: Beat["piece"]): Beat => ({ at: 0, duration: 0, piece })
|
|
241
|
+
return beats(timeline, [
|
|
242
|
+
at0(ring.to({ growth: 0, labelReveal: 0 })),
|
|
243
|
+
...(legend ? [at0(legend.run("arm"))] : []),
|
|
244
|
+
{ at: 0, duration: sweep, piece: ring.run("sweep") },
|
|
245
|
+
{ at: sweep, duration: labels, piece: ring.run("revealLabels") },
|
|
246
|
+
...(legend ? [{ at: sweep, duration: labels, piece: legend.run("enter") }] : []),
|
|
247
|
+
])
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
/**
|
|
251
|
+
* Fade the hole's summary up. Held out of `enter` on purpose: what sits in a
|
|
252
|
+
* donut's hole is usually the chart's conclusion, so the scene lands it on
|
|
253
|
+
* its own beat. Pair it with `showCenter: false`.
|
|
254
|
+
*/
|
|
255
|
+
revealCenter: {
|
|
256
|
+
args: {},
|
|
257
|
+
easing: easeOut(),
|
|
258
|
+
run: (timeline, _state, children) => partsOf(children)?.ring.run("revealCenter") ?? timeline.wait(timeline.duration),
|
|
259
|
+
},
|
|
260
|
+
/** Grow slice `index` out past the ring's rim and dim the rest, leaving any other selected slice where it is. */
|
|
261
|
+
selectSlice: {
|
|
262
|
+
args: { sliceIndex: SLICE_ARG },
|
|
263
|
+
easing: easeOut(),
|
|
264
|
+
run: (timeline, state) => {
|
|
265
|
+
const index = timeline.args.sliceIndex
|
|
266
|
+
return popping(timeline, state, (i) => (i === index ? 1 : popAt(state, i)))
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
/** Settle slice `index` back flush with the ring. */
|
|
270
|
+
deselectSlice: {
|
|
271
|
+
args: { sliceIndex: SLICE_ARG },
|
|
272
|
+
easing: easeOut(),
|
|
273
|
+
run: (timeline, state) => {
|
|
274
|
+
const index = timeline.args.sliceIndex
|
|
275
|
+
return popping(timeline, state, (i) => (i === index ? 0 : popAt(state, i)))
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
/** Move the focus to slice `index` alone, settling whatever else was out. */
|
|
279
|
+
selectOnlySlice: {
|
|
280
|
+
args: { sliceIndex: SLICE_ARG },
|
|
281
|
+
easing: easeOut(),
|
|
282
|
+
run: (timeline, state) => {
|
|
283
|
+
const index = timeline.args.sliceIndex
|
|
284
|
+
return popping(timeline, state, (i) => (i === index ? 1 : 0))
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
/** Settle every slice flush and bring the whole chart back to full. */
|
|
288
|
+
clearSelection: {
|
|
289
|
+
args: {},
|
|
290
|
+
easing: easeOut(),
|
|
291
|
+
run: (timeline, state) => popping(timeline, state, () => 0),
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
})
|
|
368
295
|
|
|
369
|
-
|
|
370
|
-
const e = easing(t)
|
|
371
|
-
this.pops = from.map((value, i) => value + (targets[i] - value) * e)
|
|
372
|
-
})
|
|
373
|
-
}
|
|
374
|
-
}
|
|
296
|
+
export type PieChart = NodeOf<typeof PieChart>
|