@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.
Files changed (154) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +99 -0
  5. package/dist/bar-chart/bar-chart.d.ts +93 -231
  6. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  7. package/dist/bar-chart/bar-chart.js +378 -565
  8. package/dist/bar-chart/bar-chart.js.map +1 -1
  9. package/dist/bar-chart/category-axis.d.ts +24 -41
  10. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  11. package/dist/bar-chart/category-axis.js +93 -115
  12. package/dist/bar-chart/category-axis.js.map +1 -1
  13. package/dist/bar-chart/chart-bars.d.ts +36 -70
  14. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  15. package/dist/bar-chart/chart-bars.js +156 -210
  16. package/dist/bar-chart/chart-bars.js.map +1 -1
  17. package/dist/bar-chart/index.d.ts +2 -2
  18. package/dist/bar-chart/index.d.ts.map +1 -1
  19. package/dist/bar-chart/index.js +1 -1
  20. package/dist/bar-chart/index.js.map +1 -1
  21. package/dist/bar-chart/shared.d.ts +6 -6
  22. package/dist/bar-chart/shared.d.ts.map +1 -1
  23. package/dist/bar-chart/shared.js +6 -6
  24. package/dist/bar-chart/shared.js.map +1 -1
  25. package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
  26. package/dist/browser/chunks/chunk-XROBIRQK.js.map +7 -0
  27. package/dist/browser/index.js +1 -1
  28. package/dist/browser/index.js.map +3 -3
  29. package/dist/browser/kit.js +1 -1
  30. package/dist/browser/manifest.json +3 -2
  31. package/dist/engine.d.ts +8 -6
  32. package/dist/engine.d.ts.map +1 -1
  33. package/dist/engine.js +8 -6
  34. package/dist/engine.js.map +1 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/kit/cartesian.d.ts +44 -0
  37. package/dist/kit/cartesian.d.ts.map +1 -0
  38. package/dist/kit/cartesian.js +59 -0
  39. package/dist/kit/cartesian.js.map +1 -0
  40. package/dist/kit/chart-border.d.ts +8 -11
  41. package/dist/kit/chart-border.d.ts.map +1 -1
  42. package/dist/kit/chart-border.js +15 -32
  43. package/dist/kit/chart-border.js.map +1 -1
  44. package/dist/kit/chart-canvas.d.ts +23 -21
  45. package/dist/kit/chart-canvas.d.ts.map +1 -1
  46. package/dist/kit/chart-canvas.js +33 -90
  47. package/dist/kit/chart-canvas.js.map +1 -1
  48. package/dist/kit/chart-legend.d.ts +79 -88
  49. package/dist/kit/chart-legend.d.ts.map +1 -1
  50. package/dist/kit/chart-legend.js +137 -186
  51. package/dist/kit/chart-legend.js.map +1 -1
  52. package/dist/kit/entries.d.ts +17 -0
  53. package/dist/kit/entries.d.ts.map +1 -0
  54. package/dist/kit/entries.js +19 -0
  55. package/dist/kit/entries.js.map +1 -0
  56. package/dist/kit/motion.d.ts +48 -0
  57. package/dist/kit/motion.d.ts.map +1 -0
  58. package/dist/kit/motion.js +63 -0
  59. package/dist/kit/motion.js.map +1 -0
  60. package/dist/kit/paints.d.ts +10 -0
  61. package/dist/kit/paints.d.ts.map +1 -0
  62. package/dist/kit/paints.js +12 -0
  63. package/dist/kit/paints.js.map +1 -0
  64. package/dist/kit/shared.d.ts +59 -8
  65. package/dist/kit/shared.d.ts.map +1 -1
  66. package/dist/kit/shared.js +105 -10
  67. package/dist/kit/shared.js.map +1 -1
  68. package/dist/kit/vertical-axis.d.ts +36 -61
  69. package/dist/kit/vertical-axis.d.ts.map +1 -1
  70. package/dist/kit/vertical-axis.js +97 -132
  71. package/dist/kit/vertical-axis.js.map +1 -1
  72. package/dist/line-chart/chart-line.d.ts +27 -53
  73. package/dist/line-chart/chart-line.d.ts.map +1 -1
  74. package/dist/line-chart/chart-line.js +89 -120
  75. package/dist/line-chart/chart-line.js.map +1 -1
  76. package/dist/line-chart/chart-region.d.ts +14 -14
  77. package/dist/line-chart/chart-region.d.ts.map +1 -1
  78. package/dist/line-chart/chart-region.js +26 -64
  79. package/dist/line-chart/chart-region.js.map +1 -1
  80. package/dist/line-chart/horizontal-axis.d.ts +32 -56
  81. package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
  82. package/dist/line-chart/horizontal-axis.js +85 -123
  83. package/dist/line-chart/horizontal-axis.js.map +1 -1
  84. package/dist/line-chart/index.d.ts +2 -2
  85. package/dist/line-chart/index.d.ts.map +1 -1
  86. package/dist/line-chart/index.js +1 -1
  87. package/dist/line-chart/index.js.map +1 -1
  88. package/dist/line-chart/line-chart.d.ts +111 -311
  89. package/dist/line-chart/line-chart.d.ts.map +1 -1
  90. package/dist/line-chart/line-chart.js +403 -639
  91. package/dist/line-chart/line-chart.js.map +1 -1
  92. package/dist/line-chart/shared.d.ts +6 -6
  93. package/dist/line-chart/shared.d.ts.map +1 -1
  94. package/dist/line-chart/shared.js +22 -7
  95. package/dist/line-chart/shared.js.map +1 -1
  96. package/dist/line-chart/x-scale.d.ts +1 -1
  97. package/dist/line-chart/x-scale.js +1 -1
  98. package/dist/line-chart/x-scale.js.map +1 -1
  99. package/dist/nodes.d.ts +6 -9
  100. package/dist/nodes.d.ts.map +1 -1
  101. package/dist/nodes.js +7 -9
  102. package/dist/nodes.js.map +1 -1
  103. package/dist/pie-chart/index.d.ts +2 -2
  104. package/dist/pie-chart/index.d.ts.map +1 -1
  105. package/dist/pie-chart/index.js +1 -1
  106. package/dist/pie-chart/index.js.map +1 -1
  107. package/dist/pie-chart/pie-chart.d.ts +77 -134
  108. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  109. package/dist/pie-chart/pie-chart.js +199 -268
  110. package/dist/pie-chart/pie-chart.js.map +1 -1
  111. package/dist/pie-chart/pie-legend.d.ts +54 -31
  112. package/dist/pie-chart/pie-legend.d.ts.map +1 -1
  113. package/dist/pie-chart/pie-legend.js +93 -107
  114. package/dist/pie-chart/pie-legend.js.map +1 -1
  115. package/dist/pie-chart/pie-ring.d.ts +142 -122
  116. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  117. package/dist/pie-chart/pie-ring.js +333 -361
  118. package/dist/pie-chart/pie-ring.js.map +1 -1
  119. package/dist/pie-chart/shared.d.ts +3 -3
  120. package/dist/pie-chart/shared.js +3 -3
  121. package/dist/pie-chart/shared.js.map +1 -1
  122. package/package.json +6 -5
  123. package/registry.json +23 -23
  124. package/src/bar-chart/bar-chart.ts +534 -814
  125. package/src/bar-chart/category-axis.ts +162 -177
  126. package/src/bar-chart/chart-bars.ts +288 -310
  127. package/src/bar-chart/index.ts +31 -31
  128. package/src/bar-chart/shared.ts +354 -354
  129. package/src/engine.ts +28 -26
  130. package/src/index.ts +5 -5
  131. package/src/kit/cartesian.ts +81 -0
  132. package/src/kit/chart-border.ts +50 -61
  133. package/src/kit/chart-canvas.ts +122 -157
  134. package/src/kit/chart-legend.ts +229 -250
  135. package/src/kit/entries.ts +20 -0
  136. package/src/kit/motion.ts +90 -0
  137. package/src/kit/paints.ts +15 -0
  138. package/src/kit/shared.ts +782 -675
  139. package/src/kit/vertical-axis.ts +194 -224
  140. package/src/line-chart/chart-line.ts +181 -208
  141. package/src/line-chart/chart-region.ts +106 -135
  142. package/src/line-chart/horizontal-axis.ts +166 -202
  143. package/src/line-chart/index.ts +39 -39
  144. package/src/line-chart/line-chart.ts +585 -929
  145. package/src/line-chart/shared.ts +473 -459
  146. package/src/line-chart/x-scale.ts +367 -367
  147. package/src/nodes.ts +18 -20
  148. package/src/pie-chart/index.ts +37 -37
  149. package/src/pie-chart/pie-chart.ts +293 -381
  150. package/src/pie-chart/pie-legend.ts +169 -159
  151. package/src/pie-chart/pie-ring.ts +848 -903
  152. package/src/pie-chart/shared.ts +490 -490
  153. package/dist/browser/chunks/chunk-RMHQ6GYU.js +0 -2
  154. package/dist/browser/chunks/chunk-RMHQ6GYU.js.map +0 -7
@@ -1,310 +1,288 @@
1
- import {
2
- command,
3
- type AssetScope,
4
- Graphics2D,
5
- Node2D,
6
- Text,
7
- easeOut,
8
- fillOps,
9
- shadowOps,
10
- strokeOps,
11
- property,
12
- textOps,
13
- type Command,
14
- type CommandArgs,
15
- type Fill,
16
- type NodeConfig,
17
- type Node2DProps,
18
- type RenderContext2D,
19
- type Shadow,
20
- type ShadowResolved,
21
- type Stroke,
22
- type StrokeResolved,
23
- type TextStyle,
24
- } from "@motionscript/sdk"
25
-
26
- import { clampUnit, formatMarker } from "../kit/shared"
27
- import type { MarkerLabel } from "../kit/vertical-axis"
28
- import { DEFAULT_BAR_CHART_THEME } from "./shared"
29
- import { declarePaints } from "@motionscript/sdk/component"
30
-
31
- /**
32
- * One drawn column, already mapped into the plot's **centred pixel space**
33
- * (origin at the plot centre, y-up) by the parent {@link BarChart}.
34
- *
35
- * `from`/`to` are the two ends of the column rather than a base and a height,
36
- * for the same reason {@link BarSpan} is a span: it is the one shape that fits a
37
- * grouped bar, a stacked segment and a negative value without a special case.
38
- */
39
- export interface BarGeometry {
40
- /** Centre x of the column. */
41
- x: number
42
- /** Column width in px. */
43
- width: number
44
- /** Pixel y of the column's base — the baseline when grouped, the running stack top when stacked. */
45
- from: number
46
- /** Pixel y of its far end, at full {@link ChartBars.growth}. */
47
- to: number
48
- /** Whether the far end takes the theme's corner radius (the stack's cap). */
49
- round: boolean
50
- /** The value this column plots, for the figure printed on it. */
51
- value: number
52
- }
53
-
54
- /** Gap between a grouped column's end and the figure printed over it, in px. */
55
- const VALUE_GAP = 10
56
- /** A segment shorter than this many multiples of the type size prints no figure. */
57
- const VALUE_FIT = 1.6
58
- /** Fallback type size when the theme's value style leaves it unset or autofit. */
59
- const VALUE_SIZE = 24
60
-
61
- export interface ChartBarsProps extends Node2DProps {
62
- /** This series' columns, one per row of the chart's data. */
63
- bars: BarGeometry[]
64
- /** Pixel y of value 0 — where every column grows *from*. */
65
- baseline: number
66
- /** Column paint — this series' own, or the theme palette's entry for its index. */
67
- fill: Fill
68
- /** Optional outline on every column. */
69
- barStroke: Stroke
70
- /** Optional drop shadow on every column. */
71
- barShadow: Shadow
72
- /** Radius of the far-end corners, in px. */
73
- cornerRadius: number
74
- /**
75
- * Rise of every column in `[0, 1]`, measured **about the baseline**: 0
76
- * collapses them all onto it, 1 draws them at full length.
77
- *
78
- * Scaling about the baseline rather than about each column's own base is what
79
- * keeps a stack flush mid-animation — every segment's base and end travel by
80
- * the same factor, so the segments stay stacked instead of the upper ones
81
- * floating above the still-growing lower ones. It also means a negative
82
- * column grows *down* out of the same signal.
83
- */
84
- growth: number
85
- /**
86
- * Per-column opacity multiplier, by row index — how the chart's category
87
- * spotlight reaches a single column without touching the series it belongs
88
- * to. Missing entries read as 1.
89
- */
90
- dims: number[]
91
- /** Whether the figure each column plots is printed on it. */
92
- showValues: boolean
93
- /** Type style for those figures. */
94
- valueStyle: TextStyle
95
- /**
96
- * Whether this series draws at all — the wholesale switch behind
97
- * `BarChart.showSeries`/`hideSeries`, alongside the slot weight that animates
98
- * the reflow. Off skips the columns *and* their figures.
99
- */
100
- enabled: boolean
101
- }
102
-
103
- /**
104
- * One **series** of a {@link BarChart}, drawn as one command list: its column in
105
- * every category, plus the figures printed on them.
106
- *
107
- * A node per series rather than a node per bar, deliberately — it is the same
108
- * unit `ChartLine` is, which is what lets `showSeries`/`hideSeries`/
109
- * `spotlightSeries` mean exactly the same thing on a bar chart as on a line
110
- * chart: enable the node, or tween its opacity. The columns' *positions* come
111
- * from the parent (it owns the slot arithmetic both modes share), so this part
112
- * only has to know how to draw a span and grow it.
113
- *
114
- * The figures are real {@link Text} children rather than glyphs in the command
115
- * list, so they take the theme's type style, are measured by the layout engine,
116
- * and ride the node's own opacity — which is why hiding the series takes its
117
- * figures with it for free.
118
- */
119
- export class ChartBars extends Node2D<ChartBarsProps> {
120
- @property({ default: () => [] as BarGeometry[] }) declare bars: BarGeometry[]
121
- @property({ default: 0 }) declare baseline: number
122
- @property({
123
- default: [],
124
- mapper: fillOps.resolve,
125
- tween: fillOps.lerp,
126
- })
127
- declare fill: Fill
128
- @property({
129
- default: DEFAULT_BAR_CHART_THEME.bar.stroke,
130
- mapper: strokeOps.resolve,
131
- tween: strokeOps.lerp,
132
- })
133
- declare barStroke: Stroke
134
- @property({
135
- default: DEFAULT_BAR_CHART_THEME.bar.shadow,
136
- mapper: shadowOps.resolve,
137
- tween: shadowOps.lerp,
138
- })
139
- declare barShadow: Shadow
140
- @property({ default: DEFAULT_BAR_CHART_THEME.bar.cornerRadius })
141
- declare cornerRadius: number
142
- @property({ default: 1 }) declare growth: number
143
- @property({ default: () => [] as number[] }) declare dims: number[]
144
- @property({ default: false }) declare showValues: boolean
145
- @property({ default: DEFAULT_BAR_CHART_THEME.valueStyle })
146
- declare valueStyle: TextStyle
147
- @property({ default: true }) declare enabled: boolean
148
-
149
- /**
150
- * How a column's figure is written down — the same seam the two axes carry,
151
- * so a chart's value axis and the numbers printed on its bars can't end up
152
- * saying `$1,200` in one place and `1200` in the other.
153
- */
154
- labelFor: MarkerLabel = formatMarker
155
-
156
- constructor(props?: NodeConfig<ChartBars, ChartBarsProps>) {
157
- super(props)
158
-
159
- // One caption per column, built once (the row count is fixed at
160
- // construction, like an axis's marker count) and bound to the live geometry
161
- // so it travels with its column as the chart grows, rescales or reflows.
162
- if (!this.showValues) return
163
- for (let i = 0; i < this.bars.length; i++) {
164
- this.add(
165
- new Text({
166
- ...textOps.styleProps(this.valueStyle),
167
- text: () => this.captionAt(i),
168
- x: () => this.bars[i]?.x ?? 0,
169
- y: () => this.captionY(i),
170
- opacity: () => this.dimAt(i),
171
- })
172
- )
173
- }
174
- }
175
-
176
- /**
177
- * How far column `i` is dimmed — the chart's category spotlight, and the
178
- * series switch on top of it.
179
- *
180
- * The captions read this rather than being hidden by `renderSelf`'s guard:
181
- * they are real child nodes, so a `return` in the draw pass would leave them
182
- * on screen over a series that isn't there.
183
- */
184
- private dimAt(index: number): number {
185
- if (!this.enabled) return 0
186
- return clampUnit(this.dims[index] ?? 1)
187
- }
188
-
189
- /** The two ends of column `i` at the current {@link growth}. */
190
- private endsAt(index: number): { base: number; end: number } | null {
191
- const bar = this.bars[index]
192
- if (!bar) return null
193
- const g = clampUnit(this.growth)
194
- const base = this.baseline + (bar.from - this.baseline) * g
195
- const end = this.baseline + (bar.to - this.baseline) * g
196
- return { base, end }
197
- }
198
-
199
- /** The type size the figures are set at, guarding the autofit/unset cases. */
200
- private get valueSize(): number {
201
- const size = this.valueStyle.fontSize
202
- return typeof size === "number" ? size : VALUE_SIZE
203
- }
204
-
205
- /**
206
- * The figure printed on column `i`, or `""` where there is no room for it.
207
- *
208
- * Blanking the text rather than dropping the node keeps the caption list
209
- * index-aligned with the columns, so a chart that rescales into more room
210
- * simply starts printing again.
211
- */
212
- private captionAt(index: number): string {
213
- const bar = this.bars[index]
214
- if (!bar || bar.value === 0) return ""
215
- // An interior segment prints inside itself, so it has to be tall enough to
216
- // hold a line of type; a grouped column prints in the headroom above its
217
- // end, where the only constraint is the plot's own top.
218
- if (this.insideAt(index)) {
219
- const ends = this.endsAt(index)
220
- if (!ends) return ""
221
- if (Math.abs(ends.end - ends.base) < this.valueSize * VALUE_FIT) return ""
222
- }
223
- return this.labelFor(bar.value)
224
- }
225
-
226
- /** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */
227
- private insideAt(index: number): boolean {
228
- const bar = this.bars[index]
229
- return bar !== undefined && Math.abs(bar.from - this.baseline) > 0.5
230
- }
231
-
232
- /** Where column `i`'s figure sits, in the node's centred y-up space. */
233
- private captionY(index: number): number {
234
- const ends = this.endsAt(index)
235
- if (!ends) return 0
236
- if (this.insideAt(index)) return (ends.base + ends.end) / 2
237
- // Text is centred on its `y`, so clear the column's end by the gap plus half
238
- // a line — on whichever side of the baseline the column actually reached.
239
- const direction = ends.end >= ends.base ? 1 : -1
240
- return ends.end + direction * (VALUE_GAP + this.valueSize / 2)
241
- }
242
-
243
- /**
244
- * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
245
- * paint props it carries — see {@link declarePaints}. The column stroke and
246
- * shadow are named past the conventional slots, so they are passed explicitly.
247
- */
248
- override declareAssets(assets: AssetScope): void {
249
- super.declareAssets(assets)
250
- declarePaints(this, assets, {
251
- fills: (this.barShadow as ShadowResolved[]).map((shadow) => shadow.fill),
252
- strokes: [this.barStroke as StrokeResolved[]],
253
- })
254
- }
255
-
256
- protected renderSelf(ctx: RenderContext2D): void {
257
- if (!this.enabled) return
258
- for (let i = 0; i < this.bars.length; i++) {
259
- const bar = this.bars[i]
260
- const ends = this.endsAt(i)
261
- if (!ends || bar.width <= 0) continue
262
- const height = Math.abs(ends.end - ends.base)
263
- if (height <= 0) continue
264
-
265
- const radius = bar.round ? Math.min(this.cornerRadius, height) : 0
266
- // A column per `Graphics2D` rather than one list for the series: the
267
- // category spotlight dims individual columns, and opacity is a property
268
- // of the command list rather than of a shape within it.
269
- const g = new Graphics2D()
270
- const rect = g.rect({
271
- x: bar.x,
272
- y: (ends.base + ends.end) / 2,
273
- width: bar.width,
274
- height,
275
- // This node's space is y-up, but the renderer's corner-radius keys are
276
- // labelled in canvas y-down space — so a column reaching *up* rounds
277
- // through the `bottom*` keys, and one reaching down through the `top*`
278
- // ones. Only the far end rounds, which is what makes a stack read as a
279
- // single capped column.
280
- cornerRadius:
281
- radius > 0
282
- ? ends.end >= ends.base
283
- ? { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
284
- : { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }
285
- : 0,
286
- })
287
- rect.shadow(this.barShadow)
288
- rect.fill(this.fill)
289
- rect.stroke(this.barStroke)
290
- ctx.draw(g.opacity(this.dimAt(i)))
291
- }
292
- }
293
-
294
- /**
295
- * Raise every column from the baseline by tweening {@link growth} `0 → 1`.
296
- *
297
- * The mirror of `ChartLine.enter`: same signature, same contract that the
298
- * resting state is the finished one, so the chart arms this back to 0 before
299
- * animating rather than the node hiding itself at construction.
300
- */
301
- @command()
302
- enter(args: CommandArgs<Record<string, never>> & { duration: number }): Command<ChartBarsGrowth> {
303
- // No priming to 0 first: the value at `t` is a function of `t`, so a repeat
304
- // call re-raises by definition rather than by having reset something.
305
- return this.command<ChartBarsGrowth>((t) => ({ growth: t }), args.duration, args.easing ?? easeOut())
306
- }
307
- }
308
-
309
- /** The one signal {@link ChartBars.enter} moves. */
310
- type ChartBarsGrowth = { growth: number }
1
+ import {
2
+ Graphics2D,
3
+ Text,
4
+ Types,
5
+ component2d,
6
+ fillOps,
7
+ shadowOps,
8
+ strokeOps,
9
+ textOps,
10
+ type Fill,
11
+ type FillResolved,
12
+ type NodeOf,
13
+ // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
14
+ type Node2D as _Node2D,
15
+ type Node2DProps,
16
+ type Shadow,
17
+ type ShadowResolved,
18
+ type Stroke,
19
+ type StrokeResolved,
20
+ type TextStyle,
21
+ } from "@motionscript/sdk"
22
+
23
+ import { fresh } from "../kit/entries"
24
+ import { frameEase } from "../kit/motion"
25
+ import { loadPaints } from "../kit/paints"
26
+ import { clampUnit, formatMarker } from "../kit/shared"
27
+ import { DEFAULT_BAR_CHART_THEME } from "./shared"
28
+
29
+ /**
30
+ * One drawn column, already mapped into the plot's **centred pixel space**
31
+ * (origin at the plot centre, y-up) by the parent {@link BarChart}.
32
+ *
33
+ * `from`/`to` are the two ends of the column rather than a base and a height,
34
+ * for the same reason {@link BarSpan} is a span: it is the one shape that fits a
35
+ * grouped bar, a stacked segment and a negative value without a special case.
36
+ */
37
+ export interface BarGeometry {
38
+ /** Centre x of the column. */
39
+ x: number
40
+ /** Column width in px. */
41
+ width: number
42
+ /** Pixel y of the column's base — the baseline when grouped, the running stack top when stacked. */
43
+ from: number
44
+ /** Pixel y of its far end, at full {@link ChartBarsProps.growth}. */
45
+ to: number
46
+ /** Whether the far end takes the theme's corner radius (the stack's cap). */
47
+ round: boolean
48
+ /** The value this column plots, for the figure printed on it. */
49
+ value: number
50
+ /**
51
+ * The figure printed on it, written as the chart's value axis writes its
52
+ * ticks, so the two cannot disagree. Absent, the bare marker reading of
53
+ * {@link value}.
54
+ */
55
+ caption?: string
56
+ }
57
+
58
+ /** Gap between a grouped column's end and the figure printed over it, in px. */
59
+ const VALUE_GAP = 10
60
+ /** A segment shorter than this many multiples of the type size prints no figure. */
61
+ const VALUE_FIT = 1.6
62
+ /** Fallback type size when the theme's value style leaves it unset or autofit. */
63
+ const VALUE_SIZE = 24
64
+
65
+ export interface ChartBarsProps extends Node2DProps {
66
+ /** This series' columns, one per row of the chart's data. */
67
+ bars: BarGeometry[]
68
+ /** Pixel y of value 0 — where every column grows *from*. */
69
+ baseline: number
70
+ /** Column paint — this series' own, or the theme palette's entry for its index. */
71
+ fill: Fill
72
+ /** Optional outline on every column. */
73
+ barStroke: Stroke
74
+ /** Optional drop shadow on every column. */
75
+ barShadow: Shadow
76
+ /** Radius of the far-end corners, in px. */
77
+ cornerRadius: number
78
+ /**
79
+ * Rise of every column in `[0, 1]`, measured **about the baseline**: 0
80
+ * collapses them all onto it, 1 draws them at full length.
81
+ *
82
+ * Scaling about the baseline rather than about each column's own base is what
83
+ * keeps a stack flush mid-animation — every segment's base and end travel by
84
+ * the same factor, so the segments stay stacked instead of the upper ones
85
+ * floating above the still-growing lower ones. It also means a negative
86
+ * column grows *down* out of the same signal.
87
+ */
88
+ growth: number
89
+ /**
90
+ * Per-column opacity multiplier, by row index — how the chart's category
91
+ * spotlight reaches a single column without touching the series it belongs
92
+ * to. Missing entries read as 1.
93
+ */
94
+ dims: number[]
95
+ /** Whether the figure each column plots is printed on it. */
96
+ showValues: boolean
97
+ /** Type style for those figures. */
98
+ valueStyle: TextStyle
99
+ /**
100
+ * Whether this series draws at all — the wholesale switch behind
101
+ * `BarChart.showSeries`/`hideSeries`, alongside the slot weight that animates
102
+ * the reflow. Off skips the columns *and* their figures.
103
+ */
104
+ enabled: boolean
105
+ }
106
+
107
+ /** What the column geometry reads. */
108
+ interface Columns {
109
+ readonly bars: readonly BarGeometry[]
110
+ readonly baseline: number
111
+ readonly growth: number
112
+ readonly dims: readonly number[]
113
+ readonly enabled: boolean
114
+ readonly valueStyle: TextStyle
115
+ }
116
+
117
+ /**
118
+ * How far column `i` is dimmed — the chart's category spotlight, and the series
119
+ * switch on top of it.
120
+ *
121
+ * The captions read this rather than being hidden by `render`'s guard: they are
122
+ * real child nodes, so a `return` in the draw would leave them on screen over a
123
+ * series that isn't there.
124
+ */
125
+ function dimAt(s: Columns, index: number): number {
126
+ if (!s.enabled) return 0
127
+ return clampUnit(s.dims[index] ?? 1)
128
+ }
129
+
130
+ /** The two ends of column `i` at the current growth. */
131
+ function endsAt(s: Columns, index: number): { base: number; end: number } | null {
132
+ const bar = s.bars[index]
133
+ if (!bar) return null
134
+ const g = clampUnit(s.growth)
135
+ return { base: s.baseline + (bar.from - s.baseline) * g, end: s.baseline + (bar.to - s.baseline) * g }
136
+ }
137
+
138
+ /** The type size the figures are set at, guarding the autofit/unset cases. */
139
+ function valueSize(s: Columns): number {
140
+ const size = s.valueStyle.fontSize
141
+ return typeof size === "number" ? size : VALUE_SIZE
142
+ }
143
+
144
+ /** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */
145
+ function insideAt(s: Columns, index: number): boolean {
146
+ const bar = s.bars[index]
147
+ return bar !== undefined && Math.abs(bar.from - s.baseline) > 0.5
148
+ }
149
+
150
+ /**
151
+ * The figure printed on column `i`, or `""` where there is no room for it.
152
+ *
153
+ * Blanking the text rather than dropping the node keeps the caption list
154
+ * index-aligned with the columns, so a chart that rescales into more room simply
155
+ * starts printing again.
156
+ */
157
+ function captionAt(s: Columns, index: number): string {
158
+ const bar = s.bars[index]
159
+ if (!bar || bar.value === 0) return ""
160
+ // An interior segment prints inside itself, so it has to be tall enough to
161
+ // hold a line of type; a grouped column prints in the headroom above its end,
162
+ // where the only constraint is the plot's own top.
163
+ if (insideAt(s, index)) {
164
+ const ends = endsAt(s, index)
165
+ if (!ends) return ""
166
+ if (Math.abs(ends.end - ends.base) < valueSize(s) * VALUE_FIT) return ""
167
+ }
168
+ return bar.caption ?? formatMarker(bar.value)
169
+ }
170
+
171
+ /** Where column `i`'s figure sits, in the node's centred y-up space. */
172
+ function captionY(s: Columns, index: number): number {
173
+ const ends = endsAt(s, index)
174
+ if (!ends) return 0
175
+ if (insideAt(s, index)) return (ends.base + ends.end) / 2
176
+ // Text is centred on its `y`, so clear the column's end by the gap plus half a
177
+ // line — on whichever side of the baseline the column actually reached.
178
+ const direction = ends.end >= ends.base ? 1 : -1
179
+ return ends.end + direction * (VALUE_GAP + valueSize(s) / 2)
180
+ }
181
+
182
+ /**
183
+ * One **series** of a {@link BarChart}, drawn as one command list: its column in
184
+ * every category, plus the figures printed on them.
185
+ *
186
+ * A node per series rather than a node per bar, deliberately — it is the same
187
+ * unit `ChartLine` is, which is what lets `showSeries`/`hideSeries`/
188
+ * `spotlightSeries` mean exactly the same thing on a bar chart as on a line
189
+ * chart: enable the node, or tween its opacity. The columns' *positions* come
190
+ * from the parent (it owns the slot arithmetic both modes share), so this part
191
+ * only has to know how to draw a span and grow it.
192
+ *
193
+ * The figures are real {@link Text} children rather than glyphs in the command
194
+ * list, so they take the theme's type style, are measured by the layout engine,
195
+ * and ride the node's own opacity — which is why hiding the series takes its
196
+ * figures with it for free.
197
+ */
198
+ export const ChartBars = component2d({
199
+ key: "chartBars",
200
+ state: {
201
+ bars: fresh<BarGeometry[]>(() => []),
202
+ baseline: 0,
203
+ fill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
204
+ barStroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_BAR_CHART_THEME.bar.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
205
+ barShadow: Types.custom<Shadow, ShadowResolved[]>(DEFAULT_BAR_CHART_THEME.bar.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),
206
+ cornerRadius: DEFAULT_BAR_CHART_THEME.bar.cornerRadius,
207
+ growth: 1,
208
+ dims: fresh<number[]>(() => []),
209
+ showValues: false,
210
+ valueStyle: Types.custom<TextStyle>(DEFAULT_BAR_CHART_THEME.valueStyle),
211
+ enabled: true,
212
+ },
213
+ // A plain node filled its parent; one made of its figures would hug them.
214
+ defaults: { width: "fill", height: "fill" },
215
+ // One caption per column, built once (the row count is fixed at construction,
216
+ // like an axis's marker count) and bound to the live geometry so it travels
217
+ // with its column as the chart grows, rescales or reflows.
218
+ children: (state) => {
219
+ if (!state.showValues) return []
220
+ return state.bars.map((_, i) =>
221
+ Text({
222
+ ...textOps.styleProps(state.valueStyle),
223
+ text: () => captionAt(state, i),
224
+ x: () => state.bars[i]?.x ?? 0,
225
+ y: () => captionY(state, i),
226
+ opacity: () => dimAt(state, i),
227
+ })
228
+ )
229
+ },
230
+ // The column stroke and shadow are named past the conventional slots, so they are named here.
231
+ load: (loader, state) =>
232
+ loadPaints(loader, { fills: [state.fill], shadows: [state.barShadow], strokes: [state.barStroke] }),
233
+ commands: {
234
+ /**
235
+ * Raise every column from the baseline by tweening `growth` `0 → 1`.
236
+ *
237
+ * The mirror of `ChartLine.enter`: same contract that the resting state is
238
+ * the finished one, so the chart arms this back to 0 before animating rather
239
+ * than the node hiding itself at construction. No priming to 0 first: the
240
+ * value at `t` is a function of `t`, so a repeat call re-raises by definition.
241
+ */
242
+ enter: (timeline) => {
243
+ const ease = frameEase(timeline)
244
+ return timeline.animate((t) => ({ growth: ease(t) }))
245
+ },
246
+ },
247
+ render: (ctx, state) => {
248
+ if (state.enabled) {
249
+ for (let i = 0; i < state.bars.length; i++) {
250
+ const bar = state.bars[i]
251
+ const ends = endsAt(state, i)
252
+ if (!ends || bar.width <= 0) continue
253
+ const height = Math.abs(ends.end - ends.base)
254
+ if (height <= 0) continue
255
+
256
+ const radius = bar.round ? Math.min(state.cornerRadius, height) : 0
257
+ // A column per `Graphics2D` rather than one list for the series: the
258
+ // category spotlight dims individual columns, and opacity is a property
259
+ // of the command list rather than of a shape within it.
260
+ const g = new Graphics2D()
261
+ const rect = g.rect({
262
+ x: bar.x,
263
+ y: (ends.base + ends.end) / 2,
264
+ width: bar.width,
265
+ height,
266
+ // This node's space is y-up, but the renderer's corner-radius keys are
267
+ // labelled in canvas y-down space — so a column reaching *up* rounds
268
+ // through the `bottom*` keys, and one reaching down through the `top*`
269
+ // ones. Only the far end rounds, which is what makes a stack read as a
270
+ // single capped column.
271
+ cornerRadius:
272
+ radius > 0
273
+ ? ends.end >= ends.base
274
+ ? { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
275
+ : { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }
276
+ : 0,
277
+ })
278
+ rect.shadow(state.barShadow)
279
+ rect.fill(state.fill)
280
+ rect.stroke(state.barStroke)
281
+ ctx.draw(g.opacity(dimAt(state, i)))
282
+ }
283
+ }
284
+ ctx.children()
285
+ },
286
+ })
287
+
288
+ export type ChartBars = NodeOf<typeof ChartBars>