@motionscript/charts 0.1.1-alpha.0 → 0.1.1-alpha.2

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 (207) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +109 -28
  3. package/dist/bar-chart/bar-chart.d.ts +265 -39
  4. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  5. package/dist/bar-chart/bar-chart.js +219 -171
  6. package/dist/bar-chart/bar-chart.js.map +1 -1
  7. package/dist/bar-chart/category-axis.d.ts +12 -7
  8. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  9. package/dist/bar-chart/category-axis.js +56 -42
  10. package/dist/bar-chart/category-axis.js.map +1 -1
  11. package/dist/bar-chart/chart-bars.d.ts +76 -52
  12. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  13. package/dist/bar-chart/chart-bars.js +105 -89
  14. package/dist/bar-chart/chart-bars.js.map +1 -1
  15. package/dist/bar-chart/index.d.ts +4 -4
  16. package/dist/bar-chart/index.d.ts.map +1 -1
  17. package/dist/bar-chart/index.js +2 -2
  18. package/dist/bar-chart/index.js.map +1 -1
  19. package/dist/bar-chart/shared.d.ts +41 -65
  20. package/dist/bar-chart/shared.d.ts.map +1 -1
  21. package/dist/bar-chart/shared.js +73 -117
  22. package/dist/bar-chart/shared.js.map +1 -1
  23. package/dist/browser/chunks/chunk-7XCRX6GE.js +2 -0
  24. package/dist/browser/chunks/chunk-7XCRX6GE.js.map +7 -0
  25. package/dist/browser/index.js +1 -1
  26. package/dist/browser/index.js.map +4 -4
  27. package/dist/browser/kit.js +1 -1
  28. package/dist/browser/manifest.json +1 -1
  29. package/dist/engine.d.ts +5 -2
  30. package/dist/engine.d.ts.map +1 -1
  31. package/dist/engine.js +5 -2
  32. package/dist/engine.js.map +1 -1
  33. package/dist/index.d.ts +2 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +2 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/kit/cartesian.d.ts +51 -15
  38. package/dist/kit/cartesian.d.ts.map +1 -1
  39. package/dist/kit/cartesian.js +81 -29
  40. package/dist/kit/cartesian.js.map +1 -1
  41. package/dist/kit/chart-canvas.d.ts +32 -24
  42. package/dist/kit/chart-canvas.d.ts.map +1 -1
  43. package/dist/kit/chart-canvas.js +52 -46
  44. package/dist/kit/chart-canvas.js.map +1 -1
  45. package/dist/kit/chart-legend.d.ts +8 -4
  46. package/dist/kit/chart-legend.d.ts.map +1 -1
  47. package/dist/kit/chart-legend.js +30 -20
  48. package/dist/kit/chart-legend.js.map +1 -1
  49. package/dist/{line-chart → kit}/horizontal-axis.d.ts +31 -6
  50. package/dist/kit/horizontal-axis.d.ts.map +1 -0
  51. package/dist/{line-chart → kit}/horizontal-axis.js +23 -24
  52. package/dist/kit/horizontal-axis.js.map +1 -0
  53. package/dist/kit/motion.d.ts +5 -0
  54. package/dist/kit/motion.d.ts.map +1 -1
  55. package/dist/kit/motion.js +8 -0
  56. package/dist/kit/motion.js.map +1 -1
  57. package/dist/{pie-chart → kit}/pie-legend.d.ts +3 -3
  58. package/dist/kit/pie-legend.d.ts.map +1 -0
  59. package/dist/{pie-chart → kit}/pie-legend.js +11 -10
  60. package/dist/kit/pie-legend.js.map +1 -0
  61. package/dist/kit/polar.d.ts +31 -0
  62. package/dist/kit/polar.d.ts.map +1 -0
  63. package/dist/kit/polar.js +33 -0
  64. package/dist/kit/polar.js.map +1 -0
  65. package/dist/kit/shared.d.ts +11 -50
  66. package/dist/kit/shared.d.ts.map +1 -1
  67. package/dist/kit/shared.js +58 -141
  68. package/dist/kit/shared.js.map +1 -1
  69. package/dist/kit/style.d.ts +517 -0
  70. package/dist/kit/style.d.ts.map +1 -0
  71. package/dist/kit/style.js +292 -0
  72. package/dist/kit/style.js.map +1 -0
  73. package/dist/kit/vertical-axis.d.ts +22 -2
  74. package/dist/kit/vertical-axis.d.ts.map +1 -1
  75. package/dist/kit/vertical-axis.js +56 -43
  76. package/dist/kit/vertical-axis.js.map +1 -1
  77. package/dist/line-chart/chart-line.d.ts +95 -3
  78. package/dist/line-chart/chart-line.d.ts.map +1 -1
  79. package/dist/line-chart/chart-line.js +165 -53
  80. package/dist/line-chart/chart-line.js.map +1 -1
  81. package/dist/line-chart/chart-region.d.ts +1 -1
  82. package/dist/line-chart/chart-region.d.ts.map +1 -1
  83. package/dist/line-chart/chart-region.js +7 -8
  84. package/dist/line-chart/chart-region.js.map +1 -1
  85. package/dist/line-chart/index.d.ts +5 -6
  86. package/dist/line-chart/index.d.ts.map +1 -1
  87. package/dist/line-chart/index.js +3 -4
  88. package/dist/line-chart/index.js.map +1 -1
  89. package/dist/line-chart/line-chart.d.ts +281 -18
  90. package/dist/line-chart/line-chart.d.ts.map +1 -1
  91. package/dist/line-chart/line-chart.js +284 -134
  92. package/dist/line-chart/line-chart.js.map +1 -1
  93. package/dist/line-chart/shared.d.ts +51 -34
  94. package/dist/line-chart/shared.d.ts.map +1 -1
  95. package/dist/line-chart/shared.js +175 -83
  96. package/dist/line-chart/shared.js.map +1 -1
  97. package/dist/line-chart/x-scale.d.ts +2 -0
  98. package/dist/line-chart/x-scale.d.ts.map +1 -1
  99. package/dist/line-chart/x-scale.js +15 -0
  100. package/dist/line-chart/x-scale.js.map +1 -1
  101. package/dist/nodes.d.ts +3 -1
  102. package/dist/nodes.d.ts.map +1 -1
  103. package/dist/nodes.js +9 -1
  104. package/dist/nodes.js.map +1 -1
  105. package/dist/pie-chart/index.d.ts +2 -3
  106. package/dist/pie-chart/index.d.ts.map +1 -1
  107. package/dist/pie-chart/index.js +1 -2
  108. package/dist/pie-chart/index.js.map +1 -1
  109. package/dist/pie-chart/pie-chart.d.ts +114 -18
  110. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  111. package/dist/pie-chart/pie-chart.js +38 -24
  112. package/dist/pie-chart/pie-chart.js.map +1 -1
  113. package/dist/pie-chart/pie-ring.d.ts +16 -5
  114. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  115. package/dist/pie-chart/pie-ring.js +89 -55
  116. package/dist/pie-chart/pie-ring.js.map +1 -1
  117. package/dist/pie-chart/shared.d.ts +61 -62
  118. package/dist/pie-chart/shared.d.ts.map +1 -1
  119. package/dist/pie-chart/shared.js +44 -123
  120. package/dist/pie-chart/shared.js.map +1 -1
  121. package/dist/radar-chart/index.d.ts +5 -0
  122. package/dist/radar-chart/index.d.ts.map +1 -0
  123. package/dist/radar-chart/index.js +5 -0
  124. package/dist/radar-chart/index.js.map +1 -0
  125. package/dist/radar-chart/radar-chart.d.ts +212 -0
  126. package/dist/radar-chart/radar-chart.d.ts.map +1 -0
  127. package/dist/radar-chart/radar-chart.js +224 -0
  128. package/dist/radar-chart/radar-chart.js.map +1 -0
  129. package/dist/radar-chart/radar-grid.d.ts +49 -0
  130. package/dist/radar-chart/radar-grid.d.ts.map +1 -0
  131. package/dist/radar-chart/radar-grid.js +63 -0
  132. package/dist/radar-chart/radar-grid.js.map +1 -0
  133. package/dist/radar-chart/radar-series.d.ts +59 -0
  134. package/dist/radar-chart/radar-series.d.ts.map +1 -0
  135. package/dist/radar-chart/radar-series.js +71 -0
  136. package/dist/radar-chart/radar-series.js.map +1 -0
  137. package/dist/radar-chart/shared.d.ts +78 -0
  138. package/dist/radar-chart/shared.d.ts.map +1 -0
  139. package/dist/radar-chart/shared.js +129 -0
  140. package/dist/radar-chart/shared.js.map +1 -0
  141. package/dist/radial-chart/index.d.ts +6 -0
  142. package/dist/radial-chart/index.d.ts.map +1 -0
  143. package/dist/radial-chart/index.js +4 -0
  144. package/dist/radial-chart/index.js.map +1 -0
  145. package/dist/radial-chart/radial-chart.d.ts +210 -0
  146. package/dist/radial-chart/radial-chart.d.ts.map +1 -0
  147. package/dist/radial-chart/radial-chart.js +175 -0
  148. package/dist/radial-chart/radial-chart.js.map +1 -0
  149. package/dist/radial-chart/radial-rings.d.ts +121 -0
  150. package/dist/radial-chart/radial-rings.d.ts.map +1 -0
  151. package/dist/radial-chart/radial-rings.js +273 -0
  152. package/dist/radial-chart/radial-rings.js.map +1 -0
  153. package/dist/radial-chart/shared.d.ts +77 -0
  154. package/dist/radial-chart/shared.d.ts.map +1 -0
  155. package/dist/radial-chart/shared.js +78 -0
  156. package/dist/radial-chart/shared.js.map +1 -0
  157. package/package.json +4 -4
  158. package/registry.json +21 -6
  159. package/src/bar-chart/bar-chart.ts +263 -179
  160. package/src/bar-chart/category-axis.ts +46 -40
  161. package/src/bar-chart/chart-bars.ts +136 -117
  162. package/src/bar-chart/index.ts +7 -10
  163. package/src/bar-chart/shared.ts +88 -233
  164. package/src/engine.ts +5 -2
  165. package/src/index.ts +2 -0
  166. package/src/kit/cartesian.ts +121 -40
  167. package/src/kit/chart-canvas.ts +49 -54
  168. package/src/kit/chart-legend.ts +28 -20
  169. package/src/kit/horizontal-axis.ts +114 -0
  170. package/src/kit/motion.ts +7 -0
  171. package/src/{pie-chart → kit}/pie-legend.ts +12 -11
  172. package/src/kit/polar.ts +54 -0
  173. package/src/kit/shared.ts +55 -277
  174. package/src/kit/style.ts +353 -0
  175. package/src/kit/vertical-axis.ts +67 -48
  176. package/src/line-chart/chart-line.ts +176 -54
  177. package/src/line-chart/chart-region.ts +6 -10
  178. package/src/line-chart/index.ts +14 -10
  179. package/src/line-chart/line-chart.ts +353 -151
  180. package/src/line-chart/shared.ts +193 -174
  181. package/src/line-chart/x-scale.ts +13 -0
  182. package/src/nodes.ts +9 -1
  183. package/src/pie-chart/index.ts +2 -23
  184. package/src/pie-chart/pie-chart.ts +54 -35
  185. package/src/pie-chart/pie-ring.ts +92 -71
  186. package/src/pie-chart/shared.ts +80 -270
  187. package/src/radar-chart/index.ts +4 -0
  188. package/src/radar-chart/radar-chart.ts +290 -0
  189. package/src/radar-chart/radar-grid.ts +83 -0
  190. package/src/radar-chart/radar-series.ts +88 -0
  191. package/src/radar-chart/shared.ts +153 -0
  192. package/src/radial-chart/index.ts +5 -0
  193. package/src/radial-chart/radial-chart.ts +212 -0
  194. package/src/radial-chart/radial-rings.ts +328 -0
  195. package/src/radial-chart/shared.ts +126 -0
  196. package/dist/browser/chunks/chunk-4SMRCU24.js +0 -2
  197. package/dist/browser/chunks/chunk-4SMRCU24.js.map +0 -7
  198. package/dist/kit/chart-border.d.ts +0 -18
  199. package/dist/kit/chart-border.d.ts.map +0 -1
  200. package/dist/kit/chart-border.js +0 -23
  201. package/dist/kit/chart-border.js.map +0 -1
  202. package/dist/line-chart/horizontal-axis.d.ts.map +0 -1
  203. package/dist/line-chart/horizontal-axis.js.map +0 -1
  204. package/dist/pie-chart/pie-legend.d.ts.map +0 -1
  205. package/dist/pie-chart/pie-legend.js.map +0 -1
  206. package/src/kit/chart-border.ts +0 -36
  207. package/src/line-chart/horizontal-axis.ts +0 -122
@@ -0,0 +1,212 @@
1
+ import {
2
+ Rect,
3
+ Types,
4
+ clamp,
5
+ component2d,
6
+ easeOut,
7
+ type ChildView,
8
+ type DataRecord,
9
+ type NodeOf,
10
+ // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
11
+ type Node2D,
12
+ type Node2DProps as _Node2DProps,
13
+ type TextStyle as _TextStyle,
14
+ type Fill as _Fill,
15
+ type FillResolved as _FillResolved,
16
+ } from "@motionscript/sdk"
17
+
18
+ import { SERIES_ARG, asParts, fresh } from "../kit/entries"
19
+ import { beats, type AnyTimeline, type Beat } from "../kit/motion"
20
+ import { CHART_STYLE_FAMILY, CHART_STYLE_PRESETS, migrateChartStyle } from "../kit/style"
21
+ import { PieLegend } from "../kit/pie-legend"
22
+ import { PIE_SPOTLIGHT_DIM, selectionFade } from "../kit/polar"
23
+ import { RadialRings } from "./radial-rings"
24
+ import {
25
+ RADIAL_STYLE_GROUPS,
26
+ centerOf,
27
+ legendOf,
28
+ marksOf,
29
+ resolveRadialRows,
30
+ ringLabelOf,
31
+ type RadialRow,
32
+ type RadialStyle,
33
+ } from "./shared"
34
+
35
+ export interface RadialChartValues {
36
+ readonly data: DataRecord[]
37
+ readonly categoryField: string
38
+ readonly valueField: string
39
+ readonly max: number
40
+ readonly style: RadialStyle
41
+ readonly pops: number[]
42
+ }
43
+
44
+ /** How `enter` divides its duration: rings sweeping in, then captions and legend together. Sums to 1. */
45
+ const SWEEP_SHARE = 0.7
46
+ const LABEL_SHARE = 0.3
47
+
48
+ function rowsOf(s: RadialChartValues): RadialRow[] {
49
+ return resolveRadialRows(s.data, s.categoryField, s.valueField, s.style)
50
+ }
51
+
52
+ function popAt(s: RadialChartValues, index: number): number {
53
+ return clamp(s.pops[index] ?? 0, 0, 1)
54
+ }
55
+
56
+ /** Per-row opacity for the legend: 1 while nothing is selected and for the selected ring, falling toward the dim for the rest. */
57
+ function ringDims(s: RadialChartValues): number[] {
58
+ let focus = 0
59
+ for (let i = 0; i < s.data.length; i++) focus = Math.max(focus, popAt(s, i))
60
+ return s.data.map((_, i) => selectionFade(focus, RING_DIM, popAt(s, i)))
61
+ }
62
+
63
+ const RING_DIM = Math.max(PIE_SPOTLIGHT_DIM, 0.25)
64
+
65
+ function popping(timeline: AnyTimeline, s: RadialChartValues, pop: (index: number) => number) {
66
+ const from = s.data.map((_, i) => popAt(s, i))
67
+ const targets = s.data.map((_, i) => clamp(pop(i), 0, 1))
68
+ if (from.every((value, i) => value === targets[i])) return timeline.wait(timeline.duration)
69
+ return timeline.animate((t) => ({ pops: from.map((value, i) => value + (targets[i] - value) * t) }))
70
+ }
71
+
72
+ /** The rings and legend among the children; the last row is the one the chart built. */
73
+ function partsOf(children: readonly ChildView[]) {
74
+ for (let i = children.length - 1; i >= 0; i--) {
75
+ const row = children[i]
76
+ const rings = Rect.is(row) ? row.children.find(RadialRings.is) : undefined
77
+ if (rings) return { rings, legend: row.children.find(PieLegend.is) ?? null }
78
+ }
79
+ return null
80
+ }
81
+
82
+ /**
83
+ * A radial bar chart: one concentric ring per row, the first row outermost,
84
+ * each an arc from `startAngle` sweeping `value / max` of `sweep` degrees.
85
+ * Rows are the marks and none can be hidden; `selectRing` thickens a ring and
86
+ * dims the rest.
87
+ */
88
+ export const RadialChart = component2d({
89
+ key: "radialChart",
90
+ state: {
91
+ data: fresh<DataRecord[]>(() => [], { kind: "dataset" }),
92
+ /** Ring caption, used on the ring labels and the legend. */
93
+ categoryField: "",
94
+ /** Ring magnitude: the arc's share of the full sweep. */
95
+ valueField: "",
96
+ /** The value a full sweep stands for. 0 lets the largest value fill its ring. */
97
+ max: Types.number(0, { min: 0, step: 1 }),
98
+ style: Types.style(CHART_STYLE_FAMILY, RADIAL_STYLE_GROUPS, { presets: CHART_STYLE_PRESETS }),
99
+ /** Gap between the rings and the legend, in px. */
100
+ gap: 40,
101
+ /** Text set in the centre hole. */
102
+ centerLabel: "",
103
+ /** Smaller second line under `centerLabel`. */
104
+ centerCaption: "",
105
+ /** How far each ring is emphasised, `0`-`1` per index. Here because the rings and the legend both read it. */
106
+ pops: fresh<number[]>(() => []),
107
+ /** Read as the chart is built: whether the centre summary is drawn at rest. A scene that wants it to land later sets `false` and runs `revealCenter`. */
108
+ showCenter: Types.boolean(true, { animatable: false }),
109
+ },
110
+ defaults: {
111
+ width: "fill",
112
+ height: "fill",
113
+ pops: (state) => state.data.map(() => 0),
114
+ },
115
+ migrate: { style: migrateChartStyle("radial") },
116
+ allowedChildren: [],
117
+ children: (state) => {
118
+ const style = () => state.style
119
+ const rows = () => rowsOf(state)
120
+ const parts: Node2D[] = [
121
+ new RadialRings({
122
+ width: "fill",
123
+ height: "fill",
124
+ rows,
125
+ max: () => state.max,
126
+ marks: () => marksOf(style()),
127
+ label: () => ringLabelOf(style()),
128
+ centerStyle: () => centerOf(style()),
129
+ trackFill: () => style().track,
130
+ ringLabels: () => style().ringLabels,
131
+ pops: () => state.pops,
132
+ dimOpacity: RING_DIM,
133
+ centerLabel: state.centerLabel,
134
+ centerCaption: state.centerCaption,
135
+ centerReveal: state.showCenter === false ? 0 : 1,
136
+ }) as unknown as Node2D,
137
+ ]
138
+ const position = state.style.legend
139
+ if (position !== "none") {
140
+ const side = position === "right"
141
+ const config = legendOf(state.style)
142
+ const legend = new PieLegend({
143
+ slices: rows,
144
+ legend: () => legendOf(style()),
145
+ dims: () => ringDims(state),
146
+ flow: side ? "vertical" : "horizontal",
147
+ align: "center",
148
+ gap: side ? config.itemGap / 3 : config.itemGap,
149
+ padding: 0,
150
+ }) as unknown as Node2D
151
+ if (position === "top") parts.unshift(legend)
152
+ else parts.push(legend)
153
+ }
154
+ const row = new Rect({
155
+ flow: position === "right" || position === "none" ? "horizontal" : "vertical",
156
+ width: "fill",
157
+ height: "fill",
158
+ gap: state.gap,
159
+ padding: 24,
160
+ align: "center",
161
+ fill: () => state.style.background,
162
+ children: parts,
163
+ })
164
+ return asParts([row as unknown as Node2D])
165
+ },
166
+ commands: {
167
+ /** Rings sweep in from `startAngle`, outer ring first, then captions fade up and the legend staggers in. `duration` is the whole entrance; the centre summary keeps its own signal. */
168
+ enter: {
169
+ args: {},
170
+ entrance: true,
171
+ easing: easeOut(),
172
+ run: (timeline, _state, children) => {
173
+ const parts = partsOf(children)
174
+ if (!parts) return timeline.wait(timeline.duration)
175
+ const { rings, legend } = parts
176
+ const sweep = timeline.duration * SWEEP_SHARE
177
+ const labels = timeline.duration * LABEL_SHARE
178
+ const at0 = (piece: Beat["piece"]): Beat => ({ at: 0, duration: 0, piece })
179
+ return beats(timeline, [
180
+ at0(rings.to({ growth: 0, labelReveal: 0 })),
181
+ ...(legend ? [at0(legend.run("arm"))] : []),
182
+ { at: 0, duration: sweep, piece: rings.run("sweep") },
183
+ { at: sweep, duration: labels, piece: rings.run("revealLabels") },
184
+ ...(legend ? [{ at: sweep, duration: labels, piece: legend.run("enter") }] : []),
185
+ ])
186
+ },
187
+ },
188
+ /** Fade the centre summary up. Not part of `enter`: it is usually the conclusion, which a scene lands on its own beat. */
189
+ revealCenter: {
190
+ args: {},
191
+ easing: easeOut(),
192
+ run: (timeline, _state, children) => partsOf(children)?.rings.run("revealCenter") ?? timeline.wait(timeline.duration),
193
+ },
194
+ /** Thicken ring `ringIndex` and dim the rest, leaving any other selected ring where it is. */
195
+ selectRing: {
196
+ args: { ringIndex: SERIES_ARG },
197
+ easing: easeOut(),
198
+ run: (timeline, state) => {
199
+ const index = timeline.args.ringIndex
200
+ return popping(timeline, state, (i) => (i === index ? 1 : popAt(state, i)))
201
+ },
202
+ },
203
+ /** Settle every ring and bring the whole chart back to full. */
204
+ clearSelection: {
205
+ args: {},
206
+ easing: easeOut(),
207
+ run: (timeline, state) => popping(timeline, state, () => 0),
208
+ },
209
+ },
210
+ })
211
+
212
+ export type RadialChart = NodeOf<typeof RadialChart>
@@ -0,0 +1,328 @@
1
+ import {
2
+ Graphics2D,
3
+ PathBuilder,
4
+ Rect,
5
+ Text,
6
+ Types,
7
+ clamp,
8
+ component2d,
9
+ fillOps,
10
+ textOps,
11
+ type NodeOf,
12
+ // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
13
+ type Node2D,
14
+ type Node2DProps as _Node2DProps,
15
+ type Fill as _Fill,
16
+ type FillResolved as _FillResolved,
17
+ type TextStyle,
18
+ type Vector2,
19
+ } from "@motionscript/sdk"
20
+
21
+ import { asParts, fresh } from "../kit/entries"
22
+ import { frameEase } from "../kit/motion"
23
+ import { loadPaints } from "../kit/paints"
24
+ import { DEG, ringFills, ringPoint, selectionFade, type CenterConfig } from "../kit/polar"
25
+ import { radialFractions, type RadialMarks, type RadialRow } from "./shared"
26
+
27
+ /** Share of the entrance one ring's sweep starts after the last. */
28
+ const STAGGER = 0.12
29
+ /** A selected ring's thickness grows by this share of itself, on each side. */
30
+ const GROW = 0.12
31
+ const ARC_STEP = 4 * DEG
32
+ const LABEL_PAD = 10
33
+ const CENTER_FIT = 0.78
34
+ const GLYPH_RATIO = 0.62
35
+ /** How far the radius is held in from the box, for the sake of a ring's growth. */
36
+ const EDGE = 4
37
+
38
+ const DEFAULT_MARKS: RadialMarks = { innerRadius: 0.3, ringGap: 8, sweep: 360, startAngle: 0, track: true, roundCaps: true }
39
+ const DEFAULT_LABEL: TextStyle = { fontSize: 28, fontWeight: 500, textAlign: "center", fill: "#a1a1aa" }
40
+ const DEFAULT_CENTER: CenterConfig = {
41
+ labelStyle: { fontSize: 50, fontWeight: 800, textAlign: "center", fill: "#f4f4f5" },
42
+ captionStyle: { fontSize: 22, fontWeight: 500, textAlign: "center", fill: "#a1a1aa" },
43
+ }
44
+
45
+ interface RingsState {
46
+ readonly rows: RadialRow[]
47
+ readonly max: number
48
+ readonly marks: RadialMarks
49
+ readonly label: TextStyle
50
+ readonly centerStyle: CenterConfig
51
+ readonly ringLabels: boolean
52
+ readonly pops: readonly number[]
53
+ readonly dimOpacity: number
54
+ readonly centerLabel: string
55
+ readonly centerCaption: string
56
+ }
57
+
58
+ function sizeOf(style: TextStyle, fallback: number): number {
59
+ return typeof style.fontSize === "number" ? style.fontSize : fallback
60
+ }
61
+
62
+ /** Every radius and angle for one box, derived wherever read so the drawing and the labels cannot disagree. */
63
+ export class RadialGeometry {
64
+ constructor(
65
+ private readonly s: RingsState,
66
+ private readonly box: { readonly width: number; readonly height: number }
67
+ ) {}
68
+
69
+ get count(): number {
70
+ return this.s.rows.length
71
+ }
72
+
73
+ get outerRadius(): number {
74
+ return Math.max(0, Math.min(this.box.width, this.box.height) / 2 - EDGE)
75
+ }
76
+
77
+ get holeRadius(): number {
78
+ return clamp(this.s.marks.innerRadius, 0, 0.9) * this.outerRadius
79
+ }
80
+
81
+ /** Radial depth of one ring. */
82
+ get thickness(): number {
83
+ const n = this.count
84
+ if (n === 0) return 0
85
+ return Math.max(0, (this.outerRadius - this.holeRadius - (n - 1) * Math.max(0, this.s.marks.ringGap)) / n)
86
+ }
87
+
88
+ /** Rim of ring `index`; ring 0 is the outermost. */
89
+ ringOuter(index: number): number {
90
+ return this.outerRadius - index * (this.thickness + Math.max(0, this.s.marks.ringGap))
91
+ }
92
+
93
+ ringMid(index: number): number {
94
+ return this.ringOuter(index) - this.thickness / 2
95
+ }
96
+
97
+ get fractions(): number[] {
98
+ return radialFractions(this.s.rows, this.s.max)
99
+ }
100
+
101
+ get startRad(): number {
102
+ return this.s.marks.startAngle * DEG
103
+ }
104
+
105
+ get sweepRad(): number {
106
+ return clamp(this.s.marks.sweep, 0, 360) * DEG
107
+ }
108
+
109
+ popOf(index: number): number {
110
+ return clamp(this.s.pops[index] ?? 0, 0, 1)
111
+ }
112
+
113
+ get selectionStrength(): number {
114
+ let strongest = 0
115
+ for (let i = 0; i < this.count; i++) strongest = Math.max(strongest, this.popOf(i))
116
+ return strongest
117
+ }
118
+
119
+ ringOpacity(index: number): number {
120
+ return selectionFade(this.selectionStrength, this.s.dimOpacity, this.popOf(index))
121
+ }
122
+
123
+ /** How far ring `index`'s sweep has come at entrance progress `growth`: rings start in turn, outer first. */
124
+ progress(index: number, growth: number): number {
125
+ const total = 1 + STAGGER * Math.max(0, this.count - 1)
126
+ return clamp(growth * total - index * STAGGER, 0, 1)
127
+ }
128
+
129
+ get labelFont(): number {
130
+ return Math.max(1, Math.min(sizeOf(this.s.label, 28), this.thickness * 0.62))
131
+ }
132
+
133
+ /** Where ring `index`'s caption is pinned: just behind its start, reading away from the sweep. */
134
+ labelAnchor(index: number): { at: Vector2; side: 1 | -1 } {
135
+ const a = this.startRad
136
+ const back = { x: -Math.cos(a), y: Math.sin(a) }
137
+ const p = ringPoint(this.ringMid(index), a)
138
+ const side: 1 | -1 = back.x <= 0 ? -1 : 1
139
+ return { at: { x: p.x + back.x * LABEL_PAD, y: p.y + back.y * LABEL_PAD }, side }
140
+ }
141
+
142
+ get centerFont(): number {
143
+ const { centerStyle, centerLabel, centerCaption } = this.s
144
+ const wanted = sizeOf(centerStyle.labelStyle, 50)
145
+ const hole = this.holeRadius
146
+ if (hole <= 0) return wanted
147
+ const ratio = sizeOf(centerStyle.captionStyle, 22) / Math.max(1, wanted)
148
+ const glyphs = Math.max(1, centerLabel.length, centerCaption.length * ratio)
149
+ return Math.min(wanted, (hole * 2 * CENTER_FIT) / (glyphs * GLYPH_RATIO))
150
+ }
151
+ }
152
+
153
+ /**
154
+ * An arc band between `rIn` and `rOut` over `a0`..`a1` (radians clockwise from
155
+ * 12 o'clock), the ends squared or capped with half circles. A band shorter
156
+ * than its caps is two overlapping caps, which fill as a pill.
157
+ */
158
+ export function bandPath(a0: number, a1: number, rIn: number, rOut: number, round: boolean): PathBuilder {
159
+ const path = new PathBuilder()
160
+ const mid = (rIn + rOut) / 2
161
+ const cap = (rOut - rIn) / 2
162
+ const delta = round && mid > 0 ? Math.min(cap / mid, (a1 - a0) / 2) : 0
163
+ const from = a0 + delta
164
+ const to = a1 - delta
165
+ const steps = Math.max(2, Math.ceil(Math.abs(to - from) / ARC_STEP))
166
+ const arc = (r: number, x: number, y: number, first: boolean) => {
167
+ for (let i = 0; i <= steps; i++) {
168
+ const p = ringPoint(r, x + ((y - x) * i) / steps)
169
+ if (first && i === 0) path.moveTo(p.x, p.y)
170
+ else path.lineTo(p.x, p.y)
171
+ }
172
+ }
173
+ // The end at `angle` rounded through a tip `direction` along the sweep.
174
+ const semicircle = (angle: number, direction: 1 | -1, fromOuter: boolean) => {
175
+ const centre = ringPoint(mid, angle)
176
+ const radial = { x: Math.sin(angle), y: Math.cos(angle) }
177
+ const tangent = { x: Math.cos(angle) * direction, y: -Math.sin(angle) * direction }
178
+ const turns = 12
179
+ for (let i = 1; i < turns; i++) {
180
+ const phi = (Math.PI * i) / turns
181
+ const r = fromOuter ? Math.cos(phi) : -Math.cos(phi)
182
+ path.lineTo(centre.x + cap * (radial.x * r + tangent.x * Math.sin(phi)), centre.y + cap * (radial.y * r + tangent.y * Math.sin(phi)))
183
+ }
184
+ }
185
+ arc(rOut, from, to, true)
186
+ if (round) semicircle(to, 1, true)
187
+ arc(rIn, to, from, false)
188
+ if (round) semicircle(from, -1, false)
189
+ path.close()
190
+ return path
191
+ }
192
+
193
+ const NO_BOX = { width: 0, height: 0 }
194
+
195
+ function labelBlocks(s: RingsState & { readonly labelReveal: number; readonly centerReveal: number }) {
196
+ const blocks: Node2D[] = []
197
+ const geometry = () => new RadialGeometry(s, (blocks[0]?.parent as Node2D | null | undefined)?.layoutBounds ?? NO_BOX)
198
+ const side = geometry().labelAnchor(0).side
199
+
200
+ s.rows.forEach((_, index) => {
201
+ const anchor = () => geometry().labelAnchor(index).at
202
+ blocks.push(
203
+ new Rect({
204
+ flow: "vertical",
205
+ width: "hug",
206
+ height: "hug",
207
+ align: side > 0 ? "centerLeft" : "centerRight",
208
+ opacity: () => (s.ringLabels ? clamp(s.labelReveal, 0, 1) * geometry().ringOpacity(index) : 0),
209
+ centerLeft: side > 0 ? anchor : undefined,
210
+ centerRight: side > 0 ? undefined : anchor,
211
+ children: [
212
+ new Text({
213
+ ...textOps.styleProps(s.label),
214
+ text: () => s.rows[index]?.category ?? "",
215
+ textAlign: side > 0 ? "left" : "right",
216
+ fontSize: () => geometry().labelFont,
217
+ }),
218
+ ],
219
+ })
220
+ )
221
+ })
222
+
223
+ if (s.centerLabel !== "" || s.centerCaption !== "") {
224
+ const children: Node2D[] = []
225
+ if (s.centerLabel !== "") {
226
+ children.push(new Text({ ...textOps.styleProps(s.centerStyle.labelStyle), text: s.centerLabel, fontSize: () => geometry().centerFont }))
227
+ }
228
+ if (s.centerCaption !== "") {
229
+ children.push(
230
+ new Text({
231
+ ...textOps.styleProps(s.centerStyle.captionStyle),
232
+ text: s.centerCaption,
233
+ fontSize: () => geometry().centerFont * (sizeOf(s.centerStyle.captionStyle, 22) / sizeOf(s.centerStyle.labelStyle, 50)),
234
+ })
235
+ )
236
+ }
237
+ blocks.push(
238
+ new Rect({
239
+ flow: "vertical",
240
+ width: "hug",
241
+ height: "hug",
242
+ gap: 2,
243
+ align: "center",
244
+ opacity: () => clamp(s.centerReveal, 0, 1),
245
+ center: { x: 0, y: 0 },
246
+ children,
247
+ })
248
+ )
249
+ }
250
+ return asParts(blocks)
251
+ }
252
+
253
+ /**
254
+ * The rings: row `i` is the `i`-th ring from the outside, an arc from
255
+ * `startAngle` whose sweep is `value / max` of the full sweep, over an
256
+ * optional full-sweep track. A selected ring thickens and the rest dims.
257
+ */
258
+ export const RadialRings = component2d({
259
+ key: "radialRings",
260
+ state: {
261
+ rows: fresh<RadialRow[]>(() => []),
262
+ max: 0,
263
+ marks: Types.custom<RadialMarks>(DEFAULT_MARKS),
264
+ label: Types.custom<TextStyle>(DEFAULT_LABEL),
265
+ centerStyle: Types.custom<CenterConfig>(DEFAULT_CENTER),
266
+ /** The empty-ring colour drawn under each arc. */
267
+ trackFill: Types.fill("white/15"),
268
+ ringLabels: Types.boolean(false),
269
+ /** Entrance progress `[0, 1]`: rings sweep in turn. */
270
+ growth: 1,
271
+ labelReveal: 1,
272
+ pops: fresh<number[]>(() => []),
273
+ dimOpacity: 0.25,
274
+ centerLabel: "",
275
+ centerCaption: "",
276
+ centerReveal: 1,
277
+ },
278
+ defaults: { width: "fill", height: "fill" },
279
+ children: (state) => labelBlocks(state),
280
+ load: (loader, state) => loadPaints(loader, { fills: [...ringFills(state.rows), fillOps.resolve(state.trackFill)] }),
281
+ commands: {
282
+ /** Sweep the rings in, outer first. */
283
+ sweep: (timeline) => {
284
+ const ease = frameEase(timeline)
285
+ return timeline.animate((t) => ({ growth: ease(t) }))
286
+ },
287
+ /** Fade the ring captions up. */
288
+ revealLabels: (timeline) => {
289
+ const ease = frameEase(timeline)
290
+ return timeline.animate((t) => ({ labelReveal: ease(t) }))
291
+ },
292
+ /** Fade the centre summary up. */
293
+ revealCenter: (timeline, state) => {
294
+ const ease = frameEase(timeline)
295
+ const from = clamp(state.centerReveal, 0, 1)
296
+ return timeline.animate((t) => ({ centerReveal: from + (1 - from) * ease(t) }))
297
+ },
298
+ },
299
+ render: (ctx, state) => {
300
+ const g = new RadialGeometry(state, ctx.size)
301
+ const thickness = g.thickness
302
+ if (g.outerRadius > 0 && g.count > 0 && thickness > 0) {
303
+ const half = g.outerRadius + EDGE + 1
304
+ const frame = { centerBounds: [-half, -half, half, half] as [number, number, number, number], width: half * 2, height: half * 2 }
305
+ const fills = ringFills(state.rows)
306
+ const fractions = g.fractions
307
+ const a0 = g.startRad
308
+ const round = state.marks.roundCaps
309
+ // Selected rings last, so a thickened ring is never under its neighbour.
310
+ const order = [...state.rows.keys()].sort((a, b) => g.popOf(a) - g.popOf(b))
311
+ for (const i of order) {
312
+ const grow = thickness * GROW * g.popOf(i)
313
+ const rOut = g.ringOuter(i) + grow
314
+ const rIn = g.ringOuter(i) - thickness - grow
315
+ const opacity = g.ringOpacity(i)
316
+ if (state.marks.track && g.sweepRad > 0) {
317
+ ctx.draw(new Graphics2D().path(bandPath(a0, a0 + g.sweepRad, rIn, rOut, round).toPathState(frame)).fill(state.trackFill).opacity(opacity))
318
+ }
319
+ const span = g.sweepRad * fractions[i] * g.progress(i, state.growth)
320
+ if (span <= 1e-4) continue
321
+ ctx.draw(new Graphics2D().path(bandPath(a0, a0 + span, rIn, rOut, round).toPathState(frame)).fill(fills[i] ?? []).opacity(opacity))
322
+ }
323
+ }
324
+ ctx.children()
325
+ },
326
+ })
327
+
328
+ export type RadialRings = NodeOf<typeof RadialRings>
@@ -0,0 +1,126 @@
1
+ import { Types, clamp, type DataRecord, type TextStyle, type Fill as _Fill, type FillResolved as _FillResolved } from "@motionscript/sdk"
2
+
3
+ import {
4
+ CHART_BACKGROUND,
5
+ CHART_FOREGROUND,
6
+ CHART_LEGEND,
7
+ CHART_MUTED,
8
+ CHART_SERIES,
9
+ LEGEND_TEXT,
10
+ legendConfigOf,
11
+ paletteFill,
12
+ placedText,
13
+ sizeOf,
14
+ type ChartStyleBase,
15
+ } from "../kit/style"
16
+ import type { LegendConfig } from "../kit/shared"
17
+ import { num, text } from "../kit/shared"
18
+ import type { CenterConfig, PieSlice } from "../kit/polar"
19
+
20
+ /** One ring: a row's caption, magnitude and paint. The same shape as a pie's slice, so the legend is shared. */
21
+ export type RadialRow = PieSlice
22
+
23
+ export function resolveRadialRows(data: readonly DataRecord[], category: string, value: string, style: Pick<ChartStyleBase, "seriesFills">): RadialRow[] {
24
+ return data.map((row, i) => ({ category: text(row, category), value: num(row, value), fill: paletteFill(style, i) }))
25
+ }
26
+
27
+ /** A row's plotted value: negatives and non-finite values count as 0. */
28
+ export function rowValue(row: RadialRow): number {
29
+ return row.value > 0 && Number.isFinite(row.value) ? row.value : 0
30
+ }
31
+
32
+ /** What a full sweep stands for: `max` when it is positive, else the largest value (1 when every value is 0). */
33
+ export function radialMax(rows: readonly RadialRow[], max: number): number {
34
+ if (max > 0 && Number.isFinite(max)) return max
35
+ let largest = 0
36
+ for (const row of rows) largest = Math.max(largest, rowValue(row))
37
+ return largest > 0 ? largest : 1
38
+ }
39
+
40
+ /** The share of a full sweep each row fills, `[0, 1]`. */
41
+ export function radialFractions(rows: readonly RadialRow[], max: number): number[] {
42
+ const top = radialMax(rows, max)
43
+ return rows.map((row) => clamp(rowValue(row) / top, 0, 1))
44
+ }
45
+
46
+ /** The ring geometry and look, from `style`. */
47
+ export interface RadialMarks {
48
+ innerRadius: number
49
+ ringGap: number
50
+ /** Degrees of a full ring. */
51
+ sweep: number
52
+ /** Degrees clockwise from 12 o'clock. */
53
+ startAngle: number
54
+ track: boolean
55
+ roundCaps: boolean
56
+ }
57
+
58
+ export interface RadialStyle extends ChartStyleBase {
59
+ readonly innerRadius: number
60
+ readonly ringGap: number
61
+ readonly sweep: number
62
+ readonly startAngle: number
63
+ readonly track: _FillResolved[]
64
+ readonly roundCaps: boolean
65
+ readonly ringLabels: boolean
66
+ readonly labelText: TextStyle
67
+ readonly centerText: TextStyle
68
+ }
69
+
70
+ export const RADIAL_STYLE_GROUPS = {
71
+ series: CHART_SERIES,
72
+ background: CHART_BACKGROUND,
73
+ text: {
74
+ /** The category at each ring's start, and the caption under the centre. */
75
+ labelText: Types.typography({ fontSize: 28, fontWeight: 500, fill: CHART_MUTED }),
76
+ /** The summary in the middle. */
77
+ centerText: Types.typography({ fontSize: 50, fontWeight: 800, fill: CHART_FOREGROUND }),
78
+ ...LEGEND_TEXT,
79
+ },
80
+ marks: {
81
+ /** Where the innermost ring starts, as a fraction of the outer radius. */
82
+ innerRadius: Types.number(0.3, { min: 0, max: 0.9, step: 0.05 }),
83
+ /** Space between neighbouring rings, px. */
84
+ ringGap: Types.number(8, { min: 0, max: 60, step: 1, unit: "px" }),
85
+ /** Degrees a full ring spans: 360 a circle, 270 a gauge-like open ring. */
86
+ sweep: Types.number(360, { min: 30, max: 360, step: 5, unit: "deg" }),
87
+ /** Degrees clockwise from 12 o'clock where every ring begins. */
88
+ startAngle: Types.number(0, { min: -360, max: 360, step: 5, unit: "deg" }),
89
+ /** The full ring under each arc; empty draws none. */
90
+ track: Types.fill("white/10"),
91
+ /** Round the ends of the arcs and tracks. */
92
+ roundCaps: Types.boolean(true),
93
+ },
94
+ labels: {
95
+ /** The category written at each ring's start, set in the label text. */
96
+ ringLabels: Types.boolean(false),
97
+ },
98
+ legend: CHART_LEGEND,
99
+ }
100
+
101
+ export function marksOf(style: RadialStyle): RadialMarks {
102
+ return {
103
+ innerRadius: style.innerRadius,
104
+ ringGap: style.ringGap,
105
+ sweep: style.sweep,
106
+ startAngle: style.startAngle,
107
+ track: style.track.length > 0,
108
+ roundCaps: style.roundCaps,
109
+ }
110
+ }
111
+
112
+ export function legendOf(style: RadialStyle): LegendConfig {
113
+ return legendConfigOf(style)
114
+ }
115
+
116
+ export function ringLabelOf(style: RadialStyle): TextStyle {
117
+ return placedText(style.labelText)
118
+ }
119
+
120
+ export function centerOf(style: RadialStyle): CenterConfig {
121
+ const size = sizeOf(style.centerText, 50)
122
+ return {
123
+ labelStyle: placedText(style.centerText),
124
+ captionStyle: placedText(style.labelText, "center", { fontSize: Math.round(size * 0.45) }),
125
+ }
126
+ }