@motionscript/charts 0.1.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 +38 -0
- package/README.md +98 -28
- package/dist/bar-chart/bar-chart.d.ts +242 -119
- package/dist/bar-chart/bar-chart.d.ts.map +1 -1
- package/dist/bar-chart/bar-chart.js +231 -260
- package/dist/bar-chart/bar-chart.js.map +1 -1
- package/dist/bar-chart/category-axis.d.ts +16 -38
- package/dist/bar-chart/category-axis.d.ts.map +1 -1
- package/dist/bar-chart/category-axis.js +59 -68
- package/dist/bar-chart/category-axis.js.map +1 -1
- package/dist/bar-chart/chart-bars.d.ts +60 -91
- package/dist/bar-chart/chart-bars.d.ts.map +1 -1
- package/dist/bar-chart/chart-bars.js +88 -125
- package/dist/bar-chart/chart-bars.js.map +1 -1
- package/dist/bar-chart/index.d.ts +5 -13
- package/dist/bar-chart/index.d.ts.map +1 -1
- package/dist/bar-chart/index.js +2 -9
- package/dist/bar-chart/index.js.map +1 -1
- package/dist/bar-chart/shared.d.ts +40 -132
- package/dist/bar-chart/shared.d.ts.map +1 -1
- package/dist/bar-chart/shared.js +70 -158
- 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 +8 -17
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +8 -17
- 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 -35
- package/dist/kit/cartesian.d.ts.map +1 -1
- package/dist/kit/cartesian.js +80 -42
- package/dist/kit/cartesian.js.map +1 -1
- package/dist/kit/chart-canvas.d.ts +37 -40
- package/dist/kit/chart-canvas.d.ts.map +1 -1
- package/dist/kit/chart-canvas.js +53 -47
- package/dist/kit/chart-canvas.js.map +1 -1
- package/dist/kit/chart-legend.d.ts +16 -59
- package/dist/kit/chart-legend.d.ts.map +1 -1
- package/dist/kit/chart-legend.js +38 -76
- package/dist/kit/chart-legend.js.map +1 -1
- package/dist/kit/entries.d.ts +2 -0
- package/dist/kit/entries.d.ts.map +1 -1
- package/dist/kit/entries.js +2 -0
- package/dist/kit/entries.js.map +1 -1
- package/dist/kit/horizontal-axis.d.ts +77 -0
- package/dist/kit/horizontal-axis.d.ts.map +1 -0
- package/dist/kit/horizontal-axis.js +99 -0
- package/dist/kit/horizontal-axis.js.map +1 -0
- package/dist/kit/motion.d.ts +16 -3
- package/dist/kit/motion.d.ts.map +1 -1
- package/dist/kit/motion.js +15 -1
- package/dist/kit/motion.js.map +1 -1
- package/dist/{pie-chart → kit}/pie-legend.d.ts +14 -29
- package/dist/kit/pie-legend.d.ts.map +1 -0
- package/dist/kit/pie-legend.js +93 -0
- 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 +60 -214
- package/dist/kit/shared.d.ts.map +1 -1
- package/dist/kit/shared.js +91 -291
- 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 +36 -45
- package/dist/kit/vertical-axis.d.ts.map +1 -1
- package/dist/kit/vertical-axis.js +69 -81
- package/dist/kit/vertical-axis.js.map +1 -1
- package/dist/line-chart/chart-line.d.ts +107 -40
- package/dist/line-chart/chart-line.d.ts.map +1 -1
- package/dist/line-chart/chart-line.js +172 -89
- package/dist/line-chart/chart-line.js.map +1 -1
- package/dist/line-chart/chart-region.d.ts +14 -27
- package/dist/line-chart/chart-region.d.ts.map +1 -1
- package/dist/line-chart/chart-region.js +13 -28
- package/dist/line-chart/chart-region.js.map +1 -1
- package/dist/line-chart/index.d.ts +5 -18
- package/dist/line-chart/index.d.ts.map +1 -1
- package/dist/line-chart/index.js +3 -16
- package/dist/line-chart/index.js.map +1 -1
- package/dist/line-chart/line-chart.d.ts +272 -119
- package/dist/line-chart/line-chart.d.ts.map +1 -1
- package/dist/line-chart/line-chart.js +309 -257
- package/dist/line-chart/line-chart.js.map +1 -1
- package/dist/line-chart/shared.d.ts +73 -130
- package/dist/line-chart/shared.d.ts.map +1 -1
- package/dist/line-chart/shared.js +195 -156
- package/dist/line-chart/shared.js.map +1 -1
- package/dist/line-chart/x-scale.d.ts +16 -55
- package/dist/line-chart/x-scale.d.ts.map +1 -1
- package/dist/line-chart/x-scale.js +44 -111
- package/dist/line-chart/x-scale.js.map +1 -1
- package/dist/nodes.d.ts +3 -13
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +9 -13
- package/dist/nodes.js.map +1 -1
- package/dist/pie-chart/index.d.ts +3 -15
- package/dist/pie-chart/index.d.ts.map +1 -1
- package/dist/pie-chart/index.js +1 -11
- package/dist/pie-chart/index.js.map +1 -1
- package/dist/pie-chart/pie-chart.d.ts +147 -85
- package/dist/pie-chart/pie-chart.d.ts.map +1 -1
- package/dist/pie-chart/pie-chart.js +65 -86
- package/dist/pie-chart/pie-chart.js.map +1 -1
- package/dist/pie-chart/pie-ring.d.ts +65 -196
- package/dist/pie-chart/pie-ring.d.ts.map +1 -1
- package/dist/pie-chart/pie-ring.js +154 -292
- package/dist/pie-chart/pie-ring.js.map +1 -1
- package/dist/pie-chart/shared.d.ts +78 -172
- package/dist/pie-chart/shared.d.ts.map +1 -1
- package/dist/pie-chart/shared.js +54 -177
- 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 +274 -315
- package/src/bar-chart/category-axis.ts +53 -88
- package/src/bar-chart/chart-bars.ts +108 -205
- package/src/bar-chart/index.ts +8 -19
- package/src/bar-chart/shared.ts +81 -309
- package/src/engine.ts +8 -17
- package/src/index.ts +2 -0
- package/src/kit/cartesian.ts +118 -55
- package/src/kit/chart-canvas.ts +50 -80
- package/src/kit/chart-legend.ts +36 -97
- package/src/kit/entries.ts +4 -1
- package/src/kit/horizontal-axis.ts +114 -0
- package/src/kit/motion.ts +18 -2
- package/src/{pie-chart → kit}/pie-legend.ts +25 -65
- package/src/kit/polar.ts +54 -0
- package/src/kit/shared.ts +104 -487
- package/src/kit/style.ts +257 -0
- package/src/kit/vertical-axis.ts +81 -115
- package/src/line-chart/chart-line.ts +185 -91
- package/src/line-chart/chart-region.ts +14 -31
- package/src/line-chart/index.ts +14 -22
- package/src/line-chart/line-chart.ts +377 -315
- package/src/line-chart/shared.ts +229 -304
- package/src/line-chart/x-scale.ts +44 -121
- package/src/nodes.ts +9 -13
- package/src/pie-chart/index.ts +3 -35
- package/src/pie-chart/pie-chart.ts +83 -134
- package/src/pie-chart/pie-ring.ts +162 -368
- package/src/pie-chart/shared.ts +92 -379
- 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-TJNHLQGV.js +0 -2
- package/dist/browser/chunks/chunk-TJNHLQGV.js.map +0 -7
- package/dist/kit/chart-border.d.ts +0 -27
- package/dist/kit/chart-border.d.ts.map +0 -1
- package/dist/kit/chart-border.js +0 -28
- package/dist/kit/chart-border.js.map +0 -1
- package/dist/line-chart/horizontal-axis.d.ts +0 -79
- package/dist/line-chart/horizontal-axis.d.ts.map +0 -1
- package/dist/line-chart/horizontal-axis.js +0 -129
- 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 +0 -117
- package/dist/pie-chart/pie-legend.js.map +0 -1
- package/src/kit/chart-border.ts +0 -50
- package/src/line-chart/horizontal-axis.ts +0 -151
package/dist/kit/chart-canvas.js
CHANGED
|
@@ -1,68 +1,74 @@
|
|
|
1
|
-
import { Graphics2D, Types, component2d, fillOps,
|
|
1
|
+
import { Graphics2D, Types, component2d, fillOps, strokeOps, } from "@motionscript/sdk";
|
|
2
2
|
import { fresh } from "./entries.js";
|
|
3
3
|
import { loadPaints } from "./paints.js";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const strokeEntry = () => Types.custom([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp });
|
|
5
|
+
/** Where along a scale a fraction lands in a run of `length` px centred on 0. */
|
|
6
|
+
function along(value, [min, max], length) {
|
|
7
|
+
return (max <= min ? 0 : (value - min) / (max - min) - 0.5) * length;
|
|
8
|
+
}
|
|
9
|
+
/** One rule per draw: stroking a union of open paths paints nothing, and a dashed rule keeps its own phase. */
|
|
10
|
+
export function rule(ctx, from, to, stroke, opacity = 1) {
|
|
11
|
+
ctx.draw(new Graphics2D().line({ points: [from, to] }).stroke(stroke).opacity(opacity));
|
|
12
|
+
}
|
|
6
13
|
/**
|
|
7
|
-
* The plot
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* The thick frame is a **separate** {@link ChartBorder} node so it can be drawn
|
|
12
|
-
* *over* the marks while this panel sits under them. Grid lines are placed from
|
|
13
|
-
* the axis marker positions (in data units) mapped into the panel through
|
|
14
|
-
* {@link ChartCanvasProps.xExtent}/`yExtent` — so each line lands under its
|
|
15
|
-
* marker whether or not the markers are evenly spaced.
|
|
14
|
+
* The plot backdrop: the panel fill, the grid and the axis rules, sized from its
|
|
15
|
+
* box. Grid lines are placed from data-unit marker positions mapped through the
|
|
16
|
+
* extents, so each lands under its marker however the markers are spaced; a line
|
|
17
|
+
* on an edge is drawn just inside it.
|
|
16
18
|
*/
|
|
17
19
|
export const ChartCanvas = component2d({
|
|
18
20
|
key: "chartCanvas",
|
|
19
21
|
state: {
|
|
20
|
-
fill: Types.custom(
|
|
21
|
-
|
|
22
|
-
xGridStroke: strokeEntry(
|
|
23
|
-
|
|
22
|
+
fill: Types.custom([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),
|
|
23
|
+
/** The vertical grid lines, one per `xMarkers` entry. */
|
|
24
|
+
xGridStroke: strokeEntry(),
|
|
25
|
+
/** The horizontal grid lines, one per `yMarkers` entry. */
|
|
26
|
+
yGridStroke: strokeEntry(),
|
|
24
27
|
xMarkers: fresh(() => []),
|
|
25
28
|
yMarkers: fresh(() => []),
|
|
29
|
+
/** `[min, max]` of each scale, mapping the markers into the panel. */
|
|
26
30
|
xExtent: fresh(() => [0, 1]),
|
|
27
31
|
yExtent: fresh(() => [0, 1]),
|
|
32
|
+
/** The baseline and the value-axis rule. */
|
|
33
|
+
axisStroke: strokeEntry(),
|
|
34
|
+
/** Where the horizontal axis rule sits on the y scale, or `null` for none. */
|
|
35
|
+
xAxisAt: Types.custom(null),
|
|
36
|
+
/** Where the vertical axis rule sits on the x scale, or `null` for none. */
|
|
37
|
+
yAxisAt: Types.custom(null),
|
|
28
38
|
},
|
|
29
|
-
|
|
30
|
-
load: (loader, state) => loadPaints(loader, { fills: [state.fill], shadows: [state.shadow], strokes: [state.xGridStroke, state.yGridStroke] }),
|
|
39
|
+
load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.xGridStroke, state.yGridStroke, state.axisStroke] }),
|
|
31
40
|
render: (ctx, state) => {
|
|
32
41
|
const { width: W, height: H } = ctx.size;
|
|
33
42
|
if (W <= 0 || H <= 0)
|
|
34
43
|
return;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (xStroke) {
|
|
46
|
-
const xGrid = new Graphics2D();
|
|
47
|
-
for (const v of state.xMarkers) {
|
|
48
|
-
const f = xFrac(v);
|
|
49
|
-
if (!interior(f))
|
|
50
|
-
continue;
|
|
51
|
-
xGrid.rect({ x: -W / 2 + f * W, y: 0, width: xStroke.weight, height: H });
|
|
44
|
+
if (state.fill.length > 0)
|
|
45
|
+
ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).fill(state.fill));
|
|
46
|
+
const inside = (at, length, weight) => Math.max(-length / 2 + weight / 2, Math.min(length / 2 - weight / 2, at));
|
|
47
|
+
const verticals = (values, stroke) => {
|
|
48
|
+
const weight = stroke[0]?.weight ?? 0;
|
|
49
|
+
if (weight <= 0)
|
|
50
|
+
return;
|
|
51
|
+
for (const v of values) {
|
|
52
|
+
const x = inside(along(v, state.xExtent, W), W, weight);
|
|
53
|
+
rule(ctx, { x, y: -H / 2 }, { x, y: H / 2 }, stroke);
|
|
52
54
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
for (const v of
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
continue;
|
|
62
|
-
yGrid.rect({ x: 0, y: -H / 2 + f * H, width: W, height: yStroke.weight });
|
|
55
|
+
};
|
|
56
|
+
const horizontals = (values, stroke) => {
|
|
57
|
+
const weight = stroke[0]?.weight ?? 0;
|
|
58
|
+
if (weight <= 0)
|
|
59
|
+
return;
|
|
60
|
+
for (const v of values) {
|
|
61
|
+
const y = inside(along(v, state.yExtent, H), H, weight);
|
|
62
|
+
rule(ctx, { x: -W / 2, y }, { x: W / 2, y }, stroke);
|
|
63
63
|
}
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
};
|
|
65
|
+
const within = ([min, max]) => (v) => v >= Math.min(min, max) - 1e-9 && v <= Math.max(min, max) + 1e-9;
|
|
66
|
+
verticals(state.xMarkers.filter(within(state.xExtent)), state.xGridStroke);
|
|
67
|
+
horizontals(state.yMarkers.filter(within(state.yExtent)), state.yGridStroke);
|
|
68
|
+
if (state.xAxisAt !== null)
|
|
69
|
+
horizontals([state.xAxisAt], state.axisStroke);
|
|
70
|
+
if (state.yAxisAt !== null)
|
|
71
|
+
verticals([state.yAxisAt], state.axisStroke);
|
|
66
72
|
},
|
|
67
73
|
});
|
|
68
74
|
//# sourceMappingURL=chart-canvas.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chart-canvas.js","sourceRoot":"","sources":["../../src/kit/chart-canvas.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,KAAK,EACL,WAAW,EACX,OAAO,EACP,SAAS,
|
|
1
|
+
{"version":3,"file":"chart-canvas.js","sourceRoot":"","sources":["../../src/kit/chart-canvas.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,KAAK,EACL,WAAW,EACX,OAAO,EACP,SAAS,GAWV,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAA;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAGrC,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,CAA2B,EAAE,EAAE,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC,CAAA;AAE1H,iFAAiF;AACjF,SAAS,KAAK,CAAC,KAAa,EAAE,CAAC,GAAG,EAAE,GAAG,CAAQ,EAAE,MAAc;IAC7D,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,MAAM,CAAA;AACtE,CAAC;AAED,+GAA+G;AAC/G,MAAM,UAAU,IAAI,CAAC,GAAkB,EAAE,IAAa,EAA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C,KAAK,CAAC,OAAO,CAAC,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,CAAA;QAC5E,IAAI,KAAK,CAAC,OAAO,KAAK,IAAI;YAAE,WAAW,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;QAC1E,IAAI,KAAK,CAAC,OAAO,KAAK,IAAI;YAAE,SAAS,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;IAC1E,CAAC;CACF,CAAC,CAAA","sourcesContent":["import {\n Graphics2D,\n Types,\n component2d,\n fillOps,\n strokeOps,\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 RenderContext,\n type Stroke,\n type StrokeResolved,\n type Vector2,\n} from \"@motionscript/sdk\"\n\nimport { fresh } from \"./entries\"\nimport { loadPaints } from \"./paints\"\nimport type { Range } from \"./shared\"\n\nconst strokeEntry = () => Types.custom<Stroke, StrokeResolved[]>([], { resolve: strokeOps.resolve, lerp: strokeOps.lerp })\n\n/** Where along a scale a fraction lands in a run of `length` px centred on 0. */\nfunction along(value: number, [min, max]: Range, length: number): number {\n return (max <= min ? 0 : (value - min) / (max - min) - 0.5) * length\n}\n\n/** One rule per draw: stroking a union of open paths paints nothing, and a dashed rule keeps its own phase. */\nexport function rule(ctx: RenderContext, from: Vector2, to: Vector2, stroke: StrokeResolved[], opacity = 1): void {\n ctx.draw(new Graphics2D().line({ points: [from, to] }).stroke(stroke).opacity(opacity))\n}\n\n/**\n * The plot backdrop: the panel fill, the grid and the axis rules, sized from its\n * box. Grid lines are placed from data-unit marker positions mapped through the\n * extents, so each lands under its marker however the markers are spaced; a line\n * on an edge is drawn just inside it.\n */\nexport const ChartCanvas = component2d({\n key: \"chartCanvas\",\n state: {\n fill: Types.custom<Fill, FillResolved[]>([], { resolve: fillOps.resolve, lerp: fillOps.lerp }),\n /** The vertical grid lines, one per `xMarkers` entry. */\n xGridStroke: strokeEntry(),\n /** The horizontal grid lines, one per `yMarkers` entry. */\n yGridStroke: strokeEntry(),\n xMarkers: fresh<number[]>(() => []),\n yMarkers: fresh<number[]>(() => []),\n /** `[min, max]` of each scale, mapping the markers into the panel. */\n xExtent: fresh<Range>(() => [0, 1]),\n yExtent: fresh<Range>(() => [0, 1]),\n /** The baseline and the value-axis rule. */\n axisStroke: strokeEntry(),\n /** Where the horizontal axis rule sits on the y scale, or `null` for none. */\n xAxisAt: Types.custom<number | null>(null),\n /** Where the vertical axis rule sits on the x scale, or `null` for none. */\n yAxisAt: Types.custom<number | null>(null),\n },\n load: (loader, state) => loadPaints(loader, { fills: [state.fill], strokes: [state.xGridStroke, state.yGridStroke, state.axisStroke] }),\n render: (ctx, state) => {\n const { width: W, height: H } = ctx.size\n if (W <= 0 || H <= 0) return\n if (state.fill.length > 0) ctx.draw(new Graphics2D().rect({ x: 0, y: 0, width: W, height: H }).fill(state.fill))\n\n const inside = (at: number, length: number, weight: number) => Math.max(-length / 2 + weight / 2, Math.min(length / 2 - weight / 2, at))\n const verticals = (values: readonly number[], stroke: StrokeResolved[]) => {\n const weight = stroke[0]?.weight ?? 0\n if (weight <= 0) return\n for (const v of values) {\n const x = inside(along(v, state.xExtent, W), W, weight)\n rule(ctx, { x, y: -H / 2 }, { x, y: H / 2 }, stroke)\n }\n }\n const horizontals = (values: readonly number[], stroke: StrokeResolved[]) => {\n const weight = stroke[0]?.weight ?? 0\n if (weight <= 0) return\n for (const v of values) {\n const y = inside(along(v, state.yExtent, H), H, weight)\n rule(ctx, { x: -W / 2, y }, { x: W / 2, y }, stroke)\n }\n }\n const within = ([min, max]: Range) => (v: number) => v >= Math.min(min, max) - 1e-9 && v <= Math.max(min, max) + 1e-9\n verticals(state.xMarkers.filter(within(state.xExtent)), state.xGridStroke)\n horizontals(state.yMarkers.filter(within(state.yExtent)), state.yGridStroke)\n if (state.xAxisAt !== null) horizontals([state.xAxisAt], state.axisStroke)\n if (state.yAxisAt !== null) verticals([state.yAxisAt], state.axisStroke)\n },\n})\n\nexport type ChartCanvas = NodeOf<typeof ChartCanvas>\n"]}
|
|
@@ -1,50 +1,19 @@
|
|
|
1
|
-
import { type Fill, type NodeOf, type
|
|
1
|
+
import { type Fill, type NodeOf, type Alignment as _Alignment, type CornerRadiusResolved as _CornerRadiusResolved, type CornerStyleResolved as _CornerStyleResolved, type FillResolved as _FillResolved, type FlowMode as _FlowMode, type FlowResolved as _FlowResolved, type GapSize as _GapSize, type Node2D as _Node2D, type Node2DProps as _Node2DProps, type RectCornerRadius as _RectCornerRadius, type RectCornerStyle as _RectCornerStyle, type Shadow as _Shadow, type ShadowResolved as _ShadowResolved, type Stroke as _Stroke, type StrokeResolved as _StrokeResolved, type Vector2 as _Vector2 } from "@motionscript/sdk";
|
|
2
2
|
import { type ChartSeries, type LegendConfig } from "./shared.js";
|
|
3
|
-
export interface ChartLegendProps extends RectProps {
|
|
4
|
-
/**
|
|
5
|
-
* One entry per series: legend `label`, its `enabled` default, and — through
|
|
6
|
-
* {@link ChartLegendProps.swatches} — the colour its chip is painted. A series
|
|
7
|
-
* that starts enabled (the default) is added to the row up front; one that
|
|
8
|
-
* starts `false` is hidden until a later `showSeries`.
|
|
9
|
-
*/
|
|
10
|
-
series: ChartSeries[];
|
|
11
|
-
/**
|
|
12
|
-
* The swatch colour for each series, by index — resolved by the chart, whose
|
|
13
|
-
* business it is whether a series' colour comes out of a stroke palette (a
|
|
14
|
-
* line) or a fill palette (a bar). Shorter than the series list means the
|
|
15
|
-
* missing entries fall back to white.
|
|
16
|
-
*/
|
|
17
|
-
swatches: Fill[];
|
|
18
|
-
/** Padding, caption style, gaps, and swatch geometry — see {@link LegendConfig}. */
|
|
19
|
-
legend: LegendConfig;
|
|
20
|
-
}
|
|
21
3
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* caption reads in the legend's neutral ink.
|
|
26
|
-
*
|
|
27
|
-
* `showSeries`/`hideSeries` flip a series on or off — showing one grows its
|
|
28
|
-
* entry back in at its stable series position, hiding one shrinks it away. So
|
|
29
|
-
* the row only ever shows the currently-enabled series, in series order, and
|
|
30
|
-
* closes the gap when one leaves.
|
|
31
|
-
*
|
|
32
|
-
* This node *is* the row, so the parent places it (centred below the chart) and
|
|
33
|
-
* it hugs its entries.
|
|
4
|
+
* A chart's legend: one entry per series, a colour swatch then its caption, in
|
|
5
|
+
* a row (`flow: "horizontal"`) or a column (`"vertical"`, for a legend beside
|
|
6
|
+
* the plot). This node is the row, so the parent places it.
|
|
34
7
|
*/
|
|
35
8
|
export declare const ChartLegend: import("@motionscript/sdk").Component<"chartLegend", import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
|
|
36
9
|
readonly series: import("@motionscript/sdk").Type<ChartSeries[], ChartSeries[], false>;
|
|
10
|
+
/** Swatch colour by series index, resolved by the chart; a missing entry is white. */
|
|
37
11
|
readonly swatches: import("@motionscript/sdk").Type<Fill[], Fill[], false>;
|
|
38
12
|
readonly legend: import("@motionscript/sdk").Type<LegendConfig, LegendConfig, false>;
|
|
39
13
|
/**
|
|
40
|
-
* How far each entry is shown, `0
|
|
41
|
-
* together
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* This replaces adding and removing the entry from the row: a command is
|
|
45
|
-
* asked what it looks like at a time, and "is this node a child" is not a
|
|
46
|
-
* value a time can produce. Collapsed-but-present is the same picture reached
|
|
47
|
-
* by a route that *is* a function of `t`.
|
|
14
|
+
* How far each entry is shown, `0`-`1`, driving its length, opacity and gap
|
|
15
|
+
* together. Entries are never added or removed: "is this node a child" is not
|
|
16
|
+
* a function of time, while collapsed-but-present is.
|
|
48
17
|
*/
|
|
49
18
|
readonly reveal: import("@motionscript/sdk").Type<number[] | Readonly<Record<number, number>>, number[], false>;
|
|
50
19
|
readonly cornerRadius: import("@motionscript/sdk").Attribute<_RectCornerRadius, _CornerRadiusResolved>;
|
|
@@ -81,17 +50,13 @@ export declare const ChartLegend: import("@motionscript/sdk").Component<"chartLe
|
|
|
81
50
|
};
|
|
82
51
|
}, null, _Node2D<_Node2DProps>, import("@motionscript/sdk").CallProps<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
|
|
83
52
|
readonly series: import("@motionscript/sdk").Type<ChartSeries[], ChartSeries[], false>;
|
|
53
|
+
/** Swatch colour by series index, resolved by the chart; a missing entry is white. */
|
|
84
54
|
readonly swatches: import("@motionscript/sdk").Type<Fill[], Fill[], false>;
|
|
85
55
|
readonly legend: import("@motionscript/sdk").Type<LegendConfig, LegendConfig, false>;
|
|
86
56
|
/**
|
|
87
|
-
* How far each entry is shown, `0
|
|
88
|
-
* together
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* This replaces adding and removing the entry from the row: a command is
|
|
92
|
-
* asked what it looks like at a time, and "is this node a child" is not a
|
|
93
|
-
* value a time can produce. Collapsed-but-present is the same picture reached
|
|
94
|
-
* by a route that *is* a function of `t`.
|
|
57
|
+
* How far each entry is shown, `0`-`1`, driving its length, opacity and gap
|
|
58
|
+
* together. Entries are never added or removed: "is this node a child" is not
|
|
59
|
+
* a function of time, while collapsed-but-present is.
|
|
95
60
|
*/
|
|
96
61
|
readonly reveal: import("@motionscript/sdk").Type<number[] | Readonly<Record<number, number>>, number[], false>;
|
|
97
62
|
readonly cornerRadius: import("@motionscript/sdk").Attribute<_RectCornerRadius, _CornerRadiusResolved>;
|
|
@@ -110,17 +75,13 @@ export declare const ChartLegend: import("@motionscript/sdk").Component<"chartLe
|
|
|
110
75
|
readonly end: import("@motionscript/sdk").Type<number, number, false>;
|
|
111
76
|
}>, null, import("@motionscript/sdk").BuiltNode<import("@motionscript/sdk").Merge<import("@motionscript/sdk").Box2DState, {
|
|
112
77
|
readonly series: import("@motionscript/sdk").Type<ChartSeries[], ChartSeries[], false>;
|
|
78
|
+
/** Swatch colour by series index, resolved by the chart; a missing entry is white. */
|
|
113
79
|
readonly swatches: import("@motionscript/sdk").Type<Fill[], Fill[], false>;
|
|
114
80
|
readonly legend: import("@motionscript/sdk").Type<LegendConfig, LegendConfig, false>;
|
|
115
81
|
/**
|
|
116
|
-
* How far each entry is shown, `0
|
|
117
|
-
* together
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* This replaces adding and removing the entry from the row: a command is
|
|
121
|
-
* asked what it looks like at a time, and "is this node a child" is not a
|
|
122
|
-
* value a time can produce. Collapsed-but-present is the same picture reached
|
|
123
|
-
* by a route that *is* a function of `t`.
|
|
82
|
+
* How far each entry is shown, `0`-`1`, driving its length, opacity and gap
|
|
83
|
+
* together. Entries are never added or removed: "is this node a child" is not
|
|
84
|
+
* a function of time, while collapsed-but-present is.
|
|
124
85
|
*/
|
|
125
86
|
readonly reveal: import("@motionscript/sdk").Type<number[] | Readonly<Record<number, number>>, number[], false>;
|
|
126
87
|
readonly cornerRadius: import("@motionscript/sdk").Attribute<_RectCornerRadius, _CornerRadiusResolved>;
|
|
@@ -156,9 +117,5 @@ export declare const ChartLegend: import("@motionscript/sdk").Component<"chartLe
|
|
|
156
117
|
readonly portion: import("@motionscript/sdk").Type<number, number, false>;
|
|
157
118
|
};
|
|
158
119
|
}, _Node2D<_Node2DProps>>>>;
|
|
159
|
-
/** Whether series `index` is showing at all on `legend`. */
|
|
160
|
-
export declare function legendShows(legend: {
|
|
161
|
-
readonly reveal: readonly number[];
|
|
162
|
-
}, index: number): boolean;
|
|
163
120
|
export type ChartLegend = NodeOf<typeof ChartLegend>;
|
|
164
121
|
//# sourceMappingURL=chart-legend.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chart-legend.d.ts","sourceRoot":"","sources":["../../src/kit/chart-legend.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"chart-legend.d.ts","sourceRoot":"","sources":["../../src/kit/chart-legend.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,KAAK,IAAI,EACT,KAAK,MAAM,EAEX,KAAK,SAAS,IAAI,UAAU,EAC5B,KAAK,oBAAoB,IAAI,qBAAqB,EAClD,KAAK,mBAAmB,IAAI,oBAAoB,EAChD,KAAK,YAAY,IAAI,aAAa,EAClC,KAAK,QAAQ,IAAI,SAAS,EAC1B,KAAK,YAAY,IAAI,aAAa,EAClC,KAAK,OAAO,IAAI,QAAQ,EACxB,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,WAAW,IAAI,YAAY,EAChC,KAAK,gBAAgB,IAAI,iBAAiB,EAC1C,KAAK,eAAe,IAAI,gBAAgB,EACxC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACtC,KAAK,MAAM,IAAI,OAAO,EACtB,KAAK,cAAc,IAAI,eAAe,EACtC,KAAK,OAAO,IAAI,QAAQ,EACzB,MAAM,mBAAmB,CAAA;AAK1B,OAAO,EAGL,KAAK,WAAW,EAChB,KAAK,YAAY,EAClB,MAAM,UAAU,CAAA;AA+BjB;;;;GAIG;AACH,eAAO,MAAM,WAAW;;IAKpB,sFAAsF;;;IAGtF;;;;OAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAPH,sFAAsF;;;IAGtF;;;;OAIG;;;;;;;;;;;;;;;;;;IAPH,sFAAsF;;;IAGtF;;;;OAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2BA0EL,CAAA;AAQF,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,WAAW,CAAC,CAAA"}
|
package/dist/kit/chart-legend.js
CHANGED
|
@@ -1,34 +1,29 @@
|
|
|
1
|
-
import { Flow, Paint, Rect, Shapes, Text, Types, component2d,
|
|
1
|
+
import { Flow, Paint, Rect, Shapes, Text, Types, component2d, } from "@motionscript/sdk";
|
|
2
|
+
import { boundText } from "./cartesian.js";
|
|
2
3
|
import { fresh } from "./entries.js";
|
|
3
4
|
import { frameEase } from "./motion.js";
|
|
4
5
|
import { DEFAULT_LEGEND, SPOTLIGHT_DIM, } from "./shared.js";
|
|
5
|
-
/** A row's entries are a `Rect`'s: paint, the flow of its children, its corners. */
|
|
6
6
|
const row = { ...Paint.state, ...Flow.state, ...Shapes.corners };
|
|
7
7
|
/**
|
|
8
|
-
* One legend entry
|
|
9
|
-
* from nothing
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* never depends on whether the entry was once seen fully shown. A collapse needs a
|
|
13
|
-
* number to scale, and `"hug"` is not one until something measures it — this is
|
|
14
|
-
* the something.
|
|
8
|
+
* One legend entry, hugging its swatch and caption; `shown` scales its length
|
|
9
|
+
* along the legend (`along`) from nothing. The length is measured from the
|
|
10
|
+
* content at any reveal, so a frame never depends on the entry once having been
|
|
11
|
+
* fully shown.
|
|
15
12
|
*/
|
|
16
13
|
const LegendEntry = component2d({
|
|
17
14
|
key: "legendEntry",
|
|
18
|
-
state: { ...row, shown: Types.number(1) },
|
|
15
|
+
state: { ...row, shown: Types.number(1), along: Types.oneOf(["horizontal", "vertical"]) },
|
|
19
16
|
commands: { ...Paint.commands },
|
|
20
17
|
measure: (measurer, state, children) => {
|
|
21
18
|
const size = Flow.measure(measurer, state, children, Paint.inset(state));
|
|
22
|
-
|
|
19
|
+
if (state.shown >= 1)
|
|
20
|
+
return size;
|
|
21
|
+
return state.along === "vertical" ? { ...size, height: state.shown * size.height } : { ...size, width: state.shown * size.width };
|
|
23
22
|
},
|
|
24
23
|
layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),
|
|
25
24
|
render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),
|
|
26
25
|
});
|
|
27
|
-
/**
|
|
28
|
-
* How far each entry is shown, written whole (a document, a `set`) or one
|
|
29
|
-
* entry at a time (a command, as `{ [index]: reveal }`), which leaves the others
|
|
30
|
-
* as the frame left them — so two series moving at once each move their own.
|
|
31
|
-
*/
|
|
26
|
+
/** Written whole, or one entry at a time as `{ [index]: reveal }`, which leaves the others as the frame left them so two series can move at once. */
|
|
32
27
|
function revealOf(value, previous) {
|
|
33
28
|
if (Array.isArray(value))
|
|
34
29
|
return value;
|
|
@@ -38,86 +33,66 @@ function revealOf(value, previous) {
|
|
|
38
33
|
return next;
|
|
39
34
|
}
|
|
40
35
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* caption reads in the legend's neutral ink.
|
|
45
|
-
*
|
|
46
|
-
* `showSeries`/`hideSeries` flip a series on or off — showing one grows its
|
|
47
|
-
* entry back in at its stable series position, hiding one shrinks it away. So
|
|
48
|
-
* the row only ever shows the currently-enabled series, in series order, and
|
|
49
|
-
* closes the gap when one leaves.
|
|
50
|
-
*
|
|
51
|
-
* This node *is* the row, so the parent places it (centred below the chart) and
|
|
52
|
-
* it hugs its entries.
|
|
36
|
+
* A chart's legend: one entry per series, a colour swatch then its caption, in
|
|
37
|
+
* a row (`flow: "horizontal"`) or a column (`"vertical"`, for a legend beside
|
|
38
|
+
* the plot). This node is the row, so the parent places it.
|
|
53
39
|
*/
|
|
54
40
|
export const ChartLegend = component2d({
|
|
55
41
|
key: "chartLegend",
|
|
56
42
|
state: {
|
|
57
43
|
...row,
|
|
58
44
|
series: fresh(() => []),
|
|
45
|
+
/** Swatch colour by series index, resolved by the chart; a missing entry is white. */
|
|
59
46
|
swatches: fresh(() => []),
|
|
60
47
|
legend: Types.custom(DEFAULT_LEGEND),
|
|
61
48
|
/**
|
|
62
|
-
* How far each entry is shown, `0
|
|
63
|
-
* together
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* This replaces adding and removing the entry from the row: a command is
|
|
67
|
-
* asked what it looks like at a time, and "is this node a child" is not a
|
|
68
|
-
* value a time can produce. Collapsed-but-present is the same picture reached
|
|
69
|
-
* by a route that *is* a function of `t`.
|
|
49
|
+
* How far each entry is shown, `0`-`1`, driving its length, opacity and gap
|
|
50
|
+
* together. Entries are never added or removed: "is this node a child" is not
|
|
51
|
+
* a function of time, while collapsed-but-present is.
|
|
70
52
|
*/
|
|
71
53
|
reveal: fresh(() => [], { resolve: revealOf }),
|
|
72
54
|
},
|
|
73
|
-
//
|
|
74
|
-
// stated — a `Rect` given no flow lays its children out `freeform`, which
|
|
75
|
-
// stacks every entry on the same spot. The gap and padding are the theme's.
|
|
55
|
+
// The flow must be stated: a `Rect` with none lays children out `freeform`, stacking every entry on one spot.
|
|
76
56
|
defaults: {
|
|
77
57
|
flow: "horizontal",
|
|
78
58
|
width: "hug",
|
|
79
59
|
height: "hug",
|
|
80
|
-
gap: (state) => state.legend.itemGap,
|
|
60
|
+
gap: (state) => (state.flow === "vertical" ? Math.round(state.legend.itemGap * 0.4) : state.legend.itemGap),
|
|
81
61
|
padding: (state) => state.legend.padding,
|
|
62
|
+
align: (state) => (state.flow === "vertical" ? "centerLeft" : "center"),
|
|
82
63
|
reveal: (state) => state.series.map((s) => ((s.enabled ?? true) ? 1 : 0)),
|
|
83
64
|
},
|
|
84
|
-
//
|
|
85
|
-
// fixed rather than depending on which siblings happen to be shown, so an entry
|
|
86
|
-
// always comes back where it belongs.
|
|
65
|
+
// Every entry stays a child, so one always comes back where it belongs.
|
|
87
66
|
children: (state) => {
|
|
88
|
-
const marker = state.legend.markerStyle;
|
|
67
|
+
const marker = () => state.legend.markerStyle;
|
|
68
|
+
const along = state.flow === "vertical" ? "vertical" : "horizontal";
|
|
89
69
|
return state.series.map((series, index) => LegendEntry({
|
|
90
70
|
flow: "horizontal",
|
|
91
|
-
gap:
|
|
71
|
+
gap: () => Math.round((state.legend.textStyle.fontSize ?? 24) * 0.45),
|
|
92
72
|
align: "center",
|
|
93
73
|
width: "hug",
|
|
94
74
|
height: "hug",
|
|
95
|
-
|
|
96
|
-
//
|
|
97
|
-
// and the flanking gap closes with the entry rather than snapping.
|
|
75
|
+
along,
|
|
76
|
+
// The length follows the reveal in `measure`, and the flanking gap closes with the entry.
|
|
98
77
|
opacity: () => state.reveal[index] ?? 0,
|
|
99
78
|
gapScale: () => state.reveal[index] ?? 0,
|
|
100
79
|
shown: () => state.reveal[index] ?? 0,
|
|
101
80
|
children: [
|
|
102
|
-
Text({ ...textOps.styleProps(state.legend.textStyle), text: series.label }),
|
|
103
81
|
Rect({
|
|
104
|
-
width: marker.width,
|
|
105
|
-
height: marker.height,
|
|
106
|
-
cornerRadius: marker.borderRadius,
|
|
107
|
-
fill: state.swatches[index] ?? "white",
|
|
108
|
-
stroke: marker.stroke,
|
|
109
|
-
shadow: marker.shadow,
|
|
82
|
+
width: () => marker().width,
|
|
83
|
+
height: () => marker().height,
|
|
84
|
+
cornerRadius: () => marker().borderRadius,
|
|
85
|
+
fill: () => state.swatches[index] ?? "white",
|
|
86
|
+
stroke: () => marker().stroke,
|
|
87
|
+
shadow: () => marker().shadow,
|
|
110
88
|
}),
|
|
89
|
+
Text({ ...boundText(() => state.legend.textStyle), text: series.label || series.field }),
|
|
111
90
|
],
|
|
112
91
|
}));
|
|
113
92
|
},
|
|
114
93
|
commands: {
|
|
115
94
|
...Paint.commands,
|
|
116
|
-
/**
|
|
117
|
-
* Grow series `index`'s entry back in at its series-order position, the
|
|
118
|
-
* siblings sliding over to make room. A reveal tween rather than an animated
|
|
119
|
-
* `addChildAt`: the entry never left, so the whole thing is a function of `t`.
|
|
120
|
-
*/
|
|
95
|
+
/** Grow series `index`'s entry back in at its position, the siblings sliding over. */
|
|
121
96
|
showSeries: {
|
|
122
97
|
args: { index: Types.number(0, { min: 0, step: 1 }) },
|
|
123
98
|
// An index that is not a series takes its slot and moves nothing.
|
|
@@ -125,20 +100,14 @@ export const ChartLegend = component2d({
|
|
|
125
100
|
? timeline.animate(revealFrame(state, timeline.args.index, 1, frameEase(timeline)))
|
|
126
101
|
: timeline.wait(timeline.duration)),
|
|
127
102
|
},
|
|
128
|
-
/**
|
|
103
|
+
/** The mirror of `showSeries`. */
|
|
129
104
|
hideSeries: {
|
|
130
105
|
args: { index: Types.number(0, { min: 0, step: 1 }) },
|
|
131
106
|
run: (timeline, state) => (timeline.args.index in state.series
|
|
132
107
|
? timeline.animate(revealFrame(state, timeline.args.index, 0, frameEase(timeline)))
|
|
133
108
|
: timeline.wait(timeline.duration)),
|
|
134
109
|
},
|
|
135
|
-
/**
|
|
136
|
-
* Dim every entry but series `index` to {@link SPOTLIGHT_DIM} and bring that
|
|
137
|
-
* one back to full — the legend half of a chart's `spotlightSeries`. Entries
|
|
138
|
-
* stay in place; only their opacity moves, so the row doesn't reflow. Only
|
|
139
|
-
* the entries shown when it is built are touched: a hidden series' entry is
|
|
140
|
-
* left as `hideSeries` left it.
|
|
141
|
-
*/
|
|
110
|
+
/** Dim every entry but `index` to {@link SPOTLIGHT_DIM}, in place. A hidden series' entry is left as `hideSeries` left it. */
|
|
142
111
|
spotlightSeries: {
|
|
143
112
|
args: { index: Types.number(0, { min: 0, step: 1 }) },
|
|
144
113
|
run: (timeline, state, children) => timeline.together(children.flatMap((entry, i) => (state.reveal[i] ?? 0) > 0 ? [entry.to({ opacity: i === timeline.args.index ? 1 : SPOTLIGHT_DIM })] : [])),
|
|
@@ -148,16 +117,9 @@ export const ChartLegend = component2d({
|
|
|
148
117
|
layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),
|
|
149
118
|
render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),
|
|
150
119
|
});
|
|
151
|
-
/**
|
|
152
|
-
* One entry's reveal ramping to `target` from where the command finds it — so a
|
|
153
|
-
* hide that follows a show starts where the show left it.
|
|
154
|
-
*/
|
|
120
|
+
/** One entry's reveal ramping to `target` from where the command finds it, so a hide after a show starts where the show left it. */
|
|
155
121
|
function revealFrame(state, index, target, ease) {
|
|
156
122
|
const from = state.reveal[index] ?? 0;
|
|
157
123
|
return (t) => ({ reveal: { [index]: from + (target - from) * ease(t) } });
|
|
158
124
|
}
|
|
159
|
-
/** Whether series `index` is showing at all on `legend`. */
|
|
160
|
-
export function legendShows(legend, index) {
|
|
161
|
-
return (legend.reveal[index] ?? 0) > 0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,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvF,MAAM,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;CAC7E,CAAC,CAAA;AAEF;;;GAGG;AACH,SAAS,WAAW,CAAC,KAA6C,EAAE,KAAa,EAAE,MAAc,EAAE,IAA2B;IAC5H,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IACrC,OAAO,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,IAAI,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;AACnF,CAAC;AAED,4DAA4D;AAC5D,MAAM,UAAU,WAAW,CAAC,MAA8C,EAAE,KAAa;IACvF,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;AACxC,CAAC","sourcesContent":["import {\n Flow,\n Paint,\n Rect,\n Shapes,\n Text,\n Types,\n component2d,\n textOps,\n type Fill,\n type NodeOf,\n type RectProps,\n // Named so the row's emitted type reaches the Rect surface it spreads through the sdk, not core.\n type Alignment as _Alignment,\n type CornerRadiusResolved as _CornerRadiusResolved,\n type CornerStyleResolved as _CornerStyleResolved,\n type FillResolved as _FillResolved,\n type FlowMode as _FlowMode,\n type FlowResolved as _FlowResolved,\n type GapSize as _GapSize,\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n type RectCornerRadius as _RectCornerRadius,\n type RectCornerStyle as _RectCornerStyle,\n type Shadow as _Shadow,\n type ShadowResolved as _ShadowResolved,\n type Stroke as _Stroke,\n type StrokeResolved as _StrokeResolved,\n type Vector2 as _Vector2,\n} from \"@motionscript/sdk\"\n\nimport { fresh } from \"./entries\"\nimport { frameEase } from \"./motion\"\nimport {\n DEFAULT_LEGEND,\n SPOTLIGHT_DIM,\n type ChartSeries,\n type LegendConfig,\n} from \"./shared\"\n\nexport interface ChartLegendProps extends RectProps {\n /**\n * One entry per series: legend `label`, its `enabled` default, and — through\n * {@link ChartLegendProps.swatches} — the colour its chip is painted. A series\n * that starts enabled (the default) is added to the row up front; one that\n * starts `false` is hidden until a later `showSeries`.\n */\n series: ChartSeries[]\n /**\n * The swatch colour for each series, by index — resolved by the chart, whose\n * business it is whether a series' colour comes out of a stroke palette (a\n * line) or a fill palette (a bar). Shorter than the series list means the\n * missing entries fall back to white.\n */\n swatches: Fill[]\n /** Padding, caption style, gaps, and swatch geometry — see {@link LegendConfig}. */\n legend: LegendConfig\n}\n\n/** A row's entries are a `Rect`'s: paint, the flow of its children, its corners. */\nconst row = { ...Paint.state, ...Flow.state, ...Shapes.corners }\n\n/**\n * One legend entry. It hugs its caption and swatch; `shown` scales that width\n * from nothing to the whole of it.\n *\n * The width is taken from the content on every measure, at any reveal, so a frame\n * never depends on whether the entry was once seen fully shown. A collapse needs a\n * number to scale, and `\"hug\"` is not one until something measures it — this is\n * the something.\n */\nconst LegendEntry = component2d({\n key: \"legendEntry\",\n state: { ...row, shown: Types.number(1) },\n commands: { ...Paint.commands },\n measure: (measurer, state, children) => {\n const size = Flow.measure(measurer, state, children, Paint.inset(state))\n return state.shown >= 1 ? size : { ...size, width: state.shown * size.width }\n },\n layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),\n render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),\n})\n\n/**\n * How far each entry is shown, written whole (a document, a `set`) or one\n * entry at a time (a command, as `{ [index]: reveal }`), which leaves the others\n * as the frame left them — so two series moving at once each move their own.\n */\nfunction revealOf(value: readonly number[] | Readonly<Record<number, number>>, previous?: number[]): number[] {\n if (Array.isArray(value)) return value as number[]\n const next = [...(previous ?? [])]\n for (const [index, reveal] of Object.entries(value)) next[Number(index)] = reveal\n return next\n}\n\n/**\n * The legend of a chart: a **horizontal-flow row of entries**, one per series, laid out\n * left-to-right with the theme's `itemGap`. Each entry pairs the series label\n * with a rounded colour swatch; only the swatch carries the series colour, the\n * caption reads in the legend's neutral ink.\n *\n * `showSeries`/`hideSeries` flip a series on or off — showing one grows its\n * entry back in at its stable series position, hiding one shrinks it away. So\n * the row only ever shows the currently-enabled series, in series order, and\n * closes the gap when one leaves.\n *\n * This node *is* the row, so the parent places it (centred below the chart) and\n * it hugs its entries.\n */\nexport const ChartLegend = component2d({\n key: \"chartLegend\",\n state: {\n ...row,\n series: fresh<ChartSeries[]>(() => []),\n swatches: fresh<Fill[]>(() => []),\n legend: Types.custom<LegendConfig>(DEFAULT_LEGEND),\n /**\n * How far each entry is shown, `0`–`1`. Drives its width, opacity and gap\n * together, so `0` is an entry that takes no room and `1` is one at its\n * natural size.\n *\n * This replaces adding and removing the entry from the row: a command is\n * asked what it looks like at a time, and \"is this node a child\" is not a\n * value a time can produce. Collapsed-but-present is the same picture reached\n * by a route that *is* a function of `t`.\n */\n reveal: fresh<number[], number[] | Readonly<Record<number, number>>>(() => [], { resolve: revealOf }),\n },\n // This node *is* the row, so the direction is its own answer. It has to be\n // stated — a `Rect` given no flow lays its children out `freeform`, which\n // stacks every entry on the same spot. The gap and padding are the theme's.\n defaults: {\n flow: \"horizontal\",\n width: \"hug\",\n height: \"hug\",\n gap: (state) => state.legend.itemGap,\n padding: (state) => state.legend.padding,\n reveal: (state) => state.series.map((s) => ((s.enabled ?? true) ? 1 : 0)),\n },\n // One entry per series, in series order, every one a child for good: order is\n // fixed rather than depending on which siblings happen to be shown, so an entry\n // always comes back where it belongs.\n children: (state) => {\n const marker = state.legend.markerStyle\n return state.series.map((series, index) =>\n LegendEntry({\n flow: \"horizontal\",\n gap: 12,\n align: \"center\",\n width: \"hug\",\n height: \"hug\",\n // Bound to the reveal, so showing and hiding is a number moving rather\n // than a child arriving and leaving. The width follows it in `measure`,\n // and the flanking gap closes with the entry rather than snapping.\n opacity: () => state.reveal[index] ?? 0,\n gapScale: () => state.reveal[index] ?? 0,\n shown: () => state.reveal[index] ?? 0,\n children: [\n Text({ ...textOps.styleProps(state.legend.textStyle), text: series.label }),\n Rect({\n width: marker.width,\n height: marker.height,\n cornerRadius: marker.borderRadius,\n fill: state.swatches[index] ?? \"white\",\n stroke: marker.stroke,\n shadow: marker.shadow,\n }),\n ],\n })\n )\n },\n commands: {\n ...Paint.commands,\n /**\n * Grow series `index`'s entry back in at its series-order position, the\n * siblings sliding over to make room. A reveal tween rather than an animated\n * `addChildAt`: the entry never left, so the whole thing is a function of `t`.\n */\n showSeries: {\n args: { index: Types.number(0, { min: 0, step: 1 }) },\n // An index that is not a series takes its slot and moves nothing.\n run: (timeline, state) => (timeline.args.index in state.series\n ? timeline.animate(revealFrame(state, timeline.args.index, 1, frameEase(timeline)))\n : timeline.wait(timeline.duration)),\n },\n /** Shrink series `index`'s entry away, the gap closing behind it — the mirror of `showSeries`. */\n hideSeries: {\n args: { index: Types.number(0, { min: 0, step: 1 }) },\n run: (timeline, state) => (timeline.args.index in state.series\n ? timeline.animate(revealFrame(state, timeline.args.index, 0, frameEase(timeline)))\n : timeline.wait(timeline.duration)),\n },\n /**\n * Dim every entry but series `index` to {@link SPOTLIGHT_DIM} and bring that\n * one back to full — the legend half of a chart's `spotlightSeries`. Entries\n * stay in place; only their opacity moves, so the row doesn't reflow. Only\n * the entries shown when it is built are touched: a hidden series' entry is\n * left as `hideSeries` left it.\n */\n spotlightSeries: {\n args: { index: Types.number(0, { min: 0, step: 1 }) },\n run: (timeline, state, children) =>\n timeline.together(\n children.flatMap((entry, i) =>\n (state.reveal[i] ?? 0) > 0 ? [entry.to({ opacity: i === timeline.args.index ? 1 : SPOTLIGHT_DIM })] : []\n )\n ),\n },\n },\n measure: (measurer, state, children) => Flow.measure(measurer, state, children, Paint.inset(state)),\n layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),\n render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),\n})\n\n/**\n * One entry's reveal ramping to `target` from where the command finds it — so a\n * hide that follows a show starts where the show left it.\n */\nfunction revealFrame(state: { readonly reveal: readonly number[] }, index: number, target: number, ease: (t: number) => number) {\n const from = state.reveal[index] ?? 0\n return (t: number) => ({ reveal: { [index]: from + (target - from) * ease(t) } })\n}\n\n/** Whether series `index` is showing at all on `legend`. */\nexport function legendShows(legend: { readonly reveal: readonly number[] }, index: number): boolean {\n return (legend.reveal[index] ?? 0) > 0\n}\n\nexport type ChartLegend = NodeOf<typeof ChartLegend>\n"]}
|
|
1
|
+
{"version":3,"file":"chart-legend.js","sourceRoot":"","sources":["../../src/kit/chart-legend.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,IAAI,EACJ,KAAK,EACL,IAAI,EACJ,MAAM,EACN,IAAI,EACJ,KAAK,EACL,WAAW,GAoBZ,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAA;AACjC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACpC,OAAO,EACL,cAAc,EACd,aAAa,GAGd,MAAM,UAAU,CAAA;AAEjB,MAAM,GAAG,GAAG,EAAE,GAAG,KAAK,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,GAAG,MAAM,CAAC,OAAO,EAAE,CAAA;AAEhE;;;;;GAKG;AACH,MAAM,WAAW,GAAG,WAAW,CAAC;IAC9B,GAAG,EAAE,aAAa;IAClB,KAAK,EAAE,EAAE,GAAG,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,UAAU,CAAU,CAAC,EAAE;IAClG,QAAQ,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AK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACvF,MAAM,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;CAC7E,CAAC,CAAA;AAEF,oIAAoI;AACpI,SAAS,WAAW,CAAC,KAA6C,EAAE,KAAa,EAAE,MAAc,EAAE,IAA2B;IAC5H,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IACrC,OAAO,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,IAAI,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;AACnF,CAAC","sourcesContent":["import {\n Flow,\n Paint,\n Rect,\n Shapes,\n Text,\n Types,\n component2d,\n type Fill,\n type NodeOf,\n // Named so the row's emitted type reaches the Rect surface it spreads through the sdk, not core.\n type Alignment as _Alignment,\n type CornerRadiusResolved as _CornerRadiusResolved,\n type CornerStyleResolved as _CornerStyleResolved,\n type FillResolved as _FillResolved,\n type FlowMode as _FlowMode,\n type FlowResolved as _FlowResolved,\n type GapSize as _GapSize,\n type Node2D as _Node2D,\n type Node2DProps as _Node2DProps,\n type RectCornerRadius as _RectCornerRadius,\n type RectCornerStyle as _RectCornerStyle,\n type Shadow as _Shadow,\n type ShadowResolved as _ShadowResolved,\n type Stroke as _Stroke,\n type StrokeResolved as _StrokeResolved,\n type Vector2 as _Vector2,\n} from \"@motionscript/sdk\"\n\nimport { boundText } from \"./cartesian\"\nimport { fresh } from \"./entries\"\nimport { frameEase } from \"./motion\"\nimport {\n DEFAULT_LEGEND,\n SPOTLIGHT_DIM,\n type ChartSeries,\n type LegendConfig,\n} from \"./shared\"\n\nconst row = { ...Paint.state, ...Flow.state, ...Shapes.corners }\n\n/**\n * One legend entry, hugging its swatch and caption; `shown` scales its length\n * along the legend (`along`) from nothing. The length is measured from the\n * content at any reveal, so a frame never depends on the entry once having been\n * fully shown.\n */\nconst LegendEntry = component2d({\n key: \"legendEntry\",\n state: { ...row, shown: Types.number(1), along: Types.oneOf([\"horizontal\", \"vertical\"] as const) },\n commands: { ...Paint.commands },\n measure: (measurer, state, children) => {\n const size = Flow.measure(measurer, state, children, Paint.inset(state))\n if (state.shown >= 1) return size\n return state.along === \"vertical\" ? { ...size, height: state.shown * size.height } : { ...size, width: state.shown * size.width }\n },\n layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),\n render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),\n})\n\n/** Written whole, or one entry at a time as `{ [index]: reveal }`, which leaves the others as the frame left them so two series can move at once. */\nfunction revealOf(value: readonly number[] | Readonly<Record<number, number>>, previous?: number[]): number[] {\n if (Array.isArray(value)) return value as number[]\n const next = [...(previous ?? [])]\n for (const [index, reveal] of Object.entries(value)) next[Number(index)] = reveal\n return next\n}\n\n/**\n * A chart's legend: one entry per series, a colour swatch then its caption, in\n * a row (`flow: \"horizontal\"`) or a column (`\"vertical\"`, for a legend beside\n * the plot). This node is the row, so the parent places it.\n */\nexport const ChartLegend = component2d({\n key: \"chartLegend\",\n state: {\n ...row,\n series: fresh<ChartSeries[]>(() => []),\n /** Swatch colour by series index, resolved by the chart; a missing entry is white. */\n swatches: fresh<Fill[]>(() => []),\n legend: Types.custom<LegendConfig>(DEFAULT_LEGEND),\n /**\n * How far each entry is shown, `0`-`1`, driving its length, opacity and gap\n * together. Entries are never added or removed: \"is this node a child\" is not\n * a function of time, while collapsed-but-present is.\n */\n reveal: fresh<number[], number[] | Readonly<Record<number, number>>>(() => [], { resolve: revealOf }),\n },\n // The flow must be stated: a `Rect` with none lays children out `freeform`, stacking every entry on one spot.\n defaults: {\n flow: \"horizontal\",\n width: \"hug\",\n height: \"hug\",\n gap: (state) => (state.flow === \"vertical\" ? Math.round(state.legend.itemGap * 0.4) : state.legend.itemGap),\n padding: (state) => state.legend.padding,\n align: (state) => (state.flow === \"vertical\" ? \"centerLeft\" : \"center\"),\n reveal: (state) => state.series.map((s) => ((s.enabled ?? true) ? 1 : 0)),\n },\n // Every entry stays a child, so one always comes back where it belongs.\n children: (state) => {\n const marker = () => state.legend.markerStyle\n const along = state.flow === \"vertical\" ? \"vertical\" : \"horizontal\"\n return state.series.map((series, index) =>\n LegendEntry({\n flow: \"horizontal\",\n gap: () => Math.round(((state.legend.textStyle.fontSize as number | undefined) ?? 24) * 0.45),\n align: \"center\",\n width: \"hug\",\n height: \"hug\",\n along,\n // The length follows the reveal in `measure`, and the flanking gap closes with the entry.\n opacity: () => state.reveal[index] ?? 0,\n gapScale: () => state.reveal[index] ?? 0,\n shown: () => state.reveal[index] ?? 0,\n children: [\n Rect({\n width: () => marker().width,\n height: () => marker().height,\n cornerRadius: () => marker().borderRadius,\n fill: () => state.swatches[index] ?? \"white\",\n stroke: () => marker().stroke,\n shadow: () => marker().shadow,\n }),\n Text({ ...boundText(() => state.legend.textStyle), text: series.label || series.field }),\n ],\n })\n )\n },\n commands: {\n ...Paint.commands,\n /** Grow series `index`'s entry back in at its position, the siblings sliding over. */\n showSeries: {\n args: { index: Types.number(0, { min: 0, step: 1 }) },\n // An index that is not a series takes its slot and moves nothing.\n run: (timeline, state) => (timeline.args.index in state.series\n ? timeline.animate(revealFrame(state, timeline.args.index, 1, frameEase(timeline)))\n : timeline.wait(timeline.duration)),\n },\n /** The mirror of `showSeries`. */\n hideSeries: {\n args: { index: Types.number(0, { min: 0, step: 1 }) },\n run: (timeline, state) => (timeline.args.index in state.series\n ? timeline.animate(revealFrame(state, timeline.args.index, 0, frameEase(timeline)))\n : timeline.wait(timeline.duration)),\n },\n /** Dim every entry but `index` to {@link SPOTLIGHT_DIM}, in place. A hidden series' entry is left as `hideSeries` left it. */\n spotlightSeries: {\n args: { index: Types.number(0, { min: 0, step: 1 }) },\n run: (timeline, state, children) =>\n timeline.together(\n children.flatMap((entry, i) =>\n (state.reveal[i] ?? 0) > 0 ? [entry.to({ opacity: i === timeline.args.index ? 1 : SPOTLIGHT_DIM })] : []\n )\n ),\n },\n },\n measure: (measurer, state, children) => Flow.measure(measurer, state, children, Paint.inset(state)),\n layout: (box, state, children) => Flow.layout(box, state, children, Paint.inset(state)),\n render: (ctx, state) => Paint.draw(ctx, state, Shapes.rect(ctx.size, state)),\n})\n\n/** One entry's reveal ramping to `target` from where the command finds it, so a hide after a show starts where the show left it. */\nfunction revealFrame(state: { readonly reveal: readonly number[] }, index: number, target: number, ease: (t: number) => number) {\n const from = state.reveal[index] ?? 0\n return (t: number) => ({ reveal: { [index]: from + (target - from) * ease(t) } })\n}\n\nexport type ChartLegend = NodeOf<typeof ChartLegend>\n"]}
|
package/dist/kit/entries.d.ts
CHANGED
|
@@ -14,4 +14,6 @@ export declare function fresh<T, In = T>(make: () => T, options?: {
|
|
|
14
14
|
* the element.
|
|
15
15
|
*/
|
|
16
16
|
export declare function asParts(nodes: readonly Node2D[]): readonly Element[];
|
|
17
|
+
/** The argument every per-series command takes: an index into `series`, offered by name. */
|
|
18
|
+
export declare const SERIES_ARG: Type<number, number, false>;
|
|
17
19
|
//# sourceMappingURL=entries.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entries.d.ts","sourceRoot":"","sources":["../../src/kit/entries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,OAAO,
|
|
1
|
+
{"version":3,"file":"entries.d.ts","sourceRoot":"","sources":["../../src/kit/entries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,OAAO,EAAqB,KAAK,MAAM,EAAE,KAAK,IAAI,EAAE,MAAM,mBAAmB,CAAA;AAElG;;;GAGG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE;IAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,KAAK,CAAC,CAAC;IAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,CAGtI;AAED;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,OAAO,EAAE,CAEpE;AAED,4FAA4F;AAC5F,eAAO,MAAM,UAAU,6BAAuI,CAAA"}
|
package/dist/kit/entries.js
CHANGED
|
@@ -16,4 +16,6 @@ export function fresh(make, options) {
|
|
|
16
16
|
export function asParts(nodes) {
|
|
17
17
|
return nodes;
|
|
18
18
|
}
|
|
19
|
+
/** The argument every per-series command takes: an index into `series`, offered by name. */
|
|
20
|
+
export const SERIES_ARG = Types.custom(0, { kind: "series", min: 0, step: 1 });
|
|
19
21
|
//# sourceMappingURL=entries.js.map
|