@motionscript/charts 0.1.0-alpha.3 → 0.1.0-alpha.5

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 (93) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +48 -0
  4. package/README.md +85 -0
  5. package/dist/bar-chart/bar-chart.d.ts +28 -13
  6. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  7. package/dist/bar-chart/bar-chart.js +60 -56
  8. package/dist/bar-chart/bar-chart.js.map +1 -1
  9. package/dist/bar-chart/category-axis.js.map +1 -1
  10. package/dist/bar-chart/chart-bars.js.map +1 -1
  11. package/dist/bar-chart/index.js.map +1 -1
  12. package/dist/bar-chart/shared.d.ts +3 -3
  13. package/dist/bar-chart/shared.js +3 -3
  14. package/dist/bar-chart/shared.js.map +1 -1
  15. package/dist/browser/chunks/chunk-5ZPBV6BU.js +2 -0
  16. package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +7 -0
  17. package/dist/browser/index.js +1 -1
  18. package/dist/browser/index.js.map +3 -3
  19. package/dist/browser/kit.js +1 -1
  20. package/dist/browser/manifest.json +3 -2
  21. package/dist/engine.js.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/kit/chart-border.js.map +1 -1
  24. package/dist/kit/chart-canvas.js.map +1 -1
  25. package/dist/kit/chart-legend.d.ts +4 -25
  26. package/dist/kit/chart-legend.d.ts.map +1 -1
  27. package/dist/kit/chart-legend.js +31 -55
  28. package/dist/kit/chart-legend.js.map +1 -1
  29. package/dist/kit/shared.d.ts +29 -6
  30. package/dist/kit/shared.d.ts.map +1 -1
  31. package/dist/kit/shared.js +64 -8
  32. package/dist/kit/shared.js.map +1 -1
  33. package/dist/kit/vertical-axis.js.map +1 -1
  34. package/dist/line-chart/chart-line.d.ts +2 -2
  35. package/dist/line-chart/chart-line.js.map +1 -1
  36. package/dist/line-chart/chart-region.js.map +1 -1
  37. package/dist/line-chart/horizontal-axis.js.map +1 -1
  38. package/dist/line-chart/index.js.map +1 -1
  39. package/dist/line-chart/line-chart.d.ts +9 -1
  40. package/dist/line-chart/line-chart.d.ts.map +1 -1
  41. package/dist/line-chart/line-chart.js +32 -8
  42. package/dist/line-chart/line-chart.js.map +1 -1
  43. package/dist/line-chart/shared.d.ts +3 -3
  44. package/dist/line-chart/shared.d.ts.map +1 -1
  45. package/dist/line-chart/shared.js +19 -4
  46. package/dist/line-chart/shared.js.map +1 -1
  47. package/dist/line-chart/x-scale.d.ts +1 -1
  48. package/dist/line-chart/x-scale.js +1 -1
  49. package/dist/line-chart/x-scale.js.map +1 -1
  50. package/dist/nodes.js.map +1 -1
  51. package/dist/pie-chart/index.js.map +1 -1
  52. package/dist/pie-chart/pie-chart.d.ts +2 -2
  53. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  54. package/dist/pie-chart/pie-chart.js +9 -7
  55. package/dist/pie-chart/pie-chart.js.map +1 -1
  56. package/dist/pie-chart/pie-legend.js +4 -4
  57. package/dist/pie-chart/pie-legend.js.map +1 -1
  58. package/dist/pie-chart/pie-ring.d.ts +13 -4
  59. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  60. package/dist/pie-chart/pie-ring.js +44 -14
  61. package/dist/pie-chart/pie-ring.js.map +1 -1
  62. package/dist/pie-chart/shared.d.ts +3 -3
  63. package/dist/pie-chart/shared.js +3 -3
  64. package/dist/pie-chart/shared.js.map +1 -1
  65. package/package.json +6 -5
  66. package/registry.json +23 -23
  67. package/src/bar-chart/bar-chart.ts +820 -814
  68. package/src/bar-chart/category-axis.ts +177 -177
  69. package/src/bar-chart/chart-bars.ts +310 -310
  70. package/src/bar-chart/index.ts +31 -31
  71. package/src/bar-chart/shared.ts +354 -354
  72. package/src/engine.ts +26 -26
  73. package/src/index.ts +5 -5
  74. package/src/kit/chart-border.ts +61 -61
  75. package/src/kit/chart-canvas.ts +157 -157
  76. package/src/kit/chart-legend.ts +245 -250
  77. package/src/kit/shared.ts +733 -675
  78. package/src/kit/vertical-axis.ts +224 -224
  79. package/src/line-chart/chart-line.ts +208 -208
  80. package/src/line-chart/chart-region.ts +135 -135
  81. package/src/line-chart/horizontal-axis.ts +202 -202
  82. package/src/line-chart/index.ts +39 -39
  83. package/src/line-chart/line-chart.ts +961 -929
  84. package/src/line-chart/shared.ts +473 -459
  85. package/src/line-chart/x-scale.ts +367 -367
  86. package/src/nodes.ts +20 -20
  87. package/src/pie-chart/index.ts +37 -37
  88. package/src/pie-chart/pie-chart.ts +383 -381
  89. package/src/pie-chart/pie-legend.ts +159 -159
  90. package/src/pie-chart/pie-ring.ts +935 -903
  91. package/src/pie-chart/shared.ts +490 -490
  92. package/dist/browser/chunks/chunk-RMHQ6GYU.js +0 -2
  93. package/dist/browser/chunks/chunk-RMHQ6GYU.js.map +0 -7
@@ -1,208 +1,208 @@
1
- import {
2
- type AssetScope,
3
- Graphics2D,
4
- Node2D,
5
- command,
6
- easeOut,
7
- strokeOps,
8
- property,
9
- type Command,
10
- type CommandArgs,
11
- type EasingFunction,
12
- type Node2DProps,
13
- type RenderContext2D,
14
- type Stroke,
15
- type StrokeResolved,
16
- type Vector2,
17
- } from "@motionscript/sdk"
18
-
19
- import { SPOTLIGHT_DIM } from "../kit/shared"
20
- import {
21
- EMPTY_REGION_BOX,
22
- clipPolylineToBox,
23
- hasRegion,
24
- type RegionBox,
25
- } from "./shared"
26
- import { declarePaints } from "@motionscript/sdk/component"
27
-
28
- export interface ChartLineProps extends Node2DProps {
29
- /**
30
- * The series polyline, already mapped into this node's local pixel space
31
- * (origin at centre, y-up) by the parent {@link LineChart}. Ordered along x.
32
- */
33
- points: Vector2[]
34
- /** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
35
- stroke: Stroke
36
- /**
37
- * Reveal of the line in `[0, 1]`: 0 hides it, 1 traces it fully from its left
38
- * end to its right.
39
- *
40
- * Defaults to **1**, unlike the standalone chart this was ported from: in the
41
- * studio a node's resting state is what the paused editor canvas paints, so a
42
- * chart that only appeared once a command had run would edit blind. {@link
43
- * enter} drives it from 0 regardless of where it rests, so the animation is
44
- * unchanged.
45
- */
46
- progress: number
47
- /**
48
- * Whether this line is drawn at all. `false` hides it outright — the switch
49
- * the chart flips alongside its legend's per-series toggle.
50
- */
51
- enabled: boolean
52
- /**
53
- * A slice of the plot to spotlight, in this node's own pixel space. When set
54
- * to a real box, the stretches of the line *outside* it drop to
55
- * {@link ChartLineProps.dimOpacity} while the stretches inside stay full.
56
- */
57
- spotlight: RegionBox
58
- /** Opacity the out-of-region stretches fade to. */
59
- dimOpacity: number
60
- }
61
-
62
- /**
63
- * A single series line of a {@link LineChart}: one rounded polyline traced
64
- * through pre-mapped pixel `points`, drawn as one {@link Graphics2D} command list.
65
- * Sized and positioned by the parent, which shares the plot's coordinate frame.
66
- *
67
- * Two signals drive it: {@link ChartLineProps.progress} traces the line in
68
- * (animated by {@link enter}) and `enabled` shows or hides it wholesale.
69
- */
70
- export class ChartLine extends Node2D<ChartLineProps> {
71
- @property({ default: () => [] as Vector2[] }) declare points: Vector2[]
72
- @property({ default: [], mapper: strokeOps.resolve, tween: strokeOps.lerp })
73
- declare stroke: Stroke
74
- @property({ default: 1 }) declare progress: number
75
- @property({ default: true }) declare enabled: boolean
76
- @property({ default: () => EMPTY_REGION_BOX }) declare spotlight: RegionBox
77
- @property({ default: SPOTLIGHT_DIM }) declare dimOpacity: number
78
-
79
- /**
80
- * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
81
- * paint props it carries — see {@link declarePaints}.
82
- */
83
- override declareAssets(assets: AssetScope): void {
84
- super.declareAssets(assets)
85
- declarePaints(this, assets)
86
- }
87
-
88
- protected renderSelf(ctx: RenderContext2D): void {
89
- if (!this.enabled) return
90
- const pts = this.points
91
- if (pts.length < 2) return
92
-
93
- const p = Math.max(0, Math.min(1, this.progress))
94
- if (p <= 0) return
95
-
96
- const drawn = this.tracePoints(pts, p)
97
- if (drawn.length < 2) return
98
- const stroke = this.stroke as StrokeResolved[]
99
- const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
100
-
101
- const box = this.spotlight
102
- if (!hasRegion(box)) {
103
- ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
104
- return
105
- }
106
-
107
- // Spotlit: the whole line goes down to `dimOpacity`, then the stretches
108
- // inside the region are re-drawn over it at full strength. Two passes rather
109
- // than one dim + one bright *complementary* pair, so the bright run has
110
- // continuous geometry through the region and its joins round the way an
111
- // unspotlit line's do — and so the boundary lands exactly on the region edge.
112
- ctx.draw(
113
- new Graphics2D()
114
- .line({ points: drawn, radius })
115
- .stroke(stroke)
116
- .opacity(Math.max(0, Math.min(1, this.dimOpacity)))
117
- )
118
-
119
- // One `draw` per run: shapes chained before a `stroke()` are combined into a
120
- // single surface, and stroking a union of disjoint open paths doesn't paint
121
- // — so a line that leaves the region and comes back would lose both halves.
122
- for (const run of clipPolylineToBox(drawn, box)) {
123
- ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
124
- }
125
- }
126
-
127
- /**
128
- * The prefix of `pts` revealed at fraction `p ∈ [0, 1]`, measuring the reveal
129
- * by *cumulative arc length* so the line traces at a steady pace regardless of
130
- * uneven point spacing. Returns the whole-point prefix plus one interpolated
131
- * point partway along the active segment.
132
- */
133
- private tracePoints(pts: Vector2[], p: number): Vector2[] {
134
- if (p >= 1 || pts.length < 2) return pts
135
- if (p <= 0) return []
136
-
137
- const segLen: number[] = []
138
- let total = 0
139
- for (let i = 1; i < pts.length; i++) {
140
- const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
141
- segLen.push(d)
142
- total += d
143
- }
144
- if (total === 0) return pts
145
-
146
- let target = total * p
147
- const out: Vector2[] = [pts[0]]
148
- for (let i = 0; i < segLen.length; i++) {
149
- if (target >= segLen[i]) {
150
- out.push(pts[i + 1])
151
- target -= segLen[i]
152
- } else {
153
- const t = segLen[i] === 0 ? 0 : target / segLen[i]
154
- out.push({
155
- x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
156
- y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
157
- })
158
- break
159
- }
160
- }
161
- return out
162
- }
163
-
164
- /**
165
- * Trace the line in from its left end to its right over `duration` seconds.
166
- * Drives `progress` `0 → 1`.
167
- *
168
- * A `Command` rather than a generator, so the trace can be *evaluated* at a
169
- * time instead of run to one — which is what lets a scene containing this
170
- * chart be scrubbed in constant time. It no longer needs to prime `progress`
171
- * to 0 first: the value at `t` is a function of `t`, so a repeat call re-traces
172
- * by definition rather than by having reset something.
173
- */
174
- @command()
175
- enter(args: CommandArgs<Record<string, never>> & { duration: number }): Command<ChartLineProps> {
176
- return this.enterFrom(0, args.duration, args.easing ?? easeOut())
177
- }
178
-
179
- /**
180
- * The trace, with both ends stated: `progress` runs `from` → `to`.
181
- *
182
- * What {@link LineChart.showSeries} and `hideSeries` are both made of — they
183
- * are the same motion in opposite directions, and expressing them as one thing
184
- * with endpoints is what stops the pair drifting apart.
185
- *
186
- * `enabled` rides along in the same value rather than being set before or after
187
- * the command. A line is drawable for every `t` the trace covers and undrawn
188
- * outside it, which is what makes arriving at a time backwards show what
189
- * arriving forwards does — a flag flipped by a side effect would not.
190
- */
191
- enterFrom(
192
- from: number,
193
- duration: number,
194
- easing: EasingFunction = easeOut(),
195
- to = 1
196
- ): Command<ChartLineProps> {
197
- return this.command<ChartLineProps>(
198
- (t) => {
199
- const progress = from + (to - from) * t
200
- // Hidden only once it has fully retraced, so the last frame of a hide is
201
- // the empty plot rather than a line that vanished a frame early.
202
- return { progress, enabled: progress > 0 }
203
- },
204
- duration,
205
- easing
206
- )
207
- }
208
- }
1
+ import {
2
+ type AssetScope,
3
+ Graphics2D,
4
+ Node2D,
5
+ command,
6
+ easeOut,
7
+ strokeOps,
8
+ property,
9
+ type Command,
10
+ type CommandArgs,
11
+ type EasingFunction,
12
+ type Node2DProps,
13
+ type RenderContext2D,
14
+ type Stroke,
15
+ type StrokeResolved,
16
+ type Vector2,
17
+ } from "@motionscript/sdk"
18
+
19
+ import { SPOTLIGHT_DIM } from "../kit/shared"
20
+ import {
21
+ EMPTY_REGION_BOX,
22
+ clipPolylineToBox,
23
+ hasRegion,
24
+ type RegionBox,
25
+ } from "./shared"
26
+ import { declarePaints } from "@motionscript/sdk/component"
27
+
28
+ export interface ChartLineProps extends Node2DProps {
29
+ /**
30
+ * The series polyline, already mapped into this node's local pixel space
31
+ * (origin at centre, y-up) by the parent {@link LineChart}. Ordered along x.
32
+ */
33
+ points: Vector2[]
34
+ /** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
35
+ stroke: Stroke
36
+ /**
37
+ * Reveal of the line in `[0, 1]`: 0 hides it, 1 traces it fully from its left
38
+ * end to its right.
39
+ *
40
+ * Defaults to **1**, unlike the standalone chart this was ported from: in an
41
+ * editor a node's resting state is what the paused editor canvas paints, so a
42
+ * chart that only appeared once a command had run would edit blind. {@link
43
+ * enter} drives it from 0 regardless of where it rests, so the animation is
44
+ * unchanged.
45
+ */
46
+ progress: number
47
+ /**
48
+ * Whether this line is drawn at all. `false` hides it outright — the switch
49
+ * the chart flips alongside its legend's per-series toggle.
50
+ */
51
+ enabled: boolean
52
+ /**
53
+ * A slice of the plot to spotlight, in this node's own pixel space. When set
54
+ * to a real box, the stretches of the line *outside* it drop to
55
+ * {@link ChartLineProps.dimOpacity} while the stretches inside stay full.
56
+ */
57
+ spotlight: RegionBox
58
+ /** Opacity the out-of-region stretches fade to. */
59
+ dimOpacity: number
60
+ }
61
+
62
+ /**
63
+ * A single series line of a {@link LineChart}: one rounded polyline traced
64
+ * through pre-mapped pixel `points`, drawn as one {@link Graphics2D} command list.
65
+ * Sized and positioned by the parent, which shares the plot's coordinate frame.
66
+ *
67
+ * Two signals drive it: {@link ChartLineProps.progress} traces the line in
68
+ * (animated by {@link enter}) and `enabled` shows or hides it wholesale.
69
+ */
70
+ export class ChartLine extends Node2D<ChartLineProps> {
71
+ @property({ default: () => [] as Vector2[] }) declare points: Vector2[]
72
+ @property({ default: [], mapper: strokeOps.resolve, tween: strokeOps.lerp })
73
+ declare stroke: Stroke
74
+ @property({ default: 1 }) declare progress: number
75
+ @property({ default: true }) declare enabled: boolean
76
+ @property({ default: () => EMPTY_REGION_BOX }) declare spotlight: RegionBox
77
+ @property({ default: SPOTLIGHT_DIM }) declare dimOpacity: number
78
+
79
+ /**
80
+ * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
81
+ * paint props it carries — see {@link declarePaints}.
82
+ */
83
+ override declareAssets(assets: AssetScope): void {
84
+ super.declareAssets(assets)
85
+ declarePaints(this, assets)
86
+ }
87
+
88
+ protected renderSelf(ctx: RenderContext2D): void {
89
+ if (!this.enabled) return
90
+ const pts = this.points
91
+ if (pts.length < 2) return
92
+
93
+ const p = Math.max(0, Math.min(1, this.progress))
94
+ if (p <= 0) return
95
+
96
+ const drawn = this.tracePoints(pts, p)
97
+ if (drawn.length < 2) return
98
+ const stroke = this.stroke as StrokeResolved[]
99
+ const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
100
+
101
+ const box = this.spotlight
102
+ if (!hasRegion(box)) {
103
+ ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
104
+ return
105
+ }
106
+
107
+ // Spotlit: the whole line goes down to `dimOpacity`, then the stretches
108
+ // inside the region are re-drawn over it at full strength. Two passes rather
109
+ // than one dim + one bright *complementary* pair, so the bright run has
110
+ // continuous geometry through the region and its joins round the way an
111
+ // unspotlit line's do — and so the boundary lands exactly on the region edge.
112
+ ctx.draw(
113
+ new Graphics2D()
114
+ .line({ points: drawn, radius })
115
+ .stroke(stroke)
116
+ .opacity(Math.max(0, Math.min(1, this.dimOpacity)))
117
+ )
118
+
119
+ // One `draw` per run: shapes chained before a `stroke()` are combined into a
120
+ // single surface, and stroking a union of disjoint open paths doesn't paint
121
+ // — so a line that leaves the region and comes back would lose both halves.
122
+ for (const run of clipPolylineToBox(drawn, box)) {
123
+ ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
124
+ }
125
+ }
126
+
127
+ /**
128
+ * The prefix of `pts` revealed at fraction `p ∈ [0, 1]`, measuring the reveal
129
+ * by *cumulative arc length* so the line traces at a steady pace regardless of
130
+ * uneven point spacing. Returns the whole-point prefix plus one interpolated
131
+ * point partway along the active segment.
132
+ */
133
+ private tracePoints(pts: Vector2[], p: number): Vector2[] {
134
+ if (p >= 1 || pts.length < 2) return pts
135
+ if (p <= 0) return []
136
+
137
+ const segLen: number[] = []
138
+ let total = 0
139
+ for (let i = 1; i < pts.length; i++) {
140
+ const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
141
+ segLen.push(d)
142
+ total += d
143
+ }
144
+ if (total === 0) return pts
145
+
146
+ let target = total * p
147
+ const out: Vector2[] = [pts[0]]
148
+ for (let i = 0; i < segLen.length; i++) {
149
+ if (target >= segLen[i]) {
150
+ out.push(pts[i + 1])
151
+ target -= segLen[i]
152
+ } else {
153
+ const t = segLen[i] === 0 ? 0 : target / segLen[i]
154
+ out.push({
155
+ x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
156
+ y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
157
+ })
158
+ break
159
+ }
160
+ }
161
+ return out
162
+ }
163
+
164
+ /**
165
+ * Trace the line in from its left end to its right over `duration` seconds.
166
+ * Drives `progress` `0 → 1`.
167
+ *
168
+ * A `Command` rather than a generator, so the trace can be *evaluated* at a
169
+ * time instead of run to one — which is what lets a scene containing this
170
+ * chart be scrubbed in constant time. It no longer needs to prime `progress`
171
+ * to 0 first: the value at `t` is a function of `t`, so a repeat call re-traces
172
+ * by definition rather than by having reset something.
173
+ */
174
+ @command()
175
+ enter(args: CommandArgs<Record<string, never>> & { duration: number }): Command<ChartLineProps> {
176
+ return this.enterFrom(0, args.duration, args.easing ?? easeOut())
177
+ }
178
+
179
+ /**
180
+ * The trace, with both ends stated: `progress` runs `from` → `to`.
181
+ *
182
+ * What {@link LineChart.showSeries} and `hideSeries` are both made of — they
183
+ * are the same motion in opposite directions, and expressing them as one thing
184
+ * with endpoints is what stops the pair drifting apart.
185
+ *
186
+ * `enabled` rides along in the same value rather than being set before or after
187
+ * the command. A line is drawable for every `t` the trace covers and undrawn
188
+ * outside it, which is what makes arriving at a time backwards show what
189
+ * arriving forwards does — a flag flipped by a side effect would not.
190
+ */
191
+ enterFrom(
192
+ from: number,
193
+ duration: number,
194
+ easing: EasingFunction = easeOut(),
195
+ to = 1
196
+ ): Command<ChartLineProps> {
197
+ return this.command<ChartLineProps>(
198
+ (t) => {
199
+ const progress = from + (to - from) * t
200
+ // Hidden only once it has fully retraced, so the last frame of a hide is
201
+ // the empty plot rather than a line that vanished a frame early.
202
+ return { progress, enabled: progress > 0 }
203
+ },
204
+ duration,
205
+ easing
206
+ )
207
+ }
208
+ }