@motionscript/charts 0.1.1-alpha.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 (207) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +98 -28
  3. package/dist/bar-chart/bar-chart.d.ts +233 -39
  4. package/dist/bar-chart/bar-chart.d.ts.map +1 -1
  5. package/dist/bar-chart/bar-chart.js +215 -171
  6. package/dist/bar-chart/bar-chart.js.map +1 -1
  7. package/dist/bar-chart/category-axis.d.ts +12 -7
  8. package/dist/bar-chart/category-axis.d.ts.map +1 -1
  9. package/dist/bar-chart/category-axis.js +56 -42
  10. package/dist/bar-chart/category-axis.js.map +1 -1
  11. package/dist/bar-chart/chart-bars.d.ts +52 -52
  12. package/dist/bar-chart/chart-bars.d.ts.map +1 -1
  13. package/dist/bar-chart/chart-bars.js +80 -89
  14. package/dist/bar-chart/chart-bars.js.map +1 -1
  15. package/dist/bar-chart/index.d.ts +4 -4
  16. package/dist/bar-chart/index.d.ts.map +1 -1
  17. package/dist/bar-chart/index.js +2 -2
  18. package/dist/bar-chart/index.js.map +1 -1
  19. package/dist/bar-chart/shared.d.ts +36 -65
  20. package/dist/bar-chart/shared.d.ts.map +1 -1
  21. package/dist/bar-chart/shared.js +68 -117
  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 +5 -2
  30. package/dist/engine.d.ts.map +1 -1
  31. package/dist/engine.js +5 -2
  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 -15
  38. package/dist/kit/cartesian.d.ts.map +1 -1
  39. package/dist/kit/cartesian.js +79 -29
  40. package/dist/kit/cartesian.js.map +1 -1
  41. package/dist/kit/chart-canvas.d.ts +32 -24
  42. package/dist/kit/chart-canvas.d.ts.map +1 -1
  43. package/dist/kit/chart-canvas.js +52 -46
  44. package/dist/kit/chart-canvas.js.map +1 -1
  45. package/dist/kit/chart-legend.d.ts +8 -4
  46. package/dist/kit/chart-legend.d.ts.map +1 -1
  47. package/dist/kit/chart-legend.js +30 -20
  48. package/dist/kit/chart-legend.js.map +1 -1
  49. package/dist/{line-chart → kit}/horizontal-axis.d.ts +31 -6
  50. package/dist/kit/horizontal-axis.d.ts.map +1 -0
  51. package/dist/{line-chart → kit}/horizontal-axis.js +23 -24
  52. package/dist/kit/horizontal-axis.js.map +1 -0
  53. package/dist/kit/motion.d.ts +5 -0
  54. package/dist/kit/motion.d.ts.map +1 -1
  55. package/dist/kit/motion.js +8 -0
  56. package/dist/kit/motion.js.map +1 -1
  57. package/dist/{pie-chart → kit}/pie-legend.d.ts +3 -3
  58. package/dist/kit/pie-legend.d.ts.map +1 -0
  59. package/dist/{pie-chart → kit}/pie-legend.js +11 -10
  60. package/dist/kit/pie-legend.js.map +1 -0
  61. package/dist/kit/polar.d.ts +31 -0
  62. package/dist/kit/polar.d.ts.map +1 -0
  63. package/dist/kit/polar.js +33 -0
  64. package/dist/kit/polar.js.map +1 -0
  65. package/dist/kit/shared.d.ts +11 -50
  66. package/dist/kit/shared.d.ts.map +1 -1
  67. package/dist/kit/shared.js +58 -141
  68. package/dist/kit/shared.js.map +1 -1
  69. package/dist/kit/style.d.ts +254 -0
  70. package/dist/kit/style.d.ts.map +1 -0
  71. package/dist/kit/style.js +197 -0
  72. package/dist/kit/style.js.map +1 -0
  73. package/dist/kit/vertical-axis.d.ts +22 -2
  74. package/dist/kit/vertical-axis.d.ts.map +1 -1
  75. package/dist/kit/vertical-axis.js +56 -43
  76. package/dist/kit/vertical-axis.js.map +1 -1
  77. package/dist/line-chart/chart-line.d.ts +95 -3
  78. package/dist/line-chart/chart-line.d.ts.map +1 -1
  79. package/dist/line-chart/chart-line.js +165 -53
  80. package/dist/line-chart/chart-line.js.map +1 -1
  81. package/dist/line-chart/chart-region.d.ts +1 -1
  82. package/dist/line-chart/chart-region.d.ts.map +1 -1
  83. package/dist/line-chart/chart-region.js +7 -8
  84. package/dist/line-chart/chart-region.js.map +1 -1
  85. package/dist/line-chart/index.d.ts +5 -6
  86. package/dist/line-chart/index.d.ts.map +1 -1
  87. package/dist/line-chart/index.js +3 -4
  88. package/dist/line-chart/index.js.map +1 -1
  89. package/dist/line-chart/line-chart.d.ts +257 -18
  90. package/dist/line-chart/line-chart.d.ts.map +1 -1
  91. package/dist/line-chart/line-chart.js +282 -134
  92. package/dist/line-chart/line-chart.js.map +1 -1
  93. package/dist/line-chart/shared.d.ts +51 -34
  94. package/dist/line-chart/shared.d.ts.map +1 -1
  95. package/dist/line-chart/shared.js +175 -83
  96. package/dist/line-chart/shared.js.map +1 -1
  97. package/dist/line-chart/x-scale.d.ts +2 -0
  98. package/dist/line-chart/x-scale.d.ts.map +1 -1
  99. package/dist/line-chart/x-scale.js +15 -0
  100. package/dist/line-chart/x-scale.js.map +1 -1
  101. package/dist/nodes.d.ts +3 -1
  102. package/dist/nodes.d.ts.map +1 -1
  103. package/dist/nodes.js +9 -1
  104. package/dist/nodes.js.map +1 -1
  105. package/dist/pie-chart/index.d.ts +2 -3
  106. package/dist/pie-chart/index.d.ts.map +1 -1
  107. package/dist/pie-chart/index.js +1 -2
  108. package/dist/pie-chart/index.js.map +1 -1
  109. package/dist/pie-chart/pie-chart.d.ts +114 -18
  110. package/dist/pie-chart/pie-chart.d.ts.map +1 -1
  111. package/dist/pie-chart/pie-chart.js +37 -24
  112. package/dist/pie-chart/pie-chart.js.map +1 -1
  113. package/dist/pie-chart/pie-ring.d.ts +16 -5
  114. package/dist/pie-chart/pie-ring.d.ts.map +1 -1
  115. package/dist/pie-chart/pie-ring.js +89 -55
  116. package/dist/pie-chart/pie-ring.js.map +1 -1
  117. package/dist/pie-chart/shared.d.ts +53 -61
  118. package/dist/pie-chart/shared.d.ts.map +1 -1
  119. package/dist/pie-chart/shared.js +41 -121
  120. package/dist/pie-chart/shared.js.map +1 -1
  121. package/dist/radar-chart/index.d.ts +5 -0
  122. package/dist/radar-chart/index.d.ts.map +1 -0
  123. package/dist/radar-chart/index.js +5 -0
  124. package/dist/radar-chart/index.js.map +1 -0
  125. package/dist/radar-chart/radar-chart.d.ts +218 -0
  126. package/dist/radar-chart/radar-chart.d.ts.map +1 -0
  127. package/dist/radar-chart/radar-chart.js +224 -0
  128. package/dist/radar-chart/radar-chart.js.map +1 -0
  129. package/dist/radar-chart/radar-grid.d.ts +43 -0
  130. package/dist/radar-chart/radar-grid.d.ts.map +1 -0
  131. package/dist/radar-chart/radar-grid.js +61 -0
  132. package/dist/radar-chart/radar-grid.js.map +1 -0
  133. package/dist/radar-chart/radar-series.d.ts +59 -0
  134. package/dist/radar-chart/radar-series.d.ts.map +1 -0
  135. package/dist/radar-chart/radar-series.js +71 -0
  136. package/dist/radar-chart/radar-series.js.map +1 -0
  137. package/dist/radar-chart/shared.d.ts +73 -0
  138. package/dist/radar-chart/shared.d.ts.map +1 -0
  139. package/dist/radar-chart/shared.js +123 -0
  140. package/dist/radar-chart/shared.js.map +1 -0
  141. package/dist/radial-chart/index.d.ts +6 -0
  142. package/dist/radial-chart/index.d.ts.map +1 -0
  143. package/dist/radial-chart/index.js +4 -0
  144. package/dist/radial-chart/index.js.map +1 -0
  145. package/dist/radial-chart/radial-chart.d.ts +216 -0
  146. package/dist/radial-chart/radial-chart.d.ts.map +1 -0
  147. package/dist/radial-chart/radial-chart.js +174 -0
  148. package/dist/radial-chart/radial-chart.js.map +1 -0
  149. package/dist/radial-chart/radial-rings.d.ts +121 -0
  150. package/dist/radial-chart/radial-rings.d.ts.map +1 -0
  151. package/dist/radial-chart/radial-rings.js +273 -0
  152. package/dist/radial-chart/radial-rings.js.map +1 -0
  153. package/dist/radial-chart/shared.d.ts +73 -0
  154. package/dist/radial-chart/shared.d.ts.map +1 -0
  155. package/dist/radial-chart/shared.js +79 -0
  156. package/dist/radial-chart/shared.js.map +1 -0
  157. package/package.json +4 -4
  158. package/registry.json +21 -6
  159. package/src/bar-chart/bar-chart.ts +252 -179
  160. package/src/bar-chart/category-axis.ts +46 -40
  161. package/src/bar-chart/chart-bars.ts +97 -117
  162. package/src/bar-chart/index.ts +7 -10
  163. package/src/bar-chart/shared.ts +78 -238
  164. package/src/engine.ts +5 -2
  165. package/src/index.ts +2 -0
  166. package/src/kit/cartesian.ts +118 -40
  167. package/src/kit/chart-canvas.ts +49 -54
  168. package/src/kit/chart-legend.ts +28 -20
  169. package/src/kit/horizontal-axis.ts +114 -0
  170. package/src/kit/motion.ts +7 -0
  171. package/src/{pie-chart → kit}/pie-legend.ts +12 -11
  172. package/src/kit/polar.ts +54 -0
  173. package/src/kit/shared.ts +55 -277
  174. package/src/kit/style.ts +257 -0
  175. package/src/kit/vertical-axis.ts +67 -48
  176. package/src/line-chart/chart-line.ts +176 -54
  177. package/src/line-chart/chart-region.ts +6 -10
  178. package/src/line-chart/index.ts +14 -10
  179. package/src/line-chart/line-chart.ts +347 -151
  180. package/src/line-chart/shared.ts +193 -174
  181. package/src/line-chart/x-scale.ts +13 -0
  182. package/src/nodes.ts +9 -1
  183. package/src/pie-chart/index.ts +2 -23
  184. package/src/pie-chart/pie-chart.ts +52 -35
  185. package/src/pie-chart/pie-ring.ts +92 -71
  186. package/src/pie-chart/shared.ts +68 -266
  187. package/src/radar-chart/index.ts +4 -0
  188. package/src/radar-chart/radar-chart.ts +287 -0
  189. package/src/radar-chart/radar-grid.ts +78 -0
  190. package/src/radar-chart/radar-series.ts +88 -0
  191. package/src/radar-chart/shared.ts +146 -0
  192. package/src/radial-chart/index.ts +5 -0
  193. package/src/radial-chart/radial-chart.ts +210 -0
  194. package/src/radial-chart/radial-rings.ts +328 -0
  195. package/src/radial-chart/shared.ts +114 -0
  196. package/dist/browser/chunks/chunk-4SMRCU24.js +0 -2
  197. package/dist/browser/chunks/chunk-4SMRCU24.js.map +0 -7
  198. package/dist/kit/chart-border.d.ts +0 -18
  199. package/dist/kit/chart-border.d.ts.map +0 -1
  200. package/dist/kit/chart-border.js +0 -23
  201. package/dist/kit/chart-border.js.map +0 -1
  202. package/dist/line-chart/horizontal-axis.d.ts.map +0 -1
  203. package/dist/line-chart/horizontal-axis.js.map +0 -1
  204. package/dist/pie-chart/pie-legend.d.ts.map +0 -1
  205. package/dist/pie-chart/pie-legend.js.map +0 -1
  206. package/src/kit/chart-border.ts +0 -36
  207. package/src/line-chart/horizontal-axis.ts +0 -122
@@ -5,7 +5,6 @@ import {
5
5
  Types,
6
6
  component2d,
7
7
  insetsOps,
8
- textOps,
9
8
  type NodeOf,
10
9
  // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
11
10
  type Node2D,
@@ -13,6 +12,7 @@ import {
13
12
  } from "@motionscript/sdk"
14
13
  import { enterBudget } from "@motionscript/sdk/component"
15
14
 
15
+ import { boundText } from "./cartesian"
16
16
  import { asParts, fresh } from "./entries"
17
17
  import { armed, arrival, arrivalOf, arrivalsEntry, beats, frameEase, type Arrival } from "./motion"
18
18
  import {
@@ -30,11 +30,65 @@ export function markerCaption(s: { readonly markers: readonly number[]; readonly
30
30
  return (index >= 0 ? s.captions[index] : undefined) ?? formatMarker(value)
31
31
  }
32
32
 
33
+ /** The gap between an axis's markers and the plot, and between the markers and the caption, read off its config. */
34
+ export function axisPads(axis: AxisConfig, side: "top" | "right"): { tick: number; title: number } {
35
+ return { tick: insetsOps.resolve(axis.tickPadding)[side], title: insetsOps.resolve(axis.titlePadding)[side] }
36
+ }
37
+
38
+ /**
39
+ * A column of right-aligned captions beside the plot: `place(i)` is caption
40
+ * `i`'s centre height in the column (y-up), whose height it is handed. The
41
+ * value axis and a horizontal bar chart's category axis are both this column.
42
+ */
43
+ export function captionColumn(spec: {
44
+ readonly count: number
45
+ readonly axis: () => AxisConfig
46
+ readonly text: (i: number) => string
47
+ readonly place: (i: number, height: number) => number
48
+ readonly arrivals: () => Readonly<Record<string, Arrival>>
49
+ readonly opacity?: (i: number) => number
50
+ readonly label: string
51
+ }): Node2D {
52
+ const pads = () => axisPads(spec.axis(), "right")
53
+ const markers = Array.from({ length: spec.count }, (_, i) => {
54
+ const column = () => (marker.parent as Node2D | null)?.layoutBounds ?? { width: 0, height: 0 }
55
+ const marker: Node2D = new Text({
56
+ ...boundText(() => spec.axis().labelStyle),
57
+ text: () => spec.text(i),
58
+ // Right-aligned against the tick padding: freeform children sit on the content box's centre.
59
+ x: () => (column().width - pads().tick) / 2 - marker.layoutBounds.width / 2 + arrivalOf(spec.arrivals(), i).shift,
60
+ y: () => spec.place(i, column().height),
61
+ opacity: () => arrivalOf(spec.arrivals(), i).opacity * (spec.opacity?.(i) ?? 1),
62
+ })
63
+ return marker
64
+ })
65
+ const children: Node2D[] = []
66
+ if (spec.label !== "") {
67
+ // Node2D space is y-up and rotation is clockwise, so -90° reads bottom-to-top.
68
+ children.push(
69
+ new RotatedBox({
70
+ angle: -90,
71
+ width: "hug",
72
+ height: "hug",
73
+ children: new Text({
74
+ ...boundText(() => spec.axis().titleStyle),
75
+ text: spec.label,
76
+ opacity: () => arrivalOf(spec.arrivals(), "label").opacity,
77
+ x: () => arrivalOf(spec.arrivals(), "label").shift,
78
+ }),
79
+ }) as unknown as Node2D
80
+ )
81
+ }
82
+ children.push(new Rect({ height: "fill", width: "hug", padding: () => ({ right: pads().tick }), children: markers }) as unknown as Node2D)
83
+ return new Rect({ flow: "horizontal", gap: () => pads().title, height: "fill", children }) as unknown as Node2D
84
+ }
85
+
33
86
  /**
34
87
  * The numeric vertical axis the cartesian charts share: a column of {@link Text}
35
88
  * markers, highest at the top, each centred on its value's fraction across
36
- * `range`. Caption and position are bound, so a rescale tweens live; only the
37
- * marker count is fixed at construction. The resting state is landed.
89
+ * `range` and right-aligned against the plot. Caption and position are bound, so
90
+ * a rescale tweens live; only the marker count is fixed at construction. The
91
+ * resting state is landed.
38
92
  */
39
93
  export const VerticalAxis = component2d({
40
94
  key: "verticalAxis",
@@ -52,61 +106,26 @@ export const VerticalAxis = component2d({
52
106
  // A node made of markers would otherwise hug them.
53
107
  defaults: { width: "fill", height: "fill" },
54
108
  children: (state) => {
55
- const tickPad = insetsOps.resolve(state.axis.tickPadding).right
56
- const titlePad = insetsOps.resolve(state.axis.titlePadding).right
57
-
58
- const row: Node2D[] = []
59
- // The row's parent is the axis, whose box the markers are placed in.
60
- const box = () => (row[0]?.parent as Node2D | null | undefined)?.layoutBounds ?? { width: 0, height: 0 }
61
109
  const sorted = () => [...state.markers].sort((a, b) => b - a)
62
110
  const fracAt = (value: number | undefined) => {
63
111
  if (value === undefined) return 0
64
112
  const [min, max] = state.range
65
113
  return max <= min ? 0 : (value - min) / (max - min)
66
114
  }
67
-
68
115
  // A slot with no value (the live list is shorter than the column built) renders empty, not its neighbour.
69
- const markers = state.markers.map((_, i) => {
70
- const marker: Node2D = new Text({
71
- ...textOps.styleProps(state.axis.labelStyle),
72
- text: () => {
116
+ return asParts([
117
+ captionColumn({
118
+ count: state.markers.length,
119
+ axis: () => state.axis,
120
+ text: (i) => {
73
121
  const value = sorted()[i]
74
122
  return value === undefined ? "" : markerCaption(state, value)
75
123
  },
76
- y: () => {
77
- // Node2D space is y-up, so the fraction runs from the bottom.
78
- const height = box().height
79
- const centred = (fracAt(sorted()[i]) - 0.5) * height
80
- // Held inside the top edge for a theme whose markers are taller than the headroom; only the top, since the chart's own axis and legend lie below.
81
- const limit = height / 2 - marker.layoutBounds.height / 2
82
- return Math.min(centred, limit)
83
- },
84
- opacity: () => arrivalOf(state.arrivals, i).opacity,
85
- x: () => arrivalOf(state.arrivals, i).shift,
86
- })
87
- return marker
88
- })
89
-
90
- const children: Node2D[] = []
91
- if (state.label !== "") {
92
- // Node2D space is y-up and rotation is clockwise, so -90° reads bottom-to-top.
93
- children.push(
94
- new RotatedBox({
95
- angle: -90,
96
- width: "hug",
97
- height: "hug",
98
- children: new Text({
99
- ...textOps.styleProps(state.axis.titleStyle),
100
- text: state.label,
101
- opacity: () => arrivalOf(state.arrivals, "label").opacity,
102
- x: () => arrivalOf(state.arrivals, "label").shift,
103
- }),
104
- })
105
- )
106
- }
107
- children.push(new Rect({ height: "fill", width: "hug", padding: { right: tickPad }, children: markers }))
108
- row.push(new Rect({ flow: "horizontal", gap: titlePad, children }))
109
- return asParts(row)
124
+ place: (i, height) => (fracAt(sorted()[i]) - 0.5) * height,
125
+ arrivals: () => state.arrivals,
126
+ label: state.label,
127
+ }),
128
+ ])
110
129
  },
111
130
  commands: {
112
131
  /**
@@ -1,67 +1,83 @@
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
11
  // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
8
12
  type Node2D as _Node2D,
9
13
  type Node2DProps as _Node2DProps,
14
+ type PathCommand,
15
+ type RenderContext,
10
16
  type Stroke,
11
17
  type StrokeResolved,
12
18
  type Vector2,
13
19
  } from "@motionscript/sdk"
14
20
 
21
+ import { fade } from "../kit/cartesian"
15
22
  import { fresh } from "../kit/entries"
16
23
  import { frameEase } from "../kit/motion"
17
24
  import { loadPaints } from "../kit/paints"
18
25
  import { SPOTLIGHT_DIM } from "../kit/shared"
19
26
  import {
20
27
  EMPTY_REGION_BOX,
28
+ clipPolygonToBox,
21
29
  clipPolylineToBox,
30
+ cutAt,
31
+ curvePoints,
22
32
  hasRegion,
33
+ type LineCurve,
23
34
  type RegionBox,
24
35
  } from "./shared"
25
36
 
26
- /** The prefix of `pts` revealed at fraction `p`, measured by cumulative arc length so the trace keeps a steady pace over uneven spacing. */
27
- function tracePoints(pts: Vector2[], p: number): Vector2[] {
28
- if (p >= 1 || pts.length < 2) return pts
29
- if (p <= 0) return []
30
-
31
- const segLen: number[] = []
32
- let total = 0
33
- for (let i = 1; i < pts.length; i++) {
34
- const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
35
- segLen.push(d)
36
- total += d
37
- }
38
- if (total === 0) return pts
39
-
40
- let target = total * p
41
- const out: Vector2[] = [pts[0]]
42
- for (let i = 0; i < segLen.length; i++) {
43
- if (target >= segLen[i]) {
44
- out.push(pts[i + 1])
45
- target -= segLen[i]
46
- } else {
47
- const t = segLen[i] === 0 ? 0 : target / segLen[i]
48
- out.push({
49
- x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
50
- y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
51
- })
52
- break
53
- }
54
- }
55
- 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
56
43
  }
57
44
 
58
- /** One series of a {@link LineChart}: a rounded polyline through pre-mapped pixel `points`, one {@link Graphics2D} command list. `progress` traces it in; `enabled` shows or hides it wholesale. */
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))
64
+ }
65
+
66
+ /**
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.
70
+ */
59
71
  export const ChartLine = component2d({
60
72
  key: "chartLine",
61
73
  state: {
62
74
  /** In this node's pixel space (origin at centre, y-up), mapped by the parent and ordered along x. */
63
75
  points: fresh<Vector2[]>(() => []),
76
+ curve: curveEntry(),
64
77
  stroke: Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
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 }),
65
81
  /** Trace from the left end, `[0, 1]`. Rests at 1 so a paused editor canvas paints the line; `enter` drives it from 0. */
66
82
  progress: 1,
67
83
  /** Whether the line is drawn at all, flipped alongside its legend entry. */
@@ -70,7 +86,7 @@ export const ChartLine = component2d({
70
86
  spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
71
87
  dimOpacity: SPOTLIGHT_DIM,
72
88
  },
73
- load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
89
+ load: (loader, state) => loadPaints(loader, { fills: [state.dotFill], strokes: [state.stroke] }),
74
90
  commands: {
75
91
  /**
76
92
  * Trace the line from `from` to `to`; `showSeries`/`hideSeries` are this with
@@ -90,39 +106,145 @@ export const ChartLine = component2d({
90
106
  })
91
107
  },
92
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
+ },
93
117
  },
94
118
  render: (ctx, state) => {
95
119
  if (!state.enabled) return
96
120
  const pts = state.points
97
- if (pts.length < 2) return
98
-
99
- const p = Math.max(0, Math.min(1, state.progress))
100
- if (p <= 0) return
121
+ const p = clamp(state.progress, 0, 1)
122
+ if (pts.length === 0 || p <= 0) return
101
123
 
102
- const drawn = tracePoints(pts, p)
103
- if (drawn.length < 2) return
124
+ const box = state.spotlight
125
+ const lit = hasRegion(box)
126
+ const dim = clamp(state.dimOpacity, 0, 1)
104
127
  const stroke = state.stroke
105
- const radius = (stroke[0]?.weight ?? 0) * 2
128
+ const weight = stroke[0]?.weight ?? 0
129
+ const drawn = traced(pts, state.curve, p)
106
130
 
107
- const box = state.spotlight
108
- if (!hasRegion(box)) {
109
- ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
110
- 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))
111
137
  }
112
138
 
113
- // Dim the whole line, then redraw the stretches inside the region at full strength: the bright run keeps continuous geometry and round joins, and the boundary lands on the region edge.
114
- ctx.draw(
115
- new Graphics2D()
116
- .line({ points: drawn, radius })
117
- .stroke(stroke)
118
- .opacity(Math.max(0, Math.min(1, state.dimOpacity)))
119
- )
120
-
121
- // One `draw` per run: shapes chained before `stroke()` combine into one surface, and stroking a union of disjoint open paths paints nothing.
122
- for (const run of clipPolylineToBox(drawn, box)) {
123
- 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
124
154
  }
155
+ if (dimAny) ctx.draw(dimmed.fill(state.dotFill).opacity(dim))
156
+ if (brightAny) ctx.draw(bright.fill(state.dotFill))
125
157
  },
126
158
  })
127
159
 
128
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>
@@ -14,10 +14,10 @@ import {
14
14
  type StrokeResolved,
15
15
  } from "@motionscript/sdk"
16
16
 
17
+ import { rule } from "../kit/chart-canvas"
17
18
  import { fresh } from "../kit/entries"
18
19
  import { loadPaints } from "../kit/paints"
19
20
  import {
20
- DEFAULT_CHART_THEME,
21
21
  EMPTY_REGION_BOX,
22
22
  hasRegion,
23
23
  type RegionBox,
@@ -26,7 +26,7 @@ import {
26
26
  /**
27
27
  * The call-out of a {@link LineChart}: a washed rectangle with a dashed rule on
28
28
  * each bounded edge, drawn from the pre-mapped `box`. Dimming the series outside
29
- * is {@link ChartLine}'s job, clipped against the same box so the two agree on
29
+ * is the line's and area's job, clipped against the same box so they agree on
30
30
  * the boundary. An unbounded edge is the plot's own border, and a rule there
31
31
  * would assert a limit the author did not set.
32
32
  */
@@ -35,8 +35,8 @@ export const ChartRegion = component2d({
35
35
  state: {
36
36
  /** In this node's pixel space (origin at centre, y-up), mapped by the parent. A degenerate box draws nothing. */
37
37
  box: fresh<RegionBox>(() => EMPTY_REGION_BOX),
38
- stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_CHART_THEME.region.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
39
- fill: Types.custom<Fill, FillResolved[]>(DEFAULT_CHART_THEME.region.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
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
40
  /** Driven by the chart's `regionReveal`, so the rules and the series' dimming move as one. */
41
41
  reveal: 0,
42
42
  },
@@ -59,17 +59,13 @@ export const ChartRegion = component2d({
59
59
 
60
60
  if (state.stroke.length === 0) return
61
61
 
62
- // One `draw` per rule: stroking a union of disjoint open paths paints nothing.
63
- const rule = (points: { x: number; y: number }[]) =>
64
- ctx.draw(new Graphics2D().line({ points }).stroke(state.stroke).opacity(r))
65
-
66
62
  if (box.xBounded) {
67
63
  const half = (height / 2) * r
68
- 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)
69
65
  }
70
66
  if (box.yBounded) {
71
67
  const half = (width / 2) * r
72
- 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)
73
69
  }
74
70
  },
75
71
  })
@@ -1,27 +1,31 @@
1
1
  export { LineChart, lineChartDomains } from "./line-chart"
2
- export type { LineChartDomains, LineChartValues } from "./line-chart"
2
+ export type { LineChartDomains, LineChartStyle, LineChartValues } from "./line-chart"
3
3
  export { ChartRegion } from "./chart-region"
4
- export { ChartLine } from "./chart-line"
5
- export { HorizontalAxis } from "./horizontal-axis"
4
+ export { ChartArea, ChartLine, areaFill, traceX } from "./chart-line"
6
5
  export {
7
- DEFAULT_CHART_THEME,
8
6
  EMPTY_REGION_BOX,
7
+ LINE_LABELS,
8
+ LINE_MARKS,
9
+ clipPolygonToBox,
9
10
  clipPolylineToBox,
11
+ curvePoints,
12
+ cutAt,
10
13
  hasRegion,
11
- lerpChartTheme,
12
- resolveChartTheme,
14
+
13
15
  resolveRegionBox,
14
16
  resolveYScale,
15
17
  seriesPoints,
18
+ stackExtent,
19
+ stackSeries,
16
20
  yExtent,
17
21
  } from "./shared"
18
22
  export type {
19
- ChartConfig,
20
- ChartTheme,
23
+ LineCurve,
21
24
  LineSeries,
25
+ LineStack,
22
26
  PlotRegion,
23
27
  RegionBox,
24
- RegionConfig,
28
+ StackedSeries,
25
29
  } from "./shared"
26
- export { formatInstant, parseInstant, resolveXScale } from "./x-scale"
30
+ export { EVERY_SAMPLE, formatInstant, parseInstant, resolveXScale } from "./x-scale"
27
31
  export type { XScale, XScaleKind } from "./x-scale"