@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,354 +1,354 @@
1
- /**
2
- * Types and pure helpers specific to the {@link BarChart} family: how a row's
3
- * values become the spans its columns cover, how the value axis is scaled around
4
- * them, and the theme the bars add to the shared chart vocabulary.
5
- *
6
- * Everything a line or a pie chart would want too — what a series is, the axis
7
- * domain model, the theme leaf resolvers, the legend and plot-panel branches —
8
- * lives in `../kit/shared.ts` and is imported here.
9
- *
10
- * Kept renderer-agnostic (no `Node` imports) so a host's inspector mapping
11
- * can build a partial {@link BarChartTheme} without
12
- * pulling the components in.
13
- */
14
-
15
- import {
16
- fillOps,
17
- lerpNumber,
18
- shadowOps,
19
- strokeOps,
20
- textOps,
21
- type DataRecord,
22
- type Fill,
23
- type Shadow,
24
- type ShadowResolved,
25
- type Stroke,
26
- type StrokeResolved,
27
- type TextStyle,
28
- } from "@motionscript/sdk"
29
-
30
- import {
31
- CHART_PALETTE,
32
- DEFAULT_LEGEND,
33
- DEFAULT_PLOT_AREA,
34
- DEFAULT_SERIES_FILLS,
35
- DEFAULT_X_AXIS,
36
- DEFAULT_Y_AXIS,
37
- SPOTLIGHT_DIM,
38
- lerpAxisConfig,
39
- lerpLegendConfig,
40
- lerpPlotArea,
41
- lerpSeriesFills,
42
- niceStep,
43
- num,
44
- resolveAxisConfig,
45
- resolveLegendConfig,
46
- resolvePlotArea,
47
- resolveSeriesFills,
48
- text,
49
- type AxisConfig,
50
- type ChartSeries,
51
- type LegendConfig,
52
- type LegendTheme,
53
- type PlotAreaConfig,
54
- type Range,
55
- } from "../kit/shared"
56
-
57
- /**
58
- * One series in a {@link BarChart}: a column of the table drawn as one bar per
59
- * row — side by side with its siblings within each category's slot, or stacked
60
- * on top of them.
61
- *
62
- * The family-wide {@link ChartSeries} under a name that says what it draws here.
63
- */
64
- export type BarSeries = ChartSeries
65
-
66
- /**
67
- * One drawn bar's span in **data units**: the value its base sits at and the
68
- * value its far end reaches.
69
- *
70
- * A span rather than a height because that is the one shape both modes fit. A
71
- * grouped bar runs from zero to its value; a stacked segment runs from the
72
- * running total below it to that total plus its own value. Negative values fall
73
- * out of the same arithmetic — the span simply runs the other way — so a chart
74
- * with a loss in it needs no special case, only a baseline that isn't the floor.
75
- */
76
- export interface BarSpan {
77
- /** Value the bar's base sits at — 0 when grouped, the running total when stacked. */
78
- from: number
79
- /** Value its far end reaches. */
80
- to: number
81
- /** The value this bar plots — `to - from`, kept for the figure printed on it. */
82
- value: number
83
- }
84
-
85
- /**
86
- * The spans one row's bars cover, one per series, in series order.
87
- *
88
- * Stacking is a running sum in the order the series are listed, which is also
89
- * the order they're drawn: series 0 sits on the baseline and each later one
90
- * rides on what's below it. A zero-valued series still yields a (degenerate)
91
- * span so the returned list stays index-aligned with the series list.
92
- */
93
- export function barSpans(
94
- row: DataRecord,
95
- series: BarSeries[],
96
- stacked: boolean
97
- ): BarSpan[] {
98
- let running = 0
99
- return series.map((s) => {
100
- const value = num(row, s.field)
101
- if (!stacked) return { from: 0, to: value, value }
102
- const from = running
103
- running += value
104
- return { from, to: running, value }
105
- })
106
- }
107
-
108
- /**
109
- * The index of the series whose segment ends a row's stack — the one that gets
110
- * the rounded cap, so a stack reads as one capped column rather than a pile of
111
- * separate boxes. `-1` when every value in the row is zero.
112
- *
113
- * Read off the *last non-zero* series rather than the last one listed, so a
114
- * series that happens to be 0 in this row doesn't take the cap and leave the
115
- * segment below it square.
116
- */
117
- export function stackCapIndex(row: DataRecord, series: BarSeries[]): number {
118
- let last = -1
119
- for (let i = 0; i < series.length; i++) {
120
- if (num(row, series[i].field) !== 0) last = i
121
- }
122
- return last
123
- }
124
-
125
- /** The captions beneath the baseline: one per row, read off the category column. */
126
- export function barCategories(data: DataRecord[], field: string): string[] {
127
- return data.map((row) => text(row, field))
128
- }
129
-
130
- /**
131
- * The `[min, max]` of everything the chart draws, **zero always included**.
132
- *
133
- * Zero is in the range whatever the data says, because a bar chart's baseline is
134
- * zero: a scale starting at 40 draws a column three times its neighbour for a
135
- * 10% difference, which is the classic way to lie with one. A chart that really
136
- * does want a cropped axis says so with an explicit range.
137
- */
138
- export function barValueExtent(
139
- data: DataRecord[],
140
- series: BarSeries[],
141
- stacked: boolean
142
- ): Range {
143
- let lo = 0
144
- let hi = 0
145
- for (const row of data) {
146
- for (const span of barSpans(row, series, stacked)) {
147
- lo = Math.min(lo, span.from, span.to)
148
- hi = Math.max(hi, span.from, span.to)
149
- }
150
- }
151
- return lo < hi ? [lo, hi] : [0, 1]
152
- }
153
-
154
- /**
155
- * Effective value scale: the explicit `range`, else the data's extent nudged out
156
- * to tidy step bounds so the markers read as round numbers.
157
- *
158
- * `divisions` is how many segments the auto-range is stepped into (one fewer
159
- * than the marker count), so the ticks the axis draws land on the bounds this
160
- * picks.
161
- */
162
- export function resolveBarScale(
163
- data: DataRecord[],
164
- series: BarSeries[],
165
- stacked: boolean,
166
- range: Range | undefined,
167
- divisions: number
168
- ): Range {
169
- if (range) return range
170
- const [lo, hi] = barValueExtent(data, series, stacked)
171
- const segments = Math.max(1, Math.round(divisions))
172
- const step = niceStep((hi - lo) / segments || 1)
173
- return [Math.floor(lo / step) * step, Math.ceil(hi / step) * step]
174
- }
175
-
176
- // ---- Theme ---------------------------------------------------------------
177
-
178
- /** How the columns themselves are drawn and how the slot is divided up. */
179
- export interface BarStyleConfig {
180
- /**
181
- * Radius of a column's far-end corners, in px. Only the outer end rounds — an
182
- * interior stacked segment stays square, so the stack reads as one column.
183
- */
184
- cornerRadius: number
185
- /**
186
- * Fraction of its category's slot the bars fill, `[0, 1]`. The remainder is
187
- * the channel between one category and the next, which is what makes a bar
188
- * chart read as groups rather than as a solid block.
189
- */
190
- slotFill: number
191
- /** Gap between the grouped bars *within* one slot, in px. Ignored when stacked. */
192
- groupGap: number
193
- /** Optional outline on every column. */
194
- stroke: Stroke
195
- /** Optional drop shadow on every column. */
196
- shadow: Shadow
197
- }
198
-
199
- /** A {@link BarChart}'s full, resolved visual theme. */
200
- export interface BarChartConfig {
201
- /**
202
- * Default per-series bar paint, cycled by series index (`i % length`). A
203
- * series' own `fill`, when set, replaces its entry outright.
204
- */
205
- seriesFills: Fill[]
206
- bar: BarStyleConfig
207
- /** The category axis beneath the baseline. */
208
- xAxis: AxisConfig
209
- /** The numeric value axis up the left. */
210
- yAxis: AxisConfig
211
- plotArea: PlotAreaConfig
212
- legend: LegendConfig
213
- /** The figure printed on each column, when the chart prints them. */
214
- valueStyle: TextStyle
215
- /** How far everything *outside* a spotlit series or category fades. */
216
- spotlight: { dimOpacity: number }
217
- }
218
-
219
- /** Author-facing partial theme — every branch optional and deep-merged. */
220
- export type BarChartTheme = {
221
- seriesFills?: Fill[]
222
- bar?: Partial<BarStyleConfig>
223
- xAxis?: Partial<AxisConfig>
224
- yAxis?: Partial<AxisConfig>
225
- plotArea?: Partial<PlotAreaConfig>
226
- legend?: LegendTheme
227
- valueStyle?: TextStyle
228
- spotlight?: Partial<BarChartConfig["spotlight"]>
229
- }
230
-
231
- /** The bar chart's out-of-the-box look. */
232
- export const DEFAULT_BAR_CHART_THEME: BarChartConfig = {
233
- seriesFills: DEFAULT_SERIES_FILLS,
234
- bar: {
235
- cornerRadius: 6,
236
- slotFill: 0.72,
237
- groupGap: 6,
238
- stroke: [],
239
- shadow: [],
240
- },
241
- // A category axis names its slots, so its captions are set like a caption
242
- // rather than like a numeric marker: same size, less weight than the line
243
- // chart's ticks, and centred on the slot.
244
- xAxis: {
245
- ...DEFAULT_X_AXIS,
246
- labelStyle: { ...DEFAULT_X_AXIS.labelStyle, fontWeight: 600 },
247
- tickPadding: 16,
248
- },
249
- yAxis: DEFAULT_Y_AXIS,
250
- plotArea: DEFAULT_PLOT_AREA,
251
- legend: DEFAULT_LEGEND,
252
- valueStyle: {
253
- fontSize: 24,
254
- fontWeight: 700,
255
- textAlign: "center",
256
- fill: CHART_PALETTE.markerColor,
257
- },
258
- spotlight: { dimOpacity: SPOTLIGHT_DIM },
259
- }
260
-
261
- function resolveBarStyle(
262
- bar: Partial<BarStyleConfig> | undefined,
263
- previous: BarStyleConfig
264
- ): BarStyleConfig {
265
- return {
266
- cornerRadius: bar?.cornerRadius ?? previous.cornerRadius,
267
- slotFill: bar?.slotFill ?? previous.slotFill,
268
- groupGap: bar?.groupGap ?? previous.groupGap,
269
- stroke: strokeOps.resolve(bar?.stroke ?? previous.stroke),
270
- shadow: shadowOps.resolve(bar?.shadow ?? previous.shadow),
271
- }
272
- }
273
-
274
- function lerpBarStyle(
275
- from: BarStyleConfig,
276
- to: BarStyleConfig,
277
- t: number
278
- ): BarStyleConfig {
279
- return {
280
- cornerRadius: lerpNumber(from.cornerRadius, to.cornerRadius, t),
281
- slotFill: lerpNumber(from.slotFill, to.slotFill, t),
282
- groupGap: lerpNumber(from.groupGap, to.groupGap, t),
283
- stroke: strokeOps.lerp(
284
- from.stroke as StrokeResolved[],
285
- to.stroke as StrokeResolved[],
286
- t
287
- ),
288
- shadow: shadowOps.lerp(
289
- from.shadow as ShadowResolved[],
290
- to.shadow as ShadowResolved[],
291
- t
292
- ),
293
- }
294
- }
295
-
296
- /**
297
- * {@link BarChart}'s `theme` entry's resolve: merges the author's partial
298
- * {@link BarChartTheme} onto `previous` (falling back to
299
- * {@link DEFAULT_BAR_CHART_THEME} on first resolution), fully resolving every
300
- * leaf so the tween below always has resolved data to interpolate.
301
- */
302
- export function resolveBarChartTheme(
303
- theme: BarChartTheme | undefined,
304
- previous?: BarChartConfig
305
- ): BarChartConfig {
306
- const base = previous ?? DEFAULT_BAR_CHART_THEME
307
- return {
308
- seriesFills: resolveSeriesFills(theme?.seriesFills, base.seriesFills),
309
- bar: resolveBarStyle(theme?.bar, base.bar),
310
- xAxis: resolveAxisConfig(theme?.xAxis, base.xAxis),
311
- yAxis: resolveAxisConfig(theme?.yAxis, base.yAxis),
312
- plotArea: resolvePlotArea(theme?.plotArea, base.plotArea),
313
- legend: resolveLegendConfig(theme?.legend, base.legend),
314
- valueStyle: textOps.resolveStyle(theme?.valueStyle, base.valueStyle),
315
- spotlight: {
316
- dimOpacity: theme?.spotlight?.dimOpacity ?? base.spotlight.dimOpacity,
317
- },
318
- }
319
- }
320
-
321
- /** {@link BarChart}'s `theme` entry's lerp — leaf by leaf. */
322
- export function lerpBarChartTheme(
323
- from: BarChartConfig,
324
- to: BarChartConfig,
325
- t: number
326
- ): BarChartConfig {
327
- return {
328
- seriesFills: lerpSeriesFills(from.seriesFills, to.seriesFills, t),
329
- bar: lerpBarStyle(from.bar, to.bar, t),
330
- xAxis: lerpAxisConfig(from.xAxis, to.xAxis, t),
331
- yAxis: lerpAxisConfig(from.yAxis, to.yAxis, t),
332
- plotArea: lerpPlotArea(from.plotArea, to.plotArea, t),
333
- legend: lerpLegendConfig(from.legend, to.legend, t),
334
- valueStyle: textOps.lerpStyle(from.valueStyle, to.valueStyle, t),
335
- spotlight: {
336
- dimOpacity: lerpNumber(
337
- from.spotlight.dimOpacity,
338
- to.spotlight.dimOpacity,
339
- t
340
- ),
341
- },
342
- }
343
- }
344
-
345
- /** The paint series `index` is drawn in: its own override, else the palette's entry. */
346
- export function resolveBarFill(
347
- palette: Fill[],
348
- series: BarSeries,
349
- index: number
350
- ): Fill {
351
- if (series.fill != null) return fillOps.resolve(series.fill)
352
- if (palette.length === 0) return "white"
353
- return palette[index % palette.length]
354
- }
1
+ /**
2
+ * Types and pure helpers specific to the {@link BarChart} family: how a row's
3
+ * values become the spans its columns cover, how the value axis is scaled around
4
+ * them, and the theme the bars add to the shared chart vocabulary.
5
+ *
6
+ * Everything a line or a pie chart would want too — what a series is, the axis
7
+ * domain model, the theme leaf resolvers, the legend and plot-panel branches —
8
+ * lives in `../kit/shared.ts` and is imported here.
9
+ *
10
+ * Kept renderer-agnostic (no `Node` imports) so a host's inspector mapping
11
+ * can build a partial {@link BarChartTheme} without
12
+ * pulling the components in.
13
+ */
14
+
15
+ import {
16
+ fillOps,
17
+ lerpNumber,
18
+ shadowOps,
19
+ strokeOps,
20
+ textOps,
21
+ type DataRecord,
22
+ type Fill,
23
+ type Shadow,
24
+ type ShadowResolved,
25
+ type Stroke,
26
+ type StrokeResolved,
27
+ type TextStyle,
28
+ } from "@motionscript/sdk"
29
+
30
+ import {
31
+ CHART_PALETTE,
32
+ DEFAULT_LEGEND,
33
+ DEFAULT_PLOT_AREA,
34
+ DEFAULT_SERIES_FILLS,
35
+ DEFAULT_X_AXIS,
36
+ DEFAULT_Y_AXIS,
37
+ SPOTLIGHT_DIM,
38
+ lerpAxisConfig,
39
+ lerpLegendConfig,
40
+ lerpPlotArea,
41
+ lerpSeriesFills,
42
+ niceStep,
43
+ num,
44
+ resolveAxisConfig,
45
+ resolveLegendConfig,
46
+ resolvePlotArea,
47
+ resolveSeriesFills,
48
+ text,
49
+ type AxisConfig,
50
+ type ChartSeries,
51
+ type LegendConfig,
52
+ type LegendTheme,
53
+ type PlotAreaConfig,
54
+ type Range,
55
+ } from "../kit/shared"
56
+
57
+ /**
58
+ * One series in a {@link BarChart}: a column of the table drawn as one bar per
59
+ * row — side by side with its siblings within each category's slot, or stacked
60
+ * on top of them.
61
+ *
62
+ * The family-wide {@link ChartSeries} under a name that says what it draws here.
63
+ */
64
+ export type BarSeries = ChartSeries
65
+
66
+ /**
67
+ * One drawn bar's span in **data units**: the value its base sits at and the
68
+ * value its far end reaches.
69
+ *
70
+ * A span rather than a height because that is the one shape both modes fit. A
71
+ * grouped bar runs from zero to its value; a stacked segment runs from the
72
+ * running total below it to that total plus its own value. Negative values fall
73
+ * out of the same arithmetic — the span simply runs the other way — so a chart
74
+ * with a loss in it needs no special case, only a baseline that isn't the floor.
75
+ */
76
+ export interface BarSpan {
77
+ /** Value the bar's base sits at — 0 when grouped, the running total when stacked. */
78
+ from: number
79
+ /** Value its far end reaches. */
80
+ to: number
81
+ /** The value this bar plots — `to - from`, kept for the figure printed on it. */
82
+ value: number
83
+ }
84
+
85
+ /**
86
+ * The spans one row's bars cover, one per series, in series order.
87
+ *
88
+ * Stacking is a running sum in the order the series are listed, which is also
89
+ * the order they're drawn: series 0 sits on the baseline and each later one
90
+ * rides on what's below it. A zero-valued series still yields a (degenerate)
91
+ * span so the returned list stays index-aligned with the series list.
92
+ */
93
+ export function barSpans(
94
+ row: DataRecord,
95
+ series: BarSeries[],
96
+ stacked: boolean
97
+ ): BarSpan[] {
98
+ let running = 0
99
+ return series.map((s) => {
100
+ const value = num(row, s.field)
101
+ if (!stacked) return { from: 0, to: value, value }
102
+ const from = running
103
+ running += value
104
+ return { from, to: running, value }
105
+ })
106
+ }
107
+
108
+ /**
109
+ * The index of the series whose segment ends a row's stack — the one that gets
110
+ * the rounded cap, so a stack reads as one capped column rather than a pile of
111
+ * separate boxes. `-1` when every value in the row is zero.
112
+ *
113
+ * Read off the *last non-zero* series rather than the last one listed, so a
114
+ * series that happens to be 0 in this row doesn't take the cap and leave the
115
+ * segment below it square.
116
+ */
117
+ export function stackCapIndex(row: DataRecord, series: BarSeries[]): number {
118
+ let last = -1
119
+ for (let i = 0; i < series.length; i++) {
120
+ if (num(row, series[i].field) !== 0) last = i
121
+ }
122
+ return last
123
+ }
124
+
125
+ /** The captions beneath the baseline: one per row, read off the category column. */
126
+ export function barCategories(data: DataRecord[], field: string): string[] {
127
+ return data.map((row) => text(row, field))
128
+ }
129
+
130
+ /**
131
+ * The `[min, max]` of everything the chart draws, **zero always included**.
132
+ *
133
+ * Zero is in the range whatever the data says, because a bar chart's baseline is
134
+ * zero: a scale starting at 40 draws a column three times its neighbour for a
135
+ * 10% difference, which is the classic way to lie with one. A chart that really
136
+ * does want a cropped axis says so with an explicit range.
137
+ */
138
+ export function barValueExtent(
139
+ data: DataRecord[],
140
+ series: BarSeries[],
141
+ stacked: boolean
142
+ ): Range {
143
+ let lo = 0
144
+ let hi = 0
145
+ for (const row of data) {
146
+ for (const span of barSpans(row, series, stacked)) {
147
+ lo = Math.min(lo, span.from, span.to)
148
+ hi = Math.max(hi, span.from, span.to)
149
+ }
150
+ }
151
+ return lo < hi ? [lo, hi] : [0, 1]
152
+ }
153
+
154
+ /**
155
+ * Effective value scale: the explicit `range`, else the data's extent nudged out
156
+ * to tidy step bounds so the markers read as round numbers.
157
+ *
158
+ * `divisions` is how many segments the auto-range is stepped into (one fewer
159
+ * than the marker count), so the ticks the axis draws land on the bounds this
160
+ * picks.
161
+ */
162
+ export function resolveBarScale(
163
+ data: DataRecord[],
164
+ series: BarSeries[],
165
+ stacked: boolean,
166
+ range: Range | undefined,
167
+ divisions: number
168
+ ): Range {
169
+ if (range) return range
170
+ const [lo, hi] = barValueExtent(data, series, stacked)
171
+ const segments = Math.max(1, Math.round(divisions))
172
+ const step = niceStep((hi - lo) / segments || 1)
173
+ return [Math.floor(lo / step) * step, Math.ceil(hi / step) * step]
174
+ }
175
+
176
+ // ---- Theme ---------------------------------------------------------------
177
+
178
+ /** How the columns themselves are drawn and how the slot is divided up. */
179
+ export interface BarStyleConfig {
180
+ /**
181
+ * Radius of a column's far-end corners, in px. Only the outer end rounds — an
182
+ * interior stacked segment stays square, so the stack reads as one column.
183
+ */
184
+ cornerRadius: number
185
+ /**
186
+ * Fraction of its category's slot the bars fill, `[0, 1]`. The remainder is
187
+ * the channel between one category and the next, which is what makes a bar
188
+ * chart read as groups rather than as a solid block.
189
+ */
190
+ slotFill: number
191
+ /** Gap between the grouped bars *within* one slot, in px. Ignored when stacked. */
192
+ groupGap: number
193
+ /** Optional outline on every column. */
194
+ stroke: Stroke
195
+ /** Optional drop shadow on every column. */
196
+ shadow: Shadow
197
+ }
198
+
199
+ /** A {@link BarChart}'s full, resolved visual theme. */
200
+ export interface BarChartConfig {
201
+ /**
202
+ * Default per-series bar paint, cycled by series index (`i % length`). A
203
+ * series' own `fill`, when set, replaces its entry outright.
204
+ */
205
+ seriesFills: Fill[]
206
+ bar: BarStyleConfig
207
+ /** The category axis beneath the baseline. */
208
+ xAxis: AxisConfig
209
+ /** The numeric value axis up the left. */
210
+ yAxis: AxisConfig
211
+ plotArea: PlotAreaConfig
212
+ legend: LegendConfig
213
+ /** The figure printed on each column, when the chart prints them. */
214
+ valueStyle: TextStyle
215
+ /** How far everything *outside* a spotlit series or category fades. */
216
+ spotlight: { dimOpacity: number }
217
+ }
218
+
219
+ /** Author-facing partial theme — every branch optional and deep-merged. */
220
+ export type BarChartTheme = {
221
+ seriesFills?: Fill[]
222
+ bar?: Partial<BarStyleConfig>
223
+ xAxis?: Partial<AxisConfig>
224
+ yAxis?: Partial<AxisConfig>
225
+ plotArea?: Partial<PlotAreaConfig>
226
+ legend?: LegendTheme
227
+ valueStyle?: TextStyle
228
+ spotlight?: Partial<BarChartConfig["spotlight"]>
229
+ }
230
+
231
+ /** The bar chart's out-of-the-box look. */
232
+ export const DEFAULT_BAR_CHART_THEME: BarChartConfig = {
233
+ seriesFills: DEFAULT_SERIES_FILLS,
234
+ bar: {
235
+ cornerRadius: 6,
236
+ slotFill: 0.72,
237
+ groupGap: 6,
238
+ stroke: [],
239
+ shadow: [],
240
+ },
241
+ // A category axis names its slots, so its captions are set like a caption
242
+ // rather than like a numeric marker: same size, less weight than the line
243
+ // chart's ticks, and centred on the slot.
244
+ xAxis: {
245
+ ...DEFAULT_X_AXIS,
246
+ labelStyle: { ...DEFAULT_X_AXIS.labelStyle, fontWeight: 600 },
247
+ tickPadding: 16,
248
+ },
249
+ yAxis: DEFAULT_Y_AXIS,
250
+ plotArea: DEFAULT_PLOT_AREA,
251
+ legend: DEFAULT_LEGEND,
252
+ valueStyle: {
253
+ fontSize: 24,
254
+ fontWeight: 700,
255
+ textAlign: "center",
256
+ fill: CHART_PALETTE.markerColor,
257
+ },
258
+ spotlight: { dimOpacity: SPOTLIGHT_DIM },
259
+ }
260
+
261
+ function resolveBarStyle(
262
+ bar: Partial<BarStyleConfig> | undefined,
263
+ previous: BarStyleConfig
264
+ ): BarStyleConfig {
265
+ return {
266
+ cornerRadius: bar?.cornerRadius ?? previous.cornerRadius,
267
+ slotFill: bar?.slotFill ?? previous.slotFill,
268
+ groupGap: bar?.groupGap ?? previous.groupGap,
269
+ stroke: strokeOps.resolve(bar?.stroke ?? previous.stroke),
270
+ shadow: shadowOps.resolve(bar?.shadow ?? previous.shadow),
271
+ }
272
+ }
273
+
274
+ function lerpBarStyle(
275
+ from: BarStyleConfig,
276
+ to: BarStyleConfig,
277
+ t: number
278
+ ): BarStyleConfig {
279
+ return {
280
+ cornerRadius: lerpNumber(from.cornerRadius, to.cornerRadius, t),
281
+ slotFill: lerpNumber(from.slotFill, to.slotFill, t),
282
+ groupGap: lerpNumber(from.groupGap, to.groupGap, t),
283
+ stroke: strokeOps.lerp(
284
+ from.stroke as StrokeResolved[],
285
+ to.stroke as StrokeResolved[],
286
+ t
287
+ ),
288
+ shadow: shadowOps.lerp(
289
+ from.shadow as ShadowResolved[],
290
+ to.shadow as ShadowResolved[],
291
+ t
292
+ ),
293
+ }
294
+ }
295
+
296
+ /**
297
+ * {@link BarChart}'s `theme` entry's resolve: merges the author's partial
298
+ * {@link BarChartTheme} onto `previous` (falling back to
299
+ * {@link DEFAULT_BAR_CHART_THEME} on first resolution), fully resolving every
300
+ * leaf so the tween below always has resolved data to interpolate.
301
+ */
302
+ export function resolveBarChartTheme(
303
+ theme: BarChartTheme | undefined,
304
+ previous?: BarChartConfig
305
+ ): BarChartConfig {
306
+ const base = previous ?? DEFAULT_BAR_CHART_THEME
307
+ return {
308
+ seriesFills: resolveSeriesFills(theme?.seriesFills, base.seriesFills),
309
+ bar: resolveBarStyle(theme?.bar, base.bar),
310
+ xAxis: resolveAxisConfig(theme?.xAxis, base.xAxis),
311
+ yAxis: resolveAxisConfig(theme?.yAxis, base.yAxis),
312
+ plotArea: resolvePlotArea(theme?.plotArea, base.plotArea),
313
+ legend: resolveLegendConfig(theme?.legend, base.legend),
314
+ valueStyle: textOps.resolveStyle(theme?.valueStyle, base.valueStyle),
315
+ spotlight: {
316
+ dimOpacity: theme?.spotlight?.dimOpacity ?? base.spotlight.dimOpacity,
317
+ },
318
+ }
319
+ }
320
+
321
+ /** {@link BarChart}'s `theme` entry's lerp — leaf by leaf. */
322
+ export function lerpBarChartTheme(
323
+ from: BarChartConfig,
324
+ to: BarChartConfig,
325
+ t: number
326
+ ): BarChartConfig {
327
+ return {
328
+ seriesFills: lerpSeriesFills(from.seriesFills, to.seriesFills, t),
329
+ bar: lerpBarStyle(from.bar, to.bar, t),
330
+ xAxis: lerpAxisConfig(from.xAxis, to.xAxis, t),
331
+ yAxis: lerpAxisConfig(from.yAxis, to.yAxis, t),
332
+ plotArea: lerpPlotArea(from.plotArea, to.plotArea, t),
333
+ legend: lerpLegendConfig(from.legend, to.legend, t),
334
+ valueStyle: textOps.lerpStyle(from.valueStyle, to.valueStyle, t),
335
+ spotlight: {
336
+ dimOpacity: lerpNumber(
337
+ from.spotlight.dimOpacity,
338
+ to.spotlight.dimOpacity,
339
+ t
340
+ ),
341
+ },
342
+ }
343
+ }
344
+
345
+ /** The paint series `index` is drawn in: its own override, else the palette's entry. */
346
+ export function resolveBarFill(
347
+ palette: Fill[],
348
+ series: BarSeries,
349
+ index: number
350
+ ): Fill {
351
+ if (series.fill != null) return fillOps.resolve(series.fill)
352
+ if (palette.length === 0) return "white"
353
+ return palette[index % palette.length]
354
+ }