@motionscript/charts 0.1.0-alpha.0 → 0.1.0-alpha.5

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 (124) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +85 -0
  5. package/dist/bar-chart/bar-chart.d.ts +33 -19
  6. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  7. package/dist/bar-chart/bar-chart.js +68 -64
  8. package/dist/bar-chart/bar-chart.js.map +1 -1
  9. package/dist/bar-chart/category-axis.d.ts +2 -2
  10. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  11. package/dist/bar-chart/category-axis.js +2 -2
  12. package/dist/bar-chart/category-axis.js.map +1 -1
  13. package/dist/bar-chart/chart-bars.d.ts +1 -1
  14. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  15. package/dist/bar-chart/chart-bars.js +2 -2
  16. package/dist/bar-chart/chart-bars.js.map +1 -1
  17. package/dist/bar-chart/index.js.map +1 -1
  18. package/dist/bar-chart/shared.d.ts +4 -4
  19. package/dist/bar-chart/shared.d.ts.map +1 -1
  20. package/dist/bar-chart/shared.js +4 -4
  21. package/dist/bar-chart/shared.js.map +1 -1
  22. package/dist/browser/chunks/chunk-5ZPBV6BU.js +2 -0
  23. package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +7 -0
  24. package/dist/browser/index.js +1 -1
  25. package/dist/browser/index.js.map +3 -3
  26. package/dist/browser/kit.js +1 -1
  27. package/dist/browser/manifest.json +4 -3
  28. package/dist/engine.d.ts +1 -1
  29. package/dist/engine.d.ts.map +1 -1
  30. package/dist/engine.js +1 -1
  31. package/dist/engine.js.map +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/kit/chart-border.d.ts +1 -1
  34. package/dist/kit/chart-border.d.ts.map +1 -1
  35. package/dist/kit/chart-border.js +2 -2
  36. package/dist/kit/chart-border.js.map +1 -1
  37. package/dist/kit/chart-canvas.d.ts +1 -1
  38. package/dist/kit/chart-canvas.d.ts.map +1 -1
  39. package/dist/kit/chart-canvas.js +2 -2
  40. package/dist/kit/chart-canvas.js.map +1 -1
  41. package/dist/kit/chart-legend.d.ts +6 -27
  42. package/dist/kit/chart-legend.d.ts.map +1 -1
  43. package/dist/kit/chart-legend.js +33 -57
  44. package/dist/kit/chart-legend.js.map +1 -1
  45. package/dist/kit/shared.d.ts +31 -8
  46. package/dist/kit/shared.d.ts.map +1 -1
  47. package/dist/kit/shared.js +67 -11
  48. package/dist/kit/shared.js.map +1 -1
  49. package/dist/kit/vertical-axis.d.ts +2 -2
  50. package/dist/kit/vertical-axis.d.ts.map +1 -1
  51. package/dist/kit/vertical-axis.js +2 -2
  52. package/dist/kit/vertical-axis.js.map +1 -1
  53. package/dist/line-chart/chart-line.d.ts +3 -3
  54. package/dist/line-chart/chart-line.d.ts.map +1 -1
  55. package/dist/line-chart/chart-line.js +2 -2
  56. package/dist/line-chart/chart-line.js.map +1 -1
  57. package/dist/line-chart/chart-region.d.ts +1 -1
  58. package/dist/line-chart/chart-region.d.ts.map +1 -1
  59. package/dist/line-chart/chart-region.js +2 -2
  60. package/dist/line-chart/chart-region.js.map +1 -1
  61. package/dist/line-chart/horizontal-axis.d.ts +2 -2
  62. package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
  63. package/dist/line-chart/horizontal-axis.js +2 -2
  64. package/dist/line-chart/horizontal-axis.js.map +1 -1
  65. package/dist/line-chart/index.js.map +1 -1
  66. package/dist/line-chart/line-chart.d.ts +25 -9
  67. package/dist/line-chart/line-chart.d.ts.map +1 -1
  68. package/dist/line-chart/line-chart.js +66 -43
  69. package/dist/line-chart/line-chart.js.map +1 -1
  70. package/dist/line-chart/shared.d.ts +4 -4
  71. package/dist/line-chart/shared.d.ts.map +1 -1
  72. package/dist/line-chart/shared.js +20 -5
  73. package/dist/line-chart/shared.js.map +1 -1
  74. package/dist/line-chart/x-scale.d.ts +2 -2
  75. package/dist/line-chart/x-scale.d.ts.map +1 -1
  76. package/dist/line-chart/x-scale.js +2 -2
  77. package/dist/line-chart/x-scale.js.map +1 -1
  78. package/dist/nodes.js.map +1 -1
  79. package/dist/pie-chart/index.js.map +1 -1
  80. package/dist/pie-chart/pie-chart.d.ts +16 -10
  81. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  82. package/dist/pie-chart/pie-chart.js +15 -16
  83. package/dist/pie-chart/pie-chart.js.map +1 -1
  84. package/dist/pie-chart/pie-legend.d.ts +2 -2
  85. package/dist/pie-chart/pie-legend.d.ts.map +1 -1
  86. package/dist/pie-chart/pie-legend.js +6 -6
  87. package/dist/pie-chart/pie-legend.js.map +1 -1
  88. package/dist/pie-chart/pie-ring.d.ts +14 -5
  89. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  90. package/dist/pie-chart/pie-ring.js +45 -15
  91. package/dist/pie-chart/pie-ring.js.map +1 -1
  92. package/dist/pie-chart/shared.d.ts +4 -4
  93. package/dist/pie-chart/shared.d.ts.map +1 -1
  94. package/dist/pie-chart/shared.js +4 -4
  95. package/dist/pie-chart/shared.js.map +1 -1
  96. package/package.json +6 -4
  97. package/registry.json +23 -23
  98. package/src/bar-chart/bar-chart.ts +820 -804
  99. package/src/bar-chart/category-axis.ts +177 -177
  100. package/src/bar-chart/chart-bars.ts +310 -310
  101. package/src/bar-chart/index.ts +31 -31
  102. package/src/bar-chart/shared.ts +354 -354
  103. package/src/engine.ts +26 -26
  104. package/src/index.ts +5 -5
  105. package/src/kit/chart-border.ts +61 -61
  106. package/src/kit/chart-canvas.ts +157 -157
  107. package/src/kit/chart-legend.ts +245 -250
  108. package/src/kit/shared.ts +733 -675
  109. package/src/kit/vertical-axis.ts +224 -224
  110. package/src/line-chart/chart-line.ts +208 -208
  111. package/src/line-chart/chart-region.ts +135 -135
  112. package/src/line-chart/horizontal-axis.ts +202 -202
  113. package/src/line-chart/index.ts +39 -39
  114. package/src/line-chart/line-chart.ts +961 -894
  115. package/src/line-chart/shared.ts +473 -459
  116. package/src/line-chart/x-scale.ts +367 -367
  117. package/src/nodes.ts +20 -20
  118. package/src/pie-chart/index.ts +37 -37
  119. package/src/pie-chart/pie-chart.ts +383 -374
  120. package/src/pie-chart/pie-legend.ts +159 -159
  121. package/src/pie-chart/pie-ring.ts +935 -903
  122. package/src/pie-chart/shared.ts +490 -490
  123. package/dist/browser/chunks/chunk-ZMBOBHYL.js +0 -2
  124. package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +0 -7
@@ -1,310 +1,310 @@
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/core"
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/core/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
+ 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 }