@motionscript/charts 0.1.0 → 0.1.1-alpha.1
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 +38 -0
- package/README.md +98 -28
- package/dist/bar-chart/bar-chart.d.ts +242 -119
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +231 -260
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +16 -38
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +59 -68
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +60 -91
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +88 -125
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +5 -13
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +2 -9
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +40 -132
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +70 -158
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-R3Q5EE6E.js +2 -0
- package/dist/browser/chunks/chunk-R3Q5EE6E.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +4 -4
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/engine.d.ts +8 -17
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +8 -17
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/kit/cartesian.d.ts +49 -35
- package/dist/kit/cartesian.d.ts.map +1 -1
- package/dist/kit/cartesian.js +80 -42
- package/dist/kit/cartesian.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +37 -40
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +53 -47
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +16 -59
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +38 -76
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.d.ts +2 -0
- package/dist/kit/entries.d.ts.map +1 -1
- package/dist/kit/entries.js +2 -0
- package/dist/kit/entries.js.map +1 -1
- package/dist/kit/horizontal-axis.d.ts +77 -0
- package/dist/kit/horizontal-axis.d.ts.map +1 -0
- package/dist/kit/horizontal-axis.js +99 -0
- package/dist/kit/horizontal-axis.js.map +1 -0
- package/dist/kit/motion.d.ts +16 -3
- package/dist/kit/motion.d.ts.map +1 -1
- package/dist/kit/motion.js +15 -1
- package/dist/kit/motion.js.map +1 -1
- package/dist/{pie-chart → kit}/pie-legend.d.ts +14 -29
- package/dist/kit/pie-legend.d.ts.map +1 -0
- package/dist/kit/pie-legend.js +93 -0
- package/dist/kit/pie-legend.js.map +1 -0
- package/dist/kit/polar.d.ts +31 -0
- package/dist/kit/polar.d.ts.map +1 -0
- package/dist/kit/polar.js +33 -0
- package/dist/kit/polar.js.map +1 -0
- package/dist/kit/shared.d.ts +60 -214
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +91 -291
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/style.d.ts +254 -0
- package/dist/kit/style.d.ts.map +1 -0
- package/dist/kit/style.js +197 -0
- package/dist/kit/style.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +36 -45
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +69 -81
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +107 -40
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +172 -89
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +14 -27
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +13 -28
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/index.d.ts +5 -18
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +3 -16
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +272 -119
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +309 -257
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +73 -130
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +195 -156
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +16 -55
- package/dist/line-chart/x-scale.d.ts.map +1 -1
- package/dist/line-chart/x-scale.js +44 -111
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.d.ts +3 -13
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +9 -13
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +3 -15
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -11
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +147 -85
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +65 -86
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +65 -196
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +154 -292
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +78 -172
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +54 -177
- package/dist/pie-chart/shared.js.map +1 -1
- package/dist/radar-chart/index.d.ts +5 -0
- package/dist/radar-chart/index.d.ts.map +1 -0
- package/dist/radar-chart/index.js +5 -0
- package/dist/radar-chart/index.js.map +1 -0
- package/dist/radar-chart/radar-chart.d.ts +218 -0
- package/dist/radar-chart/radar-chart.d.ts.map +1 -0
- package/dist/radar-chart/radar-chart.js +224 -0
- package/dist/radar-chart/radar-chart.js.map +1 -0
- package/dist/radar-chart/radar-grid.d.ts +43 -0
- package/dist/radar-chart/radar-grid.d.ts.map +1 -0
- package/dist/radar-chart/radar-grid.js +61 -0
- package/dist/radar-chart/radar-grid.js.map +1 -0
- package/dist/radar-chart/radar-series.d.ts +59 -0
- package/dist/radar-chart/radar-series.d.ts.map +1 -0
- package/dist/radar-chart/radar-series.js +71 -0
- package/dist/radar-chart/radar-series.js.map +1 -0
- package/dist/radar-chart/shared.d.ts +73 -0
- package/dist/radar-chart/shared.d.ts.map +1 -0
- package/dist/radar-chart/shared.js +123 -0
- package/dist/radar-chart/shared.js.map +1 -0
- package/dist/radial-chart/index.d.ts +6 -0
- package/dist/radial-chart/index.d.ts.map +1 -0
- package/dist/radial-chart/index.js +4 -0
- package/dist/radial-chart/index.js.map +1 -0
- package/dist/radial-chart/radial-chart.d.ts +216 -0
- package/dist/radial-chart/radial-chart.d.ts.map +1 -0
- package/dist/radial-chart/radial-chart.js +174 -0
- package/dist/radial-chart/radial-chart.js.map +1 -0
- package/dist/radial-chart/radial-rings.d.ts +121 -0
- package/dist/radial-chart/radial-rings.d.ts.map +1 -0
- package/dist/radial-chart/radial-rings.js +273 -0
- package/dist/radial-chart/radial-rings.js.map +1 -0
- package/dist/radial-chart/shared.d.ts +73 -0
- package/dist/radial-chart/shared.d.ts.map +1 -0
- package/dist/radial-chart/shared.js +79 -0
- package/dist/radial-chart/shared.js.map +1 -0
- package/package.json +4 -4
- package/registry.json +21 -6
- package/src/bar-chart/bar-chart.ts +274 -315
- package/src/bar-chart/category-axis.ts +53 -88
- package/src/bar-chart/chart-bars.ts +108 -205
- package/src/bar-chart/index.ts +8 -19
- package/src/bar-chart/shared.ts +81 -309
- package/src/engine.ts +8 -17
- package/src/index.ts +2 -0
- package/src/kit/cartesian.ts +118 -55
- package/src/kit/chart-canvas.ts +50 -80
- package/src/kit/chart-legend.ts +36 -97
- package/src/kit/entries.ts +4 -1
- package/src/kit/horizontal-axis.ts +114 -0
- package/src/kit/motion.ts +18 -2
- package/src/{pie-chart → kit}/pie-legend.ts +25 -65
- package/src/kit/polar.ts +54 -0
- package/src/kit/shared.ts +104 -487
- package/src/kit/style.ts +257 -0
- package/src/kit/vertical-axis.ts +81 -115
- package/src/line-chart/chart-line.ts +185 -91
- package/src/line-chart/chart-region.ts +14 -31
- package/src/line-chart/index.ts +14 -22
- package/src/line-chart/line-chart.ts +377 -315
- package/src/line-chart/shared.ts +229 -304
- package/src/line-chart/x-scale.ts +44 -121
- package/src/nodes.ts +9 -13
- package/src/pie-chart/index.ts +3 -35
- package/src/pie-chart/pie-chart.ts +83 -134
- package/src/pie-chart/pie-ring.ts +162 -368
- package/src/pie-chart/shared.ts +92 -379
- package/src/radar-chart/index.ts +4 -0
- package/src/radar-chart/radar-chart.ts +287 -0
- package/src/radar-chart/radar-grid.ts +78 -0
- package/src/radar-chart/radar-series.ts +88 -0
- package/src/radar-chart/shared.ts +146 -0
- package/src/radial-chart/index.ts +5 -0
- package/src/radial-chart/radial-chart.ts +210 -0
- package/src/radial-chart/radial-rings.ts +328 -0
- package/src/radial-chart/shared.ts +114 -0
- package/dist/browser/chunks/chunk-TJNHLQGV.js +0 -2
- package/dist/browser/chunks/chunk-TJNHLQGV.js.map +0 -7
- package/dist/kit/chart-border.d.ts +0 -27
- package/dist/kit/chart-border.d.ts.map +0 -1
- package/dist/kit/chart-border.js +0 -28
- package/dist/kit/chart-border.js.map +0 -1
- package/dist/line-chart/horizontal-axis.d.ts +0 -79
- package/dist/line-chart/horizontal-axis.d.ts.map +0 -1
- package/dist/line-chart/horizontal-axis.js +0 -129
- package/dist/line-chart/horizontal-axis.js.map +0 -1
- package/dist/pie-chart/pie-legend.d.ts.map +0 -1
- package/dist/pie-chart/pie-legend.js +0 -117
- package/dist/pie-chart/pie-legend.js.map +0 -1
- package/src/kit/chart-border.ts +0 -50
- package/src/line-chart/horizontal-axis.ts +0 -151
|
@@ -1,103 +1,98 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Graphics2D,
|
|
3
3
|
Types,
|
|
4
|
+
clamp,
|
|
4
5
|
component2d,
|
|
6
|
+
fillOps,
|
|
5
7
|
strokeOps,
|
|
8
|
+
type Fill,
|
|
9
|
+
type FillResolved,
|
|
6
10
|
type NodeOf,
|
|
7
|
-
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
11
|
+
// Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
|
|
8
12
|
type Node2D as _Node2D,
|
|
13
|
+
type Node2DProps as _Node2DProps,
|
|
14
|
+
type PathCommand,
|
|
15
|
+
type RenderContext,
|
|
9
16
|
type Stroke,
|
|
10
17
|
type StrokeResolved,
|
|
11
18
|
type Vector2,
|
|
12
19
|
} from "@motionscript/sdk"
|
|
13
20
|
|
|
21
|
+
import { fade } from "../kit/cartesian"
|
|
14
22
|
import { fresh } from "../kit/entries"
|
|
15
23
|
import { frameEase } from "../kit/motion"
|
|
16
24
|
import { loadPaints } from "../kit/paints"
|
|
17
25
|
import { SPOTLIGHT_DIM } from "../kit/shared"
|
|
18
26
|
import {
|
|
19
27
|
EMPTY_REGION_BOX,
|
|
28
|
+
clipPolygonToBox,
|
|
20
29
|
clipPolylineToBox,
|
|
30
|
+
cutAt,
|
|
31
|
+
curvePoints,
|
|
21
32
|
hasRegion,
|
|
33
|
+
type LineCurve,
|
|
22
34
|
type RegionBox,
|
|
23
35
|
} from "./shared"
|
|
24
36
|
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
|
37
|
+
/** The x a trace at `progress` has reached, across `points` ordered along x. */
|
|
38
|
+
export function traceX(points: readonly Vector2[], progress: number): number {
|
|
39
|
+
if (points.length === 0) return 0
|
|
40
|
+
const first = points[0].x
|
|
41
|
+
const last = points[points.length - 1].x
|
|
42
|
+
return progress >= 1 ? Infinity : first + (last - first) * progress
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The drawn path at `progress`: the whole curve once traced, else the part left of the trace. */
|
|
46
|
+
function traced(points: readonly Vector2[], curve: LineCurve, progress: number): Vector2[] {
|
|
47
|
+
const path = curvePoints(points, curve)
|
|
48
|
+
return progress >= 1 ? path : cutAt(path, traceX(points, progress))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const curveEntry = () => Types.custom<LineCurve>("smooth")
|
|
52
|
+
|
|
53
|
+
/** A closed outline as path commands. */
|
|
54
|
+
function outlineCommands(points: readonly Vector2[]): PathCommand[] {
|
|
55
|
+
return [...points.map((p, i): PathCommand => ({ type: i === 0 ? "M" : "L", x: p.x, y: p.y })), { type: "Z" }]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function fillOutline(ctx: RenderContext, outline: readonly Vector2[], fill: FillResolved[], opacity: number): void {
|
|
59
|
+
if (outline.length < 3 || opacity <= 0) return
|
|
60
|
+
const { width, height } = ctx.size
|
|
61
|
+
// A frame symmetric about the origin, so the outline draws where its points say rather than re-centred on itself.
|
|
62
|
+
const centerBounds: [number, number, number, number] = [-width / 2, -height / 2, width / 2, height / 2]
|
|
63
|
+
ctx.draw(new Graphics2D().path({ data: outlineCommands(outline), centerBounds }).fill(fill).opacity(opacity))
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* Two signals drive it: `progress` traces the line in
|
|
68
|
-
* (animated by `enter`) and `enabled` shows or hides it wholesale.
|
|
67
|
+
* One series of a {@link LineChart}: its line through pre-mapped pixel `points`,
|
|
68
|
+
* shaped by `curve`, and a dot on each point. `progress` traces it in from the
|
|
69
|
+
* left; `enabled` shows or hides it wholesale.
|
|
69
70
|
*/
|
|
70
71
|
export const ChartLine = component2d({
|
|
71
72
|
key: "chartLine",
|
|
72
73
|
state: {
|
|
73
|
-
/**
|
|
74
|
+
/** In this node's pixel space (origin at centre, y-up), mapped by the parent and ordered along x. */
|
|
74
75
|
points: fresh<Vector2[]>(() => []),
|
|
75
|
-
|
|
76
|
+
curve: curveEntry(),
|
|
76
77
|
stroke: Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
*/
|
|
78
|
+
/** A dot of `dotSize` on each point, in `dotFill`; none at size 0. */
|
|
79
|
+
dotSize: 0,
|
|
80
|
+
dotFill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
81
|
+
/** Trace from the left end, `[0, 1]`. Rests at 1 so a paused editor canvas paints the line; `enter` drives it from 0. */
|
|
82
82
|
progress: 1,
|
|
83
|
-
/** Whether
|
|
83
|
+
/** Whether the line is drawn at all, flipped alongside its legend entry. */
|
|
84
84
|
enabled: true,
|
|
85
|
-
/**
|
|
85
|
+
/** In this node's pixel space; outside it the line drops to `dimOpacity`. */
|
|
86
86
|
spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
87
|
-
/** Opacity the out-of-region stretches fade to. */
|
|
88
87
|
dimOpacity: SPOTLIGHT_DIM,
|
|
89
88
|
},
|
|
90
|
-
load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
|
|
89
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.dotFill], strokes: [state.stroke] }),
|
|
91
90
|
commands: {
|
|
92
91
|
/**
|
|
93
|
-
* Trace the line from `from` to `to`
|
|
94
|
-
*
|
|
95
|
-
*
|
|
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.
|
|
92
|
+
* Trace the line from `from` to `to`; `showSeries`/`hideSeries` are this with
|
|
93
|
+
* the ends stated. `enabled` rides along in the same value, so the line is
|
|
94
|
+
* drawable for every `t` the trace covers and arriving backwards matches
|
|
95
|
+
* arriving forwards.
|
|
101
96
|
*/
|
|
102
97
|
enter: {
|
|
103
98
|
args: { from: Types.number(0, { min: 0, max: 1 }), to: Types.number(1, { min: 0, max: 1 }) },
|
|
@@ -106,51 +101,150 @@ export const ChartLine = component2d({
|
|
|
106
101
|
const ease = frameEase(timeline)
|
|
107
102
|
return timeline.animate((t) => {
|
|
108
103
|
const progress = from + (to - from) * ease(t)
|
|
109
|
-
// Hidden only once
|
|
110
|
-
// the empty plot rather than a line that vanished a frame early.
|
|
104
|
+
// Hidden only once fully retraced, so a hide does not vanish a frame early.
|
|
111
105
|
return { progress, enabled: progress > 0 }
|
|
112
106
|
})
|
|
113
107
|
},
|
|
114
108
|
},
|
|
109
|
+
/** Hold the line fully traced while its chart moves it, switched on throughout a show and off once a hide lands. */
|
|
110
|
+
hold: {
|
|
111
|
+
args: { shown: Types.boolean(true) },
|
|
112
|
+
run: (timeline) => {
|
|
113
|
+
const { shown } = timeline.args
|
|
114
|
+
return timeline.animate((t) => ({ progress: 1, enabled: shown || t < 1 }), { eased: false })
|
|
115
|
+
},
|
|
116
|
+
},
|
|
115
117
|
},
|
|
116
118
|
render: (ctx, state) => {
|
|
117
119
|
if (!state.enabled) return
|
|
118
120
|
const pts = state.points
|
|
119
|
-
|
|
121
|
+
const p = clamp(state.progress, 0, 1)
|
|
122
|
+
if (pts.length === 0 || p <= 0) return
|
|
120
123
|
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const drawn = tracePoints(pts, p)
|
|
125
|
-
if (drawn.length < 2) return
|
|
124
|
+
const box = state.spotlight
|
|
125
|
+
const lit = hasRegion(box)
|
|
126
|
+
const dim = clamp(state.dimOpacity, 0, 1)
|
|
126
127
|
const stroke = state.stroke
|
|
127
|
-
const
|
|
128
|
+
const weight = stroke[0]?.weight ?? 0
|
|
129
|
+
const drawn = traced(pts, state.curve, p)
|
|
128
130
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
131
|
+
if (drawn.length >= 2 && weight > 0) {
|
|
132
|
+
// Rounded corners only where the line has corners to round.
|
|
133
|
+
const radius = state.curve === "linear" ? weight * 2 : 0
|
|
134
|
+
ctx.draw(new Graphics2D().line({ points: drawn, radius }).stroke(stroke).opacity(lit ? dim : 1))
|
|
135
|
+
// One `draw` per run: shapes chained before `stroke()` combine into one surface, and stroking a union of disjoint open paths paints nothing.
|
|
136
|
+
if (lit) for (const run of clipPolylineToBox(drawn, box)) ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
|
|
133
137
|
}
|
|
134
138
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
for (const run of clipPolylineToBox(drawn, box)) {
|
|
151
|
-
ctx.draw(new Graphics2D().line({ points: run, radius }).stroke(stroke))
|
|
139
|
+
const size = state.dotSize
|
|
140
|
+
if (size <= 0) return
|
|
141
|
+
const reach = traceX(pts, p)
|
|
142
|
+
const bright = new Graphics2D()
|
|
143
|
+
const dimmed = new Graphics2D()
|
|
144
|
+
let brightAny = false
|
|
145
|
+
let dimAny = false
|
|
146
|
+
for (const pt of pts) {
|
|
147
|
+
// A dot grows in as the trace passes it.
|
|
148
|
+
const grown = reach === Infinity ? 1 : clamp((reach - pt.x) / (size * 2), 0, 1)
|
|
149
|
+
if (grown <= 0) continue
|
|
150
|
+
const inside = !lit || (pt.x >= box.left && pt.x <= box.right && pt.y >= box.bottom && pt.y <= box.top)
|
|
151
|
+
;(inside ? bright : dimmed).ellipse({ x: pt.x, y: pt.y, width: size * grown, height: size * grown })
|
|
152
|
+
if (inside) brightAny = true
|
|
153
|
+
else dimAny = true
|
|
152
154
|
}
|
|
155
|
+
if (dimAny) ctx.draw(dimmed.fill(state.dotFill).opacity(dim))
|
|
156
|
+
if (brightAny) ctx.draw(bright.fill(state.dotFill))
|
|
153
157
|
},
|
|
154
158
|
})
|
|
155
159
|
|
|
156
160
|
export type ChartLine = NodeOf<typeof ChartLine>
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The fill under one series of a {@link LineChart}: down to `base` (the series
|
|
164
|
+
* below it in a stack) or, without one, to the `baseline`. Drawn by its own node
|
|
165
|
+
* so every area sits under every line; the chart binds its trace, switch and
|
|
166
|
+
* opacity to its line's, so the two move as one.
|
|
167
|
+
*/
|
|
168
|
+
export const ChartArea = component2d({
|
|
169
|
+
key: "chartArea",
|
|
170
|
+
state: {
|
|
171
|
+
/** The series' points, as its line has them. */
|
|
172
|
+
points: fresh<Vector2[]>(() => []),
|
|
173
|
+
/** The line the area fills down to, at the same x positions; empty for the baseline. */
|
|
174
|
+
base: fresh<Vector2[]>(() => []),
|
|
175
|
+
/** Pixel y of the baseline. */
|
|
176
|
+
baseline: 0,
|
|
177
|
+
curve: curveEntry(),
|
|
178
|
+
area: Types.custom<"none" | "solid" | "gradient">("solid"),
|
|
179
|
+
/** The series colour; the area paints it at `areaOpacity`, a gradient fading it out towards the bottom. */
|
|
180
|
+
fill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
181
|
+
areaOpacity: 0.4,
|
|
182
|
+
progress: 1,
|
|
183
|
+
enabled: true,
|
|
184
|
+
spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
185
|
+
dimOpacity: SPOTLIGHT_DIM,
|
|
186
|
+
},
|
|
187
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.fill] }),
|
|
188
|
+
render: (ctx, state) => {
|
|
189
|
+
const pts = state.points
|
|
190
|
+
const p = clamp(state.progress, 0, 1)
|
|
191
|
+
if (!state.enabled || state.area === "none" || pts.length < 2 || p <= 0 || state.fill.length === 0) return
|
|
192
|
+
const outlineAt = (progress: number) => {
|
|
193
|
+
const top = traced(pts, state.curve, progress)
|
|
194
|
+
if (top.length < 2) return []
|
|
195
|
+
const base = state.base.length === pts.length
|
|
196
|
+
? traced(state.base, state.curve, progress)
|
|
197
|
+
: [{ x: top[0].x, y: state.baseline }, { x: top[top.length - 1].x, y: state.baseline }]
|
|
198
|
+
return [...top, ...[...base].reverse()]
|
|
199
|
+
}
|
|
200
|
+
// The gradient spans the whole area's height, so it holds still while the area traces in and across a spotlight's cut.
|
|
201
|
+
const span = verticalSpan(outlineAt(1))
|
|
202
|
+
const paint = (outline: readonly Vector2[], opacity: number) =>
|
|
203
|
+
fillOutline(ctx, outline, areaFill(state.fill, state.area, state.areaOpacity, span, verticalSpan(outline)), opacity)
|
|
204
|
+
const outline = outlineAt(p)
|
|
205
|
+
const box = state.spotlight
|
|
206
|
+
if (!hasRegion(box)) {
|
|
207
|
+
paint(outline, 1)
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
paint(outline, clamp(state.dimOpacity, 0, 1))
|
|
211
|
+
paint(clipPolygonToBox(outline, box), 1)
|
|
212
|
+
},
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
interface Span {
|
|
216
|
+
readonly top: number
|
|
217
|
+
readonly bottom: number
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function verticalSpan(points: readonly Vector2[]): Span {
|
|
221
|
+
let top = -Infinity
|
|
222
|
+
let bottom = Infinity
|
|
223
|
+
for (const p of points) {
|
|
224
|
+
if (p.y > top) top = p.y
|
|
225
|
+
if (p.y < bottom) bottom = p.y
|
|
226
|
+
}
|
|
227
|
+
return { top, bottom }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The area's paint for an outline spanning `own`: the series colour at
|
|
232
|
+
* `opacity`, or for a gradient that colour fading to nearly nothing across
|
|
233
|
+
* `whole`. A gradient maps onto the bounds of the shape it fills, so its ends
|
|
234
|
+
* are placed in `own`'s unit box (y-up) at `whole`'s top and bottom.
|
|
235
|
+
*/
|
|
236
|
+
export function areaFill(colour: FillResolved[], area: "none" | "solid" | "gradient", opacity: number, whole: Span, own: Span): FillResolved[] {
|
|
237
|
+
const top = colour[colour.length - 1]
|
|
238
|
+
if (area !== "gradient" || top?.type !== "solid" || !(own.top > own.bottom)) return fade(colour, opacity) as FillResolved[]
|
|
239
|
+
const [r, g, b, a] = top.color
|
|
240
|
+
const middle = (own.top + own.bottom) / 2
|
|
241
|
+
const half = (own.top - own.bottom) / 2
|
|
242
|
+
return fillOps.resolve({
|
|
243
|
+
type: "linear-gradient",
|
|
244
|
+
colors: [[r, g, b, a * opacity], [r, g, b, a * opacity * 0.04]],
|
|
245
|
+
start: { x: 0, y: (whole.top - middle) / half },
|
|
246
|
+
end: { x: 0, y: (whole.bottom - middle) / half },
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export type ChartArea = NodeOf<typeof ChartArea>
|
|
@@ -7,45 +7,37 @@ import {
|
|
|
7
7
|
type Fill,
|
|
8
8
|
type FillResolved,
|
|
9
9
|
type NodeOf,
|
|
10
|
-
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
10
|
+
// Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
|
|
11
11
|
type Node2D as _Node2D,
|
|
12
|
+
type Node2DProps as _Node2DProps,
|
|
12
13
|
type Stroke,
|
|
13
14
|
type StrokeResolved,
|
|
14
15
|
} from "@motionscript/sdk"
|
|
15
16
|
|
|
17
|
+
import { rule } from "../kit/chart-canvas"
|
|
16
18
|
import { fresh } from "../kit/entries"
|
|
17
19
|
import { loadPaints } from "../kit/paints"
|
|
18
20
|
import {
|
|
19
|
-
DEFAULT_CHART_THEME,
|
|
20
21
|
EMPTY_REGION_BOX,
|
|
21
22
|
hasRegion,
|
|
22
23
|
type RegionBox,
|
|
23
24
|
} from "./shared"
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
* The
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
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.
|
|
27
|
+
* The call-out of a {@link LineChart}: a washed rectangle with a dashed rule on
|
|
28
|
+
* each bounded edge, drawn from the pre-mapped `box`. Dimming the series outside
|
|
29
|
+
* is the line's and area's job, clipped against the same box so they agree on
|
|
30
|
+
* the boundary. An unbounded edge is the plot's own border, and a rule there
|
|
31
|
+
* would assert a limit the author did not set.
|
|
38
32
|
*/
|
|
39
33
|
export const ChartRegion = component2d({
|
|
40
34
|
key: "chartRegion",
|
|
41
35
|
state: {
|
|
42
|
-
/**
|
|
36
|
+
/** In this node's pixel space (origin at centre, y-up), mapped by the parent. A degenerate box draws nothing. */
|
|
43
37
|
box: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
/**
|
|
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. */
|
|
38
|
+
stroke: Types.custom<Stroke, StrokeResolved[]>({ weight: 2, fill: "white/70", dash: [14, 12], cap: "butt", align: "center" }, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
39
|
+
fill: Types.custom<Fill, FillResolved[]>("white/6", { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
40
|
+
/** Driven by the chart's `regionReveal`, so the rules and the series' dimming move as one. */
|
|
49
41
|
reveal: 0,
|
|
50
42
|
},
|
|
51
43
|
load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.stroke] }),
|
|
@@ -61,28 +53,19 @@ export const ChartRegion = component2d({
|
|
|
61
53
|
const cx = (box.left + box.right) / 2
|
|
62
54
|
const cy = (box.bottom + box.top) / 2
|
|
63
55
|
|
|
64
|
-
// Wash first, so the rules sit over their own tint.
|
|
65
56
|
if (state.fill.length > 0) {
|
|
66
57
|
ctx.draw(new Graphics2D().rect({ x: cx, y: cy, width, height }).fill(state.fill).opacity(r))
|
|
67
58
|
}
|
|
68
59
|
|
|
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
60
|
if (state.stroke.length === 0) return
|
|
72
61
|
|
|
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
62
|
if (box.xBounded) {
|
|
80
63
|
const half = (height / 2) * r
|
|
81
|
-
for (const x of [box.left, box.right]) rule(
|
|
64
|
+
for (const x of [box.left, box.right]) rule(ctx, { x, y: cy - half }, { x, y: cy + half }, state.stroke, r)
|
|
82
65
|
}
|
|
83
66
|
if (box.yBounded) {
|
|
84
67
|
const half = (width / 2) * r
|
|
85
|
-
for (const y of [box.bottom, box.top]) rule(
|
|
68
|
+
for (const y of [box.bottom, box.top]) rule(ctx, { x: cx - half, y }, { x: cx + half, y }, state.stroke, r)
|
|
86
69
|
}
|
|
87
70
|
},
|
|
88
71
|
})
|
package/src/line-chart/index.ts
CHANGED
|
@@ -1,39 +1,31 @@
|
|
|
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
1
|
export { LineChart, lineChartDomains } from "./line-chart"
|
|
14
|
-
export type { LineChartDomains,
|
|
2
|
+
export type { LineChartDomains, LineChartStyle, LineChartValues } from "./line-chart"
|
|
15
3
|
export { ChartRegion } from "./chart-region"
|
|
16
|
-
export { ChartLine } from "./chart-line"
|
|
17
|
-
export { HorizontalAxis } from "./horizontal-axis"
|
|
4
|
+
export { ChartArea, ChartLine, areaFill, traceX } from "./chart-line"
|
|
18
5
|
export {
|
|
19
|
-
DEFAULT_CHART_THEME,
|
|
20
6
|
EMPTY_REGION_BOX,
|
|
7
|
+
LINE_LABELS,
|
|
8
|
+
LINE_MARKS,
|
|
9
|
+
clipPolygonToBox,
|
|
21
10
|
clipPolylineToBox,
|
|
11
|
+
curvePoints,
|
|
12
|
+
cutAt,
|
|
22
13
|
hasRegion,
|
|
23
|
-
|
|
24
|
-
resolveChartTheme,
|
|
14
|
+
|
|
25
15
|
resolveRegionBox,
|
|
26
16
|
resolveYScale,
|
|
27
17
|
seriesPoints,
|
|
18
|
+
stackExtent,
|
|
19
|
+
stackSeries,
|
|
28
20
|
yExtent,
|
|
29
21
|
} from "./shared"
|
|
30
22
|
export type {
|
|
31
|
-
|
|
32
|
-
ChartTheme,
|
|
23
|
+
LineCurve,
|
|
33
24
|
LineSeries,
|
|
25
|
+
LineStack,
|
|
34
26
|
PlotRegion,
|
|
35
27
|
RegionBox,
|
|
36
|
-
|
|
28
|
+
StackedSeries,
|
|
37
29
|
} from "./shared"
|
|
38
|
-
export { formatInstant, parseInstant, resolveXScale } from "./x-scale"
|
|
30
|
+
export { EVERY_SAMPLE, formatInstant, parseInstant, resolveXScale } from "./x-scale"
|
|
39
31
|
export type { XScale, XScaleKind } from "./x-scale"
|