@motionscript/charts 0.1.0-alpha.5 → 0.1.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +30 -16
- package/dist/bar-chart/bar-chart.d.ts +93 -246
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +378 -569
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +24 -41
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +93 -115
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +36 -70
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +156 -210
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +2 -2
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +1 -1
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +3 -3
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +3 -3
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
- package/dist/browser/chunks/chunk-XROBIRQK.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/engine.d.ts +8 -6
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +8 -6
- package/dist/engine.js.map +1 -1
- package/dist/kit/cartesian.d.ts +44 -0
- package/dist/kit/cartesian.d.ts.map +1 -0
- package/dist/kit/cartesian.js +59 -0
- package/dist/kit/cartesian.js.map +1 -0
- package/dist/kit/chart-border.d.ts +8 -11
- package/dist/kit/chart-border.d.ts.map +1 -1
- package/dist/kit/chart-border.js +15 -32
- package/dist/kit/chart-border.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +23 -21
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +33 -90
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +79 -67
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +129 -154
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.d.ts +17 -0
- package/dist/kit/entries.d.ts.map +1 -0
- package/dist/kit/entries.js +19 -0
- package/dist/kit/entries.js.map +1 -0
- package/dist/kit/motion.d.ts +48 -0
- package/dist/kit/motion.d.ts.map +1 -0
- package/dist/kit/motion.js +63 -0
- package/dist/kit/motion.js.map +1 -0
- package/dist/kit/paints.d.ts +10 -0
- package/dist/kit/paints.d.ts.map +1 -0
- package/dist/kit/paints.js +12 -0
- package/dist/kit/paints.js.map +1 -0
- package/dist/kit/shared.d.ts +31 -3
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +42 -3
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +36 -61
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +97 -132
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +25 -51
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +89 -120
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +14 -14
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +26 -64
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +32 -56
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +85 -123
- package/dist/line-chart/horizontal-axis.js.map +1 -1
- package/dist/line-chart/index.d.ts +2 -2
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +1 -1
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +111 -319
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +403 -663
- 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 +3 -3
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/nodes.d.ts +6 -9
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +7 -9
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +2 -2
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +77 -134
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +200 -271
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +54 -31
- package/dist/pie-chart/pie-legend.d.ts.map +1 -1
- package/dist/pie-chart/pie-legend.js +93 -107
- package/dist/pie-chart/pie-legend.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +142 -131
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +333 -391
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/package.json +4 -4
- package/src/bar-chart/bar-chart.ts +415 -701
- package/src/bar-chart/category-axis.ts +95 -110
- package/src/bar-chart/chart-bars.ts +178 -200
- package/src/bar-chart/index.ts +2 -2
- package/src/bar-chart/shared.ts +3 -3
- package/src/engine.ts +8 -6
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +22 -33
- package/src/kit/chart-canvas.ts +44 -79
- package/src/kit/chart-legend.ts +173 -189
- package/src/kit/entries.ts +20 -0
- package/src/kit/motion.ts +90 -0
- package/src/kit/paints.ts +15 -0
- package/src/kit/shared.ts +52 -3
- package/src/kit/vertical-axis.ts +110 -140
- package/src/line-chart/chart-line.ts +95 -122
- package/src/line-chart/chart-region.ts +30 -59
- package/src/line-chart/horizontal-axis.ts +99 -135
- package/src/line-chart/index.ts +2 -2
- package/src/line-chart/line-chart.ts +449 -825
- package/src/line-chart/shared.ts +3 -3
- package/src/nodes.ts +8 -10
- package/src/pie-chart/index.ts +2 -2
- package/src/pie-chart/pie-chart.ts +221 -311
- package/src/pie-chart/pie-legend.ts +124 -114
- package/src/pie-chart/pie-ring.ts +386 -473
- package/dist/browser/chunks/chunk-5ZPBV6BU.js +0 -2
- package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +0 -7
package/src/bar-chart/index.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* which is what lets a scene swap one for the other without re-theming — and
|
|
6
6
|
* brings its own category axis, since a band scale is not a continuous one.
|
|
7
7
|
*/
|
|
8
|
-
export { BarChart } from "./bar-chart"
|
|
9
|
-
export type { BarChartProps } from "./bar-chart"
|
|
8
|
+
export { BarChart, barChartDomain } from "./bar-chart"
|
|
9
|
+
export type { BarChartDomain, BarChartProps, BarChartValues } from "./bar-chart"
|
|
10
10
|
export { ChartBars } from "./chart-bars"
|
|
11
11
|
export type { BarGeometry, ChartBarsProps } from "./chart-bars"
|
|
12
12
|
export { CategoryAxis } from "./category-axis"
|
package/src/bar-chart/shared.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*
|
|
10
10
|
* Kept renderer-agnostic (no `Node` imports) so a host's inspector mapping
|
|
11
11
|
* can build a partial {@link BarChartTheme} without
|
|
12
|
-
* pulling the
|
|
12
|
+
* pulling the components in.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import {
|
|
@@ -294,7 +294,7 @@ function lerpBarStyle(
|
|
|
294
294
|
}
|
|
295
295
|
|
|
296
296
|
/**
|
|
297
|
-
* {@link BarChart}'s `theme`
|
|
297
|
+
* {@link BarChart}'s `theme` entry's resolve: merges the author's partial
|
|
298
298
|
* {@link BarChartTheme} onto `previous` (falling back to
|
|
299
299
|
* {@link DEFAULT_BAR_CHART_THEME} on first resolution), fully resolving every
|
|
300
300
|
* leaf so the tween below always has resolved data to interpolate.
|
|
@@ -318,7 +318,7 @@ export function resolveBarChartTheme(
|
|
|
318
318
|
}
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
-
/** {@link BarChart}'s `theme`
|
|
321
|
+
/** {@link BarChart}'s `theme` entry's lerp — leaf by leaf. */
|
|
322
322
|
export function lerpBarChartTheme(
|
|
323
323
|
from: BarChartConfig,
|
|
324
324
|
to: BarChartConfig,
|
package/src/engine.ts
CHANGED
|
@@ -7,20 +7,22 @@
|
|
|
7
7
|
* axis, the legend and the number formatting stay a real dependency. That is
|
|
8
8
|
* not a size decision — it is the only arrangement that works.
|
|
9
9
|
*
|
|
10
|
-
* A chart identifies its own parts
|
|
11
|
-
*
|
|
12
|
-
* and
|
|
13
|
-
*
|
|
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
|
|
10
|
+
* A chart identifies its own parts by component identity (`VerticalAxis.is`).
|
|
11
|
+
* Copy `VerticalAxis` alongside your fork and there are now two of it — two
|
|
12
|
+
* components answering one name, and whichever one your chart did not get is
|
|
13
|
+
* a part it never finds, with no error. Keeping the kit external makes that
|
|
16
14
|
* impossible rather than unlikely.
|
|
17
15
|
*
|
|
18
16
|
* It also means a fix to the axis reaches a forked chart without a re-add,
|
|
19
17
|
* which is the half of the bargain you get back for it.
|
|
20
18
|
*/
|
|
21
19
|
export * from "./kit/shared"
|
|
20
|
+
export * from "./kit/cartesian"
|
|
22
21
|
export * from "./kit/chart-border"
|
|
23
22
|
export * from "./kit/chart-canvas"
|
|
24
23
|
export * from "./kit/chart-legend"
|
|
24
|
+
export * from "./kit/entries"
|
|
25
|
+
export * from "./kit/motion"
|
|
26
|
+
export * from "./kit/paints"
|
|
25
27
|
export * from "./kit/vertical-axis"
|
|
26
28
|
export * from "@motionscript/sdk/component"
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Rect, type ChildView, type Node2D, type Size2D } from "@motionscript/sdk"
|
|
2
|
+
|
|
3
|
+
import { VerticalAxis } from "./vertical-axis"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Headroom above the plot, in px.
|
|
7
|
+
*
|
|
8
|
+
* Two jobs, and the second is why it is real padding on the composition rather
|
|
9
|
+
* than a number the point-mapping math imagines: a series peaking at the top of
|
|
10
|
+
* the scale isn't flush to the top edge, **and** the top marker, which is
|
|
11
|
+
* centred on that top grid line, has somewhere to be. Without it the marker's
|
|
12
|
+
* upper half is drawn above the chart's own box.
|
|
13
|
+
*/
|
|
14
|
+
export const TOP_HEADROOM = 24
|
|
15
|
+
|
|
16
|
+
/** The parts of a cartesian chart's frame, as {@link cartesianFrame} arranges them. */
|
|
17
|
+
export interface CartesianParts {
|
|
18
|
+
/** The value axis, left of the plot. */
|
|
19
|
+
readonly axis: Node2D
|
|
20
|
+
/** The plot panel: the box the grid and the marks share. */
|
|
21
|
+
readonly plot: Node2D
|
|
22
|
+
/** An invisible twin of the value axis, reserving its gutter under the plot. */
|
|
23
|
+
readonly gutter: Node2D
|
|
24
|
+
/** What sits under the plot, top to bottom: the other axis, then the legend. */
|
|
25
|
+
readonly below: readonly Node2D[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The frame the line and bar charts share: the value axis beside the plot, and
|
|
30
|
+
* under them the gutter beside the rows below the plot, so those rows start at
|
|
31
|
+
* the plot's left edge rather than the chart's.
|
|
32
|
+
*/
|
|
33
|
+
export function cartesianFrame(parts: CartesianParts): Node2D {
|
|
34
|
+
return new Rect({
|
|
35
|
+
flow: "vertical",
|
|
36
|
+
width: "fill",
|
|
37
|
+
height: "fill",
|
|
38
|
+
padding: { top: TOP_HEADROOM },
|
|
39
|
+
children: [
|
|
40
|
+
new Rect({ flow: "horizontal", height: "fill", width: "fill", children: [parts.axis, parts.plot] }),
|
|
41
|
+
new Rect({
|
|
42
|
+
flow: "horizontal",
|
|
43
|
+
width: "fill",
|
|
44
|
+
children: [parts.gutter, new Rect({ flow: "vertical", width: "fill", gap: 20, children: [...parts.below] })],
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
}) as unknown as Node2D
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A cartesian frame's parts, as a command reaches them through its chart's children. */
|
|
51
|
+
export interface CartesianViews {
|
|
52
|
+
readonly axis: ChildView
|
|
53
|
+
/** The plot panel's own children: the grid, the marks, the border. */
|
|
54
|
+
readonly plot: readonly ChildView[]
|
|
55
|
+
readonly below: readonly ChildView[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The frame among a chart's children: the last child shaped as
|
|
60
|
+
* {@link cartesianFrame} builds it. The chart builds it after the children it
|
|
61
|
+
* was given, and a document adds its own later still, so it is found by shape.
|
|
62
|
+
*/
|
|
63
|
+
export function cartesianViews(children: readonly ChildView[]): CartesianViews | null {
|
|
64
|
+
for (let i = children.length - 1; i >= 0; i--) {
|
|
65
|
+
const frame = children[i]
|
|
66
|
+
if (!Rect.is(frame)) continue
|
|
67
|
+
const [top, bottom] = frame.children
|
|
68
|
+
const axis = top?.children[0]
|
|
69
|
+
const plot = top?.children[1]
|
|
70
|
+
const below = bottom?.children[1]
|
|
71
|
+
if (!axis || !VerticalAxis.is(axis) || !plot || !below) continue
|
|
72
|
+
return { axis, plot: plot.children, below: below.children }
|
|
73
|
+
}
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** A part's laid-out size, or nothing before it is built or laid out. */
|
|
78
|
+
export function laidOutSize(node: Node2D | undefined): Size2D {
|
|
79
|
+
const bounds = node?.layoutBounds
|
|
80
|
+
return { width: bounds?.width ?? 0, height: bounds?.height ?? 0 }
|
|
81
|
+
}
|
package/src/kit/chart-border.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
|
-
type AssetScope,
|
|
3
2
|
Graphics2D,
|
|
4
|
-
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
5
5
|
strokeOps,
|
|
6
|
-
|
|
6
|
+
type NodeOf,
|
|
7
|
+
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
8
|
+
type Node2D as _Node2D,
|
|
7
9
|
type Node2DProps,
|
|
8
|
-
type RenderContext2D,
|
|
9
10
|
type Stroke,
|
|
11
|
+
type StrokeResolved,
|
|
10
12
|
} from "@motionscript/sdk"
|
|
11
13
|
|
|
14
|
+
import { loadPaints } from "./paints"
|
|
12
15
|
import { DEFAULT_PLOT_AREA } from "./shared"
|
|
13
|
-
import { declarePaints } from "@motionscript/sdk/component"
|
|
14
16
|
|
|
15
17
|
export interface ChartBorderProps extends Node2DProps {
|
|
16
18
|
/**
|
|
@@ -23,39 +25,26 @@ export interface ChartBorderProps extends Node2DProps {
|
|
|
23
25
|
|
|
24
26
|
/**
|
|
25
27
|
* The thick **frame** around a cartesian chart's plot: an empty rect wearing an
|
|
26
|
-
* inside stroke, sized from its
|
|
28
|
+
* inside stroke, sized from its box.
|
|
27
29
|
*
|
|
28
30
|
* Split out of {@link ChartCanvas} (the filled + gridded backdrop) so it can be
|
|
29
31
|
* placed *after* the marks in the tree — the box then frames the plot **over**
|
|
30
32
|
* them, so a line or a column running to the edge tucks cleanly under the border
|
|
31
33
|
* rather than sitting on top of it.
|
|
32
34
|
*/
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
})
|
|
39
|
-
|
|
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
|
-
|
|
35
|
+
export const ChartBorder = component2d({
|
|
36
|
+
key: "chartBorder",
|
|
37
|
+
state: {
|
|
38
|
+
stroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_PLOT_AREA.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
39
|
+
},
|
|
40
|
+
load: (loader, state) => loadPaints(loader, { strokes: [state.stroke] }),
|
|
41
|
+
render: (ctx, state) => {
|
|
42
|
+
const { width, height } = ctx.size
|
|
43
|
+
if (width <= 0 || height <= 0) return
|
|
55
44
|
// Empty rect (no fill) with the themed stroke, so the frame sits over the
|
|
56
45
|
// marks but the box interior stays clear.
|
|
57
|
-
ctx.draw(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
46
|
+
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width, height }).stroke(state.stroke))
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
export type ChartBorder = NodeOf<typeof ChartBorder>
|
package/src/kit/chart-canvas.ts
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
|
-
type AssetScope,
|
|
3
2
|
Graphics2D,
|
|
4
|
-
|
|
3
|
+
Types,
|
|
4
|
+
component2d,
|
|
5
5
|
fillOps,
|
|
6
6
|
shadowOps,
|
|
7
7
|
strokeOps,
|
|
8
|
-
property,
|
|
9
8
|
type Fill,
|
|
9
|
+
type FillResolved,
|
|
10
|
+
type NodeOf,
|
|
11
|
+
// Named so this component's emitted type reaches `Node2D` through the sdk, not core.
|
|
12
|
+
type Node2D as _Node2D,
|
|
10
13
|
type Node2DProps,
|
|
11
|
-
type RenderContext2D,
|
|
12
14
|
type Shadow,
|
|
15
|
+
type ShadowResolved,
|
|
13
16
|
type Stroke,
|
|
14
17
|
type StrokeResolved,
|
|
15
18
|
} from "@motionscript/sdk"
|
|
16
19
|
|
|
20
|
+
import { fresh } from "./entries"
|
|
21
|
+
import { loadPaints } from "./paints"
|
|
17
22
|
import {
|
|
18
23
|
DEFAULT_PLOT_AREA,
|
|
19
24
|
DEFAULT_X_AXIS,
|
|
20
25
|
DEFAULT_Y_AXIS,
|
|
21
26
|
type Range,
|
|
22
27
|
} from "./shared"
|
|
23
|
-
import { declarePaints } from "@motionscript/sdk/component"
|
|
24
28
|
|
|
25
29
|
export interface ChartCanvasProps extends Node2DProps {
|
|
26
30
|
/** Fill of the plot panel behind the marks. */
|
|
@@ -47,11 +51,13 @@ export interface ChartCanvasProps extends Node2DProps {
|
|
|
47
51
|
yExtent: Range
|
|
48
52
|
}
|
|
49
53
|
|
|
54
|
+
const strokeEntry = (initial: Stroke) =>
|
|
55
|
+
Types.custom<Stroke, StrokeResolved[]>(initial, { resolve: strokeOps.resolve, lerp: strokeOps.lerp })
|
|
56
|
+
|
|
50
57
|
/**
|
|
51
58
|
* The plot **backdrop** of a cartesian chart: a filled panel with a grid of
|
|
52
|
-
* faint lines spanning both directions across it
|
|
53
|
-
*
|
|
54
|
-
* origin.
|
|
59
|
+
* faint lines spanning both directions across it, sized from its box and centred
|
|
60
|
+
* on its origin.
|
|
55
61
|
*
|
|
56
62
|
* The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
|
|
57
63
|
* *over* the marks while this panel sits under them. Grid lines are placed from
|
|
@@ -59,99 +65,58 @@ export interface ChartCanvasProps extends Node2DProps {
|
|
|
59
65
|
* {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
|
|
60
66
|
* marker whether or not the markers are evenly spaced.
|
|
61
67
|
*/
|
|
62
|
-
export
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
68
|
+
export const ChartCanvas = component2d({
|
|
69
|
+
key: "chartCanvas",
|
|
70
|
+
state: {
|
|
71
|
+
fill: Types.custom<Fill, FillResolved[]>(DEFAULT_PLOT_AREA.fill, { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
72
|
+
shadow: Types.custom<Shadow, ShadowResolved[]>(DEFAULT_PLOT_AREA.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),
|
|
73
|
+
xGridStroke: strokeEntry(DEFAULT_X_AXIS.lineStyle),
|
|
74
|
+
yGridStroke: strokeEntry(DEFAULT_Y_AXIS.lineStyle),
|
|
75
|
+
xMarkers: fresh<number[]>(() => []),
|
|
76
|
+
yMarkers: fresh<number[]>(() => []),
|
|
77
|
+
xExtent: fresh<Range>(() => [0, 1]),
|
|
78
|
+
yExtent: fresh<Range>(() => [0, 1]),
|
|
79
|
+
},
|
|
80
|
+
// The grid strokes are named past the conventional slots, so they are named here.
|
|
81
|
+
load: (loader, state) =>
|
|
82
|
+
loadPaints(loader, { fills: [state.fill], shadows: [state.shadow], strokes: [state.xGridStroke, state.yGridStroke] }),
|
|
83
|
+
render: (ctx, state) => {
|
|
84
|
+
const { width: W, height: H } = ctx.size
|
|
107
85
|
if (W <= 0 || H <= 0) return
|
|
108
86
|
|
|
109
87
|
// 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
|
-
)
|
|
88
|
+
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).shadow(state.shadow).fill(state.fill))
|
|
116
89
|
|
|
117
|
-
const [xMin, xMax] =
|
|
118
|
-
const [yMin, yMax] =
|
|
90
|
+
const [xMin, xMax] = state.xExtent
|
|
91
|
+
const [yMin, yMax] = state.yExtent
|
|
119
92
|
// Fraction of a marker across its extent; guard a zero-width extent.
|
|
120
93
|
const xFrac = (v: number) => (xMax <= xMin ? 0 : (v - xMin) / (xMax - xMin))
|
|
121
94
|
const yFrac = (v: number) => (yMax <= yMin ? 0 : (v - yMin) / (yMax - yMin))
|
|
122
95
|
// Skip markers landing on (or outside) the edges — they'd sit under the border.
|
|
123
96
|
const interior = (f: number) => f > 1e-6 && f < 1 - 1e-6
|
|
124
97
|
|
|
125
|
-
const xStroke =
|
|
98
|
+
const xStroke = state.xGridStroke[0]
|
|
126
99
|
if (xStroke) {
|
|
127
100
|
const xGrid = new Graphics2D()
|
|
128
|
-
for (const v of
|
|
101
|
+
for (const v of state.xMarkers) {
|
|
129
102
|
const f = xFrac(v)
|
|
130
103
|
if (!interior(f)) continue
|
|
131
|
-
xGrid.rect({
|
|
132
|
-
x: -W / 2 + f * W,
|
|
133
|
-
y: 0,
|
|
134
|
-
width: xStroke.weight,
|
|
135
|
-
height: H,
|
|
136
|
-
})
|
|
104
|
+
xGrid.rect({ x: -W / 2 + f * W, y: 0, width: xStroke.weight, height: H })
|
|
137
105
|
}
|
|
138
106
|
ctx.draw(xGrid.fill(xStroke.fill))
|
|
139
107
|
}
|
|
140
108
|
|
|
141
|
-
const yStroke =
|
|
109
|
+
const yStroke = state.yGridStroke[0]
|
|
142
110
|
if (yStroke) {
|
|
143
111
|
const yGrid = new Graphics2D()
|
|
144
|
-
for (const v of
|
|
112
|
+
for (const v of state.yMarkers) {
|
|
145
113
|
const f = yFrac(v)
|
|
146
114
|
if (!interior(f)) continue
|
|
147
|
-
yGrid.rect({
|
|
148
|
-
x: 0,
|
|
149
|
-
y: -H / 2 + f * H,
|
|
150
|
-
width: W,
|
|
151
|
-
height: yStroke.weight,
|
|
152
|
-
})
|
|
115
|
+
yGrid.rect({ x: 0, y: -H / 2 + f * H, width: W, height: yStroke.weight })
|
|
153
116
|
}
|
|
154
117
|
ctx.draw(yGrid.fill(yStroke.fill))
|
|
155
118
|
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
export type ChartCanvas = NodeOf<typeof ChartCanvas>
|