@motionscript/charts 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +201 -0
- package/dist/bar-chart/bar-chart.d.ts +286 -0
- package/dist/bar-chart/bar-chart.d.ts.map +1 -0
- package/dist/bar-chart/bar-chart.js +610 -0
- package/dist/bar-chart/bar-chart.js.map +1 -0
- package/dist/bar-chart/category-axis.d.ts +77 -0
- package/dist/bar-chart/category-axis.d.ts.map +1 -0
- package/dist/bar-chart/category-axis.js +141 -0
- package/dist/bar-chart/category-axis.js.map +1 -0
- package/dist/bar-chart/chart-bars.d.ts +149 -0
- package/dist/bar-chart/chart-bars.d.ts.map +1 -0
- package/dist/bar-chart/chart-bars.js +236 -0
- package/dist/bar-chart/chart-bars.js.map +1 -0
- package/dist/bar-chart/index.d.ts +16 -0
- package/dist/bar-chart/index.d.ts.map +1 -0
- package/dist/bar-chart/index.js +12 -0
- package/dist/bar-chart/index.js.map +1 -0
- package/dist/bar-chart/shared.d.ts +146 -0
- package/dist/bar-chart/shared.d.ts.map +1 -0
- package/dist/bar-chart/shared.js +182 -0
- package/dist/bar-chart/shared.js.map +1 -0
- package/dist/browser/chunks/chunk-ZMBOBHYL.js +2 -0
- package/dist/browser/chunks/chunk-ZMBOBHYL.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +12 -0
- package/dist/engine.d.ts +27 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +27 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/chart-border.d.ts +28 -0
- package/dist/kit/chart-border.d.ts.map +1 -0
- package/dist/kit/chart-border.js +45 -0
- package/dist/kit/chart-border.js.map +1 -0
- package/dist/kit/chart-canvas.d.ts +56 -0
- package/dist/kit/chart-canvas.d.ts.map +1 -0
- package/dist/kit/chart-canvas.js +125 -0
- package/dist/kit/chart-canvas.js.map +1 -0
- package/dist/kit/chart-legend.d.ts +126 -0
- package/dist/kit/chart-legend.d.ts.map +1 -0
- package/dist/kit/chart-legend.js +212 -0
- package/dist/kit/chart-legend.js.map +1 -0
- package/dist/kit/shared.d.ts +251 -0
- package/dist/kit/shared.d.ts.map +1 -0
- package/dist/kit/shared.js +401 -0
- package/dist/kit/shared.js.map +1 -0
- package/dist/kit/vertical-axis.d.ts +99 -0
- package/dist/kit/vertical-axis.d.ts.map +1 -0
- package/dist/kit/vertical-axis.js +176 -0
- package/dist/kit/vertical-axis.js.map +1 -0
- package/dist/line-chart/chart-line.d.ts +91 -0
- package/dist/line-chart/chart-line.d.ts.map +1 -0
- package/dist/line-chart/chart-line.js +156 -0
- package/dist/line-chart/chart-line.js.map +1 -0
- package/dist/line-chart/chart-region.d.ts +47 -0
- package/dist/line-chart/chart-region.d.ts.map +1 -0
- package/dist/line-chart/chart-region.js +101 -0
- package/dist/line-chart/chart-region.js.map +1 -0
- package/dist/line-chart/horizontal-axis.d.ts +85 -0
- package/dist/line-chart/horizontal-axis.d.ts.map +1 -0
- package/dist/line-chart/horizontal-axis.js +158 -0
- package/dist/line-chart/horizontal-axis.js.map +1 -0
- package/dist/line-chart/index.d.ts +22 -0
- package/dist/line-chart/index.d.ts.map +1 -0
- package/dist/line-chart/index.js +19 -0
- package/dist/line-chart/index.js.map +1 -0
- package/dist/line-chart/line-chart.d.ts +347 -0
- package/dist/line-chart/line-chart.d.ts.map +1 -0
- package/dist/line-chart/line-chart.js +701 -0
- package/dist/line-chart/line-chart.js.map +1 -0
- package/dist/line-chart/shared.d.ts +171 -0
- package/dist/line-chart/shared.d.ts.map +1 -0
- package/dist/line-chart/shared.js +270 -0
- package/dist/line-chart/shared.js.map +1 -0
- package/dist/line-chart/x-scale.d.ts +84 -0
- package/dist/line-chart/x-scale.d.ts.map +1 -0
- package/dist/line-chart/x-scale.js +302 -0
- package/dist/line-chart/x-scale.js.map +1 -0
- package/dist/nodes.d.ts +20 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +20 -0
- package/dist/nodes.js.map +1 -0
- package/dist/pie-chart/index.d.ts +18 -0
- package/dist/pie-chart/index.d.ts.map +1 -0
- package/dist/pie-chart/index.js +14 -0
- package/dist/pie-chart/index.js.map +1 -0
- package/dist/pie-chart/pie-chart.d.ts +170 -0
- package/dist/pie-chart/pie-chart.d.ts.map +1 -0
- package/dist/pie-chart/pie-chart.js +291 -0
- package/dist/pie-chart/pie-chart.js.map +1 -0
- package/dist/pie-chart/pie-legend.d.ts +60 -0
- package/dist/pie-chart/pie-legend.d.ts.map +1 -0
- package/dist/pie-chart/pie-legend.js +131 -0
- package/dist/pie-chart/pie-legend.js.map +1 -0
- package/dist/pie-chart/pie-ring.d.ts +266 -0
- package/dist/pie-chart/pie-ring.d.ts.map +1 -0
- package/dist/pie-chart/pie-ring.js +708 -0
- package/dist/pie-chart/pie-ring.js.map +1 -0
- package/dist/pie-chart/shared.d.ts +210 -0
- package/dist/pie-chart/shared.d.ts.map +1 -0
- package/dist/pie-chart/shared.js +240 -0
- package/dist/pie-chart/shared.js.map +1 -0
- package/package.json +69 -3
- package/registry.json +23 -0
- package/src/bar-chart/bar-chart.ts +804 -0
- package/src/bar-chart/category-axis.ts +177 -0
- package/src/bar-chart/chart-bars.ts +310 -0
- package/src/bar-chart/index.ts +31 -0
- package/src/bar-chart/shared.ts +354 -0
- package/src/engine.ts +26 -0
- package/src/index.ts +5 -0
- package/src/kit/chart-border.ts +61 -0
- package/src/kit/chart-canvas.ts +157 -0
- package/src/kit/chart-legend.ts +250 -0
- package/src/kit/shared.ts +675 -0
- package/src/kit/vertical-axis.ts +224 -0
- package/src/line-chart/chart-line.ts +208 -0
- package/src/line-chart/chart-region.ts +135 -0
- package/src/line-chart/horizontal-axis.ts +202 -0
- package/src/line-chart/index.ts +39 -0
- package/src/line-chart/line-chart.ts +894 -0
- package/src/line-chart/shared.ts +459 -0
- package/src/line-chart/x-scale.ts +367 -0
- package/src/nodes.ts +20 -0
- package/src/pie-chart/index.ts +37 -0
- package/src/pie-chart/pie-chart.ts +374 -0
- package/src/pie-chart/pie-legend.ts +159 -0
- package/src/pie-chart/pie-ring.ts +903 -0
- package/src/pie-chart/shared.ts +490 -0
- package/README.md +0 -4
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
command,
|
|
3
|
+
Node2D,
|
|
4
|
+
Rect,
|
|
5
|
+
Text,
|
|
6
|
+
easeOut,
|
|
7
|
+
property,
|
|
8
|
+
insetsOps,
|
|
9
|
+
textOps,
|
|
10
|
+
type CommandArgs,
|
|
11
|
+
type NodeConfig,
|
|
12
|
+
type Node2DProps,
|
|
13
|
+
} from "@motionscript/core"
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
DEFAULT_X_AXIS,
|
|
17
|
+
clampUnit,
|
|
18
|
+
enterBudget,
|
|
19
|
+
type AxisConfig,
|
|
20
|
+
} from "../kit/shared"
|
|
21
|
+
import { at, compose, hold, type Seekable } from "@motionscript/core/component"
|
|
22
|
+
|
|
23
|
+
export interface CategoryAxisProps extends Node2DProps {
|
|
24
|
+
/** One caption per category slot, left to right — the chart's rows, in order. */
|
|
25
|
+
categories: string[]
|
|
26
|
+
/** Caption/title text style and gutters — see {@link AxisConfig}. */
|
|
27
|
+
axis: AxisConfig
|
|
28
|
+
/**
|
|
29
|
+
* Caption drawn centred in the gutter beneath the category row. Empty string
|
|
30
|
+
* (the default) hides it.
|
|
31
|
+
*/
|
|
32
|
+
label: string
|
|
33
|
+
/**
|
|
34
|
+
* Per-category opacity multiplier — how the chart's category spotlight reaches
|
|
35
|
+
* the captions, so the name under a dimmed column dims with it. Missing
|
|
36
|
+
* entries read as 1.
|
|
37
|
+
*/
|
|
38
|
+
dims: number[]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** How far a caption starts above its slot when it enters, in px. */
|
|
42
|
+
const SLIDE = 24
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The **categorical** horizontal axis of a {@link BarChart}: a row of
|
|
46
|
+
* {@link Text} captions, one per row of the data, each centred on its slot
|
|
47
|
+
* beneath the baseline.
|
|
48
|
+
*
|
|
49
|
+
* Not a variant of the line chart's `HorizontalAxis` and deliberately not
|
|
50
|
+
* folded into it: that axis places markers by *value* across a numeric range,
|
|
51
|
+
* this one divides its width into equal slots and centres a name in each. The
|
|
52
|
+
* two look alike on screen and share nothing underneath — one reads a scale,
|
|
53
|
+
* the other counts rows.
|
|
54
|
+
*
|
|
55
|
+
* The slot arithmetic is the same as the plot's, because this node is laid out
|
|
56
|
+
* to exactly the plot's width (the chart reserves the axis gutter with an
|
|
57
|
+
* invisible twin of the value axis, as the line chart does), so a caption lands
|
|
58
|
+
* under the columns it names without either side being told about the other.
|
|
59
|
+
*
|
|
60
|
+
* {@link enter} staggers the captions in left-to-right, each lifted a little
|
|
61
|
+
* above its slot and transparent; the resting state is the landed one, so a
|
|
62
|
+
* chart that is never told to enter still reads as finished.
|
|
63
|
+
*/
|
|
64
|
+
export class CategoryAxis extends Node2D<CategoryAxisProps> {
|
|
65
|
+
@property({ default: () => [] as string[] }) declare categories: string[]
|
|
66
|
+
@property({ default: DEFAULT_X_AXIS }) declare axis: AxisConfig
|
|
67
|
+
@property({ default: "" }) declare label: string
|
|
68
|
+
@property({ default: () => [] as number[] }) declare dims: number[]
|
|
69
|
+
|
|
70
|
+
/** One caption per category, ordered left → right, for the staggered enter. */
|
|
71
|
+
private captionNodes: Text[] = []
|
|
72
|
+
/** The axis title, or null when `label` is empty. */
|
|
73
|
+
private labelNode: Text | null = null
|
|
74
|
+
|
|
75
|
+
/** Centre x of slot `i`, in the node's centred space. */
|
|
76
|
+
private slotCenter(index: number): number {
|
|
77
|
+
const n = this.categories.length
|
|
78
|
+
if (n <= 0) return 0
|
|
79
|
+
const width = this.layoutBounds.width
|
|
80
|
+
return -width / 2 + (width / n) * (index + 0.5)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
constructor(props?: NodeConfig<CategoryAxis, CategoryAxisProps>) {
|
|
84
|
+
super(props)
|
|
85
|
+
|
|
86
|
+
const tickPad = insetsOps.resolve(this.axis.tickPadding).top
|
|
87
|
+
const titlePad = insetsOps.resolve(this.axis.titlePadding).top
|
|
88
|
+
|
|
89
|
+
// Each caption sits in a hug box carrying the spotlight dim, so the
|
|
90
|
+
// entrance can animate the text's own opacity without the two fighting over
|
|
91
|
+
// one value — opacity multiplies down the tree, which is exactly the
|
|
92
|
+
// composition wanted here.
|
|
93
|
+
const slots = this.categories.map((caption, i) => {
|
|
94
|
+
const text = new Text({
|
|
95
|
+
...textOps.styleProps(this.axis.labelStyle),
|
|
96
|
+
text: caption,
|
|
97
|
+
})
|
|
98
|
+
this.captionNodes.push(text)
|
|
99
|
+
return new Rect({
|
|
100
|
+
width: "hug",
|
|
101
|
+
height: "hug",
|
|
102
|
+
flow: "freeform",
|
|
103
|
+
opacity: () => clampUnit(this.dims[i] ?? 1),
|
|
104
|
+
x: () => this.slotCenter(i),
|
|
105
|
+
children: [text],
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
const children: Node2D[] = [
|
|
110
|
+
new Rect({
|
|
111
|
+
width: "fill",
|
|
112
|
+
height: "hug",
|
|
113
|
+
padding: { top: tickPad },
|
|
114
|
+
children: slots,
|
|
115
|
+
}),
|
|
116
|
+
]
|
|
117
|
+
if (this.label !== "") {
|
|
118
|
+
this.labelNode = new Text({
|
|
119
|
+
...textOps.styleProps(this.axis.titleStyle),
|
|
120
|
+
text: this.label,
|
|
121
|
+
})
|
|
122
|
+
children.push(this.labelNode)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this.add(new Rect({ flow: "vertical", gap: titlePad, height: "hug", width: "fill", children }))
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Primes every caption (and the title) at its pre-entrance state: lifted above
|
|
130
|
+
* its slot and transparent. Idempotent, so the chart can arm its whole
|
|
131
|
+
* composition before the first frame is drawn.
|
|
132
|
+
*/
|
|
133
|
+
arm(slide = SLIDE): void {
|
|
134
|
+
for (const caption of this.captionNodes) caption.set({ opacity: 0, y: slide })
|
|
135
|
+
this.labelNode?.set({ opacity: 0, y: slide })
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Slide + fade every caption in from above, staggered left-to-right, then
|
|
140
|
+
* bring the title down behind them.
|
|
141
|
+
*
|
|
142
|
+
* `args.duration` is the **whole** entrance — the stagger and the title are
|
|
143
|
+
* carved out of it rather than added on top. See {@link enterBudget}.
|
|
144
|
+
*
|
|
145
|
+
* `args.data.stagger` is seconds between successive captions (derived when
|
|
146
|
+
* omitted); `args.data.slide` is the starting upward offset in px (node space
|
|
147
|
+
* is y-up, so a positive start slides *down* into place).
|
|
148
|
+
*/
|
|
149
|
+
@command()
|
|
150
|
+
enter(args: CommandArgs<{ stagger?: number; slide?: number }> & { duration: number }): Seekable {
|
|
151
|
+
const { stagger, slide = SLIDE } = args.data ?? {}
|
|
152
|
+
const easing = args.easing ?? easeOut()
|
|
153
|
+
const nodes = this.captionNodes
|
|
154
|
+
if (nodes.length === 0) return hold(args.duration)
|
|
155
|
+
const { step, per, caption } = enterBudget(
|
|
156
|
+
args.duration,
|
|
157
|
+
nodes.length,
|
|
158
|
+
this.labelNode !== null,
|
|
159
|
+
stagger
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
this.arm(slide)
|
|
163
|
+
|
|
164
|
+
// The stagger was `wait(i * step)` inside a generator, so its offsets only
|
|
165
|
+
// existed while it ran. They were always `i * step`; placed, the same
|
|
166
|
+
// entrance can be asked what it looks like partway through.
|
|
167
|
+
const lastCaptionEnds = (nodes.length - 1) * step + per
|
|
168
|
+
return compose([
|
|
169
|
+
...nodes.map((node, i) =>
|
|
170
|
+
at(i * step, node.to({ data: { opacity: 1, y: 0 }, duration: per, easing }))
|
|
171
|
+
),
|
|
172
|
+
...(this.labelNode
|
|
173
|
+
? [at(lastCaptionEnds, this.labelNode.to({ data: { opacity: 1, y: 0 }, duration: caption, easing }))]
|
|
174
|
+
: []),
|
|
175
|
+
])
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import {
|
|
2
|
+
command,
|
|
3
|
+
type AssetScope,
|
|
4
|
+
Graphics2D,
|
|
5
|
+
Node2D,
|
|
6
|
+
Text,
|
|
7
|
+
easeOut,
|
|
8
|
+
fillOps,
|
|
9
|
+
shadowOps,
|
|
10
|
+
strokeOps,
|
|
11
|
+
property,
|
|
12
|
+
textOps,
|
|
13
|
+
type Command,
|
|
14
|
+
type CommandArgs,
|
|
15
|
+
type Fill,
|
|
16
|
+
type NodeConfig,
|
|
17
|
+
type Node2DProps,
|
|
18
|
+
type RenderContext2D,
|
|
19
|
+
type Shadow,
|
|
20
|
+
type ShadowResolved,
|
|
21
|
+
type Stroke,
|
|
22
|
+
type StrokeResolved,
|
|
23
|
+
type TextStyle,
|
|
24
|
+
} from "@motionscript/core"
|
|
25
|
+
|
|
26
|
+
import { clampUnit, formatMarker } from "../kit/shared"
|
|
27
|
+
import type { MarkerLabel } from "../kit/vertical-axis"
|
|
28
|
+
import { DEFAULT_BAR_CHART_THEME } from "./shared"
|
|
29
|
+
import { declarePaints } from "@motionscript/core/component"
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* One drawn column, already mapped into the plot's **centred pixel space**
|
|
33
|
+
* (origin at the plot centre, y-up) by the parent {@link BarChart}.
|
|
34
|
+
*
|
|
35
|
+
* `from`/`to` are the two ends of the column rather than a base and a height,
|
|
36
|
+
* for the same reason {@link BarSpan} is a span: it is the one shape that fits a
|
|
37
|
+
* grouped bar, a stacked segment and a negative value without a special case.
|
|
38
|
+
*/
|
|
39
|
+
export interface BarGeometry {
|
|
40
|
+
/** Centre x of the column. */
|
|
41
|
+
x: number
|
|
42
|
+
/** Column width in px. */
|
|
43
|
+
width: number
|
|
44
|
+
/** Pixel y of the column's base — the baseline when grouped, the running stack top when stacked. */
|
|
45
|
+
from: number
|
|
46
|
+
/** Pixel y of its far end, at full {@link ChartBars.growth}. */
|
|
47
|
+
to: number
|
|
48
|
+
/** Whether the far end takes the theme's corner radius (the stack's cap). */
|
|
49
|
+
round: boolean
|
|
50
|
+
/** The value this column plots, for the figure printed on it. */
|
|
51
|
+
value: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Gap between a grouped column's end and the figure printed over it, in px. */
|
|
55
|
+
const VALUE_GAP = 10
|
|
56
|
+
/** A segment shorter than this many multiples of the type size prints no figure. */
|
|
57
|
+
const VALUE_FIT = 1.6
|
|
58
|
+
/** Fallback type size when the theme's value style leaves it unset or autofit. */
|
|
59
|
+
const VALUE_SIZE = 24
|
|
60
|
+
|
|
61
|
+
export interface ChartBarsProps extends Node2DProps {
|
|
62
|
+
/** This series' columns, one per row of the chart's data. */
|
|
63
|
+
bars: BarGeometry[]
|
|
64
|
+
/** Pixel y of value 0 — where every column grows *from*. */
|
|
65
|
+
baseline: number
|
|
66
|
+
/** Column paint — this series' own, or the theme palette's entry for its index. */
|
|
67
|
+
fill: Fill
|
|
68
|
+
/** Optional outline on every column. */
|
|
69
|
+
barStroke: Stroke
|
|
70
|
+
/** Optional drop shadow on every column. */
|
|
71
|
+
barShadow: Shadow
|
|
72
|
+
/** Radius of the far-end corners, in px. */
|
|
73
|
+
cornerRadius: number
|
|
74
|
+
/**
|
|
75
|
+
* Rise of every column in `[0, 1]`, measured **about the baseline**: 0
|
|
76
|
+
* collapses them all onto it, 1 draws them at full length.
|
|
77
|
+
*
|
|
78
|
+
* Scaling about the baseline rather than about each column's own base is what
|
|
79
|
+
* keeps a stack flush mid-animation — every segment's base and end travel by
|
|
80
|
+
* the same factor, so the segments stay stacked instead of the upper ones
|
|
81
|
+
* floating above the still-growing lower ones. It also means a negative
|
|
82
|
+
* column grows *down* out of the same signal.
|
|
83
|
+
*/
|
|
84
|
+
growth: number
|
|
85
|
+
/**
|
|
86
|
+
* Per-column opacity multiplier, by row index — how the chart's category
|
|
87
|
+
* spotlight reaches a single column without touching the series it belongs
|
|
88
|
+
* to. Missing entries read as 1.
|
|
89
|
+
*/
|
|
90
|
+
dims: number[]
|
|
91
|
+
/** Whether the figure each column plots is printed on it. */
|
|
92
|
+
showValues: boolean
|
|
93
|
+
/** Type style for those figures. */
|
|
94
|
+
valueStyle: TextStyle
|
|
95
|
+
/**
|
|
96
|
+
* Whether this series draws at all — the wholesale switch behind
|
|
97
|
+
* `BarChart.showSeries`/`hideSeries`, alongside the slot weight that animates
|
|
98
|
+
* the reflow. Off skips the columns *and* their figures.
|
|
99
|
+
*/
|
|
100
|
+
enabled: boolean
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One **series** of a {@link BarChart}, drawn as one command list: its column in
|
|
105
|
+
* every category, plus the figures printed on them.
|
|
106
|
+
*
|
|
107
|
+
* A node per series rather than a node per bar, deliberately — it is the same
|
|
108
|
+
* unit `ChartLine` is, which is what lets `showSeries`/`hideSeries`/
|
|
109
|
+
* `spotlightSeries` mean exactly the same thing on a bar chart as on a line
|
|
110
|
+
* chart: enable the node, or tween its opacity. The columns' *positions* come
|
|
111
|
+
* from the parent (it owns the slot arithmetic both modes share), so this part
|
|
112
|
+
* only has to know how to draw a span and grow it.
|
|
113
|
+
*
|
|
114
|
+
* The figures are real {@link Text} children rather than glyphs in the command
|
|
115
|
+
* list, so they take the theme's type style, are measured by the layout engine,
|
|
116
|
+
* and ride the node's own opacity — which is why hiding the series takes its
|
|
117
|
+
* figures with it for free.
|
|
118
|
+
*/
|
|
119
|
+
export class ChartBars extends Node2D<ChartBarsProps> {
|
|
120
|
+
@property({ default: () => [] as BarGeometry[] }) declare bars: BarGeometry[]
|
|
121
|
+
@property({ default: 0 }) declare baseline: number
|
|
122
|
+
@property({
|
|
123
|
+
default: [],
|
|
124
|
+
mapper: fillOps.resolve,
|
|
125
|
+
tween: fillOps.lerp,
|
|
126
|
+
})
|
|
127
|
+
declare fill: Fill
|
|
128
|
+
@property({
|
|
129
|
+
default: DEFAULT_BAR_CHART_THEME.bar.stroke,
|
|
130
|
+
mapper: strokeOps.resolve,
|
|
131
|
+
tween: strokeOps.lerp,
|
|
132
|
+
})
|
|
133
|
+
declare barStroke: Stroke
|
|
134
|
+
@property({
|
|
135
|
+
default: DEFAULT_BAR_CHART_THEME.bar.shadow,
|
|
136
|
+
mapper: shadowOps.resolve,
|
|
137
|
+
tween: shadowOps.lerp,
|
|
138
|
+
})
|
|
139
|
+
declare barShadow: Shadow
|
|
140
|
+
@property({ default: DEFAULT_BAR_CHART_THEME.bar.cornerRadius })
|
|
141
|
+
declare cornerRadius: number
|
|
142
|
+
@property({ default: 1 }) declare growth: number
|
|
143
|
+
@property({ default: () => [] as number[] }) declare dims: number[]
|
|
144
|
+
@property({ default: false }) declare showValues: boolean
|
|
145
|
+
@property({ default: DEFAULT_BAR_CHART_THEME.valueStyle })
|
|
146
|
+
declare valueStyle: TextStyle
|
|
147
|
+
@property({ default: true }) declare enabled: boolean
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* How a column's figure is written down — the same seam the two axes carry,
|
|
151
|
+
* so a chart's value axis and the numbers printed on its bars can't end up
|
|
152
|
+
* saying `$1,200` in one place and `1200` in the other.
|
|
153
|
+
*/
|
|
154
|
+
labelFor: MarkerLabel = formatMarker
|
|
155
|
+
|
|
156
|
+
constructor(props?: NodeConfig<ChartBars, ChartBarsProps>) {
|
|
157
|
+
super(props)
|
|
158
|
+
|
|
159
|
+
// One caption per column, built once (the row count is fixed at
|
|
160
|
+
// construction, like an axis's marker count) and bound to the live geometry
|
|
161
|
+
// so it travels with its column as the chart grows, rescales or reflows.
|
|
162
|
+
if (!this.showValues) return
|
|
163
|
+
for (let i = 0; i < this.bars.length; i++) {
|
|
164
|
+
this.add(
|
|
165
|
+
new Text({
|
|
166
|
+
...textOps.styleProps(this.valueStyle),
|
|
167
|
+
text: () => this.captionAt(i),
|
|
168
|
+
x: () => this.bars[i]?.x ?? 0,
|
|
169
|
+
y: () => this.captionY(i),
|
|
170
|
+
opacity: () => this.dimAt(i),
|
|
171
|
+
})
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* How far column `i` is dimmed — the chart's category spotlight, and the
|
|
178
|
+
* series switch on top of it.
|
|
179
|
+
*
|
|
180
|
+
* The captions read this rather than being hidden by `renderSelf`'s guard:
|
|
181
|
+
* they are real child nodes, so a `return` in the draw pass would leave them
|
|
182
|
+
* on screen over a series that isn't there.
|
|
183
|
+
*/
|
|
184
|
+
private dimAt(index: number): number {
|
|
185
|
+
if (!this.enabled) return 0
|
|
186
|
+
return clampUnit(this.dims[index] ?? 1)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The two ends of column `i` at the current {@link growth}. */
|
|
190
|
+
private endsAt(index: number): { base: number; end: number } | null {
|
|
191
|
+
const bar = this.bars[index]
|
|
192
|
+
if (!bar) return null
|
|
193
|
+
const g = clampUnit(this.growth)
|
|
194
|
+
const base = this.baseline + (bar.from - this.baseline) * g
|
|
195
|
+
const end = this.baseline + (bar.to - this.baseline) * g
|
|
196
|
+
return { base, end }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The type size the figures are set at, guarding the autofit/unset cases. */
|
|
200
|
+
private get valueSize(): number {
|
|
201
|
+
const size = this.valueStyle.fontSize
|
|
202
|
+
return typeof size === "number" ? size : VALUE_SIZE
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The figure printed on column `i`, or `""` where there is no room for it.
|
|
207
|
+
*
|
|
208
|
+
* Blanking the text rather than dropping the node keeps the caption list
|
|
209
|
+
* index-aligned with the columns, so a chart that rescales into more room
|
|
210
|
+
* simply starts printing again.
|
|
211
|
+
*/
|
|
212
|
+
private captionAt(index: number): string {
|
|
213
|
+
const bar = this.bars[index]
|
|
214
|
+
if (!bar || bar.value === 0) return ""
|
|
215
|
+
// An interior segment prints inside itself, so it has to be tall enough to
|
|
216
|
+
// hold a line of type; a grouped column prints in the headroom above its
|
|
217
|
+
// end, where the only constraint is the plot's own top.
|
|
218
|
+
if (this.insideAt(index)) {
|
|
219
|
+
const ends = this.endsAt(index)
|
|
220
|
+
if (!ends) return ""
|
|
221
|
+
if (Math.abs(ends.end - ends.base) < this.valueSize * VALUE_FIT) return ""
|
|
222
|
+
}
|
|
223
|
+
return this.labelFor(bar.value)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Whether column `i`'s figure sits inside the column (a stacked segment) or past its end. */
|
|
227
|
+
private insideAt(index: number): boolean {
|
|
228
|
+
const bar = this.bars[index]
|
|
229
|
+
return bar !== undefined && Math.abs(bar.from - this.baseline) > 0.5
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Where column `i`'s figure sits, in the node's centred y-up space. */
|
|
233
|
+
private captionY(index: number): number {
|
|
234
|
+
const ends = this.endsAt(index)
|
|
235
|
+
if (!ends) return 0
|
|
236
|
+
if (this.insideAt(index)) return (ends.base + ends.end) / 2
|
|
237
|
+
// Text is centred on its `y`, so clear the column's end by the gap plus half
|
|
238
|
+
// a line — on whichever side of the baseline the column actually reached.
|
|
239
|
+
const direction = ends.end >= ends.base ? 1 : -1
|
|
240
|
+
return ends.end + direction * (VALUE_GAP + this.valueSize / 2)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* This node extends `Node2D` rather than `ShapeNode`, so nothing declares the
|
|
245
|
+
* paint props it carries — see {@link declarePaints}. The column stroke and
|
|
246
|
+
* shadow are named past the conventional slots, so they are passed explicitly.
|
|
247
|
+
*/
|
|
248
|
+
override declareAssets(assets: AssetScope): void {
|
|
249
|
+
super.declareAssets(assets)
|
|
250
|
+
declarePaints(this, assets, {
|
|
251
|
+
fills: (this.barShadow as ShadowResolved[]).map((shadow) => shadow.fill),
|
|
252
|
+
strokes: [this.barStroke as StrokeResolved[]],
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
protected renderSelf(ctx: RenderContext2D): void {
|
|
257
|
+
if (!this.enabled) return
|
|
258
|
+
for (let i = 0; i < this.bars.length; i++) {
|
|
259
|
+
const bar = this.bars[i]
|
|
260
|
+
const ends = this.endsAt(i)
|
|
261
|
+
if (!ends || bar.width <= 0) continue
|
|
262
|
+
const height = Math.abs(ends.end - ends.base)
|
|
263
|
+
if (height <= 0) continue
|
|
264
|
+
|
|
265
|
+
const radius = bar.round ? Math.min(this.cornerRadius, height) : 0
|
|
266
|
+
// A column per `Graphics2D` rather than one list for the series: the
|
|
267
|
+
// category spotlight dims individual columns, and opacity is a property
|
|
268
|
+
// of the command list rather than of a shape within it.
|
|
269
|
+
const g = new Graphics2D()
|
|
270
|
+
const rect = g.rect({
|
|
271
|
+
x: bar.x,
|
|
272
|
+
y: (ends.base + ends.end) / 2,
|
|
273
|
+
width: bar.width,
|
|
274
|
+
height,
|
|
275
|
+
// This node's space is y-up, but the renderer's corner-radius keys are
|
|
276
|
+
// labelled in canvas y-down space — so a column reaching *up* rounds
|
|
277
|
+
// through the `bottom*` keys, and one reaching down through the `top*`
|
|
278
|
+
// ones. Only the far end rounds, which is what makes a stack read as a
|
|
279
|
+
// single capped column.
|
|
280
|
+
cornerRadius:
|
|
281
|
+
radius > 0
|
|
282
|
+
? ends.end >= ends.base
|
|
283
|
+
? { bottomLeft: radius, bottomRight: radius, topLeft: 0, topRight: 0 }
|
|
284
|
+
: { topLeft: radius, topRight: radius, bottomLeft: 0, bottomRight: 0 }
|
|
285
|
+
: 0,
|
|
286
|
+
})
|
|
287
|
+
rect.shadow(this.barShadow)
|
|
288
|
+
rect.fill(this.fill)
|
|
289
|
+
rect.stroke(this.barStroke)
|
|
290
|
+
ctx.draw(g.opacity(this.dimAt(i)))
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Raise every column from the baseline by tweening {@link growth} `0 → 1`.
|
|
296
|
+
*
|
|
297
|
+
* The mirror of `ChartLine.enter`: same signature, same contract that the
|
|
298
|
+
* resting state is the finished one, so the chart arms this back to 0 before
|
|
299
|
+
* animating rather than the node hiding itself at construction.
|
|
300
|
+
*/
|
|
301
|
+
@command()
|
|
302
|
+
enter(args: CommandArgs<Record<string, never>> & { duration: number }): Command<ChartBarsGrowth> {
|
|
303
|
+
// No priming to 0 first: the value at `t` is a function of `t`, so a repeat
|
|
304
|
+
// call re-raises by definition rather than by having reset something.
|
|
305
|
+
return this.command<ChartBarsGrowth>((t) => ({ growth: t }), args.duration, args.easing ?? easeOut())
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** The one signal {@link ChartBars.enter} moves. */
|
|
310
|
+
type ChartBarsGrowth = { growth: number }
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bar chart: grouped or stacked columns off a category axis.
|
|
3
|
+
*
|
|
4
|
+
* Shares the plot panel, the value axis and the legend with the line chart —
|
|
5
|
+
* which is what lets a scene swap one for the other without re-theming — and
|
|
6
|
+
* brings its own category axis, since a band scale is not a continuous one.
|
|
7
|
+
*/
|
|
8
|
+
export { BarChart } from "./bar-chart"
|
|
9
|
+
export type { BarChartProps } from "./bar-chart"
|
|
10
|
+
export { ChartBars } from "./chart-bars"
|
|
11
|
+
export type { BarGeometry, ChartBarsProps } from "./chart-bars"
|
|
12
|
+
export { CategoryAxis } from "./category-axis"
|
|
13
|
+
export type { CategoryAxisProps } from "./category-axis"
|
|
14
|
+
export {
|
|
15
|
+
DEFAULT_BAR_CHART_THEME,
|
|
16
|
+
barCategories,
|
|
17
|
+
barSpans,
|
|
18
|
+
barValueExtent,
|
|
19
|
+
lerpBarChartTheme,
|
|
20
|
+
resolveBarChartTheme,
|
|
21
|
+
resolveBarFill,
|
|
22
|
+
resolveBarScale,
|
|
23
|
+
stackCapIndex,
|
|
24
|
+
} from "./shared"
|
|
25
|
+
export type {
|
|
26
|
+
BarChartConfig,
|
|
27
|
+
BarChartTheme,
|
|
28
|
+
BarSeries,
|
|
29
|
+
BarSpan,
|
|
30
|
+
BarStyleConfig,
|
|
31
|
+
} from "./shared"
|