@motionscript/charts 0.0.0-stage → 0.1.0-alpha.10
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 +110 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +98 -3
- package/dist/bar-chart/bar-chart.d.ts +191 -0
- package/dist/bar-chart/bar-chart.d.ts.map +1 -0
- package/dist/bar-chart/bar-chart.js +431 -0
- package/dist/bar-chart/bar-chart.js.map +1 -0
- package/dist/bar-chart/category-axis.d.ts +71 -0
- package/dist/bar-chart/category-axis.d.ts.map +1 -0
- package/dist/bar-chart/category-axis.js +119 -0
- package/dist/bar-chart/category-axis.js.map +1 -0
- package/dist/bar-chart/chart-bars.d.ts +129 -0
- package/dist/bar-chart/chart-bars.d.ts.map +1 -0
- package/dist/bar-chart/chart-bars.js +181 -0
- package/dist/bar-chart/chart-bars.js.map +1 -0
- package/dist/bar-chart/index.d.ts +16 -0
- package/dist/bar-chart/index.d.ts.map +1 -0
- package/dist/bar-chart/index.js +12 -0
- package/dist/bar-chart/index.js.map +1 -0
- package/dist/bar-chart/shared.d.ts +146 -0
- package/dist/bar-chart/shared.d.ts.map +1 -0
- package/dist/bar-chart/shared.js +182 -0
- package/dist/bar-chart/shared.js.map +1 -0
- package/dist/browser/chunks/chunk-TJNHLQGV.js +2 -0
- package/dist/browser/chunks/chunk-TJNHLQGV.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +13 -0
- package/dist/engine.d.ts +29 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +29 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- 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 +27 -0
- package/dist/kit/chart-border.d.ts.map +1 -0
- package/dist/kit/chart-border.js +28 -0
- package/dist/kit/chart-border.js.map +1 -0
- package/dist/kit/chart-canvas.d.ts +67 -0
- package/dist/kit/chart-canvas.d.ts.map +1 -0
- package/dist/kit/chart-canvas.js +68 -0
- package/dist/kit/chart-canvas.js.map +1 -0
- package/dist/kit/chart-legend.d.ts +164 -0
- package/dist/kit/chart-legend.d.ts.map +1 -0
- package/dist/kit/chart-legend.js +163 -0
- package/dist/kit/chart-legend.js.map +1 -0
- 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 +302 -0
- package/dist/kit/shared.d.ts.map +1 -0
- package/dist/kit/shared.js +496 -0
- package/dist/kit/shared.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +86 -0
- package/dist/kit/vertical-axis.d.ts.map +1 -0
- package/dist/kit/vertical-axis.js +141 -0
- package/dist/kit/vertical-axis.js.map +1 -0
- package/dist/line-chart/chart-line.d.ts +74 -0
- package/dist/line-chart/chart-line.d.ts.map +1 -0
- package/dist/line-chart/chart-line.js +135 -0
- package/dist/line-chart/chart-line.js.map +1 -0
- package/dist/line-chart/chart-region.d.ts +46 -0
- package/dist/line-chart/chart-region.d.ts.map +1 -0
- package/dist/line-chart/chart-region.js +67 -0
- package/dist/line-chart/chart-region.js.map +1 -0
- package/dist/line-chart/horizontal-axis.d.ts +79 -0
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -0
- package/dist/line-chart/horizontal-axis.js +129 -0
- package/dist/line-chart/horizontal-axis.js.map +1 -0
- package/dist/line-chart/index.d.ts +22 -0
- package/dist/line-chart/index.d.ts.map +1 -0
- package/dist/line-chart/index.js +19 -0
- package/dist/line-chart/index.js.map +1 -0
- package/dist/line-chart/line-chart.d.ts +200 -0
- package/dist/line-chart/line-chart.d.ts.map +1 -0
- package/dist/line-chart/line-chart.js +473 -0
- package/dist/line-chart/line-chart.js.map +1 -0
- package/dist/line-chart/shared.d.ts +171 -0
- package/dist/line-chart/shared.d.ts.map +1 -0
- package/dist/line-chart/shared.js +285 -0
- package/dist/line-chart/shared.js.map +1 -0
- package/dist/line-chart/x-scale.d.ts +77 -0
- package/dist/line-chart/x-scale.d.ts.map +1 -0
- package/dist/line-chart/x-scale.js +313 -0
- package/dist/line-chart/x-scale.js.map +1 -0
- package/dist/nodes.d.ts +17 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +18 -0
- package/dist/nodes.js.map +1 -0
- package/dist/pie-chart/index.d.ts +18 -0
- package/dist/pie-chart/index.d.ts.map +1 -0
- package/dist/pie-chart/index.js +14 -0
- package/dist/pie-chart/index.js.map +1 -0
- package/dist/pie-chart/pie-chart.d.ts +152 -0
- package/dist/pie-chart/pie-chart.d.ts.map +1 -0
- package/dist/pie-chart/pie-chart.js +222 -0
- package/dist/pie-chart/pie-chart.js.map +1 -0
- package/dist/pie-chart/pie-legend.d.ts +113 -0
- package/dist/pie-chart/pie-legend.d.ts.map +1 -0
- package/dist/pie-chart/pie-legend.js +117 -0
- package/dist/pie-chart/pie-legend.js.map +1 -0
- package/dist/pie-chart/pie-ring.d.ts +304 -0
- package/dist/pie-chart/pie-ring.d.ts.map +1 -0
- package/dist/pie-chart/pie-ring.js +680 -0
- package/dist/pie-chart/pie-ring.js.map +1 -0
- package/dist/pie-chart/shared.d.ts +216 -0
- package/dist/pie-chart/shared.d.ts.map +1 -0
- package/dist/pie-chart/shared.js +246 -0
- package/dist/pie-chart/shared.js.map +1 -0
- package/package.json +72 -3
- package/registry.json +23 -0
- package/src/bar-chart/bar-chart.ts +542 -0
- package/src/bar-chart/category-axis.ts +162 -0
- package/src/bar-chart/chart-bars.ts +287 -0
- package/src/bar-chart/index.ts +31 -0
- package/src/bar-chart/shared.ts +354 -0
- package/src/engine.ts +28 -0
- package/src/index.ts +5 -0
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +50 -0
- package/src/kit/chart-canvas.ts +122 -0
- package/src/kit/chart-legend.ts +229 -0
- 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 -0
- package/src/kit/vertical-axis.ts +194 -0
- package/src/line-chart/chart-line.ts +156 -0
- package/src/line-chart/chart-region.ts +90 -0
- package/src/line-chart/horizontal-axis.ts +151 -0
- package/src/line-chart/index.ts +39 -0
- package/src/line-chart/line-chart.ts +598 -0
- package/src/line-chart/shared.ts +473 -0
- package/src/line-chart/x-scale.ts +382 -0
- package/src/nodes.ts +18 -0
- package/src/pie-chart/index.ts +37 -0
- package/src/pie-chart/pie-chart.ts +296 -0
- package/src/pie-chart/pie-legend.ts +169 -0
- package/src/pie-chart/pie-ring.ts +848 -0
- package/src/pie-chart/shared.ts +496 -0
|
@@ -0,0 +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
|
+
}
|
package/src/engine.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The chart **kit**: everything the three charts share, and the one thing a
|
|
3
|
+
* copied chart does not copy.
|
|
4
|
+
*
|
|
5
|
+
* A copy of the line chart takes `line-chart/*` into your project and rewrites its
|
|
6
|
+
* reach for these to `@motionscript/charts`, so the plot panel, the value
|
|
7
|
+
* axis, the legend and the number formatting stay a real dependency. That is
|
|
8
|
+
* not a size decision — it is the only arrangement that works.
|
|
9
|
+
*
|
|
10
|
+
* A chart identifies its own parts by component identity (`VerticalAxis.is`).
|
|
11
|
+
* Copy `VerticalAxis` alongside your fork and there are now two of it — two
|
|
12
|
+
* components answering one name, and whichever one your chart did not get is
|
|
13
|
+
* a part it never finds, with no error. Keeping the kit external makes that
|
|
14
|
+
* impossible rather than unlikely.
|
|
15
|
+
*
|
|
16
|
+
* It also means a fix to the axis reaches a forked chart without a re-add,
|
|
17
|
+
* which is the half of the bargain you get back for it.
|
|
18
|
+
*/
|
|
19
|
+
export * from "./kit/shared"
|
|
20
|
+
export * from "./kit/cartesian"
|
|
21
|
+
export * from "./kit/chart-border"
|
|
22
|
+
export * from "./kit/chart-canvas"
|
|
23
|
+
export * from "./kit/chart-legend"
|
|
24
|
+
export * from "./kit/entries"
|
|
25
|
+
export * from "./kit/motion"
|
|
26
|
+
export * from "./kit/paints"
|
|
27
|
+
export * from "./kit/vertical-axis"
|
|
28
|
+
export * from "@motionscript/sdk/component"
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Rect, type ChildView, type Node2D, type Size2D } from "@motionscript/sdk"
|
|
2
|
+
|
|
3
|
+
import { VerticalAxis } from "./vertical-axis"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Headroom above the plot, in px.
|
|
7
|
+
*
|
|
8
|
+
* Two jobs, and the second is why it is real padding on the composition rather
|
|
9
|
+
* than a number the point-mapping math imagines: a series peaking at the top of
|
|
10
|
+
* the scale isn't flush to the top edge, **and** the top marker, which is
|
|
11
|
+
* centred on that top grid line, has somewhere to be. Without it the marker's
|
|
12
|
+
* upper half is drawn above the chart's own box.
|
|
13
|
+
*/
|
|
14
|
+
export const TOP_HEADROOM = 24
|
|
15
|
+
|
|
16
|
+
/** The parts of a cartesian chart's frame, as {@link cartesianFrame} arranges them. */
|
|
17
|
+
export interface CartesianParts {
|
|
18
|
+
/** The value axis, left of the plot. */
|
|
19
|
+
readonly axis: Node2D
|
|
20
|
+
/** The plot panel: the box the grid and the marks share. */
|
|
21
|
+
readonly plot: Node2D
|
|
22
|
+
/** An invisible twin of the value axis, reserving its gutter under the plot. */
|
|
23
|
+
readonly gutter: Node2D
|
|
24
|
+
/** What sits under the plot, top to bottom: the other axis, then the legend. */
|
|
25
|
+
readonly below: readonly Node2D[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The frame the line and bar charts share: the value axis beside the plot, and
|
|
30
|
+
* under them the gutter beside the rows below the plot, so those rows start at
|
|
31
|
+
* the plot's left edge rather than the chart's.
|
|
32
|
+
*/
|
|
33
|
+
export function cartesianFrame(parts: CartesianParts): Node2D {
|
|
34
|
+
return new Rect({
|
|
35
|
+
flow: "vertical",
|
|
36
|
+
width: "fill",
|
|
37
|
+
height: "fill",
|
|
38
|
+
padding: { top: TOP_HEADROOM },
|
|
39
|
+
children: [
|
|
40
|
+
new Rect({ flow: "horizontal", height: "fill", width: "fill", children: [parts.axis, parts.plot] }),
|
|
41
|
+
new Rect({
|
|
42
|
+
flow: "horizontal",
|
|
43
|
+
width: "fill",
|
|
44
|
+
children: [parts.gutter, new Rect({ flow: "vertical", width: "fill", gap: 20, children: [...parts.below] })],
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
}) as unknown as Node2D
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A cartesian frame's parts, as a command reaches them through its chart's children. */
|
|
51
|
+
export interface CartesianViews {
|
|
52
|
+
readonly axis: ChildView
|
|
53
|
+
/** The plot panel's own children: the grid, the marks, the border. */
|
|
54
|
+
readonly plot: readonly ChildView[]
|
|
55
|
+
readonly below: readonly ChildView[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The frame among a chart's children: the last child shaped as
|
|
60
|
+
* {@link cartesianFrame} builds it. The chart builds it after the children it
|
|
61
|
+
* was given, and a document adds its own later still, so it is found by shape.
|
|
62
|
+
*/
|
|
63
|
+
export function cartesianViews(children: readonly ChildView[]): CartesianViews | null {
|
|
64
|
+
for (let i = children.length - 1; i >= 0; i--) {
|
|
65
|
+
const frame = children[i]
|
|
66
|
+
if (!Rect.is(frame)) continue
|
|
67
|
+
const [top, bottom] = frame.children
|
|
68
|
+
const axis = top?.children[0]
|
|
69
|
+
const plot = top?.children[1]
|
|
70
|
+
const below = bottom?.children[1]
|
|
71
|
+
if (!axis || !VerticalAxis.is(axis) || !plot || !below) continue
|
|
72
|
+
return { axis, plot: plot.children, below: below.children }
|
|
73
|
+
}
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A part's laid-out size, or nothing before it is built or laid out. */
|
|
78
|
+
export function laidOutSize(node: Node2D | undefined): Size2D {
|
|
79
|
+
const bounds = node?.layoutBounds
|
|
80
|
+
return { width: bounds?.width ?? 0, height: bounds?.height ?? 0 }
|
|
81
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Graphics2D,
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
5
|
+
strokeOps,
|
|
6
|
+
type NodeOf,
|
|
7
|
+
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
8
|
+
type Node2D as _Node2D,
|
|
9
|
+
type Node2DProps,
|
|
10
|
+
type Stroke,
|
|
11
|
+
type StrokeResolved,
|
|
12
|
+
} from "@motionscript/sdk"
|
|
13
|
+
|
|
14
|
+
import { loadPaints } from "./paints"
|
|
15
|
+
import { DEFAULT_PLOT_AREA } from "./shared"
|
|
16
|
+
|
|
17
|
+
export interface ChartBorderProps extends Node2DProps {
|
|
18
|
+
/**
|
|
19
|
+
* The plot's thick frame stroke. The stroke resolver's default `align` is
|
|
20
|
+
* `'inside'`, so the whole weight sits within the layout box unless the theme
|
|
21
|
+
* overrides it.
|
|
22
|
+
*/
|
|
23
|
+
stroke: Stroke
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The thick **frame** around a cartesian chart's plot: an empty rect wearing an
|
|
28
|
+
* inside stroke, sized from its box.
|
|
29
|
+
*
|
|
30
|
+
* Split out of {@link ChartCanvas} (the filled + gridded backdrop) so it can be
|
|
31
|
+
* placed *after* the marks in the tree — the box then frames the plot **over**
|
|
32
|
+
* them, so a line or a column running to the edge tucks cleanly under the border
|
|
33
|
+
* rather than sitting on top of it.
|
|
34
|
+
*/
|
|
35
|
+
export const ChartBorder = component2d({
|
|
36
|
+
key: "chartBorder",
|
|
37
|
+
state: {
|
|
38
|
+
stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_PLOT_AREA.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
39
|
+
},
|
|
40
|
+
load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
|
|
41
|
+
render: (ctx, state) => {
|
|
42
|
+
const { width, height } = ctx.size
|
|
43
|
+
if (width <= 0 || height <= 0) return
|
|
44
|
+
// Empty rect (no fill) with the themed stroke, so the frame sits over the
|
|
45
|
+
// marks but the box interior stays clear.
|
|
46
|
+
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).stroke(state.stroke))
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
export type ChartBorder = NodeOf<typeof ChartBorder>
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Graphics2D,
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
5
|
+
fillOps,
|
|
6
|
+
shadowOps,
|
|
7
|
+
strokeOps,
|
|
8
|
+
type Fill,
|
|
9
|
+
type FillResolved,
|
|
10
|
+
type NodeOf,
|
|
11
|
+
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
12
|
+
type Node2D as _Node2D,
|
|
13
|
+
type Node2DProps,
|
|
14
|
+
type Shadow,
|
|
15
|
+
type ShadowResolved,
|
|
16
|
+
type Stroke,
|
|
17
|
+
type StrokeResolved,
|
|
18
|
+
} from "@motionscript/sdk"
|
|
19
|
+
|
|
20
|
+
import { fresh } from "./entries"
|
|
21
|
+
import { loadPaints } from "./paints"
|
|
22
|
+
import {
|
|
23
|
+
DEFAULT_PLOT_AREA,
|
|
24
|
+
DEFAULT_X_AXIS,
|
|
25
|
+
DEFAULT_Y_AXIS,
|
|
26
|
+
type Range,
|
|
27
|
+
} from "./shared"
|
|
28
|
+
|
|
29
|
+
export interface ChartCanvasProps extends Node2DProps {
|
|
30
|
+
/** Fill of the plot panel behind the marks. */
|
|
31
|
+
fill: Fill
|
|
32
|
+
/** Drop shadow of the panel. Default none. */
|
|
33
|
+
shadow: Shadow
|
|
34
|
+
/** Stroke of the vertical grid lines (one per {@link xMarkers}). */
|
|
35
|
+
xGridStroke: Stroke
|
|
36
|
+
/** Stroke of the horizontal grid lines (one per {@link yMarkers}). */
|
|
37
|
+
yGridStroke: Stroke
|
|
38
|
+
/**
|
|
39
|
+
* Data-unit x positions to draw a vertical grid line at — one per horizontal
|
|
40
|
+
* axis marker, so a line lands under every marker regardless of spacing.
|
|
41
|
+
* Positions at (or past) an {@link xExtent} edge coincide with the border and
|
|
42
|
+
* are skipped. Pass an empty list for a chart whose horizontal axis is
|
|
43
|
+
* categorical and so has no numeric position to rule at.
|
|
44
|
+
*/
|
|
45
|
+
xMarkers: number[]
|
|
46
|
+
/** Data-unit y positions to draw a horizontal grid line at. */
|
|
47
|
+
yMarkers: number[]
|
|
48
|
+
/** `[min, max]` of the horizontal scale — maps {@link xMarkers} into the panel. */
|
|
49
|
+
xExtent: Range
|
|
50
|
+
/** `[min, max]` of the vertical scale — maps {@link yMarkers} into the panel. */
|
|
51
|
+
yExtent: Range
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const strokeEntry = (initial: Stroke) =>
|
|
55
|
+
Types.custom<Stroke, StrokeResolved[]>(initial, { resolve: strokeOps.resolve, lerp: strokeOps.lerp })
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The plot **backdrop** of a cartesian chart: a filled panel with a grid of
|
|
59
|
+
* faint lines spanning both directions across it, sized from its box and centred
|
|
60
|
+
* on its origin.
|
|
61
|
+
*
|
|
62
|
+
* The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
|
|
63
|
+
* *over* the marks while this panel sits under them. Grid lines are placed from
|
|
64
|
+
* the axis marker positions (in data units) mapped into the panel through
|
|
65
|
+
* {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
|
|
66
|
+
* marker whether or not the markers are evenly spaced.
|
|
67
|
+
*/
|
|
68
|
+
export const ChartCanvas = component2d({
|
|
69
|
+
key: "chartCanvas",
|
|
70
|
+
state: {
|
|
71
|
+
fill: Types.custom<Fill, FillResolved[]>(DEFAULT_PLOT_AREA.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
72
|
+
shadow: Types.custom<Shadow, ShadowResolved[]>(DEFAULT_PLOT_AREA.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),
|
|
73
|
+
xGridStroke: strokeEntry(DEFAULT_X_AXIS.lineStyle),
|
|
74
|
+
yGridStroke: strokeEntry(DEFAULT_Y_AXIS.lineStyle),
|
|
75
|
+
xMarkers: fresh<number[]>(() => []),
|
|
76
|
+
yMarkers: fresh<number[]>(() => []),
|
|
77
|
+
xExtent: fresh<Range>(() => [0, 1]),
|
|
78
|
+
yExtent: fresh<Range>(() => [0, 1]),
|
|
79
|
+
},
|
|
80
|
+
// The grid strokes are named past the conventional slots, so they are named here.
|
|
81
|
+
load: (loader, state) =>
|
|
82
|
+
loadPaints(loader, { fills: [state.fill], shadows: [state.shadow], strokes: [state.xGridStroke, state.yGridStroke] }),
|
|
83
|
+
render: (ctx, state) => {
|
|
84
|
+
const { width: W, height: H } = ctx.size
|
|
85
|
+
if (W <= 0 || H <= 0) return
|
|
86
|
+
|
|
87
|
+
// Plot panel, centred on the origin (node space is y-up, origin centre).
|
|
88
|
+
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).shadow(state.shadow).fill(state.fill))
|
|
89
|
+
|
|
90
|
+
const [xMin, xMax] = state.xExtent
|
|
91
|
+
const [yMin, yMax] = state.yExtent
|
|
92
|
+
// Fraction of a marker across its extent; guard a zero-width extent.
|
|
93
|
+
const xFrac = (v: number) => (xMax <= xMin ? 0 : (v - xMin) / (xMax - xMin))
|
|
94
|
+
const yFrac = (v: number) => (yMax <= yMin ? 0 : (v - yMin) / (yMax - yMin))
|
|
95
|
+
// Skip markers landing on (or outside) the edges — they'd sit under the border.
|
|
96
|
+
const interior = (f: number) => f > 1e-6 && f < 1 - 1e-6
|
|
97
|
+
|
|
98
|
+
const xStroke = state.xGridStroke[0]
|
|
99
|
+
if (xStroke) {
|
|
100
|
+
const xGrid = new Graphics2D()
|
|
101
|
+
for (const v of state.xMarkers) {
|
|
102
|
+
const f = xFrac(v)
|
|
103
|
+
if (!interior(f)) continue
|
|
104
|
+
xGrid.rect({ x: -W / 2 + f * W, y: 0, width: xStroke.weight, height: H })
|
|
105
|
+
}
|
|
106
|
+
ctx.draw(xGrid.fill(xStroke.fill))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const yStroke = state.yGridStroke[0]
|
|
110
|
+
if (yStroke) {
|
|
111
|
+
const yGrid = new Graphics2D()
|
|
112
|
+
for (const v of state.yMarkers) {
|
|
113
|
+
const f = yFrac(v)
|
|
114
|
+
if (!interior(f)) continue
|
|
115
|
+
yGrid.rect({ x: 0, y: -H / 2 + f * H, width: W, height: yStroke.weight })
|
|
116
|
+
}
|
|
117
|
+
ctx.draw(yGrid.fill(yStroke.fill))
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
export type ChartCanvas = NodeOf<typeof ChartCanvas>
|