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