@motionscript/charts 0.1.0-alpha.3 → 0.1.0-alpha.5
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 +41 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +85 -0
- package/dist/bar-chart/bar-chart.d.ts +28 -13
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +60 -56
- 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.d.ts +3 -3
- package/dist/bar-chart/shared.js +3 -3
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-5ZPBV6BU.js +2 -0
- package/dist/browser/chunks/chunk-5ZPBV6BU.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.js.map +1 -1
- package/dist/index.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.d.ts +4 -25
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +31 -55
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/shared.d.ts +29 -6
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +64 -8
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +2 -2
- 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.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +9 -1
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +32 -8
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +3 -3
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +19 -4
- 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.js.map +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +2 -2
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +9 -7
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.js +4 -4
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +13 -4
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +44 -14
- 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 +820 -814
- package/src/bar-chart/category-axis.ts +177 -177
- package/src/bar-chart/chart-bars.ts +310 -310
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +26 -26
- package/src/index.ts +5 -5
- package/src/kit/chart-border.ts +61 -61
- package/src/kit/chart-canvas.ts +157 -157
- package/src/kit/chart-legend.ts +245 -250
- package/src/kit/shared.ts +733 -675
- package/src/kit/vertical-axis.ts +224 -224
- package/src/line-chart/chart-line.ts +208 -208
- package/src/line-chart/chart-region.ts +135 -135
- package/src/line-chart/horizontal-axis.ts +202 -202
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +961 -929
- package/src/line-chart/shared.ts +473 -459
- package/src/line-chart/x-scale.ts +367 -367
- package/src/nodes.ts +20 -20
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +383 -381
- package/src/pie-chart/pie-legend.ts +159 -159
- package/src/pie-chart/pie-ring.ts +935 -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
package/src/engine.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The chart **kit**: everything the three charts share, and the one thing a
|
|
3
|
-
* copied chart does not copy.
|
|
4
|
-
*
|
|
5
|
-
* `ms add line-chart` writes `line-chart/*` into your project and rewrites its
|
|
6
|
-
* reach for these to `@motionscript/charts`, so the plot panel, the value
|
|
7
|
-
* axis, the legend and the number formatting stay a real dependency. That is
|
|
8
|
-
* not a size decision — it is the only arrangement that works.
|
|
9
|
-
*
|
|
10
|
-
* A chart identifies its own parts, and the engine keys signal machinery to
|
|
11
|
-
* class identity: `@property` installs against the class it was applied with,
|
|
12
|
-
* and the renderer asks `instanceof` about what it is drawing. Copy
|
|
13
|
-
* `VerticalAxis` alongside your fork and there are now two of it — two classes
|
|
14
|
-
* answering one name, and whichever one your chart did not get renders as
|
|
15
|
-
* nothing at all, with no error. Keeping the kit external makes that
|
|
16
|
-
* impossible rather than unlikely.
|
|
17
|
-
*
|
|
18
|
-
* It also means a fix to the axis reaches a forked chart without a re-add,
|
|
19
|
-
* which is the half of the bargain you get back for it.
|
|
20
|
-
*/
|
|
21
|
-
export * from "./kit/shared"
|
|
22
|
-
export * from "./kit/chart-border"
|
|
23
|
-
export * from "./kit/chart-canvas"
|
|
24
|
-
export * from "./kit/chart-legend"
|
|
25
|
-
export * from "./kit/vertical-axis"
|
|
26
|
-
export * from "@motionscript/sdk/component"
|
|
1
|
+
/**
|
|
2
|
+
* The chart **kit**: everything the three charts share, and the one thing a
|
|
3
|
+
* copied chart does not copy.
|
|
4
|
+
*
|
|
5
|
+
* `ms add line-chart` writes `line-chart/*` into your project and rewrites its
|
|
6
|
+
* reach for these to `@motionscript/charts`, so the plot panel, the value
|
|
7
|
+
* axis, the legend and the number formatting stay a real dependency. That is
|
|
8
|
+
* not a size decision — it is the only arrangement that works.
|
|
9
|
+
*
|
|
10
|
+
* A chart identifies its own parts, and the engine keys signal machinery to
|
|
11
|
+
* class identity: `@property` installs against the class it was applied with,
|
|
12
|
+
* and the renderer asks `instanceof` about what it is drawing. Copy
|
|
13
|
+
* `VerticalAxis` alongside your fork and there are now two of it — two classes
|
|
14
|
+
* answering one name, and whichever one your chart did not get renders as
|
|
15
|
+
* nothing at all, with no error. Keeping the kit external makes that
|
|
16
|
+
* impossible rather than unlikely.
|
|
17
|
+
*
|
|
18
|
+
* It also means a fix to the axis reaches a forked chart without a re-add,
|
|
19
|
+
* which is the half of the bargain you get back for it.
|
|
20
|
+
*/
|
|
21
|
+
export * from "./kit/shared"
|
|
22
|
+
export * from "./kit/chart-border"
|
|
23
|
+
export * from "./kit/chart-canvas"
|
|
24
|
+
export * from "./kit/chart-legend"
|
|
25
|
+
export * from "./kit/vertical-axis"
|
|
26
|
+
export * from "@motionscript/sdk/component"
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from "./line-chart";
|
|
2
|
-
export * from "./bar-chart";
|
|
3
|
-
export * from "./pie-chart";
|
|
4
|
-
export * from "./engine";
|
|
5
|
-
export { NODES } from "./nodes";
|
|
1
|
+
export * from "./line-chart";
|
|
2
|
+
export * from "./bar-chart";
|
|
3
|
+
export * from "./pie-chart";
|
|
4
|
+
export * from "./engine";
|
|
5
|
+
export { NODES } from "./nodes";
|
package/src/kit/chart-border.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type AssetScope,
|
|
3
|
-
Graphics2D,
|
|
4
|
-
Node2D,
|
|
5
|
-
strokeOps,
|
|
6
|
-
property,
|
|
7
|
-
type Node2DProps,
|
|
8
|
-
type RenderContext2D,
|
|
9
|
-
type Stroke,
|
|
10
|
-
} from "@motionscript/sdk"
|
|
11
|
-
|
|
12
|
-
import { DEFAULT_PLOT_AREA } from "./shared"
|
|
13
|
-
import { declarePaints } from "@motionscript/sdk/component"
|
|
14
|
-
|
|
15
|
-
export interface ChartBorderProps extends Node2DProps {
|
|
16
|
-
/**
|
|
17
|
-
* The plot's thick frame stroke. The stroke resolver's default `align` is
|
|
18
|
-
* `'inside'`, so the whole weight sits within the layout box unless the theme
|
|
19
|
-
* overrides it.
|
|
20
|
-
*/
|
|
21
|
-
stroke: Stroke
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The thick **frame** around a cartesian chart's plot: an empty rect wearing an
|
|
26
|
-
* inside stroke, sized from its allocated `layoutBounds`.
|
|
27
|
-
*
|
|
28
|
-
* Split out of {@link ChartCanvas} (the filled + gridded backdrop) so it can be
|
|
29
|
-
* placed *after* the marks in the tree — the box then frames the plot **over**
|
|
30
|
-
* them, so a line or a column running to the edge tucks cleanly under the border
|
|
31
|
-
* rather than sitting on top of it.
|
|
32
|
-
*/
|
|
33
|
-
export class ChartBorder extends Node2D<ChartBorderProps> {
|
|
34
|
-
@property({
|
|
35
|
-
default: DEFAULT_PLOT_AREA.stroke,
|
|
36
|
-
mapper: strokeOps.resolve,
|
|
37
|
-
tween: strokeOps.lerp,
|
|
38
|
-
})
|
|
39
|
-
declare stroke: Stroke
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
|
|
43
|
-
* paint props it carries — see {@link declarePaints}.
|
|
44
|
-
*/
|
|
45
|
-
override declareAssets(assets: AssetScope): void {
|
|
46
|
-
super.declareAssets(assets)
|
|
47
|
-
declarePaints(this, assets)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
protected renderSelf(ctx: RenderContext2D): void {
|
|
51
|
-
const W = this.layoutBounds.width
|
|
52
|
-
const H = this.layoutBounds.height
|
|
53
|
-
if (W <= 0 || H <= 0) return
|
|
54
|
-
|
|
55
|
-
// Empty rect (no fill) with the themed stroke, so the frame sits over the
|
|
56
|
-
// marks but the box interior stays clear.
|
|
57
|
-
ctx.draw(
|
|
58
|
-
new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).stroke(this.stroke)
|
|
59
|
-
)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
type AssetScope,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
Node2D,
|
|
5
|
+
strokeOps,
|
|
6
|
+
property,
|
|
7
|
+
type Node2DProps,
|
|
8
|
+
type RenderContext2D,
|
|
9
|
+
type Stroke,
|
|
10
|
+
} from "@motionscript/sdk"
|
|
11
|
+
|
|
12
|
+
import { DEFAULT_PLOT_AREA } from "./shared"
|
|
13
|
+
import { declarePaints } from "@motionscript/sdk/component"
|
|
14
|
+
|
|
15
|
+
export interface ChartBorderProps extends Node2DProps {
|
|
16
|
+
/**
|
|
17
|
+
* The plot's thick frame stroke. The stroke resolver's default `align` is
|
|
18
|
+
* `'inside'`, so the whole weight sits within the layout box unless the theme
|
|
19
|
+
* overrides it.
|
|
20
|
+
*/
|
|
21
|
+
stroke: Stroke
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The thick **frame** around a cartesian chart's plot: an empty rect wearing an
|
|
26
|
+
* inside stroke, sized from its allocated `layoutBounds`.
|
|
27
|
+
*
|
|
28
|
+
* Split out of {@link ChartCanvas} (the filled + gridded backdrop) so it can be
|
|
29
|
+
* placed *after* the marks in the tree — the box then frames the plot **over**
|
|
30
|
+
* them, so a line or a column running to the edge tucks cleanly under the border
|
|
31
|
+
* rather than sitting on top of it.
|
|
32
|
+
*/
|
|
33
|
+
export class ChartBorder extends Node2D<ChartBorderProps> {
|
|
34
|
+
@property({
|
|
35
|
+
default: DEFAULT_PLOT_AREA.stroke,
|
|
36
|
+
mapper: strokeOps.resolve,
|
|
37
|
+
tween: strokeOps.lerp,
|
|
38
|
+
})
|
|
39
|
+
declare stroke: Stroke
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
|
|
43
|
+
* paint props it carries — see {@link declarePaints}.
|
|
44
|
+
*/
|
|
45
|
+
override declareAssets(assets: AssetScope): void {
|
|
46
|
+
super.declareAssets(assets)
|
|
47
|
+
declarePaints(this, assets)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
protected renderSelf(ctx: RenderContext2D): void {
|
|
51
|
+
const W = this.layoutBounds.width
|
|
52
|
+
const H = this.layoutBounds.height
|
|
53
|
+
if (W <= 0 || H <= 0) return
|
|
54
|
+
|
|
55
|
+
// Empty rect (no fill) with the themed stroke, so the frame sits over the
|
|
56
|
+
// marks but the box interior stays clear.
|
|
57
|
+
ctx.draw(
|
|
58
|
+
new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).stroke(this.stroke)
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/kit/chart-canvas.ts
CHANGED
|
@@ -1,157 +1,157 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type AssetScope,
|
|
3
|
-
Graphics2D,
|
|
4
|
-
Node2D,
|
|
5
|
-
fillOps,
|
|
6
|
-
shadowOps,
|
|
7
|
-
strokeOps,
|
|
8
|
-
property,
|
|
9
|
-
type Fill,
|
|
10
|
-
type Node2DProps,
|
|
11
|
-
type RenderContext2D,
|
|
12
|
-
type Shadow,
|
|
13
|
-
type Stroke,
|
|
14
|
-
type StrokeResolved,
|
|
15
|
-
} from "@motionscript/sdk"
|
|
16
|
-
|
|
17
|
-
import {
|
|
18
|
-
DEFAULT_PLOT_AREA,
|
|
19
|
-
DEFAULT_X_AXIS,
|
|
20
|
-
DEFAULT_Y_AXIS,
|
|
21
|
-
type Range,
|
|
22
|
-
} from "./shared"
|
|
23
|
-
import { declarePaints } from "@motionscript/sdk/component"
|
|
24
|
-
|
|
25
|
-
export interface ChartCanvasProps extends Node2DProps {
|
|
26
|
-
/** Fill of the plot panel behind the marks. */
|
|
27
|
-
fill: Fill
|
|
28
|
-
/** Drop shadow of the panel. Default none. */
|
|
29
|
-
shadow: Shadow
|
|
30
|
-
/** Stroke of the vertical grid lines (one per {@link xMarkers}). */
|
|
31
|
-
xGridStroke: Stroke
|
|
32
|
-
/** Stroke of the horizontal grid lines (one per {@link yMarkers}). */
|
|
33
|
-
yGridStroke: Stroke
|
|
34
|
-
/**
|
|
35
|
-
* Data-unit x positions to draw a vertical grid line at — one per horizontal
|
|
36
|
-
* axis marker, so a line lands under every marker regardless of spacing.
|
|
37
|
-
* Positions at (or past) an {@link xExtent} edge coincide with the border and
|
|
38
|
-
* are skipped. Pass an empty list for a chart whose horizontal axis is
|
|
39
|
-
* categorical and so has no numeric position to rule at.
|
|
40
|
-
*/
|
|
41
|
-
xMarkers: number[]
|
|
42
|
-
/** Data-unit y positions to draw a horizontal grid line at. */
|
|
43
|
-
yMarkers: number[]
|
|
44
|
-
/** `[min, max]` of the horizontal scale — maps {@link xMarkers} into the panel. */
|
|
45
|
-
xExtent: Range
|
|
46
|
-
/** `[min, max]` of the vertical scale — maps {@link yMarkers} into the panel. */
|
|
47
|
-
yExtent: Range
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The plot **backdrop** of a cartesian chart: a filled panel with a grid of
|
|
52
|
-
* faint lines spanning both directions across it. Drawn as one {@link Graphics2D}
|
|
53
|
-
* command list, sized from its allocated `layoutBounds` and centred on the node
|
|
54
|
-
* origin.
|
|
55
|
-
*
|
|
56
|
-
* The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
|
|
57
|
-
* *over* the marks while this panel sits under them. Grid lines are placed from
|
|
58
|
-
* the axis marker positions (in data units) mapped into the panel through
|
|
59
|
-
* {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
|
|
60
|
-
* marker whether or not the markers are evenly spaced.
|
|
61
|
-
*/
|
|
62
|
-
export class ChartCanvas extends Node2D<ChartCanvasProps> {
|
|
63
|
-
@property({
|
|
64
|
-
default: DEFAULT_PLOT_AREA.fill,
|
|
65
|
-
mapper: fillOps.resolve,
|
|
66
|
-
tween: fillOps.lerp,
|
|
67
|
-
})
|
|
68
|
-
declare fill: Fill
|
|
69
|
-
@property({
|
|
70
|
-
default: DEFAULT_PLOT_AREA.shadow,
|
|
71
|
-
mapper: shadowOps.resolve,
|
|
72
|
-
tween: shadowOps.lerp,
|
|
73
|
-
})
|
|
74
|
-
declare shadow: Shadow
|
|
75
|
-
@property({
|
|
76
|
-
default: DEFAULT_X_AXIS.lineStyle,
|
|
77
|
-
mapper: strokeOps.resolve,
|
|
78
|
-
tween: strokeOps.lerp,
|
|
79
|
-
})
|
|
80
|
-
declare xGridStroke: Stroke
|
|
81
|
-
@property({
|
|
82
|
-
default: DEFAULT_Y_AXIS.lineStyle,
|
|
83
|
-
mapper: strokeOps.resolve,
|
|
84
|
-
tween: strokeOps.lerp,
|
|
85
|
-
})
|
|
86
|
-
declare yGridStroke: Stroke
|
|
87
|
-
@property({ default: () => [] as number[] }) declare xMarkers: number[]
|
|
88
|
-
@property({ default: () => [] as number[] }) declare yMarkers: number[]
|
|
89
|
-
@property({ default: () => [0, 1] as Range }) declare xExtent: Range
|
|
90
|
-
@property({ default: () => [0, 1] as Range }) declare yExtent: Range
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
|
|
94
|
-
* paint props it carries — see {@link declarePaints}. The grid strokes are
|
|
95
|
-
* named past the conventional slots, so they are passed explicitly.
|
|
96
|
-
*/
|
|
97
|
-
override declareAssets(assets: AssetScope): void {
|
|
98
|
-
super.declareAssets(assets)
|
|
99
|
-
declarePaints(this, assets, {
|
|
100
|
-
strokes: [this.xGridStroke as StrokeResolved[], this.yGridStroke as StrokeResolved[]],
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
protected renderSelf(ctx: RenderContext2D): void {
|
|
105
|
-
const W = this.layoutBounds.width
|
|
106
|
-
const H = this.layoutBounds.height
|
|
107
|
-
if (W <= 0 || H <= 0) return
|
|
108
|
-
|
|
109
|
-
// Plot panel, centred on the origin (node space is y-up, origin centre).
|
|
110
|
-
ctx.draw(
|
|
111
|
-
new Graphics2D()
|
|
112
|
-
.rect({ x: 0, y: 0, width: W, height: H })
|
|
113
|
-
.shadow(this.shadow)
|
|
114
|
-
.fill(this.fill)
|
|
115
|
-
)
|
|
116
|
-
|
|
117
|
-
const [xMin, xMax] = this.xExtent
|
|
118
|
-
const [yMin, yMax] = this.yExtent
|
|
119
|
-
// Fraction of a marker across its extent; guard a zero-width extent.
|
|
120
|
-
const xFrac = (v: number) => (xMax <= xMin ? 0 : (v - xMin) / (xMax - xMin))
|
|
121
|
-
const yFrac = (v: number) => (yMax <= yMin ? 0 : (v - yMin) / (yMax - yMin))
|
|
122
|
-
// Skip markers landing on (or outside) the edges — they'd sit under the border.
|
|
123
|
-
const interior = (f: number) => f > 1e-6 && f < 1 - 1e-6
|
|
124
|
-
|
|
125
|
-
const xStroke = (this.xGridStroke as StrokeResolved[])[0]
|
|
126
|
-
if (xStroke) {
|
|
127
|
-
const xGrid = new Graphics2D()
|
|
128
|
-
for (const v of this.xMarkers) {
|
|
129
|
-
const f = xFrac(v)
|
|
130
|
-
if (!interior(f)) continue
|
|
131
|
-
xGrid.rect({
|
|
132
|
-
x: -W / 2 + f * W,
|
|
133
|
-
y: 0,
|
|
134
|
-
width: xStroke.weight,
|
|
135
|
-
height: H,
|
|
136
|
-
})
|
|
137
|
-
}
|
|
138
|
-
ctx.draw(xGrid.fill(xStroke.fill))
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const yStroke = (this.yGridStroke as StrokeResolved[])[0]
|
|
142
|
-
if (yStroke) {
|
|
143
|
-
const yGrid = new Graphics2D()
|
|
144
|
-
for (const v of this.yMarkers) {
|
|
145
|
-
const f = yFrac(v)
|
|
146
|
-
if (!interior(f)) continue
|
|
147
|
-
yGrid.rect({
|
|
148
|
-
x: 0,
|
|
149
|
-
y: -H / 2 + f * H,
|
|
150
|
-
width: W,
|
|
151
|
-
height: yStroke.weight,
|
|
152
|
-
})
|
|
153
|
-
}
|
|
154
|
-
ctx.draw(yGrid.fill(yStroke.fill))
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
type AssetScope,
|
|
3
|
+
Graphics2D,
|
|
4
|
+
Node2D,
|
|
5
|
+
fillOps,
|
|
6
|
+
shadowOps,
|
|
7
|
+
strokeOps,
|
|
8
|
+
property,
|
|
9
|
+
type Fill,
|
|
10
|
+
type Node2DProps,
|
|
11
|
+
type RenderContext2D,
|
|
12
|
+
type Shadow,
|
|
13
|
+
type Stroke,
|
|
14
|
+
type StrokeResolved,
|
|
15
|
+
} from "@motionscript/sdk"
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
DEFAULT_PLOT_AREA,
|
|
19
|
+
DEFAULT_X_AXIS,
|
|
20
|
+
DEFAULT_Y_AXIS,
|
|
21
|
+
type Range,
|
|
22
|
+
} from "./shared"
|
|
23
|
+
import { declarePaints } from "@motionscript/sdk/component"
|
|
24
|
+
|
|
25
|
+
export interface ChartCanvasProps extends Node2DProps {
|
|
26
|
+
/** Fill of the plot panel behind the marks. */
|
|
27
|
+
fill: Fill
|
|
28
|
+
/** Drop shadow of the panel. Default none. */
|
|
29
|
+
shadow: Shadow
|
|
30
|
+
/** Stroke of the vertical grid lines (one per {@link xMarkers}). */
|
|
31
|
+
xGridStroke: Stroke
|
|
32
|
+
/** Stroke of the horizontal grid lines (one per {@link yMarkers}). */
|
|
33
|
+
yGridStroke: Stroke
|
|
34
|
+
/**
|
|
35
|
+
* Data-unit x positions to draw a vertical grid line at — one per horizontal
|
|
36
|
+
* axis marker, so a line lands under every marker regardless of spacing.
|
|
37
|
+
* Positions at (or past) an {@link xExtent} edge coincide with the border and
|
|
38
|
+
* are skipped. Pass an empty list for a chart whose horizontal axis is
|
|
39
|
+
* categorical and so has no numeric position to rule at.
|
|
40
|
+
*/
|
|
41
|
+
xMarkers: number[]
|
|
42
|
+
/** Data-unit y positions to draw a horizontal grid line at. */
|
|
43
|
+
yMarkers: number[]
|
|
44
|
+
/** `[min, max]` of the horizontal scale — maps {@link xMarkers} into the panel. */
|
|
45
|
+
xExtent: Range
|
|
46
|
+
/** `[min, max]` of the vertical scale — maps {@link yMarkers} into the panel. */
|
|
47
|
+
yExtent: Range
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The plot **backdrop** of a cartesian chart: a filled panel with a grid of
|
|
52
|
+
* faint lines spanning both directions across it. Drawn as one {@link Graphics2D}
|
|
53
|
+
* command list, sized from its allocated `layoutBounds` and centred on the node
|
|
54
|
+
* origin.
|
|
55
|
+
*
|
|
56
|
+
* The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
|
|
57
|
+
* *over* the marks while this panel sits under them. Grid lines are placed from
|
|
58
|
+
* the axis marker positions (in data units) mapped into the panel through
|
|
59
|
+
* {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
|
|
60
|
+
* marker whether or not the markers are evenly spaced.
|
|
61
|
+
*/
|
|
62
|
+
export class ChartCanvas extends Node2D<ChartCanvasProps> {
|
|
63
|
+
@property({
|
|
64
|
+
default: DEFAULT_PLOT_AREA.fill,
|
|
65
|
+
mapper: fillOps.resolve,
|
|
66
|
+
tween: fillOps.lerp,
|
|
67
|
+
})
|
|
68
|
+
declare fill: Fill
|
|
69
|
+
@property({
|
|
70
|
+
default: DEFAULT_PLOT_AREA.shadow,
|
|
71
|
+
mapper: shadowOps.resolve,
|
|
72
|
+
tween: shadowOps.lerp,
|
|
73
|
+
})
|
|
74
|
+
declare shadow: Shadow
|
|
75
|
+
@property({
|
|
76
|
+
default: DEFAULT_X_AXIS.lineStyle,
|
|
77
|
+
mapper: strokeOps.resolve,
|
|
78
|
+
tween: strokeOps.lerp,
|
|
79
|
+
})
|
|
80
|
+
declare xGridStroke: Stroke
|
|
81
|
+
@property({
|
|
82
|
+
default: DEFAULT_Y_AXIS.lineStyle,
|
|
83
|
+
mapper: strokeOps.resolve,
|
|
84
|
+
tween: strokeOps.lerp,
|
|
85
|
+
})
|
|
86
|
+
declare yGridStroke: Stroke
|
|
87
|
+
@property({ default: () => [] as number[] }) declare xMarkers: number[]
|
|
88
|
+
@property({ default: () => [] as number[] }) declare yMarkers: number[]
|
|
89
|
+
@property({ default: () => [0, 1] as Range }) declare xExtent: Range
|
|
90
|
+
@property({ default: () => [0, 1] as Range }) declare yExtent: Range
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
|
|
94
|
+
* paint props it carries — see {@link declarePaints}. The grid strokes are
|
|
95
|
+
* named past the conventional slots, so they are passed explicitly.
|
|
96
|
+
*/
|
|
97
|
+
override declareAssets(assets: AssetScope): void {
|
|
98
|
+
super.declareAssets(assets)
|
|
99
|
+
declarePaints(this, assets, {
|
|
100
|
+
strokes: [this.xGridStroke as StrokeResolved[], this.yGridStroke as StrokeResolved[]],
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected renderSelf(ctx: RenderContext2D): void {
|
|
105
|
+
const W = this.layoutBounds.width
|
|
106
|
+
const H = this.layoutBounds.height
|
|
107
|
+
if (W <= 0 || H <= 0) return
|
|
108
|
+
|
|
109
|
+
// Plot panel, centred on the origin (node space is y-up, origin centre).
|
|
110
|
+
ctx.draw(
|
|
111
|
+
new Graphics2D()
|
|
112
|
+
.rect({ x: 0, y: 0, width: W, height: H })
|
|
113
|
+
.shadow(this.shadow)
|
|
114
|
+
.fill(this.fill)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
const [xMin, xMax] = this.xExtent
|
|
118
|
+
const [yMin, yMax] = this.yExtent
|
|
119
|
+
// Fraction of a marker across its extent; guard a zero-width extent.
|
|
120
|
+
const xFrac = (v: number) => (xMax <= xMin ? 0 : (v - xMin) / (xMax - xMin))
|
|
121
|
+
const yFrac = (v: number) => (yMax <= yMin ? 0 : (v - yMin) / (yMax - yMin))
|
|
122
|
+
// Skip markers landing on (or outside) the edges — they'd sit under the border.
|
|
123
|
+
const interior = (f: number) => f > 1e-6 && f < 1 - 1e-6
|
|
124
|
+
|
|
125
|
+
const xStroke = (this.xGridStroke as StrokeResolved[])[0]
|
|
126
|
+
if (xStroke) {
|
|
127
|
+
const xGrid = new Graphics2D()
|
|
128
|
+
for (const v of this.xMarkers) {
|
|
129
|
+
const f = xFrac(v)
|
|
130
|
+
if (!interior(f)) continue
|
|
131
|
+
xGrid.rect({
|
|
132
|
+
x: -W / 2 + f * W,
|
|
133
|
+
y: 0,
|
|
134
|
+
width: xStroke.weight,
|
|
135
|
+
height: H,
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
ctx.draw(xGrid.fill(xStroke.fill))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const yStroke = (this.yGridStroke as StrokeResolved[])[0]
|
|
142
|
+
if (yStroke) {
|
|
143
|
+
const yGrid = new Graphics2D()
|
|
144
|
+
for (const v of this.yMarkers) {
|
|
145
|
+
const f = yFrac(v)
|
|
146
|
+
if (!interior(f)) continue
|
|
147
|
+
yGrid.rect({
|
|
148
|
+
x: 0,
|
|
149
|
+
y: -H / 2 + f * H,
|
|
150
|
+
width: W,
|
|
151
|
+
height: yStroke.weight,
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
ctx.draw(yGrid.fill(yStroke.fill))
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|