@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,610 +1,423 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
var BarChart_1;
|
|
8
|
-
import { command, node, Node2D, Rect, easeOut, property, } from "@motionscript/sdk";
|
|
1
|
+
import { Rect, Types, component2d, easeOut, } from "@motionscript/sdk";
|
|
2
|
+
import { formatNumber } from "@motionscript/sdk/component";
|
|
3
|
+
import { cartesianFrame, cartesianViews, laidOutSize } from "../kit/cartesian.js";
|
|
9
4
|
import { ChartBorder } from "../kit/chart-border.js";
|
|
10
5
|
import { ChartCanvas } from "../kit/chart-canvas.js";
|
|
11
6
|
import { ChartLegend } from "../kit/chart-legend.js";
|
|
7
|
+
import { asParts, fresh } from "../kit/entries.js";
|
|
8
|
+
import { beats } from "../kit/motion.js";
|
|
12
9
|
import { VerticalAxis } from "../kit/vertical-axis.js";
|
|
13
|
-
import { clampUnit, formatMarker, lerpAxisDomain, markerValues, num, resolveAxisDomain, } from "../kit/shared.js";
|
|
14
|
-
import { formatNumber } from "@motionscript/sdk/component";
|
|
15
|
-
import { asCommand, drive, hold, sequence as sequenceOf, together, } from "@motionscript/sdk/component";
|
|
10
|
+
import { clampUnit, formatMarker, lerpAxisDomain, lerpAxisEntry, markerStep, markerValues, num, resolveAxisDomain, settleAxisDomain, } from "../kit/shared.js";
|
|
16
11
|
import { CategoryAxis } from "./category-axis.js";
|
|
17
12
|
import { ChartBars } from "./chart-bars.js";
|
|
18
13
|
import { DEFAULT_BAR_CHART_THEME, barCategories, barSpans, lerpBarChartTheme, resolveBarChartTheme, resolveBarFill, resolveBarScale, stackCapIndex, } from "./shared.js";
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
* sit at the same size in the same box and a scene can swap one for the other
|
|
22
|
-
* without re-tuning its layout.
|
|
23
|
-
*/
|
|
24
|
-
/** Left gutter reserved for the value axis markers. */
|
|
25
|
-
const Y_AXIS_GUTTER = 64;
|
|
26
|
-
/** Bottom gutter (within the chart area) reserved for the category captions. */
|
|
27
|
-
const X_AXIS_GUTTER = 40;
|
|
28
|
-
/** Height reserved below the chart area for the legend row. */
|
|
29
|
-
const LEGEND_GUTTER = 56;
|
|
30
|
-
/** Extra left/bottom room when an axis caption is shown. */
|
|
31
|
-
const Y_LABEL_GUTTER = 34;
|
|
32
|
-
const X_LABEL_GUTTER = 30;
|
|
33
|
-
/** Headroom above the plot, so a column reaching the top marker isn't flush to it. */
|
|
34
|
-
const TOP_HEADROOM = 24;
|
|
35
|
-
/** Right gutter so the last slot isn't flush with the box edge. */
|
|
36
|
-
const RIGHT_GUTTER = 20;
|
|
14
|
+
/** The argument every per-series command takes: an index into `series`, offered by name. */
|
|
15
|
+
const SERIES_ARG = Types.custom(0, { kind: "series", min: 0, step: 1 });
|
|
37
16
|
/** How far the legend starts below its slot when it enters, in px. */
|
|
38
17
|
const LEGEND_SLIDE = 20;
|
|
18
|
+
/** Fallback marker count when the value axis states no ticks. */
|
|
19
|
+
const DEFAULT_MARKERS = 5;
|
|
20
|
+
/**
|
|
21
|
+
* How `enter` divides its duration between its three beats: the axes arriving,
|
|
22
|
+
* the columns rising, and the legend coming up. Shares of the whole, so they
|
|
23
|
+
* must sum to 1.
|
|
24
|
+
*/
|
|
39
25
|
const AXES_SHARE = 0.35;
|
|
40
26
|
const BARS_SHARE = 0.5;
|
|
41
27
|
const LEGEND_SHARE = 0.15;
|
|
28
|
+
/** The value axis with its range and ticks resolved: the explicit ones, else five across the scale. */
|
|
29
|
+
function drawnValue(s, domain) {
|
|
30
|
+
const divisions = (domain.ticks?.length ?? DEFAULT_MARKERS) - 1;
|
|
31
|
+
const range = resolveBarScale(s.data, s.series, s.stacked, domain.range, divisions);
|
|
32
|
+
return { ...domain, range, ticks: domain.ticks ?? markerValues(range, DEFAULT_MARKERS - 1) };
|
|
33
|
+
}
|
|
34
|
+
/** The value axis as a bar chart draws it now, an auto range or tick row read back as itself. */
|
|
35
|
+
export function barChartDomain(s) {
|
|
36
|
+
return settleAxisDomain(s.valueAxis, (d) => drawnValue(s, d));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A value written down as this chart's value axis says: the ticks and the
|
|
40
|
+
* figures printed on the columns both read through here, so the two cannot
|
|
41
|
+
* disagree about what a number looks like.
|
|
42
|
+
*/
|
|
43
|
+
function valueLabel(s, domain, value) {
|
|
44
|
+
const pattern = s.valueAxis.format;
|
|
45
|
+
return pattern === undefined ? formatMarker(value, markerStep(domain.ticks)) : formatNumber(value, pattern);
|
|
46
|
+
}
|
|
47
|
+
/** A value on the plot's **centred** pixel space (origin at its centre, y-up). */
|
|
48
|
+
function valueToY(domain, plot, value) {
|
|
49
|
+
const [min, max] = domain.range;
|
|
50
|
+
if (max <= min)
|
|
51
|
+
return -plot.height / 2;
|
|
52
|
+
return ((value - min) / (max - min) - 0.5) * plot.height;
|
|
53
|
+
}
|
|
42
54
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
55
|
+
* Pixel y of the line every column grows from: zero clamped onto the scale, so
|
|
56
|
+
* a cropped axis grows its columns from the plot floor.
|
|
57
|
+
*/
|
|
58
|
+
function baselinePx(domain, plot) {
|
|
59
|
+
const [min, max] = domain.range;
|
|
60
|
+
return valueToY(domain, plot, Math.min(Math.max(0, min), max));
|
|
61
|
+
}
|
|
62
|
+
/** Series `i`'s live share of the slot; 0 once it has been hidden. */
|
|
63
|
+
function weightAt(s, index) {
|
|
64
|
+
return Math.max(0, s.weights[index] ?? 1);
|
|
65
|
+
}
|
|
66
|
+
/** `row` with each series' value scaled by its live weight: what the stacked spans are measured from. */
|
|
67
|
+
function weightedRow(s, row) {
|
|
68
|
+
const scaled = { ...row };
|
|
69
|
+
s.series.forEach((series, i) => {
|
|
70
|
+
const value = row[series.field];
|
|
71
|
+
if (typeof value === "number")
|
|
72
|
+
scaled[series.field] = value * weightAt(s, i);
|
|
73
|
+
});
|
|
74
|
+
return scaled;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Series `index`'s columns, mapped into the plot's centred pixel space. A
|
|
78
|
+
* grouped series divides the slot with its siblings by their live weights, a
|
|
79
|
+
* stacked one takes the whole slot and runs from the running total below it,
|
|
80
|
+
* so hiding a series reflows rather than punching a hole.
|
|
81
|
+
*/
|
|
82
|
+
function seriesBars(s, index, plot, captioned) {
|
|
83
|
+
const series = s.series[index];
|
|
84
|
+
if (!series)
|
|
85
|
+
return [];
|
|
86
|
+
const domain = barChartDomain(s);
|
|
87
|
+
const n = s.data.length;
|
|
88
|
+
const slotWidth = n > 0 ? plot.width / n : 0;
|
|
89
|
+
const slotCenter = (i) => -plot.width / 2 + slotWidth * (i + 0.5);
|
|
90
|
+
const inner = slotWidth * clampUnit(s.theme.bar.slotFill);
|
|
91
|
+
const gap = s.theme.bar.groupGap;
|
|
92
|
+
let total = 0;
|
|
93
|
+
for (let i = 0; i < s.series.length; i++)
|
|
94
|
+
total += weightAt(s, i);
|
|
95
|
+
const unit = inner / (total > 0 ? total : 1);
|
|
96
|
+
const weight = weightAt(s, index);
|
|
97
|
+
let before = 0;
|
|
98
|
+
for (let i = 0; i < index; i++)
|
|
99
|
+
before += weightAt(s, i);
|
|
100
|
+
return s.data.map((row, rowIndex) => {
|
|
101
|
+
// The stack is built from the *live* contributions, so a series fading out
|
|
102
|
+
// lowers the segments above it as it goes.
|
|
103
|
+
const spans = barSpans(s.stacked ? weightedRow(s, row) : row, s.series, s.stacked);
|
|
104
|
+
const span = spans[index] ?? { from: 0, to: 0, value: 0 };
|
|
105
|
+
const cap = s.stacked ? stackCapIndex(row, s.series) : index;
|
|
106
|
+
const slotLeft = slotCenter(rowIndex) - inner / 2;
|
|
107
|
+
const x = s.stacked ? slotCenter(rowIndex) : slotLeft + unit * before + (unit * weight) / 2;
|
|
108
|
+
const width = s.stacked ? inner : Math.max(0, unit * weight - (s.series.length > 1 ? gap : 0));
|
|
109
|
+
// The figure printed is the row's own value, not the weighted one.
|
|
110
|
+
const value = num(row, series.field);
|
|
111
|
+
return {
|
|
112
|
+
x,
|
|
113
|
+
width,
|
|
114
|
+
from: valueToY(domain, plot, span.from),
|
|
115
|
+
to: valueToY(domain, plot, span.to),
|
|
116
|
+
round: cap === index,
|
|
117
|
+
value,
|
|
118
|
+
...(captioned && { caption: valueLabel(s, domain, value) }),
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* How far each category is dimmed by the spotlight, one entry per row: 1 while
|
|
124
|
+
* nothing is spotlit, falling to the theme's `dimOpacity` for every category but
|
|
125
|
+
* the chosen one as the reveal climbs.
|
|
126
|
+
*/
|
|
127
|
+
function categoryDims(s) {
|
|
128
|
+
const focus = clampUnit(s.spotlightReveal);
|
|
129
|
+
const dim = s.theme.spotlight.dimOpacity;
|
|
130
|
+
const chosen = s.spotlight;
|
|
131
|
+
return s.data.map((_, i) => (focus <= 0 || chosen === i ? 1 : 1 - (1 - dim) * focus));
|
|
132
|
+
}
|
|
133
|
+
/** The parts a bar chart's commands drive, found through its children. */
|
|
134
|
+
function partsOf(children) {
|
|
135
|
+
const frame = cartesianViews(children);
|
|
136
|
+
if (!frame)
|
|
137
|
+
return null;
|
|
138
|
+
const cAxis = frame.below.find(CategoryAxis.is);
|
|
139
|
+
if (!VerticalAxis.is(frame.axis) || !cAxis)
|
|
140
|
+
return null;
|
|
141
|
+
return {
|
|
142
|
+
vAxis: frame.axis,
|
|
143
|
+
cAxis,
|
|
144
|
+
bars: frame.plot.filter(ChartBars.is),
|
|
145
|
+
legend: frame.below.find(ChartLegend.is) ?? null,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Move series `index`'s slot share to `target` with its legend entry, the group
|
|
150
|
+
* reflowing. Its columns are drawn for every frame the move covers, and switch
|
|
151
|
+
* off only once a hide has landed, so arriving at a time backwards shows what
|
|
152
|
+
* arriving forwards does.
|
|
153
|
+
*/
|
|
154
|
+
function toggling(timeline, state, children, index, target) {
|
|
155
|
+
const parts = partsOf(children);
|
|
156
|
+
if (!parts || !parts.bars[index])
|
|
157
|
+
return timeline.wait(timeline.duration);
|
|
158
|
+
// Read through `weightAt`, so a chart whose stored weights are short still
|
|
159
|
+
// tweens from the 1 it is drawing at.
|
|
160
|
+
const from = state.series.map((_, i) => weightAt(state, i));
|
|
161
|
+
const start = from[index] ?? 1;
|
|
162
|
+
const showing = state.series.map((s, i) => state.showing[i] ?? (s.enabled ?? true));
|
|
163
|
+
return timeline.together([
|
|
164
|
+
timeline.animate((t) => {
|
|
165
|
+
const weights = [...from];
|
|
166
|
+
weights[index] = start + (target - start) * timeline.ease(t);
|
|
167
|
+
const shown = [...showing];
|
|
168
|
+
shown[index] = target > 0 || t < 1;
|
|
169
|
+
return { weights, showing: shown };
|
|
170
|
+
}, { eased: false }),
|
|
171
|
+
...(parts.legend ? [parts.legend.run(target > 0 ? "showSeries" : "hideSeries", { index })] : []),
|
|
172
|
+
]);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* A grouped or stacked bar chart, the sibling of the line chart and built the
|
|
176
|
+
* same way, out of several of the same pieces:
|
|
46
177
|
*
|
|
47
178
|
* - `ChartCanvas` — the plot panel with a horizontal grid, under everything.
|
|
48
179
|
* - `VerticalAxis` — the numeric value markers, staggering in bottom-to-top.
|
|
49
|
-
* - {@link CategoryAxis} — the row of names beneath the baseline, one per row
|
|
50
|
-
* the data, staggering in left-to-right.
|
|
180
|
+
* - {@link CategoryAxis} — the row of names beneath the baseline, one per row.
|
|
51
181
|
* - {@link ChartBars} (one per series) — that series' column in every category,
|
|
52
182
|
* plus the figures printed on them.
|
|
53
183
|
* - `ChartLegend` — a row of entries below the chart, one per series.
|
|
54
184
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* re-flows if its box changes, and the bars read the same resolved scale the
|
|
60
|
-
* axis does, so a column's end lands exactly on its marker.
|
|
61
|
-
*
|
|
62
|
-
* **Resting state is the finished chart** — the same bargain the line chart
|
|
63
|
-
* makes: the studio paints a node's Initial state on a paused canvas, so a chart
|
|
64
|
-
* that only appeared once a command had run would be edited blind. {@link enter}
|
|
65
|
-
* arms every part back to its pre-entrance state before animating.
|
|
185
|
+
* Every part reads the chart's entries through bindings, and the columns map
|
|
186
|
+
* across the plot panel's own laid-out box, so a column's end lands exactly on
|
|
187
|
+
* its marker. **Zero is always on the scale**, and the columns grow about it, so
|
|
188
|
+
* a negative value draws downward out of the same `growth` signal.
|
|
66
189
|
*
|
|
67
|
-
* **
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* {@link showSeries}/{@link hideSeries}/{@link spotlightSeries} pick out a
|
|
73
|
-
* *series* — and hiding one **reflows the group**, the remaining columns
|
|
74
|
-
* widening into the space as it goes, rather than leaving a hole where it was —
|
|
75
|
-
* while {@link spotlightCategory}/{@link clearCategory} pick out a *column of
|
|
76
|
-
* the table*, dimming every other category and its caption with it.
|
|
190
|
+
* **Resting state is the finished chart**; `enter` arms every part on its first
|
|
191
|
+
* frame. `showSeries`/`hideSeries`/`spotlightSeries` pick out a *series* (hiding
|
|
192
|
+
* one reflows the group), `spotlightCategory`/`clearCategory` a *column of the
|
|
193
|
+
* table*. A `to` of the value axis's `range` or `ticks` tweens from the domain
|
|
194
|
+
* as drawn, so an auto axis rescales the columns continuously.
|
|
77
195
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const areaTop = H / 2;
|
|
134
|
-
const areaBottom = -H / 2 + (this.legend ? LEGEND_GUTTER : 0);
|
|
135
|
-
const left = -W / 2 + this.leftInset;
|
|
136
|
-
const right = W / 2 - RIGHT_GUTTER;
|
|
137
|
-
const top = areaTop - TOP_HEADROOM;
|
|
138
|
-
const bottom = areaBottom + this.bottomInset;
|
|
139
|
-
return { left, right, top, bottom };
|
|
140
|
-
}
|
|
141
|
-
/** Width and height of the plot, in px. */
|
|
142
|
-
plotSize() {
|
|
143
|
-
const { left, right, top, bottom } = this.plotBox();
|
|
144
|
-
return { w: right - left, h: top - bottom };
|
|
145
|
-
}
|
|
146
|
-
/** Map a value onto the plot's **centred** pixel space (origin at its centre, y-up). */
|
|
147
|
-
valueToY(value) {
|
|
148
|
-
const [min, max] = this.valueScale;
|
|
149
|
-
const { h } = this.plotSize();
|
|
150
|
-
if (max <= min)
|
|
151
|
-
return -h / 2;
|
|
152
|
-
return ((value - min) / (max - min) - 0.5) * h;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Pixel y of the line every column grows from.
|
|
156
|
-
*
|
|
157
|
-
* Zero, clamped onto the scale — so an author who *did* crop the axis to
|
|
158
|
-
* `[40, 100]` gets columns rising from the plot floor rather than from a
|
|
159
|
-
* baseline off the bottom of the panel.
|
|
160
|
-
*/
|
|
161
|
-
get baselinePx() {
|
|
162
|
-
const [min, max] = this.valueScale;
|
|
163
|
-
return this.valueToY(Math.min(Math.max(0, min), max));
|
|
164
|
-
}
|
|
165
|
-
// ---- Slot geometry ----------------------------------------------------
|
|
166
|
-
/** Width of one category's slot across the plot. */
|
|
167
|
-
get slotWidth() {
|
|
168
|
-
const n = this.data.length;
|
|
169
|
-
return n > 0 ? this.plotSize().w / n : 0;
|
|
170
|
-
}
|
|
171
|
-
/** Centre x of category `i`'s slot, in the plot's centred space. */
|
|
172
|
-
slotCenter(index) {
|
|
173
|
-
return -this.plotSize().w / 2 + this.slotWidth * (index + 0.5);
|
|
174
|
-
}
|
|
175
|
-
/** The part of a slot the columns fill — the rest is the channel between categories. */
|
|
176
|
-
get slotInner() {
|
|
177
|
-
return this.slotWidth * clampUnit(this.resolvedTheme.bar.slotFill);
|
|
178
|
-
}
|
|
179
|
-
/** Series `i`'s live share of the slot; 0 once it has been hidden. */
|
|
180
|
-
weightAt(index) {
|
|
181
|
-
return Math.max(0, this.weights[index] ?? 1);
|
|
182
|
-
}
|
|
183
|
-
/** Total live share across the series — the slot's inner width divides by this. */
|
|
184
|
-
get totalWeight() {
|
|
185
|
-
let sum = 0;
|
|
186
|
-
for (let i = 0; i < this.series.length; i++)
|
|
187
|
-
sum += this.weightAt(i);
|
|
188
|
-
return sum > 0 ? sum : 1;
|
|
189
|
-
}
|
|
190
|
-
/** Live share of every series *before* `index`, for the grouped offset. */
|
|
191
|
-
weightBefore(index) {
|
|
192
|
-
let sum = 0;
|
|
193
|
-
for (let i = 0; i < index; i++)
|
|
194
|
-
sum += this.weightAt(i);
|
|
195
|
-
return sum;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Series `index`'s columns, mapped into the plot's centred pixel space.
|
|
199
|
-
*
|
|
200
|
-
* Both modes come out of one walk over the rows: a grouped series divides the
|
|
201
|
-
* slot with its siblings and every column runs from zero, a stacked one takes
|
|
202
|
-
* the whole slot and each column runs from the running total below it. The
|
|
203
|
-
* live {@link weights} scale the grouped widths and the stacked contributions
|
|
204
|
-
* alike, which is what lets `hideSeries` reflow rather than punch a hole.
|
|
205
|
-
*/
|
|
206
|
-
seriesBars(index) {
|
|
207
|
-
const series = this.series[index];
|
|
208
|
-
if (!series)
|
|
209
|
-
return [];
|
|
210
|
-
const stacked = this.stacked;
|
|
211
|
-
const inner = this.slotInner;
|
|
212
|
-
const gap = this.resolvedTheme.bar.groupGap;
|
|
213
|
-
const unit = inner / this.totalWeight;
|
|
214
|
-
const weight = this.weightAt(index);
|
|
215
|
-
return this.data.map((row, rowIndex) => {
|
|
216
|
-
// The stack is built from the *live* contributions, so a series fading out
|
|
217
|
-
// lowers the segments above it as it goes instead of dropping them by a
|
|
218
|
-
// step when it finally switches off. A grouped row needs no such scaling —
|
|
219
|
-
// there its weight moves the column's width, not its length.
|
|
220
|
-
const spans = barSpans(stacked ? this.weightedRow(row) : row, this.series, stacked);
|
|
221
|
-
const span = spans[index] ?? { from: 0, to: 0, value: 0 };
|
|
222
|
-
const cap = stacked ? stackCapIndex(row, this.series) : index;
|
|
223
|
-
const slotLeft = this.slotCenter(rowIndex) - inner / 2;
|
|
224
|
-
const x = stacked
|
|
225
|
-
? this.slotCenter(rowIndex)
|
|
226
|
-
: slotLeft + unit * this.weightBefore(index) + (unit * weight) / 2;
|
|
227
|
-
const width = stacked
|
|
228
|
-
? inner
|
|
229
|
-
: Math.max(0, unit * weight - (this.series.length > 1 ? gap : 0));
|
|
230
|
-
return {
|
|
231
|
-
x,
|
|
232
|
-
width,
|
|
233
|
-
from: this.valueToY(span.from),
|
|
234
|
-
to: this.valueToY(span.to),
|
|
235
|
-
round: cap === index,
|
|
236
|
-
// The figure printed is the row's own value, not the weighted one — a
|
|
237
|
-
// series on its way out should not be seen counting down.
|
|
238
|
-
value: num(row, series.field),
|
|
239
|
-
};
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* `row` with each series' value scaled by that series' live {@link weights}
|
|
244
|
-
* entry — what the stacked spans are measured from.
|
|
245
|
-
*/
|
|
246
|
-
weightedRow(row) {
|
|
247
|
-
const scaled = { ...row };
|
|
248
|
-
this.series.forEach((s, i) => {
|
|
249
|
-
const value = row[s.field];
|
|
250
|
-
if (typeof value === "number")
|
|
251
|
-
scaled[s.field] = value * this.weightAt(i);
|
|
252
|
-
});
|
|
253
|
-
return scaled;
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* How far each category is dimmed by the spotlight, one entry per row —
|
|
257
|
-
* 1 while nothing is spotlit, falling to the theme's `dimOpacity` for every
|
|
258
|
-
* category but the chosen one as the reveal climbs.
|
|
259
|
-
*/
|
|
260
|
-
categoryDims() {
|
|
261
|
-
const focus = clampUnit(this.spotlightReveal);
|
|
262
|
-
const dim = this.resolvedTheme.spotlight.dimOpacity;
|
|
263
|
-
const chosen = this.spotlight;
|
|
264
|
-
return this.data.map((_, i) => focus <= 0 || chosen === i ? 1 : 1 - (1 - dim) * focus);
|
|
265
|
-
}
|
|
266
|
-
// ---- Build ------------------------------------------------------------
|
|
267
|
-
constructor(props) {
|
|
268
|
-
super(props);
|
|
269
|
-
// The whole chart is composed from plain props/`data` — no context needed,
|
|
270
|
-
// so the parts are built here (super() applied the props above). Everything
|
|
271
|
-
// a *leaf* reads live in `renderSelf` — each series' geometry, paint and
|
|
272
|
-
// dim, the panel's fill, the border's stroke — is passed as a closure rather
|
|
273
|
-
// than a snapshot, so `chart.to({ theme })` and
|
|
274
|
-
// `chart.to({ valueAxis: { range } })` genuinely animate the drawn chart.
|
|
275
|
-
const enabled = this.series.map((s) => s.enabled ?? true);
|
|
276
|
-
this.weights = enabled.map((on) => (on ? 1 : 0));
|
|
277
|
-
const showLegend = props?.showLegend !== false;
|
|
278
|
-
const showValues = props?.showValues === true;
|
|
279
|
-
this.vAxis = new VerticalAxis({
|
|
280
|
-
range: () => this.valueScale,
|
|
281
|
-
markers: () => this.resolvedMarkers,
|
|
282
|
-
axis: () => this.resolvedTheme.yAxis,
|
|
283
|
-
label: this.valueAxis.label ?? "",
|
|
196
|
+
export const BarChart = component2d({
|
|
197
|
+
key: "barChart",
|
|
198
|
+
state: {
|
|
199
|
+
data: fresh(() => []),
|
|
200
|
+
categoryField: "",
|
|
201
|
+
series: fresh(() => []),
|
|
202
|
+
stacked: false,
|
|
203
|
+
valueAxis: Types.custom({}, { resolve: resolveAxisDomain, lerp: lerpAxisEntry }),
|
|
204
|
+
categoryAxis: Types.custom({}, { resolve: resolveAxisDomain, lerp: lerpAxisDomain }),
|
|
205
|
+
theme: Types.custom(DEFAULT_BAR_CHART_THEME, { resolve: resolveBarChartTheme, lerp: lerpBarChartTheme }),
|
|
206
|
+
/**
|
|
207
|
+
* Global rise of every column in `[0, 1]`, measured about the baseline: 0
|
|
208
|
+
* collapses them all onto it, 1 draws them at full length.
|
|
209
|
+
*/
|
|
210
|
+
growth: 1,
|
|
211
|
+
/**
|
|
212
|
+
* How much of the slot each series claims, one entry per series: a weight
|
|
213
|
+
* rather than a switch, so hiding one is the others widening into the space.
|
|
214
|
+
* Seeded from each series' `enabled`.
|
|
215
|
+
*/
|
|
216
|
+
weights: fresh(() => []),
|
|
217
|
+
/** The category spotlit, or `null` for none. The choice snaps; `spotlightReveal` is the animation. */
|
|
218
|
+
spotlight: Types.custom(null),
|
|
219
|
+
/** How far the category spotlight is drawn in, `[0, 1]`. */
|
|
220
|
+
spotlightReveal: 0,
|
|
221
|
+
/** Read as the chart is built: the legend is a part the chart builds or doesn't. */
|
|
222
|
+
showLegend: Types.boolean(true, { animatable: false }),
|
|
223
|
+
/** Read as the chart is built: whether each column prints the figure it plots. */
|
|
224
|
+
showValues: Types.boolean(false, { animatable: false }),
|
|
225
|
+
/** Whether each series' columns are drawn: on for every frame a show or hide covers, off once a hide lands. */
|
|
226
|
+
showing: Types.custom([], { private: true }),
|
|
227
|
+
},
|
|
228
|
+
defaults: {
|
|
229
|
+
// Its parts fill it; a node made of them would hug them.
|
|
230
|
+
width: "fill",
|
|
231
|
+
height: "fill",
|
|
232
|
+
weights: (state) => state.series.map((s) => ((s.enabled ?? true) ? 1 : 0)),
|
|
233
|
+
showing: (state) => state.series.map((s) => s.enabled ?? true),
|
|
234
|
+
},
|
|
235
|
+
children: (state) => {
|
|
236
|
+
const theme = () => state.theme;
|
|
237
|
+
const domain = () => barChartDomain(state);
|
|
238
|
+
let plot;
|
|
239
|
+
// Before the panel is built or laid out it has no size, and the columns map onto nothing.
|
|
240
|
+
const plotSize = () => laidOutSize(plot);
|
|
241
|
+
const captioned = state.showValues === true;
|
|
242
|
+
const vAxis = new VerticalAxis({
|
|
243
|
+
range: () => domain().range,
|
|
244
|
+
markers: () => domain().ticks,
|
|
245
|
+
captions: () => {
|
|
246
|
+
const d = domain();
|
|
247
|
+
return d.ticks.map((value) => valueLabel(state, d, value));
|
|
248
|
+
},
|
|
249
|
+
axis: () => theme().yAxis,
|
|
250
|
+
label: state.valueAxis.label ?? "",
|
|
284
251
|
width: "hug",
|
|
285
252
|
height: "fill",
|
|
286
253
|
});
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
dims: () => this.categoryDims(),
|
|
254
|
+
const cAxis = new CategoryAxis({
|
|
255
|
+
categories: barCategories(state.data, state.categoryField),
|
|
256
|
+
axis: () => theme().xAxis,
|
|
257
|
+
label: state.categoryAxis.label ?? "",
|
|
258
|
+
dims: () => categoryDims(state),
|
|
293
259
|
width: "fill",
|
|
294
260
|
height: "hug",
|
|
295
261
|
});
|
|
296
|
-
|
|
297
|
-
bars: () =>
|
|
298
|
-
baseline: () =>
|
|
299
|
-
fill: () => resolveBarFill(
|
|
300
|
-
barStroke: () =>
|
|
301
|
-
barShadow: () =>
|
|
302
|
-
cornerRadius: () =>
|
|
303
|
-
growth: () =>
|
|
304
|
-
dims: () =>
|
|
305
|
-
showValues,
|
|
306
|
-
valueStyle:
|
|
307
|
-
enabled:
|
|
262
|
+
const barSets = state.series.map((s, i) => new ChartBars({
|
|
263
|
+
bars: () => seriesBars(state, i, plotSize(), captioned),
|
|
264
|
+
baseline: () => baselinePx(domain(), plotSize()),
|
|
265
|
+
fill: () => resolveBarFill(theme().seriesFills, s, i),
|
|
266
|
+
barStroke: () => theme().bar.stroke,
|
|
267
|
+
barShadow: () => theme().bar.shadow,
|
|
268
|
+
cornerRadius: () => theme().bar.cornerRadius,
|
|
269
|
+
growth: () => state.growth,
|
|
270
|
+
dims: () => categoryDims(state),
|
|
271
|
+
showValues: captioned,
|
|
272
|
+
valueStyle: state.theme.valueStyle,
|
|
273
|
+
enabled: () => state.showing[i] ?? (s.enabled ?? true),
|
|
308
274
|
}));
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
275
|
+
const below = [cAxis];
|
|
276
|
+
if (state.showLegend !== false) {
|
|
277
|
+
below.push(new ChartLegend({
|
|
278
|
+
series: state.series,
|
|
279
|
+
swatches: () => state.series.map((s, i) => resolveBarFill(theme().seriesFills, s, i)),
|
|
280
|
+
legend: () => theme().legend,
|
|
281
|
+
// The row's spacing is the theme's as the chart was built, like the
|
|
282
|
+
// entries' type and chips: a theme tween re-paints the swatches only.
|
|
283
|
+
gap: state.theme.legend.itemGap,
|
|
284
|
+
padding: state.theme.legend.padding,
|
|
285
|
+
}));
|
|
319
286
|
}
|
|
320
|
-
|
|
287
|
+
plot = new Rect({
|
|
321
288
|
clip: true,
|
|
322
289
|
children: [
|
|
323
290
|
new ChartCanvas({
|
|
324
|
-
fill: () =>
|
|
325
|
-
shadow: () =>
|
|
326
|
-
xGridStroke: () =>
|
|
327
|
-
yGridStroke: () =>
|
|
328
|
-
// No vertical rules: the horizontal axis is categorical, so there is
|
|
329
|
-
//
|
|
330
|
-
// between slots does that job, and better.
|
|
291
|
+
fill: () => theme().plotArea.fill,
|
|
292
|
+
shadow: () => theme().plotArea.shadow,
|
|
293
|
+
xGridStroke: () => theme().xAxis.lineStyle,
|
|
294
|
+
yGridStroke: () => theme().yAxis.lineStyle,
|
|
295
|
+
// No vertical rules: the horizontal axis is categorical, so there is no
|
|
296
|
+
// numeric position along it for a grid line to mark.
|
|
331
297
|
xMarkers: [],
|
|
332
|
-
yMarkers: () =>
|
|
298
|
+
yMarkers: () => domain().ticks,
|
|
333
299
|
xExtent: () => [0, 1],
|
|
334
|
-
yExtent: () =>
|
|
335
|
-
}),
|
|
336
|
-
...this.barSets,
|
|
337
|
-
new ChartBorder({
|
|
338
|
-
stroke: () => this.resolvedTheme.plotArea.stroke,
|
|
300
|
+
yExtent: () => domain().range,
|
|
339
301
|
}),
|
|
302
|
+
...barSets,
|
|
303
|
+
new ChartBorder({ stroke: () => theme().plotArea.stroke }),
|
|
340
304
|
],
|
|
341
305
|
});
|
|
342
|
-
|
|
343
|
-
// plot so the category row starts at the plot's left edge rather than the
|
|
344
|
-
// chart's — and so a caption lands under the columns it names.
|
|
345
|
-
const gutterSpacer = new VerticalAxis({
|
|
306
|
+
const gutter = new VerticalAxis({
|
|
346
307
|
opacity: 0,
|
|
347
|
-
range: () =>
|
|
348
|
-
markers: () =>
|
|
349
|
-
axis: () =>
|
|
350
|
-
label:
|
|
308
|
+
range: () => domain().range,
|
|
309
|
+
markers: () => domain().ticks,
|
|
310
|
+
axis: () => theme().yAxis,
|
|
311
|
+
label: state.valueAxis.label ?? "",
|
|
351
312
|
width: "hug",
|
|
352
313
|
height: 0,
|
|
353
314
|
});
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
below
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
if (!bars)
|
|
444
|
-
return asCommand(hold(duration));
|
|
445
|
-
return asCommand(together(this.tweenWeight(index, target, duration, easing), drive(duration, (t) => {
|
|
446
|
-
const share = t >= 1 ? target : Math.max(target, 1 - t);
|
|
447
|
-
bars.set({ enabled: target > 0 || share > 0 });
|
|
448
|
-
}), ...(this.legend
|
|
449
|
-
? [
|
|
450
|
-
target > 0
|
|
451
|
-
? this.legend.showSeries({ data: { index }, duration, easing })
|
|
452
|
-
: this.legend.hideSeries({ data: { index }, duration, easing }),
|
|
453
|
-
]
|
|
454
|
-
: [])), duration);
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Fade out every other series — its columns **and its legend entry** — to
|
|
458
|
-
* focus attention on `index`, which comes back to full. Nothing moves, so the
|
|
459
|
-
* group doesn't reflow.
|
|
460
|
-
*/
|
|
461
|
-
spotlightSeries(args) {
|
|
462
|
-
const { seriesIndex = 0 } = args.data ?? {};
|
|
463
|
-
const duration = args.duration ?? 0.35;
|
|
464
|
-
const easing = args.easing ?? easeOut();
|
|
465
|
-
const dim = this.resolvedTheme.spotlight.dimOpacity;
|
|
466
|
-
return asCommand(together(...this.barSets.map((bars, i) => bars.to({ data: { opacity: i === seriesIndex ? 1 : dim }, duration, easing })), ...(this.legend
|
|
467
|
-
? [this.legend.spotlightSeries({ data: { index: seriesIndex }, duration, easing })]
|
|
468
|
-
: [])), duration);
|
|
469
|
-
}
|
|
470
|
-
/** Ease series `index`'s slot share toward `target`, reflowing the group. */
|
|
471
|
-
tweenWeight(index, target, duration, easing) {
|
|
472
|
-
// Read through `weightAt` rather than off the array, so a chart whose stored
|
|
473
|
-
// weights are short (or absent) still tweens from the 1 it is drawing at.
|
|
474
|
-
const from = this.series.map((_, i) => this.weightAt(i));
|
|
475
|
-
const start = from[index] ?? 1;
|
|
476
|
-
if (start === target)
|
|
477
|
-
return hold(duration);
|
|
478
|
-
return drive(duration, (t) => {
|
|
479
|
-
const next = [...from];
|
|
480
|
-
next[index] = start + (target - start) * easing(t);
|
|
481
|
-
this.weights = next;
|
|
482
|
-
});
|
|
483
|
-
}
|
|
484
|
-
// ---- Per-category ------------------------------------------------------
|
|
485
|
-
/**
|
|
486
|
-
* Spotlight one **category** — one column of the table rather than one series:
|
|
487
|
-
* every other category's bars and its caption fade to the theme's
|
|
488
|
-
* `dimOpacity`, and the chosen one stays full.
|
|
489
|
-
*
|
|
490
|
-
* The counterpart of the line chart's `spotlightRegion`, and the same shape of
|
|
491
|
-
* thing: it picks out a slice of the plot without changing what is drawn.
|
|
492
|
-
* Calling it while another category is already lit moves the focus directly —
|
|
493
|
-
* only the dimming changes, so there is nothing to retract first.
|
|
494
|
-
*/
|
|
495
|
-
spotlightCategory(args) {
|
|
496
|
-
const { categoryIndex = 0 } = args.data ?? {};
|
|
497
|
-
const duration = args.duration ?? 0.4;
|
|
498
|
-
const easing = args.easing ?? easeOut();
|
|
499
|
-
if (categoryIndex < 0 || categoryIndex >= this.data.length)
|
|
500
|
-
return this.holdSpotlight(duration);
|
|
501
|
-
const from = this.spotlightReveal;
|
|
502
|
-
// Which category is lit is part of the value, set from the first frame: the
|
|
503
|
-
// focus moves directly, so unlike the line chart's region there is nothing to
|
|
504
|
-
// retract before it.
|
|
505
|
-
return this.command((t) => ({ spotlight: categoryIndex, spotlightReveal: from + (1 - from) * t }), duration, easing);
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* Drop the category spotlight, bringing every category back to full. A no-op
|
|
509
|
-
* when nothing is lit — but it still takes its slot on the timeline, for the
|
|
510
|
-
* reason `LineChart.clearRegion` does.
|
|
511
|
-
*/
|
|
512
|
-
clearCategory(args) {
|
|
513
|
-
const duration = args.duration ?? 0.4;
|
|
514
|
-
const easing = args.easing ?? easeOut();
|
|
515
|
-
const from = this.spotlightReveal;
|
|
516
|
-
if (from === 0)
|
|
517
|
-
return this.holdSpotlight(duration);
|
|
518
|
-
// The lit category is held until the fade lands — cleared at `t === 1` rather
|
|
519
|
-
// than up front, or there would be nothing left to fade.
|
|
520
|
-
return this.command((t) => t >= 1
|
|
521
|
-
? { spotlight: null, spotlightReveal: 0 }
|
|
522
|
-
: { spotlightReveal: from * (1 - t) }, duration, easing);
|
|
523
|
-
}
|
|
524
|
-
/** The no-op both of the above return — see {@link hold}. */
|
|
525
|
-
holdSpotlight(duration) {
|
|
526
|
-
return this.command(() => ({}), duration);
|
|
527
|
-
}
|
|
528
|
-
};
|
|
529
|
-
__decorate([
|
|
530
|
-
property({ default: () => [] })
|
|
531
|
-
], BarChart.prototype, "data", void 0);
|
|
532
|
-
__decorate([
|
|
533
|
-
property({ default: "" })
|
|
534
|
-
], BarChart.prototype, "categoryField", void 0);
|
|
535
|
-
__decorate([
|
|
536
|
-
property({ default: () => [] })
|
|
537
|
-
], BarChart.prototype, "series", void 0);
|
|
538
|
-
__decorate([
|
|
539
|
-
property({ default: false })
|
|
540
|
-
], BarChart.prototype, "stacked", void 0);
|
|
541
|
-
__decorate([
|
|
542
|
-
property({
|
|
543
|
-
default: {},
|
|
544
|
-
mapper: resolveAxisDomain,
|
|
545
|
-
tween: lerpAxisDomain,
|
|
546
|
-
})
|
|
547
|
-
], BarChart.prototype, "valueAxis", void 0);
|
|
548
|
-
__decorate([
|
|
549
|
-
property({
|
|
550
|
-
default: {},
|
|
551
|
-
mapper: resolveAxisDomain,
|
|
552
|
-
tween: lerpAxisDomain,
|
|
553
|
-
})
|
|
554
|
-
], BarChart.prototype, "categoryAxis", void 0);
|
|
555
|
-
__decorate([
|
|
556
|
-
property({
|
|
557
|
-
default: DEFAULT_BAR_CHART_THEME,
|
|
558
|
-
mapper: resolveBarChartTheme,
|
|
559
|
-
tween: lerpBarChartTheme,
|
|
560
|
-
})
|
|
561
|
-
], BarChart.prototype, "theme", void 0);
|
|
562
|
-
__decorate([
|
|
563
|
-
property({ default: 1 })
|
|
564
|
-
], BarChart.prototype, "growth", void 0);
|
|
565
|
-
__decorate([
|
|
566
|
-
property({ default: () => [] })
|
|
567
|
-
], BarChart.prototype, "weights", void 0);
|
|
568
|
-
__decorate([
|
|
569
|
-
property({ default: null })
|
|
570
|
-
], BarChart.prototype, "spotlight", void 0);
|
|
571
|
-
__decorate([
|
|
572
|
-
property({ default: 0 })
|
|
573
|
-
], BarChart.prototype, "spotlightReveal", void 0);
|
|
574
|
-
__decorate([
|
|
575
|
-
command()
|
|
576
|
-
], BarChart.prototype, "enter", null);
|
|
577
|
-
__decorate([
|
|
578
|
-
command({ args: [{ key: "seriesIndex", kind: "series", default: 0, min: 0, step: 1 }] })
|
|
579
|
-
], BarChart.prototype, "showSeries", null);
|
|
580
|
-
__decorate([
|
|
581
|
-
command({ args: [{ key: "seriesIndex", kind: "series", default: 0, min: 0, step: 1 }] })
|
|
582
|
-
], BarChart.prototype, "hideSeries", null);
|
|
583
|
-
__decorate([
|
|
584
|
-
command({ args: [{ key: "seriesIndex", kind: "series", default: 0, min: 0, step: 1 }] })
|
|
585
|
-
], BarChart.prototype, "spotlightSeries", null);
|
|
586
|
-
__decorate([
|
|
587
|
-
command({ args: [{ key: "categoryIndex", kind: "number", default: 0, min: 0, step: 1 }] })
|
|
588
|
-
], BarChart.prototype, "spotlightCategory", null);
|
|
589
|
-
__decorate([
|
|
590
|
-
command()
|
|
591
|
-
], BarChart.prototype, "clearCategory", null);
|
|
592
|
-
BarChart = BarChart_1 = __decorate([
|
|
593
|
-
node({
|
|
594
|
-
key: "barChart",
|
|
595
|
-
parentKey: "node",
|
|
596
|
-
forkable: true,
|
|
597
|
-
layout: {
|
|
598
|
-
children: "freeform",
|
|
599
|
-
defaultWidthMode: "fixed",
|
|
600
|
-
defaultHeightMode: "fixed",
|
|
601
|
-
acceptsChildren: true,
|
|
315
|
+
return asParts([
|
|
316
|
+
...state.children,
|
|
317
|
+
cartesianFrame({ axis: vAxis, plot, gutter, below }),
|
|
318
|
+
]);
|
|
319
|
+
},
|
|
320
|
+
commands: {
|
|
321
|
+
/**
|
|
322
|
+
* Animate the whole chart in: the two axes stagger their markers and
|
|
323
|
+
* captions in, the columns rise from the baseline, and the legend fades up
|
|
324
|
+
* last. Its first frame arms every part: markers and captions shifted and
|
|
325
|
+
* transparent, columns collapsed, the legend dropped and transparent.
|
|
326
|
+
* `duration` is the **whole** entrance; the beats are shares of it.
|
|
327
|
+
*/
|
|
328
|
+
enter: {
|
|
329
|
+
args: {},
|
|
330
|
+
easing: easeOut(),
|
|
331
|
+
run: (timeline, _state, children) => {
|
|
332
|
+
const parts = partsOf(children);
|
|
333
|
+
if (!parts)
|
|
334
|
+
return timeline.wait(timeline.duration);
|
|
335
|
+
const duration = timeline.duration;
|
|
336
|
+
const axes = duration * AXES_SHARE;
|
|
337
|
+
const rise = duration * BARS_SHARE;
|
|
338
|
+
const { legend } = parts;
|
|
339
|
+
const at0 = (piece) => ({ at: 0, duration: 0, piece });
|
|
340
|
+
// Without a legend the beat is still its share of the whole.
|
|
341
|
+
return beats(timeline, [
|
|
342
|
+
at0(timeline.animate(() => ({ growth: 0 }))),
|
|
343
|
+
...(legend ? [at0(legend.to({ opacity: 0, y: -LEGEND_SLIDE }))] : []),
|
|
344
|
+
{ at: 0, duration: axes, piece: parts.vAxis.run("enter", { slide: 24 }) },
|
|
345
|
+
{ at: 0, duration: axes, piece: parts.cAxis.run("enter", { slide: 24 }) },
|
|
346
|
+
// No priming to 0 first: the value at `t` is a function of `t`.
|
|
347
|
+
{ at: axes, duration: rise, piece: timeline.animate((t) => ({ growth: t })) },
|
|
348
|
+
...(legend ? [{ at: axes + rise, duration: duration * LEGEND_SHARE, piece: legend.to({ opacity: 1, y: 0 }) }] : []),
|
|
349
|
+
]);
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
/** Show series `index`: its columns come back, the group reflowing to make room, with its legend entry. */
|
|
353
|
+
showSeries: {
|
|
354
|
+
args: { seriesIndex: SERIES_ARG },
|
|
355
|
+
easing: easeOut(),
|
|
356
|
+
run: (timeline, state, children) => toggling(timeline, state, children, timeline.args.seriesIndex, 1),
|
|
357
|
+
},
|
|
358
|
+
/**
|
|
359
|
+
* Hide series `index`: its columns narrow away (or, stacked, sink out of the
|
|
360
|
+
* stack) while the rest of the group widens into the space — the mirror of
|
|
361
|
+
* `showSeries`.
|
|
362
|
+
*/
|
|
363
|
+
hideSeries: {
|
|
364
|
+
args: { seriesIndex: SERIES_ARG },
|
|
365
|
+
easing: easeOut(),
|
|
366
|
+
run: (timeline, state, children) => toggling(timeline, state, children, timeline.args.seriesIndex, 0),
|
|
367
|
+
},
|
|
368
|
+
/**
|
|
369
|
+
* Fade every other series — its columns **and its legend entry** — to focus
|
|
370
|
+
* on `index`, which comes back to full. Nothing moves, so the group doesn't
|
|
371
|
+
* reflow.
|
|
372
|
+
*/
|
|
373
|
+
spotlightSeries: {
|
|
374
|
+
args: { seriesIndex: SERIES_ARG },
|
|
375
|
+
easing: easeOut(),
|
|
376
|
+
run: (timeline, state, children) => {
|
|
377
|
+
const parts = partsOf(children);
|
|
378
|
+
if (!parts)
|
|
379
|
+
return timeline.wait(timeline.duration);
|
|
380
|
+
const index = timeline.args.seriesIndex;
|
|
381
|
+
const dim = state.theme.spotlight.dimOpacity;
|
|
382
|
+
return timeline.together([
|
|
383
|
+
...parts.bars.map((bars, i) => bars.to({ opacity: i === index ? 1 : dim })),
|
|
384
|
+
...(parts.legend ? [parts.legend.run("spotlightSeries", { index })] : []),
|
|
385
|
+
]);
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
/**
|
|
389
|
+
* Spotlight one **category**: every other category's columns and caption
|
|
390
|
+
* fade to the theme's `dimOpacity`. Called while another is lit, the focus
|
|
391
|
+
* moves directly: only the dimming changes, so nothing retracts first. A
|
|
392
|
+
* category that is not there takes its slot and changes nothing.
|
|
393
|
+
*/
|
|
394
|
+
spotlightCategory: {
|
|
395
|
+
args: { categoryIndex: Types.number(0, { min: 0, step: 1 }) },
|
|
396
|
+
easing: easeOut(),
|
|
397
|
+
run: (timeline, state) => {
|
|
398
|
+
const index = timeline.args.categoryIndex;
|
|
399
|
+
if (index < 0 || index >= state.data.length)
|
|
400
|
+
return timeline.wait(timeline.duration);
|
|
401
|
+
const from = state.spotlightReveal;
|
|
402
|
+
return timeline.animate((t) => ({ spotlight: index, spotlightReveal: from + (1 - from) * t }));
|
|
403
|
+
},
|
|
602
404
|
},
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
405
|
+
/**
|
|
406
|
+
* Drop the category spotlight, every category back to full. The lit
|
|
407
|
+
* category is held until the fade lands; a no-op when nothing is lit, which
|
|
408
|
+
* still takes its slot.
|
|
409
|
+
*/
|
|
410
|
+
clearCategory: {
|
|
411
|
+
args: {},
|
|
412
|
+
easing: easeOut(),
|
|
413
|
+
run: (timeline, state) => {
|
|
414
|
+
const from = state.spotlightReveal;
|
|
415
|
+
if (from === 0)
|
|
416
|
+
return timeline.wait(timeline.duration);
|
|
417
|
+
const lit = state.spotlight;
|
|
418
|
+
return timeline.animate((t) => (t >= 1 ? { spotlight: null, spotlightReveal: 0 } : { spotlight: lit, spotlightReveal: from * (1 - t) }));
|
|
419
|
+
},
|
|
606
420
|
},
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
export { BarChart };
|
|
421
|
+
},
|
|
422
|
+
});
|
|
610
423
|
//# sourceMappingURL=bar-chart.js.map
|