@motionscript/charts 0.1.0 → 0.1.1-alpha.1

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 (215) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +98 -28
  3. package/dist/bar-chart/bar-chart.d.ts +242 -119
  4. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  5. package/dist/bar-chart/bar-chart.js +231 -260
  6. package/dist/bar-chart/bar-chart.js.map +1 -1
  7. package/dist/bar-chart/category-axis.d.ts +16 -38
  8. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  9. package/dist/bar-chart/category-axis.js +59 -68
  10. package/dist/bar-chart/category-axis.js.map +1 -1
  11. package/dist/bar-chart/chart-bars.d.ts +60 -91
  12. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  13. package/dist/bar-chart/chart-bars.js +88 -125
  14. package/dist/bar-chart/chart-bars.js.map +1 -1
  15. package/dist/bar-chart/index.d.ts +5 -13
  16. package/dist/bar-chart/index.d.ts.map +1 -1
  17. package/dist/bar-chart/index.js +2 -9
  18. package/dist/bar-chart/index.js.map +1 -1
  19. package/dist/bar-chart/shared.d.ts +40 -132
  20. package/dist/bar-chart/shared.d.ts.map +1 -1
  21. package/dist/bar-chart/shared.js +70 -158
  22. package/dist/bar-chart/shared.js.map +1 -1
  23. package/dist/browser/chunks/chunk-R3Q5EE6E.js +2 -0
  24. package/dist/browser/chunks/chunk-R3Q5EE6E.js.map +7 -0
  25. package/dist/browser/index.js +1 -1
  26. package/dist/browser/index.js.map +4 -4
  27. package/dist/browser/kit.js +1 -1
  28. package/dist/browser/manifest.json +1 -1
  29. package/dist/engine.d.ts +8 -17
  30. package/dist/engine.d.ts.map +1 -1
  31. package/dist/engine.js +8 -17
  32. package/dist/engine.js.map +1 -1
  33. package/dist/index.d.ts +2 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +2 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/kit/cartesian.d.ts +49 -35
  38. package/dist/kit/cartesian.d.ts.map +1 -1
  39. package/dist/kit/cartesian.js +80 -42
  40. package/dist/kit/cartesian.js.map +1 -1
  41. package/dist/kit/chart-canvas.d.ts +37 -40
  42. package/dist/kit/chart-canvas.d.ts.map +1 -1
  43. package/dist/kit/chart-canvas.js +53 -47
  44. package/dist/kit/chart-canvas.js.map +1 -1
  45. package/dist/kit/chart-legend.d.ts +16 -59
  46. package/dist/kit/chart-legend.d.ts.map +1 -1
  47. package/dist/kit/chart-legend.js +38 -76
  48. package/dist/kit/chart-legend.js.map +1 -1
  49. package/dist/kit/entries.d.ts +2 -0
  50. package/dist/kit/entries.d.ts.map +1 -1
  51. package/dist/kit/entries.js +2 -0
  52. package/dist/kit/entries.js.map +1 -1
  53. package/dist/kit/horizontal-axis.d.ts +77 -0
  54. package/dist/kit/horizontal-axis.d.ts.map +1 -0
  55. package/dist/kit/horizontal-axis.js +99 -0
  56. package/dist/kit/horizontal-axis.js.map +1 -0
  57. package/dist/kit/motion.d.ts +16 -3
  58. package/dist/kit/motion.d.ts.map +1 -1
  59. package/dist/kit/motion.js +15 -1
  60. package/dist/kit/motion.js.map +1 -1
  61. package/dist/{pie-chart → kit}/pie-legend.d.ts +14 -29
  62. package/dist/kit/pie-legend.d.ts.map +1 -0
  63. package/dist/kit/pie-legend.js +93 -0
  64. package/dist/kit/pie-legend.js.map +1 -0
  65. package/dist/kit/polar.d.ts +31 -0
  66. package/dist/kit/polar.d.ts.map +1 -0
  67. package/dist/kit/polar.js +33 -0
  68. package/dist/kit/polar.js.map +1 -0
  69. package/dist/kit/shared.d.ts +60 -214
  70. package/dist/kit/shared.d.ts.map +1 -1
  71. package/dist/kit/shared.js +91 -291
  72. package/dist/kit/shared.js.map +1 -1
  73. package/dist/kit/style.d.ts +254 -0
  74. package/dist/kit/style.d.ts.map +1 -0
  75. package/dist/kit/style.js +197 -0
  76. package/dist/kit/style.js.map +1 -0
  77. package/dist/kit/vertical-axis.d.ts +36 -45
  78. package/dist/kit/vertical-axis.d.ts.map +1 -1
  79. package/dist/kit/vertical-axis.js +69 -81
  80. package/dist/kit/vertical-axis.js.map +1 -1
  81. package/dist/line-chart/chart-line.d.ts +107 -40
  82. package/dist/line-chart/chart-line.d.ts.map +1 -1
  83. package/dist/line-chart/chart-line.js +172 -89
  84. package/dist/line-chart/chart-line.js.map +1 -1
  85. package/dist/line-chart/chart-region.d.ts +14 -27
  86. package/dist/line-chart/chart-region.d.ts.map +1 -1
  87. package/dist/line-chart/chart-region.js +13 -28
  88. package/dist/line-chart/chart-region.js.map +1 -1
  89. package/dist/line-chart/index.d.ts +5 -18
  90. package/dist/line-chart/index.d.ts.map +1 -1
  91. package/dist/line-chart/index.js +3 -16
  92. package/dist/line-chart/index.js.map +1 -1
  93. package/dist/line-chart/line-chart.d.ts +272 -119
  94. package/dist/line-chart/line-chart.d.ts.map +1 -1
  95. package/dist/line-chart/line-chart.js +309 -257
  96. package/dist/line-chart/line-chart.js.map +1 -1
  97. package/dist/line-chart/shared.d.ts +73 -130
  98. package/dist/line-chart/shared.d.ts.map +1 -1
  99. package/dist/line-chart/shared.js +195 -156
  100. package/dist/line-chart/shared.js.map +1 -1
  101. package/dist/line-chart/x-scale.d.ts +16 -55
  102. package/dist/line-chart/x-scale.d.ts.map +1 -1
  103. package/dist/line-chart/x-scale.js +44 -111
  104. package/dist/line-chart/x-scale.js.map +1 -1
  105. package/dist/nodes.d.ts +3 -13
  106. package/dist/nodes.d.ts.map +1 -1
  107. package/dist/nodes.js +9 -13
  108. package/dist/nodes.js.map +1 -1
  109. package/dist/pie-chart/index.d.ts +3 -15
  110. package/dist/pie-chart/index.d.ts.map +1 -1
  111. package/dist/pie-chart/index.js +1 -11
  112. package/dist/pie-chart/index.js.map +1 -1
  113. package/dist/pie-chart/pie-chart.d.ts +147 -85
  114. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  115. package/dist/pie-chart/pie-chart.js +65 -86
  116. package/dist/pie-chart/pie-chart.js.map +1 -1
  117. package/dist/pie-chart/pie-ring.d.ts +65 -196
  118. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  119. package/dist/pie-chart/pie-ring.js +154 -292
  120. package/dist/pie-chart/pie-ring.js.map +1 -1
  121. package/dist/pie-chart/shared.d.ts +78 -172
  122. package/dist/pie-chart/shared.d.ts.map +1 -1
  123. package/dist/pie-chart/shared.js +54 -177
  124. package/dist/pie-chart/shared.js.map +1 -1
  125. package/dist/radar-chart/index.d.ts +5 -0
  126. package/dist/radar-chart/index.d.ts.map +1 -0
  127. package/dist/radar-chart/index.js +5 -0
  128. package/dist/radar-chart/index.js.map +1 -0
  129. package/dist/radar-chart/radar-chart.d.ts +218 -0
  130. package/dist/radar-chart/radar-chart.d.ts.map +1 -0
  131. package/dist/radar-chart/radar-chart.js +224 -0
  132. package/dist/radar-chart/radar-chart.js.map +1 -0
  133. package/dist/radar-chart/radar-grid.d.ts +43 -0
  134. package/dist/radar-chart/radar-grid.d.ts.map +1 -0
  135. package/dist/radar-chart/radar-grid.js +61 -0
  136. package/dist/radar-chart/radar-grid.js.map +1 -0
  137. package/dist/radar-chart/radar-series.d.ts +59 -0
  138. package/dist/radar-chart/radar-series.d.ts.map +1 -0
  139. package/dist/radar-chart/radar-series.js +71 -0
  140. package/dist/radar-chart/radar-series.js.map +1 -0
  141. package/dist/radar-chart/shared.d.ts +73 -0
  142. package/dist/radar-chart/shared.d.ts.map +1 -0
  143. package/dist/radar-chart/shared.js +123 -0
  144. package/dist/radar-chart/shared.js.map +1 -0
  145. package/dist/radial-chart/index.d.ts +6 -0
  146. package/dist/radial-chart/index.d.ts.map +1 -0
  147. package/dist/radial-chart/index.js +4 -0
  148. package/dist/radial-chart/index.js.map +1 -0
  149. package/dist/radial-chart/radial-chart.d.ts +216 -0
  150. package/dist/radial-chart/radial-chart.d.ts.map +1 -0
  151. package/dist/radial-chart/radial-chart.js +174 -0
  152. package/dist/radial-chart/radial-chart.js.map +1 -0
  153. package/dist/radial-chart/radial-rings.d.ts +121 -0
  154. package/dist/radial-chart/radial-rings.d.ts.map +1 -0
  155. package/dist/radial-chart/radial-rings.js +273 -0
  156. package/dist/radial-chart/radial-rings.js.map +1 -0
  157. package/dist/radial-chart/shared.d.ts +73 -0
  158. package/dist/radial-chart/shared.d.ts.map +1 -0
  159. package/dist/radial-chart/shared.js +79 -0
  160. package/dist/radial-chart/shared.js.map +1 -0
  161. package/package.json +4 -4
  162. package/registry.json +21 -6
  163. package/src/bar-chart/bar-chart.ts +274 -315
  164. package/src/bar-chart/category-axis.ts +53 -88
  165. package/src/bar-chart/chart-bars.ts +108 -205
  166. package/src/bar-chart/index.ts +8 -19
  167. package/src/bar-chart/shared.ts +81 -309
  168. package/src/engine.ts +8 -17
  169. package/src/index.ts +2 -0
  170. package/src/kit/cartesian.ts +118 -55
  171. package/src/kit/chart-canvas.ts +50 -80
  172. package/src/kit/chart-legend.ts +36 -97
  173. package/src/kit/entries.ts +4 -1
  174. package/src/kit/horizontal-axis.ts +114 -0
  175. package/src/kit/motion.ts +18 -2
  176. package/src/{pie-chart → kit}/pie-legend.ts +25 -65
  177. package/src/kit/polar.ts +54 -0
  178. package/src/kit/shared.ts +104 -487
  179. package/src/kit/style.ts +257 -0
  180. package/src/kit/vertical-axis.ts +81 -115
  181. package/src/line-chart/chart-line.ts +185 -91
  182. package/src/line-chart/chart-region.ts +14 -31
  183. package/src/line-chart/index.ts +14 -22
  184. package/src/line-chart/line-chart.ts +377 -315
  185. package/src/line-chart/shared.ts +229 -304
  186. package/src/line-chart/x-scale.ts +44 -121
  187. package/src/nodes.ts +9 -13
  188. package/src/pie-chart/index.ts +3 -35
  189. package/src/pie-chart/pie-chart.ts +83 -134
  190. package/src/pie-chart/pie-ring.ts +162 -368
  191. package/src/pie-chart/shared.ts +92 -379
  192. package/src/radar-chart/index.ts +4 -0
  193. package/src/radar-chart/radar-chart.ts +287 -0
  194. package/src/radar-chart/radar-grid.ts +78 -0
  195. package/src/radar-chart/radar-series.ts +88 -0
  196. package/src/radar-chart/shared.ts +146 -0
  197. package/src/radial-chart/index.ts +5 -0
  198. package/src/radial-chart/radial-chart.ts +210 -0
  199. package/src/radial-chart/radial-rings.ts +328 -0
  200. package/src/radial-chart/shared.ts +114 -0
  201. package/dist/browser/chunks/chunk-TJNHLQGV.js +0 -2
  202. package/dist/browser/chunks/chunk-TJNHLQGV.js.map +0 -7
  203. package/dist/kit/chart-border.d.ts +0 -27
  204. package/dist/kit/chart-border.d.ts.map +0 -1
  205. package/dist/kit/chart-border.js +0 -28
  206. package/dist/kit/chart-border.js.map +0 -1
  207. package/dist/line-chart/horizontal-axis.d.ts +0 -79
  208. package/dist/line-chart/horizontal-axis.d.ts.map +0 -1
  209. package/dist/line-chart/horizontal-axis.js +0 -129
  210. package/dist/line-chart/horizontal-axis.js.map +0 -1
  211. package/dist/pie-chart/pie-legend.d.ts.map +0 -1
  212. package/dist/pie-chart/pie-legend.js +0 -117
  213. package/dist/pie-chart/pie-legend.js.map +0 -1
  214. package/src/kit/chart-border.ts +0 -50
  215. package/src/line-chart/horizontal-axis.ts +0 -151
@@ -1,103 +1,98 @@
1
1
  import {
2
2
  Graphics2D,
3
3
  Types,
4
+ clamp,
4
5
  component2d,
6
+ fillOps,
5
7
  strokeOps,
8
+ type Fill,
9
+ type FillResolved,
6
10
  type NodeOf,
7
- // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
11
+ // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
8
12
  type Node2D as _Node2D,
13
+ type Node2DProps as _Node2DProps,
14
+ type PathCommand,
15
+ type RenderContext,
9
16
  type Stroke,
10
17
  type StrokeResolved,
11
18
  type Vector2,
12
19
  } from "@motionscript/sdk"
13
20
 
21
+ import { fade } from "../kit/cartesian"
14
22
  import { fresh } from "../kit/entries"
15
23
  import { frameEase } from "../kit/motion"
16
24
  import { loadPaints } from "../kit/paints"
17
25
  import { SPOTLIGHT_DIM } from "../kit/shared"
18
26
  import {
19
27
  EMPTY_REGION_BOX,
28
+ clipPolygonToBox,
20
29
  clipPolylineToBox,
30
+ cutAt,
31
+ curvePoints,
21
32
  hasRegion,
33
+ type LineCurve,
22
34
  type RegionBox,
23
35
  } from "./shared"
24
36
 
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
37
+ /** The x a trace at `progress` has reached, across `points` ordered along x. */
38
+ export function traceX(points: readonly Vector2[], progress: number): number {
39
+ if (points.length === 0) return 0
40
+ const first = points[0].x
41
+ const last = points[points.length - 1].x
42
+ return progress >= 1 ? Infinity : first + (last - first) * progress
43
+ }
44
+
45
+ /** The drawn path at `progress`: the whole curve once traced, else the part left of the trace. */
46
+ function traced(points: readonly Vector2[], curve: LineCurve, progress: number): Vector2[] {
47
+ const path = curvePoints(points, curve)
48
+ return progress >= 1 ? path : cutAt(path, traceX(points, progress))
49
+ }
50
+
51
+ const curveEntry = () => Types.custom<LineCurve>("smooth")
52
+
53
+ /** A closed outline as path commands. */
54
+ function outlineCommands(points: readonly Vector2[]): PathCommand[] {
55
+ return [...points.map((p, i): PathCommand => ({ type: i === 0 ? "M" : "L", x: p.x, y: p.y })), { type: "Z" }]
56
+ }
57
+
58
+ function fillOutline(ctx: RenderContext, outline: readonly Vector2[], fill: FillResolved[], opacity: number): void {
59
+ if (outline.length < 3 || opacity <= 0) return
60
+ const { width, height } = ctx.size
61
+ // A frame symmetric about the origin, so the outline draws where its points say rather than re-centred on itself.
62
+ const centerBounds: [number, number, number, number] = [-width / 2, -height / 2, width / 2, height / 2]
63
+ ctx.draw(new Graphics2D().path({ data: outlineCommands(outline), centerBounds }).fill(fill).opacity(opacity))
60
64
  }
61
65
 
62
66
  /**
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: `progress` traces the line in
68
- * (animated by `enter`) and `enabled` shows or hides it wholesale.
67
+ * One series of a {@link LineChart}: its line through pre-mapped pixel `points`,
68
+ * shaped by `curve`, and a dot on each point. `progress` traces it in from the
69
+ * left; `enabled` shows or hides it wholesale.
69
70
  */
70
71
  export const ChartLine = component2d({
71
72
  key: "chartLine",
72
73
  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
+ /** In this node's pixel space (origin at centre, y-up), mapped by the parent and ordered along x. */
74
75
  points: fresh<Vector2[]>(() => []),
75
- /** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
76
+ curve: curveEntry(),
76
77
  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
- */
78
+ /** A dot of `dotSize` on each point, in `dotFill`; none at size 0. */
79
+ dotSize: 0,
80
+ dotFill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
81
+ /** Trace from the left end, `[0, 1]`. Rests at 1 so a paused editor canvas paints the line; `enter` drives it from 0. */
82
82
  progress: 1,
83
- /** Whether this line is drawn at all: the switch the chart flips alongside its legend's per-series toggle. */
83
+ /** Whether the line is drawn at all, flipped alongside its legend entry. */
84
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`. */
85
+ /** In this node's pixel space; outside it the line drops to `dimOpacity`. */
86
86
  spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
87
- /** Opacity the out-of-region stretches fade to. */
88
87
  dimOpacity: SPOTLIGHT_DIM,
89
88
  },
90
- load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
89
+ load: (loader, state) => loadPaints(loader, { fills: [state.dotFill], strokes: [state.stroke] }),
91
90
  commands: {
92
91
  /**
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.
92
+ * Trace the line from `from` to `to`; `showSeries`/`hideSeries` are this with
93
+ * the ends stated. `enabled` rides along in the same value, so the line is
94
+ * drawable for every `t` the trace covers and arriving backwards matches
95
+ * arriving forwards.
101
96
  */
102
97
  enter: {
103
98
  args: { from: Types.number(0, { min: 0, max: 1 }), to: Types.number(1, { min: 0, max: 1 }) },
@@ -106,51 +101,150 @@ export const ChartLine = component2d({
106
101
  const ease = frameEase(timeline)
107
102
  return timeline.animate((t) => {
108
103
  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.
104
+ // Hidden only once fully retraced, so a hide does not vanish a frame early.
111
105
  return { progress, enabled: progress > 0 }
112
106
  })
113
107
  },
114
108
  },
109
+ /** Hold the line fully traced while its chart moves it, switched on throughout a show and off once a hide lands. */
110
+ hold: {
111
+ args: { shown: Types.boolean(true) },
112
+ run: (timeline) => {
113
+ const { shown } = timeline.args
114
+ return timeline.animate((t) => ({ progress: 1, enabled: shown || t < 1 }), { eased: false })
115
+ },
116
+ },
115
117
  },
116
118
  render: (ctx, state) => {
117
119
  if (!state.enabled) return
118
120
  const pts = state.points
119
- if (pts.length < 2) return
121
+ const p = clamp(state.progress, 0, 1)
122
+ if (pts.length === 0 || p <= 0) return
120
123
 
121
- const p = Math.max(0, Math.min(1, state.progress))
122
- if (p <= 0) return
123
-
124
- const drawn = tracePoints(pts, p)
125
- if (drawn.length < 2) return
124
+ const box = state.spotlight
125
+ const lit = hasRegion(box)
126
+ const dim = clamp(state.dimOpacity, 0, 1)
126
127
  const stroke = state.stroke
127
- const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
128
+ const weight = stroke[0]?.weight ?? 0
129
+ const drawn = traced(pts, state.curve, p)
128
130
 
129
- const box = state.spotlight
130
- if (!hasRegion(box)) {
131
- ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
132
- return
131
+ if (drawn.length >= 2 && weight > 0) {
132
+ // Rounded corners only where the line has corners to round.
133
+ const radius = state.curve === "linear" ? weight * 2 : 0
134
+ ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke).opacity(lit ? dim : 1))
135
+ // One `draw` per run: shapes chained before `stroke()` combine into one surface, and stroking a union of disjoint open paths paints nothing.
136
+ if (lit) for (const run of clipPolylineToBox(drawn, box)) ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
133
137
  }
134
138
 
135
- // Spotlit: the whole line goes down to `dimOpacity`, then the stretches
136
- // inside the region are re-drawn over it at full strength. Two passes rather
137
- // than one dim + one bright *complementary* pair, so the bright run has
138
- // continuous geometry through the region and its joins round the way an
139
- // unspotlit line's do — and so the boundary lands exactly on the region edge.
140
- ctx.draw(
141
- new Graphics2D()
142
- .line({ points: drawn, radius })
143
- .stroke(stroke)
144
- .opacity(Math.max(0, Math.min(1, state.dimOpacity)))
145
- )
146
-
147
- // One `draw` per run: shapes chained before a `stroke()` are combined into a
148
- // single surface, and stroking a union of disjoint open paths doesn't paint
149
- // — so a line that leaves the region and comes back would lose both halves.
150
- for (const run of clipPolylineToBox(drawn, box)) {
151
- ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
139
+ const size = state.dotSize
140
+ if (size <= 0) return
141
+ const reach = traceX(pts, p)
142
+ const bright = new Graphics2D()
143
+ const dimmed = new Graphics2D()
144
+ let brightAny = false
145
+ let dimAny = false
146
+ for (const pt of pts) {
147
+ // A dot grows in as the trace passes it.
148
+ const grown = reach === Infinity ? 1 : clamp((reach - pt.x) / (size * 2), 0, 1)
149
+ if (grown <= 0) continue
150
+ const inside = !lit || (pt.x >= box.left && pt.x <= box.right && pt.y >= box.bottom && pt.y <= box.top)
151
+ ;(inside ? bright : dimmed).ellipse({ x: pt.x, y: pt.y, width: size * grown, height: size * grown })
152
+ if (inside) brightAny = true
153
+ else dimAny = true
152
154
  }
155
+ if (dimAny) ctx.draw(dimmed.fill(state.dotFill).opacity(dim))
156
+ if (brightAny) ctx.draw(bright.fill(state.dotFill))
153
157
  },
154
158
  })
155
159
 
156
160
  export type ChartLine = NodeOf<typeof ChartLine>
161
+
162
+ /**
163
+ * The fill under one series of a {@link LineChart}: down to `base` (the series
164
+ * below it in a stack) or, without one, to the `baseline`. Drawn by its own node
165
+ * so every area sits under every line; the chart binds its trace, switch and
166
+ * opacity to its line's, so the two move as one.
167
+ */
168
+ export const ChartArea = component2d({
169
+ key: "chartArea",
170
+ state: {
171
+ /** The series' points, as its line has them. */
172
+ points: fresh<Vector2[]>(() => []),
173
+ /** The line the area fills down to, at the same x positions; empty for the baseline. */
174
+ base: fresh<Vector2[]>(() => []),
175
+ /** Pixel y of the baseline. */
176
+ baseline: 0,
177
+ curve: curveEntry(),
178
+ area: Types.custom<"none" | "solid" | "gradient">("solid"),
179
+ /** The series colour; the area paints it at `areaOpacity`, a gradient fading it out towards the bottom. */
180
+ fill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
181
+ areaOpacity: 0.4,
182
+ progress: 1,
183
+ enabled: true,
184
+ spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
185
+ dimOpacity: SPOTLIGHT_DIM,
186
+ },
187
+ load: (loader, state) => loadPaints(loader, { fills: [state.fill] }),
188
+ render: (ctx, state) => {
189
+ const pts = state.points
190
+ const p = clamp(state.progress, 0, 1)
191
+ if (!state.enabled || state.area === "none" || pts.length < 2 || p <= 0 || state.fill.length === 0) return
192
+ const outlineAt = (progress: number) => {
193
+ const top = traced(pts, state.curve, progress)
194
+ if (top.length < 2) return []
195
+ const base = state.base.length === pts.length
196
+ ? traced(state.base, state.curve, progress)
197
+ : [{ x: top[0].x, y: state.baseline }, { x: top[top.length - 1].x, y: state.baseline }]
198
+ return [...top, ...[...base].reverse()]
199
+ }
200
+ // The gradient spans the whole area's height, so it holds still while the area traces in and across a spotlight's cut.
201
+ const span = verticalSpan(outlineAt(1))
202
+ const paint = (outline: readonly Vector2[], opacity: number) =>
203
+ fillOutline(ctx, outline, areaFill(state.fill, state.area, state.areaOpacity, span, verticalSpan(outline)), opacity)
204
+ const outline = outlineAt(p)
205
+ const box = state.spotlight
206
+ if (!hasRegion(box)) {
207
+ paint(outline, 1)
208
+ return
209
+ }
210
+ paint(outline, clamp(state.dimOpacity, 0, 1))
211
+ paint(clipPolygonToBox(outline, box), 1)
212
+ },
213
+ })
214
+
215
+ interface Span {
216
+ readonly top: number
217
+ readonly bottom: number
218
+ }
219
+
220
+ function verticalSpan(points: readonly Vector2[]): Span {
221
+ let top = -Infinity
222
+ let bottom = Infinity
223
+ for (const p of points) {
224
+ if (p.y > top) top = p.y
225
+ if (p.y < bottom) bottom = p.y
226
+ }
227
+ return { top, bottom }
228
+ }
229
+
230
+ /**
231
+ * The area's paint for an outline spanning `own`: the series colour at
232
+ * `opacity`, or for a gradient that colour fading to nearly nothing across
233
+ * `whole`. A gradient maps onto the bounds of the shape it fills, so its ends
234
+ * are placed in `own`'s unit box (y-up) at `whole`'s top and bottom.
235
+ */
236
+ export function areaFill(colour: FillResolved[], area: "none" | "solid" | "gradient", opacity: number, whole: Span, own: Span): FillResolved[] {
237
+ const top = colour[colour.length - 1]
238
+ if (area !== "gradient" || top?.type !== "solid" || !(own.top > own.bottom)) return fade(colour, opacity) as FillResolved[]
239
+ const [r, g, b, a] = top.color
240
+ const middle = (own.top + own.bottom) / 2
241
+ const half = (own.top - own.bottom) / 2
242
+ return fillOps.resolve({
243
+ type: "linear-gradient",
244
+ colors: [[r, g, b, a * opacity], [r, g, b, a * opacity * 0.04]],
245
+ start: { x: 0, y: (whole.top - middle) / half },
246
+ end: { x: 0, y: (whole.bottom - middle) / half },
247
+ })
248
+ }
249
+
250
+ export type ChartArea = NodeOf<typeof ChartArea>
@@ -7,45 +7,37 @@ import {
7
7
  type Fill,
8
8
  type FillResolved,
9
9
  type NodeOf,
10
- // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
10
+ // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
11
11
  type Node2D as _Node2D,
12
+ type Node2DProps as _Node2DProps,
12
13
  type Stroke,
13
14
  type StrokeResolved,
14
15
  } from "@motionscript/sdk"
15
16
 
17
+ import { rule } from "../kit/chart-canvas"
16
18
  import { fresh } from "../kit/entries"
17
19
  import { loadPaints } from "../kit/paints"
18
20
  import {
19
- DEFAULT_CHART_THEME,
20
21
  EMPTY_REGION_BOX,
21
22
  hasRegion,
22
23
  type RegionBox,
23
24
  } from "./shared"
24
25
 
25
26
  /**
26
- * The **region call-out** of a {@link LineChart}: a washed rectangle over the
27
- * plot with a dashed rule down each edge the author actually bounded. Sized and
28
- * positioned entirely from the pre-mapped `box` rather
29
- * than its own box — the region already carries the plot geometry.
30
- *
31
- * It draws only the *marking*; fading the series outside the region is
32
- * {@link ChartLine}'s job (it clips its own polyline against the same box), so
33
- * the two can't disagree about where the boundary is.
34
- *
35
- * Only bounded edges get a rule: a region given just an `x` range spans the full
36
- * plot height, so its top and bottom edges are the plot's own border, not a data
37
- * bound — drawing dashes there would assert a limit the author didn't set.
27
+ * The call-out of a {@link LineChart}: a washed rectangle with a dashed rule on
28
+ * each bounded edge, drawn from the pre-mapped `box`. Dimming the series outside
29
+ * is the line's and area's job, clipped against the same box so they agree on
30
+ * the boundary. An unbounded edge is the plot's own border, and a rule there
31
+ * would assert a limit the author did not set.
38
32
  */
39
33
  export const ChartRegion = component2d({
40
34
  key: "chartRegion",
41
35
  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. */
36
+ /** In this node's pixel space (origin at centre, y-up), mapped by the parent. A degenerate box draws nothing. */
43
37
  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. */
38
+ stroke: Types.custom<Stroke, StrokeResolved[]>({ weight: 2, fill: "white/70", dash: [14, 12], cap: "butt", align: "center" }, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
39
+ fill: Types.custom<Fill, FillResolved[]>("white/6", { resolve: fillOps.resolve, lerp: fillOps.lerp }),
40
+ /** Driven by the chart's `regionReveal`, so the rules and the series' dimming move as one. */
49
41
  reveal: 0,
50
42
  },
51
43
  load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.stroke] }),
@@ -61,28 +53,19 @@ export const ChartRegion = component2d({
61
53
  const cx = (box.left + box.right) / 2
62
54
  const cy = (box.bottom + box.top) / 2
63
55
 
64
- // Wash first, so the rules sit over their own tint.
65
56
  if (state.fill.length > 0) {
66
57
  ctx.draw(new Graphics2D().rect({ x: cx, y: cy, width, height }).fill(state.fill).opacity(r))
67
58
  }
68
59
 
69
- // One rule per bounded edge, growing from the region's centre outward as
70
- // `reveal` climbs — the same "trace it in" beat as the series lines.
71
60
  if (state.stroke.length === 0) return
72
61
 
73
- // One `draw` per rule rather than four line ops under a single paint:
74
- // shapes chained before a `stroke()` are combined into one surface, and
75
- // stroking a union of disjoint open paths doesn't paint.
76
- const rule = (points: { x: number; y: number }[]) =>
77
- ctx.draw(new Graphics2D().line({ points }).stroke(state.stroke).opacity(r))
78
-
79
62
  if (box.xBounded) {
80
63
  const half = (height / 2) * r
81
- for (const x of [box.left, box.right]) rule([{ x, y: cy - half }, { x, y: cy + half }])
64
+ for (const x of [box.left, box.right]) rule(ctx, { x, y: cy - half }, { x, y: cy + half }, state.stroke, r)
82
65
  }
83
66
  if (box.yBounded) {
84
67
  const half = (width / 2) * r
85
- for (const y of [box.bottom, box.top]) rule([{ x: cx - half, y }, { x: cx + half, y }])
68
+ for (const y of [box.bottom, box.top]) rule(ctx, { x: cx - half, y }, { x: cx + half, y }, state.stroke, r)
86
69
  }
87
70
  },
88
71
  })
@@ -1,39 +1,31 @@
1
- /**
2
- * The line chart: a cartesian plot with a traced polyline, spotlit regions and
3
- * a staggered entrance.
4
- *
5
- * {@link LineChart} composes the parts below plus the shared ones in the kit
6
- * (the plot panel, the value axis, the legend); each part is exported too, so
7
- * another node type can reuse one on its own.
8
- *
9
- * These files are what a copy of the line chart takes (`registry.json`). Everything they reach for
10
- * that is *not* here comes from the kit and stays a dependency — see
11
- * `../engine.ts` for why that line is where it is.
12
- */
13
1
  export { LineChart, lineChartDomains } from "./line-chart"
14
- export type { LineChartDomains, LineChartProps, LineChartValues } from "./line-chart"
2
+ export type { LineChartDomains, LineChartStyle, LineChartValues } from "./line-chart"
15
3
  export { ChartRegion } from "./chart-region"
16
- export { ChartLine } from "./chart-line"
17
- export { HorizontalAxis } from "./horizontal-axis"
4
+ export { ChartArea, ChartLine, areaFill, traceX } from "./chart-line"
18
5
  export {
19
- DEFAULT_CHART_THEME,
20
6
  EMPTY_REGION_BOX,
7
+ LINE_LABELS,
8
+ LINE_MARKS,
9
+ clipPolygonToBox,
21
10
  clipPolylineToBox,
11
+ curvePoints,
12
+ cutAt,
22
13
  hasRegion,
23
- lerpChartTheme,
24
- resolveChartTheme,
14
+
25
15
  resolveRegionBox,
26
16
  resolveYScale,
27
17
  seriesPoints,
18
+ stackExtent,
19
+ stackSeries,
28
20
  yExtent,
29
21
  } from "./shared"
30
22
  export type {
31
- ChartConfig,
32
- ChartTheme,
23
+ LineCurve,
33
24
  LineSeries,
25
+ LineStack,
34
26
  PlotRegion,
35
27
  RegionBox,
36
- RegionConfig,
28
+ StackedSeries,
37
29
  } from "./shared"
38
- export { formatInstant, parseInstant, resolveXScale } from "./x-scale"
30
+ export { EVERY_SAMPLE, formatInstant, parseInstant, resolveXScale } from "./x-scale"
39
31
  export type { XScale, XScaleKind } from "./x-scale"