@motionscript/charts 0.0.0-stage → 0.1.0-alpha.0
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 +5 -0
- package/LICENSE +201 -0
- package/dist/bar-chart/bar-chart.d.ts +286 -0
- package/dist/bar-chart/bar-chart.d.ts.map +1 -0
- package/dist/bar-chart/bar-chart.js +610 -0
- package/dist/bar-chart/bar-chart.js.map +1 -0
- package/dist/bar-chart/category-axis.d.ts +77 -0
- package/dist/bar-chart/category-axis.d.ts.map +1 -0
- package/dist/bar-chart/category-axis.js +141 -0
- package/dist/bar-chart/category-axis.js.map +1 -0
- package/dist/bar-chart/chart-bars.d.ts +149 -0
- package/dist/bar-chart/chart-bars.d.ts.map +1 -0
- package/dist/bar-chart/chart-bars.js +236 -0
- package/dist/bar-chart/chart-bars.js.map +1 -0
- package/dist/bar-chart/index.d.ts +16 -0
- package/dist/bar-chart/index.d.ts.map +1 -0
- package/dist/bar-chart/index.js +12 -0
- package/dist/bar-chart/index.js.map +1 -0
- package/dist/bar-chart/shared.d.ts +146 -0
- package/dist/bar-chart/shared.d.ts.map +1 -0
- package/dist/bar-chart/shared.js +182 -0
- package/dist/bar-chart/shared.js.map +1 -0
- package/dist/browser/chunks/chunk-ZMBOBHYL.js +2 -0
- package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +12 -0
- package/dist/engine.d.ts +27 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +27 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/chart-border.d.ts +28 -0
- package/dist/kit/chart-border.d.ts.map +1 -0
- package/dist/kit/chart-border.js +45 -0
- package/dist/kit/chart-border.js.map +1 -0
- package/dist/kit/chart-canvas.d.ts +56 -0
- package/dist/kit/chart-canvas.d.ts.map +1 -0
- package/dist/kit/chart-canvas.js +125 -0
- package/dist/kit/chart-canvas.js.map +1 -0
- package/dist/kit/chart-legend.d.ts +126 -0
- package/dist/kit/chart-legend.d.ts.map +1 -0
- package/dist/kit/chart-legend.js +212 -0
- package/dist/kit/chart-legend.js.map +1 -0
- package/dist/kit/shared.d.ts +251 -0
- package/dist/kit/shared.d.ts.map +1 -0
- package/dist/kit/shared.js +401 -0
- package/dist/kit/shared.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +99 -0
- package/dist/kit/vertical-axis.d.ts.map +1 -0
- package/dist/kit/vertical-axis.js +176 -0
- package/dist/kit/vertical-axis.js.map +1 -0
- package/dist/line-chart/chart-line.d.ts +91 -0
- package/dist/line-chart/chart-line.d.ts.map +1 -0
- package/dist/line-chart/chart-line.js +156 -0
- package/dist/line-chart/chart-line.js.map +1 -0
- package/dist/line-chart/chart-region.d.ts +47 -0
- package/dist/line-chart/chart-region.d.ts.map +1 -0
- package/dist/line-chart/chart-region.js +101 -0
- package/dist/line-chart/chart-region.js.map +1 -0
- package/dist/line-chart/horizontal-axis.d.ts +85 -0
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -0
- package/dist/line-chart/horizontal-axis.js +158 -0
- package/dist/line-chart/horizontal-axis.js.map +1 -0
- package/dist/line-chart/index.d.ts +22 -0
- package/dist/line-chart/index.d.ts.map +1 -0
- package/dist/line-chart/index.js +19 -0
- package/dist/line-chart/index.js.map +1 -0
- package/dist/line-chart/line-chart.d.ts +347 -0
- package/dist/line-chart/line-chart.d.ts.map +1 -0
- package/dist/line-chart/line-chart.js +701 -0
- package/dist/line-chart/line-chart.js.map +1 -0
- package/dist/line-chart/shared.d.ts +171 -0
- package/dist/line-chart/shared.d.ts.map +1 -0
- package/dist/line-chart/shared.js +270 -0
- package/dist/line-chart/shared.js.map +1 -0
- package/dist/line-chart/x-scale.d.ts +84 -0
- package/dist/line-chart/x-scale.d.ts.map +1 -0
- package/dist/line-chart/x-scale.js +302 -0
- package/dist/line-chart/x-scale.js.map +1 -0
- package/dist/nodes.d.ts +20 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +20 -0
- package/dist/nodes.js.map +1 -0
- package/dist/pie-chart/index.d.ts +18 -0
- package/dist/pie-chart/index.d.ts.map +1 -0
- package/dist/pie-chart/index.js +14 -0
- package/dist/pie-chart/index.js.map +1 -0
- package/dist/pie-chart/pie-chart.d.ts +170 -0
- package/dist/pie-chart/pie-chart.d.ts.map +1 -0
- package/dist/pie-chart/pie-chart.js +291 -0
- package/dist/pie-chart/pie-chart.js.map +1 -0
- package/dist/pie-chart/pie-legend.d.ts +60 -0
- package/dist/pie-chart/pie-legend.d.ts.map +1 -0
- package/dist/pie-chart/pie-legend.js +131 -0
- package/dist/pie-chart/pie-legend.js.map +1 -0
- package/dist/pie-chart/pie-ring.d.ts +266 -0
- package/dist/pie-chart/pie-ring.d.ts.map +1 -0
- package/dist/pie-chart/pie-ring.js +708 -0
- package/dist/pie-chart/pie-ring.js.map +1 -0
- package/dist/pie-chart/shared.d.ts +210 -0
- package/dist/pie-chart/shared.d.ts.map +1 -0
- package/dist/pie-chart/shared.js +240 -0
- package/dist/pie-chart/shared.js.map +1 -0
- package/package.json +69 -3
- package/registry.json +23 -0
- package/src/bar-chart/bar-chart.ts +804 -0
- package/src/bar-chart/category-axis.ts +177 -0
- package/src/bar-chart/chart-bars.ts +310 -0
- package/src/bar-chart/index.ts +31 -0
- package/src/bar-chart/shared.ts +354 -0
- package/src/engine.ts +26 -0
- package/src/index.ts +5 -0
- package/src/kit/chart-border.ts +61 -0
- package/src/kit/chart-canvas.ts +157 -0
- package/src/kit/chart-legend.ts +250 -0
- package/src/kit/shared.ts +675 -0
- package/src/kit/vertical-axis.ts +224 -0
- package/src/line-chart/chart-line.ts +208 -0
- package/src/line-chart/chart-region.ts +135 -0
- package/src/line-chart/horizontal-axis.ts +202 -0
- package/src/line-chart/index.ts +39 -0
- package/src/line-chart/line-chart.ts +894 -0
- package/src/line-chart/shared.ts +459 -0
- package/src/line-chart/x-scale.ts +367 -0
- package/src/nodes.ts +20 -0
- package/src/pie-chart/index.ts +37 -0
- package/src/pie-chart/pie-chart.ts +374 -0
- package/src/pie-chart/pie-legend.ts +159 -0
- package/src/pie-chart/pie-ring.ts +903 -0
- package/src/pie-chart/shared.ts +490 -0
- package/README.md +0 -4
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
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/core";
|
|
8
|
+
import { clampUnit, formatMarker } from "../kit/shared.js";
|
|
9
|
+
import { DEFAULT_BAR_CHART_THEME } from "./shared.js";
|
|
10
|
+
import { declarePaints } from "@motionscript/core/component";
|
|
11
|
+
/** Gap between a grouped column's end and the figure printed over it, in px. */
|
|
12
|
+
const VALUE_GAP = 10;
|
|
13
|
+
/** A segment shorter than this many multiples of the type size prints no figure. */
|
|
14
|
+
const VALUE_FIT = 1.6;
|
|
15
|
+
/** Fallback type size when the theme's value style leaves it unset or autofit. */
|
|
16
|
+
const VALUE_SIZE = 24;
|
|
17
|
+
/**
|
|
18
|
+
* One **series** of a {@link BarChart}, drawn as one command list: its column in
|
|
19
|
+
* every category, plus the figures printed on them.
|
|
20
|
+
*
|
|
21
|
+
* A node per series rather than a node per bar, deliberately — it is the same
|
|
22
|
+
* unit `ChartLine` is, which is what lets `showSeries`/`hideSeries`/
|
|
23
|
+
* `spotlightSeries` mean exactly the same thing on a bar chart as on a line
|
|
24
|
+
* chart: enable the node, or tween its opacity. The columns' *positions* come
|
|
25
|
+
* from the parent (it owns the slot arithmetic both modes share), so this part
|
|
26
|
+
* only has to know how to draw a span and grow it.
|
|
27
|
+
*
|
|
28
|
+
* The figures are real {@link Text} children rather than glyphs in the command
|
|
29
|
+
* list, so they take the theme's type style, are measured by the layout engine,
|
|
30
|
+
* and ride the node's own opacity — which is why hiding the series takes its
|
|
31
|
+
* figures with it for free.
|
|
32
|
+
*/
|
|
33
|
+
export class ChartBars extends Node2D {
|
|
34
|
+
/**
|
|
35
|
+
* How a column's figure is written down — the same seam the two axes carry,
|
|
36
|
+
* so a chart's value axis and the numbers printed on its bars can't end up
|
|
37
|
+
* saying `$1,200` in one place and `1200` in the other.
|
|
38
|
+
*/
|
|
39
|
+
labelFor = formatMarker;
|
|
40
|
+
constructor(props) {
|
|
41
|
+
super(props);
|
|
42
|
+
// One caption per column, built once (the row count is fixed at
|
|
43
|
+
// construction, like an axis's marker count) and bound to the live geometry
|
|
44
|
+
// so it travels with its column as the chart grows, rescales or reflows.
|
|
45
|
+
if (!this.showValues)
|
|
46
|
+
return;
|
|
47
|
+
for (let i = 0; i < this.bars.length; i++) {
|
|
48
|
+
this.add(new Text({
|
|
49
|
+
...textOps.styleProps(this.valueStyle),
|
|
50
|
+
text: () => this.captionAt(i),
|
|
51
|
+
x: () => this.bars[i]?.x ?? 0,
|
|
52
|
+
y: () => this.captionY(i),
|
|
53
|
+
opacity: () => this.dimAt(i),
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* How far column `i` is dimmed — the chart's category spotlight, and the
|
|
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);
|
|
236
|
+
//# sourceMappingURL=chart-bars.js.map
|
|
@@ -0,0 +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,oBAAoB,CAAA;AAE3B,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAEvD,OAAO,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAA;AAyB5D,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,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CAAC;6CAC7B;AACQ;IAApC,QAAQ,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;0CAAyB;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/core\"\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/core/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"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bar chart: grouped or stacked columns off a category axis.
|
|
3
|
+
*
|
|
4
|
+
* Shares the plot panel, the value axis and the legend with the line chart —
|
|
5
|
+
* which is what lets a scene swap one for the other without re-theming — and
|
|
6
|
+
* brings its own category axis, since a band scale is not a continuous one.
|
|
7
|
+
*/
|
|
8
|
+
export { BarChart } from "./bar-chart.js";
|
|
9
|
+
export type { BarChartProps } from "./bar-chart.js";
|
|
10
|
+
export { ChartBars } from "./chart-bars.js";
|
|
11
|
+
export type { BarGeometry, ChartBarsProps } from "./chart-bars.js";
|
|
12
|
+
export { CategoryAxis } from "./category-axis.js";
|
|
13
|
+
export type { CategoryAxisProps } from "./category-axis.js";
|
|
14
|
+
export { DEFAULT_BAR_CHART_THEME, barCategories, barSpans, barValueExtent, lerpBarChartTheme, resolveBarChartTheme, resolveBarFill, resolveBarScale, stackCapIndex, } from "./shared.js";
|
|
15
|
+
export type { BarChartConfig, BarChartTheme, BarSeries, BarSpan, BarStyleConfig, } from "./shared.js";
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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;AACtC,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,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"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bar chart: grouped or stacked columns off a category axis.
|
|
3
|
+
*
|
|
4
|
+
* Shares the plot panel, the value axis and the legend with the line chart —
|
|
5
|
+
* which is what lets a scene swap one for the other without re-theming — and
|
|
6
|
+
* brings its own category axis, since a band scale is not a continuous one.
|
|
7
|
+
*/
|
|
8
|
+
export { BarChart } from "./bar-chart.js";
|
|
9
|
+
export { ChartBars } from "./chart-bars.js";
|
|
10
|
+
export { CategoryAxis } from "./category-axis.js";
|
|
11
|
+
export { DEFAULT_BAR_CHART_THEME, barCategories, barSpans, barValueExtent, lerpBarChartTheme, resolveBarChartTheme, resolveBarFill, resolveBarScale, stackCapIndex, } from "./shared.js";
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bar-chart/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEtC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,EACL,uBAAuB,EACvB,aAAa,EACb,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,eAAe,EACf,aAAa,GACd,MAAM,UAAU,CAAA","sourcesContent":["/**\n * The bar chart: grouped or stacked columns off a category axis.\n *\n * Shares the plot panel, the value axis and the legend with the line chart —\n * which is what lets a scene swap one for the other without re-theming — and\n * brings its own category axis, since a band scale is not a continuous one.\n */\nexport { BarChart } from \"./bar-chart\"\nexport type { BarChartProps } from \"./bar-chart\"\nexport { ChartBars } from \"./chart-bars\"\nexport type { BarGeometry, ChartBarsProps } from \"./chart-bars\"\nexport { CategoryAxis } from \"./category-axis\"\nexport type { CategoryAxisProps } from \"./category-axis\"\nexport {\n DEFAULT_BAR_CHART_THEME,\n barCategories,\n barSpans,\n barValueExtent,\n lerpBarChartTheme,\n resolveBarChartTheme,\n resolveBarFill,\n resolveBarScale,\n stackCapIndex,\n} from \"./shared\"\nexport type {\n BarChartConfig,\n BarChartTheme,\n BarSeries,\n BarSpan,\n BarStyleConfig,\n} from \"./shared\"\n"]}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types and pure helpers specific to the {@link BarChart} family: how a row's
|
|
3
|
+
* values become the spans its columns cover, how the value axis is scaled around
|
|
4
|
+
* them, and the theme the bars add to the shared chart vocabulary.
|
|
5
|
+
*
|
|
6
|
+
* Everything a line or a pie chart would want too — what a series is, the axis
|
|
7
|
+
* domain model, the theme leaf resolvers, the legend and plot-panel branches —
|
|
8
|
+
* lives in `nodes/chart-kit/shared.ts` and is imported here.
|
|
9
|
+
*
|
|
10
|
+
* Kept renderer-agnostic (no `Node` imports) so the studio's inspector mapping
|
|
11
|
+
* (`bar-chart-props.ts`) can build a partial {@link BarChartTheme} without
|
|
12
|
+
* pulling the node classes in.
|
|
13
|
+
*/
|
|
14
|
+
import { type DataRecord, type Fill, type Shadow, type Stroke, type TextStyle } from "@motionscript/core";
|
|
15
|
+
import { type AxisConfig, type ChartSeries, type LegendConfig, type LegendTheme, type PlotAreaConfig, type Range } from "../kit/shared.js";
|
|
16
|
+
/**
|
|
17
|
+
* One series in a {@link BarChart}: a column of the table drawn as one bar per
|
|
18
|
+
* row — side by side with its siblings within each category's slot, or stacked
|
|
19
|
+
* on top of them.
|
|
20
|
+
*
|
|
21
|
+
* The family-wide {@link ChartSeries} under a name that says what it draws here.
|
|
22
|
+
*/
|
|
23
|
+
export type BarSeries = ChartSeries;
|
|
24
|
+
/**
|
|
25
|
+
* One drawn bar's span in **data units**: the value its base sits at and the
|
|
26
|
+
* value its far end reaches.
|
|
27
|
+
*
|
|
28
|
+
* A span rather than a height because that is the one shape both modes fit. A
|
|
29
|
+
* grouped bar runs from zero to its value; a stacked segment runs from the
|
|
30
|
+
* running total below it to that total plus its own value. Negative values fall
|
|
31
|
+
* out of the same arithmetic — the span simply runs the other way — so a chart
|
|
32
|
+
* with a loss in it needs no special case, only a baseline that isn't the floor.
|
|
33
|
+
*/
|
|
34
|
+
export interface BarSpan {
|
|
35
|
+
/** Value the bar's base sits at — 0 when grouped, the running total when stacked. */
|
|
36
|
+
from: number;
|
|
37
|
+
/** Value its far end reaches. */
|
|
38
|
+
to: number;
|
|
39
|
+
/** The value this bar plots — `to - from`, kept for the figure printed on it. */
|
|
40
|
+
value: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The spans one row's bars cover, one per series, in series order.
|
|
44
|
+
*
|
|
45
|
+
* Stacking is a running sum in the order the series are listed, which is also
|
|
46
|
+
* the order they're drawn: series 0 sits on the baseline and each later one
|
|
47
|
+
* rides on what's below it. A zero-valued series still yields a (degenerate)
|
|
48
|
+
* span so the returned list stays index-aligned with the series list.
|
|
49
|
+
*/
|
|
50
|
+
export declare function barSpans(row: DataRecord, series: BarSeries[], stacked: boolean): BarSpan[];
|
|
51
|
+
/**
|
|
52
|
+
* The index of the series whose segment ends a row's stack — the one that gets
|
|
53
|
+
* the rounded cap, so a stack reads as one capped column rather than a pile of
|
|
54
|
+
* separate boxes. `-1` when every value in the row is zero.
|
|
55
|
+
*
|
|
56
|
+
* Read off the *last non-zero* series rather than the last one listed, so a
|
|
57
|
+
* series that happens to be 0 in this row doesn't take the cap and leave the
|
|
58
|
+
* segment below it square.
|
|
59
|
+
*/
|
|
60
|
+
export declare function stackCapIndex(row: DataRecord, series: BarSeries[]): number;
|
|
61
|
+
/** The captions beneath the baseline: one per row, read off the category column. */
|
|
62
|
+
export declare function barCategories(data: DataRecord[], field: string): string[];
|
|
63
|
+
/**
|
|
64
|
+
* The `[min, max]` of everything the chart draws, **zero always included**.
|
|
65
|
+
*
|
|
66
|
+
* Zero is in the range whatever the data says, because a bar chart's baseline is
|
|
67
|
+
* zero: a scale starting at 40 draws a column three times its neighbour for a
|
|
68
|
+
* 10% difference, which is the classic way to lie with one. A chart that really
|
|
69
|
+
* does want a cropped axis says so with an explicit range.
|
|
70
|
+
*/
|
|
71
|
+
export declare function barValueExtent(data: DataRecord[], series: BarSeries[], stacked: boolean): Range;
|
|
72
|
+
/**
|
|
73
|
+
* Effective value scale: the explicit `range`, else the data's extent nudged out
|
|
74
|
+
* to tidy step bounds so the markers read as round numbers.
|
|
75
|
+
*
|
|
76
|
+
* `divisions` is how many segments the auto-range is stepped into (one fewer
|
|
77
|
+
* than the marker count), so the ticks the axis draws land on the bounds this
|
|
78
|
+
* picks.
|
|
79
|
+
*/
|
|
80
|
+
export declare function resolveBarScale(data: DataRecord[], series: BarSeries[], stacked: boolean, range: Range | undefined, divisions: number): Range;
|
|
81
|
+
/** How the columns themselves are drawn and how the slot is divided up. */
|
|
82
|
+
export interface BarStyleConfig {
|
|
83
|
+
/**
|
|
84
|
+
* Radius of a column's far-end corners, in px. Only the outer end rounds — an
|
|
85
|
+
* interior stacked segment stays square, so the stack reads as one column.
|
|
86
|
+
*/
|
|
87
|
+
cornerRadius: number;
|
|
88
|
+
/**
|
|
89
|
+
* Fraction of its category's slot the bars fill, `[0, 1]`. The remainder is
|
|
90
|
+
* the channel between one category and the next, which is what makes a bar
|
|
91
|
+
* chart read as groups rather than as a solid block.
|
|
92
|
+
*/
|
|
93
|
+
slotFill: number;
|
|
94
|
+
/** Gap between the grouped bars *within* one slot, in px. Ignored when stacked. */
|
|
95
|
+
groupGap: number;
|
|
96
|
+
/** Optional outline on every column. */
|
|
97
|
+
stroke: Stroke;
|
|
98
|
+
/** Optional drop shadow on every column. */
|
|
99
|
+
shadow: Shadow;
|
|
100
|
+
}
|
|
101
|
+
/** A {@link BarChart}'s full, resolved visual theme. */
|
|
102
|
+
export interface BarChartConfig {
|
|
103
|
+
/**
|
|
104
|
+
* Default per-series bar paint, cycled by series index (`i % length`). A
|
|
105
|
+
* series' own `fill`, when set, replaces its entry outright.
|
|
106
|
+
*/
|
|
107
|
+
seriesFills: Fill[];
|
|
108
|
+
bar: BarStyleConfig;
|
|
109
|
+
/** The category axis beneath the baseline. */
|
|
110
|
+
xAxis: AxisConfig;
|
|
111
|
+
/** The numeric value axis up the left. */
|
|
112
|
+
yAxis: AxisConfig;
|
|
113
|
+
plotArea: PlotAreaConfig;
|
|
114
|
+
legend: LegendConfig;
|
|
115
|
+
/** The figure printed on each column, when the chart prints them. */
|
|
116
|
+
valueStyle: TextStyle;
|
|
117
|
+
/** How far everything *outside* a spotlit series or category fades. */
|
|
118
|
+
spotlight: {
|
|
119
|
+
dimOpacity: number;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** Author-facing partial theme — every branch optional and deep-merged. */
|
|
123
|
+
export type BarChartTheme = {
|
|
124
|
+
seriesFills?: Fill[];
|
|
125
|
+
bar?: Partial<BarStyleConfig>;
|
|
126
|
+
xAxis?: Partial<AxisConfig>;
|
|
127
|
+
yAxis?: Partial<AxisConfig>;
|
|
128
|
+
plotArea?: Partial<PlotAreaConfig>;
|
|
129
|
+
legend?: LegendTheme;
|
|
130
|
+
valueStyle?: TextStyle;
|
|
131
|
+
spotlight?: Partial<BarChartConfig["spotlight"]>;
|
|
132
|
+
};
|
|
133
|
+
/** The bar chart's out-of-the-box look. */
|
|
134
|
+
export declare const DEFAULT_BAR_CHART_THEME: BarChartConfig;
|
|
135
|
+
/**
|
|
136
|
+
* {@link BarChart}'s `theme` @property mapper: merges the author's partial
|
|
137
|
+
* {@link BarChartTheme} onto `previous` (falling back to
|
|
138
|
+
* {@link DEFAULT_BAR_CHART_THEME} on first resolution), fully resolving every
|
|
139
|
+
* leaf so the tween below always has resolved data to interpolate.
|
|
140
|
+
*/
|
|
141
|
+
export declare function resolveBarChartTheme(theme: BarChartTheme | undefined, previous?: BarChartConfig): BarChartConfig;
|
|
142
|
+
/** {@link BarChart}'s `theme` @property tween — leaf by leaf. */
|
|
143
|
+
export declare function lerpBarChartTheme(from: BarChartConfig, to: BarChartConfig, t: number): BarChartConfig;
|
|
144
|
+
/** The paint series `index` is drawn in: its own override, else the palette's entry. */
|
|
145
|
+
export declare function resolveBarFill(palette: Fill[], series: BarSeries, index: number): Fill;
|
|
146
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@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A;AAED,2CAA2C;AAC3C,eAAO,MAAM,uBAAuB,EAAE,cA2BrC,CAAA;AAqCD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,aAAa,GAAG,SAAS,EAChC,QAAQ,CAAC,EAAE,cAAc,GACxB,cAAc,CAchB;AAED,iEAAiE;AACjE,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,cAAc,EACpB,EAAE,EAAE,cAAc,EAClB,CAAC,EAAE,MAAM,GACR,cAAc,CAiBhB;AAED,wFAAwF;AACxF,wBAAgB,cAAc,CAC5B,OAAO,EAAE,IAAI,EAAE,EACf,MAAM,EAAE,SAAS,EACjB,KAAK,EAAE,MAAM,GACZ,IAAI,CAIN"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types and pure helpers specific to the {@link BarChart} family: how a row's
|
|
3
|
+
* values become the spans its columns cover, how the value axis is scaled around
|
|
4
|
+
* them, and the theme the bars add to the shared chart vocabulary.
|
|
5
|
+
*
|
|
6
|
+
* Everything a line or a pie chart would want too — what a series is, the axis
|
|
7
|
+
* domain model, the theme leaf resolvers, the legend and plot-panel branches —
|
|
8
|
+
* lives in `nodes/chart-kit/shared.ts` and is imported here.
|
|
9
|
+
*
|
|
10
|
+
* Kept renderer-agnostic (no `Node` imports) so the studio's inspector mapping
|
|
11
|
+
* (`bar-chart-props.ts`) can build a partial {@link BarChartTheme} without
|
|
12
|
+
* pulling the node classes in.
|
|
13
|
+
*/
|
|
14
|
+
import { fillOps, lerpNumber, shadowOps, strokeOps, textOps, } from "@motionscript/core";
|
|
15
|
+
import { CHART_PALETTE, DEFAULT_LEGEND, DEFAULT_PLOT_AREA, DEFAULT_SERIES_FILLS, DEFAULT_X_AXIS, DEFAULT_Y_AXIS, SPOTLIGHT_DIM, lerpAxisConfig, lerpLegendConfig, lerpPlotArea, lerpSeriesFills, niceStep, num, resolveAxisConfig, resolveLegendConfig, resolvePlotArea, resolveSeriesFills, text, } from "../kit/shared.js";
|
|
16
|
+
/**
|
|
17
|
+
* The spans one row's bars cover, one per series, in series order.
|
|
18
|
+
*
|
|
19
|
+
* Stacking is a running sum in the order the series are listed, which is also
|
|
20
|
+
* the order they're drawn: series 0 sits on the baseline and each later one
|
|
21
|
+
* rides on what's below it. A zero-valued series still yields a (degenerate)
|
|
22
|
+
* span so the returned list stays index-aligned with the series list.
|
|
23
|
+
*/
|
|
24
|
+
export function barSpans(row, series, stacked) {
|
|
25
|
+
let running = 0;
|
|
26
|
+
return series.map((s) => {
|
|
27
|
+
const value = num(row, s.field);
|
|
28
|
+
if (!stacked)
|
|
29
|
+
return { from: 0, to: value, value };
|
|
30
|
+
const from = running;
|
|
31
|
+
running += value;
|
|
32
|
+
return { from, to: running, value };
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The index of the series whose segment ends a row's stack — the one that gets
|
|
37
|
+
* the rounded cap, so a stack reads as one capped column rather than a pile of
|
|
38
|
+
* separate boxes. `-1` when every value in the row is zero.
|
|
39
|
+
*
|
|
40
|
+
* Read off the *last non-zero* series rather than the last one listed, so a
|
|
41
|
+
* series that happens to be 0 in this row doesn't take the cap and leave the
|
|
42
|
+
* segment below it square.
|
|
43
|
+
*/
|
|
44
|
+
export function stackCapIndex(row, series) {
|
|
45
|
+
let last = -1;
|
|
46
|
+
for (let i = 0; i < series.length; i++) {
|
|
47
|
+
if (num(row, series[i].field) !== 0)
|
|
48
|
+
last = i;
|
|
49
|
+
}
|
|
50
|
+
return last;
|
|
51
|
+
}
|
|
52
|
+
/** The captions beneath the baseline: one per row, read off the category column. */
|
|
53
|
+
export function barCategories(data, field) {
|
|
54
|
+
return data.map((row) => text(row, field));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The `[min, max]` of everything the chart draws, **zero always included**.
|
|
58
|
+
*
|
|
59
|
+
* Zero is in the range whatever the data says, because a bar chart's baseline is
|
|
60
|
+
* zero: a scale starting at 40 draws a column three times its neighbour for a
|
|
61
|
+
* 10% difference, which is the classic way to lie with one. A chart that really
|
|
62
|
+
* does want a cropped axis says so with an explicit range.
|
|
63
|
+
*/
|
|
64
|
+
export function barValueExtent(data, series, stacked) {
|
|
65
|
+
let lo = 0;
|
|
66
|
+
let hi = 0;
|
|
67
|
+
for (const row of data) {
|
|
68
|
+
for (const span of barSpans(row, series, stacked)) {
|
|
69
|
+
lo = Math.min(lo, span.from, span.to);
|
|
70
|
+
hi = Math.max(hi, span.from, span.to);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return lo < hi ? [lo, hi] : [0, 1];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Effective value scale: the explicit `range`, else the data's extent nudged out
|
|
77
|
+
* to tidy step bounds so the markers read as round numbers.
|
|
78
|
+
*
|
|
79
|
+
* `divisions` is how many segments the auto-range is stepped into (one fewer
|
|
80
|
+
* than the marker count), so the ticks the axis draws land on the bounds this
|
|
81
|
+
* picks.
|
|
82
|
+
*/
|
|
83
|
+
export function resolveBarScale(data, series, stacked, range, divisions) {
|
|
84
|
+
if (range)
|
|
85
|
+
return range;
|
|
86
|
+
const [lo, hi] = barValueExtent(data, series, stacked);
|
|
87
|
+
const segments = Math.max(1, Math.round(divisions));
|
|
88
|
+
const step = niceStep((hi - lo) / segments || 1);
|
|
89
|
+
return [Math.floor(lo / step) * step, Math.ceil(hi / step) * step];
|
|
90
|
+
}
|
|
91
|
+
/** The bar chart's out-of-the-box look. */
|
|
92
|
+
export const DEFAULT_BAR_CHART_THEME = {
|
|
93
|
+
seriesFills: DEFAULT_SERIES_FILLS,
|
|
94
|
+
bar: {
|
|
95
|
+
cornerRadius: 6,
|
|
96
|
+
slotFill: 0.72,
|
|
97
|
+
groupGap: 6,
|
|
98
|
+
stroke: [],
|
|
99
|
+
shadow: [],
|
|
100
|
+
},
|
|
101
|
+
// A category axis names its slots, so its captions are set like a caption
|
|
102
|
+
// rather than like a numeric marker: same size, less weight than the line
|
|
103
|
+
// chart's ticks, and centred on the slot.
|
|
104
|
+
xAxis: {
|
|
105
|
+
...DEFAULT_X_AXIS,
|
|
106
|
+
labelStyle: { ...DEFAULT_X_AXIS.labelStyle, fontWeight: 600 },
|
|
107
|
+
tickPadding: 16,
|
|
108
|
+
},
|
|
109
|
+
yAxis: DEFAULT_Y_AXIS,
|
|
110
|
+
plotArea: DEFAULT_PLOT_AREA,
|
|
111
|
+
legend: DEFAULT_LEGEND,
|
|
112
|
+
valueStyle: {
|
|
113
|
+
fontSize: 24,
|
|
114
|
+
fontWeight: 700,
|
|
115
|
+
textAlign: "center",
|
|
116
|
+
fill: CHART_PALETTE.markerColor,
|
|
117
|
+
},
|
|
118
|
+
spotlight: { dimOpacity: SPOTLIGHT_DIM },
|
|
119
|
+
};
|
|
120
|
+
function resolveBarStyle(bar, previous) {
|
|
121
|
+
return {
|
|
122
|
+
cornerRadius: bar?.cornerRadius ?? previous.cornerRadius,
|
|
123
|
+
slotFill: bar?.slotFill ?? previous.slotFill,
|
|
124
|
+
groupGap: bar?.groupGap ?? previous.groupGap,
|
|
125
|
+
stroke: strokeOps.resolve(bar?.stroke ?? previous.stroke),
|
|
126
|
+
shadow: shadowOps.resolve(bar?.shadow ?? previous.shadow),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function lerpBarStyle(from, to, t) {
|
|
130
|
+
return {
|
|
131
|
+
cornerRadius: lerpNumber(from.cornerRadius, to.cornerRadius, t),
|
|
132
|
+
slotFill: lerpNumber(from.slotFill, to.slotFill, t),
|
|
133
|
+
groupGap: lerpNumber(from.groupGap, to.groupGap, t),
|
|
134
|
+
stroke: strokeOps.lerp(from.stroke, to.stroke, t),
|
|
135
|
+
shadow: shadowOps.lerp(from.shadow, to.shadow, t),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* {@link BarChart}'s `theme` @property mapper: merges the author's partial
|
|
140
|
+
* {@link BarChartTheme} onto `previous` (falling back to
|
|
141
|
+
* {@link DEFAULT_BAR_CHART_THEME} on first resolution), fully resolving every
|
|
142
|
+
* leaf so the tween below always has resolved data to interpolate.
|
|
143
|
+
*/
|
|
144
|
+
export function resolveBarChartTheme(theme, previous) {
|
|
145
|
+
const base = previous ?? DEFAULT_BAR_CHART_THEME;
|
|
146
|
+
return {
|
|
147
|
+
seriesFills: resolveSeriesFills(theme?.seriesFills, base.seriesFills),
|
|
148
|
+
bar: resolveBarStyle(theme?.bar, base.bar),
|
|
149
|
+
xAxis: resolveAxisConfig(theme?.xAxis, base.xAxis),
|
|
150
|
+
yAxis: resolveAxisConfig(theme?.yAxis, base.yAxis),
|
|
151
|
+
plotArea: resolvePlotArea(theme?.plotArea, base.plotArea),
|
|
152
|
+
legend: resolveLegendConfig(theme?.legend, base.legend),
|
|
153
|
+
valueStyle: textOps.resolveStyle(theme?.valueStyle, base.valueStyle),
|
|
154
|
+
spotlight: {
|
|
155
|
+
dimOpacity: theme?.spotlight?.dimOpacity ?? base.spotlight.dimOpacity,
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/** {@link BarChart}'s `theme` @property tween — leaf by leaf. */
|
|
160
|
+
export function lerpBarChartTheme(from, to, t) {
|
|
161
|
+
return {
|
|
162
|
+
seriesFills: lerpSeriesFills(from.seriesFills, to.seriesFills, t),
|
|
163
|
+
bar: lerpBarStyle(from.bar, to.bar, t),
|
|
164
|
+
xAxis: lerpAxisConfig(from.xAxis, to.xAxis, t),
|
|
165
|
+
yAxis: lerpAxisConfig(from.yAxis, to.yAxis, t),
|
|
166
|
+
plotArea: lerpPlotArea(from.plotArea, to.plotArea, t),
|
|
167
|
+
legend: lerpLegendConfig(from.legend, to.legend, t),
|
|
168
|
+
valueStyle: textOps.lerpStyle(from.valueStyle, to.valueStyle, t),
|
|
169
|
+
spotlight: {
|
|
170
|
+
dimOpacity: lerpNumber(from.spotlight.dimOpacity, to.spotlight.dimOpacity, t),
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/** The paint series `index` is drawn in: its own override, else the palette's entry. */
|
|
175
|
+
export function resolveBarFill(palette, series, index) {
|
|
176
|
+
if (series.fill != null)
|
|
177
|
+
return fillOps.resolve(series.fill);
|
|
178
|
+
if (palette.length === 0)
|
|
179
|
+
return "white";
|
|
180
|
+
return palette[index % palette.length];
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=shared.js.map
|