@motionscript/charts 0.1.0-alpha.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +99 -99
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/{chunk-XROBIRQK.js → chunk-TJNHLQGV.js} +1 -1
- package/dist/browser/chunks/chunk-TJNHLQGV.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/cartesian.js.map +1 -1
- package/dist/kit/chart-border.js.map +1 -1
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.js.map +1 -1
- package/dist/kit/motion.js.map +1 -1
- package/dist/kit/paints.js.map +1 -1
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +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.js.map +1 -1
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +23 -23
- package/src/bar-chart/bar-chart.ts +542 -542
- package/src/bar-chart/category-axis.ts +162 -162
- package/src/bar-chart/chart-bars.ts +287 -287
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +28 -28
- package/src/index.ts +5 -5
- package/src/kit/cartesian.ts +81 -81
- package/src/kit/chart-border.ts +50 -50
- package/src/kit/chart-canvas.ts +122 -122
- package/src/kit/chart-legend.ts +229 -229
- package/src/kit/entries.ts +20 -20
- package/src/kit/motion.ts +90 -90
- package/src/kit/paints.ts +15 -15
- package/src/kit/shared.ts +782 -782
- package/src/kit/vertical-axis.ts +194 -194
- package/src/line-chart/chart-line.ts +156 -156
- package/src/line-chart/chart-region.ts +90 -90
- package/src/line-chart/horizontal-axis.ts +151 -151
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +598 -598
- package/src/line-chart/shared.ts +473 -473
- package/src/line-chart/x-scale.ts +382 -382
- package/src/nodes.ts +18 -18
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +296 -296
- package/src/pie-chart/pie-legend.ts +169 -169
- package/src/pie-chart/pie-ring.ts +848 -848
- package/src/pie-chart/shared.ts +496 -496
- package/dist/browser/chunks/chunk-XROBIRQK.js.map +0 -7
- package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
package/src/kit/shared.ts
CHANGED
|
@@ -1,782 +1,782 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The vocabulary every chart node shares: what a *series* is, how an axis is
|
|
3
|
-
* scaled and formatted, and the theme leaves each chart's own theme is assembled
|
|
4
|
-
* from.
|
|
5
|
-
*
|
|
6
|
-
* This module exists because the three chart types are three ways of drawing the
|
|
7
|
-
* same table. A line chart, a bar chart and a pie chart disagree about almost
|
|
8
|
-
* everything visible — polylines against columns against wedges — and agree
|
|
9
|
-
* about almost everything underneath: a series is a column with a caption and a
|
|
10
|
-
* paint, a marker is a number formatted one way, a legend is a swatch beside a
|
|
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".
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
import {
|
|
32
|
-
fillOps,
|
|
33
|
-
lerpNumber,
|
|
34
|
-
shadowOps,
|
|
35
|
-
strokeOps,
|
|
36
|
-
insetsOps,
|
|
37
|
-
textOps,
|
|
38
|
-
type DataRecord,
|
|
39
|
-
type Fill,
|
|
40
|
-
type FillResolved,
|
|
41
|
-
type Insets,
|
|
42
|
-
type InsetsResolved,
|
|
43
|
-
type Shadow,
|
|
44
|
-
type ShadowResolved,
|
|
45
|
-
type Stroke,
|
|
46
|
-
type StrokeResolved,
|
|
47
|
-
type TextStyle,
|
|
48
|
-
} from "@motionscript/sdk"
|
|
49
|
-
|
|
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
|
-
*/
|
|
62
|
-
export interface ChartSeries {
|
|
63
|
-
/** Key into each data row for this series' value. */
|
|
64
|
-
field: string
|
|
65
|
-
/** Legend caption for this series. */
|
|
66
|
-
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
|
-
*/
|
|
71
|
-
fill?: Fill
|
|
72
|
-
/** Whether the series shows by default. Defaults to `true`. */
|
|
73
|
-
enabled?: boolean
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** A `[min, max]` numeric range in data units. */
|
|
77
|
-
export type Range = [min: number, max: number]
|
|
78
|
-
|
|
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
|
-
*/
|
|
84
|
-
export interface AxisDomainConfig {
|
|
85
|
-
/** Caption drawn alongside the axis. Pass `""` (the default) to hide it. */
|
|
86
|
-
label?: string
|
|
87
|
-
/** `[min, max]` of the scale. Derived from the data bounds when omitted. */
|
|
88
|
-
range?: Range
|
|
89
|
-
/** Tick values (in data units). Derived (five evenly-spaced) when omitted. */
|
|
90
|
-
ticks?: number[]
|
|
91
|
-
/**
|
|
92
|
-
* How a tick value is written down — a `number-format` pattern such as
|
|
93
|
-
* `#,##0.00` or `$#,##0`.
|
|
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.
|
|
99
|
-
*/
|
|
100
|
-
format?: string
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* An axis domain entry's resolve: merges the author's partial input onto
|
|
105
|
-
* `previous`, so a later partial `.set({ yAxis: {...} })` patches
|
|
106
|
-
* `label`/`range`/`ticks` independently. `range`/`ticks` staying `undefined`
|
|
107
|
-
* after the merge is the auto-derive signal the scale getters read.
|
|
108
|
-
*/
|
|
109
|
-
export function resolveAxisDomain(
|
|
110
|
-
domain: AxisDomainConfig | undefined,
|
|
111
|
-
previous?: AxisDomainConfig
|
|
112
|
-
): AxisDomainConfig {
|
|
113
|
-
return {
|
|
114
|
-
label: domain?.label ?? previous?.label ?? "",
|
|
115
|
-
range: domain?.range ?? previous?.range,
|
|
116
|
-
ticks: domain?.ticks ?? previous?.ticks,
|
|
117
|
-
format: domain?.format ?? previous?.format,
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* An axis domain entry's lerp: interpolates `range`/`ticks` numerically
|
|
123
|
-
* (pairwise by index for `ticks`), so a `to` command that restates a range
|
|
124
|
-
* rescales the plot smoothly. `label`, and either field when one side is in
|
|
125
|
-
* auto-derive mode, step at the midpoint.
|
|
126
|
-
*/
|
|
127
|
-
export function lerpAxisDomain(
|
|
128
|
-
from: AxisDomainConfig,
|
|
129
|
-
to: AxisDomainConfig,
|
|
130
|
-
t: number
|
|
131
|
-
): AxisDomainConfig {
|
|
132
|
-
return {
|
|
133
|
-
label: t < 0.5 ? from.label : to.label,
|
|
134
|
-
range: lerpOptionalRange(from.range, to.range, t),
|
|
135
|
-
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
|
-
format: t < 0.5 ? from.format : to.format,
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function lerpOptionalRange(
|
|
143
|
-
from: Range | undefined,
|
|
144
|
-
to: Range | undefined,
|
|
145
|
-
t: number
|
|
146
|
-
): Range | undefined {
|
|
147
|
-
if (!from || !to) return t < 0.5 ? from : to
|
|
148
|
-
return [lerpNumber(from[0], to[0], t), lerpNumber(from[1], to[1], t)]
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function lerpOptionalTicks(
|
|
152
|
-
from: number[] | undefined,
|
|
153
|
-
to: number[] | undefined,
|
|
154
|
-
t: number
|
|
155
|
-
): number[] | undefined {
|
|
156
|
-
if (!from || !to) return t < 0.5 ? from : to
|
|
157
|
-
const n = Math.max(from.length, to.length)
|
|
158
|
-
const out: number[] = []
|
|
159
|
-
for (let i = 0; i < n; i++) {
|
|
160
|
-
out.push(lerpNumber(from[i] ?? to[i], to[i] ?? from[i], t))
|
|
161
|
-
}
|
|
162
|
-
return out
|
|
163
|
-
}
|
|
164
|
-
|
|
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
|
-
/**
|
|
192
|
-
* An axis domain part-way through a tween that leaves or reaches a `range` or
|
|
193
|
-
* `ticks` left to the data: both ends as stored, and how far along. Only the
|
|
194
|
-
* chart knows what an auto side draws as, so {@link settleAxisDomain} resolves
|
|
195
|
-
* it there. Its own fields are the plain {@link lerpAxisDomain} of its ends, so
|
|
196
|
-
* a mapper merging onto one still reads a domain.
|
|
197
|
-
*/
|
|
198
|
-
export interface AxisDomainBlend extends AxisDomainConfig {
|
|
199
|
-
readonly blend: { readonly from: AxisDomainConfig; readonly to: AxisDomainConfig; readonly t: number }
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function blendOf(domain: AxisDomainConfig): AxisDomainBlend["blend"] | undefined {
|
|
203
|
-
return (domain as Partial<AxisDomainBlend>).blend
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* A chart's axis entry tween: what `to({ yAxis: { range } })` moves through.
|
|
208
|
-
* Between two explicit domains it is {@link lerpAxisDomain}, and a restated
|
|
209
|
-
* `range` without `ticks` spreads explicit ticks across it, keeping their count.
|
|
210
|
-
* When either end leaves `range` or `ticks` to the data, the value between is an
|
|
211
|
-
* {@link AxisDomainBlend}, which the chart draws as the blend of what each end
|
|
212
|
-
* draws: a rescale from an auto axis is continuous, not a cut at the midpoint.
|
|
213
|
-
*/
|
|
214
|
-
export function lerpAxisEntry(from: AxisDomainConfig, to: AxisDomainConfig, t: number): AxisDomainConfig {
|
|
215
|
-
// The mapper merged the request onto `from`, so a field it left unstated is `from`'s own, by identity.
|
|
216
|
-
const rangeMoved = to.range !== from.range
|
|
217
|
-
const target = rangeMoved && to.range && to.ticks && to.ticks === from.ticks
|
|
218
|
-
? { ...to, ticks: markerValues(to.range, Math.max(2, to.ticks.length) - 1) }
|
|
219
|
-
: to
|
|
220
|
-
const blended = blendOf(from) !== undefined
|
|
221
|
-
const moved = blended || rangeMoved || target.ticks !== from.ticks
|
|
222
|
-
const auto = blended || from.range === undefined || from.ticks === undefined || target.range === undefined || target.ticks === undefined
|
|
223
|
-
if (!moved || !auto) return lerpAxisDomain(from, target, t)
|
|
224
|
-
if (t === 0) return from
|
|
225
|
-
if (t === 1) return target
|
|
226
|
-
const blend: AxisDomainBlend = { ...lerpAxisDomain(from, target, t), blend: { from, to: target, t } }
|
|
227
|
-
return blend
|
|
228
|
-
}
|
|
229
|
-
|
|
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
|
-
*/
|
|
234
|
-
export function settleAxisDomain(domain: AxisDomainConfig, drawn: (domain: AxisDomainConfig) => AxisDomainConfig): AxisDomainConfig {
|
|
235
|
-
const blend = blendOf(domain)
|
|
236
|
-
if (!blend) return drawn(domain)
|
|
237
|
-
return lerpAxisDomain(settleAxisDomain(blend.from, drawn), settleAxisDomain(blend.to, drawn), blend.t)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* The low-level colour tokens the default themes are built from — a dark plot
|
|
242
|
-
* panel with warm-grey axis furniture.
|
|
243
|
-
*/
|
|
244
|
-
export const CHART_PALETTE = {
|
|
245
|
-
/** Fill of the plot panel behind the marks. */
|
|
246
|
-
plotFill: "#15130e",
|
|
247
|
-
/** Colour of the plot's thick border. */
|
|
248
|
-
borderColor: "#4a4036",
|
|
249
|
-
/** Colour of the faint grid lines spanning the plot. */
|
|
250
|
-
gridColor: "#2c2820",
|
|
251
|
-
/** Colour of the numeric axis markers, axis captions, and legend labels. */
|
|
252
|
-
markerColor: "#8a7f70",
|
|
253
|
-
/** Colour of a spotlit region's dashed rules and its wash. */
|
|
254
|
-
regionColor: "#d99a3a",
|
|
255
|
-
} as const
|
|
256
|
-
|
|
257
|
-
/** The out-of-the-box series palette, cycled by series index. */
|
|
258
|
-
export const DEFAULT_SERIES_COLORS = [
|
|
259
|
-
"#d9603b",
|
|
260
|
-
"#8194ad",
|
|
261
|
-
"#e0a93b",
|
|
262
|
-
"#5e9d6b",
|
|
263
|
-
"#9ec85a",
|
|
264
|
-
] as const
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Opacity an *un*-spotlighted series fades to — its mark and its legend entry
|
|
268
|
-
* alike, so the two dim together.
|
|
269
|
-
*/
|
|
270
|
-
export const SPOTLIGHT_DIM = 0.2
|
|
271
|
-
|
|
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
|
-
/**
|
|
280
|
-
* Read a numeric field off a row, defaulting missing, non-numeric and non-finite
|
|
281
|
-
* values to 0.
|
|
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.
|
|
286
|
-
*/
|
|
287
|
-
export function num(row: DataRecord, field: string | undefined): number {
|
|
288
|
-
if (field === undefined) return 0
|
|
289
|
-
const v = row[field]
|
|
290
|
-
// A `NaN` or infinite cell is no more a measurement than a missing one: read
|
|
291
|
-
// as 0, it cannot poison the scale and turn every point into `NaN`.
|
|
292
|
-
return typeof v === "number" && Number.isFinite(v) ? v : 0
|
|
293
|
-
}
|
|
294
|
-
|
|
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
|
-
*/
|
|
301
|
-
export function text(row: DataRecord, field: string | undefined): string {
|
|
302
|
-
if (field === undefined || field === "") return ""
|
|
303
|
-
const v = row[field]
|
|
304
|
-
return v === undefined || v === null ? "" : String(v)
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Every column that holds numbers across `data`, in header order, skipping the
|
|
309
|
-
* names in `exclude` (typically the category column).
|
|
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.
|
|
315
|
-
*/
|
|
316
|
-
export function numericFields(
|
|
317
|
-
data: DataRecord[],
|
|
318
|
-
exclude: string[] = []
|
|
319
|
-
): string[] {
|
|
320
|
-
if (data.length === 0) return []
|
|
321
|
-
const skip = new Set(exclude)
|
|
322
|
-
const seen = new Set<string>()
|
|
323
|
-
const order: string[] = []
|
|
324
|
-
for (const row of data)
|
|
325
|
-
for (const key of Object.keys(row))
|
|
326
|
-
if (!skip.has(key) && !seen.has(key)) {
|
|
327
|
-
seen.add(key)
|
|
328
|
-
order.push(key)
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
return order.filter((key) => {
|
|
332
|
-
let sawValue = false
|
|
333
|
-
for (const row of data) {
|
|
334
|
-
const v = row[key]
|
|
335
|
-
if (v === undefined || v === "") continue
|
|
336
|
-
if (typeof v !== "number") return false
|
|
337
|
-
sawValue = true
|
|
338
|
-
}
|
|
339
|
-
return sawValue
|
|
340
|
-
})
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
// ---- Theme branches ------------------------------------------------------
|
|
344
|
-
|
|
345
|
-
/** One axis's (x or y) style vocabulary. */
|
|
346
|
-
export interface AxisConfig {
|
|
347
|
-
/** Style of each marker along the axis. */
|
|
348
|
-
labelStyle: TextStyle
|
|
349
|
-
/** Style of the axis caption. */
|
|
350
|
-
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
|
-
*/
|
|
355
|
-
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
|
-
*/
|
|
360
|
-
tickPadding: Insets
|
|
361
|
-
/** Gap between the marker row and the caption, read the same side. */
|
|
362
|
-
titlePadding: Insets
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/** A legend's per-series colour swatch — a rounded chip, not the caption. */
|
|
366
|
-
export interface LegendMarkerConfig {
|
|
367
|
-
width: number
|
|
368
|
-
height: number
|
|
369
|
-
borderRadius: number
|
|
370
|
-
stroke: Stroke
|
|
371
|
-
shadow: Shadow
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
/** A legend's visual theme. */
|
|
375
|
-
export interface LegendConfig {
|
|
376
|
-
padding: Insets
|
|
377
|
-
/** Caption style — only the swatch carries the series colour. */
|
|
378
|
-
textStyle: TextStyle
|
|
379
|
-
/** Gap between legend entries. */
|
|
380
|
-
itemGap: number
|
|
381
|
-
markerStyle: LegendMarkerConfig
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
/** The plot panel a cartesian chart draws its marks over. */
|
|
385
|
-
export interface PlotAreaConfig {
|
|
386
|
-
stroke: Stroke
|
|
387
|
-
fill: Fill
|
|
388
|
-
shadow: Shadow
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
const AXIS_LABEL_STYLE: TextStyle = {
|
|
392
|
-
fontSize: 24,
|
|
393
|
-
fontWeight: 700,
|
|
394
|
-
textAlign: "center",
|
|
395
|
-
fill: CHART_PALETTE.markerColor,
|
|
396
|
-
}
|
|
397
|
-
const AXIS_TITLE_STYLE: TextStyle = {
|
|
398
|
-
fontSize: 30,
|
|
399
|
-
fontWeight: 600,
|
|
400
|
-
textAlign: "center",
|
|
401
|
-
fill: CHART_PALETTE.markerColor,
|
|
402
|
-
}
|
|
403
|
-
const AXIS_LINE_STYLE: Stroke = { weight: 2, fill: CHART_PALETTE.gridColor }
|
|
404
|
-
|
|
405
|
-
/** The default styling of a horizontal (marker row beneath the plot) axis. */
|
|
406
|
-
export const DEFAULT_X_AXIS: AxisConfig = {
|
|
407
|
-
labelStyle: AXIS_LABEL_STYLE,
|
|
408
|
-
titleStyle: AXIS_TITLE_STYLE,
|
|
409
|
-
lineStyle: AXIS_LINE_STYLE,
|
|
410
|
-
tickPadding: 20,
|
|
411
|
-
titlePadding: 4,
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
/** The default styling of a vertical (marker column beside the plot) axis. */
|
|
415
|
-
export const DEFAULT_Y_AXIS: AxisConfig = {
|
|
416
|
-
labelStyle: { ...AXIS_LABEL_STYLE, fontWeight: 500, textAlign: "right" },
|
|
417
|
-
titleStyle: AXIS_TITLE_STYLE,
|
|
418
|
-
lineStyle: AXIS_LINE_STYLE,
|
|
419
|
-
tickPadding: 40,
|
|
420
|
-
titlePadding: 24,
|
|
421
|
-
}
|
|
422
|
-
|
|
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
|
-
*/
|
|
428
|
-
export const DEFAULT_PLOT_AREA: PlotAreaConfig = {
|
|
429
|
-
fill: [],
|
|
430
|
-
stroke: { weight: 6, fill: CHART_PALETTE.borderColor },
|
|
431
|
-
shadow: [],
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/** The default legend styling, shared by every chart that draws one. */
|
|
435
|
-
export const DEFAULT_LEGEND: LegendConfig = {
|
|
436
|
-
padding: 0,
|
|
437
|
-
textStyle: {
|
|
438
|
-
fontSize: 30,
|
|
439
|
-
fontWeight: 600,
|
|
440
|
-
textAlign: "left",
|
|
441
|
-
fill: CHART_PALETTE.markerColor,
|
|
442
|
-
},
|
|
443
|
-
itemGap: 44,
|
|
444
|
-
markerStyle: {
|
|
445
|
-
width: 60,
|
|
446
|
-
height: 24,
|
|
447
|
-
borderRadius: 6,
|
|
448
|
-
stroke: [],
|
|
449
|
-
shadow: [],
|
|
450
|
-
},
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
/** The default per-series stroke palette — {@link DEFAULT_SERIES_COLORS}, drawn 4px. */
|
|
454
|
-
export const DEFAULT_SERIES_STROKES: Stroke[] = DEFAULT_SERIES_COLORS.map(
|
|
455
|
-
(fill) => ({ weight: 4, fill })
|
|
456
|
-
)
|
|
457
|
-
|
|
458
|
-
/** The default per-series fill palette — the same colours, as solid paint. */
|
|
459
|
-
export const DEFAULT_SERIES_FILLS: Fill[] = DEFAULT_SERIES_COLORS.map(
|
|
460
|
-
(fill) => fill
|
|
461
|
-
)
|
|
462
|
-
|
|
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
|
-
export function resolveAxisConfig(
|
|
469
|
-
axis: Partial<AxisConfig> | undefined,
|
|
470
|
-
previous: AxisConfig
|
|
471
|
-
): AxisConfig {
|
|
472
|
-
return {
|
|
473
|
-
labelStyle: textOps.resolveStyle(axis?.labelStyle, previous.labelStyle),
|
|
474
|
-
titleStyle: textOps.resolveStyle(axis?.titleStyle, previous.titleStyle),
|
|
475
|
-
lineStyle: strokeOps.resolve(axis?.lineStyle ?? previous.lineStyle),
|
|
476
|
-
tickPadding: insetsOps.resolve(
|
|
477
|
-
axis?.tickPadding ?? previous.tickPadding,
|
|
478
|
-
previous.tickPadding as InsetsResolved
|
|
479
|
-
),
|
|
480
|
-
titlePadding: insetsOps.resolve(
|
|
481
|
-
axis?.titlePadding ?? previous.titlePadding,
|
|
482
|
-
previous.titlePadding as InsetsResolved
|
|
483
|
-
),
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
export function lerpAxisConfig(
|
|
488
|
-
from: AxisConfig,
|
|
489
|
-
to: AxisConfig,
|
|
490
|
-
t: number
|
|
491
|
-
): AxisConfig {
|
|
492
|
-
return {
|
|
493
|
-
labelStyle: textOps.lerpStyle(from.labelStyle, to.labelStyle, t),
|
|
494
|
-
titleStyle: textOps.lerpStyle(from.titleStyle, to.titleStyle, t),
|
|
495
|
-
lineStyle: strokeOps.lerp(
|
|
496
|
-
from.lineStyle as StrokeResolved[],
|
|
497
|
-
to.lineStyle as StrokeResolved[],
|
|
498
|
-
t
|
|
499
|
-
),
|
|
500
|
-
tickPadding: insetsOps.lerp(
|
|
501
|
-
from.tickPadding as InsetsResolved,
|
|
502
|
-
to.tickPadding as InsetsResolved,
|
|
503
|
-
t
|
|
504
|
-
),
|
|
505
|
-
titlePadding: insetsOps.lerp(
|
|
506
|
-
from.titlePadding as InsetsResolved,
|
|
507
|
-
to.titlePadding as InsetsResolved,
|
|
508
|
-
t
|
|
509
|
-
),
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
export function resolvePlotArea(
|
|
514
|
-
plotArea: Partial<PlotAreaConfig> | undefined,
|
|
515
|
-
previous: PlotAreaConfig
|
|
516
|
-
): PlotAreaConfig {
|
|
517
|
-
return {
|
|
518
|
-
fill: fillOps.resolve(plotArea?.fill ?? previous.fill),
|
|
519
|
-
stroke: strokeOps.resolve(plotArea?.stroke ?? previous.stroke),
|
|
520
|
-
shadow: shadowOps.resolve(plotArea?.shadow ?? previous.shadow),
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
export function lerpPlotArea(
|
|
525
|
-
from: PlotAreaConfig,
|
|
526
|
-
to: PlotAreaConfig,
|
|
527
|
-
t: number
|
|
528
|
-
): PlotAreaConfig {
|
|
529
|
-
return {
|
|
530
|
-
fill: fillOps.lerp(
|
|
531
|
-
from.fill as FillResolved[],
|
|
532
|
-
to.fill as FillResolved[],
|
|
533
|
-
t
|
|
534
|
-
),
|
|
535
|
-
stroke: strokeOps.lerp(
|
|
536
|
-
from.stroke as StrokeResolved[],
|
|
537
|
-
to.stroke as StrokeResolved[],
|
|
538
|
-
t
|
|
539
|
-
),
|
|
540
|
-
shadow: shadowOps.lerp(
|
|
541
|
-
from.shadow as ShadowResolved[],
|
|
542
|
-
to.shadow as ShadowResolved[],
|
|
543
|
-
t
|
|
544
|
-
),
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
function resolveLegendMarkerConfig(
|
|
549
|
-
marker: Partial<LegendMarkerConfig> | undefined,
|
|
550
|
-
previous: LegendMarkerConfig
|
|
551
|
-
): LegendMarkerConfig {
|
|
552
|
-
return {
|
|
553
|
-
width: marker?.width ?? previous.width,
|
|
554
|
-
height: marker?.height ?? previous.height,
|
|
555
|
-
borderRadius: marker?.borderRadius ?? previous.borderRadius,
|
|
556
|
-
stroke: strokeOps.resolve(marker?.stroke ?? previous.stroke),
|
|
557
|
-
shadow: shadowOps.resolve(marker?.shadow ?? previous.shadow),
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
function lerpLegendMarkerConfig(
|
|
562
|
-
from: LegendMarkerConfig,
|
|
563
|
-
to: LegendMarkerConfig,
|
|
564
|
-
t: number
|
|
565
|
-
): LegendMarkerConfig {
|
|
566
|
-
return {
|
|
567
|
-
width: lerpNumber(from.width, to.width, t),
|
|
568
|
-
height: lerpNumber(from.height, to.height, t),
|
|
569
|
-
borderRadius: lerpNumber(from.borderRadius, to.borderRadius, t),
|
|
570
|
-
stroke: strokeOps.lerp(
|
|
571
|
-
from.stroke as StrokeResolved[],
|
|
572
|
-
to.stroke as StrokeResolved[],
|
|
573
|
-
t
|
|
574
|
-
),
|
|
575
|
-
shadow: shadowOps.lerp(
|
|
576
|
-
from.shadow as ShadowResolved[],
|
|
577
|
-
to.shadow as ShadowResolved[],
|
|
578
|
-
t
|
|
579
|
-
),
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
/** A legend theme as the author may state it — every branch optional. */
|
|
584
|
-
export type LegendTheme = Partial<Omit<LegendConfig, "markerStyle">> & {
|
|
585
|
-
markerStyle?: Partial<LegendMarkerConfig>
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
export function resolveLegendConfig(
|
|
589
|
-
legend: LegendTheme | undefined,
|
|
590
|
-
previous: LegendConfig
|
|
591
|
-
): LegendConfig {
|
|
592
|
-
return {
|
|
593
|
-
padding: insetsOps.resolve(
|
|
594
|
-
legend?.padding ?? previous.padding,
|
|
595
|
-
previous.padding as InsetsResolved
|
|
596
|
-
),
|
|
597
|
-
textStyle: textOps.resolveStyle(legend?.textStyle, previous.textStyle),
|
|
598
|
-
itemGap: legend?.itemGap ?? previous.itemGap,
|
|
599
|
-
markerStyle: resolveLegendMarkerConfig(
|
|
600
|
-
legend?.markerStyle,
|
|
601
|
-
previous.markerStyle
|
|
602
|
-
),
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
export function lerpLegendConfig(
|
|
607
|
-
from: LegendConfig,
|
|
608
|
-
to: LegendConfig,
|
|
609
|
-
t: number
|
|
610
|
-
): LegendConfig {
|
|
611
|
-
return {
|
|
612
|
-
padding: insetsOps.lerp(
|
|
613
|
-
from.padding as InsetsResolved,
|
|
614
|
-
to.padding as InsetsResolved,
|
|
615
|
-
t
|
|
616
|
-
),
|
|
617
|
-
textStyle: textOps.lerpStyle(from.textStyle, to.textStyle, t),
|
|
618
|
-
itemGap: lerpNumber(from.itemGap, to.itemGap, t),
|
|
619
|
-
markerStyle: lerpLegendMarkerConfig(from.markerStyle, to.markerStyle, t),
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
/** A whole-palette override replaces the previous palette outright. */
|
|
624
|
-
export function resolveSeriesStrokes(
|
|
625
|
-
styles: Stroke[] | undefined,
|
|
626
|
-
previous: Stroke[]
|
|
627
|
-
): Stroke[] {
|
|
628
|
-
return (styles ?? previous).map((s) => strokeOps.resolve(s))
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
/** Pairwise by index; a palette that grows/shrinks holds the longer side's tail. */
|
|
632
|
-
export function lerpSeriesStrokes(
|
|
633
|
-
from: Stroke[],
|
|
634
|
-
to: Stroke[],
|
|
635
|
-
t: number
|
|
636
|
-
): Stroke[] {
|
|
637
|
-
const n = Math.max(from.length, to.length)
|
|
638
|
-
const out: Stroke[] = []
|
|
639
|
-
for (let i = 0; i < n; i++) {
|
|
640
|
-
const a = (from[i] ?? to[i]) as StrokeResolved[]
|
|
641
|
-
const b = (to[i] ?? from[i]) as StrokeResolved[]
|
|
642
|
-
out.push(strokeOps.lerp(a, b, t))
|
|
643
|
-
}
|
|
644
|
-
return out
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
/** The fill-palette twin of {@link resolveSeriesStrokes}, for filled marks. */
|
|
648
|
-
export function resolveSeriesFills(
|
|
649
|
-
fills: Fill[] | undefined,
|
|
650
|
-
previous: Fill[]
|
|
651
|
-
): Fill[] {
|
|
652
|
-
return (fills ?? previous).map((f) => fillOps.resolve(f))
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
/** The fill-palette twin of {@link lerpSeriesStrokes}. */
|
|
656
|
-
export function lerpSeriesFills(from: Fill[], to: Fill[], t: number): Fill[] {
|
|
657
|
-
const n = Math.max(from.length, to.length)
|
|
658
|
-
const out: Fill[] = []
|
|
659
|
-
for (let i = 0; i < n; i++) {
|
|
660
|
-
const a = (from[i] ?? to[i]) as FillResolved[]
|
|
661
|
-
const b = (to[i] ?? from[i]) as FillResolved[]
|
|
662
|
-
out.push(fillOps.lerp(a, b, t))
|
|
663
|
-
}
|
|
664
|
-
return out
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
// ---- Per-series paint ----------------------------------------------------
|
|
668
|
-
|
|
669
|
-
/**
|
|
670
|
-
* The effective stroke for series `index`: the palette's entry at
|
|
671
|
-
* `index % palette.length`, with the series' own `fill`, if set, overriding just
|
|
672
|
-
* the last (topmost) layer's colour.
|
|
673
|
-
*/
|
|
674
|
-
export function resolveSeriesStroke(
|
|
675
|
-
palette: Stroke[],
|
|
676
|
-
series: ChartSeries,
|
|
677
|
-
index: number
|
|
678
|
-
): Stroke {
|
|
679
|
-
const entry: Stroke =
|
|
680
|
-
palette.length > 0
|
|
681
|
-
? palette[index % palette.length]
|
|
682
|
-
: { weight: 4, fill: "white" }
|
|
683
|
-
if (series.fill == null) return entry
|
|
684
|
-
const layers = strokeOps.resolve(entry)
|
|
685
|
-
if (layers.length === 0) return { weight: 4, fill: series.fill }
|
|
686
|
-
const next = [...layers]
|
|
687
|
-
next[next.length - 1] = {
|
|
688
|
-
...next[next.length - 1],
|
|
689
|
-
fill: fillOps.resolve(series.fill),
|
|
690
|
-
}
|
|
691
|
-
return next
|
|
692
|
-
}
|
|
693
|
-
|
|
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
|
-
*/
|
|
699
|
-
export function resolveSeriesFill(
|
|
700
|
-
palette: Stroke[],
|
|
701
|
-
series: ChartSeries,
|
|
702
|
-
index: number
|
|
703
|
-
): Fill {
|
|
704
|
-
const layers = strokeOps.resolve(resolveSeriesStroke(palette, series, index))
|
|
705
|
-
return layers[layers.length - 1]?.fill ?? "white"
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
/**
|
|
709
|
-
* The effective paint for mark `index` of a **filled** chart (a bar, a wedge):
|
|
710
|
-
* the series' own `fill` when it carries one, else the palette's entry for its
|
|
711
|
-
* index, cycled.
|
|
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.
|
|
737
|
-
*/
|
|
738
|
-
export function formatMarker(value: number, step?: number): string {
|
|
739
|
-
if (Number.isInteger(value)) return String(value)
|
|
740
|
-
const decimals = Math.max(1, step === undefined ? 1 : decimalsFor(step))
|
|
741
|
-
return value.toFixed(decimals).replace(/(\.\d*?)0+$/, "$1").replace(/\.$/, "")
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/** Decimal places needed to write `step` exactly, capped at six. */
|
|
745
|
-
function decimalsFor(step: number): number {
|
|
746
|
-
if (!(step > 0) || !Number.isFinite(step)) return 1
|
|
747
|
-
for (let d = 0; d <= 6; d++) {
|
|
748
|
-
const scaled = step * 10 ** d
|
|
749
|
-
if (Math.abs(scaled - Math.round(scaled)) < 1e-6 * Math.max(1, scaled)) return d
|
|
750
|
-
}
|
|
751
|
-
// An irrational-looking step (a third): as many places as separate the ticks.
|
|
752
|
-
return Math.min(6, Math.max(1, Math.ceil(-Math.log10(step)) + 1))
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
/** The gap between neighbouring tick values, or `undefined` for fewer than two distinct ones. */
|
|
756
|
-
export function markerStep(markers: readonly number[]): number | undefined {
|
|
757
|
-
const sorted = [...markers].filter(Number.isFinite).sort((a, b) => a - b)
|
|
758
|
-
let step: number | undefined
|
|
759
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
760
|
-
const gap = sorted[i] - sorted[i - 1]
|
|
761
|
-
if (gap > 1e-12 && (step === undefined || gap < step)) step = gap
|
|
762
|
-
}
|
|
763
|
-
return step
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
/** Format a fraction in `[0, 1]` as a percentage: integers plain, else one decimal. */
|
|
767
|
-
export function formatPercent(fraction: number): string {
|
|
768
|
-
const pct = fraction * 100
|
|
769
|
-
if (Number.isInteger(pct)) return `${pct}%`
|
|
770
|
-
return `${pct.toFixed(1).replace(/\.0$/, "")}%`
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
/** `markerCount` evenly-spaced tick values across `[min, max]`, ends inclusive. */
|
|
774
|
-
export function markerValues([min, max]: Range, markerCount: number): number[] {
|
|
775
|
-
const n = Math.max(2, Math.round(markerCount))
|
|
776
|
-
return Array.from({ length: n + 1 }, (_, i) => min + ((max - min) / n) * i)
|
|
777
|
-
}
|
|
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"
|
|
1
|
+
/**
|
|
2
|
+
* The vocabulary every chart node shares: what a *series* is, how an axis is
|
|
3
|
+
* scaled and formatted, and the theme leaves each chart's own theme is assembled
|
|
4
|
+
* from.
|
|
5
|
+
*
|
|
6
|
+
* This module exists because the three chart types are three ways of drawing the
|
|
7
|
+
* same table. A line chart, a bar chart and a pie chart disagree about almost
|
|
8
|
+
* everything visible — polylines against columns against wedges — and agree
|
|
9
|
+
* about almost everything underneath: a series is a column with a caption and a
|
|
10
|
+
* paint, a marker is a number formatted one way, a legend is a swatch beside a
|
|
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".
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import {
|
|
32
|
+
fillOps,
|
|
33
|
+
lerpNumber,
|
|
34
|
+
shadowOps,
|
|
35
|
+
strokeOps,
|
|
36
|
+
insetsOps,
|
|
37
|
+
textOps,
|
|
38
|
+
type DataRecord,
|
|
39
|
+
type Fill,
|
|
40
|
+
type FillResolved,
|
|
41
|
+
type Insets,
|
|
42
|
+
type InsetsResolved,
|
|
43
|
+
type Shadow,
|
|
44
|
+
type ShadowResolved,
|
|
45
|
+
type Stroke,
|
|
46
|
+
type StrokeResolved,
|
|
47
|
+
type TextStyle,
|
|
48
|
+
} from "@motionscript/sdk"
|
|
49
|
+
|
|
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
|
+
*/
|
|
62
|
+
export interface ChartSeries {
|
|
63
|
+
/** Key into each data row for this series' value. */
|
|
64
|
+
field: string
|
|
65
|
+
/** Legend caption for this series. */
|
|
66
|
+
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
|
+
*/
|
|
71
|
+
fill?: Fill
|
|
72
|
+
/** Whether the series shows by default. Defaults to `true`. */
|
|
73
|
+
enabled?: boolean
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A `[min, max]` numeric range in data units. */
|
|
77
|
+
export type Range = [min: number, max: number]
|
|
78
|
+
|
|
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
|
+
*/
|
|
84
|
+
export interface AxisDomainConfig {
|
|
85
|
+
/** Caption drawn alongside the axis. Pass `""` (the default) to hide it. */
|
|
86
|
+
label?: string
|
|
87
|
+
/** `[min, max]` of the scale. Derived from the data bounds when omitted. */
|
|
88
|
+
range?: Range
|
|
89
|
+
/** Tick values (in data units). Derived (five evenly-spaced) when omitted. */
|
|
90
|
+
ticks?: number[]
|
|
91
|
+
/**
|
|
92
|
+
* How a tick value is written down — a `number-format` pattern such as
|
|
93
|
+
* `#,##0.00` or `$#,##0`.
|
|
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.
|
|
99
|
+
*/
|
|
100
|
+
format?: string
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* An axis domain entry's resolve: merges the author's partial input onto
|
|
105
|
+
* `previous`, so a later partial `.set({ yAxis: {...} })` patches
|
|
106
|
+
* `label`/`range`/`ticks` independently. `range`/`ticks` staying `undefined`
|
|
107
|
+
* after the merge is the auto-derive signal the scale getters read.
|
|
108
|
+
*/
|
|
109
|
+
export function resolveAxisDomain(
|
|
110
|
+
domain: AxisDomainConfig | undefined,
|
|
111
|
+
previous?: AxisDomainConfig
|
|
112
|
+
): AxisDomainConfig {
|
|
113
|
+
return {
|
|
114
|
+
label: domain?.label ?? previous?.label ?? "",
|
|
115
|
+
range: domain?.range ?? previous?.range,
|
|
116
|
+
ticks: domain?.ticks ?? previous?.ticks,
|
|
117
|
+
format: domain?.format ?? previous?.format,
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* An axis domain entry's lerp: interpolates `range`/`ticks` numerically
|
|
123
|
+
* (pairwise by index for `ticks`), so a `to` command that restates a range
|
|
124
|
+
* rescales the plot smoothly. `label`, and either field when one side is in
|
|
125
|
+
* auto-derive mode, step at the midpoint.
|
|
126
|
+
*/
|
|
127
|
+
export function lerpAxisDomain(
|
|
128
|
+
from: AxisDomainConfig,
|
|
129
|
+
to: AxisDomainConfig,
|
|
130
|
+
t: number
|
|
131
|
+
): AxisDomainConfig {
|
|
132
|
+
return {
|
|
133
|
+
label: t < 0.5 ? from.label : to.label,
|
|
134
|
+
range: lerpOptionalRange(from.range, to.range, t),
|
|
135
|
+
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
|
+
format: t < 0.5 ? from.format : to.format,
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function lerpOptionalRange(
|
|
143
|
+
from: Range | undefined,
|
|
144
|
+
to: Range | undefined,
|
|
145
|
+
t: number
|
|
146
|
+
): Range | undefined {
|
|
147
|
+
if (!from || !to) return t < 0.5 ? from : to
|
|
148
|
+
return [lerpNumber(from[0], to[0], t), lerpNumber(from[1], to[1], t)]
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function lerpOptionalTicks(
|
|
152
|
+
from: number[] | undefined,
|
|
153
|
+
to: number[] | undefined,
|
|
154
|
+
t: number
|
|
155
|
+
): number[] | undefined {
|
|
156
|
+
if (!from || !to) return t < 0.5 ? from : to
|
|
157
|
+
const n = Math.max(from.length, to.length)
|
|
158
|
+
const out: number[] = []
|
|
159
|
+
for (let i = 0; i < n; i++) {
|
|
160
|
+
out.push(lerpNumber(from[i] ?? to[i], to[i] ?? from[i], t))
|
|
161
|
+
}
|
|
162
|
+
return out
|
|
163
|
+
}
|
|
164
|
+
|
|
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
|
+
/**
|
|
192
|
+
* An axis domain part-way through a tween that leaves or reaches a `range` or
|
|
193
|
+
* `ticks` left to the data: both ends as stored, and how far along. Only the
|
|
194
|
+
* chart knows what an auto side draws as, so {@link settleAxisDomain} resolves
|
|
195
|
+
* it there. Its own fields are the plain {@link lerpAxisDomain} of its ends, so
|
|
196
|
+
* a mapper merging onto one still reads a domain.
|
|
197
|
+
*/
|
|
198
|
+
export interface AxisDomainBlend extends AxisDomainConfig {
|
|
199
|
+
readonly blend: { readonly from: AxisDomainConfig; readonly to: AxisDomainConfig; readonly t: number }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function blendOf(domain: AxisDomainConfig): AxisDomainBlend["blend"] | undefined {
|
|
203
|
+
return (domain as Partial<AxisDomainBlend>).blend
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* A chart's axis entry tween: what `to({ yAxis: { range } })` moves through.
|
|
208
|
+
* Between two explicit domains it is {@link lerpAxisDomain}, and a restated
|
|
209
|
+
* `range` without `ticks` spreads explicit ticks across it, keeping their count.
|
|
210
|
+
* When either end leaves `range` or `ticks` to the data, the value between is an
|
|
211
|
+
* {@link AxisDomainBlend}, which the chart draws as the blend of what each end
|
|
212
|
+
* draws: a rescale from an auto axis is continuous, not a cut at the midpoint.
|
|
213
|
+
*/
|
|
214
|
+
export function lerpAxisEntry(from: AxisDomainConfig, to: AxisDomainConfig, t: number): AxisDomainConfig {
|
|
215
|
+
// The mapper merged the request onto `from`, so a field it left unstated is `from`'s own, by identity.
|
|
216
|
+
const rangeMoved = to.range !== from.range
|
|
217
|
+
const target = rangeMoved && to.range && to.ticks && to.ticks === from.ticks
|
|
218
|
+
? { ...to, ticks: markerValues(to.range, Math.max(2, to.ticks.length) - 1) }
|
|
219
|
+
: to
|
|
220
|
+
const blended = blendOf(from) !== undefined
|
|
221
|
+
const moved = blended || rangeMoved || target.ticks !== from.ticks
|
|
222
|
+
const auto = blended || from.range === undefined || from.ticks === undefined || target.range === undefined || target.ticks === undefined
|
|
223
|
+
if (!moved || !auto) return lerpAxisDomain(from, target, t)
|
|
224
|
+
if (t === 0) return from
|
|
225
|
+
if (t === 1) return target
|
|
226
|
+
const blend: AxisDomainBlend = { ...lerpAxisDomain(from, target, t), blend: { from, to: target, t } }
|
|
227
|
+
return blend
|
|
228
|
+
}
|
|
229
|
+
|
|
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
|
+
*/
|
|
234
|
+
export function settleAxisDomain(domain: AxisDomainConfig, drawn: (domain: AxisDomainConfig) => AxisDomainConfig): AxisDomainConfig {
|
|
235
|
+
const blend = blendOf(domain)
|
|
236
|
+
if (!blend) return drawn(domain)
|
|
237
|
+
return lerpAxisDomain(settleAxisDomain(blend.from, drawn), settleAxisDomain(blend.to, drawn), blend.t)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The low-level colour tokens the default themes are built from — a dark plot
|
|
242
|
+
* panel with warm-grey axis furniture.
|
|
243
|
+
*/
|
|
244
|
+
export const CHART_PALETTE = {
|
|
245
|
+
/** Fill of the plot panel behind the marks. */
|
|
246
|
+
plotFill: "#15130e",
|
|
247
|
+
/** Colour of the plot's thick border. */
|
|
248
|
+
borderColor: "#4a4036",
|
|
249
|
+
/** Colour of the faint grid lines spanning the plot. */
|
|
250
|
+
gridColor: "#2c2820",
|
|
251
|
+
/** Colour of the numeric axis markers, axis captions, and legend labels. */
|
|
252
|
+
markerColor: "#8a7f70",
|
|
253
|
+
/** Colour of a spotlit region's dashed rules and its wash. */
|
|
254
|
+
regionColor: "#d99a3a",
|
|
255
|
+
} as const
|
|
256
|
+
|
|
257
|
+
/** The out-of-the-box series palette, cycled by series index. */
|
|
258
|
+
export const DEFAULT_SERIES_COLORS = [
|
|
259
|
+
"#d9603b",
|
|
260
|
+
"#8194ad",
|
|
261
|
+
"#e0a93b",
|
|
262
|
+
"#5e9d6b",
|
|
263
|
+
"#9ec85a",
|
|
264
|
+
] as const
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Opacity an *un*-spotlighted series fades to — its mark and its legend entry
|
|
268
|
+
* alike, so the two dim together.
|
|
269
|
+
*/
|
|
270
|
+
export const SPOTLIGHT_DIM = 0.2
|
|
271
|
+
|
|
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
|
+
/**
|
|
280
|
+
* Read a numeric field off a row, defaulting missing, non-numeric and non-finite
|
|
281
|
+
* values to 0.
|
|
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.
|
|
286
|
+
*/
|
|
287
|
+
export function num(row: DataRecord, field: string | undefined): number {
|
|
288
|
+
if (field === undefined) return 0
|
|
289
|
+
const v = row[field]
|
|
290
|
+
// A `NaN` or infinite cell is no more a measurement than a missing one: read
|
|
291
|
+
// as 0, it cannot poison the scale and turn every point into `NaN`.
|
|
292
|
+
return typeof v === "number" && Number.isFinite(v) ? v : 0
|
|
293
|
+
}
|
|
294
|
+
|
|
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
|
+
*/
|
|
301
|
+
export function text(row: DataRecord, field: string | undefined): string {
|
|
302
|
+
if (field === undefined || field === "") return ""
|
|
303
|
+
const v = row[field]
|
|
304
|
+
return v === undefined || v === null ? "" : String(v)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Every column that holds numbers across `data`, in header order, skipping the
|
|
309
|
+
* names in `exclude` (typically the category column).
|
|
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.
|
|
315
|
+
*/
|
|
316
|
+
export function numericFields(
|
|
317
|
+
data: DataRecord[],
|
|
318
|
+
exclude: string[] = []
|
|
319
|
+
): string[] {
|
|
320
|
+
if (data.length === 0) return []
|
|
321
|
+
const skip = new Set(exclude)
|
|
322
|
+
const seen = new Set<string>()
|
|
323
|
+
const order: string[] = []
|
|
324
|
+
for (const row of data)
|
|
325
|
+
for (const key of Object.keys(row))
|
|
326
|
+
if (!skip.has(key) && !seen.has(key)) {
|
|
327
|
+
seen.add(key)
|
|
328
|
+
order.push(key)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return order.filter((key) => {
|
|
332
|
+
let sawValue = false
|
|
333
|
+
for (const row of data) {
|
|
334
|
+
const v = row[key]
|
|
335
|
+
if (v === undefined || v === "") continue
|
|
336
|
+
if (typeof v !== "number") return false
|
|
337
|
+
sawValue = true
|
|
338
|
+
}
|
|
339
|
+
return sawValue
|
|
340
|
+
})
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// ---- Theme branches ------------------------------------------------------
|
|
344
|
+
|
|
345
|
+
/** One axis's (x or y) style vocabulary. */
|
|
346
|
+
export interface AxisConfig {
|
|
347
|
+
/** Style of each marker along the axis. */
|
|
348
|
+
labelStyle: TextStyle
|
|
349
|
+
/** Style of the axis caption. */
|
|
350
|
+
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
|
+
*/
|
|
355
|
+
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
|
+
*/
|
|
360
|
+
tickPadding: Insets
|
|
361
|
+
/** Gap between the marker row and the caption, read the same side. */
|
|
362
|
+
titlePadding: Insets
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** A legend's per-series colour swatch — a rounded chip, not the caption. */
|
|
366
|
+
export interface LegendMarkerConfig {
|
|
367
|
+
width: number
|
|
368
|
+
height: number
|
|
369
|
+
borderRadius: number
|
|
370
|
+
stroke: Stroke
|
|
371
|
+
shadow: Shadow
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** A legend's visual theme. */
|
|
375
|
+
export interface LegendConfig {
|
|
376
|
+
padding: Insets
|
|
377
|
+
/** Caption style — only the swatch carries the series colour. */
|
|
378
|
+
textStyle: TextStyle
|
|
379
|
+
/** Gap between legend entries. */
|
|
380
|
+
itemGap: number
|
|
381
|
+
markerStyle: LegendMarkerConfig
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The plot panel a cartesian chart draws its marks over. */
|
|
385
|
+
export interface PlotAreaConfig {
|
|
386
|
+
stroke: Stroke
|
|
387
|
+
fill: Fill
|
|
388
|
+
shadow: Shadow
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const AXIS_LABEL_STYLE: TextStyle = {
|
|
392
|
+
fontSize: 24,
|
|
393
|
+
fontWeight: 700,
|
|
394
|
+
textAlign: "center",
|
|
395
|
+
fill: CHART_PALETTE.markerColor,
|
|
396
|
+
}
|
|
397
|
+
const AXIS_TITLE_STYLE: TextStyle = {
|
|
398
|
+
fontSize: 30,
|
|
399
|
+
fontWeight: 600,
|
|
400
|
+
textAlign: "center",
|
|
401
|
+
fill: CHART_PALETTE.markerColor,
|
|
402
|
+
}
|
|
403
|
+
const AXIS_LINE_STYLE: Stroke = { weight: 2, fill: CHART_PALETTE.gridColor }
|
|
404
|
+
|
|
405
|
+
/** The default styling of a horizontal (marker row beneath the plot) axis. */
|
|
406
|
+
export const DEFAULT_X_AXIS: AxisConfig = {
|
|
407
|
+
labelStyle: AXIS_LABEL_STYLE,
|
|
408
|
+
titleStyle: AXIS_TITLE_STYLE,
|
|
409
|
+
lineStyle: AXIS_LINE_STYLE,
|
|
410
|
+
tickPadding: 20,
|
|
411
|
+
titlePadding: 4,
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** The default styling of a vertical (marker column beside the plot) axis. */
|
|
415
|
+
export const DEFAULT_Y_AXIS: AxisConfig = {
|
|
416
|
+
labelStyle: { ...AXIS_LABEL_STYLE, fontWeight: 500, textAlign: "right" },
|
|
417
|
+
titleStyle: AXIS_TITLE_STYLE,
|
|
418
|
+
lineStyle: AXIS_LINE_STYLE,
|
|
419
|
+
tickPadding: 40,
|
|
420
|
+
titlePadding: 24,
|
|
421
|
+
}
|
|
422
|
+
|
|
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
|
+
*/
|
|
428
|
+
export const DEFAULT_PLOT_AREA: PlotAreaConfig = {
|
|
429
|
+
fill: [],
|
|
430
|
+
stroke: { weight: 6, fill: CHART_PALETTE.borderColor },
|
|
431
|
+
shadow: [],
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** The default legend styling, shared by every chart that draws one. */
|
|
435
|
+
export const DEFAULT_LEGEND: LegendConfig = {
|
|
436
|
+
padding: 0,
|
|
437
|
+
textStyle: {
|
|
438
|
+
fontSize: 30,
|
|
439
|
+
fontWeight: 600,
|
|
440
|
+
textAlign: "left",
|
|
441
|
+
fill: CHART_PALETTE.markerColor,
|
|
442
|
+
},
|
|
443
|
+
itemGap: 44,
|
|
444
|
+
markerStyle: {
|
|
445
|
+
width: 60,
|
|
446
|
+
height: 24,
|
|
447
|
+
borderRadius: 6,
|
|
448
|
+
stroke: [],
|
|
449
|
+
shadow: [],
|
|
450
|
+
},
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** The default per-series stroke palette — {@link DEFAULT_SERIES_COLORS}, drawn 4px. */
|
|
454
|
+
export const DEFAULT_SERIES_STROKES: Stroke[] = DEFAULT_SERIES_COLORS.map(
|
|
455
|
+
(fill) => ({ weight: 4, fill })
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
/** The default per-series fill palette — the same colours, as solid paint. */
|
|
459
|
+
export const DEFAULT_SERIES_FILLS: Fill[] = DEFAULT_SERIES_COLORS.map(
|
|
460
|
+
(fill) => fill
|
|
461
|
+
)
|
|
462
|
+
|
|
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
|
+
export function resolveAxisConfig(
|
|
469
|
+
axis: Partial<AxisConfig> | undefined,
|
|
470
|
+
previous: AxisConfig
|
|
471
|
+
): AxisConfig {
|
|
472
|
+
return {
|
|
473
|
+
labelStyle: textOps.resolveStyle(axis?.labelStyle, previous.labelStyle),
|
|
474
|
+
titleStyle: textOps.resolveStyle(axis?.titleStyle, previous.titleStyle),
|
|
475
|
+
lineStyle: strokeOps.resolve(axis?.lineStyle ?? previous.lineStyle),
|
|
476
|
+
tickPadding: insetsOps.resolve(
|
|
477
|
+
axis?.tickPadding ?? previous.tickPadding,
|
|
478
|
+
previous.tickPadding as InsetsResolved
|
|
479
|
+
),
|
|
480
|
+
titlePadding: insetsOps.resolve(
|
|
481
|
+
axis?.titlePadding ?? previous.titlePadding,
|
|
482
|
+
previous.titlePadding as InsetsResolved
|
|
483
|
+
),
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export function lerpAxisConfig(
|
|
488
|
+
from: AxisConfig,
|
|
489
|
+
to: AxisConfig,
|
|
490
|
+
t: number
|
|
491
|
+
): AxisConfig {
|
|
492
|
+
return {
|
|
493
|
+
labelStyle: textOps.lerpStyle(from.labelStyle, to.labelStyle, t),
|
|
494
|
+
titleStyle: textOps.lerpStyle(from.titleStyle, to.titleStyle, t),
|
|
495
|
+
lineStyle: strokeOps.lerp(
|
|
496
|
+
from.lineStyle as StrokeResolved[],
|
|
497
|
+
to.lineStyle as StrokeResolved[],
|
|
498
|
+
t
|
|
499
|
+
),
|
|
500
|
+
tickPadding: insetsOps.lerp(
|
|
501
|
+
from.tickPadding as InsetsResolved,
|
|
502
|
+
to.tickPadding as InsetsResolved,
|
|
503
|
+
t
|
|
504
|
+
),
|
|
505
|
+
titlePadding: insetsOps.lerp(
|
|
506
|
+
from.titlePadding as InsetsResolved,
|
|
507
|
+
to.titlePadding as InsetsResolved,
|
|
508
|
+
t
|
|
509
|
+
),
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export function resolvePlotArea(
|
|
514
|
+
plotArea: Partial<PlotAreaConfig> | undefined,
|
|
515
|
+
previous: PlotAreaConfig
|
|
516
|
+
): PlotAreaConfig {
|
|
517
|
+
return {
|
|
518
|
+
fill: fillOps.resolve(plotArea?.fill ?? previous.fill),
|
|
519
|
+
stroke: strokeOps.resolve(plotArea?.stroke ?? previous.stroke),
|
|
520
|
+
shadow: shadowOps.resolve(plotArea?.shadow ?? previous.shadow),
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
export function lerpPlotArea(
|
|
525
|
+
from: PlotAreaConfig,
|
|
526
|
+
to: PlotAreaConfig,
|
|
527
|
+
t: number
|
|
528
|
+
): PlotAreaConfig {
|
|
529
|
+
return {
|
|
530
|
+
fill: fillOps.lerp(
|
|
531
|
+
from.fill as FillResolved[],
|
|
532
|
+
to.fill as FillResolved[],
|
|
533
|
+
t
|
|
534
|
+
),
|
|
535
|
+
stroke: strokeOps.lerp(
|
|
536
|
+
from.stroke as StrokeResolved[],
|
|
537
|
+
to.stroke as StrokeResolved[],
|
|
538
|
+
t
|
|
539
|
+
),
|
|
540
|
+
shadow: shadowOps.lerp(
|
|
541
|
+
from.shadow as ShadowResolved[],
|
|
542
|
+
to.shadow as ShadowResolved[],
|
|
543
|
+
t
|
|
544
|
+
),
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function resolveLegendMarkerConfig(
|
|
549
|
+
marker: Partial<LegendMarkerConfig> | undefined,
|
|
550
|
+
previous: LegendMarkerConfig
|
|
551
|
+
): LegendMarkerConfig {
|
|
552
|
+
return {
|
|
553
|
+
width: marker?.width ?? previous.width,
|
|
554
|
+
height: marker?.height ?? previous.height,
|
|
555
|
+
borderRadius: marker?.borderRadius ?? previous.borderRadius,
|
|
556
|
+
stroke: strokeOps.resolve(marker?.stroke ?? previous.stroke),
|
|
557
|
+
shadow: shadowOps.resolve(marker?.shadow ?? previous.shadow),
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function lerpLegendMarkerConfig(
|
|
562
|
+
from: LegendMarkerConfig,
|
|
563
|
+
to: LegendMarkerConfig,
|
|
564
|
+
t: number
|
|
565
|
+
): LegendMarkerConfig {
|
|
566
|
+
return {
|
|
567
|
+
width: lerpNumber(from.width, to.width, t),
|
|
568
|
+
height: lerpNumber(from.height, to.height, t),
|
|
569
|
+
borderRadius: lerpNumber(from.borderRadius, to.borderRadius, t),
|
|
570
|
+
stroke: strokeOps.lerp(
|
|
571
|
+
from.stroke as StrokeResolved[],
|
|
572
|
+
to.stroke as StrokeResolved[],
|
|
573
|
+
t
|
|
574
|
+
),
|
|
575
|
+
shadow: shadowOps.lerp(
|
|
576
|
+
from.shadow as ShadowResolved[],
|
|
577
|
+
to.shadow as ShadowResolved[],
|
|
578
|
+
t
|
|
579
|
+
),
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** A legend theme as the author may state it — every branch optional. */
|
|
584
|
+
export type LegendTheme = Partial<Omit<LegendConfig, "markerStyle">> & {
|
|
585
|
+
markerStyle?: Partial<LegendMarkerConfig>
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export function resolveLegendConfig(
|
|
589
|
+
legend: LegendTheme | undefined,
|
|
590
|
+
previous: LegendConfig
|
|
591
|
+
): LegendConfig {
|
|
592
|
+
return {
|
|
593
|
+
padding: insetsOps.resolve(
|
|
594
|
+
legend?.padding ?? previous.padding,
|
|
595
|
+
previous.padding as InsetsResolved
|
|
596
|
+
),
|
|
597
|
+
textStyle: textOps.resolveStyle(legend?.textStyle, previous.textStyle),
|
|
598
|
+
itemGap: legend?.itemGap ?? previous.itemGap,
|
|
599
|
+
markerStyle: resolveLegendMarkerConfig(
|
|
600
|
+
legend?.markerStyle,
|
|
601
|
+
previous.markerStyle
|
|
602
|
+
),
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
export function lerpLegendConfig(
|
|
607
|
+
from: LegendConfig,
|
|
608
|
+
to: LegendConfig,
|
|
609
|
+
t: number
|
|
610
|
+
): LegendConfig {
|
|
611
|
+
return {
|
|
612
|
+
padding: insetsOps.lerp(
|
|
613
|
+
from.padding as InsetsResolved,
|
|
614
|
+
to.padding as InsetsResolved,
|
|
615
|
+
t
|
|
616
|
+
),
|
|
617
|
+
textStyle: textOps.lerpStyle(from.textStyle, to.textStyle, t),
|
|
618
|
+
itemGap: lerpNumber(from.itemGap, to.itemGap, t),
|
|
619
|
+
markerStyle: lerpLegendMarkerConfig(from.markerStyle, to.markerStyle, t),
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** A whole-palette override replaces the previous palette outright. */
|
|
624
|
+
export function resolveSeriesStrokes(
|
|
625
|
+
styles: Stroke[] | undefined,
|
|
626
|
+
previous: Stroke[]
|
|
627
|
+
): Stroke[] {
|
|
628
|
+
return (styles ?? previous).map((s) => strokeOps.resolve(s))
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** Pairwise by index; a palette that grows/shrinks holds the longer side's tail. */
|
|
632
|
+
export function lerpSeriesStrokes(
|
|
633
|
+
from: Stroke[],
|
|
634
|
+
to: Stroke[],
|
|
635
|
+
t: number
|
|
636
|
+
): Stroke[] {
|
|
637
|
+
const n = Math.max(from.length, to.length)
|
|
638
|
+
const out: Stroke[] = []
|
|
639
|
+
for (let i = 0; i < n; i++) {
|
|
640
|
+
const a = (from[i] ?? to[i]) as StrokeResolved[]
|
|
641
|
+
const b = (to[i] ?? from[i]) as StrokeResolved[]
|
|
642
|
+
out.push(strokeOps.lerp(a, b, t))
|
|
643
|
+
}
|
|
644
|
+
return out
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/** The fill-palette twin of {@link resolveSeriesStrokes}, for filled marks. */
|
|
648
|
+
export function resolveSeriesFills(
|
|
649
|
+
fills: Fill[] | undefined,
|
|
650
|
+
previous: Fill[]
|
|
651
|
+
): Fill[] {
|
|
652
|
+
return (fills ?? previous).map((f) => fillOps.resolve(f))
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** The fill-palette twin of {@link lerpSeriesStrokes}. */
|
|
656
|
+
export function lerpSeriesFills(from: Fill[], to: Fill[], t: number): Fill[] {
|
|
657
|
+
const n = Math.max(from.length, to.length)
|
|
658
|
+
const out: Fill[] = []
|
|
659
|
+
for (let i = 0; i < n; i++) {
|
|
660
|
+
const a = (from[i] ?? to[i]) as FillResolved[]
|
|
661
|
+
const b = (to[i] ?? from[i]) as FillResolved[]
|
|
662
|
+
out.push(fillOps.lerp(a, b, t))
|
|
663
|
+
}
|
|
664
|
+
return out
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// ---- Per-series paint ----------------------------------------------------
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* The effective stroke for series `index`: the palette's entry at
|
|
671
|
+
* `index % palette.length`, with the series' own `fill`, if set, overriding just
|
|
672
|
+
* the last (topmost) layer's colour.
|
|
673
|
+
*/
|
|
674
|
+
export function resolveSeriesStroke(
|
|
675
|
+
palette: Stroke[],
|
|
676
|
+
series: ChartSeries,
|
|
677
|
+
index: number
|
|
678
|
+
): Stroke {
|
|
679
|
+
const entry: Stroke =
|
|
680
|
+
palette.length > 0
|
|
681
|
+
? palette[index % palette.length]
|
|
682
|
+
: { weight: 4, fill: "white" }
|
|
683
|
+
if (series.fill == null) return entry
|
|
684
|
+
const layers = strokeOps.resolve(entry)
|
|
685
|
+
if (layers.length === 0) return { weight: 4, fill: series.fill }
|
|
686
|
+
const next = [...layers]
|
|
687
|
+
next[next.length - 1] = {
|
|
688
|
+
...next[next.length - 1],
|
|
689
|
+
fill: fillOps.resolve(series.fill),
|
|
690
|
+
}
|
|
691
|
+
return next
|
|
692
|
+
}
|
|
693
|
+
|
|
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
|
+
*/
|
|
699
|
+
export function resolveSeriesFill(
|
|
700
|
+
palette: Stroke[],
|
|
701
|
+
series: ChartSeries,
|
|
702
|
+
index: number
|
|
703
|
+
): Fill {
|
|
704
|
+
const layers = strokeOps.resolve(resolveSeriesStroke(palette, series, index))
|
|
705
|
+
return layers[layers.length - 1]?.fill ?? "white"
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* The effective paint for mark `index` of a **filled** chart (a bar, a wedge):
|
|
710
|
+
* the series' own `fill` when it carries one, else the palette's entry for its
|
|
711
|
+
* index, cycled.
|
|
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.
|
|
737
|
+
*/
|
|
738
|
+
export function formatMarker(value: number, step?: number): string {
|
|
739
|
+
if (Number.isInteger(value)) return String(value)
|
|
740
|
+
const decimals = Math.max(1, step === undefined ? 1 : decimalsFor(step))
|
|
741
|
+
return value.toFixed(decimals).replace(/(\.\d*?)0+$/, "$1").replace(/\.$/, "")
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/** Decimal places needed to write `step` exactly, capped at six. */
|
|
745
|
+
function decimalsFor(step: number): number {
|
|
746
|
+
if (!(step > 0) || !Number.isFinite(step)) return 1
|
|
747
|
+
for (let d = 0; d <= 6; d++) {
|
|
748
|
+
const scaled = step * 10 ** d
|
|
749
|
+
if (Math.abs(scaled - Math.round(scaled)) < 1e-6 * Math.max(1, scaled)) return d
|
|
750
|
+
}
|
|
751
|
+
// An irrational-looking step (a third): as many places as separate the ticks.
|
|
752
|
+
return Math.min(6, Math.max(1, Math.ceil(-Math.log10(step)) + 1))
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/** The gap between neighbouring tick values, or `undefined` for fewer than two distinct ones. */
|
|
756
|
+
export function markerStep(markers: readonly number[]): number | undefined {
|
|
757
|
+
const sorted = [...markers].filter(Number.isFinite).sort((a, b) => a - b)
|
|
758
|
+
let step: number | undefined
|
|
759
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
760
|
+
const gap = sorted[i] - sorted[i - 1]
|
|
761
|
+
if (gap > 1e-12 && (step === undefined || gap < step)) step = gap
|
|
762
|
+
}
|
|
763
|
+
return step
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/** Format a fraction in `[0, 1]` as a percentage: integers plain, else one decimal. */
|
|
767
|
+
export function formatPercent(fraction: number): string {
|
|
768
|
+
const pct = fraction * 100
|
|
769
|
+
if (Number.isInteger(pct)) return `${pct}%`
|
|
770
|
+
return `${pct.toFixed(1).replace(/\.0$/, "")}%`
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/** `markerCount` evenly-spaced tick values across `[min, max]`, ends inclusive. */
|
|
774
|
+
export function markerValues([min, max]: Range, markerCount: number): number[] {
|
|
775
|
+
const n = Math.max(2, Math.round(markerCount))
|
|
776
|
+
return Array.from({ length: n + 1 }, (_, i) => min + ((max - min) / n) * i)
|
|
777
|
+
}
|
|
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"
|