@motionscript/charts 0.1.0-alpha.3 → 0.1.0-alpha.6
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 +63 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +99 -0
- package/dist/bar-chart/bar-chart.d.ts +93 -231
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +378 -565
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +24 -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 +36 -70
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +156 -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 +6 -6
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +6 -6
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
- package/dist/browser/chunks/chunk-XROBIRQK.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 +3 -2
- package/dist/engine.d.ts +8 -6
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +8 -6
- package/dist/engine.js.map +1 -1
- package/dist/index.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 +8 -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 +23 -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 +79 -88
- 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 +59 -8
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +105 -10
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +36 -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 +27 -53
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +89 -120
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +14 -14
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +26 -64
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +32 -56
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +85 -123
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +2 -2
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +1 -1
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +111 -311
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +403 -639
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +6 -6
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +22 -7
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +1 -1
- package/dist/line-chart/x-scale.js +1 -1
- 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 +77 -134
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +199 -268
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +54 -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 +142 -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 +3 -3
- package/dist/pie-chart/shared.js +3 -3
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +6 -5
- package/registry.json +23 -23
- package/src/bar-chart/bar-chart.ts +534 -814
- package/src/bar-chart/category-axis.ts +162 -177
- package/src/bar-chart/chart-bars.ts +288 -310
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +28 -26
- package/src/index.ts +5 -5
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +50 -61
- package/src/kit/chart-canvas.ts +122 -157
- package/src/kit/chart-legend.ts +229 -250
- 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 -675
- package/src/kit/vertical-axis.ts +194 -224
- package/src/line-chart/chart-line.ts +181 -208
- package/src/line-chart/chart-region.ts +106 -135
- package/src/line-chart/horizontal-axis.ts +166 -202
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +585 -929
- package/src/line-chart/shared.ts +473 -459
- package/src/line-chart/x-scale.ts +367 -367
- package/src/nodes.ts +18 -20
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +293 -381
- package/src/pie-chart/pie-legend.ts +169 -159
- package/src/pie-chart/pie-ring.ts +848 -903
- package/src/pie-chart/shared.ts +490 -490
- package/dist/browser/chunks/chunk-RMHQ6GYU.js +0 -2
- package/dist/browser/chunks/chunk-RMHQ6GYU.js.map +0 -7
|
@@ -1,208 +1,181 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
type
|
|
10
|
-
type
|
|
11
|
-
type
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
} from "
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
}
|
|
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
|
+
type Vector2,
|
|
13
|
+
} from "@motionscript/sdk"
|
|
14
|
+
|
|
15
|
+
import { fresh } from "../kit/entries"
|
|
16
|
+
import { frameEase } from "../kit/motion"
|
|
17
|
+
import { loadPaints } from "../kit/paints"
|
|
18
|
+
import { SPOTLIGHT_DIM } from "../kit/shared"
|
|
19
|
+
import {
|
|
20
|
+
EMPTY_REGION_BOX,
|
|
21
|
+
clipPolylineToBox,
|
|
22
|
+
hasRegion,
|
|
23
|
+
type RegionBox,
|
|
24
|
+
} from "./shared"
|
|
25
|
+
|
|
26
|
+
export interface ChartLineProps extends Node2DProps {
|
|
27
|
+
/**
|
|
28
|
+
* The series polyline, already mapped into this node's local pixel space
|
|
29
|
+
* (origin at centre, y-up) by the parent {@link LineChart}. Ordered along x.
|
|
30
|
+
*/
|
|
31
|
+
points: Vector2[]
|
|
32
|
+
/** Line colour + weight (+ optional dash/cap/join) — the per-series stroke. */
|
|
33
|
+
stroke: Stroke
|
|
34
|
+
/**
|
|
35
|
+
* Reveal of the line in `[0, 1]`: 0 hides it, 1 traces it fully from its left
|
|
36
|
+
* end to its right.
|
|
37
|
+
*
|
|
38
|
+
* Defaults to **1**, unlike the standalone chart this was ported from: in an
|
|
39
|
+
* editor a node's resting state is what the paused editor canvas paints, so a
|
|
40
|
+
* chart that only appeared once a command had run would edit blind. {@link
|
|
41
|
+
* ChartLine.enter} drives it from 0 regardless of where it rests, so the
|
|
42
|
+
* animation is unchanged.
|
|
43
|
+
*/
|
|
44
|
+
progress: number
|
|
45
|
+
/**
|
|
46
|
+
* Whether this line is drawn at all. `false` hides it outright — the switch
|
|
47
|
+
* the chart flips alongside its legend's per-series toggle.
|
|
48
|
+
*/
|
|
49
|
+
enabled: boolean
|
|
50
|
+
/**
|
|
51
|
+
* A slice of the plot to spotlight, in this node's own pixel space. When set
|
|
52
|
+
* to a real box, the stretches of the line *outside* it drop to
|
|
53
|
+
* {@link ChartLineProps.dimOpacity} while the stretches inside stay full.
|
|
54
|
+
*/
|
|
55
|
+
spotlight: RegionBox
|
|
56
|
+
/** Opacity the out-of-region stretches fade to. */
|
|
57
|
+
dimOpacity: number
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The prefix of `pts` revealed at fraction `p ∈ [0, 1]`, measuring the reveal
|
|
62
|
+
* by *cumulative arc length* so the line traces at a steady pace regardless of
|
|
63
|
+
* uneven point spacing. Returns the whole-point prefix plus one interpolated
|
|
64
|
+
* point partway along the active segment.
|
|
65
|
+
*/
|
|
66
|
+
function tracePoints(pts: Vector2[], p: number): Vector2[] {
|
|
67
|
+
if (p >= 1 || pts.length < 2) return pts
|
|
68
|
+
if (p <= 0) return []
|
|
69
|
+
|
|
70
|
+
const segLen: number[] = []
|
|
71
|
+
let total = 0
|
|
72
|
+
for (let i = 1; i < pts.length; i++) {
|
|
73
|
+
const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
|
|
74
|
+
segLen.push(d)
|
|
75
|
+
total += d
|
|
76
|
+
}
|
|
77
|
+
if (total === 0) return pts
|
|
78
|
+
|
|
79
|
+
let target = total * p
|
|
80
|
+
const out: Vector2[] = [pts[0]]
|
|
81
|
+
for (let i = 0; i < segLen.length; i++) {
|
|
82
|
+
if (target >= segLen[i]) {
|
|
83
|
+
out.push(pts[i + 1])
|
|
84
|
+
target -= segLen[i]
|
|
85
|
+
} else {
|
|
86
|
+
const t = segLen[i] === 0 ? 0 : target / segLen[i]
|
|
87
|
+
out.push({
|
|
88
|
+
x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
|
|
89
|
+
y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
|
|
90
|
+
})
|
|
91
|
+
break
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return out
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A single series line of a {@link LineChart}: one rounded polyline traced
|
|
99
|
+
* through pre-mapped pixel `points`, drawn as one {@link Graphics2D} command list.
|
|
100
|
+
* Sized and positioned by the parent, which shares the plot's coordinate frame.
|
|
101
|
+
*
|
|
102
|
+
* Two signals drive it: {@link ChartLineProps.progress} traces the line in
|
|
103
|
+
* (animated by `enter`) and `enabled` shows or hides it wholesale.
|
|
104
|
+
*/
|
|
105
|
+
export const ChartLine = component2d({
|
|
106
|
+
key: "chartLine",
|
|
107
|
+
state: {
|
|
108
|
+
points: fresh<Vector2[]>(() => []),
|
|
109
|
+
stroke: Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
110
|
+
progress: 1,
|
|
111
|
+
enabled: true,
|
|
112
|
+
spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
113
|
+
dimOpacity: SPOTLIGHT_DIM,
|
|
114
|
+
},
|
|
115
|
+
load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
|
|
116
|
+
commands: {
|
|
117
|
+
/**
|
|
118
|
+
* Trace the line from `from` to `to` over the command — by default in from
|
|
119
|
+
* its left end to its right. `showSeries` and `hideSeries` are the same
|
|
120
|
+
* motion in opposite directions, so both are this with their ends stated.
|
|
121
|
+
*
|
|
122
|
+
* `enabled` rides along in the same value rather than being set before or
|
|
123
|
+
* after the command: a line is drawable for every `t` the trace covers and
|
|
124
|
+
* undrawn outside it, which is what makes arriving at a time backwards show
|
|
125
|
+
* what arriving forwards does.
|
|
126
|
+
*/
|
|
127
|
+
enter: {
|
|
128
|
+
args: { from: Types.number(0, { min: 0, max: 1 }), to: Types.number(1, { min: 0, max: 1 }) },
|
|
129
|
+
run: (timeline) => {
|
|
130
|
+
const { from, to } = timeline.args
|
|
131
|
+
const ease = frameEase(timeline)
|
|
132
|
+
return timeline.animate((t) => {
|
|
133
|
+
const progress = from + (to - from) * ease(t)
|
|
134
|
+
// Hidden only once it has fully retraced, so the last frame of a hide is
|
|
135
|
+
// the empty plot rather than a line that vanished a frame early.
|
|
136
|
+
return { progress, enabled: progress > 0 }
|
|
137
|
+
})
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
render: (ctx, state) => {
|
|
142
|
+
if (!state.enabled) return
|
|
143
|
+
const pts = state.points
|
|
144
|
+
if (pts.length < 2) return
|
|
145
|
+
|
|
146
|
+
const p = Math.max(0, Math.min(1, state.progress))
|
|
147
|
+
if (p <= 0) return
|
|
148
|
+
|
|
149
|
+
const drawn = tracePoints(pts, p)
|
|
150
|
+
if (drawn.length < 2) return
|
|
151
|
+
const stroke = state.stroke
|
|
152
|
+
const radius = (stroke[0]?.weight ?? 0) * 2 // round the joins
|
|
153
|
+
|
|
154
|
+
const box = state.spotlight
|
|
155
|
+
if (!hasRegion(box)) {
|
|
156
|
+
ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke))
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Spotlit: the whole line goes down to `dimOpacity`, then the stretches
|
|
161
|
+
// inside the region are re-drawn over it at full strength. Two passes rather
|
|
162
|
+
// than one dim + one bright *complementary* pair, so the bright run has
|
|
163
|
+
// continuous geometry through the region and its joins round the way an
|
|
164
|
+
// unspotlit line's do — and so the boundary lands exactly on the region edge.
|
|
165
|
+
ctx.draw(
|
|
166
|
+
new Graphics2D()
|
|
167
|
+
.line({ points: drawn, radius })
|
|
168
|
+
.stroke(stroke)
|
|
169
|
+
.opacity(Math.max(0, Math.min(1, state.dimOpacity)))
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
// One `draw` per run: shapes chained before a `stroke()` are combined into a
|
|
173
|
+
// single surface, and stroking a union of disjoint open paths doesn't paint
|
|
174
|
+
// — so a line that leaves the region and comes back would lose both halves.
|
|
175
|
+
for (const run of clipPolylineToBox(drawn, box)) {
|
|
176
|
+
ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
export type ChartLine = NodeOf<typeof ChartLine>
|
|
@@ -1,135 +1,106 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
fillOps,
|
|
6
|
-
strokeOps,
|
|
7
|
-
|
|
8
|
-
type
|
|
9
|
-
type
|
|
10
|
-
type
|
|
11
|
-
type
|
|
12
|
-
type
|
|
13
|
-
type
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* the
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const stroke = this.stroke as StrokeResolved[]
|
|
108
|
-
if (stroke.length === 0) return
|
|
109
|
-
|
|
110
|
-
// One `draw` per rule rather than four line ops under a single paint:
|
|
111
|
-
// shapes chained before a `stroke()` are combined into one surface, and
|
|
112
|
-
// stroking a union of disjoint open paths doesn't paint.
|
|
113
|
-
const rule = (points: { x: number; y: number }[]) =>
|
|
114
|
-
ctx.draw(new Graphics2D().line({ points }).stroke(this.stroke).opacity(r))
|
|
115
|
-
|
|
116
|
-
if (box.xBounded) {
|
|
117
|
-
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
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (box.yBounded) {
|
|
126
|
-
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
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
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 Node2DProps,
|
|
13
|
+
type Stroke,
|
|
14
|
+
type StrokeResolved,
|
|
15
|
+
} from "@motionscript/sdk"
|
|
16
|
+
|
|
17
|
+
import { fresh } from "../kit/entries"
|
|
18
|
+
import { loadPaints } from "../kit/paints"
|
|
19
|
+
import {
|
|
20
|
+
DEFAULT_CHART_THEME,
|
|
21
|
+
EMPTY_REGION_BOX,
|
|
22
|
+
hasRegion,
|
|
23
|
+
type RegionBox,
|
|
24
|
+
} from "./shared"
|
|
25
|
+
|
|
26
|
+
export interface ChartRegionProps extends Node2DProps {
|
|
27
|
+
/**
|
|
28
|
+
* The highlighted slice, already resolved into this node's local pixel space
|
|
29
|
+
* (origin at centre, y-up) by the parent {@link LineChart}. A degenerate box
|
|
30
|
+
* draws nothing.
|
|
31
|
+
*/
|
|
32
|
+
box: RegionBox
|
|
33
|
+
/** Dashed rule marking each *bounded* edge of the box. */
|
|
34
|
+
stroke: Stroke
|
|
35
|
+
/** Wash filling the box, under the series lines. */
|
|
36
|
+
fill: Fill
|
|
37
|
+
/**
|
|
38
|
+
* Reveal of the annotation in `[0, 1]`: 0 hides it, 1 draws it fully. Driven
|
|
39
|
+
* by the chart's own `regionReveal`, so the rules and the series' dimming move
|
|
40
|
+
* as one.
|
|
41
|
+
*/
|
|
42
|
+
reveal: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The **region call-out** of a {@link LineChart}: a washed rectangle over the
|
|
47
|
+
* plot with a dashed rule down each edge the author actually bounded. Sized and
|
|
48
|
+
* positioned entirely from the pre-mapped {@link ChartRegionProps.box} rather
|
|
49
|
+
* than its own box — the region already carries the plot geometry.
|
|
50
|
+
*
|
|
51
|
+
* It draws only the *marking*; fading the series outside the region is
|
|
52
|
+
* {@link ChartLine}'s job (it clips its own polyline against the same box), so
|
|
53
|
+
* the two can't disagree about where the boundary is.
|
|
54
|
+
*
|
|
55
|
+
* Only bounded edges get a rule: a region given just an `x` range spans the full
|
|
56
|
+
* plot height, so its top and bottom edges are the plot's own border, not a data
|
|
57
|
+
* bound — drawing dashes there would assert a limit the author didn't set.
|
|
58
|
+
*/
|
|
59
|
+
export const ChartRegion = component2d({
|
|
60
|
+
key: "chartRegion",
|
|
61
|
+
state: {
|
|
62
|
+
box: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
63
|
+
stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_CHART_THEME.region.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
64
|
+
fill: Types.custom<Fill, FillResolved[]>(DEFAULT_CHART_THEME.region.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
65
|
+
reveal: 0,
|
|
66
|
+
},
|
|
67
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.stroke] }),
|
|
68
|
+
render: (ctx, state) => {
|
|
69
|
+
const box = state.box
|
|
70
|
+
if (!hasRegion(box)) return
|
|
71
|
+
|
|
72
|
+
const r = Math.max(0, Math.min(1, state.reveal))
|
|
73
|
+
if (r <= 0) return
|
|
74
|
+
|
|
75
|
+
const width = box.right - box.left
|
|
76
|
+
const height = box.top - box.bottom
|
|
77
|
+
const cx = (box.left + box.right) / 2
|
|
78
|
+
const cy = (box.bottom + box.top) / 2
|
|
79
|
+
|
|
80
|
+
// Wash first, so the rules sit over their own tint.
|
|
81
|
+
if (state.fill.length > 0) {
|
|
82
|
+
ctx.draw(new Graphics2D().rect({ x: cx, y: cy, width, height }).fill(state.fill).opacity(r))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// One rule per bounded edge, growing from the region's centre outward as
|
|
86
|
+
// `reveal` climbs — the same "trace it in" beat as the series lines.
|
|
87
|
+
if (state.stroke.length === 0) return
|
|
88
|
+
|
|
89
|
+
// One `draw` per rule rather than four line ops under a single paint:
|
|
90
|
+
// shapes chained before a `stroke()` are combined into one surface, and
|
|
91
|
+
// stroking a union of disjoint open paths doesn't paint.
|
|
92
|
+
const rule = (points: { x: number; y: number }[]) =>
|
|
93
|
+
ctx.draw(new Graphics2D().line({ points }).stroke(state.stroke).opacity(r))
|
|
94
|
+
|
|
95
|
+
if (box.xBounded) {
|
|
96
|
+
const half = (height / 2) * r
|
|
97
|
+
for (const x of [box.left, box.right]) rule([{ x, y: cy - half }, { x, y: cy + half }])
|
|
98
|
+
}
|
|
99
|
+
if (box.yBounded) {
|
|
100
|
+
const half = (width / 2) * r
|
|
101
|
+
for (const y of [box.bottom, box.top]) rule([{ x: cx - half, y }, { x: cx + half, y }])
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
export type ChartRegion = NodeOf<typeof ChartRegion>
|