@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.
- package/CHANGELOG.md +63 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +99 -0
- package/dist/bar-chart/bar-chart.d.ts +93 -231
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +378 -565
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +24 -41
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +93 -115
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +36 -70
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +156 -210
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +2 -2
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +1 -1
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +6 -6
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +6 -6
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
- package/dist/browser/chunks/chunk-XROBIRQK.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +3 -2
- package/dist/engine.d.ts +8 -6
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +8 -6
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/cartesian.d.ts +44 -0
- package/dist/kit/cartesian.d.ts.map +1 -0
- package/dist/kit/cartesian.js +59 -0
- package/dist/kit/cartesian.js.map +1 -0
- package/dist/kit/chart-border.d.ts +8 -11
- package/dist/kit/chart-border.d.ts.map +1 -1
- package/dist/kit/chart-border.js +15 -32
- package/dist/kit/chart-border.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +23 -21
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +33 -90
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +79 -88
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +137 -186
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.d.ts +17 -0
- package/dist/kit/entries.d.ts.map +1 -0
- package/dist/kit/entries.js +19 -0
- package/dist/kit/entries.js.map +1 -0
- package/dist/kit/motion.d.ts +48 -0
- package/dist/kit/motion.d.ts.map +1 -0
- package/dist/kit/motion.js +63 -0
- package/dist/kit/motion.js.map +1 -0
- package/dist/kit/paints.d.ts +10 -0
- package/dist/kit/paints.d.ts.map +1 -0
- package/dist/kit/paints.js +12 -0
- package/dist/kit/paints.js.map +1 -0
- package/dist/kit/shared.d.ts +59 -8
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +105 -10
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +36 -61
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +97 -132
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +27 -53
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +89 -120
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +14 -14
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +26 -64
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +32 -56
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +85 -123
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +2 -2
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +1 -1
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +111 -311
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +403 -639
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +6 -6
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +22 -7
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +1 -1
- package/dist/line-chart/x-scale.js +1 -1
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.d.ts +6 -9
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +7 -9
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +2 -2
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +77 -134
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +199 -268
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +54 -31
- package/dist/pie-chart/pie-legend.d.ts.map +1 -1
- package/dist/pie-chart/pie-legend.js +93 -107
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +142 -122
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +333 -361
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +3 -3
- package/dist/pie-chart/shared.js +3 -3
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +6 -5
- package/registry.json +23 -23
- package/src/bar-chart/bar-chart.ts +534 -814
- package/src/bar-chart/category-axis.ts +162 -177
- package/src/bar-chart/chart-bars.ts +288 -310
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +28 -26
- package/src/index.ts +5 -5
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +50 -61
- package/src/kit/chart-canvas.ts +122 -157
- package/src/kit/chart-legend.ts +229 -250
- package/src/kit/entries.ts +20 -0
- package/src/kit/motion.ts +90 -0
- package/src/kit/paints.ts +15 -0
- package/src/kit/shared.ts +782 -675
- package/src/kit/vertical-axis.ts +194 -224
- package/src/line-chart/chart-line.ts +181 -208
- package/src/line-chart/chart-region.ts +106 -135
- package/src/line-chart/horizontal-axis.ts +166 -202
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +585 -929
- package/src/line-chart/shared.ts +473 -459
- package/src/line-chart/x-scale.ts +367 -367
- package/src/nodes.ts +18 -20
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +293 -381
- package/src/pie-chart/pie-legend.ts +169 -159
- package/src/pie-chart/pie-ring.ts +848 -903
- package/src/pie-chart/shared.ts +490 -490
- package/dist/browser/chunks/chunk-RMHQ6GYU.js +0 -2
- package/dist/browser/chunks/chunk-RMHQ6GYU.js.map +0 -7
package/src/bar-chart/shared.ts
CHANGED
|
@@ -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
|
|
9
|
-
*
|
|
10
|
-
* Kept renderer-agnostic (no `Node` imports) so
|
|
11
|
-
*
|
|
12
|
-
* pulling the
|
|
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`
|
|
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`
|
|
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
|
+
}
|