@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.
- package/CHANGELOG.md +6 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +99 -99
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/{chunk-XROBIRQK.js → chunk-TJNHLQGV.js} +1 -1
- package/dist/browser/chunks/chunk-TJNHLQGV.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/cartesian.js.map +1 -1
- package/dist/kit/chart-border.js.map +1 -1
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.js.map +1 -1
- package/dist/kit/motion.js.map +1 -1
- package/dist/kit/paints.js.map +1 -1
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +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.js.map +1 -1
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +23 -23
- package/src/bar-chart/bar-chart.ts +542 -542
- package/src/bar-chart/category-axis.ts +162 -162
- package/src/bar-chart/chart-bars.ts +287 -287
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +28 -28
- package/src/index.ts +5 -5
- package/src/kit/cartesian.ts +81 -81
- package/src/kit/chart-border.ts +50 -50
- package/src/kit/chart-canvas.ts +122 -122
- package/src/kit/chart-legend.ts +229 -229
- package/src/kit/entries.ts +20 -20
- package/src/kit/motion.ts +90 -90
- package/src/kit/paints.ts +15 -15
- package/src/kit/shared.ts +782 -782
- package/src/kit/vertical-axis.ts +194 -194
- package/src/line-chart/chart-line.ts +156 -156
- package/src/line-chart/chart-region.ts +90 -90
- package/src/line-chart/horizontal-axis.ts +151 -151
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +598 -598
- package/src/line-chart/shared.ts +473 -473
- package/src/line-chart/x-scale.ts +382 -382
- package/src/nodes.ts +18 -18
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +296 -296
- package/src/pie-chart/pie-legend.ts +169 -169
- package/src/pie-chart/pie-ring.ts +848 -848
- package/src/pie-chart/shared.ts +496 -496
- package/dist/browser/chunks/chunk-XROBIRQK.js.map +0 -7
- package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
|
@@ -1,156 +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>
|
|
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>
|
|
@@ -1,90 +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>
|
|
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>
|