@motionscript/charts 0.1.0-alpha.8 → 0.1.0

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 (76) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +99 -99
  5. package/dist/bar-chart/bar-chart.js.map +1 -1
  6. package/dist/bar-chart/category-axis.js.map +1 -1
  7. package/dist/bar-chart/chart-bars.js.map +1 -1
  8. package/dist/bar-chart/index.js.map +1 -1
  9. package/dist/bar-chart/shared.js.map +1 -1
  10. package/dist/browser/chunks/{chunk-XROBIRQK.js → chunk-TJNHLQGV.js} +1 -1
  11. package/dist/browser/chunks/chunk-TJNHLQGV.js.map +7 -0
  12. package/dist/browser/index.js +1 -1
  13. package/dist/browser/index.js.map +1 -1
  14. package/dist/browser/kit.js +1 -1
  15. package/dist/browser/manifest.json +1 -1
  16. package/dist/engine.d.ts +1 -1
  17. package/dist/engine.js +1 -1
  18. package/dist/engine.js.map +1 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/kit/cartesian.js.map +1 -1
  21. package/dist/kit/chart-border.js.map +1 -1
  22. package/dist/kit/chart-canvas.js.map +1 -1
  23. package/dist/kit/chart-legend.js.map +1 -1
  24. package/dist/kit/entries.js.map +1 -1
  25. package/dist/kit/motion.js.map +1 -1
  26. package/dist/kit/paints.js.map +1 -1
  27. package/dist/kit/shared.js.map +1 -1
  28. package/dist/kit/vertical-axis.js.map +1 -1
  29. package/dist/line-chart/chart-line.js.map +1 -1
  30. package/dist/line-chart/chart-region.js.map +1 -1
  31. package/dist/line-chart/horizontal-axis.js.map +1 -1
  32. package/dist/line-chart/index.d.ts +1 -1
  33. package/dist/line-chart/index.js +1 -1
  34. package/dist/line-chart/index.js.map +1 -1
  35. package/dist/line-chart/line-chart.js.map +1 -1
  36. package/dist/line-chart/shared.js.map +1 -1
  37. package/dist/line-chart/x-scale.js.map +1 -1
  38. package/dist/nodes.js.map +1 -1
  39. package/dist/pie-chart/index.js.map +1 -1
  40. package/dist/pie-chart/pie-chart.js.map +1 -1
  41. package/dist/pie-chart/pie-legend.js.map +1 -1
  42. package/dist/pie-chart/pie-ring.js.map +1 -1
  43. package/dist/pie-chart/shared.js.map +1 -1
  44. package/package.json +4 -4
  45. package/registry.json +23 -23
  46. package/src/bar-chart/bar-chart.ts +542 -542
  47. package/src/bar-chart/category-axis.ts +162 -162
  48. package/src/bar-chart/chart-bars.ts +287 -287
  49. package/src/bar-chart/index.ts +31 -31
  50. package/src/bar-chart/shared.ts +354 -354
  51. package/src/engine.ts +28 -28
  52. package/src/index.ts +5 -5
  53. package/src/kit/cartesian.ts +81 -81
  54. package/src/kit/chart-border.ts +50 -50
  55. package/src/kit/chart-canvas.ts +122 -122
  56. package/src/kit/chart-legend.ts +229 -229
  57. package/src/kit/entries.ts +20 -20
  58. package/src/kit/motion.ts +90 -90
  59. package/src/kit/paints.ts +15 -15
  60. package/src/kit/shared.ts +782 -782
  61. package/src/kit/vertical-axis.ts +194 -194
  62. package/src/line-chart/chart-line.ts +156 -156
  63. package/src/line-chart/chart-region.ts +90 -90
  64. package/src/line-chart/horizontal-axis.ts +151 -151
  65. package/src/line-chart/index.ts +39 -39
  66. package/src/line-chart/line-chart.ts +598 -598
  67. package/src/line-chart/shared.ts +473 -473
  68. package/src/line-chart/x-scale.ts +382 -382
  69. package/src/nodes.ts +18 -18
  70. package/src/pie-chart/index.ts +37 -37
  71. package/src/pie-chart/pie-chart.ts +296 -296
  72. package/src/pie-chart/pie-legend.ts +169 -169
  73. package/src/pie-chart/pie-ring.ts +848 -848
  74. package/src/pie-chart/shared.ts +496 -496
  75. package/dist/browser/chunks/chunk-XROBIRQK.js.map +0 -7
  76. package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
@@ -1,287 +1,287 @@
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
- // Corner keys are visual (the renderer keeps them in y-up author space),
267
- // so a column reaching *up* rounds its `top*` corners and one reaching
268
- // down its `bottom*` ones. Only the far end rounds, which is what makes
269
- // a stack read as a single capped column.
270
- cornerRadius:
271
- radius > 0
272
- ? ends.end >= ends.base
273
- ? { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }
274
- : { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
275
- : 0,
276
- })
277
- rect.shadow(state.barShadow)
278
- rect.fill(state.fill)
279
- rect.stroke(state.barStroke)
280
- ctx.draw(g.opacity(dimAt(state, i)))
281
- }
282
- }
283
- ctx.children()
284
- },
285
- })
286
-
287
- export type ChartBars = NodeOf<typeof ChartBars>
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
+ // Corner keys are visual (the renderer keeps them in y-up author space),
267
+ // so a column reaching *up* rounds its `top*` corners and one reaching
268
+ // down its `bottom*` ones. Only the far end rounds, which is what makes
269
+ // a stack read as a single capped column.
270
+ cornerRadius:
271
+ radius > 0
272
+ ? ends.end >= ends.base
273
+ ? { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }
274
+ : { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
275
+ : 0,
276
+ })
277
+ rect.shadow(state.barShadow)
278
+ rect.fill(state.fill)
279
+ rect.stroke(state.barStroke)
280
+ ctx.draw(g.opacity(dimAt(state, i)))
281
+ }
282
+ }
283
+ ctx.children()
284
+ },
285
+ })
286
+
287
+ export type ChartBars = NodeOf<typeof ChartBars>
@@ -1,31 +1,31 @@
1
- /**
2
- * The bar chart: grouped or stacked columns off a category axis.
3
- *
4
- * Shares the plot panel, the value axis and the legend with the line chart —
5
- * which is what lets a scene swap one for the other without re-theming — and
6
- * brings its own category axis, since a band scale is not a continuous one.
7
- */
8
- export { BarChart, barChartDomain } from "./bar-chart"
9
- export type { BarChartDomain, BarChartProps, BarChartValues } from "./bar-chart"
10
- export { ChartBars } from "./chart-bars"
11
- export type { BarGeometry, ChartBarsProps } from "./chart-bars"
12
- export { CategoryAxis } from "./category-axis"
13
- export type { CategoryAxisProps } from "./category-axis"
14
- export {
15
- DEFAULT_BAR_CHART_THEME,
16
- barCategories,
17
- barSpans,
18
- barValueExtent,
19
- lerpBarChartTheme,
20
- resolveBarChartTheme,
21
- resolveBarFill,
22
- resolveBarScale,
23
- stackCapIndex,
24
- } from "./shared"
25
- export type {
26
- BarChartConfig,
27
- BarChartTheme,
28
- BarSeries,
29
- BarSpan,
30
- BarStyleConfig,
31
- } from "./shared"
1
+ /**
2
+ * The bar chart: grouped or stacked columns off a category axis.
3
+ *
4
+ * Shares the plot panel, the value axis and the legend with the line chart —
5
+ * which is what lets a scene swap one for the other without re-theming — and
6
+ * brings its own category axis, since a band scale is not a continuous one.
7
+ */
8
+ export { BarChart, barChartDomain } from "./bar-chart"
9
+ export type { BarChartDomain, BarChartProps, BarChartValues } from "./bar-chart"
10
+ export { ChartBars } from "./chart-bars"
11
+ export type { BarGeometry, ChartBarsProps } from "./chart-bars"
12
+ export { CategoryAxis } from "./category-axis"
13
+ export type { CategoryAxisProps } from "./category-axis"
14
+ export {
15
+ DEFAULT_BAR_CHART_THEME,
16
+ barCategories,
17
+ barSpans,
18
+ barValueExtent,
19
+ lerpBarChartTheme,
20
+ resolveBarChartTheme,
21
+ resolveBarFill,
22
+ resolveBarScale,
23
+ stackCapIndex,
24
+ } from "./shared"
25
+ export type {
26
+ BarChartConfig,
27
+ BarChartTheme,
28
+ BarSeries,
29
+ BarSpan,
30
+ BarStyleConfig,
31
+ } from "./shared"