@motionscript/charts 0.1.0-alpha.0 → 0.1.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/CHANGELOG.md +105 -0
  2. package/NOTICE +49 -0
  3. package/README.md +99 -0
  4. package/dist/bar-chart/bar-chart.d.ts +133 -228
  5. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  6. package/dist/bar-chart/bar-chart.js +386 -565
  7. package/dist/bar-chart/bar-chart.js.map +1 -1
  8. package/dist/bar-chart/category-axis.d.ts +35 -41
  9. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  10. package/dist/bar-chart/category-axis.js +93 -115
  11. package/dist/bar-chart/category-axis.js.map +1 -1
  12. package/dist/bar-chart/chart-bars.d.ts +50 -70
  13. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  14. package/dist/bar-chart/chart-bars.js +155 -210
  15. package/dist/bar-chart/chart-bars.js.map +1 -1
  16. package/dist/bar-chart/index.d.ts +2 -2
  17. package/dist/bar-chart/index.d.ts.map +1 -1
  18. package/dist/bar-chart/index.js +1 -1
  19. package/dist/bar-chart/index.js.map +1 -1
  20. package/dist/bar-chart/shared.d.ts +7 -7
  21. package/dist/bar-chart/shared.d.ts.map +1 -1
  22. package/dist/bar-chart/shared.js +7 -7
  23. package/dist/bar-chart/shared.js.map +1 -1
  24. package/dist/browser/chunks/chunk-TJNHLQGV.js +2 -0
  25. package/dist/browser/chunks/chunk-TJNHLQGV.js.map +7 -0
  26. package/dist/browser/index.js +1 -1
  27. package/dist/browser/index.js.map +3 -3
  28. package/dist/browser/kit.js +1 -1
  29. package/dist/browser/manifest.json +4 -3
  30. package/dist/engine.d.ts +10 -8
  31. package/dist/engine.d.ts.map +1 -1
  32. package/dist/engine.js +10 -8
  33. package/dist/engine.js.map +1 -1
  34. package/dist/kit/cartesian.d.ts +44 -0
  35. package/dist/kit/cartesian.d.ts.map +1 -0
  36. package/dist/kit/cartesian.js +59 -0
  37. package/dist/kit/cartesian.js.map +1 -0
  38. package/dist/kit/chart-border.d.ts +10 -11
  39. package/dist/kit/chart-border.d.ts.map +1 -1
  40. package/dist/kit/chart-border.js +15 -32
  41. package/dist/kit/chart-border.js.map +1 -1
  42. package/dist/kit/chart-canvas.d.ts +32 -21
  43. package/dist/kit/chart-canvas.d.ts.map +1 -1
  44. package/dist/kit/chart-canvas.js +33 -90
  45. package/dist/kit/chart-canvas.js.map +1 -1
  46. package/dist/kit/chart-legend.d.ts +123 -85
  47. package/dist/kit/chart-legend.d.ts.map +1 -1
  48. package/dist/kit/chart-legend.js +137 -186
  49. package/dist/kit/chart-legend.js.map +1 -1
  50. package/dist/kit/entries.d.ts +17 -0
  51. package/dist/kit/entries.d.ts.map +1 -0
  52. package/dist/kit/entries.js +19 -0
  53. package/dist/kit/entries.js.map +1 -0
  54. package/dist/kit/motion.d.ts +48 -0
  55. package/dist/kit/motion.d.ts.map +1 -0
  56. package/dist/kit/motion.js +63 -0
  57. package/dist/kit/motion.js.map +1 -0
  58. package/dist/kit/paints.d.ts +10 -0
  59. package/dist/kit/paints.d.ts.map +1 -0
  60. package/dist/kit/paints.js +12 -0
  61. package/dist/kit/paints.js.map +1 -0
  62. package/dist/kit/shared.d.ts +61 -10
  63. package/dist/kit/shared.d.ts.map +1 -1
  64. package/dist/kit/shared.js +108 -13
  65. package/dist/kit/shared.js.map +1 -1
  66. package/dist/kit/vertical-axis.d.ts +48 -61
  67. package/dist/kit/vertical-axis.d.ts.map +1 -1
  68. package/dist/kit/vertical-axis.js +97 -132
  69. package/dist/kit/vertical-axis.js.map +1 -1
  70. package/dist/line-chart/chart-line.d.ts +60 -77
  71. package/dist/line-chart/chart-line.d.ts.map +1 -1
  72. package/dist/line-chart/chart-line.js +100 -121
  73. package/dist/line-chart/chart-line.js.map +1 -1
  74. package/dist/line-chart/chart-region.d.ts +32 -33
  75. package/dist/line-chart/chart-region.d.ts.map +1 -1
  76. package/dist/line-chart/chart-region.js +31 -65
  77. package/dist/line-chart/chart-region.js.map +1 -1
  78. package/dist/line-chart/horizontal-axis.d.ts +58 -64
  79. package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
  80. package/dist/line-chart/horizontal-axis.js +94 -123
  81. package/dist/line-chart/horizontal-axis.js.map +1 -1
  82. package/dist/line-chart/index.d.ts +3 -3
  83. package/dist/line-chart/index.d.ts.map +1 -1
  84. package/dist/line-chart/index.js +2 -2
  85. package/dist/line-chart/index.js.map +1 -1
  86. package/dist/line-chart/line-chart.d.ts +156 -303
  87. package/dist/line-chart/line-chart.d.ts.map +1 -1
  88. package/dist/line-chart/line-chart.js +410 -638
  89. package/dist/line-chart/line-chart.js.map +1 -1
  90. package/dist/line-chart/shared.d.ts +7 -7
  91. package/dist/line-chart/shared.d.ts.map +1 -1
  92. package/dist/line-chart/shared.js +23 -8
  93. package/dist/line-chart/shared.js.map +1 -1
  94. package/dist/line-chart/x-scale.d.ts +4 -11
  95. package/dist/line-chart/x-scale.d.ts.map +1 -1
  96. package/dist/line-chart/x-scale.js +16 -5
  97. package/dist/line-chart/x-scale.js.map +1 -1
  98. package/dist/nodes.d.ts +6 -9
  99. package/dist/nodes.d.ts.map +1 -1
  100. package/dist/nodes.js +7 -9
  101. package/dist/nodes.js.map +1 -1
  102. package/dist/pie-chart/index.d.ts +2 -2
  103. package/dist/pie-chart/index.d.ts.map +1 -1
  104. package/dist/pie-chart/index.js +1 -1
  105. package/dist/pie-chart/index.js.map +1 -1
  106. package/dist/pie-chart/pie-chart.d.ts +108 -126
  107. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  108. package/dist/pie-chart/pie-chart.js +202 -271
  109. package/dist/pie-chart/pie-chart.js.map +1 -1
  110. package/dist/pie-chart/pie-legend.d.ts +84 -31
  111. package/dist/pie-chart/pie-legend.d.ts.map +1 -1
  112. package/dist/pie-chart/pie-legend.js +93 -107
  113. package/dist/pie-chart/pie-legend.js.map +1 -1
  114. package/dist/pie-chart/pie-ring.d.ts +160 -122
  115. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  116. package/dist/pie-chart/pie-ring.js +333 -361
  117. package/dist/pie-chart/pie-ring.js.map +1 -1
  118. package/dist/pie-chart/shared.d.ts +12 -6
  119. package/dist/pie-chart/shared.d.ts.map +1 -1
  120. package/dist/pie-chart/shared.js +14 -8
  121. package/dist/pie-chart/shared.js.map +1 -1
  122. package/package.json +7 -4
  123. package/src/bar-chart/bar-chart.ts +424 -686
  124. package/src/bar-chart/category-axis.ts +96 -111
  125. package/src/bar-chart/chart-bars.ts +178 -201
  126. package/src/bar-chart/index.ts +2 -2
  127. package/src/bar-chart/shared.ts +7 -7
  128. package/src/engine.ts +10 -8
  129. package/src/kit/cartesian.ts +81 -0
  130. package/src/kit/chart-border.ts +23 -34
  131. package/src/kit/chart-canvas.ts +45 -80
  132. package/src/kit/chart-legend.ts +185 -206
  133. package/src/kit/entries.ts +20 -0
  134. package/src/kit/motion.ts +90 -0
  135. package/src/kit/paints.ts +15 -0
  136. package/src/kit/shared.ts +120 -13
  137. package/src/kit/vertical-axis.ts +111 -141
  138. package/src/line-chart/chart-line.ts +103 -155
  139. package/src/line-chart/chart-region.ts +36 -81
  140. package/src/line-chart/horizontal-axis.ts +102 -153
  141. package/src/line-chart/index.ts +3 -3
  142. package/src/line-chart/line-chart.ts +463 -759
  143. package/src/line-chart/shared.ts +22 -8
  144. package/src/line-chart/x-scale.ts +20 -5
  145. package/src/nodes.ts +8 -10
  146. package/src/pie-chart/index.ts +2 -2
  147. package/src/pie-chart/pie-chart.ts +225 -303
  148. package/src/pie-chart/pie-legend.ts +125 -115
  149. package/src/pie-chart/pie-ring.ts +388 -443
  150. package/src/pie-chart/shared.ts +14 -8
  151. package/dist/browser/chunks/chunk-ZMBOBHYL.js +0 -2
  152. package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +0 -7
@@ -1,21 +1,19 @@
1
1
  import {
2
- type AssetScope,
3
2
  Graphics2D,
4
- Node2D,
5
- command,
6
- easeOut,
3
+ Types,
4
+ component2d,
7
5
  strokeOps,
8
- property,
9
- type Command,
10
- type CommandArgs,
11
- type EasingFunction,
12
- type Node2DProps,
13
- type RenderContext2D,
6
+ type NodeOf,
7
+ // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
8
+ type Node2D as _Node2D,
14
9
  type Stroke,
15
10
  type StrokeResolved,
16
11
  type Vector2,
17
- } from "@motionscript/core"
12
+ } from "@motionscript/sdk"
18
13
 
14
+ import { fresh } from "../kit/entries"
15
+ import { frameEase } from "../kit/motion"
16
+ import { loadPaints } from "../kit/paints"
19
17
  import { SPOTLIGHT_DIM } from "../kit/shared"
20
18
  import {
21
19
  EMPTY_REGION_BOX,
@@ -23,40 +21,42 @@ import {
23
21
  hasRegion,
24
22
  type RegionBox,
25
23
  } from "./shared"
26
- import { declarePaints } from "@motionscript/core/component"
27
24
 
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
25
+ /**
26
+ * The prefix of `pts` revealed at fraction `p ∈ [0, 1]`, measuring the reveal
27
+ * by *cumulative arc length* so the line traces at a steady pace regardless of
28
+ * uneven point spacing. Returns the whole-point prefix plus one interpolated
29
+ * point partway along the active segment.
30
+ */
31
+ function tracePoints(pts: Vector2[], p: number): Vector2[] {
32
+ if (p >= 1 || pts.length < 2) return pts
33
+ if (p <= 0) return []
34
+
35
+ const segLen: number[] = []
36
+ let total = 0
37
+ for (let i = 1; i < pts.length; i++) {
38
+ const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
39
+ segLen.push(d)
40
+ total += d
41
+ }
42
+ if (total === 0) return pts
43
+
44
+ let target = total * p
45
+ const out: Vector2[] = [pts[0]]
46
+ for (let i = 0; i < segLen.length; i++) {
47
+ if (target >= segLen[i]) {
48
+ out.push(pts[i + 1])
49
+ target -= segLen[i]
50
+ } else {
51
+ const t = segLen[i] === 0 ? 0 : target / segLen[i]
52
+ out.push({
53
+ x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
54
+ y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
55
+ })
56
+ break
57
+ }
58
+ }
59
+ return out
60
60
  }
61
61
 
62
62
  /**
@@ -64,41 +64,69 @@ export interface ChartLineProps extends Node2DProps {
64
64
  * through pre-mapped pixel `points`, drawn as one {@link Graphics2D} command list.
65
65
  * Sized and positioned by the parent, which shares the plot's coordinate frame.
66
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.
67
+ * Two signals drive it: `progress` traces the line in
68
+ * (animated by `enter`) and `enabled` shows or hides it wholesale.
69
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
70
+ export const ChartLine = component2d({
71
+ key: "chartLine",
72
+ state: {
73
+ /** The series polyline, already mapped into this node's local pixel space (origin at centre, y-up) by the parent {@link LineChart}. Ordered along x. */
74
+ points: fresh<Vector2[]>(() => []),
75
+ /** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
76
+ stroke: Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
77
+ /**
78
+ * Reveal of the line in `[0, 1]`: 0 hides it, 1 traces it fully from its left
79
+ * end to its right. Defaults to 1, so the paused editor canvas paints the line;
80
+ * `enter` drives it from 0 regardless of where it rests.
81
+ */
82
+ progress: 1,
83
+ /** Whether this line is drawn at all: the switch the chart flips alongside its legend's per-series toggle. */
84
+ enabled: true,
85
+ /** A slice of the plot to spotlight, in this node's own pixel space: outside it the line drops to `dimOpacity`. */
86
+ spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
87
+ /** Opacity the out-of-region stretches fade to. */
88
+ dimOpacity: SPOTLIGHT_DIM,
89
+ },
90
+ load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
91
+ commands: {
92
+ /**
93
+ * Trace the line from `from` to `to` over the command — by default in from
94
+ * its left end to its right. `showSeries` and `hideSeries` are the same
95
+ * motion in opposite directions, so both are this with their ends stated.
96
+ *
97
+ * `enabled` rides along in the same value rather than being set before or
98
+ * after the command: a line is drawable for every `t` the trace covers and
99
+ * undrawn outside it, which is what makes arriving at a time backwards show
100
+ * what arriving forwards does.
101
+ */
102
+ enter: {
103
+ args: { from: Types.number(0, { min: 0, max: 1 }), to: Types.number(1, { min: 0, max: 1 }) },
104
+ run: (timeline) => {
105
+ const { from, to } = timeline.args
106
+ const ease = frameEase(timeline)
107
+ return timeline.animate((t) => {
108
+ const progress = from + (to - from) * ease(t)
109
+ // Hidden only once it has fully retraced, so the last frame of a hide is
110
+ // the empty plot rather than a line that vanished a frame early.
111
+ return { progress, enabled: progress > 0 }
112
+ })
113
+ },
114
+ },
115
+ },
116
+ render: (ctx, state) => {
117
+ if (!state.enabled) return
118
+ const pts = state.points
91
119
  if (pts.length < 2) return
92
120
 
93
- const p = Math.max(0, Math.min(1, this.progress))
121
+ const p = Math.max(0, Math.min(1, state.progress))
94
122
  if (p <= 0) return
95
123
 
96
- const drawn = this.tracePoints(pts, p)
124
+ const drawn = tracePoints(pts, p)
97
125
  if (drawn.length < 2) return
98
- const stroke = this.stroke as StrokeResolved[]
126
+ const stroke = state.stroke
99
127
  const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
100
128
 
101
- const box = this.spotlight
129
+ const box = state.spotlight
102
130
  if (!hasRegion(box)) {
103
131
  ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
104
132
  return
@@ -113,7 +141,7 @@ export class ChartLine extends Node2D<ChartLineProps> {
113
141
  new Graphics2D()
114
142
  .line({ points: drawn, radius })
115
143
  .stroke(stroke)
116
- .opacity(Math.max(0, Math.min(1, this.dimOpacity)))
144
+ .opacity(Math.max(0, Math.min(1, state.dimOpacity)))
117
145
  )
118
146
 
119
147
  // One `draw` per run: shapes chained before a `stroke()` are combined into a
@@ -122,87 +150,7 @@ export class ChartLine extends Node2D<ChartLineProps> {
122
150
  for (const run of clipPolylineToBox(drawn, box)) {
123
151
  ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
124
152
  }
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
- }
153
+ },
154
+ })
163
155
 
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
- }
156
+ export type ChartLine = NodeOf<typeof ChartLine>
@@ -1,50 +1,32 @@
1
1
  import {
2
- type AssetScope,
3
2
  Graphics2D,
4
- Node2D,
3
+ Types,
4
+ component2d,
5
5
  fillOps,
6
6
  strokeOps,
7
- property,
8
7
  type Fill,
9
8
  type FillResolved,
10
- type Node2DProps,
11
- type RenderContext2D,
9
+ type NodeOf,
10
+ // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
11
+ type Node2D as _Node2D,
12
12
  type Stroke,
13
13
  type StrokeResolved,
14
- } from "@motionscript/core"
14
+ } from "@motionscript/sdk"
15
15
 
16
+ import { fresh } from "../kit/entries"
17
+ import { loadPaints } from "../kit/paints"
16
18
  import {
17
19
  DEFAULT_CHART_THEME,
18
20
  EMPTY_REGION_BOX,
19
21
  hasRegion,
20
22
  type RegionBox,
21
23
  } from "./shared"
22
- import { declarePaints } from "@motionscript/core/component"
23
-
24
- export interface ChartRegionProps extends Node2DProps {
25
- /**
26
- * The highlighted slice, already resolved into this node's local pixel space
27
- * (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box
28
- * draws nothing.
29
- */
30
- box: RegionBox
31
- /** Dashed rule marking each *bounded* edge of the box. */
32
- stroke: Stroke
33
- /** Wash filling the box, under the series lines. */
34
- fill: Fill
35
- /**
36
- * Reveal of the annotation in `[0, 1]`: 0 hides it, 1 draws it fully. Driven
37
- * by the chart's own `regionReveal`, so the rules and the series' dimming move
38
- * as one.
39
- */
40
- reveal: number
41
- }
42
24
 
43
25
  /**
44
26
  * The **region call-out** of a {@link LineChart}: a washed rectangle over the
45
27
  * plot with a dashed rule down each edge the author actually bounded. Sized and
46
- * positioned entirely from the pre-mapped {@link ChartRegionProps.box} rather
47
- * than its own `layoutBounds` — the box already carries the plot geometry.
28
+ * positioned entirely from the pre-mapped `box` rather
29
+ * than its own box — the region already carries the plot geometry.
48
30
  *
49
31
  * It draws only the *marking*; fading the series outside the region is
50
32
  * {@link ChartLine}'s job (it clips its own polyline against the same box), so
@@ -54,36 +36,24 @@ export interface ChartRegionProps extends Node2DProps {
54
36
  * plot height, so its top and bottom edges are the plot's own border, not a data
55
37
  * bound — drawing dashes there would assert a limit the author didn't set.
56
38
  */
57
- export class ChartRegion extends Node2D<ChartRegionProps> {
58
- @property({ default: () => EMPTY_REGION_BOX }) declare box: RegionBox
59
- @property({
60
- default: DEFAULT_CHART_THEME.region.stroke,
61
- mapper: strokeOps.resolve,
62
- tween: strokeOps.lerp,
63
- })
64
- declare stroke: Stroke
65
- @property({
66
- default: DEFAULT_CHART_THEME.region.fill,
67
- mapper: fillOps.resolve,
68
- tween: fillOps.lerp,
69
- })
70
- declare fill: Fill
71
- @property({ default: 0 }) declare reveal: number
72
-
73
- /**
74
- * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
75
- * paint props it carries — see {@link declarePaints}.
76
- */
77
- override declareAssets(assets: AssetScope): void {
78
- super.declareAssets(assets)
79
- declarePaints(this, assets)
80
- }
81
-
82
- protected renderSelf(ctx: RenderContext2D): void {
83
- const box = this.box
39
+ export const ChartRegion = component2d({
40
+ key: "chartRegion",
41
+ state: {
42
+ /** The highlighted slice, already resolved into this node's local pixel space (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box draws nothing. */
43
+ box: fresh<RegionBox>(() => EMPTY_REGION_BOX),
44
+ /** Dashed rule marking each *bounded* edge of the box. */
45
+ stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_CHART_THEME.region.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
46
+ /** Wash filling the box, under the series lines. */
47
+ fill: Types.custom<Fill, FillResolved[]>(DEFAULT_CHART_THEME.region.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
48
+ /** Reveal of the annotation in `[0, 1]`, driven by the chart's own `regionReveal` so the rules and the series' dimming move as one. */
49
+ reveal: 0,
50
+ },
51
+ load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.stroke] }),
52
+ render: (ctx, state) => {
53
+ const box = state.box
84
54
  if (!hasRegion(box)) return
85
55
 
86
- const r = Math.max(0, Math.min(1, this.reveal))
56
+ const r = Math.max(0, Math.min(1, state.reveal))
87
57
  if (r <= 0) return
88
58
 
89
59
  const width = box.right - box.left
@@ -92,44 +62,29 @@ export class ChartRegion extends Node2D<ChartRegionProps> {
92
62
  const cy = (box.bottom + box.top) / 2
93
63
 
94
64
  // Wash first, so the rules sit over their own tint.
95
- const layers = this.fill as FillResolved[]
96
- if (layers.length > 0) {
97
- ctx.draw(
98
- new Graphics2D()
99
- .rect({ x: cx, y: cy, width, height })
100
- .fill(this.fill)
101
- .opacity(r)
102
- )
65
+ if (state.fill.length > 0) {
66
+ ctx.draw(new Graphics2D().rect({ x: cx, y: cy, width, height }).fill(state.fill).opacity(r))
103
67
  }
104
68
 
105
69
  // One rule per bounded edge, growing from the region's centre outward as
106
70
  // `reveal` climbs — the same "trace it in" beat as the series lines.
107
- const stroke = this.stroke as StrokeResolved[]
108
- if (stroke.length === 0) return
71
+ if (state.stroke.length === 0) return
109
72
 
110
73
  // One `draw` per rule rather than four line ops under a single paint:
111
74
  // shapes chained before a `stroke()` are combined into one surface, and
112
75
  // stroking a union of disjoint open paths doesn't paint.
113
76
  const rule = (points: { x: number; y: number }[]) =>
114
- ctx.draw(new Graphics2D().line({ points }).stroke(this.stroke).opacity(r))
77
+ ctx.draw(new Graphics2D().line({ points }).stroke(state.stroke).opacity(r))
115
78
 
116
79
  if (box.xBounded) {
117
80
  const half = (height / 2) * r
118
- for (const x of [box.left, box.right]) {
119
- rule([
120
- { x, y: cy - half },
121
- { x, y: cy + half },
122
- ])
123
- }
81
+ for (const x of [box.left, box.right]) rule([{ x, y: cy - half }, { x, y: cy + half }])
124
82
  }
125
83
  if (box.yBounded) {
126
84
  const half = (width / 2) * r
127
- for (const y of [box.bottom, box.top]) {
128
- rule([
129
- { x: cx - half, y },
130
- { x: cx + half, y },
131
- ])
132
- }
85
+ for (const y of [box.bottom, box.top]) rule([{ x: cx - half, y }, { x: cx + half, y }])
133
86
  }
134
- }
135
- }
87
+ },
88
+ })
89
+
90
+ export type ChartRegion = NodeOf<typeof ChartRegion>