@motionscript/charts 0.0.0-stage → 0.1.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +110 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +98 -3
- package/dist/bar-chart/bar-chart.d.ts +191 -0
- package/dist/bar-chart/bar-chart.d.ts.map +1 -0
- package/dist/bar-chart/bar-chart.js +431 -0
- package/dist/bar-chart/bar-chart.js.map +1 -0
- package/dist/bar-chart/category-axis.d.ts +71 -0
- package/dist/bar-chart/category-axis.d.ts.map +1 -0
- package/dist/bar-chart/category-axis.js +119 -0
- package/dist/bar-chart/category-axis.js.map +1 -0
- package/dist/bar-chart/chart-bars.d.ts +129 -0
- package/dist/bar-chart/chart-bars.d.ts.map +1 -0
- package/dist/bar-chart/chart-bars.js +181 -0
- package/dist/bar-chart/chart-bars.js.map +1 -0
- package/dist/bar-chart/index.d.ts +16 -0
- package/dist/bar-chart/index.d.ts.map +1 -0
- package/dist/bar-chart/index.js +12 -0
- package/dist/bar-chart/index.js.map +1 -0
- package/dist/bar-chart/shared.d.ts +146 -0
- package/dist/bar-chart/shared.d.ts.map +1 -0
- package/dist/bar-chart/shared.js +182 -0
- package/dist/bar-chart/shared.js.map +1 -0
- package/dist/browser/chunks/chunk-TJNHLQGV.js +2 -0
- package/dist/browser/chunks/chunk-TJNHLQGV.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +13 -0
- package/dist/engine.d.ts +29 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +29 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/cartesian.d.ts +44 -0
- package/dist/kit/cartesian.d.ts.map +1 -0
- package/dist/kit/cartesian.js +59 -0
- package/dist/kit/cartesian.js.map +1 -0
- package/dist/kit/chart-border.d.ts +27 -0
- package/dist/kit/chart-border.d.ts.map +1 -0
- package/dist/kit/chart-border.js +28 -0
- package/dist/kit/chart-border.js.map +1 -0
- package/dist/kit/chart-canvas.d.ts +67 -0
- package/dist/kit/chart-canvas.d.ts.map +1 -0
- package/dist/kit/chart-canvas.js +68 -0
- package/dist/kit/chart-canvas.js.map +1 -0
- package/dist/kit/chart-legend.d.ts +164 -0
- package/dist/kit/chart-legend.d.ts.map +1 -0
- package/dist/kit/chart-legend.js +163 -0
- package/dist/kit/chart-legend.js.map +1 -0
- package/dist/kit/entries.d.ts +17 -0
- package/dist/kit/entries.d.ts.map +1 -0
- package/dist/kit/entries.js +19 -0
- package/dist/kit/entries.js.map +1 -0
- package/dist/kit/motion.d.ts +48 -0
- package/dist/kit/motion.d.ts.map +1 -0
- package/dist/kit/motion.js +63 -0
- package/dist/kit/motion.js.map +1 -0
- package/dist/kit/paints.d.ts +10 -0
- package/dist/kit/paints.d.ts.map +1 -0
- package/dist/kit/paints.js +12 -0
- package/dist/kit/paints.js.map +1 -0
- package/dist/kit/shared.d.ts +302 -0
- package/dist/kit/shared.d.ts.map +1 -0
- package/dist/kit/shared.js +496 -0
- package/dist/kit/shared.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +86 -0
- package/dist/kit/vertical-axis.d.ts.map +1 -0
- package/dist/kit/vertical-axis.js +141 -0
- package/dist/kit/vertical-axis.js.map +1 -0
- package/dist/line-chart/chart-line.d.ts +74 -0
- package/dist/line-chart/chart-line.d.ts.map +1 -0
- package/dist/line-chart/chart-line.js +135 -0
- package/dist/line-chart/chart-line.js.map +1 -0
- package/dist/line-chart/chart-region.d.ts +46 -0
- package/dist/line-chart/chart-region.d.ts.map +1 -0
- package/dist/line-chart/chart-region.js +67 -0
- package/dist/line-chart/chart-region.js.map +1 -0
- package/dist/line-chart/horizontal-axis.d.ts +79 -0
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -0
- package/dist/line-chart/horizontal-axis.js +129 -0
- package/dist/line-chart/horizontal-axis.js.map +1 -0
- package/dist/line-chart/index.d.ts +22 -0
- package/dist/line-chart/index.d.ts.map +1 -0
- package/dist/line-chart/index.js +19 -0
- package/dist/line-chart/index.js.map +1 -0
- package/dist/line-chart/line-chart.d.ts +200 -0
- package/dist/line-chart/line-chart.d.ts.map +1 -0
- package/dist/line-chart/line-chart.js +473 -0
- package/dist/line-chart/line-chart.js.map +1 -0
- package/dist/line-chart/shared.d.ts +171 -0
- package/dist/line-chart/shared.d.ts.map +1 -0
- package/dist/line-chart/shared.js +285 -0
- package/dist/line-chart/shared.js.map +1 -0
- package/dist/line-chart/x-scale.d.ts +77 -0
- package/dist/line-chart/x-scale.d.ts.map +1 -0
- package/dist/line-chart/x-scale.js +313 -0
- package/dist/line-chart/x-scale.js.map +1 -0
- package/dist/nodes.d.ts +17 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +18 -0
- package/dist/nodes.js.map +1 -0
- package/dist/pie-chart/index.d.ts +18 -0
- package/dist/pie-chart/index.d.ts.map +1 -0
- package/dist/pie-chart/index.js +14 -0
- package/dist/pie-chart/index.js.map +1 -0
- package/dist/pie-chart/pie-chart.d.ts +152 -0
- package/dist/pie-chart/pie-chart.d.ts.map +1 -0
- package/dist/pie-chart/pie-chart.js +222 -0
- package/dist/pie-chart/pie-chart.js.map +1 -0
- package/dist/pie-chart/pie-legend.d.ts +113 -0
- package/dist/pie-chart/pie-legend.d.ts.map +1 -0
- package/dist/pie-chart/pie-legend.js +117 -0
- package/dist/pie-chart/pie-legend.js.map +1 -0
- package/dist/pie-chart/pie-ring.d.ts +304 -0
- package/dist/pie-chart/pie-ring.d.ts.map +1 -0
- package/dist/pie-chart/pie-ring.js +680 -0
- package/dist/pie-chart/pie-ring.js.map +1 -0
- package/dist/pie-chart/shared.d.ts +216 -0
- package/dist/pie-chart/shared.d.ts.map +1 -0
- package/dist/pie-chart/shared.js +246 -0
- package/dist/pie-chart/shared.js.map +1 -0
- package/package.json +72 -3
- package/registry.json +23 -0
- package/src/bar-chart/bar-chart.ts +542 -0
- package/src/bar-chart/category-axis.ts +162 -0
- package/src/bar-chart/chart-bars.ts +287 -0
- package/src/bar-chart/index.ts +31 -0
- package/src/bar-chart/shared.ts +354 -0
- package/src/engine.ts +28 -0
- package/src/index.ts +5 -0
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +50 -0
- package/src/kit/chart-canvas.ts +122 -0
- package/src/kit/chart-legend.ts +229 -0
- package/src/kit/entries.ts +20 -0
- package/src/kit/motion.ts +90 -0
- package/src/kit/paints.ts +15 -0
- package/src/kit/shared.ts +782 -0
- package/src/kit/vertical-axis.ts +194 -0
- package/src/line-chart/chart-line.ts +156 -0
- package/src/line-chart/chart-region.ts +90 -0
- package/src/line-chart/horizontal-axis.ts +151 -0
- package/src/line-chart/index.ts +39 -0
- package/src/line-chart/line-chart.ts +598 -0
- package/src/line-chart/shared.ts +473 -0
- package/src/line-chart/x-scale.ts +382 -0
- package/src/nodes.ts +18 -0
- package/src/pie-chart/index.ts +37 -0
- package/src/pie-chart/pie-chart.ts +296 -0
- package/src/pie-chart/pie-legend.ts +169 -0
- package/src/pie-chart/pie-ring.ts +848 -0
- package/src/pie-chart/shared.ts +496 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Rect,
|
|
3
|
+
RotatedBox,
|
|
4
|
+
Text,
|
|
5
|
+
Types,
|
|
6
|
+
component2d,
|
|
7
|
+
insetsOps,
|
|
8
|
+
textOps,
|
|
9
|
+
type NodeOf,
|
|
10
|
+
type Node2D,
|
|
11
|
+
type Node2DProps,
|
|
12
|
+
} from "@motionscript/sdk"
|
|
13
|
+
|
|
14
|
+
import { asParts, fresh } from "./entries"
|
|
15
|
+
import { armed, arrival, arrivalOf, arrivalsEntry, beats, frameEase, type Arrival } from "./motion"
|
|
16
|
+
import {
|
|
17
|
+
DEFAULT_Y_AXIS,
|
|
18
|
+
enterBudget,
|
|
19
|
+
formatMarker,
|
|
20
|
+
type AxisConfig,
|
|
21
|
+
type Range,
|
|
22
|
+
} from "./shared"
|
|
23
|
+
|
|
24
|
+
export interface VerticalAxisProps extends Node2DProps {
|
|
25
|
+
/** `[min, max]` of the vertical scale the markers are positioned within. */
|
|
26
|
+
range: Range
|
|
27
|
+
/**
|
|
28
|
+
* Tick values (in data units) to draw up the axis, each placed by its fraction
|
|
29
|
+
* across {@link VerticalAxisProps.range}. Need not be evenly spaced or include
|
|
30
|
+
* the ends.
|
|
31
|
+
*/
|
|
32
|
+
markers: number[]
|
|
33
|
+
/**
|
|
34
|
+
* How each marker is captioned, one per entry of `markers` and in its order.
|
|
35
|
+
* Empty (the default), or short, captions a marker with its bare numeric
|
|
36
|
+
* reading; a chart with a number format on this axis writes them through it.
|
|
37
|
+
*/
|
|
38
|
+
captions: string[]
|
|
39
|
+
/** Marker/caption text style, grid stroke, and gutters — see {@link AxisConfig}. */
|
|
40
|
+
axis: AxisConfig
|
|
41
|
+
/**
|
|
42
|
+
* Rotated caption drawn in the gutter to the left of the marker column. Empty
|
|
43
|
+
* string (the default) hides it — no caption node is built and no label gutter
|
|
44
|
+
* is reserved.
|
|
45
|
+
*/
|
|
46
|
+
label: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** How far a marker starts to the right of its slot when it enters, in px. */
|
|
50
|
+
const SLIDE = 24
|
|
51
|
+
|
|
52
|
+
/** How a tick value is captioned — see {@link VerticalAxisProps.captions}. */
|
|
53
|
+
export type MarkerLabel = (value: number) => string
|
|
54
|
+
|
|
55
|
+
/** What a marker's caption reads: the one listed for its value, else its bare reading. */
|
|
56
|
+
export function markerCaption(s: { readonly markers: readonly number[]; readonly captions: readonly string[] }, value: number): string {
|
|
57
|
+
const index = s.markers.indexOf(value)
|
|
58
|
+
return (index >= 0 ? s.captions[index] : undefined) ?? formatMarker(value)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The numeric vertical axis shared by the cartesian charts: a **column of
|
|
63
|
+
* {@link Text} nodes**, one per marker value, running top-to-bottom (highest
|
|
64
|
+
* value at the top). Pure layout — no `Graphics` — so the markers are real text
|
|
65
|
+
* nodes the scene can measure, restyle, or animate individually.
|
|
66
|
+
*
|
|
67
|
+
* Each marker is positioned so its centre lands exactly on its value's fraction
|
|
68
|
+
* across `range` within the axis's filled height, so unevenly-spaced markers
|
|
69
|
+
* land where their values fall. Both the caption and the position are *bound*,
|
|
70
|
+
* not snapshotted: a chart that rescales its axes (see `LineChart.zoomTo`)
|
|
71
|
+
* tweens `range`/`markers` live. Only the marker **count** is fixed at
|
|
72
|
+
* construction.
|
|
73
|
+
*
|
|
74
|
+
* `enter` staggers them in **bottom-to-top**: each marker starts pushed a
|
|
75
|
+
* little to the right and transparent, then fades and slides left into place.
|
|
76
|
+
* The resting state is the landed one, so a chart that is never told to enter
|
|
77
|
+
* still reads as a finished chart.
|
|
78
|
+
*/
|
|
79
|
+
export const VerticalAxis = component2d({
|
|
80
|
+
key: "verticalAxis",
|
|
81
|
+
state: {
|
|
82
|
+
range: fresh<Range>(() => [0, 1]),
|
|
83
|
+
markers: fresh<number[]>(() => []),
|
|
84
|
+
captions: fresh<string[]>(() => []),
|
|
85
|
+
axis: Types.custom<AxisConfig>(DEFAULT_Y_AXIS),
|
|
86
|
+
label: "",
|
|
87
|
+
arrivals: arrivalsEntry(),
|
|
88
|
+
},
|
|
89
|
+
// A plain node filled its parent; one made of its markers would hug them.
|
|
90
|
+
defaults: { width: "fill", height: "fill" },
|
|
91
|
+
children: (state) => {
|
|
92
|
+
// Composed from plain entries (range/markers/label), so it's built once here.
|
|
93
|
+
const tickPad = insetsOps.resolve(state.axis.tickPadding).right
|
|
94
|
+
const titlePad = insetsOps.resolve(state.axis.titlePadding).right
|
|
95
|
+
|
|
96
|
+
const row: Node2D[] = []
|
|
97
|
+
// The row's parent is the axis: its box is what the markers are placed in.
|
|
98
|
+
const box = () => (row[0]?.parent as Node2D | null | undefined)?.layoutBounds ?? { width: 0, height: 0 }
|
|
99
|
+
/** The live tick values, top → bottom (highest first) — re-read every frame. */
|
|
100
|
+
const sorted = () => [...state.markers].sort((a, b) => b - a)
|
|
101
|
+
/** Tick `i`'s fraction across the live `range`, 0 at the bottom edge. */
|
|
102
|
+
const fracAt = (value: number | undefined) => {
|
|
103
|
+
if (value === undefined) return 0
|
|
104
|
+
const [min, max] = state.range
|
|
105
|
+
return max <= min ? 0 : (value - min) / (max - min)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// One text node per marker, ordered top → bottom. A slot with no value (the
|
|
109
|
+
// live list is shorter than the column built for it) renders as empty text
|
|
110
|
+
// rather than repeating its neighbour.
|
|
111
|
+
const markers = state.markers.map((_, i) => {
|
|
112
|
+
const marker: Node2D = new Text({
|
|
113
|
+
...textOps.styleProps(state.axis.labelStyle),
|
|
114
|
+
text: () => {
|
|
115
|
+
const value = sorted()[i]
|
|
116
|
+
return value === undefined ? "" : markerCaption(state, value)
|
|
117
|
+
},
|
|
118
|
+
y: () => {
|
|
119
|
+
// Node2D space is y-up, so the fraction runs from the bottom.
|
|
120
|
+
const height = box().height
|
|
121
|
+
const centred = (fracAt(sorted()[i]) - 0.5) * height
|
|
122
|
+
// Held inside the axis's top edge — the backstop for a chart whose
|
|
123
|
+
// headroom is smaller than its marker text is tall, which a theme with
|
|
124
|
+
// much larger markers can arrange. Only the top, because what lies
|
|
125
|
+
// below the axis is the chart's own horizontal axis and legend.
|
|
126
|
+
const limit = height / 2 - marker.layoutBounds.height / 2
|
|
127
|
+
return Math.min(centred, limit)
|
|
128
|
+
},
|
|
129
|
+
opacity: () => arrivalOf(state.arrivals, i).opacity,
|
|
130
|
+
x: () => arrivalOf(state.arrivals, i).shift,
|
|
131
|
+
})
|
|
132
|
+
return marker
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
const children: Node2D[] = []
|
|
136
|
+
if (state.label !== "") {
|
|
137
|
+
// Node2D space is y-up and rotation is clockwise, so -90° reads bottom-to-top.
|
|
138
|
+
children.push(
|
|
139
|
+
new RotatedBox({
|
|
140
|
+
angle: -90,
|
|
141
|
+
width: "hug",
|
|
142
|
+
height: "hug",
|
|
143
|
+
children: new Text({
|
|
144
|
+
...textOps.styleProps(state.axis.titleStyle),
|
|
145
|
+
text: state.label,
|
|
146
|
+
opacity: () => arrivalOf(state.arrivals, "label").opacity,
|
|
147
|
+
x: () => arrivalOf(state.arrivals, "label").shift,
|
|
148
|
+
}),
|
|
149
|
+
})
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
children.push(new Rect({ height: "fill", width: "hug", padding: { right: tickPad }, children: markers }))
|
|
153
|
+
row.push(new Rect({ flow: "horizontal", gap: titlePad, children }))
|
|
154
|
+
return asParts(row)
|
|
155
|
+
},
|
|
156
|
+
commands: {
|
|
157
|
+
/**
|
|
158
|
+
* Stagger the markers in bottom to top, then slide the caption in from the
|
|
159
|
+
* right after them. Its first frame arms them all: shifted right by `slide`
|
|
160
|
+
* and transparent.
|
|
161
|
+
*
|
|
162
|
+
* The timeline's duration is the **whole** entrance, not one marker's slide:
|
|
163
|
+
* the stagger and the caption are carved out of it rather than added on top.
|
|
164
|
+
* See {@link enterBudget} for why that distinction matters here.
|
|
165
|
+
*
|
|
166
|
+
* `stagger` is seconds between successive markers (derived when omitted);
|
|
167
|
+
* `slide` is the starting rightward offset in px.
|
|
168
|
+
*/
|
|
169
|
+
enter: {
|
|
170
|
+
args: {
|
|
171
|
+
stagger: Types.custom<number | undefined>(undefined),
|
|
172
|
+
slide: Types.number(SLIDE),
|
|
173
|
+
},
|
|
174
|
+
run: (timeline, state) => {
|
|
175
|
+
const { stagger, slide } = timeline.args
|
|
176
|
+
const count = state.markers.length
|
|
177
|
+
const captioned = state.label !== ""
|
|
178
|
+
const from: Arrival = { opacity: 0, shift: slide }
|
|
179
|
+
const keys = [...state.markers.keys(), ...(captioned ? ["label"] : [])]
|
|
180
|
+
const arm = timeline.animate(() => ({ arrivals: armed(keys, from) }))
|
|
181
|
+
if (count === 0) return arm
|
|
182
|
+
const { step, per, caption } = enterBudget(timeline.duration, count, captioned, stagger)
|
|
183
|
+
const ease = frameEase(timeline)
|
|
184
|
+
// Bottom marker leads: the column is top → bottom, so walk it in reverse.
|
|
185
|
+
const markers = state.markers.map((_, i) => ({ at: (count - 1 - i) * step, duration: per, piece: arrival(timeline, i, from, ease) }))
|
|
186
|
+
// Once every marker has landed, the caption slides in from the right.
|
|
187
|
+
const label = captioned ? [{ at: (count - 1) * step + per, duration: caption, piece: arrival(timeline, "label", from, ease) }] : []
|
|
188
|
+
return timeline.together([arm, beats(timeline, [...markers, ...label])])
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
export type VerticalAxis = NodeOf<typeof VerticalAxis>
|
|
@@ -0,0 +1,156 @@
|
|
|
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 Stroke,
|
|
10
|
+
type StrokeResolved,
|
|
11
|
+
type Vector2,
|
|
12
|
+
} from "@motionscript/sdk"
|
|
13
|
+
|
|
14
|
+
import { fresh } from "../kit/entries"
|
|
15
|
+
import { frameEase } from "../kit/motion"
|
|
16
|
+
import { loadPaints } from "../kit/paints"
|
|
17
|
+
import { SPOTLIGHT_DIM } from "../kit/shared"
|
|
18
|
+
import {
|
|
19
|
+
EMPTY_REGION_BOX,
|
|
20
|
+
clipPolylineToBox,
|
|
21
|
+
hasRegion,
|
|
22
|
+
type RegionBox,
|
|
23
|
+
} from "./shared"
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The prefix of `pts` revealed at fraction `p ∈ [0, 1]`, measuring the reveal
|
|
27
|
+
* by *cumulative arc length* so the line traces at a steady pace regardless of
|
|
28
|
+
* uneven point spacing. Returns the whole-point prefix plus one interpolated
|
|
29
|
+
* point partway along the active segment.
|
|
30
|
+
*/
|
|
31
|
+
function tracePoints(pts: Vector2[], p: number): Vector2[] {
|
|
32
|
+
if (p >= 1 || pts.length < 2) return pts
|
|
33
|
+
if (p <= 0) return []
|
|
34
|
+
|
|
35
|
+
const segLen: number[] = []
|
|
36
|
+
let total = 0
|
|
37
|
+
for (let i = 1; i < pts.length; i++) {
|
|
38
|
+
const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
|
|
39
|
+
segLen.push(d)
|
|
40
|
+
total += d
|
|
41
|
+
}
|
|
42
|
+
if (total === 0) return pts
|
|
43
|
+
|
|
44
|
+
let target = total * p
|
|
45
|
+
const out: Vector2[] = [pts[0]]
|
|
46
|
+
for (let i = 0; i < segLen.length; i++) {
|
|
47
|
+
if (target >= segLen[i]) {
|
|
48
|
+
out.push(pts[i + 1])
|
|
49
|
+
target -= segLen[i]
|
|
50
|
+
} else {
|
|
51
|
+
const t = segLen[i] === 0 ? 0 : target / segLen[i]
|
|
52
|
+
out.push({
|
|
53
|
+
x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
|
|
54
|
+
y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
|
|
55
|
+
})
|
|
56
|
+
break
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A single series line of a {@link LineChart}: one rounded polyline traced
|
|
64
|
+
* through pre-mapped pixel `points`, drawn as one {@link Graphics2D} command list.
|
|
65
|
+
* Sized and positioned by the parent, which shares the plot's coordinate frame.
|
|
66
|
+
*
|
|
67
|
+
* Two signals drive it: `progress` traces the line in
|
|
68
|
+
* (animated by `enter`) and `enabled` shows or hides it wholesale.
|
|
69
|
+
*/
|
|
70
|
+
export const ChartLine = component2d({
|
|
71
|
+
key: "chartLine",
|
|
72
|
+
state: {
|
|
73
|
+
/** The series polyline, already mapped into this node's local pixel space (origin at centre, y-up) by the parent {@link LineChart}. Ordered along x. */
|
|
74
|
+
points: fresh<Vector2[]>(() => []),
|
|
75
|
+
/** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
|
|
76
|
+
stroke: Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
77
|
+
/**
|
|
78
|
+
* Reveal of the line in `[0, 1]`: 0 hides it, 1 traces it fully from its left
|
|
79
|
+
* end to its right. Defaults to 1, so the paused editor canvas paints the line;
|
|
80
|
+
* `enter` drives it from 0 regardless of where it rests.
|
|
81
|
+
*/
|
|
82
|
+
progress: 1,
|
|
83
|
+
/** Whether this line is drawn at all: the switch the chart flips alongside its legend's per-series toggle. */
|
|
84
|
+
enabled: true,
|
|
85
|
+
/** A slice of the plot to spotlight, in this node's own pixel space: outside it the line drops to `dimOpacity`. */
|
|
86
|
+
spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
87
|
+
/** Opacity the out-of-region stretches fade to. */
|
|
88
|
+
dimOpacity: SPOTLIGHT_DIM,
|
|
89
|
+
},
|
|
90
|
+
load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
|
|
91
|
+
commands: {
|
|
92
|
+
/**
|
|
93
|
+
* Trace the line from `from` to `to` over the command — by default in from
|
|
94
|
+
* its left end to its right. `showSeries` and `hideSeries` are the same
|
|
95
|
+
* motion in opposite directions, so both are this with their ends stated.
|
|
96
|
+
*
|
|
97
|
+
* `enabled` rides along in the same value rather than being set before or
|
|
98
|
+
* after the command: a line is drawable for every `t` the trace covers and
|
|
99
|
+
* undrawn outside it, which is what makes arriving at a time backwards show
|
|
100
|
+
* what arriving forwards does.
|
|
101
|
+
*/
|
|
102
|
+
enter: {
|
|
103
|
+
args: { from: Types.number(0, { min: 0, max: 1 }), to: Types.number(1, { min: 0, max: 1 }) },
|
|
104
|
+
run: (timeline) => {
|
|
105
|
+
const { from, to } = timeline.args
|
|
106
|
+
const ease = frameEase(timeline)
|
|
107
|
+
return timeline.animate((t) => {
|
|
108
|
+
const progress = from + (to - from) * ease(t)
|
|
109
|
+
// Hidden only once it has fully retraced, so the last frame of a hide is
|
|
110
|
+
// the empty plot rather than a line that vanished a frame early.
|
|
111
|
+
return { progress, enabled: progress > 0 }
|
|
112
|
+
})
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
render: (ctx, state) => {
|
|
117
|
+
if (!state.enabled) return
|
|
118
|
+
const pts = state.points
|
|
119
|
+
if (pts.length < 2) return
|
|
120
|
+
|
|
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
|
|
126
|
+
const stroke = state.stroke
|
|
127
|
+
const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
|
|
128
|
+
|
|
129
|
+
const box = state.spotlight
|
|
130
|
+
if (!hasRegion(box)) {
|
|
131
|
+
ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
|
|
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))
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
export type ChartLine = NodeOf<typeof ChartLine>
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Graphics2D,
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
5
|
+
fillOps,
|
|
6
|
+
strokeOps,
|
|
7
|
+
type Fill,
|
|
8
|
+
type FillResolved,
|
|
9
|
+
type NodeOf,
|
|
10
|
+
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
11
|
+
type Node2D as _Node2D,
|
|
12
|
+
type Stroke,
|
|
13
|
+
type StrokeResolved,
|
|
14
|
+
} from "@motionscript/sdk"
|
|
15
|
+
|
|
16
|
+
import { fresh } from "../kit/entries"
|
|
17
|
+
import { loadPaints } from "../kit/paints"
|
|
18
|
+
import {
|
|
19
|
+
DEFAULT_CHART_THEME,
|
|
20
|
+
EMPTY_REGION_BOX,
|
|
21
|
+
hasRegion,
|
|
22
|
+
type RegionBox,
|
|
23
|
+
} from "./shared"
|
|
24
|
+
|
|
25
|
+
/**
|
|
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.
|
|
38
|
+
*/
|
|
39
|
+
export const ChartRegion = component2d({
|
|
40
|
+
key: "chartRegion",
|
|
41
|
+
state: {
|
|
42
|
+
/** The highlighted slice, already resolved into this node's local pixel space (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box draws nothing. */
|
|
43
|
+
box: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
44
|
+
/** Dashed rule marking each *bounded* edge of the box. */
|
|
45
|
+
stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_CHART_THEME.region.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
46
|
+
/** Wash filling the box, under the series lines. */
|
|
47
|
+
fill: Types.custom<Fill, FillResolved[]>(DEFAULT_CHART_THEME.region.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
48
|
+
/** Reveal of the annotation in `[0, 1]`, driven by the chart's own `regionReveal` so the rules and the series' dimming move as one. */
|
|
49
|
+
reveal: 0,
|
|
50
|
+
},
|
|
51
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.stroke] }),
|
|
52
|
+
render: (ctx, state) => {
|
|
53
|
+
const box = state.box
|
|
54
|
+
if (!hasRegion(box)) return
|
|
55
|
+
|
|
56
|
+
const r = Math.max(0, Math.min(1, state.reveal))
|
|
57
|
+
if (r <= 0) return
|
|
58
|
+
|
|
59
|
+
const width = box.right - box.left
|
|
60
|
+
const height = box.top - box.bottom
|
|
61
|
+
const cx = (box.left + box.right) / 2
|
|
62
|
+
const cy = (box.bottom + box.top) / 2
|
|
63
|
+
|
|
64
|
+
// Wash first, so the rules sit over their own tint.
|
|
65
|
+
if (state.fill.length > 0) {
|
|
66
|
+
ctx.draw(new Graphics2D().rect({ x: cx, y: cy, width, height }).fill(state.fill).opacity(r))
|
|
67
|
+
}
|
|
68
|
+
|
|
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
|
+
if (state.stroke.length === 0) return
|
|
72
|
+
|
|
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
|
+
if (box.xBounded) {
|
|
80
|
+
const half = (height / 2) * r
|
|
81
|
+
for (const x of [box.left, box.right]) rule([{ x, y: cy - half }, { x, y: cy + half }])
|
|
82
|
+
}
|
|
83
|
+
if (box.yBounded) {
|
|
84
|
+
const half = (width / 2) * r
|
|
85
|
+
for (const y of [box.bottom, box.top]) rule([{ x: cx - half, y }, { x: cx + half, y }])
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
export type ChartRegion = NodeOf<typeof ChartRegion>
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Rect,
|
|
3
|
+
Text,
|
|
4
|
+
Types,
|
|
5
|
+
component2d,
|
|
6
|
+
insetsOps,
|
|
7
|
+
textOps,
|
|
8
|
+
type NodeOf,
|
|
9
|
+
type Node2D,
|
|
10
|
+
} from "@motionscript/sdk";
|
|
11
|
+
|
|
12
|
+
import { asParts, fresh } from "../kit/entries";
|
|
13
|
+
import { armed, arrival, arrivalOf, arrivalsEntry, beats, frameEase, type Arrival } from "../kit/motion";
|
|
14
|
+
import {
|
|
15
|
+
DEFAULT_X_AXIS,
|
|
16
|
+
enterBudget,
|
|
17
|
+
type AxisConfig,
|
|
18
|
+
type Range,
|
|
19
|
+
} from "../kit/shared";
|
|
20
|
+
import { markerCaption } from "../kit/vertical-axis";
|
|
21
|
+
|
|
22
|
+
/** How far a marker starts above its slot when it enters, in px. */
|
|
23
|
+
const SLIDE = 24;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The horizontal axis of a {@link LineChart}: a **row of {@link Text} nodes**,
|
|
27
|
+
* one per marker value, running left-to-right. Pure layout — the mirror of the
|
|
28
|
+
* kit's `VerticalAxis`, including its bound (not snapshotted) marker text and
|
|
29
|
+
* positions, and its landed resting state.
|
|
30
|
+
*
|
|
31
|
+
* Markers are always placed **by value** across the range, whatever they say:
|
|
32
|
+
* `2026-03` sits three months along, not three slots along. That is what keeps
|
|
33
|
+
* it distinct from a bar chart's bottom axis, which centres a caption on each
|
|
34
|
+
* slot and has no scale at all — the two only look alike. What the values are
|
|
35
|
+
* captioned *as* is the chart's to decide, in `captions`.
|
|
36
|
+
*
|
|
37
|
+
* `enter` staggers the markers in **left-to-right**: each starts lifted a
|
|
38
|
+
* little above its slot and transparent, then fades and slides down into place.
|
|
39
|
+
*/
|
|
40
|
+
export const HorizontalAxis = component2d({
|
|
41
|
+
key: "horizontalAxis",
|
|
42
|
+
state: {
|
|
43
|
+
/** `[min, max]` of the horizontal scale the markers are positioned within. */
|
|
44
|
+
range: fresh<Range>(() => [0, 1]),
|
|
45
|
+
/** Tick values (in data units) to draw along the axis. */
|
|
46
|
+
markers: fresh<number[]>(() => []),
|
|
47
|
+
/**
|
|
48
|
+
* How each marker is captioned, one per entry of `markers` and in its order.
|
|
49
|
+
* Empty (the default), or short, captions a marker with its bare numeric
|
|
50
|
+
* reading; a chart whose x column names months or categories writes those.
|
|
51
|
+
*/
|
|
52
|
+
captions: fresh<string[]>(() => []),
|
|
53
|
+
/** Marker/caption text style, grid stroke, and gutters — see {@link AxisConfig}. */
|
|
54
|
+
axis: Types.custom<AxisConfig>(DEFAULT_X_AXIS),
|
|
55
|
+
/** Caption drawn centred in the gutter beneath the marker row; empty (the default) hides it. */
|
|
56
|
+
label: "",
|
|
57
|
+
arrivals: arrivalsEntry(),
|
|
58
|
+
},
|
|
59
|
+
// A plain node filled its parent; one made of its markers would hug them.
|
|
60
|
+
defaults: { width: "fill", height: "fill" },
|
|
61
|
+
children: (state) => {
|
|
62
|
+
const tickPad = insetsOps.resolve(state.axis.tickPadding).top;
|
|
63
|
+
const titlePad = insetsOps.resolve(state.axis.titlePadding).top;
|
|
64
|
+
|
|
65
|
+
const column: Node2D[] = [];
|
|
66
|
+
// The column's parent is the axis: its box is what the markers are placed in.
|
|
67
|
+
const box = () => (column[0]?.parent as Node2D | null | undefined)?.layoutBounds ?? { width: 0, height: 0 };
|
|
68
|
+
/** The live tick values, left → right — re-read every frame, not snapshotted. */
|
|
69
|
+
const sorted = () => [...state.markers].sort((a, b) => a - b);
|
|
70
|
+
/** Tick `i`'s fraction across the live `range`, 0 at the left edge. */
|
|
71
|
+
const fracAt = (value: number | undefined) => {
|
|
72
|
+
if (value === undefined) return 0;
|
|
73
|
+
const [min, max] = state.range;
|
|
74
|
+
return max <= min ? 0 : (value - min) / (max - min);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const markers = state.markers.map((_, i) => {
|
|
78
|
+
const marker: Node2D = new Text({
|
|
79
|
+
...textOps.styleProps(state.axis.labelStyle),
|
|
80
|
+
text: () => {
|
|
81
|
+
const value = sorted()[i];
|
|
82
|
+
return value === undefined ? "" : markerCaption(state, value);
|
|
83
|
+
},
|
|
84
|
+
x: () => {
|
|
85
|
+
const width = box().width;
|
|
86
|
+
const centred = (fracAt(sorted()[i]) - 0.5) * width;
|
|
87
|
+
// Held inside the axis's right edge, which is also the *chart's*: the
|
|
88
|
+
// last marker is centred on the last grid line, so half of `2026-06`
|
|
89
|
+
// — or of `3300` — would be drawn outside the node's box and outside
|
|
90
|
+
// the rectangle the editor draws around it. Only the right edge is
|
|
91
|
+
// held, because the left one has the vertical axis's whole gutter
|
|
92
|
+
// beyond it, and a marker overhanging into that is inside the chart.
|
|
93
|
+
const limit = width / 2 - marker.layoutBounds.width / 2;
|
|
94
|
+
return Math.min(centred, limit);
|
|
95
|
+
},
|
|
96
|
+
opacity: () => arrivalOf(state.arrivals, i).opacity,
|
|
97
|
+
y: () => arrivalOf(state.arrivals, i).shift,
|
|
98
|
+
});
|
|
99
|
+
return marker;
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const children: Node2D[] = [
|
|
103
|
+
new Rect({ width: "fill", height: "hug", padding: { top: tickPad }, children: markers }),
|
|
104
|
+
];
|
|
105
|
+
if (state.label !== "") {
|
|
106
|
+
children.push(
|
|
107
|
+
new Text({
|
|
108
|
+
...textOps.styleProps(state.axis.titleStyle),
|
|
109
|
+
text: state.label,
|
|
110
|
+
opacity: () => arrivalOf(state.arrivals, "label").opacity,
|
|
111
|
+
y: () => arrivalOf(state.arrivals, "label").shift,
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
column.push(new Rect({ flow: "vertical", gap: titlePad, height: "hug", width: "fill", children }));
|
|
117
|
+
return asParts(column);
|
|
118
|
+
},
|
|
119
|
+
commands: {
|
|
120
|
+
/**
|
|
121
|
+
* Stagger the markers in left to right, then slide the caption in after
|
|
122
|
+
* them. Its first frame arms them all: lifted by `slide` and transparent.
|
|
123
|
+
*
|
|
124
|
+
* The timeline's duration is the **whole** entrance — see
|
|
125
|
+
* `VerticalAxis.enter` and {@link enterBudget}.
|
|
126
|
+
*/
|
|
127
|
+
enter: {
|
|
128
|
+
args: {
|
|
129
|
+
stagger: Types.custom<number | undefined>(undefined),
|
|
130
|
+
slide: Types.number(SLIDE),
|
|
131
|
+
},
|
|
132
|
+
run: (timeline, state) => {
|
|
133
|
+
const { stagger, slide } = timeline.args;
|
|
134
|
+
const count = state.markers.length;
|
|
135
|
+
const captioned = state.label !== "";
|
|
136
|
+
const from: Arrival = { opacity: 0, shift: slide };
|
|
137
|
+
const keys = [...state.markers.keys(), ...(captioned ? ["label"] : [])];
|
|
138
|
+
const arm = timeline.animate(() => ({ arrivals: armed(keys, from) }));
|
|
139
|
+
if (count === 0) return arm;
|
|
140
|
+
const { step, per, caption } = enterBudget(timeline.duration, count, captioned, stagger);
|
|
141
|
+
const ease = frameEase(timeline);
|
|
142
|
+
// Left marker leads: the row is already left → right.
|
|
143
|
+
const markers = state.markers.map((_, i) => ({ at: i * step, duration: per, piece: arrival(timeline, i, from, ease) }));
|
|
144
|
+
const label = captioned ? [{ at: (count - 1) * step + per, duration: caption, piece: arrival(timeline, "label", from, ease) }] : [];
|
|
145
|
+
return timeline.together([arm, beats(timeline, [...markers, ...label])]);
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
export type HorizontalAxis = NodeOf<typeof HorizontalAxis>;
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
export { LineChart, lineChartDomains } from "./line-chart"
|
|
14
|
+
export type { LineChartDomains, LineChartProps, LineChartValues } from "./line-chart"
|
|
15
|
+
export { ChartRegion } from "./chart-region"
|
|
16
|
+
export { ChartLine } from "./chart-line"
|
|
17
|
+
export { HorizontalAxis } from "./horizontal-axis"
|
|
18
|
+
export {
|
|
19
|
+
DEFAULT_CHART_THEME,
|
|
20
|
+
EMPTY_REGION_BOX,
|
|
21
|
+
clipPolylineToBox,
|
|
22
|
+
hasRegion,
|
|
23
|
+
lerpChartTheme,
|
|
24
|
+
resolveChartTheme,
|
|
25
|
+
resolveRegionBox,
|
|
26
|
+
resolveYScale,
|
|
27
|
+
seriesPoints,
|
|
28
|
+
yExtent,
|
|
29
|
+
} from "./shared"
|
|
30
|
+
export type {
|
|
31
|
+
ChartConfig,
|
|
32
|
+
ChartTheme,
|
|
33
|
+
LineSeries,
|
|
34
|
+
PlotRegion,
|
|
35
|
+
RegionBox,
|
|
36
|
+
RegionConfig,
|
|
37
|
+
} from "./shared"
|
|
38
|
+
export { formatInstant, parseInstant, resolveXScale } from "./x-scale"
|
|
39
|
+
export type { XScale, XScaleKind } from "./x-scale"
|