@motionscript/charts 0.1.0 → 0.1.1-alpha.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.
- package/CHANGELOG.md +19 -0
- package/dist/bar-chart/bar-chart.d.ts +22 -93
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +48 -121
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +11 -38
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +15 -38
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +43 -74
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +31 -59
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +2 -10
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +0 -7
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +27 -90
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +15 -54
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-4SMRCU24.js +2 -0
- package/dist/browser/chunks/chunk-4SMRCU24.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 +1 -1
- package/dist/engine.d.ts +4 -16
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +4 -16
- package/dist/engine.js.map +1 -1
- package/dist/kit/cartesian.d.ts +8 -28
- package/dist/kit/cartesian.d.ts.map +1 -1
- package/dist/kit/cartesian.js +7 -19
- package/dist/kit/cartesian.js.map +1 -1
- package/dist/kit/chart-border.d.ts +9 -18
- package/dist/kit/chart-border.d.ts.map +1 -1
- package/dist/kit/chart-border.js +4 -9
- package/dist/kit/chart-border.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +25 -36
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +13 -13
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +14 -61
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +17 -65
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.d.ts +2 -0
- package/dist/kit/entries.d.ts.map +1 -1
- package/dist/kit/entries.js +2 -0
- package/dist/kit/entries.js.map +1 -1
- package/dist/kit/motion.d.ts +11 -3
- package/dist/kit/motion.d.ts.map +1 -1
- package/dist/kit/motion.js +7 -1
- package/dist/kit/motion.js.map +1 -1
- package/dist/kit/shared.d.ts +60 -175
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +40 -157
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +16 -45
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +17 -42
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +16 -41
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +14 -43
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +14 -27
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +8 -22
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +16 -43
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +13 -42
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +1 -13
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +0 -12
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +23 -109
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +45 -141
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +33 -107
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +28 -81
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +14 -55
- package/dist/line-chart/x-scale.d.ts.map +1 -1
- package/dist/line-chart/x-scale.js +29 -111
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.d.ts +0 -12
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +0 -12
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +1 -12
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +0 -9
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +37 -71
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +31 -65
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +11 -26
- package/dist/pie-chart/pie-legend.d.ts.map +1 -1
- package/dist/pie-chart/pie-legend.js +14 -39
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +50 -192
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +75 -247
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +45 -131
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +29 -72
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +4 -4
- package/src/bar-chart/bar-chart.ts +51 -165
- package/src/bar-chart/category-axis.ts +16 -57
- package/src/bar-chart/chart-bars.ts +43 -120
- package/src/bar-chart/index.ts +2 -10
- package/src/bar-chart/shared.ts +29 -97
- package/src/engine.ts +4 -16
- package/src/kit/cartesian.ts +11 -26
- package/src/kit/chart-border.ts +6 -20
- package/src/kit/chart-canvas.ts +15 -40
- package/src/kit/chart-legend.ts +17 -86
- package/src/kit/entries.ts +4 -1
- package/src/kit/motion.ts +11 -2
- package/src/kit/shared.ts +58 -219
- package/src/kit/vertical-axis.ts +18 -71
- package/src/line-chart/chart-line.ts +16 -44
- package/src/line-chart/chart-region.ts +10 -23
- package/src/line-chart/horizontal-axis.ts +13 -42
- package/src/line-chart/index.ts +1 -13
- package/src/line-chart/line-chart.ts +50 -184
- package/src/line-chart/shared.ts +51 -145
- package/src/line-chart/x-scale.ts +31 -121
- package/src/nodes.ts +0 -12
- package/src/pie-chart/index.ts +1 -12
- package/src/pie-chart/pie-chart.ts +34 -102
- package/src/pie-chart/pie-legend.ts +14 -55
- package/src/pie-chart/pie-ring.ts +80 -307
- package/src/pie-chart/shared.ts +50 -139
- package/dist/browser/chunks/chunk-TJNHLQGV.js +0 -2
- package/dist/browser/chunks/chunk-TJNHLQGV.js.map +0 -7
package/src/kit/shared.ts
CHANGED
|
@@ -1,31 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* What every chart shares: series, axis scaling and formatting, and the theme
|
|
3
|
+
* leaves each chart's theme is assembled from. No `Node` imports, so a host's
|
|
4
|
+
* inspector mappings can build a partial theme without the components.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* label, and a theme is a tree of paints with one mapper and one tween. Writing
|
|
12
|
-
* that three times would mean three answers to "how does a chart round its axis"
|
|
13
|
-
* within one app.
|
|
14
|
-
*
|
|
15
|
-
* Kept renderer-agnostic (no `Node` imports) so a host's own inspector
|
|
16
|
-
* mappings can build a partial theme without pulling the components in.
|
|
17
|
-
*
|
|
18
|
-
* **Two conventions run through every resolver and lerp below**, inherited from
|
|
19
|
-
* the shape system's `resolveStroke`/`resolveShadow`:
|
|
20
|
-
*
|
|
21
|
-
* - A **mapper** merges the author's loose, partial input onto a `previous`
|
|
22
|
-
* resolved value and resolves every leaf, so the tween always has resolved
|
|
23
|
-
* data to interpolate. It is idempotent.
|
|
24
|
-
* - A **tween** interpolates two resolved values leaf by leaf. Categorical
|
|
25
|
-
* fields (`fontFamily`, `textAlign`) and fields that are `undefined` on either
|
|
26
|
-
* side **step at the midpoint**, because there is no meaningful in-between and
|
|
27
|
-
* defaulting the missing side to 0 would animate a font weight down to nothing
|
|
28
|
-
* on the way to "unset".
|
|
6
|
+
* A mapper merges partial input onto a `previous` resolved value (idempotent). A
|
|
7
|
+
* tween interpolates two resolved values leaf by leaf, but categorical fields
|
|
8
|
+
* (`fontFamily`, `textAlign`) and any field `undefined` on either side step at
|
|
9
|
+
* the midpoint: defaulting the missing side to 0 would animate a font weight
|
|
10
|
+
* down to nothing on the way to "unset".
|
|
29
11
|
*/
|
|
30
12
|
|
|
31
13
|
import {
|
|
@@ -47,64 +29,43 @@ import {
|
|
|
47
29
|
type TextStyle,
|
|
48
30
|
} from "@motionscript/sdk"
|
|
49
31
|
|
|
50
|
-
/**
|
|
51
|
-
* One series of a chart: a column of the table, captioned and painted.
|
|
52
|
-
*
|
|
53
|
-
* `field` names the column carrying this series' values. What that becomes is
|
|
54
|
-
* the chart's business — a traced polyline, a column of bars, a wedge — but the
|
|
55
|
-
* three questions asked of it are the same everywhere: which column, what to
|
|
56
|
-
* call it, what to paint it, and whether it is currently drawn.
|
|
57
|
-
*
|
|
58
|
-
* `fill` is an **override**: omit it and the series takes the theme palette's
|
|
59
|
-
* entry for its own index, which is what makes a chart nobody has themed come
|
|
60
|
-
* out in five distinct colours rather than five identical ones.
|
|
61
|
-
*/
|
|
32
|
+
/** One series of a chart: a column of the table, captioned and painted. */
|
|
62
33
|
export interface ChartSeries {
|
|
63
34
|
/** Key into each data row for this series' value. */
|
|
64
35
|
field: string
|
|
65
|
-
/** Legend caption for this series. */
|
|
66
36
|
label: string
|
|
67
|
-
/**
|
|
68
|
-
* Paint override for this series' mark and its legend swatch. Omit to take
|
|
69
|
-
* the theme palette's entry for this series' index.
|
|
70
|
-
*/
|
|
37
|
+
/** Overrides the theme palette's entry for this series' index, for its mark and legend swatch. */
|
|
71
38
|
fill?: Fill
|
|
72
|
-
/**
|
|
39
|
+
/** Defaults to `true`. */
|
|
73
40
|
enabled?: boolean
|
|
74
41
|
}
|
|
75
42
|
|
|
76
43
|
/** A `[min, max]` numeric range in data units. */
|
|
77
44
|
export type Range = [min: number, max: number]
|
|
78
45
|
|
|
79
|
-
/**
|
|
80
|
-
* One axis's *data* domain — what it shows — as opposed to {@link AxisConfig},
|
|
81
|
-
* which is how it's *styled*. Every field is optional: `range`/`ticks` derive
|
|
82
|
-
* from the plotted data when omitted, and `label` defaults to `""`.
|
|
83
|
-
*/
|
|
46
|
+
/** One axis's *data* domain, as opposed to {@link AxisConfig}, which is how it is styled. */
|
|
84
47
|
export interface AxisDomainConfig {
|
|
85
|
-
/** Caption drawn alongside the axis
|
|
48
|
+
/** Caption drawn alongside the axis; `""` hides it. */
|
|
86
49
|
label?: string
|
|
87
|
-
/**
|
|
50
|
+
/** Derived from the data bounds when omitted. */
|
|
88
51
|
range?: Range
|
|
89
|
-
/**
|
|
52
|
+
/** Derived (five evenly spaced) when omitted. */
|
|
90
53
|
ticks?: number[]
|
|
91
54
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* Here rather than in {@link AxisConfig} because it is a statement about the
|
|
96
|
-
* *numbers*, not about the ink they are set in: `$` on a revenue axis says
|
|
97
|
-
* what the column holds, and it has to survive a re-theme. It sits beside
|
|
98
|
-
* `range` and `ticks` for the same reason those do.
|
|
55
|
+
* A `number-format` pattern such as `#,##0.00` or `$#,##0`. It lives here, not
|
|
56
|
+
* in {@link AxisConfig}, because it says what the column holds and has to
|
|
57
|
+
* survive a re-theme.
|
|
99
58
|
*/
|
|
100
59
|
format?: string
|
|
101
60
|
}
|
|
102
61
|
|
|
62
|
+
/** An {@link AxisDomainConfig} with `range` and `ticks` worked out. */
|
|
63
|
+
export type ResolvedAxisDomain = AxisDomainConfig & { readonly range: Range; readonly ticks: number[] }
|
|
64
|
+
|
|
103
65
|
/**
|
|
104
|
-
*
|
|
105
|
-
* `
|
|
106
|
-
*
|
|
107
|
-
* after the merge is the auto-derive signal the scale getters read.
|
|
66
|
+
* Merges partial input onto `previous`, so a later `.set({ yAxis: {...} })`
|
|
67
|
+
* patches `label`/`range`/`ticks` independently. An unset `range`/`ticks` is the
|
|
68
|
+
* auto-derive signal.
|
|
108
69
|
*/
|
|
109
70
|
export function resolveAxisDomain(
|
|
110
71
|
domain: AxisDomainConfig | undefined,
|
|
@@ -119,10 +80,9 @@ export function resolveAxisDomain(
|
|
|
119
80
|
}
|
|
120
81
|
|
|
121
82
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* auto-derive mode, step at the midpoint.
|
|
83
|
+
* Interpolates `range` and `ticks` numerically (ticks pairwise by index).
|
|
84
|
+
* `label`, `format`, and either field when one side is auto, step at the
|
|
85
|
+
* midpoint: there is no halfway between `$#,##0` and `0.0%`.
|
|
126
86
|
*/
|
|
127
87
|
export function lerpAxisDomain(
|
|
128
88
|
from: AxisDomainConfig,
|
|
@@ -133,8 +93,6 @@ export function lerpAxisDomain(
|
|
|
133
93
|
label: t < 0.5 ? from.label : to.label,
|
|
134
94
|
range: lerpOptionalRange(from.range, to.range, t),
|
|
135
95
|
ticks: lerpOptionalTicks(from.ticks, to.ticks, t),
|
|
136
|
-
// Steps at the midpoint with the caption, for the same reason: there is no
|
|
137
|
-
// halfway between `$#,##0` and `0.0%`.
|
|
138
96
|
format: t < 0.5 ? from.format : to.format,
|
|
139
97
|
}
|
|
140
98
|
}
|
|
@@ -162,32 +120,6 @@ function lerpOptionalTicks(
|
|
|
162
120
|
return out
|
|
163
121
|
}
|
|
164
122
|
|
|
165
|
-
/**
|
|
166
|
-
* Where a generic `to()` of an axis domain should land, given the domain as it
|
|
167
|
-
* is *drawn* (`drawn`, with `range` and `ticks` resolved off the data), or
|
|
168
|
-
* `undefined` when the request states neither a `range` nor `ticks` and the
|
|
169
|
-
* plain property tween will do.
|
|
170
|
-
*
|
|
171
|
-
* The stored domain of an auto axis has no `range` and no `ticks`, and
|
|
172
|
-
* {@link lerpAxisDomain} steps an unset side at the midpoint, so tweening the
|
|
173
|
-
* stored value from an auto axis is a cut. The charts therefore tween between
|
|
174
|
-
* resolved domains, as their zoom does; this builds the destination. A `range`
|
|
175
|
-
* given without `ticks` keeps the tick *count* the axis already draws, so each
|
|
176
|
-
* marker slides from its old value to its new one instead of the row popping.
|
|
177
|
-
*/
|
|
178
|
-
export function axisTweenTarget(
|
|
179
|
-
drawn: AxisDomainConfig,
|
|
180
|
-
requested: AxisDomainConfig | undefined
|
|
181
|
-
): AxisDomainConfig | undefined {
|
|
182
|
-
if (requested?.range === undefined && requested?.ticks === undefined) return undefined
|
|
183
|
-
const target = resolveAxisDomain(requested, drawn)
|
|
184
|
-
if (requested.ticks === undefined && requested.range !== undefined) {
|
|
185
|
-
const count = Math.max(2, drawn.ticks?.length ?? 2)
|
|
186
|
-
target.ticks = markerValues(requested.range, count - 1)
|
|
187
|
-
}
|
|
188
|
-
return target
|
|
189
|
-
}
|
|
190
|
-
|
|
191
123
|
/**
|
|
192
124
|
* An axis domain part-way through a tween that leaves or reaches a `range` or
|
|
193
125
|
* `ticks` left to the data: both ends as stored, and how far along. Only the
|
|
@@ -204,12 +136,11 @@ function blendOf(domain: AxisDomainConfig): AxisDomainBlend["blend"] | undefined
|
|
|
204
136
|
}
|
|
205
137
|
|
|
206
138
|
/**
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
* draws: a rescale from an auto axis is continuous, not a cut at the midpoint.
|
|
139
|
+
* The axis entry tween behind `to({ yAxis: { range } })`. Between explicit
|
|
140
|
+
* domains it is {@link lerpAxisDomain}, and a restated `range` without `ticks`
|
|
141
|
+
* spreads ticks across it, keeping their count. When either end leaves `range`
|
|
142
|
+
* or `ticks` to the data, the value between is an {@link AxisDomainBlend}, so a
|
|
143
|
+
* rescale from an auto axis is continuous rather than a cut at the midpoint.
|
|
213
144
|
*/
|
|
214
145
|
export function lerpAxisEntry(from: AxisDomainConfig, to: AxisDomainConfig, t: number): AxisDomainConfig {
|
|
215
146
|
// The mapper merged the request onto `from`, so a field it left unstated is `from`'s own, by identity.
|
|
@@ -227,30 +158,21 @@ export function lerpAxisEntry(from: AxisDomainConfig, to: AxisDomainConfig, t: n
|
|
|
227
158
|
return blend
|
|
228
159
|
}
|
|
229
160
|
|
|
230
|
-
/**
|
|
231
|
-
* `domain` as the chart draws it: `drawn` resolves a plain domain's auto `range`
|
|
232
|
-
* and `ticks` off the data, and a blend is the lerp of what its two ends draw.
|
|
233
|
-
*/
|
|
161
|
+
/** `domain` as drawn: `drawn` resolves a plain domain's auto `range` and `ticks`; a blend is the lerp of what its ends draw. */
|
|
234
162
|
export function settleAxisDomain(domain: AxisDomainConfig, drawn: (domain: AxisDomainConfig) => AxisDomainConfig): AxisDomainConfig {
|
|
235
163
|
const blend = blendOf(domain)
|
|
236
164
|
if (!blend) return drawn(domain)
|
|
237
165
|
return lerpAxisDomain(settleAxisDomain(blend.from, drawn), settleAxisDomain(blend.to, drawn), blend.t)
|
|
238
166
|
}
|
|
239
167
|
|
|
240
|
-
/**
|
|
241
|
-
* The low-level colour tokens the default themes are built from — a dark plot
|
|
242
|
-
* panel with warm-grey axis furniture.
|
|
243
|
-
*/
|
|
168
|
+
/** The colour tokens the default themes are built from: a dark plot panel with warm-grey axis furniture. */
|
|
244
169
|
export const CHART_PALETTE = {
|
|
245
|
-
/** Fill of the plot panel behind the marks. */
|
|
246
170
|
plotFill: "#15130e",
|
|
247
|
-
/** Colour of the plot's thick border. */
|
|
248
171
|
borderColor: "#4a4036",
|
|
249
|
-
/** Colour of the faint grid lines spanning the plot. */
|
|
250
172
|
gridColor: "#2c2820",
|
|
251
|
-
/**
|
|
173
|
+
/** Axis markers, axis captions and legend labels. */
|
|
252
174
|
markerColor: "#8a7f70",
|
|
253
|
-
/**
|
|
175
|
+
/** A spotlit region's dashed rules and wash. */
|
|
254
176
|
regionColor: "#d99a3a",
|
|
255
177
|
} as const
|
|
256
178
|
|
|
@@ -263,26 +185,12 @@ export const DEFAULT_SERIES_COLORS = [
|
|
|
263
185
|
"#9ec85a",
|
|
264
186
|
] as const
|
|
265
187
|
|
|
266
|
-
/**
|
|
267
|
-
* Opacity an *un*-spotlighted series fades to — its mark and its legend entry
|
|
268
|
-
* alike, so the two dim together.
|
|
269
|
-
*/
|
|
188
|
+
/** Opacity an un-spotlighted series fades to, mark and legend entry alike. */
|
|
270
189
|
export const SPOTLIGHT_DIM = 0.2
|
|
271
190
|
|
|
272
|
-
/** Clamp a number into `[0, 1]` — the range every reveal signal here runs in. */
|
|
273
|
-
export function clampUnit(value: number): number {
|
|
274
|
-
return value < 0 ? 0 : value > 1 ? 1 : value
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// ---- Row readers ---------------------------------------------------------
|
|
278
|
-
|
|
279
191
|
/**
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
* A column left as strings silently plots as 0, which is why the CSV parser's
|
|
284
|
-
* numeric auto-coercion matters — a quoted or unit-suffixed column won't plot
|
|
285
|
-
* until it's coerced.
|
|
192
|
+
* Reads a numeric field off a row. A string column plots as 0, so a quoted or
|
|
193
|
+
* unit-suffixed column needs the CSV parser's numeric coercion first.
|
|
286
194
|
*/
|
|
287
195
|
export function num(row: DataRecord, field: string | undefined): number {
|
|
288
196
|
if (field === undefined) return 0
|
|
@@ -292,12 +200,7 @@ export function num(row: DataRecord, field: string | undefined): number {
|
|
|
292
200
|
return typeof v === "number" && Number.isFinite(v) ? v : 0
|
|
293
201
|
}
|
|
294
202
|
|
|
295
|
-
/**
|
|
296
|
-
* Read a field off a row as its caption — the counterpart to {@link num} for
|
|
297
|
-
* the columns a chart *names* things by rather than measures them by (a bar's
|
|
298
|
-
* category, a wedge's slice). A missing cell reads as empty rather than as the
|
|
299
|
-
* string `"undefined"`.
|
|
300
|
-
*/
|
|
203
|
+
/** The counterpart to {@link num} for the columns a chart names things by; a missing cell reads as empty, not `"undefined"`. */
|
|
301
204
|
export function text(row: DataRecord, field: string | undefined): string {
|
|
302
205
|
if (field === undefined || field === "") return ""
|
|
303
206
|
const v = row[field]
|
|
@@ -305,13 +208,9 @@ export function text(row: DataRecord, field: string | undefined): string {
|
|
|
305
208
|
}
|
|
306
209
|
|
|
307
210
|
/**
|
|
308
|
-
* Every column
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
* A column counts as numeric only if every non-empty cell in it is a number, so
|
|
312
|
-
* a stray text cell leaves the column out rather than plotting zeros. This is
|
|
313
|
-
* what lets a chart plot "every other column" without the author naming twenty
|
|
314
|
-
* fields.
|
|
211
|
+
* Every numeric column of `data` in header order, skipping `exclude`. A column
|
|
212
|
+
* is numeric only if every non-empty cell is a number, so a stray text cell
|
|
213
|
+
* drops the column rather than plotting zeros.
|
|
315
214
|
*/
|
|
316
215
|
export function numericFields(
|
|
317
216
|
data: DataRecord[],
|
|
@@ -340,29 +239,20 @@ export function numericFields(
|
|
|
340
239
|
})
|
|
341
240
|
}
|
|
342
241
|
|
|
343
|
-
// ---- Theme branches ------------------------------------------------------
|
|
344
|
-
|
|
345
242
|
/** One axis's (x or y) style vocabulary. */
|
|
346
243
|
export interface AxisConfig {
|
|
347
244
|
/** Style of each marker along the axis. */
|
|
348
245
|
labelStyle: TextStyle
|
|
349
|
-
/** Style of the axis caption. */
|
|
350
246
|
titleStyle: TextStyle
|
|
351
|
-
/**
|
|
352
|
-
* This axis's direction of the plot's internal grid — weight+fill honoured,
|
|
353
|
-
* dash/cap/join ignored (grid lines are drawn as filled rects).
|
|
354
|
-
*/
|
|
247
|
+
/** This axis's grid lines: weight and fill honoured, dash/cap/join ignored (they are filled rects). */
|
|
355
248
|
lineStyle: Stroke
|
|
356
|
-
/**
|
|
357
|
-
* Gap between the plot and the marker row. Only the side facing the plot is
|
|
358
|
-
* read — `.top` for a horizontal axis, `.right` for a vertical one.
|
|
359
|
-
*/
|
|
249
|
+
/** Gap between the plot and the marker row; only the side facing the plot is read (`.top` horizontal, `.right` vertical). */
|
|
360
250
|
tickPadding: Insets
|
|
361
251
|
/** Gap between the marker row and the caption, read the same side. */
|
|
362
252
|
titlePadding: Insets
|
|
363
253
|
}
|
|
364
254
|
|
|
365
|
-
/** A legend's per-series colour swatch
|
|
255
|
+
/** A legend's per-series colour swatch. */
|
|
366
256
|
export interface LegendMarkerConfig {
|
|
367
257
|
width: number
|
|
368
258
|
height: number
|
|
@@ -371,12 +261,10 @@ export interface LegendMarkerConfig {
|
|
|
371
261
|
shadow: Shadow
|
|
372
262
|
}
|
|
373
263
|
|
|
374
|
-
/** A legend's visual theme. */
|
|
375
264
|
export interface LegendConfig {
|
|
376
265
|
padding: Insets
|
|
377
|
-
/**
|
|
266
|
+
/** Only the swatch carries the series colour. */
|
|
378
267
|
textStyle: TextStyle
|
|
379
|
-
/** Gap between legend entries. */
|
|
380
268
|
itemGap: number
|
|
381
269
|
markerStyle: LegendMarkerConfig
|
|
382
270
|
}
|
|
@@ -402,7 +290,6 @@ const AXIS_TITLE_STYLE: TextStyle = {
|
|
|
402
290
|
}
|
|
403
291
|
const AXIS_LINE_STYLE: Stroke = { weight: 2, fill: CHART_PALETTE.gridColor }
|
|
404
292
|
|
|
405
|
-
/** The default styling of a horizontal (marker row beneath the plot) axis. */
|
|
406
293
|
export const DEFAULT_X_AXIS: AxisConfig = {
|
|
407
294
|
labelStyle: AXIS_LABEL_STYLE,
|
|
408
295
|
titleStyle: AXIS_TITLE_STYLE,
|
|
@@ -411,7 +298,6 @@ export const DEFAULT_X_AXIS: AxisConfig = {
|
|
|
411
298
|
titlePadding: 4,
|
|
412
299
|
}
|
|
413
300
|
|
|
414
|
-
/** The default styling of a vertical (marker column beside the plot) axis. */
|
|
415
301
|
export const DEFAULT_Y_AXIS: AxisConfig = {
|
|
416
302
|
labelStyle: { ...AXIS_LABEL_STYLE, fontWeight: 500, textAlign: "right" },
|
|
417
303
|
titleStyle: AXIS_TITLE_STYLE,
|
|
@@ -420,18 +306,13 @@ export const DEFAULT_Y_AXIS: AxisConfig = {
|
|
|
420
306
|
titlePadding: 24,
|
|
421
307
|
}
|
|
422
308
|
|
|
423
|
-
/**
|
|
424
|
-
* The default plot panel: **unpainted**, framed. A chart reads against the
|
|
425
|
-
* stage rather than punching a panel into it, which is the look the family was
|
|
426
|
-
* designed at.
|
|
427
|
-
*/
|
|
309
|
+
/** Unpainted but framed: a chart reads against the stage rather than punching a panel into it. */
|
|
428
310
|
export const DEFAULT_PLOT_AREA: PlotAreaConfig = {
|
|
429
311
|
fill: [],
|
|
430
312
|
stroke: { weight: 6, fill: CHART_PALETTE.borderColor },
|
|
431
313
|
shadow: [],
|
|
432
314
|
}
|
|
433
315
|
|
|
434
|
-
/** The default legend styling, shared by every chart that draws one. */
|
|
435
316
|
export const DEFAULT_LEGEND: LegendConfig = {
|
|
436
317
|
padding: 0,
|
|
437
318
|
textStyle: {
|
|
@@ -450,21 +331,16 @@ export const DEFAULT_LEGEND: LegendConfig = {
|
|
|
450
331
|
},
|
|
451
332
|
}
|
|
452
333
|
|
|
453
|
-
/**
|
|
334
|
+
/** {@link DEFAULT_SERIES_COLORS} as 4px strokes. */
|
|
454
335
|
export const DEFAULT_SERIES_STROKES: Stroke[] = DEFAULT_SERIES_COLORS.map(
|
|
455
336
|
(fill) => ({ weight: 4, fill })
|
|
456
337
|
)
|
|
457
338
|
|
|
458
|
-
/**
|
|
339
|
+
/** {@link DEFAULT_SERIES_COLORS} as solid fills. */
|
|
459
340
|
export const DEFAULT_SERIES_FILLS: Fill[] = DEFAULT_SERIES_COLORS.map(
|
|
460
341
|
(fill) => fill
|
|
461
342
|
)
|
|
462
343
|
|
|
463
|
-
// ---- Theme branch resolvers ----------------------------------------------
|
|
464
|
-
// Each chart type assembles its own theme out of these, so "the axis furniture"
|
|
465
|
-
// means one thing across the family and each chart still declares only the
|
|
466
|
-
// branches it actually draws.
|
|
467
|
-
|
|
468
344
|
export function resolveAxisConfig(
|
|
469
345
|
axis: Partial<AxisConfig> | undefined,
|
|
470
346
|
previous: AxisConfig
|
|
@@ -580,7 +456,7 @@ function lerpLegendMarkerConfig(
|
|
|
580
456
|
}
|
|
581
457
|
}
|
|
582
458
|
|
|
583
|
-
/** A legend theme as the author may state it
|
|
459
|
+
/** A legend theme as the author may state it: every branch optional. */
|
|
584
460
|
export type LegendTheme = Partial<Omit<LegendConfig, "markerStyle">> & {
|
|
585
461
|
markerStyle?: Partial<LegendMarkerConfig>
|
|
586
462
|
}
|
|
@@ -620,7 +496,7 @@ export function lerpLegendConfig(
|
|
|
620
496
|
}
|
|
621
497
|
}
|
|
622
498
|
|
|
623
|
-
/** A
|
|
499
|
+
/** A palette override replaces the previous palette outright. */
|
|
624
500
|
export function resolveSeriesStrokes(
|
|
625
501
|
styles: Stroke[] | undefined,
|
|
626
502
|
previous: Stroke[]
|
|
@@ -664,12 +540,9 @@ export function lerpSeriesFills(from: Fill[], to: Fill[], t: number): Fill[] {
|
|
|
664
540
|
return out
|
|
665
541
|
}
|
|
666
542
|
|
|
667
|
-
// ---- Per-series paint ----------------------------------------------------
|
|
668
|
-
|
|
669
543
|
/**
|
|
670
|
-
* The
|
|
671
|
-
*
|
|
672
|
-
* the last (topmost) layer's colour.
|
|
544
|
+
* The stroke for series `index`: the palette's entry at `index % palette.length`,
|
|
545
|
+
* with the series' own `fill`, if set, replacing just the topmost layer's colour.
|
|
673
546
|
*/
|
|
674
547
|
export function resolveSeriesStroke(
|
|
675
548
|
palette: Stroke[],
|
|
@@ -691,11 +564,7 @@ export function resolveSeriesStroke(
|
|
|
691
564
|
return next
|
|
692
565
|
}
|
|
693
566
|
|
|
694
|
-
/**
|
|
695
|
-
* The effective *colour* for series `index` — the same colour
|
|
696
|
-
* {@link resolveSeriesStroke} draws its line in — for contexts (a legend
|
|
697
|
-
* swatch) that only need the fill.
|
|
698
|
-
*/
|
|
567
|
+
/** The colour {@link resolveSeriesStroke} draws series `index` in, for a legend swatch. */
|
|
699
568
|
export function resolveSeriesFill(
|
|
700
569
|
palette: Stroke[],
|
|
701
570
|
series: ChartSeries,
|
|
@@ -706,34 +575,9 @@ export function resolveSeriesFill(
|
|
|
706
575
|
}
|
|
707
576
|
|
|
708
577
|
/**
|
|
709
|
-
*
|
|
710
|
-
* the
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
* The stroke twin above has to reach inside a stroke to swap one layer's colour;
|
|
714
|
-
* a filled mark has no such geometry wrapped around its paint, so an override is
|
|
715
|
-
* simply the whole answer.
|
|
716
|
-
*/
|
|
717
|
-
export function resolvePalettePaint(
|
|
718
|
-
palette: Fill[],
|
|
719
|
-
override: Fill | undefined,
|
|
720
|
-
index: number
|
|
721
|
-
): Fill {
|
|
722
|
-
if (override != null) return override
|
|
723
|
-
if (palette.length === 0) return "white"
|
|
724
|
-
return palette[index % palette.length]
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
// ---- Scales and formatting -----------------------------------------------
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
/**
|
|
731
|
-
* Format a marker value: integers plain, otherwise trimmed to one decimal, or to
|
|
732
|
-
* as many as the axis's tick `step` needs to tell neighbouring ticks apart.
|
|
733
|
-
*
|
|
734
|
-
* One decimal is the floor, so a step of 0.1 or more reads exactly as it always
|
|
735
|
-
* has; a finer step (a unit axis ticked every 0.05) gets two, instead of five
|
|
736
|
-
* ticks captioned `0.1 0.1 0.2 0.2 0.3`. Trailing zeros are dropped either way.
|
|
578
|
+
* Format a marker value: integers plain, otherwise one decimal, or as many as
|
|
579
|
+
* the tick `step` needs to tell neighbours apart (a unit axis ticked every 0.05
|
|
580
|
+
* gets two, not `0.1 0.1 0.2 0.2 0.3`). Trailing zeros are dropped.
|
|
737
581
|
*/
|
|
738
582
|
export function formatMarker(value: number, step?: number): string {
|
|
739
583
|
if (Number.isInteger(value)) return String(value)
|
|
@@ -775,8 +619,3 @@ export function markerValues([min, max]: Range, markerCount: number): number[] {
|
|
|
775
619
|
const n = Math.max(2, Math.round(markerCount))
|
|
776
620
|
return Array.from({ length: n + 1 }, (_, i) => min + ((max - min) / n) * i)
|
|
777
621
|
}
|
|
778
|
-
|
|
779
|
-
// Lifted into the engine, because a graph and a diagram want them too — see
|
|
780
|
-
// `@motionscript/sdk/component`. Re-exported so a chart, and anything that
|
|
781
|
-
// copied one, still reaches them where it always did.
|
|
782
|
-
export { niceStep, enterBudget } from "@motionscript/sdk/component"
|
package/src/kit/vertical-axis.ts
CHANGED
|
@@ -7,51 +7,23 @@ import {
|
|
|
7
7
|
insetsOps,
|
|
8
8
|
textOps,
|
|
9
9
|
type NodeOf,
|
|
10
|
+
// Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
|
|
10
11
|
type Node2D,
|
|
11
|
-
type Node2DProps,
|
|
12
|
+
type Node2DProps as _Node2DProps,
|
|
12
13
|
} from "@motionscript/sdk"
|
|
14
|
+
import { enterBudget } from "@motionscript/sdk/component"
|
|
13
15
|
|
|
14
16
|
import { asParts, fresh } from "./entries"
|
|
15
17
|
import { armed, arrival, arrivalOf, arrivalsEntry, beats, frameEase, type Arrival } from "./motion"
|
|
16
18
|
import {
|
|
17
19
|
DEFAULT_Y_AXIS,
|
|
18
|
-
enterBudget,
|
|
19
20
|
formatMarker,
|
|
20
21
|
type AxisConfig,
|
|
21
22
|
type Range,
|
|
22
23
|
} from "./shared"
|
|
23
24
|
|
|
24
|
-
export interface VerticalAxisProps extends Node2DProps {
|
|
25
|
-
/** `[min, max]` of the vertical scale the markers are positioned within. */
|
|
26
|
-
range: Range
|
|
27
|
-
/**
|
|
28
|
-
* Tick values (in data units) to draw up the axis, each placed by its fraction
|
|
29
|
-
* across {@link VerticalAxisProps.range}. Need not be evenly spaced or include
|
|
30
|
-
* the ends.
|
|
31
|
-
*/
|
|
32
|
-
markers: number[]
|
|
33
|
-
/**
|
|
34
|
-
* How each marker is captioned, one per entry of `markers` and in its order.
|
|
35
|
-
* Empty (the default), or short, captions a marker with its bare numeric
|
|
36
|
-
* reading; a chart with a number format on this axis writes them through it.
|
|
37
|
-
*/
|
|
38
|
-
captions: string[]
|
|
39
|
-
/** Marker/caption text style, grid stroke, and gutters — see {@link AxisConfig}. */
|
|
40
|
-
axis: AxisConfig
|
|
41
|
-
/**
|
|
42
|
-
* Rotated caption drawn in the gutter to the left of the marker column. Empty
|
|
43
|
-
* string (the default) hides it — no caption node is built and no label gutter
|
|
44
|
-
* is reserved.
|
|
45
|
-
*/
|
|
46
|
-
label: string
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** How far a marker starts to the right of its slot when it enters, in px. */
|
|
50
25
|
const SLIDE = 24
|
|
51
26
|
|
|
52
|
-
/** How a tick value is captioned — see {@link VerticalAxisProps.captions}. */
|
|
53
|
-
export type MarkerLabel = (value: number) => string
|
|
54
|
-
|
|
55
27
|
/** What a marker's caption reads: the one listed for its value, else its bare reading. */
|
|
56
28
|
export function markerCaption(s: { readonly markers: readonly number[]; readonly captions: readonly string[] }, value: number): string {
|
|
57
29
|
const index = s.markers.indexOf(value)
|
|
@@ -59,55 +31,41 @@ export function markerCaption(s: { readonly markers: readonly number[]; readonly
|
|
|
59
31
|
}
|
|
60
32
|
|
|
61
33
|
/**
|
|
62
|
-
* The numeric vertical axis
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* Each marker is positioned so its centre lands exactly on its value's fraction
|
|
68
|
-
* across `range` within the axis's filled height, so unevenly-spaced markers
|
|
69
|
-
* land where their values fall. Both the caption and the position are *bound*,
|
|
70
|
-
* not snapshotted: a chart that rescales its axes (see `LineChart.zoomTo`)
|
|
71
|
-
* tweens `range`/`markers` live. Only the marker **count** is fixed at
|
|
72
|
-
* construction.
|
|
73
|
-
*
|
|
74
|
-
* `enter` staggers them in **bottom-to-top**: each marker starts pushed a
|
|
75
|
-
* little to the right and transparent, then fades and slides left into place.
|
|
76
|
-
* The resting state is the landed one, so a chart that is never told to enter
|
|
77
|
-
* still reads as a finished chart.
|
|
34
|
+
* The numeric vertical axis the cartesian charts share: a column of {@link Text}
|
|
35
|
+
* markers, highest at the top, each centred on its value's fraction across
|
|
36
|
+
* `range`. Caption and position are bound, so a rescale tweens live; only the
|
|
37
|
+
* marker count is fixed at construction. The resting state is landed.
|
|
78
38
|
*/
|
|
79
39
|
export const VerticalAxis = component2d({
|
|
80
40
|
key: "verticalAxis",
|
|
81
41
|
state: {
|
|
82
42
|
range: fresh<Range>(() => [0, 1]),
|
|
43
|
+
/** Tick values in data units, each placed by its fraction across `range`; need not be even or include the ends. */
|
|
83
44
|
markers: fresh<number[]>(() => []),
|
|
45
|
+
/** One caption per marker in its order; a missing one reads as the bare numeric value. */
|
|
84
46
|
captions: fresh<string[]>(() => []),
|
|
85
47
|
axis: Types.custom<AxisConfig>(DEFAULT_Y_AXIS),
|
|
48
|
+
/** Rotated caption in the gutter left of the markers; empty builds no caption node and reserves no gutter. */
|
|
86
49
|
label: "",
|
|
87
50
|
arrivals: arrivalsEntry(),
|
|
88
51
|
},
|
|
89
|
-
// A
|
|
52
|
+
// A node made of markers would otherwise hug them.
|
|
90
53
|
defaults: { width: "fill", height: "fill" },
|
|
91
54
|
children: (state) => {
|
|
92
|
-
// Composed from plain entries (range/markers/label), so it's built once here.
|
|
93
55
|
const tickPad = insetsOps.resolve(state.axis.tickPadding).right
|
|
94
56
|
const titlePad = insetsOps.resolve(state.axis.titlePadding).right
|
|
95
57
|
|
|
96
58
|
const row: Node2D[] = []
|
|
97
|
-
// The row's parent is the axis
|
|
59
|
+
// The row's parent is the axis, whose box the markers are placed in.
|
|
98
60
|
const box = () => (row[0]?.parent as Node2D | null | undefined)?.layoutBounds ?? { width: 0, height: 0 }
|
|
99
|
-
/** The live tick values, top → bottom (highest first) — re-read every frame. */
|
|
100
61
|
const sorted = () => [...state.markers].sort((a, b) => b - a)
|
|
101
|
-
/** Tick `i`'s fraction across the live `range`, 0 at the bottom edge. */
|
|
102
62
|
const fracAt = (value: number | undefined) => {
|
|
103
63
|
if (value === undefined) return 0
|
|
104
64
|
const [min, max] = state.range
|
|
105
65
|
return max <= min ? 0 : (value - min) / (max - min)
|
|
106
66
|
}
|
|
107
67
|
|
|
108
|
-
//
|
|
109
|
-
// live list is shorter than the column built for it) renders as empty text
|
|
110
|
-
// rather than repeating its neighbour.
|
|
68
|
+
// A slot with no value (the live list is shorter than the column built) renders empty, not its neighbour.
|
|
111
69
|
const markers = state.markers.map((_, i) => {
|
|
112
70
|
const marker: Node2D = new Text({
|
|
113
71
|
...textOps.styleProps(state.axis.labelStyle),
|
|
@@ -119,10 +77,7 @@ export const VerticalAxis = component2d({
|
|
|
119
77
|
// Node2D space is y-up, so the fraction runs from the bottom.
|
|
120
78
|
const height = box().height
|
|
121
79
|
const centred = (fracAt(sorted()[i]) - 0.5) * height
|
|
122
|
-
// Held inside the
|
|
123
|
-
// headroom is smaller than its marker text is tall, which a theme with
|
|
124
|
-
// much larger markers can arrange. Only the top, because what lies
|
|
125
|
-
// below the axis is the chart's own horizontal axis and legend.
|
|
80
|
+
// Held inside the top edge for a theme whose markers are taller than the headroom; only the top, since the chart's own axis and legend lie below.
|
|
126
81
|
const limit = height / 2 - marker.layoutBounds.height / 2
|
|
127
82
|
return Math.min(centred, limit)
|
|
128
83
|
},
|
|
@@ -155,16 +110,9 @@ export const VerticalAxis = component2d({
|
|
|
155
110
|
},
|
|
156
111
|
commands: {
|
|
157
112
|
/**
|
|
158
|
-
* Stagger
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* The timeline's duration is the **whole** entrance, not one marker's slide:
|
|
163
|
-
* the stagger and the caption are carved out of it rather than added on top.
|
|
164
|
-
* See {@link enterBudget} for why that distinction matters here.
|
|
165
|
-
*
|
|
166
|
-
* `stagger` is seconds between successive markers (derived when omitted);
|
|
167
|
-
* `slide` is the starting rightward offset in px.
|
|
113
|
+
* Stagger markers in bottom to top, then the caption. The duration is the
|
|
114
|
+
* whole entrance ({@link enterBudget}). `stagger` is seconds between markers
|
|
115
|
+
* (derived when omitted); `slide` is the starting rightward offset in px.
|
|
168
116
|
*/
|
|
169
117
|
enter: {
|
|
170
118
|
args: {
|
|
@@ -181,9 +129,8 @@ export const VerticalAxis = component2d({
|
|
|
181
129
|
if (count === 0) return arm
|
|
182
130
|
const { step, per, caption } = enterBudget(timeline.duration, count, captioned, stagger)
|
|
183
131
|
const ease = frameEase(timeline)
|
|
184
|
-
//
|
|
132
|
+
// The bottom marker leads, so walk the top-to-bottom column in reverse.
|
|
185
133
|
const markers = state.markers.map((_, i) => ({ at: (count - 1 - i) * step, duration: per, piece: arrival(timeline, i, from, ease) }))
|
|
186
|
-
// Once every marker has landed, the caption slides in from the right.
|
|
187
134
|
const label = captioned ? [{ at: (count - 1) * step + per, duration: caption, piece: arrival(timeline, "label", from, ease) }] : []
|
|
188
135
|
return timeline.together([arm, beats(timeline, [...markers, ...label])])
|
|
189
136
|
},
|