@motionscript/charts 0.1.1-alpha.0 → 0.1.1-alpha.1
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 +19 -0
- package/README.md +98 -28
- package/dist/bar-chart/bar-chart.d.ts +233 -39
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +215 -171
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +12 -7
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +56 -42
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +52 -52
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +80 -89
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +4 -4
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +2 -2
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +36 -65
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +68 -117
- package/dist/bar-chart/shared.js.map +1 -1
- package/dist/browser/chunks/chunk-R3Q5EE6E.js +2 -0
- package/dist/browser/chunks/chunk-R3Q5EE6E.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +4 -4
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +1 -1
- package/dist/engine.d.ts +5 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +5 -2
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/kit/cartesian.d.ts +49 -15
- package/dist/kit/cartesian.d.ts.map +1 -1
- package/dist/kit/cartesian.js +79 -29
- package/dist/kit/cartesian.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +32 -24
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +52 -46
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +8 -4
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +30 -20
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/{line-chart → kit}/horizontal-axis.d.ts +31 -6
- package/dist/kit/horizontal-axis.d.ts.map +1 -0
- package/dist/{line-chart → kit}/horizontal-axis.js +23 -24
- package/dist/kit/horizontal-axis.js.map +1 -0
- package/dist/kit/motion.d.ts +5 -0
- package/dist/kit/motion.d.ts.map +1 -1
- package/dist/kit/motion.js +8 -0
- package/dist/kit/motion.js.map +1 -1
- package/dist/{pie-chart → kit}/pie-legend.d.ts +3 -3
- package/dist/kit/pie-legend.d.ts.map +1 -0
- package/dist/{pie-chart → kit}/pie-legend.js +11 -10
- package/dist/kit/pie-legend.js.map +1 -0
- package/dist/kit/polar.d.ts +31 -0
- package/dist/kit/polar.d.ts.map +1 -0
- package/dist/kit/polar.js +33 -0
- package/dist/kit/polar.js.map +1 -0
- package/dist/kit/shared.d.ts +11 -50
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +58 -141
- package/dist/kit/shared.js.map +1 -1
- package/dist/kit/style.d.ts +254 -0
- package/dist/kit/style.d.ts.map +1 -0
- package/dist/kit/style.js +197 -0
- package/dist/kit/style.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +22 -2
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +56 -43
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +95 -3
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +165 -53
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +1 -1
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +7 -8
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/index.d.ts +5 -6
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +3 -4
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +257 -18
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +282 -134
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +51 -34
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +175 -83
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +2 -0
- package/dist/line-chart/x-scale.d.ts.map +1 -1
- package/dist/line-chart/x-scale.js +15 -0
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.d.ts +3 -1
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +9 -1
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +2 -3
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -2
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +114 -18
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +37 -24
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +16 -5
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +89 -55
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +53 -61
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +41 -121
- package/dist/pie-chart/shared.js.map +1 -1
- package/dist/radar-chart/index.d.ts +5 -0
- package/dist/radar-chart/index.d.ts.map +1 -0
- package/dist/radar-chart/index.js +5 -0
- package/dist/radar-chart/index.js.map +1 -0
- package/dist/radar-chart/radar-chart.d.ts +218 -0
- package/dist/radar-chart/radar-chart.d.ts.map +1 -0
- package/dist/radar-chart/radar-chart.js +224 -0
- package/dist/radar-chart/radar-chart.js.map +1 -0
- package/dist/radar-chart/radar-grid.d.ts +43 -0
- package/dist/radar-chart/radar-grid.d.ts.map +1 -0
- package/dist/radar-chart/radar-grid.js +61 -0
- package/dist/radar-chart/radar-grid.js.map +1 -0
- package/dist/radar-chart/radar-series.d.ts +59 -0
- package/dist/radar-chart/radar-series.d.ts.map +1 -0
- package/dist/radar-chart/radar-series.js +71 -0
- package/dist/radar-chart/radar-series.js.map +1 -0
- package/dist/radar-chart/shared.d.ts +73 -0
- package/dist/radar-chart/shared.d.ts.map +1 -0
- package/dist/radar-chart/shared.js +123 -0
- package/dist/radar-chart/shared.js.map +1 -0
- package/dist/radial-chart/index.d.ts +6 -0
- package/dist/radial-chart/index.d.ts.map +1 -0
- package/dist/radial-chart/index.js +4 -0
- package/dist/radial-chart/index.js.map +1 -0
- package/dist/radial-chart/radial-chart.d.ts +216 -0
- package/dist/radial-chart/radial-chart.d.ts.map +1 -0
- package/dist/radial-chart/radial-chart.js +174 -0
- package/dist/radial-chart/radial-chart.js.map +1 -0
- package/dist/radial-chart/radial-rings.d.ts +121 -0
- package/dist/radial-chart/radial-rings.d.ts.map +1 -0
- package/dist/radial-chart/radial-rings.js +273 -0
- package/dist/radial-chart/radial-rings.js.map +1 -0
- package/dist/radial-chart/shared.d.ts +73 -0
- package/dist/radial-chart/shared.d.ts.map +1 -0
- package/dist/radial-chart/shared.js +79 -0
- package/dist/radial-chart/shared.js.map +1 -0
- package/package.json +4 -4
- package/registry.json +21 -6
- package/src/bar-chart/bar-chart.ts +252 -179
- package/src/bar-chart/category-axis.ts +46 -40
- package/src/bar-chart/chart-bars.ts +97 -117
- package/src/bar-chart/index.ts +7 -10
- package/src/bar-chart/shared.ts +78 -238
- package/src/engine.ts +5 -2
- package/src/index.ts +2 -0
- package/src/kit/cartesian.ts +118 -40
- package/src/kit/chart-canvas.ts +49 -54
- package/src/kit/chart-legend.ts +28 -20
- package/src/kit/horizontal-axis.ts +114 -0
- package/src/kit/motion.ts +7 -0
- package/src/{pie-chart → kit}/pie-legend.ts +12 -11
- package/src/kit/polar.ts +54 -0
- package/src/kit/shared.ts +55 -277
- package/src/kit/style.ts +257 -0
- package/src/kit/vertical-axis.ts +67 -48
- package/src/line-chart/chart-line.ts +176 -54
- package/src/line-chart/chart-region.ts +6 -10
- package/src/line-chart/index.ts +14 -10
- package/src/line-chart/line-chart.ts +347 -151
- package/src/line-chart/shared.ts +193 -174
- package/src/line-chart/x-scale.ts +13 -0
- package/src/nodes.ts +9 -1
- package/src/pie-chart/index.ts +2 -23
- package/src/pie-chart/pie-chart.ts +52 -35
- package/src/pie-chart/pie-ring.ts +92 -71
- package/src/pie-chart/shared.ts +68 -266
- package/src/radar-chart/index.ts +4 -0
- package/src/radar-chart/radar-chart.ts +287 -0
- package/src/radar-chart/radar-grid.ts +78 -0
- package/src/radar-chart/radar-series.ts +88 -0
- package/src/radar-chart/shared.ts +146 -0
- package/src/radial-chart/index.ts +5 -0
- package/src/radial-chart/radial-chart.ts +210 -0
- package/src/radial-chart/radial-rings.ts +328 -0
- package/src/radial-chart/shared.ts +114 -0
- package/dist/browser/chunks/chunk-4SMRCU24.js +0 -2
- package/dist/browser/chunks/chunk-4SMRCU24.js.map +0 -7
- package/dist/kit/chart-border.d.ts +0 -18
- package/dist/kit/chart-border.d.ts.map +0 -1
- package/dist/kit/chart-border.js +0 -23
- package/dist/kit/chart-border.js.map +0 -1
- package/dist/line-chart/horizontal-axis.d.ts.map +0 -1
- package/dist/line-chart/horizontal-axis.js.map +0 -1
- package/dist/pie-chart/pie-legend.d.ts.map +0 -1
- package/dist/pie-chart/pie-legend.js.map +0 -1
- package/src/kit/chart-border.ts +0 -36
- package/src/line-chart/horizontal-axis.ts +0 -122
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
import { Graphics2D, Text, Types, clamp, component2d, fillOps,
|
|
2
|
-
import {
|
|
1
|
+
import { Graphics2D, Text, Types, clamp, component2d, fillOps, } from "@motionscript/sdk";
|
|
2
|
+
import { boundText } from "../kit/cartesian.js";
|
|
3
|
+
import { asParts, fresh } from "../kit/entries.js";
|
|
3
4
|
import { frameEase } from "../kit/motion.js";
|
|
4
5
|
import { loadPaints } from "../kit/paints.js";
|
|
5
6
|
import { formatMarker } from "../kit/shared.js";
|
|
6
|
-
import {
|
|
7
|
-
/**
|
|
8
|
-
const
|
|
9
|
-
/**
|
|
10
|
-
const VALUE_FIT = 1.6;
|
|
11
|
-
/** Fallback type size when the theme's value style leaves it unset or autofit. */
|
|
12
|
-
const VALUE_SIZE = 24;
|
|
13
|
-
/**
|
|
14
|
-
* Column `i`'s dimming: the category spotlight, then the series switch. Captions
|
|
15
|
-
* read this because they are child nodes, which a `render` guard would leave on
|
|
16
|
-
* screen over a series that is not there.
|
|
17
|
-
*/
|
|
7
|
+
import { inkOn } from "./shared.js";
|
|
8
|
+
/** A figure inside a bar needs this much room around it, in multiples of the type size. */
|
|
9
|
+
const INSIDE_ROOM = 0.6;
|
|
10
|
+
/** Bar `i`'s dimming: the category spotlight, then the series switch. */
|
|
18
11
|
function dimAt(s, index) {
|
|
19
12
|
if (!s.enabled)
|
|
20
13
|
return 0;
|
|
21
14
|
return clamp(s.dims[index] ?? 1, 0, 1);
|
|
22
15
|
}
|
|
16
|
+
/** Bar `i`'s two ends as grown so far, along the value axis. */
|
|
23
17
|
function endsAt(s, index) {
|
|
24
18
|
const bar = s.bars[index];
|
|
25
19
|
if (!bar)
|
|
@@ -27,90 +21,87 @@ function endsAt(s, index) {
|
|
|
27
21
|
const g = clamp(s.growth, 0, 1);
|
|
28
22
|
return { base: s.baseline + (bar.from - s.baseline) * g, end: s.baseline + (bar.to - s.baseline) * g };
|
|
29
23
|
}
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
return typeof size === "number" ? size : VALUE_SIZE;
|
|
33
|
-
}
|
|
34
|
-
function insideAt(s, index) {
|
|
35
|
-
const bar = s.bars[index];
|
|
36
|
-
return bar !== undefined && Math.abs(bar.from - s.baseline) > 0.5;
|
|
37
|
-
}
|
|
38
|
-
/** Column `i`'s figure, or `""` where there is no room. Blanking rather than dropping the node keeps captions index-aligned, so a rescale into more room prints again. */
|
|
39
|
-
function captionAt(s, index) {
|
|
40
|
-
const bar = s.bars[index];
|
|
41
|
-
if (!bar || bar.value === 0)
|
|
42
|
-
return "";
|
|
43
|
-
// An interior segment prints inside itself and must hold a line of type; a grouped column prints past its end.
|
|
44
|
-
if (insideAt(s, index)) {
|
|
45
|
-
const ends = endsAt(s, index);
|
|
46
|
-
if (!ends)
|
|
47
|
-
return "";
|
|
48
|
-
if (Math.abs(ends.end - ends.base) < valueSize(s) * VALUE_FIT)
|
|
49
|
-
return "";
|
|
50
|
-
}
|
|
51
|
-
return bar.caption ?? formatMarker(bar.value);
|
|
24
|
+
function sizeOf(style) {
|
|
25
|
+
return typeof style.fontSize === "number" ? style.fontSize : 24;
|
|
52
26
|
}
|
|
53
|
-
|
|
27
|
+
/** Where bar `i`'s figure sits along the value axis: past its end, or in its middle. */
|
|
28
|
+
function captionAlong(s, index, extent) {
|
|
54
29
|
const ends = endsAt(s, index);
|
|
55
30
|
if (!ends)
|
|
56
31
|
return 0;
|
|
57
|
-
if (
|
|
32
|
+
if (s.values === "inside")
|
|
58
33
|
return (ends.base + ends.end) / 2;
|
|
59
|
-
// Text is centred on `y`: clear the end by the gap plus half a line, on the side the column reached.
|
|
60
34
|
const direction = ends.end >= ends.base ? 1 : -1;
|
|
61
|
-
return ends.end + direction * (
|
|
35
|
+
return ends.end + direction * (sizeOf(s.valueStyle) * 0.4 + extent / 2);
|
|
36
|
+
}
|
|
37
|
+
/** Whether bar `i` has room for a figure `extent` long inside it. */
|
|
38
|
+
function fitsInside(s, index, extent, across) {
|
|
39
|
+
const bar = s.bars[index];
|
|
40
|
+
const ends = endsAt(s, index);
|
|
41
|
+
if (!bar || !ends)
|
|
42
|
+
return false;
|
|
43
|
+
const room = sizeOf(s.valueStyle) * INSIDE_ROOM;
|
|
44
|
+
return Math.abs(ends.end - ends.base) >= extent + room && bar.thickness >= across;
|
|
62
45
|
}
|
|
63
46
|
/**
|
|
64
|
-
* One series of a {@link BarChart}: its
|
|
65
|
-
*
|
|
47
|
+
* One series of a {@link BarChart}: its bar in every category plus the printed
|
|
48
|
+
* figures. A node per series, like `ChartLine`, so `showSeries` and
|
|
66
49
|
* `spotlightSeries` mean the same on both: enable the node or tween its opacity.
|
|
67
50
|
* Positions come from the parent, which owns the slot arithmetic.
|
|
68
|
-
*
|
|
69
|
-
* The figures are {@link Text} children, so they take the theme's type style,
|
|
70
|
-
* are measured by layout, and hide with the series.
|
|
71
51
|
*/
|
|
72
52
|
export const ChartBars = component2d({
|
|
73
53
|
key: "chartBars",
|
|
74
54
|
state: {
|
|
75
55
|
bars: fresh(() => []),
|
|
76
|
-
/** Pixel
|
|
56
|
+
/** Pixel position of value 0 along the value axis, where every bar grows from. */
|
|
77
57
|
baseline: 0,
|
|
58
|
+
/** Bars grow along x from a category column rather than along y from a row. */
|
|
59
|
+
horizontal: false,
|
|
78
60
|
fill: Types.custom([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
79
|
-
|
|
80
|
-
barShadow: Types.custom(DEFAULT_BAR_CHART_THEME.bar.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),
|
|
81
|
-
cornerRadius: DEFAULT_BAR_CHART_THEME.bar.cornerRadius,
|
|
61
|
+
cornerRadius: 8,
|
|
82
62
|
/**
|
|
83
|
-
* Rise of every
|
|
84
|
-
*
|
|
85
|
-
* mid-animation and a negative
|
|
63
|
+
* Rise of every bar in `[0, 1]` about the baseline, not each bar's own base:
|
|
64
|
+
* every segment travels by the same factor, so a stack stays flush
|
|
65
|
+
* mid-animation and a negative bar grows the other way.
|
|
86
66
|
*/
|
|
87
67
|
growth: 1,
|
|
88
|
-
/** Per-
|
|
68
|
+
/** Per-bar opacity by row index, how the category spotlight reaches one bar. Missing reads as 1. */
|
|
89
69
|
dims: fresh(() => []),
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
70
|
+
/** Read as built: whether each bar prints its figure, and where. */
|
|
71
|
+
values: Types.custom("none"),
|
|
72
|
+
valueStyle: Types.custom({ fontSize: 24, fill: "white" }),
|
|
73
|
+
/** The wholesale switch behind `showSeries`/`hideSeries`; off skips the bars and their figures. */
|
|
93
74
|
enabled: true,
|
|
94
75
|
},
|
|
95
76
|
// A node made of its figures would otherwise hug them.
|
|
96
77
|
defaults: { width: "fill", height: "fill" },
|
|
97
|
-
// One
|
|
98
|
-
// and bound to the live geometry so it travels with its
|
|
78
|
+
// One figure per bar, built once (the row count is fixed at construction)
|
|
79
|
+
// and bound to the live geometry so it travels with its bar.
|
|
99
80
|
children: (state) => {
|
|
100
|
-
if (
|
|
81
|
+
if (state.values === "none")
|
|
101
82
|
return [];
|
|
102
|
-
return state.bars.map((_, i) =>
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
83
|
+
return asParts(state.bars.map((_, i) => {
|
|
84
|
+
const caption = new Text({
|
|
85
|
+
...boundText(() => ({ ...state.valueStyle, ...(state.values === "inside" && { fill: inkOn(state.fill) }) })),
|
|
86
|
+
text: () => state.bars[i]?.caption ?? "",
|
|
87
|
+
x: () => (state.horizontal ? captionAlong(state, i, caption.layoutBounds.width) : (state.bars[i]?.center ?? 0)),
|
|
88
|
+
y: () => (state.horizontal ? (state.bars[i]?.center ?? 0) : captionAlong(state, i, caption.layoutBounds.height)),
|
|
89
|
+
opacity: () => {
|
|
90
|
+
const box = caption.layoutBounds;
|
|
91
|
+
const along = state.horizontal ? box.width : box.height;
|
|
92
|
+
const across = state.horizontal ? box.height : box.width;
|
|
93
|
+
if (state.values === "inside" && !fitsInside(state, i, along, across))
|
|
94
|
+
return 0;
|
|
95
|
+
// A figure past the end rises in with its bar's last stretch.
|
|
96
|
+
return dimAt(state, i) * clamp((state.growth - 0.6) / 0.4, 0, 1);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
return caption;
|
|
108
100
|
}));
|
|
109
101
|
},
|
|
110
|
-
|
|
111
|
-
load: (loader, state) => loadPaints(loader, { fills: [state.fill], shadows: [state.barShadow], strokes: [state.barStroke] }),
|
|
102
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.fill] }),
|
|
112
103
|
commands: {
|
|
113
|
-
/** Raise every
|
|
104
|
+
/** Raise every bar from the baseline (`growth` 0 to 1). The resting state is the finished one; the chart arms it back to 0. */
|
|
114
105
|
enter: (timeline) => {
|
|
115
106
|
const ease = frameEase(timeline);
|
|
116
107
|
return timeline.animate((t) => ({ growth: ease(t) }));
|
|
@@ -121,33 +112,33 @@ export const ChartBars = component2d({
|
|
|
121
112
|
for (let i = 0; i < state.bars.length; i++) {
|
|
122
113
|
const bar = state.bars[i];
|
|
123
114
|
const ends = endsAt(state, i);
|
|
124
|
-
if (!ends || bar.
|
|
115
|
+
if (!ends || bar.thickness <= 0)
|
|
125
116
|
continue;
|
|
126
|
-
|
|
127
|
-
|
|
117
|
+
// A segment inside a stack reaches a pixel under the next, so no seam of background shows between them.
|
|
118
|
+
const seam = bar.round ? 0 : 1;
|
|
119
|
+
const length = Math.abs(ends.end - ends.base) + seam;
|
|
120
|
+
if (length <= seam)
|
|
128
121
|
continue;
|
|
129
|
-
const radius = bar.round ? Math.min(state.cornerRadius,
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
cornerRadius: radius > 0
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
: { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
|
|
142
|
-
: 0,
|
|
143
|
-
});
|
|
144
|
-
rect.shadow(state.barShadow);
|
|
145
|
-
rect.fill(state.fill);
|
|
146
|
-
rect.stroke(state.barStroke);
|
|
122
|
+
const radius = bar.round ? Math.max(0, Math.min(state.cornerRadius, length, bar.thickness / 2)) : 0;
|
|
123
|
+
const forward = ends.end >= ends.base;
|
|
124
|
+
const mid = (ends.base + ends.end) / 2 + (forward ? seam : -seam) / 2;
|
|
125
|
+
// Corner keys are visual (y-up): only the far end rounds.
|
|
126
|
+
const far = state.horizontal
|
|
127
|
+
? (forward ? { topRight: radius, bottomRight: radius, topLeft: 0, bottomLeft: 0 } : { topLeft: radius, bottomLeft: radius, topRight: 0, bottomRight: 0 })
|
|
128
|
+
: (forward ? { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 } : { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 });
|
|
129
|
+
// One `Graphics2D` per bar: opacity belongs to the command list, and the spotlight dims single bars.
|
|
130
|
+
const g = new Graphics2D().rect(state.horizontal
|
|
131
|
+
? { x: mid, y: bar.center, width: length, height: bar.thickness, cornerRadius: radius > 0 ? far : 0 }
|
|
132
|
+
: { x: bar.center, y: mid, width: bar.thickness, height: length, cornerRadius: radius > 0 ? far : 0 });
|
|
133
|
+
g.fill(state.fill);
|
|
147
134
|
ctx.draw(g.opacity(dimAt(state, i)));
|
|
148
135
|
}
|
|
149
136
|
}
|
|
150
137
|
ctx.children();
|
|
151
138
|
},
|
|
152
139
|
});
|
|
140
|
+
/** A figure as the bar prints it when the chart gives no caption. */
|
|
141
|
+
export function barCaption(value) {
|
|
142
|
+
return value === 0 ? "" : formatMarker(value);
|
|
143
|
+
}
|
|
153
144
|
//# sourceMappingURL=chart-bars.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chart-bars.js","sourceRoot":"","sources":["../../src/bar-chart/chart-bars.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,IAAI,EACJ,KAAK,EACL,KAAK,EACL,WAAW,EACX,OAAO,EACP,SAAS,EACT,SAAS,EACT,OA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wBACR,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI;4BACrB,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE;4BACtE,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE;wBACxE,CAAC,CAAC,CAAC;iBACR,CAAC,CAAA;gBACF,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;gBAC5B,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBACrB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;gBAC5B,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;YACtC,CAAC;QACH,CAAC;QACD,GAAG,CAAC,QAAQ,EAAE,CAAA;IAChB,CAAC;CACF,CAAC,CAAA","sourcesContent":["import {\n Graphics2D,\n Text,\n Types,\n clamp,\n component2d,\n fillOps,\n shadowOps,\n strokeOps,\n textOps,\n type Fill,\n type FillResolved,\n type NodeOf,\n // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n type Shadow,\n type ShadowResolved,\n type Stroke,\n type StrokeResolved,\n type TextStyle,\n} from \"@motionscript/sdk\"\n\nimport { fresh } from \"../kit/entries\"\nimport { frameEase } from \"../kit/motion\"\nimport { loadPaints } from \"../kit/paints\"\nimport { formatMarker } from \"../kit/shared\"\nimport { DEFAULT_BAR_CHART_THEME } from \"./shared\"\n\n/**\n * One drawn column in the plot's centred pixel space (origin at the centre,\n * y-up), mapped by the parent {@link BarChart}. `from`/`to` are the two ends\n * rather than a base and a height, like {@link BarSpan}: one shape fits grouped,\n * stacked and negative.\n */\nexport interface BarGeometry {\n /** Centre x. */\n x: number\n width: number\n /** Pixel y of the base: the baseline when grouped, the running stack top when stacked. */\n from: number\n /** Pixel y of the far end at full growth. */\n to: number\n /** Whether the far end takes the corner radius (the stack's cap). */\n round: boolean\n /** The value plotted, for the printed figure. */\n value: number\n /** The printed figure, written as the value axis writes its ticks; absent, the bare marker reading of `value`. */\n caption?: string\n}\n\n/** Gap between a grouped column's end and the figure printed over it, in px. */\nconst VALUE_GAP = 10\n/** A segment shorter than this many multiples of the type size prints no figure. */\nconst VALUE_FIT = 1.6\n/** Fallback type size when the theme's value style leaves it unset or autofit. */\nconst VALUE_SIZE = 24\n\ninterface Columns {\n readonly bars: readonly BarGeometry[]\n readonly baseline: number\n readonly growth: number\n readonly dims: readonly number[]\n readonly enabled: boolean\n readonly valueStyle: TextStyle\n}\n\n/**\n * Column `i`'s dimming: the category spotlight, then the series switch. Captions\n * read this because they are child nodes, which a `render` guard would leave on\n * screen over a series that is not there.\n */\nfunction dimAt(s: Columns, index: number): number {\n if (!s.enabled) return 0\n return clamp(s.dims[index] ?? 1, 0, 1)\n}\n\nfunction endsAt(s: Columns, index: number): { base: number; end: number } | null {\n const bar = s.bars[index]\n if (!bar) return null\n const g = clamp(s.growth, 0, 1)\n return { base: s.baseline + (bar.from - s.baseline) * g, end: s.baseline + (bar.to - s.baseline) * g }\n}\n\nfunction valueSize(s: Columns): number {\n const size = s.valueStyle.fontSize\n return typeof size === \"number\" ? size : VALUE_SIZE\n}\n\nfunction insideAt(s: Columns, index: number): boolean {\n const bar = s.bars[index]\n return bar !== undefined && Math.abs(bar.from - s.baseline) > 0.5\n}\n\n/** Column `i`'s figure, or `\"\"` where there is no room. Blanking rather than dropping the node keeps captions index-aligned, so a rescale into more room prints again. */\nfunction captionAt(s: Columns, index: number): string {\n const bar = s.bars[index]\n if (!bar || bar.value === 0) return \"\"\n // An interior segment prints inside itself and must hold a line of type; a grouped column prints past its end.\n if (insideAt(s, index)) {\n const ends = endsAt(s, index)\n if (!ends) return \"\"\n if (Math.abs(ends.end - ends.base) < valueSize(s) * VALUE_FIT) return \"\"\n }\n return bar.caption ?? formatMarker(bar.value)\n}\n\nfunction captionY(s: Columns, index: number): number {\n const ends = endsAt(s, index)\n if (!ends) return 0\n if (insideAt(s, index)) return (ends.base + ends.end) / 2\n // Text is centred on `y`: clear the end by the gap plus half a line, on the side the column reached.\n const direction = ends.end >= ends.base ? 1 : -1\n return ends.end + direction * (VALUE_GAP + valueSize(s) / 2)\n}\n\n/**\n * One series of a {@link BarChart}: its column in every category plus the\n * printed figures. A node per series, like `ChartLine`, so `showSeries` and\n * `spotlightSeries` mean the same on both: enable the node or tween its opacity.\n * Positions come from the parent, which owns the slot arithmetic.\n *\n * The figures are {@link Text} children, so they take the theme's type style,\n * are measured by layout, and hide with the series.\n */\nexport const ChartBars = component2d({\n key: \"chartBars\",\n state: {\n bars: fresh<BarGeometry[]>(() => []),\n /** Pixel y of value 0, where every column grows from. */\n baseline: 0,\n fill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),\n barStroke: Types.custom<Stroke, StrokeResolved[]>(DEFAULT_BAR_CHART_THEME.bar.stroke, { resolve: strokeOps.resolve, lerp: strokeOps.lerp }),\n barShadow: Types.custom<Shadow, ShadowResolved[]>(DEFAULT_BAR_CHART_THEME.bar.shadow, { resolve: shadowOps.resolve, lerp: shadowOps.lerp }),\n cornerRadius: DEFAULT_BAR_CHART_THEME.bar.cornerRadius,\n /**\n * Rise of every column in `[0, 1]` about the baseline, not each column's own\n * base: every segment travels by the same factor, so a stack stays flush\n * mid-animation and a negative column grows down.\n */\n growth: 1,\n /** Per-column opacity by row index, how the category spotlight reaches one column. Missing reads as 1. */\n dims: fresh<number[]>(() => []),\n showValues: false,\n valueStyle: Types.custom<TextStyle>(DEFAULT_BAR_CHART_THEME.valueStyle),\n /** The wholesale switch behind `showSeries`/`hideSeries`; off skips the columns and their figures. */\n enabled: true,\n },\n // A node made of its figures would otherwise hug them.\n defaults: { width: \"fill\", height: \"fill\" },\n // One caption per column, built once (the row count is fixed at construction)\n // and bound to the live geometry so it travels with its column.\n children: (state) => {\n if (!state.showValues) return []\n return state.bars.map((_, i) =>\n Text({\n ...textOps.styleProps(state.valueStyle),\n text: () => captionAt(state, i),\n x: () => state.bars[i]?.x ?? 0,\n y: () => captionY(state, i),\n opacity: () => dimAt(state, i),\n })\n )\n },\n // The stroke and shadow sit past the conventional slots, so they are named here.\n load: (loader, state) =>\n loadPaints(loader, { fills: [state.fill], shadows: [state.barShadow], strokes: [state.barStroke] }),\n commands: {\n /** Raise every column from the baseline (`growth` 0 to 1). The resting state is the finished one; the chart arms it back to 0. */\n enter: (timeline) => {\n const ease = frameEase(timeline)\n return timeline.animate((t) => ({ growth: ease(t) }))\n },\n },\n render: (ctx, state) => {\n if (state.enabled) {\n for (let i = 0; i < state.bars.length; i++) {\n const bar = state.bars[i]\n const ends = endsAt(state, i)\n if (!ends || bar.width <= 0) continue\n const height = Math.abs(ends.end - ends.base)\n if (height <= 0) continue\n\n const radius = bar.round ? Math.min(state.cornerRadius, height) : 0\n // One `Graphics2D` per column: opacity belongs to the command list, and the spotlight dims single columns.\n const g = new Graphics2D()\n const rect = g.rect({\n x: bar.x,\n y: (ends.base + ends.end) / 2,\n width: bar.width,\n height,\n // Corner keys are visual (y-up): a column reaching up rounds `top*`, one reaching down `bottom*`. Only the far end rounds.\n cornerRadius:\n radius > 0\n ? ends.end >= ends.base\n ? { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }\n : { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }\n : 0,\n })\n rect.shadow(state.barShadow)\n rect.fill(state.fill)\n rect.stroke(state.barStroke)\n ctx.draw(g.opacity(dimAt(state, i)))\n }\n }\n ctx.children()\n },\n})\n\nexport type ChartBars = NodeOf<typeof ChartBars>\n"]}
|
|
1
|
+
{"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,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAA;gBAC3J,qGAAqG;gBACrG,MAAM,CAAC,GAAG,IAAI,UAAU,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU;oBAC9C,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,SAAS,EAAE,YAAY,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE;oBACrG,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;gBACxG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBAClB,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;YACtC,CAAC;QACH,CAAC;QACD,GAAG,CAAC,QAAQ,EAAE,CAAA;IAChB,CAAC;CACF,CAAC,CAAA;AAIF,qEAAqE;AACrE,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;AAC/C,CAAC","sourcesContent":["import {\n Graphics2D,\n Text,\n Types,\n clamp,\n component2d,\n fillOps,\n type Fill,\n type FillResolved,\n type NodeOf,\n // Named so this component's emitted type reaches `Node2D` and its props through the sdk, not core.\n type Node2D,\n type Node2DProps as _Node2DProps,\n type TextStyle,\n} from \"@motionscript/sdk\"\n\nimport { boundText } from \"../kit/cartesian\"\nimport { asParts, fresh } from \"../kit/entries\"\nimport { frameEase } from \"../kit/motion\"\nimport { loadPaints } from \"../kit/paints\"\nimport { formatMarker } from \"../kit/shared\"\nimport { inkOn } from \"./shared\"\n\n/**\n * One drawn bar in the plot's centred pixel space (origin at the centre, y-up),\n * mapped by the parent {@link BarChart}: `center` and `thickness` across the\n * category slot, `from` and `to` along the value axis (y for a column, x for a\n * horizontal bar). Two ends rather than a base and a length, like `BarSpan`:\n * one shape fits grouped, stacked and negative.\n */\nexport interface BarGeometry {\n center: number\n thickness: number\n /** The base: the baseline when grouped, the running stack end when stacked. */\n from: number\n /** The far end at full growth. */\n to: number\n /** Whether the far end takes the corner radius (the stack's last segment on its side). */\n round: boolean\n /** The figure printed by the bar; empty for none. */\n caption: string\n}\n\n/** A figure inside a bar needs this much room around it, in multiples of the type size. */\nconst INSIDE_ROOM = 0.6\n\ninterface Bars {\n readonly bars: readonly BarGeometry[]\n readonly baseline: number\n readonly horizontal: boolean\n readonly growth: number\n readonly dims: readonly number[]\n readonly enabled: boolean\n readonly values: \"none\" | \"end\" | \"inside\"\n readonly valueStyle: TextStyle\n}\n\n/** Bar `i`'s dimming: the category spotlight, then the series switch. */\nfunction dimAt(s: Bars, index: number): number {\n if (!s.enabled) return 0\n return clamp(s.dims[index] ?? 1, 0, 1)\n}\n\n/** Bar `i`'s two ends as grown so far, along the value axis. */\nfunction endsAt(s: Bars, index: number): { base: number; end: number } | null {\n const bar = s.bars[index]\n if (!bar) return null\n const g = clamp(s.growth, 0, 1)\n return { base: s.baseline + (bar.from - s.baseline) * g, end: s.baseline + (bar.to - s.baseline) * g }\n}\n\nfunction sizeOf(style: TextStyle): number {\n return typeof style.fontSize === \"number\" ? style.fontSize : 24\n}\n\n/** Where bar `i`'s figure sits along the value axis: past its end, or in its middle. */\nfunction captionAlong(s: Bars, index: number, extent: number): number {\n const ends = endsAt(s, index)\n if (!ends) return 0\n if (s.values === \"inside\") return (ends.base + ends.end) / 2\n const direction = ends.end >= ends.base ? 1 : -1\n return ends.end + direction * (sizeOf(s.valueStyle) * 0.4 + extent / 2)\n}\n\n/** Whether bar `i` has room for a figure `extent` long inside it. */\nfunction fitsInside(s: Bars, index: number, extent: number, across: number): boolean {\n const bar = s.bars[index]\n const ends = endsAt(s, index)\n if (!bar || !ends) return false\n const room = sizeOf(s.valueStyle) * INSIDE_ROOM\n return Math.abs(ends.end - ends.base) >= extent + room && bar.thickness >= across\n}\n\n/**\n * One series of a {@link BarChart}: its bar in every category plus the printed\n * figures. A node per series, like `ChartLine`, so `showSeries` and\n * `spotlightSeries` mean the same on both: enable the node or tween its opacity.\n * Positions come from the parent, which owns the slot arithmetic.\n */\nexport const ChartBars = component2d({\n key: \"chartBars\",\n state: {\n bars: fresh<BarGeometry[]>(() => []),\n /** Pixel position of value 0 along the value axis, where every bar grows from. */\n baseline: 0,\n /** Bars grow along x from a category column rather than along y from a row. */\n horizontal: false,\n fill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),\n cornerRadius: 8,\n /**\n * Rise of every bar in `[0, 1]` about the baseline, not each bar's own base:\n * every segment travels by the same factor, so a stack stays flush\n * mid-animation and a negative bar grows the other way.\n */\n growth: 1,\n /** Per-bar opacity by row index, how the category spotlight reaches one bar. Missing reads as 1. */\n dims: fresh<number[]>(() => []),\n /** Read as built: whether each bar prints its figure, and where. */\n values: Types.custom<\"none\" | \"end\" | \"inside\">(\"none\"),\n valueStyle: Types.custom<TextStyle>({ fontSize: 24, fill: \"white\" }),\n /** The wholesale switch behind `showSeries`/`hideSeries`; off skips the bars and their figures. */\n enabled: true,\n },\n // A node made of its figures would otherwise hug them.\n defaults: { width: \"fill\", height: \"fill\" },\n // One figure per bar, built once (the row count is fixed at construction)\n // and bound to the live geometry so it travels with its bar.\n children: (state) => {\n if (state.values === \"none\") return []\n return asParts(state.bars.map((_, i) => {\n const caption: Node2D = new Text({\n ...boundText(() => ({ ...state.valueStyle, ...(state.values === \"inside\" && { fill: inkOn(state.fill) }) })),\n text: () => state.bars[i]?.caption ?? \"\",\n x: () => (state.horizontal ? captionAlong(state, i, caption.layoutBounds.width) : (state.bars[i]?.center ?? 0)),\n y: () => (state.horizontal ? (state.bars[i]?.center ?? 0) : captionAlong(state, i, caption.layoutBounds.height)),\n opacity: () => {\n const box = caption.layoutBounds\n const along = state.horizontal ? box.width : box.height\n const across = state.horizontal ? box.height : box.width\n if (state.values === \"inside\" && !fitsInside(state, i, along, across)) return 0\n // A figure past the end rises in with its bar's last stretch.\n return dimAt(state, i) * clamp((state.growth - 0.6) / 0.4, 0, 1)\n },\n }) as unknown as Node2D\n return caption\n }))\n },\n load: (loader, state) => loadPaints(loader, { fills: [state.fill] }),\n commands: {\n /** Raise every bar from the baseline (`growth` 0 to 1). The resting state is the finished one; the chart arms it back to 0. */\n enter: (timeline) => {\n const ease = frameEase(timeline)\n return timeline.animate((t) => ({ growth: ease(t) }))\n },\n },\n render: (ctx, state) => {\n if (state.enabled) {\n for (let i = 0; i < state.bars.length; i++) {\n const bar = state.bars[i]\n const ends = endsAt(state, i)\n if (!ends || bar.thickness <= 0) continue\n // A segment inside a stack reaches a pixel under the next, so no seam of background shows between them.\n const seam = bar.round ? 0 : 1\n const length = Math.abs(ends.end - ends.base) + seam\n if (length <= seam) continue\n const radius = bar.round ? Math.max(0, Math.min(state.cornerRadius, length, bar.thickness / 2)) : 0\n const forward = ends.end >= ends.base\n const mid = (ends.base + ends.end) / 2 + (forward ? seam : -seam) / 2\n // Corner keys are visual (y-up): only the far end rounds.\n const far = state.horizontal\n ? (forward ? { topRight: radius, bottomRight: radius, topLeft: 0, bottomLeft: 0 } : { topLeft: radius, bottomLeft: radius, topRight: 0, bottomRight: 0 })\n : (forward ? { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 } : { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 })\n // One `Graphics2D` per bar: opacity belongs to the command list, and the spotlight dims single bars.\n const g = new Graphics2D().rect(state.horizontal\n ? { x: mid, y: bar.center, width: length, height: bar.thickness, cornerRadius: radius > 0 ? far : 0 }\n : { x: bar.center, y: mid, width: bar.thickness, height: length, cornerRadius: radius > 0 ? far : 0 })\n g.fill(state.fill)\n ctx.draw(g.opacity(dimAt(state, i)))\n }\n }\n ctx.children()\n },\n})\n\nexport type ChartBars = NodeOf<typeof ChartBars>\n\n/** A figure as the bar prints it when the chart gives no caption. */\nexport function barCaption(value: number): string {\n return value === 0 ? \"\" : formatMarker(value)\n}\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { BarChart, barChartDomain } from "./bar-chart.js";
|
|
2
|
-
export type { BarChartDomain, BarChartValues } from "./bar-chart.js";
|
|
2
|
+
export type { BarChartDomain, BarChartStyle, BarChartValues } from "./bar-chart.js";
|
|
3
3
|
export { ChartBars } from "./chart-bars.js";
|
|
4
4
|
export type { BarGeometry } from "./chart-bars.js";
|
|
5
|
-
export { CategoryAxis } from "./category-axis.js";
|
|
6
|
-
export {
|
|
7
|
-
export type {
|
|
5
|
+
export { CategoryAxis, slotCentre } from "./category-axis.js";
|
|
6
|
+
export { BAR_LABELS, BAR_MARKS, barCategories, barSpans, barValueExtent, inkOn, resolveBarScale, } from "./shared.js";
|
|
7
|
+
export type { BarOrientation, BarSeries, BarSpan, BarStack, } from "./shared.js";
|
|
8
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bar-chart/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AACtD,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/bar-chart/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AACtD,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAChF,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,EACL,UAAU,EACV,SAAS,EACT,aAAa,EACb,QAAQ,EACR,cAAc,EACd,KAAK,EACL,eAAe,GAChB,MAAM,UAAU,CAAA;AACjB,YAAY,EACV,cAAc,EACd,SAAS,EACT,OAAO,EACP,QAAQ,GACT,MAAM,UAAU,CAAA"}
|
package/dist/bar-chart/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { BarChart, barChartDomain } from "./bar-chart.js";
|
|
2
2
|
export { ChartBars } from "./chart-bars.js";
|
|
3
|
-
export { CategoryAxis } from "./category-axis.js";
|
|
4
|
-
export {
|
|
3
|
+
export { CategoryAxis, slotCentre } from "./category-axis.js";
|
|
4
|
+
export { BAR_LABELS, BAR_MARKS, barCategories, barSpans, barValueExtent, inkOn, resolveBarScale, } from "./shared.js";
|
|
5
5
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bar-chart/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEtD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bar-chart/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEtD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC1D,OAAO,EACL,UAAU,EACV,SAAS,EACT,aAAa,EACb,QAAQ,EACR,cAAc,EACd,KAAK,EACL,eAAe,GAChB,MAAM,UAAU,CAAA","sourcesContent":["export { BarChart, barChartDomain } from \"./bar-chart\"\nexport type { BarChartDomain, BarChartStyle, BarChartValues } from \"./bar-chart\"\nexport { ChartBars } from \"./chart-bars\"\nexport type { BarGeometry } from \"./chart-bars\"\nexport { CategoryAxis, slotCentre } from \"./category-axis\"\nexport {\n BAR_LABELS,\n BAR_MARKS,\n barCategories,\n barSpans,\n barValueExtent,\n inkOn,\n resolveBarScale,\n} from \"./shared\"\nexport type {\n BarOrientation,\n BarSeries,\n BarSpan,\n BarStack,\n} from \"./shared\"\n"]}
|
|
@@ -1,83 +1,54 @@
|
|
|
1
|
-
import { type DataRecord, type Fill
|
|
2
|
-
import { type
|
|
1
|
+
import { type DataRecord, type Fill } from "@motionscript/sdk";
|
|
2
|
+
import { type ChartSeries, type Range } from "../kit/shared.js";
|
|
3
3
|
/** One bar per row, beside its siblings in the category slot or stacked on them: {@link ChartSeries} under the name for what it draws here. */
|
|
4
4
|
export type BarSeries = ChartSeries;
|
|
5
|
+
/** How the series share a category: side by side, stacked, or stacked as shares of 100. */
|
|
6
|
+
export type BarStack = "none" | "stacked" | "percent";
|
|
7
|
+
/** Which way the bars grow: up from a category row, or right from a category column. */
|
|
8
|
+
export type BarOrientation = "vertical" | "horizontal";
|
|
5
9
|
/**
|
|
6
|
-
* One bar's span in data units. A span rather than a height because
|
|
7
|
-
*
|
|
10
|
+
* One bar's span in data units. A span rather than a height because every mode
|
|
11
|
+
* fits it, and a negative value just runs the other way with no special case.
|
|
8
12
|
*/
|
|
9
13
|
export interface BarSpan {
|
|
10
|
-
/** Where the base sits: 0
|
|
14
|
+
/** Where the base sits: 0 apart, the running total below (or above, for a negative) when stacked. */
|
|
11
15
|
from: number;
|
|
12
16
|
to: number;
|
|
13
|
-
/** `to - from
|
|
17
|
+
/** `to - from`. */
|
|
14
18
|
value: number;
|
|
19
|
+
/** Whether it ends its stack on its side of zero, so its far end is rounded. */
|
|
20
|
+
outer: boolean;
|
|
15
21
|
}
|
|
16
22
|
/**
|
|
17
|
-
* One row's spans, in series order. Stacking
|
|
18
|
-
*
|
|
19
|
-
*
|
|
23
|
+
* One row's spans, in series order. Stacking keeps a running sum on each side
|
|
24
|
+
* of zero, so a negative value stacks downward from the baseline rather than
|
|
25
|
+
* eating into the positives. In `percent` each value is its share of the row's
|
|
26
|
+
* total magnitude. A zero value still yields a degenerate span, keeping the list
|
|
27
|
+
* index-aligned with the series. `weights` scale each series' contribution.
|
|
20
28
|
*/
|
|
21
|
-
export declare function barSpans(row: DataRecord, series: BarSeries[],
|
|
22
|
-
/**
|
|
23
|
-
* The series ending a row's stack, which gets the rounded cap; `-1` when the row
|
|
24
|
-
* is all zero. It is the last non-zero series, so a zero one does not take the
|
|
25
|
-
* cap and leave the segment below square.
|
|
26
|
-
*/
|
|
27
|
-
export declare function stackCapIndex(row: DataRecord, series: BarSeries[]): number;
|
|
29
|
+
export declare function barSpans(row: DataRecord, series: readonly BarSeries[], stack: BarStack, weights?: readonly number[]): BarSpan[];
|
|
28
30
|
export declare function barCategories(data: DataRecord[], field: string): string[];
|
|
29
31
|
/**
|
|
30
32
|
* The `[min, max]` of everything drawn, zero always included: a scale starting
|
|
31
|
-
* at 40 draws a
|
|
33
|
+
* at 40 draws a bar three times its neighbour for a 10% difference. A cropped
|
|
32
34
|
* axis takes an explicit range.
|
|
33
35
|
*/
|
|
34
|
-
export declare function barValueExtent(data: DataRecord[], series: BarSeries[],
|
|
35
|
-
/** The explicit `range`, else the data's extent nudged to tidy bounds; `divisions` (one fewer than the marker count) makes the drawn ticks land on them. */
|
|
36
|
-
export declare function resolveBarScale(data: DataRecord[], series: BarSeries[],
|
|
37
|
-
/**
|
|
38
|
-
export
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
seriesFills: Fill[];
|
|
52
|
-
bar: BarStyleConfig;
|
|
53
|
-
/** The category axis. */
|
|
54
|
-
xAxis: AxisConfig;
|
|
55
|
-
/** The value axis. */
|
|
56
|
-
yAxis: AxisConfig;
|
|
57
|
-
plotArea: PlotAreaConfig;
|
|
58
|
-
legend: LegendConfig;
|
|
59
|
-
/** The figure printed on each column. */
|
|
60
|
-
valueStyle: TextStyle;
|
|
61
|
-
/** How far everything outside a spotlit series or category fades. */
|
|
62
|
-
spotlight: {
|
|
63
|
-
dimOpacity: number;
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
/** The author's partial theme, deep-merged onto {@link DEFAULT_BAR_CHART_THEME}. */
|
|
67
|
-
export type BarChartTheme = {
|
|
68
|
-
seriesFills?: Fill[];
|
|
69
|
-
bar?: Partial<BarStyleConfig>;
|
|
70
|
-
xAxis?: Partial<AxisConfig>;
|
|
71
|
-
yAxis?: Partial<AxisConfig>;
|
|
72
|
-
plotArea?: Partial<PlotAreaConfig>;
|
|
73
|
-
legend?: LegendTheme;
|
|
74
|
-
valueStyle?: TextStyle;
|
|
75
|
-
spotlight?: Partial<BarChartConfig["spotlight"]>;
|
|
36
|
+
export declare function barValueExtent(data: DataRecord[], series: readonly BarSeries[], stack: BarStack, weights?: readonly number[]): Range;
|
|
37
|
+
/** The explicit `range`, else the data's extent nudged to tidy bounds (a percent stack reads to 100); `divisions` (one fewer than the marker count) makes the drawn ticks land on them. */
|
|
38
|
+
export declare function resolveBarScale(data: DataRecord[], series: readonly BarSeries[], stack: BarStack, range: Range | undefined, divisions: number, weights?: readonly number[]): Range;
|
|
39
|
+
/** Black or white, whichever reads on `fill`: a figure printed inside a bar. */
|
|
40
|
+
export declare function inkOn(fill: Fill): string;
|
|
41
|
+
/** A bar chart's own look: how round and how wide the bars are. */
|
|
42
|
+
export declare const BAR_MARKS: {
|
|
43
|
+
/** Radius of each bar's far corners; a stacked segment inside the stack stays square. */
|
|
44
|
+
barRadius: import("@motionscript/sdk").Type<number, number, false>;
|
|
45
|
+
/** How much of its category slot a bar group fills, the rest spacing the groups. */
|
|
46
|
+
barWidth: import("@motionscript/sdk").Type<number, number, false>;
|
|
47
|
+
/** Space between the bars of one group, side by side. */
|
|
48
|
+
barGap: import("@motionscript/sdk").Type<number, number, false>;
|
|
49
|
+
};
|
|
50
|
+
export declare const BAR_LABELS: {
|
|
51
|
+
/** Read as the chart is built: each bar's value past its end, inside it, or none. */
|
|
52
|
+
values: import("@motionscript/sdk").Type<"end" | "inside" | "none", "end" | "inside" | "none", false>;
|
|
76
53
|
};
|
|
77
|
-
export declare const DEFAULT_BAR_CHART_THEME: BarChartConfig;
|
|
78
|
-
/** The `theme` entry's resolve: merges a partial {@link BarChartTheme} onto `previous` (first resolution: the default). */
|
|
79
|
-
export declare function resolveBarChartTheme(theme: BarChartTheme | undefined, previous?: BarChartConfig): BarChartConfig;
|
|
80
|
-
export declare function lerpBarChartTheme(from: BarChartConfig, to: BarChartConfig, t: number): BarChartConfig;
|
|
81
|
-
/** Series `index`'s own `fill`, else the palette's entry. */
|
|
82
|
-
export declare function resolveBarFill(palette: Fill[], series: BarSeries, index: number): Fill;
|
|
83
54
|
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/bar-chart/shared.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/bar-chart/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,mBAAmB,CAAA;AAE9E,O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