@motionscript/charts 0.1.0-alpha.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +99 -99
  5. package/dist/bar-chart/bar-chart.js.map +1 -1
  6. package/dist/bar-chart/category-axis.js.map +1 -1
  7. package/dist/bar-chart/chart-bars.js.map +1 -1
  8. package/dist/bar-chart/index.js.map +1 -1
  9. package/dist/bar-chart/shared.js.map +1 -1
  10. package/dist/browser/chunks/{chunk-XROBIRQK.js → chunk-TJNHLQGV.js} +1 -1
  11. package/dist/browser/chunks/chunk-TJNHLQGV.js.map +7 -0
  12. package/dist/browser/index.js +1 -1
  13. package/dist/browser/index.js.map +1 -1
  14. package/dist/browser/kit.js +1 -1
  15. package/dist/browser/manifest.json +1 -1
  16. package/dist/engine.d.ts +1 -1
  17. package/dist/engine.js +1 -1
  18. package/dist/engine.js.map +1 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/kit/cartesian.js.map +1 -1
  21. package/dist/kit/chart-border.js.map +1 -1
  22. package/dist/kit/chart-canvas.js.map +1 -1
  23. package/dist/kit/chart-legend.js.map +1 -1
  24. package/dist/kit/entries.js.map +1 -1
  25. package/dist/kit/motion.js.map +1 -1
  26. package/dist/kit/paints.js.map +1 -1
  27. package/dist/kit/shared.js.map +1 -1
  28. package/dist/kit/vertical-axis.js.map +1 -1
  29. package/dist/line-chart/chart-line.js.map +1 -1
  30. package/dist/line-chart/chart-region.js.map +1 -1
  31. package/dist/line-chart/horizontal-axis.js.map +1 -1
  32. package/dist/line-chart/index.d.ts +1 -1
  33. package/dist/line-chart/index.js +1 -1
  34. package/dist/line-chart/index.js.map +1 -1
  35. package/dist/line-chart/line-chart.js.map +1 -1
  36. package/dist/line-chart/shared.js.map +1 -1
  37. package/dist/line-chart/x-scale.js.map +1 -1
  38. package/dist/nodes.js.map +1 -1
  39. package/dist/pie-chart/index.js.map +1 -1
  40. package/dist/pie-chart/pie-chart.js.map +1 -1
  41. package/dist/pie-chart/pie-legend.js.map +1 -1
  42. package/dist/pie-chart/pie-ring.js.map +1 -1
  43. package/dist/pie-chart/shared.js.map +1 -1
  44. package/package.json +4 -4
  45. package/registry.json +23 -23
  46. package/src/bar-chart/bar-chart.ts +542 -542
  47. package/src/bar-chart/category-axis.ts +162 -162
  48. package/src/bar-chart/chart-bars.ts +287 -287
  49. package/src/bar-chart/index.ts +31 -31
  50. package/src/bar-chart/shared.ts +354 -354
  51. package/src/engine.ts +28 -28
  52. package/src/index.ts +5 -5
  53. package/src/kit/cartesian.ts +81 -81
  54. package/src/kit/chart-border.ts +50 -50
  55. package/src/kit/chart-canvas.ts +122 -122
  56. package/src/kit/chart-legend.ts +229 -229
  57. package/src/kit/entries.ts +20 -20
  58. package/src/kit/motion.ts +90 -90
  59. package/src/kit/paints.ts +15 -15
  60. package/src/kit/shared.ts +782 -782
  61. package/src/kit/vertical-axis.ts +194 -194
  62. package/src/line-chart/chart-line.ts +156 -156
  63. package/src/line-chart/chart-region.ts +90 -90
  64. package/src/line-chart/horizontal-axis.ts +151 -151
  65. package/src/line-chart/index.ts +39 -39
  66. package/src/line-chart/line-chart.ts +598 -598
  67. package/src/line-chart/shared.ts +473 -473
  68. package/src/line-chart/x-scale.ts +382 -382
  69. package/src/nodes.ts +18 -18
  70. package/src/pie-chart/index.ts +37 -37
  71. package/src/pie-chart/pie-chart.ts +296 -296
  72. package/src/pie-chart/pie-legend.ts +169 -169
  73. package/src/pie-chart/pie-ring.ts +848 -848
  74. package/src/pie-chart/shared.ts +496 -496
  75. package/dist/browser/chunks/chunk-XROBIRQK.js.map +0 -7
  76. package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
package/src/engine.ts CHANGED
@@ -1,28 +1,28 @@
1
- /**
2
- * The chart **kit**: everything the three charts share, and the one thing a
3
- * copied chart does not copy.
4
- *
5
- * `ms add line-chart` writes `line-chart/*` into your project and rewrites its
6
- * reach for these to `@motionscript/charts`, so the plot panel, the value
7
- * axis, the legend and the number formatting stay a real dependency. That is
8
- * not a size decision — it is the only arrangement that works.
9
- *
10
- * A chart identifies its own parts by component identity (`VerticalAxis.is`).
11
- * Copy `VerticalAxis` alongside your fork and there are now two of it — two
12
- * components answering one name, and whichever one your chart did not get is
13
- * a part it never finds, with no error. Keeping the kit external makes that
14
- * impossible rather than unlikely.
15
- *
16
- * It also means a fix to the axis reaches a forked chart without a re-add,
17
- * which is the half of the bargain you get back for it.
18
- */
19
- export * from "./kit/shared"
20
- export * from "./kit/cartesian"
21
- export * from "./kit/chart-border"
22
- export * from "./kit/chart-canvas"
23
- export * from "./kit/chart-legend"
24
- export * from "./kit/entries"
25
- export * from "./kit/motion"
26
- export * from "./kit/paints"
27
- export * from "./kit/vertical-axis"
28
- export * from "@motionscript/sdk/component"
1
+ /**
2
+ * The chart **kit**: everything the three charts share, and the one thing a
3
+ * copied chart does not copy.
4
+ *
5
+ * A copy of the line chart takes `line-chart/*` into your project and rewrites its
6
+ * reach for these to `@motionscript/charts`, so the plot panel, the value
7
+ * axis, the legend and the number formatting stay a real dependency. That is
8
+ * not a size decision — it is the only arrangement that works.
9
+ *
10
+ * A chart identifies its own parts by component identity (`VerticalAxis.is`).
11
+ * Copy `VerticalAxis` alongside your fork and there are now two of it — two
12
+ * components answering one name, and whichever one your chart did not get is
13
+ * a part it never finds, with no error. Keeping the kit external makes that
14
+ * impossible rather than unlikely.
15
+ *
16
+ * It also means a fix to the axis reaches a forked chart without a re-add,
17
+ * which is the half of the bargain you get back for it.
18
+ */
19
+ export * from "./kit/shared"
20
+ export * from "./kit/cartesian"
21
+ export * from "./kit/chart-border"
22
+ export * from "./kit/chart-canvas"
23
+ export * from "./kit/chart-legend"
24
+ export * from "./kit/entries"
25
+ export * from "./kit/motion"
26
+ export * from "./kit/paints"
27
+ export * from "./kit/vertical-axis"
28
+ export * from "@motionscript/sdk/component"
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
- export * from "./line-chart";
2
- export * from "./bar-chart";
3
- export * from "./pie-chart";
4
- export * from "./engine";
5
- export { NODES } from "./nodes";
1
+ export * from "./line-chart";
2
+ export * from "./bar-chart";
3
+ export * from "./pie-chart";
4
+ export * from "./engine";
5
+ export { NODES } from "./nodes";
@@ -1,81 +1,81 @@
1
- import { Rect, type ChildView, type Node2D, type Size2D } from "@motionscript/sdk"
2
-
3
- import { VerticalAxis } from "./vertical-axis"
4
-
5
- /**
6
- * Headroom above the plot, in px.
7
- *
8
- * Two jobs, and the second is why it is real padding on the composition rather
9
- * than a number the point-mapping math imagines: a series peaking at the top of
10
- * the scale isn't flush to the top edge, **and** the top marker, which is
11
- * centred on that top grid line, has somewhere to be. Without it the marker's
12
- * upper half is drawn above the chart's own box.
13
- */
14
- export const TOP_HEADROOM = 24
15
-
16
- /** The parts of a cartesian chart's frame, as {@link cartesianFrame} arranges them. */
17
- export interface CartesianParts {
18
- /** The value axis, left of the plot. */
19
- readonly axis: Node2D
20
- /** The plot panel: the box the grid and the marks share. */
21
- readonly plot: Node2D
22
- /** An invisible twin of the value axis, reserving its gutter under the plot. */
23
- readonly gutter: Node2D
24
- /** What sits under the plot, top to bottom: the other axis, then the legend. */
25
- readonly below: readonly Node2D[]
26
- }
27
-
28
- /**
29
- * The frame the line and bar charts share: the value axis beside the plot, and
30
- * under them the gutter beside the rows below the plot, so those rows start at
31
- * the plot's left edge rather than the chart's.
32
- */
33
- export function cartesianFrame(parts: CartesianParts): Node2D {
34
- return new Rect({
35
- flow: "vertical",
36
- width: "fill",
37
- height: "fill",
38
- padding: { top: TOP_HEADROOM },
39
- children: [
40
- new Rect({ flow: "horizontal", height: "fill", width: "fill", children: [parts.axis, parts.plot] }),
41
- new Rect({
42
- flow: "horizontal",
43
- width: "fill",
44
- children: [parts.gutter, new Rect({ flow: "vertical", width: "fill", gap: 20, children: [...parts.below] })],
45
- }),
46
- ],
47
- }) as unknown as Node2D
48
- }
49
-
50
- /** A cartesian frame's parts, as a command reaches them through its chart's children. */
51
- export interface CartesianViews {
52
- readonly axis: ChildView
53
- /** The plot panel's own children: the grid, the marks, the border. */
54
- readonly plot: readonly ChildView[]
55
- readonly below: readonly ChildView[]
56
- }
57
-
58
- /**
59
- * The frame among a chart's children: the last child shaped as
60
- * {@link cartesianFrame} builds it. The chart builds it after the children it
61
- * was given, and a document adds its own later still, so it is found by shape.
62
- */
63
- export function cartesianViews(children: readonly ChildView[]): CartesianViews | null {
64
- for (let i = children.length - 1; i >= 0; i--) {
65
- const frame = children[i]
66
- if (!Rect.is(frame)) continue
67
- const [top, bottom] = frame.children
68
- const axis = top?.children[0]
69
- const plot = top?.children[1]
70
- const below = bottom?.children[1]
71
- if (!axis || !VerticalAxis.is(axis) || !plot || !below) continue
72
- return { axis, plot: plot.children, below: below.children }
73
- }
74
- return null
75
- }
76
-
77
- /** A part's laid-out size, or nothing before it is built or laid out. */
78
- export function laidOutSize(node: Node2D | undefined): Size2D {
79
- const bounds = node?.layoutBounds
80
- return { width: bounds?.width ?? 0, height: bounds?.height ?? 0 }
81
- }
1
+ import { Rect, type ChildView, type Node2D, type Size2D } from "@motionscript/sdk"
2
+
3
+ import { VerticalAxis } from "./vertical-axis"
4
+
5
+ /**
6
+ * Headroom above the plot, in px.
7
+ *
8
+ * Two jobs, and the second is why it is real padding on the composition rather
9
+ * than a number the point-mapping math imagines: a series peaking at the top of
10
+ * the scale isn't flush to the top edge, **and** the top marker, which is
11
+ * centred on that top grid line, has somewhere to be. Without it the marker's
12
+ * upper half is drawn above the chart's own box.
13
+ */
14
+ export const TOP_HEADROOM = 24
15
+
16
+ /** The parts of a cartesian chart's frame, as {@link cartesianFrame} arranges them. */
17
+ export interface CartesianParts {
18
+ /** The value axis, left of the plot. */
19
+ readonly axis: Node2D
20
+ /** The plot panel: the box the grid and the marks share. */
21
+ readonly plot: Node2D
22
+ /** An invisible twin of the value axis, reserving its gutter under the plot. */
23
+ readonly gutter: Node2D
24
+ /** What sits under the plot, top to bottom: the other axis, then the legend. */
25
+ readonly below: readonly Node2D[]
26
+ }
27
+
28
+ /**
29
+ * The frame the line and bar charts share: the value axis beside the plot, and
30
+ * under them the gutter beside the rows below the plot, so those rows start at
31
+ * the plot's left edge rather than the chart's.
32
+ */
33
+ export function cartesianFrame(parts: CartesianParts): Node2D {
34
+ return new Rect({
35
+ flow: "vertical",
36
+ width: "fill",
37
+ height: "fill",
38
+ padding: { top: TOP_HEADROOM },
39
+ children: [
40
+ new Rect({ flow: "horizontal", height: "fill", width: "fill", children: [parts.axis, parts.plot] }),
41
+ new Rect({
42
+ flow: "horizontal",
43
+ width: "fill",
44
+ children: [parts.gutter, new Rect({ flow: "vertical", width: "fill", gap: 20, children: [...parts.below] })],
45
+ }),
46
+ ],
47
+ }) as unknown as Node2D
48
+ }
49
+
50
+ /** A cartesian frame's parts, as a command reaches them through its chart's children. */
51
+ export interface CartesianViews {
52
+ readonly axis: ChildView
53
+ /** The plot panel's own children: the grid, the marks, the border. */
54
+ readonly plot: readonly ChildView[]
55
+ readonly below: readonly ChildView[]
56
+ }
57
+
58
+ /**
59
+ * The frame among a chart's children: the last child shaped as
60
+ * {@link cartesianFrame} builds it. The chart builds it after the children it
61
+ * was given, and a document adds its own later still, so it is found by shape.
62
+ */
63
+ export function cartesianViews(children: readonly ChildView[]): CartesianViews | null {
64
+ for (let i = children.length - 1; i >= 0; i--) {
65
+ const frame = children[i]
66
+ if (!Rect.is(frame)) continue
67
+ const [top, bottom] = frame.children
68
+ const axis = top?.children[0]
69
+ const plot = top?.children[1]
70
+ const below = bottom?.children[1]
71
+ if (!axis || !VerticalAxis.is(axis) || !plot || !below) continue
72
+ return { axis, plot: plot.children, below: below.children }
73
+ }
74
+ return null
75
+ }
76
+
77
+ /** A part's laid-out size, or nothing before it is built or laid out. */
78
+ export function laidOutSize(node: Node2D | undefined): Size2D {
79
+ const bounds = node?.layoutBounds
80
+ return { width: bounds?.width ?? 0, height: bounds?.height ?? 0 }
81
+ }
@@ -1,50 +1,50 @@
1
- import {
2
- Graphics2D,
3
- Types,
4
- component2d,
5
- strokeOps,
6
- type NodeOf,
7
- // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
8
- type Node2D as _Node2D,
9
- type Node2DProps,
10
- type Stroke,
11
- type StrokeResolved,
12
- } from "@motionscript/sdk"
13
-
14
- import { loadPaints } from "./paints"
15
- import { DEFAULT_PLOT_AREA } from "./shared"
16
-
17
- export interface ChartBorderProps extends Node2DProps {
18
- /**
19
- * The plot's thick frame stroke. The stroke resolver's default `align` is
20
- * `'inside'`, so the whole weight sits within the layout box unless the theme
21
- * overrides it.
22
- */
23
- stroke: Stroke
24
- }
25
-
26
- /**
27
- * The thick **frame** around a cartesian chart's plot: an empty rect wearing an
28
- * inside stroke, sized from its box.
29
- *
30
- * Split out of {@link ChartCanvas} (the filled + gridded backdrop) so it can be
31
- * placed *after* the marks in the tree — the box then frames the plot **over**
32
- * them, so a line or a column running to the edge tucks cleanly under the border
33
- * rather than sitting on top of it.
34
- */
35
- export const ChartBorder = component2d({
36
- key: "chartBorder",
37
- state: {
38
- stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_PLOT_AREA.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
39
- },
40
- load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
41
- render: (ctx, state) => {
42
- const { width, height } = ctx.size
43
- if (width <= 0 || height <= 0) return
44
- // Empty rect (no fill) with the themed stroke, so the frame sits over the
45
- // marks but the box interior stays clear.
46
- ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).stroke(state.stroke))
47
- },
48
- })
49
-
50
- export type ChartBorder = NodeOf<typeof ChartBorder>
1
+ import {
2
+ Graphics2D,
3
+ Types,
4
+ component2d,
5
+ strokeOps,
6
+ type NodeOf,
7
+ // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
8
+ type Node2D as _Node2D,
9
+ type Node2DProps,
10
+ type Stroke,
11
+ type StrokeResolved,
12
+ } from "@motionscript/sdk"
13
+
14
+ import { loadPaints } from "./paints"
15
+ import { DEFAULT_PLOT_AREA } from "./shared"
16
+
17
+ export interface ChartBorderProps extends Node2DProps {
18
+ /**
19
+ * The plot's thick frame stroke. The stroke resolver's default `align` is
20
+ * `'inside'`, so the whole weight sits within the layout box unless the theme
21
+ * overrides it.
22
+ */
23
+ stroke: Stroke
24
+ }
25
+
26
+ /**
27
+ * The thick **frame** around a cartesian chart's plot: an empty rect wearing an
28
+ * inside stroke, sized from its box.
29
+ *
30
+ * Split out of {@link ChartCanvas} (the filled + gridded backdrop) so it can be
31
+ * placed *after* the marks in the tree — the box then frames the plot **over**
32
+ * them, so a line or a column running to the edge tucks cleanly under the border
33
+ * rather than sitting on top of it.
34
+ */
35
+ export const ChartBorder = component2d({
36
+ key: "chartBorder",
37
+ state: {
38
+ stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_PLOT_AREA.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
39
+ },
40
+ load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
41
+ render: (ctx, state) => {
42
+ const { width, height } = ctx.size
43
+ if (width <= 0 || height <= 0) return
44
+ // Empty rect (no fill) with the themed stroke, so the frame sits over the
45
+ // marks but the box interior stays clear.
46
+ ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).stroke(state.stroke))
47
+ },
48
+ })
49
+
50
+ export type ChartBorder = NodeOf<typeof ChartBorder>
@@ -1,122 +1,122 @@
1
- import {
2
- Graphics2D,
3
- Types,
4
- component2d,
5
- fillOps,
6
- shadowOps,
7
- strokeOps,
8
- type Fill,
9
- type FillResolved,
10
- type NodeOf,
11
- // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
12
- type Node2D as _Node2D,
13
- type Node2DProps,
14
- type Shadow,
15
- type ShadowResolved,
16
- type Stroke,
17
- type StrokeResolved,
18
- } from "@motionscript/sdk"
19
-
20
- import { fresh } from "./entries"
21
- import { loadPaints } from "./paints"
22
- import {
23
- DEFAULT_PLOT_AREA,
24
- DEFAULT_X_AXIS,
25
- DEFAULT_Y_AXIS,
26
- type Range,
27
- } from "./shared"
28
-
29
- export interface ChartCanvasProps extends Node2DProps {
30
- /** Fill of the plot panel behind the marks. */
31
- fill: Fill
32
- /** Drop shadow of the panel. Default none. */
33
- shadow: Shadow
34
- /** Stroke of the vertical grid lines (one per {@link xMarkers}). */
35
- xGridStroke: Stroke
36
- /** Stroke of the horizontal grid lines (one per {@link yMarkers}). */
37
- yGridStroke: Stroke
38
- /**
39
- * Data-unit x positions to draw a vertical grid line at — one per horizontal
40
- * axis marker, so a line lands under every marker regardless of spacing.
41
- * Positions at (or past) an {@link xExtent} edge coincide with the border and
42
- * are skipped. Pass an empty list for a chart whose horizontal axis is
43
- * categorical and so has no numeric position to rule at.
44
- */
45
- xMarkers: number[]
46
- /** Data-unit y positions to draw a horizontal grid line at. */
47
- yMarkers: number[]
48
- /** `[min, max]` of the horizontal scale — maps {@link xMarkers} into the panel. */
49
- xExtent: Range
50
- /** `[min, max]` of the vertical scale — maps {@link yMarkers} into the panel. */
51
- yExtent: Range
52
- }
53
-
54
- const strokeEntry = (initial: Stroke) =>
55
- Types.custom<Stroke, StrokeResolved[]>(initial, { resolve: strokeOps.resolve, lerp: strokeOps.lerp })
56
-
57
- /**
58
- * The plot **backdrop** of a cartesian chart: a filled panel with a grid of
59
- * faint lines spanning both directions across it, sized from its box and centred
60
- * on its origin.
61
- *
62
- * The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
63
- * *over* the marks while this panel sits under them. Grid lines are placed from
64
- * the axis marker positions (in data units) mapped into the panel through
65
- * {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
66
- * marker whether or not the markers are evenly spaced.
67
- */
68
- export const ChartCanvas = component2d({
69
- key: "chartCanvas",
70
- state: {
71
- fill: Types.custom<Fill, FillResolved[]>(DEFAULT_PLOT_AREA.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
72
- shadow: Types.custom<Shadow, ShadowResolved[]>(DEFAULT_PLOT_AREA.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),
73
- xGridStroke: strokeEntry(DEFAULT_X_AXIS.lineStyle),
74
- yGridStroke: strokeEntry(DEFAULT_Y_AXIS.lineStyle),
75
- xMarkers: fresh<number[]>(() => []),
76
- yMarkers: fresh<number[]>(() => []),
77
- xExtent: fresh<Range>(() => [0, 1]),
78
- yExtent: fresh<Range>(() => [0, 1]),
79
- },
80
- // The grid strokes are named past the conventional slots, so they are named here.
81
- load: (loader, state) =>
82
- loadPaints(loader, { fills: [state.fill], shadows: [state.shadow], strokes: [state.xGridStroke, state.yGridStroke] }),
83
- render: (ctx, state) => {
84
- const { width: W, height: H } = ctx.size
85
- if (W <= 0 || H <= 0) return
86
-
87
- // Plot panel, centred on the origin (node space is y-up, origin centre).
88
- ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).shadow(state.shadow).fill(state.fill))
89
-
90
- const [xMin, xMax] = state.xExtent
91
- const [yMin, yMax] = state.yExtent
92
- // Fraction of a marker across its extent; guard a zero-width extent.
93
- const xFrac = (v: number) => (xMax <= xMin ? 0 : (v - xMin) / (xMax - xMin))
94
- const yFrac = (v: number) => (yMax <= yMin ? 0 : (v - yMin) / (yMax - yMin))
95
- // Skip markers landing on (or outside) the edges — they'd sit under the border.
96
- const interior = (f: number) => f > 1e-6 && f < 1 - 1e-6
97
-
98
- const xStroke = state.xGridStroke[0]
99
- if (xStroke) {
100
- const xGrid = new Graphics2D()
101
- for (const v of state.xMarkers) {
102
- const f = xFrac(v)
103
- if (!interior(f)) continue
104
- xGrid.rect({ x: -W / 2 + f * W, y: 0, width: xStroke.weight, height: H })
105
- }
106
- ctx.draw(xGrid.fill(xStroke.fill))
107
- }
108
-
109
- const yStroke = state.yGridStroke[0]
110
- if (yStroke) {
111
- const yGrid = new Graphics2D()
112
- for (const v of state.yMarkers) {
113
- const f = yFrac(v)
114
- if (!interior(f)) continue
115
- yGrid.rect({ x: 0, y: -H / 2 + f * H, width: W, height: yStroke.weight })
116
- }
117
- ctx.draw(yGrid.fill(yStroke.fill))
118
- }
119
- },
120
- })
121
-
122
- export type ChartCanvas = NodeOf<typeof ChartCanvas>
1
+ import {
2
+ Graphics2D,
3
+ Types,
4
+ component2d,
5
+ fillOps,
6
+ shadowOps,
7
+ strokeOps,
8
+ type Fill,
9
+ type FillResolved,
10
+ type NodeOf,
11
+ // Named so this component's emitted type reaches `Node2D` through the sdk, not core.
12
+ type Node2D as _Node2D,
13
+ type Node2DProps,
14
+ type Shadow,
15
+ type ShadowResolved,
16
+ type Stroke,
17
+ type StrokeResolved,
18
+ } from "@motionscript/sdk"
19
+
20
+ import { fresh } from "./entries"
21
+ import { loadPaints } from "./paints"
22
+ import {
23
+ DEFAULT_PLOT_AREA,
24
+ DEFAULT_X_AXIS,
25
+ DEFAULT_Y_AXIS,
26
+ type Range,
27
+ } from "./shared"
28
+
29
+ export interface ChartCanvasProps extends Node2DProps {
30
+ /** Fill of the plot panel behind the marks. */
31
+ fill: Fill
32
+ /** Drop shadow of the panel. Default none. */
33
+ shadow: Shadow
34
+ /** Stroke of the vertical grid lines (one per {@link xMarkers}). */
35
+ xGridStroke: Stroke
36
+ /** Stroke of the horizontal grid lines (one per {@link yMarkers}). */
37
+ yGridStroke: Stroke
38
+ /**
39
+ * Data-unit x positions to draw a vertical grid line at — one per horizontal
40
+ * axis marker, so a line lands under every marker regardless of spacing.
41
+ * Positions at (or past) an {@link xExtent} edge coincide with the border and
42
+ * are skipped. Pass an empty list for a chart whose horizontal axis is
43
+ * categorical and so has no numeric position to rule at.
44
+ */
45
+ xMarkers: number[]
46
+ /** Data-unit y positions to draw a horizontal grid line at. */
47
+ yMarkers: number[]
48
+ /** `[min, max]` of the horizontal scale — maps {@link xMarkers} into the panel. */
49
+ xExtent: Range
50
+ /** `[min, max]` of the vertical scale — maps {@link yMarkers} into the panel. */
51
+ yExtent: Range
52
+ }
53
+
54
+ const strokeEntry = (initial: Stroke) =>
55
+ Types.custom<Stroke, StrokeResolved[]>(initial, { resolve: strokeOps.resolve, lerp: strokeOps.lerp })
56
+
57
+ /**
58
+ * The plot **backdrop** of a cartesian chart: a filled panel with a grid of
59
+ * faint lines spanning both directions across it, sized from its box and centred
60
+ * on its origin.
61
+ *
62
+ * The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
63
+ * *over* the marks while this panel sits under them. Grid lines are placed from
64
+ * the axis marker positions (in data units) mapped into the panel through
65
+ * {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
66
+ * marker whether or not the markers are evenly spaced.
67
+ */
68
+ export const ChartCanvas = component2d({
69
+ key: "chartCanvas",
70
+ state: {
71
+ fill: Types.custom<Fill, FillResolved[]>(DEFAULT_PLOT_AREA.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
72
+ shadow: Types.custom<Shadow, ShadowResolved[]>(DEFAULT_PLOT_AREA.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),
73
+ xGridStroke: strokeEntry(DEFAULT_X_AXIS.lineStyle),
74
+ yGridStroke: strokeEntry(DEFAULT_Y_AXIS.lineStyle),
75
+ xMarkers: fresh<number[]>(() => []),
76
+ yMarkers: fresh<number[]>(() => []),
77
+ xExtent: fresh<Range>(() => [0, 1]),
78
+ yExtent: fresh<Range>(() => [0, 1]),
79
+ },
80
+ // The grid strokes are named past the conventional slots, so they are named here.
81
+ load: (loader, state) =>
82
+ loadPaints(loader, { fills: [state.fill], shadows: [state.shadow], strokes: [state.xGridStroke, state.yGridStroke] }),
83
+ render: (ctx, state) => {
84
+ const { width: W, height: H } = ctx.size
85
+ if (W <= 0 || H <= 0) return
86
+
87
+ // Plot panel, centred on the origin (node space is y-up, origin centre).
88
+ ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).shadow(state.shadow).fill(state.fill))
89
+
90
+ const [xMin, xMax] = state.xExtent
91
+ const [yMin, yMax] = state.yExtent
92
+ // Fraction of a marker across its extent; guard a zero-width extent.
93
+ const xFrac = (v: number) => (xMax <= xMin ? 0 : (v - xMin) / (xMax - xMin))
94
+ const yFrac = (v: number) => (yMax <= yMin ? 0 : (v - yMin) / (yMax - yMin))
95
+ // Skip markers landing on (or outside) the edges — they'd sit under the border.
96
+ const interior = (f: number) => f > 1e-6 && f < 1 - 1e-6
97
+
98
+ const xStroke = state.xGridStroke[0]
99
+ if (xStroke) {
100
+ const xGrid = new Graphics2D()
101
+ for (const v of state.xMarkers) {
102
+ const f = xFrac(v)
103
+ if (!interior(f)) continue
104
+ xGrid.rect({ x: -W / 2 + f * W, y: 0, width: xStroke.weight, height: H })
105
+ }
106
+ ctx.draw(xGrid.fill(xStroke.fill))
107
+ }
108
+
109
+ const yStroke = state.yGridStroke[0]
110
+ if (yStroke) {
111
+ const yGrid = new Graphics2D()
112
+ for (const v of state.yMarkers) {
113
+ const f = yFrac(v)
114
+ if (!interior(f)) continue
115
+ yGrid.rect({ x: 0, y: -H / 2 + f * H, width: W, height: yStroke.weight })
116
+ }
117
+ ctx.draw(yGrid.fill(yStroke.fill))
118
+ }
119
+ },
120
+ })
121
+
122
+ export type ChartCanvas = NodeOf<typeof ChartCanvas>