@motionscript/charts 0.1.0-alpha.3 → 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 +63 -0
- package/LICENSE +201 -201
- package/NOTICE +48 -0
- package/README.md +99 -0
- package/dist/bar-chart/bar-chart.d.ts +93 -231
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +378 -565
- 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 +6 -6
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +6 -6
- 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 +3 -2
- 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/index.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 -88
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +137 -186
- 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 +59 -8
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +105 -10
- 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 +27 -53
- 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 -311
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +403 -639
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +6 -6
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +22 -7
- 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.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 +199 -268
- 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 -122
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +333 -361
- 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 +534 -814
- package/src/bar-chart/category-axis.ts +162 -177
- package/src/bar-chart/chart-bars.ts +288 -310
- package/src/bar-chart/index.ts +31 -31
- package/src/bar-chart/shared.ts +354 -354
- package/src/engine.ts +28 -26
- package/src/index.ts +5 -5
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +50 -61
- package/src/kit/chart-canvas.ts +122 -157
- package/src/kit/chart-legend.ts +229 -250
- 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 +782 -675
- package/src/kit/vertical-axis.ts +194 -224
- package/src/line-chart/chart-line.ts +181 -208
- package/src/line-chart/chart-region.ts +106 -135
- package/src/line-chart/horizontal-axis.ts +166 -202
- package/src/line-chart/index.ts +39 -39
- package/src/line-chart/line-chart.ts +585 -929
- package/src/line-chart/shared.ts +473 -459
- package/src/line-chart/x-scale.ts +367 -367
- package/src/nodes.ts +18 -20
- package/src/pie-chart/index.ts +37 -37
- package/src/pie-chart/pie-chart.ts +293 -381
- package/src/pie-chart/pie-legend.ts +169 -159
- package/src/pie-chart/pie-ring.ts +848 -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
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type { MarkerLabel } from "../kit/vertical-axis.js";
|
|
1
|
+
import { type Fill, type FillResolved, type NodeOf, type Node2D as _Node2D, type Node2DProps, type Shadow, type ShadowResolved, type Stroke, type StrokeResolved, type TextStyle } from "@motionscript/sdk";
|
|
3
2
|
/**
|
|
4
3
|
* One drawn column, already mapped into the plot's **centred pixel space**
|
|
5
4
|
* (origin at the plot centre, y-up) by the parent {@link BarChart}.
|
|
@@ -15,12 +14,18 @@ export interface BarGeometry {
|
|
|
15
14
|
width: number;
|
|
16
15
|
/** Pixel y of the column's base — the baseline when grouped, the running stack top when stacked. */
|
|
17
16
|
from: number;
|
|
18
|
-
/** Pixel y of its far end, at full {@link
|
|
17
|
+
/** Pixel y of its far end, at full {@link ChartBarsProps.growth}. */
|
|
19
18
|
to: number;
|
|
20
19
|
/** Whether the far end takes the theme's corner radius (the stack's cap). */
|
|
21
20
|
round: boolean;
|
|
22
21
|
/** The value this column plots, for the figure printed on it. */
|
|
23
22
|
value: number;
|
|
23
|
+
/**
|
|
24
|
+
* The figure printed on it, written as the chart's value axis writes its
|
|
25
|
+
* ticks, so the two cannot disagree. Absent, the bare marker reading of
|
|
26
|
+
* {@link value}.
|
|
27
|
+
*/
|
|
28
|
+
caption?: string;
|
|
24
29
|
}
|
|
25
30
|
export interface ChartBarsProps extends Node2DProps {
|
|
26
31
|
/** This series' columns, one per row of the chart's data. */
|
|
@@ -79,71 +84,32 @@ export interface ChartBarsProps extends Node2DProps {
|
|
|
79
84
|
* and ride the node's own opacity — which is why hiding the series takes its
|
|
80
85
|
* figures with it for free.
|
|
81
86
|
*/
|
|
82
|
-
export declare
|
|
83
|
-
bars: BarGeometry[]
|
|
84
|
-
baseline:
|
|
85
|
-
fill: Fill
|
|
86
|
-
barStroke: Stroke
|
|
87
|
-
barShadow: Shadow
|
|
88
|
-
cornerRadius: number;
|
|
89
|
-
growth:
|
|
90
|
-
dims: number[]
|
|
91
|
-
showValues:
|
|
92
|
-
valueStyle: TextStyle
|
|
93
|
-
enabled:
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
/** The two ends of column `i` at the current {@link growth}. */
|
|
111
|
-
private endsAt;
|
|
112
|
-
/** The type size the figures are set at, guarding the autofit/unset cases. */
|
|
113
|
-
private get valueSize();
|
|
114
|
-
/**
|
|
115
|
-
* The figure printed on column `i`, or `""` where there is no room for it.
|
|
116
|
-
*
|
|
117
|
-
* Blanking the text rather than dropping the node keeps the caption list
|
|
118
|
-
* index-aligned with the columns, so a chart that rescales into more room
|
|
119
|
-
* simply starts printing again.
|
|
120
|
-
*/
|
|
121
|
-
private captionAt;
|
|
122
|
-
/** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */
|
|
123
|
-
private insideAt;
|
|
124
|
-
/** Where column `i`'s figure sits, in the node's centred y-up space. */
|
|
125
|
-
private captionY;
|
|
126
|
-
/**
|
|
127
|
-
* This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
|
|
128
|
-
* paint props it carries — see {@link declarePaints}. The column stroke and
|
|
129
|
-
* shadow are named past the conventional slots, so they are passed explicitly.
|
|
130
|
-
*/
|
|
131
|
-
declareAssets(assets: AssetScope): void;
|
|
132
|
-
protected renderSelf(ctx: RenderContext2D): void;
|
|
133
|
-
/**
|
|
134
|
-
* Raise every column from the baseline by tweening {@link growth} `0 → 1`.
|
|
135
|
-
*
|
|
136
|
-
* The mirror of `ChartLine.enter`: same signature, same contract that the
|
|
137
|
-
* resting state is the finished one, so the chart arms this back to 0 before
|
|
138
|
-
* animating rather than the node hiding itself at construction.
|
|
139
|
-
*/
|
|
140
|
-
enter(args: CommandArgs<Record<string, never>> & {
|
|
141
|
-
duration: number;
|
|
142
|
-
}): Command<ChartBarsGrowth>;
|
|
143
|
-
}
|
|
144
|
-
/** The one signal {@link ChartBars.enter} moves. */
|
|
145
|
-
type ChartBarsGrowth = {
|
|
146
|
-
growth: number;
|
|
147
|
-
};
|
|
148
|
-
export {};
|
|
87
|
+
export declare const ChartBars: import("@motionscript/sdk").Component<"chartBars", import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
|
|
88
|
+
readonly bars: import("@motionscript/sdk").Type<BarGeometry[], BarGeometry[], false>;
|
|
89
|
+
readonly baseline: 0;
|
|
90
|
+
readonly fill: import("@motionscript/sdk").Type<Fill, FillResolved[], false>;
|
|
91
|
+
readonly barStroke: import("@motionscript/sdk").Type<Stroke, StrokeResolved[], false>;
|
|
92
|
+
readonly barShadow: import("@motionscript/sdk").Type<Shadow, ShadowResolved[], false>;
|
|
93
|
+
readonly cornerRadius: number;
|
|
94
|
+
readonly growth: 1;
|
|
95
|
+
readonly dims: import("@motionscript/sdk").Type<number[], number[], false>;
|
|
96
|
+
readonly showValues: false;
|
|
97
|
+
readonly valueStyle: import("@motionscript/sdk").Type<TextStyle, TextStyle, false>;
|
|
98
|
+
readonly enabled: true;
|
|
99
|
+
}>, {
|
|
100
|
+
readonly enter: unknown;
|
|
101
|
+
}, null, _Node2D<Node2DProps>, import("@motionscript/sdk").CallProps<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
|
|
102
|
+
readonly bars: import("@motionscript/sdk").Type<BarGeometry[], BarGeometry[], false>;
|
|
103
|
+
readonly baseline: 0;
|
|
104
|
+
readonly fill: import("@motionscript/sdk").Type<Fill, FillResolved[], false>;
|
|
105
|
+
readonly barStroke: import("@motionscript/sdk").Type<Stroke, StrokeResolved[], false>;
|
|
106
|
+
readonly barShadow: import("@motionscript/sdk").Type<Shadow, ShadowResolved[], false>;
|
|
107
|
+
readonly cornerRadius: number;
|
|
108
|
+
readonly growth: 1;
|
|
109
|
+
readonly dims: import("@motionscript/sdk").Type<number[], number[], false>;
|
|
110
|
+
readonly showValues: false;
|
|
111
|
+
readonly valueStyle: import("@motionscript/sdk").Type<TextStyle, TextStyle, false>;
|
|
112
|
+
readonly enabled: true;
|
|
113
|
+
}>, null>>;
|
|
114
|
+
export type ChartBars = NodeOf<typeof ChartBars>;
|
|
149
115
|
//# sourceMappingURL=chart-bars.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chart-bars.d.ts","sourceRoot":"","sources":["../../src/bar-chart/chart-bars.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"chart-bars.d.ts","sourceRoot":"","sources":["../../src/bar-chart/chart-bars.ts"],"names":[],"mappings":"AAA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CAAA;AAEF,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,SAAS,CAAC,CAAA"}
|
|
@@ -1,19 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
import { command, Graphics2D, Node2D, Text, easeOut, fillOps, shadowOps, strokeOps, property, textOps, } from "@motionscript/sdk";
|
|
1
|
+
import { Graphics2D, Text, Types, component2d, fillOps, shadowOps, strokeOps, textOps, } from "@motionscript/sdk";
|
|
2
|
+
import { fresh } from "../kit/entries.js";
|
|
3
|
+
import { frameEase } from "../kit/motion.js";
|
|
4
|
+
import { loadPaints } from "../kit/paints.js";
|
|
8
5
|
import { clampUnit, formatMarker } from "../kit/shared.js";
|
|
9
6
|
import { DEFAULT_BAR_CHART_THEME } from "./shared.js";
|
|
10
|
-
import { declarePaints } from "@motionscript/sdk/component";
|
|
11
7
|
/** Gap between a grouped column's end and the figure printed over it, in px. */
|
|
12
8
|
const VALUE_GAP = 10;
|
|
13
9
|
/** A segment shorter than this many multiples of the type size prints no figure. */
|
|
14
10
|
const VALUE_FIT = 1.6;
|
|
15
11
|
/** Fallback type size when the theme's value style leaves it unset or autofit. */
|
|
16
12
|
const VALUE_SIZE = 24;
|
|
13
|
+
/**
|
|
14
|
+
* How far column `i` is dimmed — the chart's category spotlight, and the series
|
|
15
|
+
* switch on top of it.
|
|
16
|
+
*
|
|
17
|
+
* The captions read this rather than being hidden by `render`'s guard: they are
|
|
18
|
+
* real child nodes, so a `return` in the draw would leave them on screen over a
|
|
19
|
+
* series that isn't there.
|
|
20
|
+
*/
|
|
21
|
+
function dimAt(s, index) {
|
|
22
|
+
if (!s.enabled)
|
|
23
|
+
return 0;
|
|
24
|
+
return clampUnit(s.dims[index] ?? 1);
|
|
25
|
+
}
|
|
26
|
+
/** The two ends of column `i` at the current growth. */
|
|
27
|
+
function endsAt(s, index) {
|
|
28
|
+
const bar = s.bars[index];
|
|
29
|
+
if (!bar)
|
|
30
|
+
return null;
|
|
31
|
+
const g = clampUnit(s.growth);
|
|
32
|
+
return { base: s.baseline + (bar.from - s.baseline) * g, end: s.baseline + (bar.to - s.baseline) * g };
|
|
33
|
+
}
|
|
34
|
+
/** The type size the figures are set at, guarding the autofit/unset cases. */
|
|
35
|
+
function valueSize(s) {
|
|
36
|
+
const size = s.valueStyle.fontSize;
|
|
37
|
+
return typeof size === "number" ? size : VALUE_SIZE;
|
|
38
|
+
}
|
|
39
|
+
/** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */
|
|
40
|
+
function insideAt(s, index) {
|
|
41
|
+
const bar = s.bars[index];
|
|
42
|
+
return bar !== undefined && Math.abs(bar.from - s.baseline) > 0.5;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The figure printed on column `i`, or `""` where there is no room for it.
|
|
46
|
+
*
|
|
47
|
+
* Blanking the text rather than dropping the node keeps the caption list
|
|
48
|
+
* index-aligned with the columns, so a chart that rescales into more room simply
|
|
49
|
+
* starts printing again.
|
|
50
|
+
*/
|
|
51
|
+
function captionAt(s, index) {
|
|
52
|
+
const bar = s.bars[index];
|
|
53
|
+
if (!bar || bar.value === 0)
|
|
54
|
+
return "";
|
|
55
|
+
// An interior segment prints inside itself, so it has to be tall enough to
|
|
56
|
+
// hold a line of type; a grouped column prints in the headroom above its end,
|
|
57
|
+
// where the only constraint is the plot's own top.
|
|
58
|
+
if (insideAt(s, index)) {
|
|
59
|
+
const ends = endsAt(s, index);
|
|
60
|
+
if (!ends)
|
|
61
|
+
return "";
|
|
62
|
+
if (Math.abs(ends.end - ends.base) < valueSize(s) * VALUE_FIT)
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
return bar.caption ?? formatMarker(bar.value);
|
|
66
|
+
}
|
|
67
|
+
/** Where column `i`'s figure sits, in the node's centred y-up space. */
|
|
68
|
+
function captionY(s, index) {
|
|
69
|
+
const ends = endsAt(s, index);
|
|
70
|
+
if (!ends)
|
|
71
|
+
return 0;
|
|
72
|
+
if (insideAt(s, index))
|
|
73
|
+
return (ends.base + ends.end) / 2;
|
|
74
|
+
// Text is centred on its `y`, so clear the column's end by the gap plus half a
|
|
75
|
+
// line — on whichever side of the baseline the column actually reached.
|
|
76
|
+
const direction = ends.end >= ends.base ? 1 : -1;
|
|
77
|
+
return ends.end + direction * (VALUE_GAP + valueSize(s) / 2);
|
|
78
|
+
}
|
|
17
79
|
/**
|
|
18
80
|
* One **series** of a {@link BarChart}, drawn as one command list: its column in
|
|
19
81
|
* every category, plus the figures printed on them.
|
|
@@ -30,207 +92,91 @@ const VALUE_SIZE = 24;
|
|
|
30
92
|
* and ride the node's own opacity — which is why hiding the series takes its
|
|
31
93
|
* figures with it for free.
|
|
32
94
|
*/
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
95
|
+
export const ChartBars = component2d({
|
|
96
|
+
key: "chartBars",
|
|
97
|
+
state: {
|
|
98
|
+
bars: fresh(() => []),
|
|
99
|
+
baseline: 0,
|
|
100
|
+
fill: Types.custom([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
101
|
+
barStroke: Types.custom(DEFAULT_BAR_CHART_THEME.bar.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),
|
|
102
|
+
barShadow: Types.custom(DEFAULT_BAR_CHART_THEME.bar.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),
|
|
103
|
+
cornerRadius: DEFAULT_BAR_CHART_THEME.bar.cornerRadius,
|
|
104
|
+
growth: 1,
|
|
105
|
+
dims: fresh(() => []),
|
|
106
|
+
showValues: false,
|
|
107
|
+
valueStyle: Types.custom(DEFAULT_BAR_CHART_THEME.valueStyle),
|
|
108
|
+
enabled: true,
|
|
109
|
+
},
|
|
110
|
+
// A plain node filled its parent; one made of its figures would hug them.
|
|
111
|
+
defaults: { width: "fill", height: "fill" },
|
|
112
|
+
// One caption per column, built once (the row count is fixed at construction,
|
|
113
|
+
// like an axis's marker count) and bound to the live geometry so it travels
|
|
114
|
+
// with its column as the chart grows, rescales or reflows.
|
|
115
|
+
children: (state) => {
|
|
116
|
+
if (!state.showValues)
|
|
117
|
+
return [];
|
|
118
|
+
return state.bars.map((_, i) => Text({
|
|
119
|
+
...textOps.styleProps(state.valueStyle),
|
|
120
|
+
text: () => captionAt(state, i),
|
|
121
|
+
x: () => state.bars[i]?.x ?? 0,
|
|
122
|
+
y: () => captionY(state, i),
|
|
123
|
+
opacity: () => dimAt(state, i),
|
|
124
|
+
}));
|
|
125
|
+
},
|
|
126
|
+
// The column stroke and shadow are named past the conventional slots, so they are named here.
|
|
127
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.fill], shadows: [state.barShadow], strokes: [state.barStroke] }),
|
|
128
|
+
commands: {
|
|
129
|
+
/**
|
|
130
|
+
* Raise every column from the baseline by tweening `growth` `0 → 1`.
|
|
131
|
+
*
|
|
132
|
+
* The mirror of `ChartLine.enter`: same contract that the resting state is
|
|
133
|
+
* the finished one, so the chart arms this back to 0 before animating rather
|
|
134
|
+
* than the node hiding itself at construction. No priming to 0 first: the
|
|
135
|
+
* value at `t` is a function of `t`, so a repeat call re-raises by definition.
|
|
136
|
+
*/
|
|
137
|
+
enter: (timeline) => {
|
|
138
|
+
const ease = frameEase(timeline);
|
|
139
|
+
return timeline.animate((t) => ({ growth: ease(t) }));
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
render: (ctx, state) => {
|
|
143
|
+
if (state.enabled) {
|
|
144
|
+
for (let i = 0; i < state.bars.length; i++) {
|
|
145
|
+
const bar = state.bars[i];
|
|
146
|
+
const ends = endsAt(state, i);
|
|
147
|
+
if (!ends || bar.width <= 0)
|
|
148
|
+
continue;
|
|
149
|
+
const height = Math.abs(ends.end - ends.base);
|
|
150
|
+
if (height <= 0)
|
|
151
|
+
continue;
|
|
152
|
+
const radius = bar.round ? Math.min(state.cornerRadius, height) : 0;
|
|
153
|
+
// A column per `Graphics2D` rather than one list for the series: the
|
|
154
|
+
// category spotlight dims individual columns, and opacity is a property
|
|
155
|
+
// of the command list rather than of a shape within it.
|
|
156
|
+
const g = new Graphics2D();
|
|
157
|
+
const rect = g.rect({
|
|
158
|
+
x: bar.x,
|
|
159
|
+
y: (ends.base + ends.end) / 2,
|
|
160
|
+
width: bar.width,
|
|
161
|
+
height,
|
|
162
|
+
// This node's space is y-up, but the renderer's corner-radius keys are
|
|
163
|
+
// labelled in canvas y-down space — so a column reaching *up* rounds
|
|
164
|
+
// through the `bottom*` keys, and one reaching down through the `top*`
|
|
165
|
+
// ones. Only the far end rounds, which is what makes a stack read as a
|
|
166
|
+
// single capped column.
|
|
167
|
+
cornerRadius: radius > 0
|
|
168
|
+
? ends.end >= ends.base
|
|
169
|
+
? { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
|
|
170
|
+
: { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }
|
|
171
|
+
: 0,
|
|
172
|
+
});
|
|
173
|
+
rect.shadow(state.barShadow);
|
|
174
|
+
rect.fill(state.fill);
|
|
175
|
+
rect.stroke(state.barStroke);
|
|
176
|
+
ctx.draw(g.opacity(dimAt(state, i)));
|
|
177
|
+
}
|
|
55
178
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
* series switch on top of it.
|
|
60
|
-
*
|
|
61
|
-
* The captions read this rather than being hidden by `renderSelf`'s guard:
|
|
62
|
-
* they are real child nodes, so a `return` in the draw pass would leave them
|
|
63
|
-
* on screen over a series that isn't there.
|
|
64
|
-
*/
|
|
65
|
-
dimAt(index) {
|
|
66
|
-
if (!this.enabled)
|
|
67
|
-
return 0;
|
|
68
|
-
return clampUnit(this.dims[index] ?? 1);
|
|
69
|
-
}
|
|
70
|
-
/** The two ends of column `i` at the current {@link growth}. */
|
|
71
|
-
endsAt(index) {
|
|
72
|
-
const bar = this.bars[index];
|
|
73
|
-
if (!bar)
|
|
74
|
-
return null;
|
|
75
|
-
const g = clampUnit(this.growth);
|
|
76
|
-
const base = this.baseline + (bar.from - this.baseline) * g;
|
|
77
|
-
const end = this.baseline + (bar.to - this.baseline) * g;
|
|
78
|
-
return { base, end };
|
|
79
|
-
}
|
|
80
|
-
/** The type size the figures are set at, guarding the autofit/unset cases. */
|
|
81
|
-
get valueSize() {
|
|
82
|
-
const size = this.valueStyle.fontSize;
|
|
83
|
-
return typeof size === "number" ? size : VALUE_SIZE;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* The figure printed on column `i`, or `""` where there is no room for it.
|
|
87
|
-
*
|
|
88
|
-
* Blanking the text rather than dropping the node keeps the caption list
|
|
89
|
-
* index-aligned with the columns, so a chart that rescales into more room
|
|
90
|
-
* simply starts printing again.
|
|
91
|
-
*/
|
|
92
|
-
captionAt(index) {
|
|
93
|
-
const bar = this.bars[index];
|
|
94
|
-
if (!bar || bar.value === 0)
|
|
95
|
-
return "";
|
|
96
|
-
// An interior segment prints inside itself, so it has to be tall enough to
|
|
97
|
-
// hold a line of type; a grouped column prints in the headroom above its
|
|
98
|
-
// end, where the only constraint is the plot's own top.
|
|
99
|
-
if (this.insideAt(index)) {
|
|
100
|
-
const ends = this.endsAt(index);
|
|
101
|
-
if (!ends)
|
|
102
|
-
return "";
|
|
103
|
-
if (Math.abs(ends.end - ends.base) < this.valueSize * VALUE_FIT)
|
|
104
|
-
return "";
|
|
105
|
-
}
|
|
106
|
-
return this.labelFor(bar.value);
|
|
107
|
-
}
|
|
108
|
-
/** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */
|
|
109
|
-
insideAt(index) {
|
|
110
|
-
const bar = this.bars[index];
|
|
111
|
-
return bar !== undefined && Math.abs(bar.from - this.baseline) > 0.5;
|
|
112
|
-
}
|
|
113
|
-
/** Where column `i`'s figure sits, in the node's centred y-up space. */
|
|
114
|
-
captionY(index) {
|
|
115
|
-
const ends = this.endsAt(index);
|
|
116
|
-
if (!ends)
|
|
117
|
-
return 0;
|
|
118
|
-
if (this.insideAt(index))
|
|
119
|
-
return (ends.base + ends.end) / 2;
|
|
120
|
-
// Text is centred on its `y`, so clear the column's end by the gap plus half
|
|
121
|
-
// a line — on whichever side of the baseline the column actually reached.
|
|
122
|
-
const direction = ends.end >= ends.base ? 1 : -1;
|
|
123
|
-
return ends.end + direction * (VALUE_GAP + this.valueSize / 2);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
|
|
127
|
-
* paint props it carries — see {@link declarePaints}. The column stroke and
|
|
128
|
-
* shadow are named past the conventional slots, so they are passed explicitly.
|
|
129
|
-
*/
|
|
130
|
-
declareAssets(assets) {
|
|
131
|
-
super.declareAssets(assets);
|
|
132
|
-
declarePaints(this, assets, {
|
|
133
|
-
fills: this.barShadow.map((shadow) => shadow.fill),
|
|
134
|
-
strokes: [this.barStroke],
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
renderSelf(ctx) {
|
|
138
|
-
if (!this.enabled)
|
|
139
|
-
return;
|
|
140
|
-
for (let i = 0; i < this.bars.length; i++) {
|
|
141
|
-
const bar = this.bars[i];
|
|
142
|
-
const ends = this.endsAt(i);
|
|
143
|
-
if (!ends || bar.width <= 0)
|
|
144
|
-
continue;
|
|
145
|
-
const height = Math.abs(ends.end - ends.base);
|
|
146
|
-
if (height <= 0)
|
|
147
|
-
continue;
|
|
148
|
-
const radius = bar.round ? Math.min(this.cornerRadius, height) : 0;
|
|
149
|
-
// A column per `Graphics2D` rather than one list for the series: the
|
|
150
|
-
// category spotlight dims individual columns, and opacity is a property
|
|
151
|
-
// of the command list rather than of a shape within it.
|
|
152
|
-
const g = new Graphics2D();
|
|
153
|
-
const rect = g.rect({
|
|
154
|
-
x: bar.x,
|
|
155
|
-
y: (ends.base + ends.end) / 2,
|
|
156
|
-
width: bar.width,
|
|
157
|
-
height,
|
|
158
|
-
// This node's space is y-up, but the renderer's corner-radius keys are
|
|
159
|
-
// labelled in canvas y-down space — so a column reaching *up* rounds
|
|
160
|
-
// through the `bottom*` keys, and one reaching down through the `top*`
|
|
161
|
-
// ones. Only the far end rounds, which is what makes a stack read as a
|
|
162
|
-
// single capped column.
|
|
163
|
-
cornerRadius: radius > 0
|
|
164
|
-
? ends.end >= ends.base
|
|
165
|
-
? { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
|
|
166
|
-
: { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }
|
|
167
|
-
: 0,
|
|
168
|
-
});
|
|
169
|
-
rect.shadow(this.barShadow);
|
|
170
|
-
rect.fill(this.fill);
|
|
171
|
-
rect.stroke(this.barStroke);
|
|
172
|
-
ctx.draw(g.opacity(this.dimAt(i)));
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Raise every column from the baseline by tweening {@link growth} `0 → 1`.
|
|
177
|
-
*
|
|
178
|
-
* The mirror of `ChartLine.enter`: same signature, same contract that the
|
|
179
|
-
* resting state is the finished one, so the chart arms this back to 0 before
|
|
180
|
-
* animating rather than the node hiding itself at construction.
|
|
181
|
-
*/
|
|
182
|
-
enter(args) {
|
|
183
|
-
// No priming to 0 first: the value at `t` is a function of `t`, so a repeat
|
|
184
|
-
// call re-raises by definition rather than by having reset something.
|
|
185
|
-
return this.command((t) => ({ growth: t }), args.duration, args.easing ?? easeOut());
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
__decorate([
|
|
189
|
-
property({ default: () => [] })
|
|
190
|
-
], ChartBars.prototype, "bars", void 0);
|
|
191
|
-
__decorate([
|
|
192
|
-
property({ default: 0 })
|
|
193
|
-
], ChartBars.prototype, "baseline", void 0);
|
|
194
|
-
__decorate([
|
|
195
|
-
property({
|
|
196
|
-
default: [],
|
|
197
|
-
mapper: fillOps.resolve,
|
|
198
|
-
tween: fillOps.lerp,
|
|
199
|
-
})
|
|
200
|
-
], ChartBars.prototype, "fill", void 0);
|
|
201
|
-
__decorate([
|
|
202
|
-
property({
|
|
203
|
-
default: DEFAULT_BAR_CHART_THEME.bar.stroke,
|
|
204
|
-
mapper: strokeOps.resolve,
|
|
205
|
-
tween: strokeOps.lerp,
|
|
206
|
-
})
|
|
207
|
-
], ChartBars.prototype, "barStroke", void 0);
|
|
208
|
-
__decorate([
|
|
209
|
-
property({
|
|
210
|
-
default: DEFAULT_BAR_CHART_THEME.bar.shadow,
|
|
211
|
-
mapper: shadowOps.resolve,
|
|
212
|
-
tween: shadowOps.lerp,
|
|
213
|
-
})
|
|
214
|
-
], ChartBars.prototype, "barShadow", void 0);
|
|
215
|
-
__decorate([
|
|
216
|
-
property({ default: DEFAULT_BAR_CHART_THEME.bar.cornerRadius })
|
|
217
|
-
], ChartBars.prototype, "cornerRadius", void 0);
|
|
218
|
-
__decorate([
|
|
219
|
-
property({ default: 1 })
|
|
220
|
-
], ChartBars.prototype, "growth", void 0);
|
|
221
|
-
__decorate([
|
|
222
|
-
property({ default: () => [] })
|
|
223
|
-
], ChartBars.prototype, "dims", void 0);
|
|
224
|
-
__decorate([
|
|
225
|
-
property({ default: false })
|
|
226
|
-
], ChartBars.prototype, "showValues", void 0);
|
|
227
|
-
__decorate([
|
|
228
|
-
property({ default: DEFAULT_BAR_CHART_THEME.valueStyle })
|
|
229
|
-
], ChartBars.prototype, "valueStyle", void 0);
|
|
230
|
-
__decorate([
|
|
231
|
-
property({ default: true })
|
|
232
|
-
], ChartBars.prototype, "enabled", void 0);
|
|
233
|
-
__decorate([
|
|
234
|
-
command()
|
|
235
|
-
], ChartBars.prototype, "enter", null);
|
|
179
|
+
ctx.children();
|
|
180
|
+
},
|
|
181
|
+
});
|
|
236
182
|
//# sourceMappingURL=chart-bars.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chart-bars.js","sourceRoot":"","sources":["../../src/bar-chart/chart-bars.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,OAAO,EAEP,UAAU,EACV,MAAM,EACN,IAAI,EACJ,OAAO,EACP,OAAO,EACP,SAAS,EACT,SAAS,EACT,QAAQ,EACR,OAAO,GAYR,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAEvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAyB3D,gFAAgF;AAChF,MAAM,SAAS,GAAG,EAAE,CAAA;AACpB,oFAAoF;AACpF,MAAM,SAAS,GAAG,GAAG,CAAA;AACrB,kFAAkF;AAClF,MAAM,UAAU,GAAG,EAAE,CAAA;AA4CrB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,SAAU,SAAQ,MAAsB;IA8BnD;;;;OAIG;IACH,QAAQ,GAAgB,YAAY,CAAA;IAEpC,YAAY,KAA6C;QACvD,KAAK,CAAC,KAAK,CAAC,CAAA;QAEZ,gEAAgE;QAChE,4EAA4E;QAC5E,yEAAyE;QACzE,IAAI,CAAC,IAAI,CAAC,UAAU;YAAE,OAAM;QAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC1C,IAAI,CAAC,GAAG,CACN,IAAI,IAAI,CAAC;gBACP,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC;gBACtC,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;gBAC7B,CAAC,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yB;AA2JrD;IADC,OAAO,EAAE;sCAKT","sourcesContent":["import {\n command,\n type AssetScope,\n Graphics2D,\n Node2D,\n Text,\n easeOut,\n fillOps,\n shadowOps,\n strokeOps,\n property,\n textOps,\n type Command,\n type CommandArgs,\n type Fill,\n type NodeConfig,\n type Node2DProps,\n type RenderContext2D,\n type Shadow,\n type ShadowResolved,\n type Stroke,\n type StrokeResolved,\n type TextStyle,\n} from \"@motionscript/sdk\"\n\nimport { clampUnit, formatMarker } from \"../kit/shared\"\nimport type { MarkerLabel } from \"../kit/vertical-axis\"\nimport { DEFAULT_BAR_CHART_THEME } from \"./shared\"\nimport { declarePaints } from \"@motionscript/sdk/component\"\n\n/**\n * One drawn column, already mapped into the plot's **centred pixel space**\n * (origin at the plot centre, y-up) by the parent {@link BarChart}.\n *\n * `from`/`to` are the two ends of the column rather than a base and a height,\n * for the same reason {@link BarSpan} is a span: it is the one shape that fits a\n * grouped bar, a stacked segment and a negative value without a special case.\n */\nexport interface BarGeometry {\n /** Centre x of the column. */\n x: number\n /** Column width in px. */\n width: number\n /** Pixel y of the column's base — the baseline when grouped, the running stack top when stacked. */\n from: number\n /** Pixel y of its far end, at full {@link ChartBars.growth}. */\n to: number\n /** Whether the far end takes the theme's corner radius (the stack's cap). */\n round: boolean\n /** The value this column plots, for the figure printed on it. */\n value: number\n}\n\n/** Gap between a grouped column's end and the figure printed over it, in px. */\nconst VALUE_GAP = 10\n/** A segment shorter than this many multiples of the type size prints no figure. */\nconst VALUE_FIT = 1.6\n/** Fallback type size when the theme's value style leaves it unset or autofit. */\nconst VALUE_SIZE = 24\n\nexport interface ChartBarsProps extends Node2DProps {\n /** This series' columns, one per row of the chart's data. */\n bars: BarGeometry[]\n /** Pixel y of value 0 — where every column grows *from*. */\n baseline: number\n /** Column paint — this series' own, or the theme palette's entry for its index. */\n fill: Fill\n /** Optional outline on every column. */\n barStroke: Stroke\n /** Optional drop shadow on every column. */\n barShadow: Shadow\n /** Radius of the far-end corners, in px. */\n cornerRadius: number\n /**\n * Rise of every column in `[0, 1]`, measured **about the baseline**: 0\n * collapses them all onto it, 1 draws them at full length.\n *\n * Scaling about the baseline rather than about each column's own base is what\n * keeps a stack flush mid-animation — every segment's base and end travel by\n * the same factor, so the segments stay stacked instead of the upper ones\n * floating above the still-growing lower ones. It also means a negative\n * column grows *down* out of the same signal.\n */\n growth: number\n /**\n * Per-column opacity multiplier, by row index — how the chart's category\n * spotlight reaches a single column without touching the series it belongs\n * to. Missing entries read as 1.\n */\n dims: number[]\n /** Whether the figure each column plots is printed on it. */\n showValues: boolean\n /** Type style for those figures. */\n valueStyle: TextStyle\n /**\n * Whether this series draws at all — the wholesale switch behind\n * `BarChart.showSeries`/`hideSeries`, alongside the slot weight that animates\n * the reflow. Off skips the columns *and* their figures.\n */\n enabled: boolean\n}\n\n/**\n * One **series** of a {@link BarChart}, drawn as one command list: its column in\n * every category, plus the figures printed on them.\n *\n * A node per series rather than a node per bar, deliberately — it is the same\n * unit `ChartLine` is, which is what lets `showSeries`/`hideSeries`/\n * `spotlightSeries` mean exactly the same thing on a bar chart as on a line\n * chart: enable the node, or tween its opacity. The columns' *positions* come\n * from the parent (it owns the slot arithmetic both modes share), so this part\n * only has to know how to draw a span and grow it.\n *\n * The figures are real {@link Text} children rather than glyphs in the command\n * list, so they take the theme's type style, are measured by the layout engine,\n * and ride the node's own opacity — which is why hiding the series takes its\n * figures with it for free.\n */\nexport class ChartBars extends Node2D<ChartBarsProps> {\n @property({ default: () => [] as BarGeometry[] }) declare bars: BarGeometry[]\n @property({ default: 0 }) declare baseline: number\n @property({\n default: [],\n mapper: fillOps.resolve,\n tween: fillOps.lerp,\n })\n declare fill: Fill\n @property({\n default: DEFAULT_BAR_CHART_THEME.bar.stroke,\n mapper: strokeOps.resolve,\n tween: strokeOps.lerp,\n })\n declare barStroke: Stroke\n @property({\n default: DEFAULT_BAR_CHART_THEME.bar.shadow,\n mapper: shadowOps.resolve,\n tween: shadowOps.lerp,\n })\n declare barShadow: Shadow\n @property({ default: DEFAULT_BAR_CHART_THEME.bar.cornerRadius })\n declare cornerRadius: number\n @property({ default: 1 }) declare growth: number\n @property({ default: () => [] as number[] }) declare dims: number[]\n @property({ default: false }) declare showValues: boolean\n @property({ default: DEFAULT_BAR_CHART_THEME.valueStyle })\n declare valueStyle: TextStyle\n @property({ default: true }) declare enabled: boolean\n\n /**\n * How a column's figure is written down — the same seam the two axes carry,\n * so a chart's value axis and the numbers printed on its bars can't end up\n * saying `$1,200` in one place and `1200` in the other.\n */\n labelFor: MarkerLabel = formatMarker\n\n constructor(props?: NodeConfig<ChartBars, ChartBarsProps>) {\n super(props)\n\n // One caption per column, built once (the row count is fixed at\n // construction, like an axis's marker count) and bound to the live geometry\n // so it travels with its column as the chart grows, rescales or reflows.\n if (!this.showValues) return\n for (let i = 0; i < this.bars.length; i++) {\n this.add(\n new Text({\n ...textOps.styleProps(this.valueStyle),\n text: () => this.captionAt(i),\n x: () => this.bars[i]?.x ?? 0,\n y: () => this.captionY(i),\n opacity: () => this.dimAt(i),\n })\n )\n }\n }\n\n /**\n * How far column `i` is dimmed — the chart's category spotlight, and the\n * series switch on top of it.\n *\n * The captions read this rather than being hidden by `renderSelf`'s guard:\n * they are real child nodes, so a `return` in the draw pass would leave them\n * on screen over a series that isn't there.\n */\n private dimAt(index: number): number {\n if (!this.enabled) return 0\n return clampUnit(this.dims[index] ?? 1)\n }\n\n /** The two ends of column `i` at the current {@link growth}. */\n private endsAt(index: number): { base: number; end: number } | null {\n const bar = this.bars[index]\n if (!bar) return null\n const g = clampUnit(this.growth)\n const base = this.baseline + (bar.from - this.baseline) * g\n const end = this.baseline + (bar.to - this.baseline) * g\n return { base, end }\n }\n\n /** The type size the figures are set at, guarding the autofit/unset cases. */\n private get valueSize(): number {\n const size = this.valueStyle.fontSize\n return typeof size === \"number\" ? size : VALUE_SIZE\n }\n\n /**\n * The figure printed on column `i`, or `\"\"` where there is no room for it.\n *\n * Blanking the text rather than dropping the node keeps the caption list\n * index-aligned with the columns, so a chart that rescales into more room\n * simply starts printing again.\n */\n private captionAt(index: number): string {\n const bar = this.bars[index]\n if (!bar || bar.value === 0) return \"\"\n // An interior segment prints inside itself, so it has to be tall enough to\n // hold a line of type; a grouped column prints in the headroom above its\n // end, where the only constraint is the plot's own top.\n if (this.insideAt(index)) {\n const ends = this.endsAt(index)\n if (!ends) return \"\"\n if (Math.abs(ends.end - ends.base) < this.valueSize * VALUE_FIT) return \"\"\n }\n return this.labelFor(bar.value)\n }\n\n /** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */\n private insideAt(index: number): boolean {\n const bar = this.bars[index]\n return bar !== undefined && Math.abs(bar.from - this.baseline) > 0.5\n }\n\n /** Where column `i`'s figure sits, in the node's centred y-up space. */\n private captionY(index: number): number {\n const ends = this.endsAt(index)\n if (!ends) return 0\n if (this.insideAt(index)) return (ends.base + ends.end) / 2\n // Text is centred on its `y`, so clear the column's end by the gap plus half\n // a line — on whichever side of the baseline the column actually reached.\n const direction = ends.end >= ends.base ? 1 : -1\n return ends.end + direction * (VALUE_GAP + this.valueSize / 2)\n }\n\n /**\n * This node extends `Node2D` rather than `ShapeNode`, so nothing declares the\n * paint props it carries — see {@link declarePaints}. The column stroke and\n * shadow are named past the conventional slots, so they are passed explicitly.\n */\n override declareAssets(assets: AssetScope): void {\n super.declareAssets(assets)\n declarePaints(this, assets, {\n fills: (this.barShadow as ShadowResolved[]).map((shadow) => shadow.fill),\n strokes: [this.barStroke as StrokeResolved[]],\n })\n }\n\n protected renderSelf(ctx: RenderContext2D): void {\n if (!this.enabled) return\n for (let i = 0; i < this.bars.length; i++) {\n const bar = this.bars[i]\n const ends = this.endsAt(i)\n if (!ends || bar.width <= 0) continue\n const height = Math.abs(ends.end - ends.base)\n if (height <= 0) continue\n\n const radius = bar.round ? Math.min(this.cornerRadius, height) : 0\n // A column per `Graphics2D` rather than one list for the series: the\n // category spotlight dims individual columns, and opacity is a property\n // of the command list rather than of a shape within it.\n const g = new Graphics2D()\n const rect = g.rect({\n x: bar.x,\n y: (ends.base + ends.end) / 2,\n width: bar.width,\n height,\n // This node's space is y-up, but the renderer's corner-radius keys are\n // labelled in canvas y-down space — so a column reaching *up* rounds\n // through the `bottom*` keys, and one reaching down through the `top*`\n // ones. Only the far end rounds, which is what makes a stack read as a\n // single capped column.\n cornerRadius:\n radius > 0\n ? ends.end >= ends.base\n ? { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }\n : { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }\n : 0,\n })\n rect.shadow(this.barShadow)\n rect.fill(this.fill)\n rect.stroke(this.barStroke)\n ctx.draw(g.opacity(this.dimAt(i)))\n }\n }\n\n /**\n * Raise every column from the baseline by tweening {@link growth} `0 → 1`.\n *\n * The mirror of `ChartLine.enter`: same signature, same contract that the\n * resting state is the finished one, so the chart arms this back to 0 before\n * animating rather than the node hiding itself at construction.\n */\n @command()\n enter(args: CommandArgs<Record<string, never>> & { duration: number }): Command<ChartBarsGrowth> {\n // No priming to 0 first: the value at `t` is a function of `t`, so a repeat\n // call re-raises by definition rather than by having reset something.\n return this.command<ChartBarsGrowth>((t) => ({ growth: t }), args.duration, args.easing ?? easeOut())\n }\n}\n\n/** The one signal {@link ChartBars.enter} moves. */\ntype ChartBarsGrowth = { growth: number }\n"]}
|
|
1
|
+
{"version":3,"file":"chart-bars.js","sourceRoot":"","sources":["../../src/bar-chart/chart-bars.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,IAAI,EACJ,KAAK,EACL,WAAW,EACX,OAAO,EACP,SAAS,EACT,SAAS,EACT,OAAO,GAYR,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,eA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EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE;4BACtE,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE;wBACxE,CAAC,CAAC,CAAC;iBACR,CAAC,CAAA;gBACF,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;gBAC5B,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBACrB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;gBAC5B,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;YACtC,CAAC;QACH,CAAC;QACD,GAAG,CAAC,QAAQ,EAAE,CAAA;IAChB,CAAC;CACF,CAAC,CAAA","sourcesContent":["import {\r\n Graphics2D,\r\n Text,\r\n Types,\r\n component2d,\r\n fillOps,\r\n shadowOps,\r\n strokeOps,\r\n textOps,\r\n type Fill,\r\n type FillResolved,\r\n type NodeOf,\r\n // Named so this component's emitted type reaches `Node2D` through the sdk, not core.\r\n type Node2D as _Node2D,\r\n type Node2DProps,\r\n type Shadow,\r\n type ShadowResolved,\r\n type Stroke,\r\n type StrokeResolved,\r\n type TextStyle,\r\n} from \"@motionscript/sdk\"\r\n\r\nimport { fresh } from \"../kit/entries\"\r\nimport { frameEase } from \"../kit/motion\"\r\nimport { loadPaints } from \"../kit/paints\"\r\nimport { clampUnit, formatMarker } from \"../kit/shared\"\r\nimport { DEFAULT_BAR_CHART_THEME } from \"./shared\"\r\n\r\n/**\r\n * One drawn column, already mapped into the plot's **centred pixel space**\r\n * (origin at the plot centre, y-up) by the parent {@link BarChart}.\r\n *\r\n * `from`/`to` are the two ends of the column rather than a base and a height,\r\n * for the same reason {@link BarSpan} is a span: it is the one shape that fits a\r\n * grouped bar, a stacked segment and a negative value without a special case.\r\n */\r\nexport interface BarGeometry {\r\n /** Centre x of the column. */\r\n x: number\r\n /** Column width in px. */\r\n width: number\r\n /** Pixel y of the column's base — the baseline when grouped, the running stack top when stacked. */\r\n from: number\r\n /** Pixel y of its far end, at full {@link ChartBarsProps.growth}. */\r\n to: number\r\n /** Whether the far end takes the theme's corner radius (the stack's cap). */\r\n round: boolean\r\n /** The value this column plots, for the figure printed on it. */\r\n value: number\r\n /**\r\n * The figure printed on it, written as the chart's value axis writes its\r\n * ticks, so the two cannot disagree. Absent, the bare marker reading of\r\n * {@link value}.\r\n */\r\n caption?: string\r\n}\r\n\r\n/** Gap between a grouped column's end and the figure printed over it, in px. */\r\nconst VALUE_GAP = 10\r\n/** A segment shorter than this many multiples of the type size prints no figure. */\r\nconst VALUE_FIT = 1.6\r\n/** Fallback type size when the theme's value style leaves it unset or autofit. */\r\nconst VALUE_SIZE = 24\r\n\r\nexport interface ChartBarsProps extends Node2DProps {\r\n /** This series' columns, one per row of the chart's data. */\r\n bars: BarGeometry[]\r\n /** Pixel y of value 0 — where every column grows *from*. */\r\n baseline: number\r\n /** Column paint — this series' own, or the theme palette's entry for its index. */\r\n fill: Fill\r\n /** Optional outline on every column. */\r\n barStroke: Stroke\r\n /** Optional drop shadow on every column. */\r\n barShadow: Shadow\r\n /** Radius of the far-end corners, in px. */\r\n cornerRadius: number\r\n /**\r\n * Rise of every column in `[0, 1]`, measured **about the baseline**: 0\r\n * collapses them all onto it, 1 draws them at full length.\r\n *\r\n * Scaling about the baseline rather than about each column's own base is what\r\n * keeps a stack flush mid-animation — every segment's base and end travel by\r\n * the same factor, so the segments stay stacked instead of the upper ones\r\n * floating above the still-growing lower ones. It also means a negative\r\n * column grows *down* out of the same signal.\r\n */\r\n growth: number\r\n /**\r\n * Per-column opacity multiplier, by row index — how the chart's category\r\n * spotlight reaches a single column without touching the series it belongs\r\n * to. Missing entries read as 1.\r\n */\r\n dims: number[]\r\n /** Whether the figure each column plots is printed on it. */\r\n showValues: boolean\r\n /** Type style for those figures. */\r\n valueStyle: TextStyle\r\n /**\r\n * Whether this series draws at all — the wholesale switch behind\r\n * `BarChart.showSeries`/`hideSeries`, alongside the slot weight that animates\r\n * the reflow. Off skips the columns *and* their figures.\r\n */\r\n enabled: boolean\r\n}\r\n\r\n/** What the column geometry reads. */\r\ninterface Columns {\r\n readonly bars: readonly BarGeometry[]\r\n readonly baseline: number\r\n readonly growth: number\r\n readonly dims: readonly number[]\r\n readonly enabled: boolean\r\n readonly valueStyle: TextStyle\r\n}\r\n\r\n/**\r\n * How far column `i` is dimmed — the chart's category spotlight, and the series\r\n * switch on top of it.\r\n *\r\n * The captions read this rather than being hidden by `render`'s guard: they are\r\n * real child nodes, so a `return` in the draw would leave them on screen over a\r\n * series that isn't there.\r\n */\r\nfunction dimAt(s: Columns, index: number): number {\r\n if (!s.enabled) return 0\r\n return clampUnit(s.dims[index] ?? 1)\r\n}\r\n\r\n/** The two ends of column `i` at the current growth. */\r\nfunction endsAt(s: Columns, index: number): { base: number; end: number } | null {\r\n const bar = s.bars[index]\r\n if (!bar) return null\r\n const g = clampUnit(s.growth)\r\n return { base: s.baseline + (bar.from - s.baseline) * g, end: s.baseline + (bar.to - s.baseline) * g }\r\n}\r\n\r\n/** The type size the figures are set at, guarding the autofit/unset cases. */\r\nfunction valueSize(s: Columns): number {\r\n const size = s.valueStyle.fontSize\r\n return typeof size === \"number\" ? size : VALUE_SIZE\r\n}\r\n\r\n/** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */\r\nfunction insideAt(s: Columns, index: number): boolean {\r\n const bar = s.bars[index]\r\n return bar !== undefined && Math.abs(bar.from - s.baseline) > 0.5\r\n}\r\n\r\n/**\r\n * The figure printed on column `i`, or `\"\"` where there is no room for it.\r\n *\r\n * Blanking the text rather than dropping the node keeps the caption list\r\n * index-aligned with the columns, so a chart that rescales into more room simply\r\n * starts printing again.\r\n */\r\nfunction captionAt(s: Columns, index: number): string {\r\n const bar = s.bars[index]\r\n if (!bar || bar.value === 0) return \"\"\r\n // An interior segment prints inside itself, so it has to be tall enough to\r\n // hold a line of type; a grouped column prints in the headroom above its end,\r\n // where the only constraint is the plot's own top.\r\n if (insideAt(s, index)) {\r\n const ends = endsAt(s, index)\r\n if (!ends) return \"\"\r\n if (Math.abs(ends.end - ends.base) < valueSize(s) * VALUE_FIT) return \"\"\r\n }\r\n return bar.caption ?? formatMarker(bar.value)\r\n}\r\n\r\n/** Where column `i`'s figure sits, in the node's centred y-up space. */\r\nfunction captionY(s: Columns, index: number): number {\r\n const ends = endsAt(s, index)\r\n if (!ends) return 0\r\n if (insideAt(s, index)) return (ends.base + ends.end) / 2\r\n // Text is centred on its `y`, so clear the column's end by the gap plus half a\r\n // line — on whichever side of the baseline the column actually reached.\r\n const direction = ends.end >= ends.base ? 1 : -1\r\n return ends.end + direction * (VALUE_GAP + valueSize(s) / 2)\r\n}\r\n\r\n/**\r\n * One **series** of a {@link BarChart}, drawn as one command list: its column in\r\n * every category, plus the figures printed on them.\r\n *\r\n * A node per series rather than a node per bar, deliberately — it is the same\r\n * unit `ChartLine` is, which is what lets `showSeries`/`hideSeries`/\r\n * `spotlightSeries` mean exactly the same thing on a bar chart as on a line\r\n * chart: enable the node, or tween its opacity. The columns' *positions* come\r\n * from the parent (it owns the slot arithmetic both modes share), so this part\r\n * only has to know how to draw a span and grow it.\r\n *\r\n * The figures are real {@link Text} children rather than glyphs in the command\r\n * list, so they take the theme's type style, are measured by the layout engine,\r\n * and ride the node's own opacity — which is why hiding the series takes its\r\n * figures with it for free.\r\n */\r\nexport const ChartBars = component2d({\r\n key: \"chartBars\",\r\n state: {\r\n bars: fresh<BarGeometry[]>(() => []),\r\n baseline: 0,\r\n fill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),\r\n barStroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_BAR_CHART_THEME.bar.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),\r\n barShadow: Types.custom<Shadow, ShadowResolved[]>(DEFAULT_BAR_CHART_THEME.bar.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),\r\n cornerRadius: DEFAULT_BAR_CHART_THEME.bar.cornerRadius,\r\n growth: 1,\r\n dims: fresh<number[]>(() => []),\r\n showValues: false,\r\n valueStyle: Types.custom<TextStyle>(DEFAULT_BAR_CHART_THEME.valueStyle),\r\n enabled: true,\r\n },\r\n // A plain node filled its parent; one made of its figures would hug them.\r\n defaults: { width: \"fill\", height: \"fill\" },\r\n // One caption per column, built once (the row count is fixed at construction,\r\n // like an axis's marker count) and bound to the live geometry so it travels\r\n // with its column as the chart grows, rescales or reflows.\r\n children: (state) => {\r\n if (!state.showValues) return []\r\n return state.bars.map((_, i) =>\r\n Text({\r\n ...textOps.styleProps(state.valueStyle),\r\n text: () => captionAt(state, i),\r\n x: () => state.bars[i]?.x ?? 0,\r\n y: () => captionY(state, i),\r\n opacity: () => dimAt(state, i),\r\n })\r\n )\r\n },\r\n // The column stroke and shadow are named past the conventional slots, so they are named here.\r\n load: (loader, state) =>\r\n loadPaints(loader, { fills: [state.fill], shadows: [state.barShadow], strokes: [state.barStroke] }),\r\n commands: {\r\n /**\r\n * Raise every column from the baseline by tweening `growth` `0 → 1`.\r\n *\r\n * The mirror of `ChartLine.enter`: same contract that the resting state is\r\n * the finished one, so the chart arms this back to 0 before animating rather\r\n * than the node hiding itself at construction. No priming to 0 first: the\r\n * value at `t` is a function of `t`, so a repeat call re-raises by definition.\r\n */\r\n enter: (timeline) => {\r\n const ease = frameEase(timeline)\r\n return timeline.animate((t) => ({ growth: ease(t) }))\r\n },\r\n },\r\n render: (ctx, state) => {\r\n if (state.enabled) {\r\n for (let i = 0; i < state.bars.length; i++) {\r\n const bar = state.bars[i]\r\n const ends = endsAt(state, i)\r\n if (!ends || bar.width <= 0) continue\r\n const height = Math.abs(ends.end - ends.base)\r\n if (height <= 0) continue\r\n\r\n const radius = bar.round ? Math.min(state.cornerRadius, height) : 0\r\n // A column per `Graphics2D` rather than one list for the series: the\r\n // category spotlight dims individual columns, and opacity is a property\r\n // of the command list rather than of a shape within it.\r\n const g = new Graphics2D()\r\n const rect = g.rect({\r\n x: bar.x,\r\n y: (ends.base + ends.end) / 2,\r\n width: bar.width,\r\n height,\r\n // This node's space is y-up, but the renderer's corner-radius keys are\r\n // labelled in canvas y-down space — so a column reaching *up* rounds\r\n // through the `bottom*` keys, and one reaching down through the `top*`\r\n // ones. Only the far end rounds, which is what makes a stack read as a\r\n // single capped column.\r\n cornerRadius:\r\n radius > 0\r\n ? ends.end >= ends.base\r\n ? { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }\r\n : { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }\r\n : 0,\r\n })\r\n rect.shadow(state.barShadow)\r\n rect.fill(state.fill)\r\n rect.stroke(state.barStroke)\r\n ctx.draw(g.opacity(dimAt(state, i)))\r\n }\r\n }\r\n ctx.children()\r\n },\r\n})\r\n\r\nexport type ChartBars = NodeOf<typeof ChartBars>\r\n"]}
|
|
@@ -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.js";
|
|
9
|
-
export type { BarChartProps } from "./bar-chart.js";
|
|
8
|
+
export { BarChart, barChartDomain } from "./bar-chart.js";
|
|
9
|
+
export type { BarChartDomain, BarChartProps, BarChartValues } from "./bar-chart.js";
|
|
10
10
|
export { ChartBars } from "./chart-bars.js";
|
|
11
11
|
export type { BarGeometry, ChartBarsProps } from "./chart-bars.js";
|
|
12
12
|
export { CategoryAxis } from "./category-axis.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bar-chart/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bar-chart/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AACtD,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAChF,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC/D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,EACL,uBAAuB,EACvB,aAAa,EACb,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,eAAe,EACf,aAAa,GACd,MAAM,UAAU,CAAA;AACjB,YAAY,EACV,cAAc,EACd,aAAa,EACb,SAAS,EACT,OAAO,EACP,cAAc,GACf,MAAM,UAAU,CAAA"}
|
package/dist/bar-chart/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
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.js";
|
|
8
|
+
export { BarChart, barChartDomain } from "./bar-chart.js";
|
|
9
9
|
export { ChartBars } from "./chart-bars.js";
|
|
10
10
|
export { CategoryAxis } from "./category-axis.js";
|
|
11
11
|
export { DEFAULT_BAR_CHART_THEME, barCategories, barSpans, barValueExtent, lerpBarChartTheme, resolveBarChartTheme, resolveBarFill, resolveBarScale, stackCapIndex, } from "./shared.js";
|