@motionscript/charts 0.0.0-stage → 0.1.0-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.
Files changed (137) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +201 -0
  3. package/dist/bar-chart/bar-chart.d.ts +286 -0
  4. package/dist/bar-chart/bar-chart.d.ts.map +1 -0
  5. package/dist/bar-chart/bar-chart.js +610 -0
  6. package/dist/bar-chart/bar-chart.js.map +1 -0
  7. package/dist/bar-chart/category-axis.d.ts +77 -0
  8. package/dist/bar-chart/category-axis.d.ts.map +1 -0
  9. package/dist/bar-chart/category-axis.js +141 -0
  10. package/dist/bar-chart/category-axis.js.map +1 -0
  11. package/dist/bar-chart/chart-bars.d.ts +149 -0
  12. package/dist/bar-chart/chart-bars.d.ts.map +1 -0
  13. package/dist/bar-chart/chart-bars.js +236 -0
  14. package/dist/bar-chart/chart-bars.js.map +1 -0
  15. package/dist/bar-chart/index.d.ts +16 -0
  16. package/dist/bar-chart/index.d.ts.map +1 -0
  17. package/dist/bar-chart/index.js +12 -0
  18. package/dist/bar-chart/index.js.map +1 -0
  19. package/dist/bar-chart/shared.d.ts +146 -0
  20. package/dist/bar-chart/shared.d.ts.map +1 -0
  21. package/dist/bar-chart/shared.js +182 -0
  22. package/dist/bar-chart/shared.js.map +1 -0
  23. package/dist/browser/chunks/chunk-ZMBOBHYL.js +2 -0
  24. package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +7 -0
  25. package/dist/browser/index.js +2 -0
  26. package/dist/browser/index.js.map +7 -0
  27. package/dist/browser/kit.js +2 -0
  28. package/dist/browser/kit.js.map +7 -0
  29. package/dist/browser/manifest.json +12 -0
  30. package/dist/engine.d.ts +27 -0
  31. package/dist/engine.d.ts.map +1 -0
  32. package/dist/engine.js +27 -0
  33. package/dist/engine.js.map +1 -0
  34. package/dist/index.d.ts +6 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +6 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/kit/chart-border.d.ts +28 -0
  39. package/dist/kit/chart-border.d.ts.map +1 -0
  40. package/dist/kit/chart-border.js +45 -0
  41. package/dist/kit/chart-border.js.map +1 -0
  42. package/dist/kit/chart-canvas.d.ts +56 -0
  43. package/dist/kit/chart-canvas.d.ts.map +1 -0
  44. package/dist/kit/chart-canvas.js +125 -0
  45. package/dist/kit/chart-canvas.js.map +1 -0
  46. package/dist/kit/chart-legend.d.ts +126 -0
  47. package/dist/kit/chart-legend.d.ts.map +1 -0
  48. package/dist/kit/chart-legend.js +212 -0
  49. package/dist/kit/chart-legend.js.map +1 -0
  50. package/dist/kit/shared.d.ts +251 -0
  51. package/dist/kit/shared.d.ts.map +1 -0
  52. package/dist/kit/shared.js +401 -0
  53. package/dist/kit/shared.js.map +1 -0
  54. package/dist/kit/vertical-axis.d.ts +99 -0
  55. package/dist/kit/vertical-axis.d.ts.map +1 -0
  56. package/dist/kit/vertical-axis.js +176 -0
  57. package/dist/kit/vertical-axis.js.map +1 -0
  58. package/dist/line-chart/chart-line.d.ts +91 -0
  59. package/dist/line-chart/chart-line.d.ts.map +1 -0
  60. package/dist/line-chart/chart-line.js +156 -0
  61. package/dist/line-chart/chart-line.js.map +1 -0
  62. package/dist/line-chart/chart-region.d.ts +47 -0
  63. package/dist/line-chart/chart-region.d.ts.map +1 -0
  64. package/dist/line-chart/chart-region.js +101 -0
  65. package/dist/line-chart/chart-region.js.map +1 -0
  66. package/dist/line-chart/horizontal-axis.d.ts +85 -0
  67. package/dist/line-chart/horizontal-axis.d.ts.map +1 -0
  68. package/dist/line-chart/horizontal-axis.js +158 -0
  69. package/dist/line-chart/horizontal-axis.js.map +1 -0
  70. package/dist/line-chart/index.d.ts +22 -0
  71. package/dist/line-chart/index.d.ts.map +1 -0
  72. package/dist/line-chart/index.js +19 -0
  73. package/dist/line-chart/index.js.map +1 -0
  74. package/dist/line-chart/line-chart.d.ts +347 -0
  75. package/dist/line-chart/line-chart.d.ts.map +1 -0
  76. package/dist/line-chart/line-chart.js +701 -0
  77. package/dist/line-chart/line-chart.js.map +1 -0
  78. package/dist/line-chart/shared.d.ts +171 -0
  79. package/dist/line-chart/shared.d.ts.map +1 -0
  80. package/dist/line-chart/shared.js +270 -0
  81. package/dist/line-chart/shared.js.map +1 -0
  82. package/dist/line-chart/x-scale.d.ts +84 -0
  83. package/dist/line-chart/x-scale.d.ts.map +1 -0
  84. package/dist/line-chart/x-scale.js +302 -0
  85. package/dist/line-chart/x-scale.js.map +1 -0
  86. package/dist/nodes.d.ts +20 -0
  87. package/dist/nodes.d.ts.map +1 -0
  88. package/dist/nodes.js +20 -0
  89. package/dist/nodes.js.map +1 -0
  90. package/dist/pie-chart/index.d.ts +18 -0
  91. package/dist/pie-chart/index.d.ts.map +1 -0
  92. package/dist/pie-chart/index.js +14 -0
  93. package/dist/pie-chart/index.js.map +1 -0
  94. package/dist/pie-chart/pie-chart.d.ts +170 -0
  95. package/dist/pie-chart/pie-chart.d.ts.map +1 -0
  96. package/dist/pie-chart/pie-chart.js +291 -0
  97. package/dist/pie-chart/pie-chart.js.map +1 -0
  98. package/dist/pie-chart/pie-legend.d.ts +60 -0
  99. package/dist/pie-chart/pie-legend.d.ts.map +1 -0
  100. package/dist/pie-chart/pie-legend.js +131 -0
  101. package/dist/pie-chart/pie-legend.js.map +1 -0
  102. package/dist/pie-chart/pie-ring.d.ts +266 -0
  103. package/dist/pie-chart/pie-ring.d.ts.map +1 -0
  104. package/dist/pie-chart/pie-ring.js +708 -0
  105. package/dist/pie-chart/pie-ring.js.map +1 -0
  106. package/dist/pie-chart/shared.d.ts +210 -0
  107. package/dist/pie-chart/shared.d.ts.map +1 -0
  108. package/dist/pie-chart/shared.js +240 -0
  109. package/dist/pie-chart/shared.js.map +1 -0
  110. package/package.json +69 -3
  111. package/registry.json +23 -0
  112. package/src/bar-chart/bar-chart.ts +804 -0
  113. package/src/bar-chart/category-axis.ts +177 -0
  114. package/src/bar-chart/chart-bars.ts +310 -0
  115. package/src/bar-chart/index.ts +31 -0
  116. package/src/bar-chart/shared.ts +354 -0
  117. package/src/engine.ts +26 -0
  118. package/src/index.ts +5 -0
  119. package/src/kit/chart-border.ts +61 -0
  120. package/src/kit/chart-canvas.ts +157 -0
  121. package/src/kit/chart-legend.ts +250 -0
  122. package/src/kit/shared.ts +675 -0
  123. package/src/kit/vertical-axis.ts +224 -0
  124. package/src/line-chart/chart-line.ts +208 -0
  125. package/src/line-chart/chart-region.ts +135 -0
  126. package/src/line-chart/horizontal-axis.ts +202 -0
  127. package/src/line-chart/index.ts +39 -0
  128. package/src/line-chart/line-chart.ts +894 -0
  129. package/src/line-chart/shared.ts +459 -0
  130. package/src/line-chart/x-scale.ts +367 -0
  131. package/src/nodes.ts +20 -0
  132. package/src/pie-chart/index.ts +37 -0
  133. package/src/pie-chart/pie-chart.ts +374 -0
  134. package/src/pie-chart/pie-legend.ts +159 -0
  135. package/src/pie-chart/pie-ring.ts +903 -0
  136. package/src/pie-chart/shared.ts +490 -0
  137. package/README.md +0 -4
@@ -0,0 +1,354 @@
1
+ /**
2
+ * Types and pure helpers specific to the {@link BarChart} family: how a row's
3
+ * values become the spans its columns cover, how the value axis is scaled around
4
+ * them, and the theme the bars add to the shared chart vocabulary.
5
+ *
6
+ * Everything a line or a pie chart would want too — what a series is, the axis
7
+ * domain model, the theme leaf resolvers, the legend and plot-panel branches —
8
+ * lives in `nodes/chart-kit/shared.ts` and is imported here.
9
+ *
10
+ * Kept renderer-agnostic (no `Node` imports) so the studio's inspector mapping
11
+ * (`bar-chart-props.ts`) can build a partial {@link BarChartTheme} without
12
+ * pulling the node classes in.
13
+ */
14
+
15
+ import {
16
+ fillOps,
17
+ lerpNumber,
18
+ shadowOps,
19
+ strokeOps,
20
+ textOps,
21
+ type DataRecord,
22
+ type Fill,
23
+ type Shadow,
24
+ type ShadowResolved,
25
+ type Stroke,
26
+ type StrokeResolved,
27
+ type TextStyle,
28
+ } from "@motionscript/core"
29
+
30
+ import {
31
+ CHART_PALETTE,
32
+ DEFAULT_LEGEND,
33
+ DEFAULT_PLOT_AREA,
34
+ DEFAULT_SERIES_FILLS,
35
+ DEFAULT_X_AXIS,
36
+ DEFAULT_Y_AXIS,
37
+ SPOTLIGHT_DIM,
38
+ lerpAxisConfig,
39
+ lerpLegendConfig,
40
+ lerpPlotArea,
41
+ lerpSeriesFills,
42
+ niceStep,
43
+ num,
44
+ resolveAxisConfig,
45
+ resolveLegendConfig,
46
+ resolvePlotArea,
47
+ resolveSeriesFills,
48
+ text,
49
+ type AxisConfig,
50
+ type ChartSeries,
51
+ type LegendConfig,
52
+ type LegendTheme,
53
+ type PlotAreaConfig,
54
+ type Range,
55
+ } from "../kit/shared"
56
+
57
+ /**
58
+ * One series in a {@link BarChart}: a column of the table drawn as one bar per
59
+ * row — side by side with its siblings within each category's slot, or stacked
60
+ * on top of them.
61
+ *
62
+ * The family-wide {@link ChartSeries} under a name that says what it draws here.
63
+ */
64
+ export type BarSeries = ChartSeries
65
+
66
+ /**
67
+ * One drawn bar's span in **data units**: the value its base sits at and the
68
+ * value its far end reaches.
69
+ *
70
+ * A span rather than a height because that is the one shape both modes fit. A
71
+ * grouped bar runs from zero to its value; a stacked segment runs from the
72
+ * running total below it to that total plus its own value. Negative values fall
73
+ * out of the same arithmetic — the span simply runs the other way — so a chart
74
+ * with a loss in it needs no special case, only a baseline that isn't the floor.
75
+ */
76
+ export interface BarSpan {
77
+ /** Value the bar's base sits at — 0 when grouped, the running total when stacked. */
78
+ from: number
79
+ /** Value its far end reaches. */
80
+ to: number
81
+ /** The value this bar plots — `to - from`, kept for the figure printed on it. */
82
+ value: number
83
+ }
84
+
85
+ /**
86
+ * The spans one row's bars cover, one per series, in series order.
87
+ *
88
+ * Stacking is a running sum in the order the series are listed, which is also
89
+ * the order they're drawn: series 0 sits on the baseline and each later one
90
+ * rides on what's below it. A zero-valued series still yields a (degenerate)
91
+ * span so the returned list stays index-aligned with the series list.
92
+ */
93
+ export function barSpans(
94
+ row: DataRecord,
95
+ series: BarSeries[],
96
+ stacked: boolean
97
+ ): BarSpan[] {
98
+ let running = 0
99
+ return series.map((s) => {
100
+ const value = num(row, s.field)
101
+ if (!stacked) return { from: 0, to: value, value }
102
+ const from = running
103
+ running += value
104
+ return { from, to: running, value }
105
+ })
106
+ }
107
+
108
+ /**
109
+ * The index of the series whose segment ends a row's stack — the one that gets
110
+ * the rounded cap, so a stack reads as one capped column rather than a pile of
111
+ * separate boxes. `-1` when every value in the row is zero.
112
+ *
113
+ * Read off the *last non-zero* series rather than the last one listed, so a
114
+ * series that happens to be 0 in this row doesn't take the cap and leave the
115
+ * segment below it square.
116
+ */
117
+ export function stackCapIndex(row: DataRecord, series: BarSeries[]): number {
118
+ let last = -1
119
+ for (let i = 0; i < series.length; i++) {
120
+ if (num(row, series[i].field) !== 0) last = i
121
+ }
122
+ return last
123
+ }
124
+
125
+ /** The captions beneath the baseline: one per row, read off the category column. */
126
+ export function barCategories(data: DataRecord[], field: string): string[] {
127
+ return data.map((row) => text(row, field))
128
+ }
129
+
130
+ /**
131
+ * The `[min, max]` of everything the chart draws, **zero always included**.
132
+ *
133
+ * Zero is in the range whatever the data says, because a bar chart's baseline is
134
+ * zero: a scale starting at 40 draws a column three times its neighbour for a
135
+ * 10% difference, which is the classic way to lie with one. A chart that really
136
+ * does want a cropped axis says so with an explicit range.
137
+ */
138
+ export function barValueExtent(
139
+ data: DataRecord[],
140
+ series: BarSeries[],
141
+ stacked: boolean
142
+ ): Range {
143
+ let lo = 0
144
+ let hi = 0
145
+ for (const row of data) {
146
+ for (const span of barSpans(row, series, stacked)) {
147
+ lo = Math.min(lo, span.from, span.to)
148
+ hi = Math.max(hi, span.from, span.to)
149
+ }
150
+ }
151
+ return lo < hi ? [lo, hi] : [0, 1]
152
+ }
153
+
154
+ /**
155
+ * Effective value scale: the explicit `range`, else the data's extent nudged out
156
+ * to tidy step bounds so the markers read as round numbers.
157
+ *
158
+ * `divisions` is how many segments the auto-range is stepped into (one fewer
159
+ * than the marker count), so the ticks the axis draws land on the bounds this
160
+ * picks.
161
+ */
162
+ export function resolveBarScale(
163
+ data: DataRecord[],
164
+ series: BarSeries[],
165
+ stacked: boolean,
166
+ range: Range | undefined,
167
+ divisions: number
168
+ ): Range {
169
+ if (range) return range
170
+ const [lo, hi] = barValueExtent(data, series, stacked)
171
+ const segments = Math.max(1, Math.round(divisions))
172
+ const step = niceStep((hi - lo) / segments || 1)
173
+ return [Math.floor(lo / step) * step, Math.ceil(hi / step) * step]
174
+ }
175
+
176
+ // ---- Theme ---------------------------------------------------------------
177
+
178
+ /** How the columns themselves are drawn and how the slot is divided up. */
179
+ export interface BarStyleConfig {
180
+ /**
181
+ * Radius of a column's far-end corners, in px. Only the outer end rounds — an
182
+ * interior stacked segment stays square, so the stack reads as one column.
183
+ */
184
+ cornerRadius: number
185
+ /**
186
+ * Fraction of its category's slot the bars fill, `[0, 1]`. The remainder is
187
+ * the channel between one category and the next, which is what makes a bar
188
+ * chart read as groups rather than as a solid block.
189
+ */
190
+ slotFill: number
191
+ /** Gap between the grouped bars *within* one slot, in px. Ignored when stacked. */
192
+ groupGap: number
193
+ /** Optional outline on every column. */
194
+ stroke: Stroke
195
+ /** Optional drop shadow on every column. */
196
+ shadow: Shadow
197
+ }
198
+
199
+ /** A {@link BarChart}'s full, resolved visual theme. */
200
+ export interface BarChartConfig {
201
+ /**
202
+ * Default per-series bar paint, cycled by series index (`i % length`). A
203
+ * series' own `fill`, when set, replaces its entry outright.
204
+ */
205
+ seriesFills: Fill[]
206
+ bar: BarStyleConfig
207
+ /** The category axis beneath the baseline. */
208
+ xAxis: AxisConfig
209
+ /** The numeric value axis up the left. */
210
+ yAxis: AxisConfig
211
+ plotArea: PlotAreaConfig
212
+ legend: LegendConfig
213
+ /** The figure printed on each column, when the chart prints them. */
214
+ valueStyle: TextStyle
215
+ /** How far everything *outside* a spotlit series or category fades. */
216
+ spotlight: { dimOpacity: number }
217
+ }
218
+
219
+ /** Author-facing partial theme — every branch optional and deep-merged. */
220
+ export type BarChartTheme = {
221
+ seriesFills?: Fill[]
222
+ bar?: Partial<BarStyleConfig>
223
+ xAxis?: Partial<AxisConfig>
224
+ yAxis?: Partial<AxisConfig>
225
+ plotArea?: Partial<PlotAreaConfig>
226
+ legend?: LegendTheme
227
+ valueStyle?: TextStyle
228
+ spotlight?: Partial<BarChartConfig["spotlight"]>
229
+ }
230
+
231
+ /** The bar chart's out-of-the-box look. */
232
+ export const DEFAULT_BAR_CHART_THEME: BarChartConfig = {
233
+ seriesFills: DEFAULT_SERIES_FILLS,
234
+ bar: {
235
+ cornerRadius: 6,
236
+ slotFill: 0.72,
237
+ groupGap: 6,
238
+ stroke: [],
239
+ shadow: [],
240
+ },
241
+ // A category axis names its slots, so its captions are set like a caption
242
+ // rather than like a numeric marker: same size, less weight than the line
243
+ // chart's ticks, and centred on the slot.
244
+ xAxis: {
245
+ ...DEFAULT_X_AXIS,
246
+ labelStyle: { ...DEFAULT_X_AXIS.labelStyle, fontWeight: 600 },
247
+ tickPadding: 16,
248
+ },
249
+ yAxis: DEFAULT_Y_AXIS,
250
+ plotArea: DEFAULT_PLOT_AREA,
251
+ legend: DEFAULT_LEGEND,
252
+ valueStyle: {
253
+ fontSize: 24,
254
+ fontWeight: 700,
255
+ textAlign: "center",
256
+ fill: CHART_PALETTE.markerColor,
257
+ },
258
+ spotlight: { dimOpacity: SPOTLIGHT_DIM },
259
+ }
260
+
261
+ function resolveBarStyle(
262
+ bar: Partial<BarStyleConfig> | undefined,
263
+ previous: BarStyleConfig
264
+ ): BarStyleConfig {
265
+ return {
266
+ cornerRadius: bar?.cornerRadius ?? previous.cornerRadius,
267
+ slotFill: bar?.slotFill ?? previous.slotFill,
268
+ groupGap: bar?.groupGap ?? previous.groupGap,
269
+ stroke: strokeOps.resolve(bar?.stroke ?? previous.stroke),
270
+ shadow: shadowOps.resolve(bar?.shadow ?? previous.shadow),
271
+ }
272
+ }
273
+
274
+ function lerpBarStyle(
275
+ from: BarStyleConfig,
276
+ to: BarStyleConfig,
277
+ t: number
278
+ ): BarStyleConfig {
279
+ return {
280
+ cornerRadius: lerpNumber(from.cornerRadius, to.cornerRadius, t),
281
+ slotFill: lerpNumber(from.slotFill, to.slotFill, t),
282
+ groupGap: lerpNumber(from.groupGap, to.groupGap, t),
283
+ stroke: strokeOps.lerp(
284
+ from.stroke as StrokeResolved[],
285
+ to.stroke as StrokeResolved[],
286
+ t
287
+ ),
288
+ shadow: shadowOps.lerp(
289
+ from.shadow as ShadowResolved[],
290
+ to.shadow as ShadowResolved[],
291
+ t
292
+ ),
293
+ }
294
+ }
295
+
296
+ /**
297
+ * {@link BarChart}'s `theme` @property mapper: merges the author's partial
298
+ * {@link BarChartTheme} onto `previous` (falling back to
299
+ * {@link DEFAULT_BAR_CHART_THEME} on first resolution), fully resolving every
300
+ * leaf so the tween below always has resolved data to interpolate.
301
+ */
302
+ export function resolveBarChartTheme(
303
+ theme: BarChartTheme | undefined,
304
+ previous?: BarChartConfig
305
+ ): BarChartConfig {
306
+ const base = previous ?? DEFAULT_BAR_CHART_THEME
307
+ return {
308
+ seriesFills: resolveSeriesFills(theme?.seriesFills, base.seriesFills),
309
+ bar: resolveBarStyle(theme?.bar, base.bar),
310
+ xAxis: resolveAxisConfig(theme?.xAxis, base.xAxis),
311
+ yAxis: resolveAxisConfig(theme?.yAxis, base.yAxis),
312
+ plotArea: resolvePlotArea(theme?.plotArea, base.plotArea),
313
+ legend: resolveLegendConfig(theme?.legend, base.legend),
314
+ valueStyle: textOps.resolveStyle(theme?.valueStyle, base.valueStyle),
315
+ spotlight: {
316
+ dimOpacity: theme?.spotlight?.dimOpacity ?? base.spotlight.dimOpacity,
317
+ },
318
+ }
319
+ }
320
+
321
+ /** {@link BarChart}'s `theme` @property tween — leaf by leaf. */
322
+ export function lerpBarChartTheme(
323
+ from: BarChartConfig,
324
+ to: BarChartConfig,
325
+ t: number
326
+ ): BarChartConfig {
327
+ return {
328
+ seriesFills: lerpSeriesFills(from.seriesFills, to.seriesFills, t),
329
+ bar: lerpBarStyle(from.bar, to.bar, t),
330
+ xAxis: lerpAxisConfig(from.xAxis, to.xAxis, t),
331
+ yAxis: lerpAxisConfig(from.yAxis, to.yAxis, t),
332
+ plotArea: lerpPlotArea(from.plotArea, to.plotArea, t),
333
+ legend: lerpLegendConfig(from.legend, to.legend, t),
334
+ valueStyle: textOps.lerpStyle(from.valueStyle, to.valueStyle, t),
335
+ spotlight: {
336
+ dimOpacity: lerpNumber(
337
+ from.spotlight.dimOpacity,
338
+ to.spotlight.dimOpacity,
339
+ t
340
+ ),
341
+ },
342
+ }
343
+ }
344
+
345
+ /** The paint series `index` is drawn in: its own override, else the palette's entry. */
346
+ export function resolveBarFill(
347
+ palette: Fill[],
348
+ series: BarSeries,
349
+ index: number
350
+ ): Fill {
351
+ if (series.fill != null) return fillOps.resolve(series.fill)
352
+ if (palette.length === 0) return "white"
353
+ return palette[index % palette.length]
354
+ }
package/src/engine.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The chart **kit**: everything the three charts share, and the one thing a
3
+ * copied chart does not copy.
4
+ *
5
+ * `ms add line-chart` writes `line-chart/*` into your project and rewrites its
6
+ * reach for these to `@motionscript/charts`, so the plot panel, the value
7
+ * axis, the legend and the number formatting stay a real dependency. That is
8
+ * not a size decision — it is the only arrangement that works.
9
+ *
10
+ * A chart identifies its own parts, and the engine keys signal machinery to
11
+ * class identity: `@property` installs against the class it was applied with,
12
+ * and the renderer asks `instanceof` about what it is drawing. Copy
13
+ * `VerticalAxis` alongside your fork and there are now two of it — two classes
14
+ * answering one name, and whichever one your chart did not get renders as
15
+ * nothing at all, with no error. Keeping the kit external makes that
16
+ * impossible rather than unlikely.
17
+ *
18
+ * It also means a fix to the axis reaches a forked chart without a re-add,
19
+ * which is the half of the bargain you get back for it.
20
+ */
21
+ export * from "./kit/shared"
22
+ export * from "./kit/chart-border"
23
+ export * from "./kit/chart-canvas"
24
+ export * from "./kit/chart-legend"
25
+ export * from "./kit/vertical-axis"
26
+ export * from "@motionscript/core/component"
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./line-chart";
2
+ export * from "./bar-chart";
3
+ export * from "./pie-chart";
4
+ export * from "./engine";
5
+ export { NODES } from "./nodes";
@@ -0,0 +1,61 @@
1
+ import {
2
+ type AssetScope,
3
+ Graphics2D,
4
+ Node2D,
5
+ strokeOps,
6
+ property,
7
+ type Node2DProps,
8
+ type RenderContext2D,
9
+ type Stroke,
10
+ } from "@motionscript/core"
11
+
12
+ import { DEFAULT_PLOT_AREA } from "./shared"
13
+ import { declarePaints } from "@motionscript/core/component"
14
+
15
+ export interface ChartBorderProps extends Node2DProps {
16
+ /**
17
+ * The plot's thick frame stroke. The stroke resolver's default `align` is
18
+ * `'inside'`, so the whole weight sits within the layout box unless the theme
19
+ * overrides it.
20
+ */
21
+ stroke: Stroke
22
+ }
23
+
24
+ /**
25
+ * The thick **frame** around a cartesian chart's plot: an empty rect wearing an
26
+ * inside stroke, sized from its allocated `layoutBounds`.
27
+ *
28
+ * Split out of {@link ChartCanvas} (the filled + gridded backdrop) so it can be
29
+ * placed *after* the marks in the tree — the box then frames the plot **over**
30
+ * them, so a line or a column running to the edge tucks cleanly under the border
31
+ * rather than sitting on top of it.
32
+ */
33
+ export class ChartBorder extends Node2D<ChartBorderProps> {
34
+ @property({
35
+ default: DEFAULT_PLOT_AREA.stroke,
36
+ mapper: strokeOps.resolve,
37
+ tween: strokeOps.lerp,
38
+ })
39
+ declare stroke: Stroke
40
+
41
+ /**
42
+ * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
43
+ * paint props it carries — see {@link declarePaints}.
44
+ */
45
+ override declareAssets(assets: AssetScope): void {
46
+ super.declareAssets(assets)
47
+ declarePaints(this, assets)
48
+ }
49
+
50
+ protected renderSelf(ctx: RenderContext2D): void {
51
+ const W = this.layoutBounds.width
52
+ const H = this.layoutBounds.height
53
+ if (W <= 0 || H <= 0) return
54
+
55
+ // Empty rect (no fill) with the themed stroke, so the frame sits over the
56
+ // marks but the box interior stays clear.
57
+ ctx.draw(
58
+ new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).stroke(this.stroke)
59
+ )
60
+ }
61
+ }
@@ -0,0 +1,157 @@
1
+ import {
2
+ type AssetScope,
3
+ Graphics2D,
4
+ Node2D,
5
+ fillOps,
6
+ shadowOps,
7
+ strokeOps,
8
+ property,
9
+ type Fill,
10
+ type Node2DProps,
11
+ type RenderContext2D,
12
+ type Shadow,
13
+ type Stroke,
14
+ type StrokeResolved,
15
+ } from "@motionscript/core"
16
+
17
+ import {
18
+ DEFAULT_PLOT_AREA,
19
+ DEFAULT_X_AXIS,
20
+ DEFAULT_Y_AXIS,
21
+ type Range,
22
+ } from "./shared"
23
+ import { declarePaints } from "@motionscript/core/component"
24
+
25
+ export interface ChartCanvasProps extends Node2DProps {
26
+ /** Fill of the plot panel behind the marks. */
27
+ fill: Fill
28
+ /** Drop shadow of the panel. Default none. */
29
+ shadow: Shadow
30
+ /** Stroke of the vertical grid lines (one per {@link xMarkers}). */
31
+ xGridStroke: Stroke
32
+ /** Stroke of the horizontal grid lines (one per {@link yMarkers}). */
33
+ yGridStroke: Stroke
34
+ /**
35
+ * Data-unit x positions to draw a vertical grid line at — one per horizontal
36
+ * axis marker, so a line lands under every marker regardless of spacing.
37
+ * Positions at (or past) an {@link xExtent} edge coincide with the border and
38
+ * are skipped. Pass an empty list for a chart whose horizontal axis is
39
+ * categorical and so has no numeric position to rule at.
40
+ */
41
+ xMarkers: number[]
42
+ /** Data-unit y positions to draw a horizontal grid line at. */
43
+ yMarkers: number[]
44
+ /** `[min, max]` of the horizontal scale — maps {@link xMarkers} into the panel. */
45
+ xExtent: Range
46
+ /** `[min, max]` of the vertical scale — maps {@link yMarkers} into the panel. */
47
+ yExtent: Range
48
+ }
49
+
50
+ /**
51
+ * The plot **backdrop** of a cartesian chart: a filled panel with a grid of
52
+ * faint lines spanning both directions across it. Drawn as one {@link Graphics2D}
53
+ * command list, sized from its allocated `layoutBounds` and centred on the node
54
+ * origin.
55
+ *
56
+ * The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
57
+ * *over* the marks while this panel sits under them. Grid lines are placed from
58
+ * the axis marker positions (in data units) mapped into the panel through
59
+ * {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
60
+ * marker whether or not the markers are evenly spaced.
61
+ */
62
+ export class ChartCanvas extends Node2D<ChartCanvasProps> {
63
+ @property({
64
+ default: DEFAULT_PLOT_AREA.fill,
65
+ mapper: fillOps.resolve,
66
+ tween: fillOps.lerp,
67
+ })
68
+ declare fill: Fill
69
+ @property({
70
+ default: DEFAULT_PLOT_AREA.shadow,
71
+ mapper: shadowOps.resolve,
72
+ tween: shadowOps.lerp,
73
+ })
74
+ declare shadow: Shadow
75
+ @property({
76
+ default: DEFAULT_X_AXIS.lineStyle,
77
+ mapper: strokeOps.resolve,
78
+ tween: strokeOps.lerp,
79
+ })
80
+ declare xGridStroke: Stroke
81
+ @property({
82
+ default: DEFAULT_Y_AXIS.lineStyle,
83
+ mapper: strokeOps.resolve,
84
+ tween: strokeOps.lerp,
85
+ })
86
+ declare yGridStroke: Stroke
87
+ @property({ default: () => [] as number[] }) declare xMarkers: number[]
88
+ @property({ default: () => [] as number[] }) declare yMarkers: number[]
89
+ @property({ default: () => [0, 1] as Range }) declare xExtent: Range
90
+ @property({ default: () => [0, 1] as Range }) declare yExtent: Range
91
+
92
+ /**
93
+ * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
94
+ * paint props it carries — see {@link declarePaints}. The grid strokes are
95
+ * named past the conventional slots, so they are passed explicitly.
96
+ */
97
+ override declareAssets(assets: AssetScope): void {
98
+ super.declareAssets(assets)
99
+ declarePaints(this, assets, {
100
+ strokes: [this.xGridStroke as StrokeResolved[], this.yGridStroke as StrokeResolved[]],
101
+ })
102
+ }
103
+
104
+ protected renderSelf(ctx: RenderContext2D): void {
105
+ const W = this.layoutBounds.width
106
+ const H = this.layoutBounds.height
107
+ if (W <= 0 || H <= 0) return
108
+
109
+ // Plot panel, centred on the origin (node space is y-up, origin centre).
110
+ ctx.draw(
111
+ new Graphics2D()
112
+ .rect({ x: 0, y: 0, width: W, height: H })
113
+ .shadow(this.shadow)
114
+ .fill(this.fill)
115
+ )
116
+
117
+ const [xMin, xMax] = this.xExtent
118
+ const [yMin, yMax] = this.yExtent
119
+ // Fraction of a marker across its extent; guard a zero-width extent.
120
+ const xFrac = (v: number) => (xMax <= xMin ? 0 : (v - xMin) / (xMax - xMin))
121
+ const yFrac = (v: number) => (yMax <= yMin ? 0 : (v - yMin) / (yMax - yMin))
122
+ // Skip markers landing on (or outside) the edges — they'd sit under the border.
123
+ const interior = (f: number) => f > 1e-6 && f < 1 - 1e-6
124
+
125
+ const xStroke = (this.xGridStroke as StrokeResolved[])[0]
126
+ if (xStroke) {
127
+ const xGrid = new Graphics2D()
128
+ for (const v of this.xMarkers) {
129
+ const f = xFrac(v)
130
+ if (!interior(f)) continue
131
+ xGrid.rect({
132
+ x: -W / 2 + f * W,
133
+ y: 0,
134
+ width: xStroke.weight,
135
+ height: H,
136
+ })
137
+ }
138
+ ctx.draw(xGrid.fill(xStroke.fill))
139
+ }
140
+
141
+ const yStroke = (this.yGridStroke as StrokeResolved[])[0]
142
+ if (yStroke) {
143
+ const yGrid = new Graphics2D()
144
+ for (const v of this.yMarkers) {
145
+ const f = yFrac(v)
146
+ if (!interior(f)) continue
147
+ yGrid.rect({
148
+ x: 0,
149
+ y: -H / 2 + f * H,
150
+ width: W,
151
+ height: yStroke.weight,
152
+ })
153
+ }
154
+ ctx.draw(yGrid.fill(yStroke.fill))
155
+ }
156
+ }
157
+ }