@motionscript/charts 0.1.0-alpha.5 → 0.1.0-alpha.7
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 +40 -0
- package/README.md +30 -16
- package/dist/bar-chart/bar-chart.d.ts +133 -242
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +378 -569
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +35 -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 +50 -70
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +156 -210
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +2 -2
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +1 -1
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +3 -3
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +3 -3
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-XROBIRQK.js +2 -0
- package/dist/browser/chunks/chunk-XROBIRQK.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/engine.d.ts +8 -6
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +8 -6
- package/dist/engine.js.map +1 -1
- package/dist/kit/cartesian.d.ts +44 -0
- package/dist/kit/cartesian.d.ts.map +1 -0
- package/dist/kit/cartesian.js +59 -0
- package/dist/kit/cartesian.js.map +1 -0
- package/dist/kit/chart-border.d.ts +10 -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 +32 -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 +123 -64
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +129 -154
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.d.ts +17 -0
- package/dist/kit/entries.d.ts.map +1 -0
- package/dist/kit/entries.js +19 -0
- package/dist/kit/entries.js.map +1 -0
- package/dist/kit/motion.d.ts +48 -0
- package/dist/kit/motion.d.ts.map +1 -0
- package/dist/kit/motion.js +63 -0
- package/dist/kit/motion.js.map +1 -0
- package/dist/kit/paints.d.ts +10 -0
- package/dist/kit/paints.d.ts.map +1 -0
- package/dist/kit/paints.js +12 -0
- package/dist/kit/paints.js.map +1 -0
- package/dist/kit/shared.d.ts +31 -3
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +42 -3
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/vertical-axis.d.ts +48 -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 +60 -77
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +100 -121
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +32 -33
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +31 -65
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/horizontal-axis.d.ts +58 -64
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -1
- package/dist/line-chart/horizontal-axis.js +94 -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 +156 -319
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +403 -663
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +3 -3
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +3 -3
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/nodes.d.ts +6 -9
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +7 -9
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +2 -2
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -1
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +108 -132
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +200 -271
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-legend.d.ts +84 -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 +160 -131
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +333 -391
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +2 -2
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +2 -2
- package/dist/pie-chart/shared.js.map +1 -1
- package/package.json +4 -4
- package/src/bar-chart/bar-chart.ts +415 -701
- package/src/bar-chart/category-axis.ts +95 -110
- package/src/bar-chart/chart-bars.ts +178 -200
- package/src/bar-chart/index.ts +2 -2
- package/src/bar-chart/shared.ts +3 -3
- package/src/engine.ts +8 -6
- package/src/kit/cartesian.ts +81 -0
- package/src/kit/chart-border.ts +22 -33
- package/src/kit/chart-canvas.ts +44 -79
- package/src/kit/chart-legend.ts +173 -189
- package/src/kit/entries.ts +20 -0
- package/src/kit/motion.ts +90 -0
- package/src/kit/paints.ts +15 -0
- package/src/kit/shared.ts +52 -3
- package/src/kit/vertical-axis.ts +110 -140
- package/src/line-chart/chart-line.ts +102 -154
- package/src/line-chart/chart-region.ts +35 -80
- package/src/line-chart/horizontal-axis.ts +101 -152
- package/src/line-chart/index.ts +2 -2
- package/src/line-chart/line-chart.ts +449 -825
- package/src/line-chart/shared.ts +3 -3
- package/src/nodes.ts +8 -10
- package/src/pie-chart/index.ts +2 -2
- package/src/pie-chart/pie-chart.ts +221 -311
- package/src/pie-chart/pie-legend.ts +124 -114
- package/src/pie-chart/pie-ring.ts +386 -473
- package/src/pie-chart/shared.ts +2 -2
- package/dist/browser/chunks/chunk-5ZPBV6BU.js +0 -2
- package/dist/browser/chunks/chunk-5ZPBV6BU.js.map +0 -7
|
@@ -1,614 +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,
|
|
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
|
-
* size in the same box and a scene can swap one for the other without re-tuning
|
|
22
|
-
* its layout. The plot's top grid line is the one place a marker would
|
|
23
|
-
* otherwise be drawn outside the chart's own box.
|
|
24
|
-
*/
|
|
25
|
-
const TOP_HEADROOM = 24;
|
|
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 });
|
|
26
16
|
/** How far the legend starts below its slot when it enters, in px. */
|
|
27
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
|
+
*/
|
|
28
25
|
const AXES_SHARE = 0.35;
|
|
29
26
|
const BARS_SHARE = 0.5;
|
|
30
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
|
+
}
|
|
54
|
+
/**
|
|
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
|
+
}
|
|
31
76
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
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:
|
|
35
177
|
*
|
|
36
178
|
* - `ChartCanvas` — the plot panel with a horizontal grid, under everything.
|
|
37
179
|
* - `VerticalAxis` — the numeric value markers, staggering in bottom-to-top.
|
|
38
|
-
* - {@link CategoryAxis} — the row of names beneath the baseline, one per row
|
|
39
|
-
* the data, staggering in left-to-right.
|
|
180
|
+
* - {@link CategoryAxis} — the row of names beneath the baseline, one per row.
|
|
40
181
|
* - {@link ChartBars} (one per series) — that series' column in every category,
|
|
41
182
|
* plus the figures printed on them.
|
|
42
183
|
* - `ChartLegend` — a row of entries below the chart, one per series.
|
|
43
184
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* resolved scale the axis does, so a column's end lands exactly on its marker
|
|
49
|
-
* and the chart re-flows if its box changes.
|
|
50
|
-
*
|
|
51
|
-
* **Resting state is the finished chart** — the same bargain the line chart
|
|
52
|
-
* makes: an editor paints a node's Initial state on a paused canvas, so a chart
|
|
53
|
-
* that only appeared once a command had run would be edited blind. {@link enter}
|
|
54
|
-
* 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.
|
|
55
189
|
*
|
|
56
|
-
* **
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* {@link showSeries}/{@link hideSeries}/{@link spotlightSeries} pick out a
|
|
62
|
-
* *series* — and hiding one **reflows the group**, the remaining columns
|
|
63
|
-
* widening into the space as it goes, rather than leaving a hole where it was —
|
|
64
|
-
* while {@link spotlightCategory}/{@link clearCategory} pick out a *column of
|
|
65
|
-
* 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.
|
|
66
195
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
return { w: width, h: height };
|
|
123
|
-
}
|
|
124
|
-
/** Map a value onto the plot's **centred** pixel space (origin at its centre, y-up). */
|
|
125
|
-
valueToY(value) {
|
|
126
|
-
const [min, max] = this.valueScale;
|
|
127
|
-
const { h } = this.plotSize();
|
|
128
|
-
if (max <= min)
|
|
129
|
-
return -h / 2;
|
|
130
|
-
return ((value - min) / (max - min) - 0.5) * h;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Pixel y of the line every column grows from.
|
|
134
|
-
*
|
|
135
|
-
* Zero, clamped onto the scale — so an author who *did* crop the axis to
|
|
136
|
-
* `[40, 100]` gets columns rising from the plot floor rather than from a
|
|
137
|
-
* baseline off the bottom of the panel.
|
|
138
|
-
*/
|
|
139
|
-
get baselinePx() {
|
|
140
|
-
const [min, max] = this.valueScale;
|
|
141
|
-
return this.valueToY(Math.min(Math.max(0, min), max));
|
|
142
|
-
}
|
|
143
|
-
// ---- Slot geometry ----------------------------------------------------
|
|
144
|
-
/** Width of one category's slot across the plot. */
|
|
145
|
-
get slotWidth() {
|
|
146
|
-
const n = this.data.length;
|
|
147
|
-
return n > 0 ? this.plotSize().w / n : 0;
|
|
148
|
-
}
|
|
149
|
-
/** Centre x of category `i`'s slot, in the plot's centred space. */
|
|
150
|
-
slotCenter(index) {
|
|
151
|
-
return -this.plotSize().w / 2 + this.slotWidth * (index + 0.5);
|
|
152
|
-
}
|
|
153
|
-
/** The part of a slot the columns fill — the rest is the channel between categories. */
|
|
154
|
-
get slotInner() {
|
|
155
|
-
return this.slotWidth * clampUnit(this.resolvedTheme.bar.slotFill);
|
|
156
|
-
}
|
|
157
|
-
/** Series `i`'s live share of the slot; 0 once it has been hidden. */
|
|
158
|
-
weightAt(index) {
|
|
159
|
-
return Math.max(0, this.weights[index] ?? 1);
|
|
160
|
-
}
|
|
161
|
-
/** Total live share across the series — the slot's inner width divides by this. */
|
|
162
|
-
get totalWeight() {
|
|
163
|
-
let sum = 0;
|
|
164
|
-
for (let i = 0; i < this.series.length; i++)
|
|
165
|
-
sum += this.weightAt(i);
|
|
166
|
-
return sum > 0 ? sum : 1;
|
|
167
|
-
}
|
|
168
|
-
/** Live share of every series *before* `index`, for the grouped offset. */
|
|
169
|
-
weightBefore(index) {
|
|
170
|
-
let sum = 0;
|
|
171
|
-
for (let i = 0; i < index; i++)
|
|
172
|
-
sum += this.weightAt(i);
|
|
173
|
-
return sum;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Series `index`'s columns, mapped into the plot's centred pixel space.
|
|
177
|
-
*
|
|
178
|
-
* Both modes come out of one walk over the rows: a grouped series divides the
|
|
179
|
-
* slot with its siblings and every column runs from zero, a stacked one takes
|
|
180
|
-
* the whole slot and each column runs from the running total below it. The
|
|
181
|
-
* live {@link weights} scale the grouped widths and the stacked contributions
|
|
182
|
-
* alike, which is what lets `hideSeries` reflow rather than punch a hole.
|
|
183
|
-
*/
|
|
184
|
-
seriesBars(index) {
|
|
185
|
-
const series = this.series[index];
|
|
186
|
-
if (!series)
|
|
187
|
-
return [];
|
|
188
|
-
const stacked = this.stacked;
|
|
189
|
-
const inner = this.slotInner;
|
|
190
|
-
const gap = this.resolvedTheme.bar.groupGap;
|
|
191
|
-
const unit = inner / this.totalWeight;
|
|
192
|
-
const weight = this.weightAt(index);
|
|
193
|
-
return this.data.map((row, rowIndex) => {
|
|
194
|
-
// The stack is built from the *live* contributions, so a series fading out
|
|
195
|
-
// lowers the segments above it as it goes instead of dropping them by a
|
|
196
|
-
// step when it finally switches off. A grouped row needs no such scaling —
|
|
197
|
-
// there its weight moves the column's width, not its length.
|
|
198
|
-
const spans = barSpans(stacked ? this.weightedRow(row) : row, this.series, stacked);
|
|
199
|
-
const span = spans[index] ?? { from: 0, to: 0, value: 0 };
|
|
200
|
-
const cap = stacked ? stackCapIndex(row, this.series) : index;
|
|
201
|
-
const slotLeft = this.slotCenter(rowIndex) - inner / 2;
|
|
202
|
-
const x = stacked
|
|
203
|
-
? this.slotCenter(rowIndex)
|
|
204
|
-
: slotLeft + unit * this.weightBefore(index) + (unit * weight) / 2;
|
|
205
|
-
const width = stacked
|
|
206
|
-
? inner
|
|
207
|
-
: Math.max(0, unit * weight - (this.series.length > 1 ? gap : 0));
|
|
208
|
-
return {
|
|
209
|
-
x,
|
|
210
|
-
width,
|
|
211
|
-
from: this.valueToY(span.from),
|
|
212
|
-
to: this.valueToY(span.to),
|
|
213
|
-
round: cap === index,
|
|
214
|
-
// The figure printed is the row's own value, not the weighted one — a
|
|
215
|
-
// series on its way out should not be seen counting down.
|
|
216
|
-
value: num(row, series.field),
|
|
217
|
-
};
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* `row` with each series' value scaled by that series' live {@link weights}
|
|
222
|
-
* entry — what the stacked spans are measured from.
|
|
223
|
-
*/
|
|
224
|
-
weightedRow(row) {
|
|
225
|
-
const scaled = { ...row };
|
|
226
|
-
this.series.forEach((s, i) => {
|
|
227
|
-
const value = row[s.field];
|
|
228
|
-
if (typeof value === "number")
|
|
229
|
-
scaled[s.field] = value * this.weightAt(i);
|
|
230
|
-
});
|
|
231
|
-
return scaled;
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* How far each category is dimmed by the spotlight, one entry per row —
|
|
235
|
-
* 1 while nothing is spotlit, falling to the theme's `dimOpacity` for every
|
|
236
|
-
* category but the chosen one as the reveal climbs.
|
|
237
|
-
*/
|
|
238
|
-
categoryDims() {
|
|
239
|
-
const focus = clampUnit(this.spotlightReveal);
|
|
240
|
-
const dim = this.resolvedTheme.spotlight.dimOpacity;
|
|
241
|
-
const chosen = this.spotlight;
|
|
242
|
-
return this.data.map((_, i) => focus <= 0 || chosen === i ? 1 : 1 - (1 - dim) * focus);
|
|
243
|
-
}
|
|
244
|
-
// ---- Build ------------------------------------------------------------
|
|
245
|
-
constructor(props) {
|
|
246
|
-
super(props);
|
|
247
|
-
// The whole chart is composed from plain props/`data` — no context needed,
|
|
248
|
-
// so the parts are built here (super() applied the props above). Everything
|
|
249
|
-
// a *leaf* reads live in `renderSelf` — each series' geometry, paint and
|
|
250
|
-
// dim, the panel's fill, the border's stroke — is passed as a closure rather
|
|
251
|
-
// than a snapshot, so `chart.to({ theme })` and
|
|
252
|
-
// `chart.to({ valueAxis: { range } })` genuinely animate the drawn chart.
|
|
253
|
-
const enabled = this.series.map((s) => s.enabled ?? true);
|
|
254
|
-
this.weights = enabled.map((on) => (on ? 1 : 0));
|
|
255
|
-
const showLegend = props?.showLegend !== false;
|
|
256
|
-
const showValues = props?.showValues === true;
|
|
257
|
-
this.vAxis = new VerticalAxis({
|
|
258
|
-
range: () => this.valueScale,
|
|
259
|
-
markers: () => this.resolvedMarkers,
|
|
260
|
-
axis: () => this.resolvedTheme.yAxis,
|
|
261
|
-
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 ?? "",
|
|
262
251
|
width: "hug",
|
|
263
252
|
height: "fill",
|
|
264
253
|
});
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
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),
|
|
271
259
|
width: "fill",
|
|
272
260
|
height: "hug",
|
|
273
261
|
});
|
|
274
|
-
|
|
275
|
-
bars: () =>
|
|
276
|
-
baseline: () =>
|
|
277
|
-
fill: () => resolveBarFill(
|
|
278
|
-
barStroke: () =>
|
|
279
|
-
barShadow: () =>
|
|
280
|
-
cornerRadius: () =>
|
|
281
|
-
growth: () =>
|
|
282
|
-
dims: () =>
|
|
283
|
-
showValues,
|
|
284
|
-
valueStyle:
|
|
285
|
-
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),
|
|
286
274
|
}));
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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
|
+
}));
|
|
297
286
|
}
|
|
298
|
-
|
|
287
|
+
plot = new Rect({
|
|
299
288
|
clip: true,
|
|
300
289
|
children: [
|
|
301
290
|
new ChartCanvas({
|
|
302
|
-
fill: () =>
|
|
303
|
-
shadow: () =>
|
|
304
|
-
xGridStroke: () =>
|
|
305
|
-
yGridStroke: () =>
|
|
306
|
-
// No vertical rules: the horizontal axis is categorical, so there is
|
|
307
|
-
//
|
|
308
|
-
// 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.
|
|
309
297
|
xMarkers: [],
|
|
310
|
-
yMarkers: () =>
|
|
298
|
+
yMarkers: () => domain().ticks,
|
|
311
299
|
xExtent: () => [0, 1],
|
|
312
|
-
yExtent: () =>
|
|
313
|
-
}),
|
|
314
|
-
...this.barSets,
|
|
315
|
-
new ChartBorder({
|
|
316
|
-
stroke: () => this.resolvedTheme.plotArea.stroke,
|
|
300
|
+
yExtent: () => domain().range,
|
|
317
301
|
}),
|
|
302
|
+
...barSets,
|
|
303
|
+
new ChartBorder({ stroke: () => theme().plotArea.stroke }),
|
|
318
304
|
],
|
|
319
305
|
});
|
|
320
|
-
|
|
321
|
-
// plot so the category row starts at the plot's left edge rather than the
|
|
322
|
-
// chart's — and so a caption lands under the columns it names.
|
|
323
|
-
const gutterSpacer = new VerticalAxis({
|
|
306
|
+
const gutter = new VerticalAxis({
|
|
324
307
|
opacity: 0,
|
|
325
|
-
range: () =>
|
|
326
|
-
markers: () =>
|
|
327
|
-
axis: () =>
|
|
328
|
-
label:
|
|
308
|
+
range: () => domain().range,
|
|
309
|
+
markers: () => domain().ticks,
|
|
310
|
+
axis: () => theme().yAxis,
|
|
311
|
+
label: state.valueAxis.label ?? "",
|
|
329
312
|
width: "hug",
|
|
330
313
|
height: 0,
|
|
331
314
|
});
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
below
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
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
|
-
* Show series `index`: bring its columns back, the group reflowing to make
|
|
422
|
-
* room as they arrive, in lockstep with its legend entry growing in.
|
|
423
|
-
*/
|
|
424
|
-
showSeries(args) {
|
|
425
|
-
const { seriesIndex = 0 } = args.data ?? {};
|
|
426
|
-
return this.toggleSeries(seriesIndex, 1, args.duration ?? 0.35, args.easing ?? easeOut());
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Hide series `index`: its columns narrow away (or, stacked, sink out of the
|
|
430
|
-
* stack) while the rest of the group widens into the space, its legend entry
|
|
431
|
-
* shrinking beside them — the mirror of {@link showSeries}.
|
|
432
|
-
*/
|
|
433
|
-
hideSeries(args) {
|
|
434
|
-
const { seriesIndex = 0 } = args.data ?? {};
|
|
435
|
-
return this.toggleSeries(seriesIndex, 0, args.duration ?? 0.35, args.easing ?? easeOut());
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Move series `index`'s slot share to `target`, with its legend entry.
|
|
439
|
-
*
|
|
440
|
-
* One method for both directions, so the pair cannot drift apart. `enabled`
|
|
441
|
-
* rides in the same value as the weight rather than being set before or after,
|
|
442
|
-
* which is what makes arriving at a time backwards show what arriving forwards
|
|
443
|
-
* does.
|
|
444
|
-
*/
|
|
445
|
-
toggleSeries(index, target, duration, easing) {
|
|
446
|
-
const bars = this.barSets[index];
|
|
447
|
-
if (!bars)
|
|
448
|
-
return asCommand(hold(duration));
|
|
449
|
-
return asCommand(together(this.tweenWeight(index, target, duration, easing), drive(duration, (t) => {
|
|
450
|
-
const share = t >= 1 ? target : Math.max(target, 1 - t);
|
|
451
|
-
bars.set({ enabled: target > 0 || share > 0 });
|
|
452
|
-
}), ...(this.legend
|
|
453
|
-
? [
|
|
454
|
-
target > 0
|
|
455
|
-
? this.legend.showSeries({ data: { index }, duration, easing })
|
|
456
|
-
: this.legend.hideSeries({ data: { index }, duration, easing }),
|
|
457
|
-
]
|
|
458
|
-
: [])), duration);
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Fade out every other series — its columns **and its legend entry** — to
|
|
462
|
-
* focus attention on `index`, which comes back to full. Nothing moves, so the
|
|
463
|
-
* group doesn't reflow.
|
|
464
|
-
*/
|
|
465
|
-
spotlightSeries(args) {
|
|
466
|
-
const { seriesIndex = 0 } = args.data ?? {};
|
|
467
|
-
const duration = args.duration ?? 0.35;
|
|
468
|
-
const easing = args.easing ?? easeOut();
|
|
469
|
-
const dim = this.resolvedTheme.spotlight.dimOpacity;
|
|
470
|
-
return asCommand(together(...this.barSets.map((bars, i) => bars.to({ data: { opacity: i === seriesIndex ? 1 : dim }, duration, easing })), ...(this.legend
|
|
471
|
-
? [this.legend.spotlightSeries({ data: { index: seriesIndex }, duration, easing })]
|
|
472
|
-
: [])), duration);
|
|
473
|
-
}
|
|
474
|
-
/** Ease series `index`'s slot share toward `target`, reflowing the group. */
|
|
475
|
-
tweenWeight(index, target, duration, easing) {
|
|
476
|
-
// Read through `weightAt` rather than off the array, so a chart whose stored
|
|
477
|
-
// weights are short (or absent) still tweens from the 1 it is drawing at.
|
|
478
|
-
const from = this.series.map((_, i) => this.weightAt(i));
|
|
479
|
-
const start = from[index] ?? 1;
|
|
480
|
-
if (start === target)
|
|
481
|
-
return hold(duration);
|
|
482
|
-
return drive(duration, (t) => {
|
|
483
|
-
const next = [...from];
|
|
484
|
-
next[index] = start + (target - start) * easing(t);
|
|
485
|
-
this.weights = next;
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
// ---- Per-category ------------------------------------------------------
|
|
489
|
-
/**
|
|
490
|
-
* Spotlight one **category** — one column of the table rather than one series:
|
|
491
|
-
* every other category's bars and its caption fade to the theme's
|
|
492
|
-
* `dimOpacity`, and the chosen one stays full.
|
|
493
|
-
*
|
|
494
|
-
* The counterpart of the line chart's `spotlightRegion`, and the same shape of
|
|
495
|
-
* thing: it picks out a slice of the plot without changing what is drawn.
|
|
496
|
-
* Calling it while another category is already lit moves the focus directly —
|
|
497
|
-
* only the dimming changes, so there is nothing to retract first.
|
|
498
|
-
*/
|
|
499
|
-
spotlightCategory(args) {
|
|
500
|
-
const { categoryIndex = 0 } = args.data ?? {};
|
|
501
|
-
const duration = args.duration ?? 0.4;
|
|
502
|
-
const easing = args.easing ?? easeOut();
|
|
503
|
-
if (categoryIndex < 0 || categoryIndex >= this.data.length)
|
|
504
|
-
return this.holdSpotlight(duration);
|
|
505
|
-
const from = this.spotlightReveal;
|
|
506
|
-
// Which category is lit is part of the value, set from the first frame: the
|
|
507
|
-
// focus moves directly, so unlike the line chart's region there is nothing to
|
|
508
|
-
// retract before it.
|
|
509
|
-
return this.command((t) => ({ spotlight: categoryIndex, spotlightReveal: from + (1 - from) * t }), duration, easing);
|
|
510
|
-
}
|
|
511
|
-
/**
|
|
512
|
-
* Drop the category spotlight, bringing every category back to full. A no-op
|
|
513
|
-
* when nothing is lit — but it still takes its slot on the timeline, for the
|
|
514
|
-
* reason `LineChart.clearRegion` does.
|
|
515
|
-
*/
|
|
516
|
-
clearCategory(args) {
|
|
517
|
-
const duration = args.duration ?? 0.4;
|
|
518
|
-
const easing = args.easing ?? easeOut();
|
|
519
|
-
const from = this.spotlightReveal;
|
|
520
|
-
if (from === 0)
|
|
521
|
-
return this.holdSpotlight(duration);
|
|
522
|
-
// The lit category is held until the fade lands — cleared at `t === 1` rather
|
|
523
|
-
// than up front, or there would be nothing left to fade.
|
|
524
|
-
return this.command((t) => t >= 1
|
|
525
|
-
? { spotlight: null, spotlightReveal: 0 }
|
|
526
|
-
: { spotlightReveal: from * (1 - t) }, duration, easing);
|
|
527
|
-
}
|
|
528
|
-
/** The no-op both of the above return — see {@link hold}. */
|
|
529
|
-
holdSpotlight(duration) {
|
|
530
|
-
return this.command(() => ({}), duration);
|
|
531
|
-
}
|
|
532
|
-
};
|
|
533
|
-
__decorate([
|
|
534
|
-
property({ default: () => [] })
|
|
535
|
-
], BarChart.prototype, "data", void 0);
|
|
536
|
-
__decorate([
|
|
537
|
-
property({ default: "" })
|
|
538
|
-
], BarChart.prototype, "categoryField", void 0);
|
|
539
|
-
__decorate([
|
|
540
|
-
property({ default: () => [] })
|
|
541
|
-
], BarChart.prototype, "series", void 0);
|
|
542
|
-
__decorate([
|
|
543
|
-
property({ default: false })
|
|
544
|
-
], BarChart.prototype, "stacked", void 0);
|
|
545
|
-
__decorate([
|
|
546
|
-
property({
|
|
547
|
-
default: {},
|
|
548
|
-
mapper: resolveAxisDomain,
|
|
549
|
-
tween: lerpAxisDomain,
|
|
550
|
-
})
|
|
551
|
-
], BarChart.prototype, "valueAxis", void 0);
|
|
552
|
-
__decorate([
|
|
553
|
-
property({
|
|
554
|
-
default: {},
|
|
555
|
-
mapper: resolveAxisDomain,
|
|
556
|
-
tween: lerpAxisDomain,
|
|
557
|
-
})
|
|
558
|
-
], BarChart.prototype, "categoryAxis", void 0);
|
|
559
|
-
__decorate([
|
|
560
|
-
property({
|
|
561
|
-
default: DEFAULT_BAR_CHART_THEME,
|
|
562
|
-
mapper: resolveBarChartTheme,
|
|
563
|
-
tween: lerpBarChartTheme,
|
|
564
|
-
})
|
|
565
|
-
], BarChart.prototype, "theme", void 0);
|
|
566
|
-
__decorate([
|
|
567
|
-
property({ default: 1 })
|
|
568
|
-
], BarChart.prototype, "growth", void 0);
|
|
569
|
-
__decorate([
|
|
570
|
-
property({ default: () => [] })
|
|
571
|
-
], BarChart.prototype, "weights", void 0);
|
|
572
|
-
__decorate([
|
|
573
|
-
property({ default: null })
|
|
574
|
-
], BarChart.prototype, "spotlight", void 0);
|
|
575
|
-
__decorate([
|
|
576
|
-
property({ default: 0 })
|
|
577
|
-
], BarChart.prototype, "spotlightReveal", void 0);
|
|
578
|
-
__decorate([
|
|
579
|
-
command()
|
|
580
|
-
], BarChart.prototype, "enter", null);
|
|
581
|
-
__decorate([
|
|
582
|
-
command({ args: [{ key: "seriesIndex", kind: "series", default: 0, min: 0, step: 1 }] })
|
|
583
|
-
], BarChart.prototype, "showSeries", null);
|
|
584
|
-
__decorate([
|
|
585
|
-
command({ args: [{ key: "seriesIndex", kind: "series", default: 0, min: 0, step: 1 }] })
|
|
586
|
-
], BarChart.prototype, "hideSeries", null);
|
|
587
|
-
__decorate([
|
|
588
|
-
command({ args: [{ key: "seriesIndex", kind: "series", default: 0, min: 0, step: 1 }] })
|
|
589
|
-
], BarChart.prototype, "spotlightSeries", null);
|
|
590
|
-
__decorate([
|
|
591
|
-
command({ args: [{ key: "categoryIndex", kind: "number", default: 0, min: 0, step: 1 }] })
|
|
592
|
-
], BarChart.prototype, "spotlightCategory", null);
|
|
593
|
-
__decorate([
|
|
594
|
-
command()
|
|
595
|
-
], BarChart.prototype, "clearCategory", null);
|
|
596
|
-
BarChart = BarChart_1 = __decorate([
|
|
597
|
-
node({
|
|
598
|
-
key: "barChart",
|
|
599
|
-
parentKey: "node",
|
|
600
|
-
forkable: true,
|
|
601
|
-
layout: {
|
|
602
|
-
children: "freeform",
|
|
603
|
-
defaultWidthMode: "fixed",
|
|
604
|
-
defaultHeightMode: "fixed",
|
|
605
|
-
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
|
+
},
|
|
606
404
|
},
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
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
|
+
},
|
|
610
420
|
},
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
export { BarChart };
|
|
421
|
+
},
|
|
422
|
+
});
|
|
614
423
|
//# sourceMappingURL=bar-chart.js.map
|