@motionscript/charts 0.1.1-alpha.0 → 0.1.1-alpha.2
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 +34 -0
- package/README.md +109 -28
- package/dist/bar-chart/bar-chart.d.ts +265 -39
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +219 -171
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +12 -7
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +56 -42
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +76 -52
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +105 -89
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +4 -4
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +2 -2
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +41 -65
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +73 -117
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-7XCRX6GE.js +2 -0
- package/dist/browser/chunks/chunk-7XCRX6GE.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 +5 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +5 -2
- 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 +51 -15
- package/dist/kit/cartesian.d.ts.map +1 -1
- package/dist/kit/cartesian.js +81 -29
- package/dist/kit/cartesian.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +32 -24
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +52 -46
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +8 -4
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +30 -20
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/{line-chart → kit}/horizontal-axis.d.ts +31 -6
- package/dist/kit/horizontal-axis.d.ts.map +1 -0
- package/dist/{line-chart → kit}/horizontal-axis.js +23 -24
- package/dist/kit/horizontal-axis.js.map +1 -0
- package/dist/kit/motion.d.ts +5 -0
- package/dist/kit/motion.d.ts.map +1 -1
- package/dist/kit/motion.js +8 -0
- package/dist/kit/motion.js.map +1 -1
- package/dist/{pie-chart → kit}/pie-legend.d.ts +3 -3
- package/dist/kit/pie-legend.d.ts.map +1 -0
- package/dist/{pie-chart → kit}/pie-legend.js +11 -10
- 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 +11 -50
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +58 -141
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/style.d.ts +517 -0
- package/dist/kit/style.d.ts.map +1 -0
- package/dist/kit/style.js +292 -0
- package/dist/kit/style.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +22 -2
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +56 -43
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +95 -3
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +165 -53
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +1 -1
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +7 -8
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/index.d.ts +5 -6
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +3 -4
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +281 -18
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +284 -134
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +51 -34
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +175 -83
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +2 -0
- package/dist/line-chart/x-scale.d.ts.map +1 -1
- package/dist/line-chart/x-scale.js +15 -0
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.d.ts +3 -1
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +9 -1
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +2 -3
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -2
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +114 -18
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +38 -24
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +16 -5
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +89 -55
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +61 -62
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +44 -123
- 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 +212 -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 +49 -0
- package/dist/radar-chart/radar-grid.d.ts.map +1 -0
- package/dist/radar-chart/radar-grid.js +63 -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 +78 -0
- package/dist/radar-chart/shared.d.ts.map +1 -0
- package/dist/radar-chart/shared.js +129 -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 +210 -0
- package/dist/radial-chart/radial-chart.d.ts.map +1 -0
- package/dist/radial-chart/radial-chart.js +175 -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 +77 -0
- package/dist/radial-chart/shared.d.ts.map +1 -0
- package/dist/radial-chart/shared.js +78 -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 +263 -179
- package/src/bar-chart/category-axis.ts +46 -40
- package/src/bar-chart/chart-bars.ts +136 -117
- package/src/bar-chart/index.ts +7 -10
- package/src/bar-chart/shared.ts +88 -233
- package/src/engine.ts +5 -2
- package/src/index.ts +2 -0
- package/src/kit/cartesian.ts +121 -40
- package/src/kit/chart-canvas.ts +49 -54
- package/src/kit/chart-legend.ts +28 -20
- package/src/kit/horizontal-axis.ts +114 -0
- package/src/kit/motion.ts +7 -0
- package/src/{pie-chart → kit}/pie-legend.ts +12 -11
- package/src/kit/polar.ts +54 -0
- package/src/kit/shared.ts +55 -277
- package/src/kit/style.ts +353 -0
- package/src/kit/vertical-axis.ts +67 -48
- package/src/line-chart/chart-line.ts +176 -54
- package/src/line-chart/chart-region.ts +6 -10
- package/src/line-chart/index.ts +14 -10
- package/src/line-chart/line-chart.ts +353 -151
- package/src/line-chart/shared.ts +193 -174
- package/src/line-chart/x-scale.ts +13 -0
- package/src/nodes.ts +9 -1
- package/src/pie-chart/index.ts +2 -23
- package/src/pie-chart/pie-chart.ts +54 -35
- package/src/pie-chart/pie-ring.ts +92 -71
- package/src/pie-chart/shared.ts +80 -270
- package/src/radar-chart/index.ts +4 -0
- package/src/radar-chart/radar-chart.ts +290 -0
- package/src/radar-chart/radar-grid.ts +83 -0
- package/src/radar-chart/radar-series.ts +88 -0
- package/src/radar-chart/shared.ts +153 -0
- package/src/radial-chart/index.ts +5 -0
- package/src/radial-chart/radial-chart.ts +212 -0
- package/src/radial-chart/radial-rings.ts +328 -0
- package/src/radial-chart/shared.ts +126 -0
- package/dist/browser/chunks/chunk-4SMRCU24.js +0 -2
- package/dist/browser/chunks/chunk-4SMRCU24.js.map +0 -7
- package/dist/kit/chart-border.d.ts +0 -18
- package/dist/kit/chart-border.d.ts.map +0 -1
- package/dist/kit/chart-border.js +0 -23
- package/dist/kit/chart-border.js.map +0 -1
- package/dist/line-chart/horizontal-axis.d.ts.map +0 -1
- 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.map +0 -1
- package/src/kit/chart-border.ts +0 -36
- package/src/line-chart/horizontal-axis.ts +0 -122
|
@@ -1,67 +1,83 @@
|
|
|
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
11
|
// Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.
|
|
8
12
|
type Node2D as _Node2D,
|
|
9
13
|
type Node2DProps as _Node2DProps,
|
|
14
|
+
type PathCommand,
|
|
15
|
+
type RenderContext,
|
|
10
16
|
type Stroke,
|
|
11
17
|
type StrokeResolved,
|
|
12
18
|
type Vector2,
|
|
13
19
|
} from "@motionscript/sdk"
|
|
14
20
|
|
|
21
|
+
import { fade } from "../kit/cartesian"
|
|
15
22
|
import { fresh } from "../kit/entries"
|
|
16
23
|
import { frameEase } from "../kit/motion"
|
|
17
24
|
import { loadPaints } from "../kit/paints"
|
|
18
25
|
import { SPOTLIGHT_DIM } from "../kit/shared"
|
|
19
26
|
import {
|
|
20
27
|
EMPTY_REGION_BOX,
|
|
28
|
+
clipPolygonToBox,
|
|
21
29
|
clipPolylineToBox,
|
|
30
|
+
cutAt,
|
|
31
|
+
curvePoints,
|
|
22
32
|
hasRegion,
|
|
33
|
+
type LineCurve,
|
|
23
34
|
type RegionBox,
|
|
24
35
|
} from "./shared"
|
|
25
36
|
|
|
26
|
-
/** The
|
|
27
|
-
function
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
let total = 0
|
|
33
|
-
for (let i = 1; i < pts.length; i++) {
|
|
34
|
-
const d = Math.hypot(pts[i].x - pts[i - 1].x, pts[i].y - pts[i - 1].y)
|
|
35
|
-
segLen.push(d)
|
|
36
|
-
total += d
|
|
37
|
-
}
|
|
38
|
-
if (total === 0) return pts
|
|
39
|
-
|
|
40
|
-
let target = total * p
|
|
41
|
-
const out: Vector2[] = [pts[0]]
|
|
42
|
-
for (let i = 0; i < segLen.length; i++) {
|
|
43
|
-
if (target >= segLen[i]) {
|
|
44
|
-
out.push(pts[i + 1])
|
|
45
|
-
target -= segLen[i]
|
|
46
|
-
} else {
|
|
47
|
-
const t = segLen[i] === 0 ? 0 : target / segLen[i]
|
|
48
|
-
out.push({
|
|
49
|
-
x: pts[i].x + (pts[i + 1].x - pts[i].x) * t,
|
|
50
|
-
y: pts[i].y + (pts[i + 1].y - pts[i].y) * t,
|
|
51
|
-
})
|
|
52
|
-
break
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
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
|
|
56
43
|
}
|
|
57
44
|
|
|
58
|
-
/**
|
|
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))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
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.
|
|
70
|
+
*/
|
|
59
71
|
export const ChartLine = component2d({
|
|
60
72
|
key: "chartLine",
|
|
61
73
|
state: {
|
|
62
74
|
/** In this node's pixel space (origin at centre, y-up), mapped by the parent and ordered along x. */
|
|
63
75
|
points: fresh<Vector2[]>(() => []),
|
|
76
|
+
curve: curveEntry(),
|
|
64
77
|
stroke: Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
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 }),
|
|
65
81
|
/** Trace from the left end, `[0, 1]`. Rests at 1 so a paused editor canvas paints the line; `enter` drives it from 0. */
|
|
66
82
|
progress: 1,
|
|
67
83
|
/** Whether the line is drawn at all, flipped alongside its legend entry. */
|
|
@@ -70,7 +86,7 @@ export const ChartLine = component2d({
|
|
|
70
86
|
spotlight: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
71
87
|
dimOpacity: SPOTLIGHT_DIM,
|
|
72
88
|
},
|
|
73
|
-
load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
|
|
89
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.dotFill], strokes: [state.stroke] }),
|
|
74
90
|
commands: {
|
|
75
91
|
/**
|
|
76
92
|
* Trace the line from `from` to `to`; `showSeries`/`hideSeries` are this with
|
|
@@ -90,39 +106,145 @@ export const ChartLine = component2d({
|
|
|
90
106
|
})
|
|
91
107
|
},
|
|
92
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
|
+
},
|
|
93
117
|
},
|
|
94
118
|
render: (ctx, state) => {
|
|
95
119
|
if (!state.enabled) return
|
|
96
120
|
const pts = state.points
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const p = Math.max(0, Math.min(1, state.progress))
|
|
100
|
-
if (p <= 0) return
|
|
121
|
+
const p = clamp(state.progress, 0, 1)
|
|
122
|
+
if (pts.length === 0 || p <= 0) return
|
|
101
123
|
|
|
102
|
-
const
|
|
103
|
-
|
|
124
|
+
const box = state.spotlight
|
|
125
|
+
const lit = hasRegion(box)
|
|
126
|
+
const dim = clamp(state.dimOpacity, 0, 1)
|
|
104
127
|
const stroke = state.stroke
|
|
105
|
-
const
|
|
128
|
+
const weight = stroke[0]?.weight ?? 0
|
|
129
|
+
const drawn = traced(pts, state.curve, p)
|
|
106
130
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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))
|
|
111
137
|
}
|
|
112
138
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
|
124
154
|
}
|
|
155
|
+
if (dimAny) ctx.draw(dimmed.fill(state.dotFill).opacity(dim))
|
|
156
|
+
if (brightAny) ctx.draw(bright.fill(state.dotFill))
|
|
125
157
|
},
|
|
126
158
|
})
|
|
127
159
|
|
|
128
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>
|
|
@@ -14,10 +14,10 @@ import {
|
|
|
14
14
|
type StrokeResolved,
|
|
15
15
|
} from "@motionscript/sdk"
|
|
16
16
|
|
|
17
|
+
import { rule } from "../kit/chart-canvas"
|
|
17
18
|
import { fresh } from "../kit/entries"
|
|
18
19
|
import { loadPaints } from "../kit/paints"
|
|
19
20
|
import {
|
|
20
|
-
DEFAULT_CHART_THEME,
|
|
21
21
|
EMPTY_REGION_BOX,
|
|
22
22
|
hasRegion,
|
|
23
23
|
type RegionBox,
|
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
/**
|
|
27
27
|
* The call-out of a {@link LineChart}: a washed rectangle with a dashed rule on
|
|
28
28
|
* each bounded edge, drawn from the pre-mapped `box`. Dimming the series outside
|
|
29
|
-
* is
|
|
29
|
+
* is the line's and area's job, clipped against the same box so they agree on
|
|
30
30
|
* the boundary. An unbounded edge is the plot's own border, and a rule there
|
|
31
31
|
* would assert a limit the author did not set.
|
|
32
32
|
*/
|
|
@@ -35,8 +35,8 @@ export const ChartRegion = component2d({
|
|
|
35
35
|
state: {
|
|
36
36
|
/** In this node's pixel space (origin at centre, y-up), mapped by the parent. A degenerate box draws nothing. */
|
|
37
37
|
box: fresh<RegionBox>(() => EMPTY_REGION_BOX),
|
|
38
|
-
stroke: Types.custom<Stroke, StrokeResolved[]>(
|
|
39
|
-
fill: Types.custom<Fill, FillResolved[]>(
|
|
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
40
|
/** Driven by the chart's `regionReveal`, so the rules and the series' dimming move as one. */
|
|
41
41
|
reveal: 0,
|
|
42
42
|
},
|
|
@@ -59,17 +59,13 @@ export const ChartRegion = component2d({
|
|
|
59
59
|
|
|
60
60
|
if (state.stroke.length === 0) return
|
|
61
61
|
|
|
62
|
-
// One `draw` per rule: stroking a union of disjoint open paths paints nothing.
|
|
63
|
-
const rule = (points: { x: number; y: number }[]) =>
|
|
64
|
-
ctx.draw(new Graphics2D().line({ points }).stroke(state.stroke).opacity(r))
|
|
65
|
-
|
|
66
62
|
if (box.xBounded) {
|
|
67
63
|
const half = (height / 2) * r
|
|
68
|
-
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)
|
|
69
65
|
}
|
|
70
66
|
if (box.yBounded) {
|
|
71
67
|
const half = (width / 2) * r
|
|
72
|
-
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)
|
|
73
69
|
}
|
|
74
70
|
},
|
|
75
71
|
})
|
package/src/line-chart/index.ts
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
export { LineChart, lineChartDomains } from "./line-chart"
|
|
2
|
-
export type { LineChartDomains, LineChartValues } from "./line-chart"
|
|
2
|
+
export type { LineChartDomains, LineChartStyle, LineChartValues } from "./line-chart"
|
|
3
3
|
export { ChartRegion } from "./chart-region"
|
|
4
|
-
export { ChartLine } from "./chart-line"
|
|
5
|
-
export { HorizontalAxis } from "./horizontal-axis"
|
|
4
|
+
export { ChartArea, ChartLine, areaFill, traceX } from "./chart-line"
|
|
6
5
|
export {
|
|
7
|
-
DEFAULT_CHART_THEME,
|
|
8
6
|
EMPTY_REGION_BOX,
|
|
7
|
+
LINE_LABELS,
|
|
8
|
+
LINE_MARKS,
|
|
9
|
+
clipPolygonToBox,
|
|
9
10
|
clipPolylineToBox,
|
|
11
|
+
curvePoints,
|
|
12
|
+
cutAt,
|
|
10
13
|
hasRegion,
|
|
11
|
-
|
|
12
|
-
resolveChartTheme,
|
|
14
|
+
|
|
13
15
|
resolveRegionBox,
|
|
14
16
|
resolveYScale,
|
|
15
17
|
seriesPoints,
|
|
18
|
+
stackExtent,
|
|
19
|
+
stackSeries,
|
|
16
20
|
yExtent,
|
|
17
21
|
} from "./shared"
|
|
18
22
|
export type {
|
|
19
|
-
|
|
20
|
-
ChartTheme,
|
|
23
|
+
LineCurve,
|
|
21
24
|
LineSeries,
|
|
25
|
+
LineStack,
|
|
22
26
|
PlotRegion,
|
|
23
27
|
RegionBox,
|
|
24
|
-
|
|
28
|
+
StackedSeries,
|
|
25
29
|
} from "./shared"
|
|
26
|
-
export { formatInstant, parseInstant, resolveXScale } from "./x-scale"
|
|
30
|
+
export { EVERY_SAMPLE, formatInstant, parseInstant, resolveXScale } from "./x-scale"
|
|
27
31
|
export type { XScale, XScaleKind } from "./x-scale"
|