@lostgradient/cinder 0.22.0 → 0.23.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 +63 -0
- package/components.json +1 -1
- package/dist/_internal/chart/chart-bar-model.d.ts +24 -0
- package/dist/_internal/chart/chart-cartesian-model.d.ts +16 -0
- package/dist/_internal/chart/chart-layout.d.ts +23 -0
- package/dist/_internal/chart/chart-model-utilities.d.ts +157 -0
- package/dist/_internal/chart/chart-scale.d.ts +37 -0
- package/dist/_internal/chart/chart-utilities.d.ts +7 -164
- package/dist/_internal/chart/heatmap-utilities.d.ts +5 -4
- package/dist/components/_internal/chart-tooltip.css +21 -0
- package/dist/components/_internal/chart-tooltip.svelte.d.ts +11 -0
- package/dist/components/approval-card/index.js +5 -5
- package/dist/components/area-chart/area-chart.css +27 -23
- package/dist/components/area-chart/area-chart.schema.js +38 -1
- package/dist/components/area-chart/area-chart.types.d.ts +6 -2
- package/dist/components/area-chart/index.js +1589 -768
- package/dist/components/badge/badge.css +2 -2
- package/dist/components/badge/badge.schema.js +2 -2
- package/dist/components/badge/badge.types.d.ts +3 -3
- package/dist/components/badge/index.js +4 -4
- package/dist/components/bar-chart/bar-chart.css +27 -21
- package/dist/components/bar-chart/bar-chart.schema.js +38 -1
- package/dist/components/bar-chart/bar-chart.types.d.ts +7 -3
- package/dist/components/bar-chart/index.js +1544 -740
- package/dist/components/chart.types.d.ts +98 -2
- package/dist/components/json-schema-editor/index.js +4 -4
- package/dist/components/line-chart/index.js +1587 -785
- package/dist/components/line-chart/line-chart.css +25 -24
- package/dist/components/line-chart/line-chart.schema.js +38 -1
- package/dist/components/line-chart/line-chart.types.d.ts +6 -2
- package/dist/components/matrix-chart/index.js +1038 -429
- package/dist/components/matrix-chart/matrix-chart.css +12 -6
- package/dist/components/matrix-chart/matrix-chart.schema.js +26 -1
- package/dist/components/matrix-chart/matrix-chart.types.d.ts +5 -1
- package/dist/components/payload-inspector/index.js +4 -4
- package/dist/components/run-step-timeline/index.js +6 -6
- package/dist/components/sparkbar/index.js +1291 -4
- package/dist/components/sparkbar/sparkbar.css +5 -4
- package/dist/components/sparkbar/sparkbar.schema.js +26 -1
- package/dist/components/sparkbar/sparkbar.types.d.ts +5 -0
- package/dist/components/spectrogram/index.js +1076 -437
- package/dist/components/spectrogram/spectrogram.css +7 -6
- package/dist/components/spectrogram/spectrogram.schema.js +26 -1
- package/dist/components/spectrogram/spectrogram.types.d.ts +5 -1
- package/dist/components/spectrum-chart/index.js +1037 -423
- package/dist/components/spectrum-chart/spectrum-chart.css +8 -7
- package/dist/components/spectrum-chart/spectrum-chart.schema.js +26 -1
- package/dist/components/spectrum-chart/spectrum-chart.types.d.ts +5 -1
- package/dist/components/statistic/index.js +1288 -2
- package/dist/components/statistic/statistic.css +6 -5
- package/dist/components/statistic/statistic.schema.js +26 -1
- package/dist/components/statistic/statistic.types.d.ts +5 -0
- package/dist/components/statistic-group/index.js +1288 -2
- package/dist/components/waveform/index.js +1004 -404
- package/dist/components/waveform/waveform.css +8 -7
- package/dist/components/waveform/waveform.schema.js +26 -1
- package/dist/components/waveform/waveform.types.d.ts +5 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +17734 -16540
- package/dist/server/components/approval-card/index.js +3 -3
- package/dist/server/components/area-chart/index.js +3 -3
- package/dist/server/components/badge/index.js +2 -2
- package/dist/server/components/bar-chart/index.js +3 -3
- package/dist/server/components/json-schema-editor/index.js +2 -2
- package/dist/server/components/line-chart/index.js +3 -3
- package/dist/server/components/matrix-chart/index.js +3 -3
- package/dist/server/components/payload-inspector/index.js +3 -3
- package/dist/server/components/run-step-timeline/index.js +2 -2
- package/dist/server/components/sparkbar/index.js +3 -2
- package/dist/server/components/spectrogram/index.js +3 -3
- package/dist/server/components/spectrum-chart/index.js +2 -2
- package/dist/server/components/statistic/index.js +4 -3
- package/dist/server/components/statistic-group/index.js +4 -3
- package/dist/server/components/waveform/index.js +2 -2
- package/dist/server/index.js +21 -21
- package/dist/server/{index.server-3dkwzkd3.js → index.server-10r7yf13.js} +110 -27
- package/dist/server/{index.server-pp2n6def.js → index.server-2y0xz53g.js} +37 -14
- package/dist/server/{index.server-bd8rcgf0.js → index.server-4hvjet4z.js} +1 -1
- package/dist/server/{index.server-bsm39gpy.js → index.server-5d83fkyg.js} +75 -21
- package/dist/server/{index.server-abqbstkt.js → index.server-9gj583qs.js} +41 -14
- package/dist/server/{index.server-2nmwsacy.js → index.server-9scm7zf9.js} +4 -4
- package/dist/server/{index.server-5j6jv7rm.js → index.server-9vwxyy06.js} +108 -29
- package/dist/server/{index.server-abjsjsw8.js → index.server-afpjvfz1.js} +111 -35
- package/dist/server/{index.server-wm8957be.js → index.server-bp0vzxby.js} +14 -1
- package/dist/server/{index.server-86sw5ea6.js → index.server-dw9bz17h.js} +4 -4
- package/dist/server/{index.server-d26jzn2m.js → index.server-en31rprd.js} +3 -3
- package/dist/server/{index.server-dx58qant.js → index.server-f3a0ecqg.js} +72 -9
- package/dist/server/{index.server-d63pwytk.js → index.server-kz8aeeq5.js} +18 -10
- package/dist/server/index.server-mmmrgvyd.js +1274 -0
- package/dist/server/{index.server-93943z3e.js → index.server-rx9aszk6.js} +1 -1
- package/dist/server/{index.server-1221p7e4.js → index.server-scahngwv.js} +13 -1
- package/dist/server/{index.server-nj8kk5bv.js → index.server-t73q59c1.js} +1 -1
- package/dist/server/{index.server-5schb2vt.js → index.server-tdrncj5e.js} +16 -6
- package/dist/server/{index.server-4wweyb2e.js → index.server-vnr1h5g5.js} +1 -1
- package/dist/server/{index.server-fphvzcfb.js → index.server-wqajghn9.js} +1 -1
- package/dist/server/{index.server-yd9shwqq.js → index.server-zjcjzcv6.js} +1 -1
- package/package.json +1 -1
- package/src/_internal/chart/chart-bar-model.ts +330 -0
- package/src/_internal/chart/chart-cartesian-model.ts +450 -0
- package/src/_internal/chart/chart-interaction.svelte.ts +25 -6
- package/src/_internal/chart/chart-layout.ts +348 -0
- package/src/_internal/chart/chart-model-utilities.ts +357 -0
- package/src/_internal/chart/chart-scale.ts +487 -0
- package/src/_internal/chart/chart-utilities.ts +22 -1078
- package/src/_internal/chart/heatmap-utilities.ts +21 -17
- package/src/components/_internal/chart-tooltip.css +21 -0
- package/src/components/_internal/chart-tooltip.svelte +44 -0
- package/src/components/approval-card/approval-card.svelte +1 -1
- package/src/components/area-chart/area-chart.css +27 -23
- package/src/components/area-chart/area-chart.examples.json +1 -1
- package/src/components/area-chart/area-chart.schema.json +37 -0
- package/src/components/area-chart/area-chart.schema.ts +40 -0
- package/src/components/area-chart/area-chart.svelte +103 -35
- package/src/components/area-chart/area-chart.types.ts +7 -1
- package/src/components/badge/badge.css +2 -2
- package/src/components/badge/badge.schema.json +1 -1
- package/src/components/badge/badge.schema.ts +1 -1
- package/src/components/badge/badge.svelte +2 -2
- package/src/components/badge/badge.types.ts +3 -3
- package/src/components/bar-chart/bar-chart.css +27 -21
- package/src/components/bar-chart/bar-chart.schema.json +37 -0
- package/src/components/bar-chart/bar-chart.schema.ts +40 -0
- package/src/components/bar-chart/bar-chart.svelte +94 -33
- package/src/components/bar-chart/bar-chart.types.ts +9 -2
- package/src/components/chart.types.ts +112 -2
- package/src/components/line-chart/line-chart.css +25 -24
- package/src/components/line-chart/line-chart.examples.json +1 -1
- package/src/components/line-chart/line-chart.schema.json +37 -0
- package/src/components/line-chart/line-chart.schema.ts +40 -0
- package/src/components/line-chart/line-chart.svelte +95 -40
- package/src/components/line-chart/line-chart.types.ts +7 -1
- package/src/components/matrix-chart/matrix-chart.css +12 -6
- package/src/components/matrix-chart/matrix-chart.schema.json +25 -0
- package/src/components/matrix-chart/matrix-chart.schema.ts +25 -0
- package/src/components/matrix-chart/matrix-chart.svelte +30 -12
- package/src/components/matrix-chart/matrix-chart.types.ts +5 -1
- package/src/components/run-step-timeline/run-step-timeline.svelte +2 -2
- package/src/components/sparkbar/sparkbar.css +5 -4
- package/src/components/sparkbar/sparkbar.schema.json +25 -0
- package/src/components/sparkbar/sparkbar.schema.ts +26 -0
- package/src/components/sparkbar/sparkbar.svelte +8 -1
- package/src/components/sparkbar/sparkbar.types.ts +5 -0
- package/src/components/spectrogram/spectrogram.css +7 -6
- package/src/components/spectrogram/spectrogram.schema.json +25 -0
- package/src/components/spectrogram/spectrogram.schema.ts +25 -0
- package/src/components/spectrogram/spectrogram.svelte +73 -28
- package/src/components/spectrogram/spectrogram.types.ts +5 -1
- package/src/components/spectrum-chart/spectrum-chart.css +8 -7
- package/src/components/spectrum-chart/spectrum-chart.schema.json +25 -0
- package/src/components/spectrum-chart/spectrum-chart.schema.ts +26 -0
- package/src/components/spectrum-chart/spectrum-chart.svelte +38 -12
- package/src/components/spectrum-chart/spectrum-chart.types.ts +5 -1
- package/src/components/statistic/statistic.css +6 -5
- package/src/components/statistic/statistic.schema.json +25 -0
- package/src/components/statistic/statistic.schema.ts +26 -0
- package/src/components/statistic/statistic.svelte +42 -1
- package/src/components/statistic/statistic.types.ts +5 -0
- package/src/components/waveform/waveform.css +8 -7
- package/src/components/waveform/waveform.schema.json +25 -0
- package/src/components/waveform/waveform.schema.ts +26 -0
- package/src/components/waveform/waveform.svelte +16 -3
- package/src/components/waveform/waveform.types.ts +5 -1
- package/src/index.ts +8 -0
- package/dist/server/index.server-6vbpz6dd.js +0 -690
|
@@ -1,1078 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
'var(--cinder-chart-series-8)',
|
|
24
|
-
] as const;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Resolves a palette color for a series by index, wrapping around the palette.
|
|
28
|
-
* Exported so chart components can render legend swatches using the same
|
|
29
|
-
* resolved color the chart uses for the series itself.
|
|
30
|
-
*/
|
|
31
|
-
export function chartPaletteColor(index: number): string {
|
|
32
|
-
return chartPalette[index % chartPalette.length] ?? 'var(--cinder-chart-series-1)';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type NormalizedXValue = {
|
|
36
|
-
raw: ChartXValue;
|
|
37
|
-
key: string;
|
|
38
|
-
label: string;
|
|
39
|
-
comparable: string | number;
|
|
40
|
-
kind: 'string' | 'number' | 'date';
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export type NormalizedPoint = {
|
|
44
|
-
seriesId: string;
|
|
45
|
-
seriesLabel: string;
|
|
46
|
-
color: string;
|
|
47
|
-
x: NormalizedXValue;
|
|
48
|
-
y: number | null;
|
|
49
|
-
originalY: ChartNumericValue;
|
|
50
|
-
index: number;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* A point that has been placed in pixel space by the chart model. Components
|
|
55
|
-
* read `pixelX`/`pixelY` directly without a secondary lookup against targets.
|
|
56
|
-
*/
|
|
57
|
-
export type PlacedPoint = NormalizedPoint & {
|
|
58
|
-
pixelX: number;
|
|
59
|
-
pixelY: number;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export type ChartTarget = {
|
|
63
|
-
id: string;
|
|
64
|
-
seriesId: string;
|
|
65
|
-
seriesLabel: string;
|
|
66
|
-
xLabel: string;
|
|
67
|
-
valueLabel: string;
|
|
68
|
-
x: number;
|
|
69
|
-
y: number;
|
|
70
|
-
width?: number;
|
|
71
|
-
height?: number;
|
|
72
|
-
color: string;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export type ChartGeometry = {
|
|
76
|
-
plotWidth: number;
|
|
77
|
-
plotHeight: number;
|
|
78
|
-
marginTop: number;
|
|
79
|
-
marginRight: number;
|
|
80
|
-
marginBottom: number;
|
|
81
|
-
marginLeft: number;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* An x-axis tick paired with its already-scaled pixel position. Charts render
|
|
86
|
-
* labels at the model-provided `x` so labels and points stay aligned for
|
|
87
|
-
* numeric and date domains.
|
|
88
|
-
*/
|
|
89
|
-
export type ChartXTick = {
|
|
90
|
-
label: string;
|
|
91
|
-
x: number;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
export type CartesianChartModel = {
|
|
95
|
-
geometry: ChartGeometry;
|
|
96
|
-
/** Pre-scaled x-axis ticks. Render labels at `tick.x`, not by ordinal index. */
|
|
97
|
-
xTicks: ChartXTick[];
|
|
98
|
-
yTicks: number[];
|
|
99
|
-
normalizedSeries: Array<{
|
|
100
|
-
id: string;
|
|
101
|
-
label: string;
|
|
102
|
-
color: string;
|
|
103
|
-
points: PlacedPoint[];
|
|
104
|
-
path: string;
|
|
105
|
-
areaPath: string;
|
|
106
|
-
hidden: boolean;
|
|
107
|
-
}>;
|
|
108
|
-
tableRows: Array<{
|
|
109
|
-
id: string;
|
|
110
|
-
seriesLabel: string;
|
|
111
|
-
xLabel: string;
|
|
112
|
-
valueLabel: string;
|
|
113
|
-
}>;
|
|
114
|
-
/** Targets sorted by `x` (binary-search precondition for nearestTarget). */
|
|
115
|
-
targets: ChartTarget[];
|
|
116
|
-
empty: boolean;
|
|
117
|
-
yDomain: [number, number];
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
export type BarChartModel = {
|
|
121
|
-
geometry: ChartGeometry;
|
|
122
|
-
categories: NormalizedXValue[];
|
|
123
|
-
yTicks: number[];
|
|
124
|
-
categoryTicks: Array<{
|
|
125
|
-
categoryKey: string;
|
|
126
|
-
label: string;
|
|
127
|
-
fullLabel: string;
|
|
128
|
-
x: number;
|
|
129
|
-
y: number;
|
|
130
|
-
}>;
|
|
131
|
-
bars: Array<{
|
|
132
|
-
id: string;
|
|
133
|
-
seriesId: string;
|
|
134
|
-
seriesLabel: string;
|
|
135
|
-
categoryLabel: string;
|
|
136
|
-
valueLabel: string;
|
|
137
|
-
x: number;
|
|
138
|
-
y: number;
|
|
139
|
-
width: number;
|
|
140
|
-
height: number;
|
|
141
|
-
color: string;
|
|
142
|
-
hidden: boolean;
|
|
143
|
-
}>;
|
|
144
|
-
tableRows: Array<{
|
|
145
|
-
categoryKey: string;
|
|
146
|
-
categoryLabel: string;
|
|
147
|
-
values: Array<{ seriesId: string; seriesLabel: string; valueLabel: string }>;
|
|
148
|
-
}>;
|
|
149
|
-
targets: ChartTarget[];
|
|
150
|
-
empty: boolean;
|
|
151
|
-
valueDomain: [number, number];
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
export function assertValidChartNumber(
|
|
155
|
-
componentId: string,
|
|
156
|
-
rule: string,
|
|
157
|
-
value: number,
|
|
158
|
-
label: string,
|
|
159
|
-
): void {
|
|
160
|
-
if (!Number.isFinite(value) || value <= 0) {
|
|
161
|
-
throw new Error(
|
|
162
|
-
`[cinder/${componentId}] rule=${rule} ${label}="${value}": expected a positive finite number.`,
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export function assertValidNonNegativeInteger(
|
|
168
|
-
componentId: string,
|
|
169
|
-
rule: string,
|
|
170
|
-
value: number,
|
|
171
|
-
label: string,
|
|
172
|
-
): void {
|
|
173
|
-
if (!Number.isInteger(value) || value < 0) {
|
|
174
|
-
throw new Error(
|
|
175
|
-
`[cinder/${componentId}] rule=${rule} ${label}="${value}": expected a non-negative integer.`,
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export function assertValidTickCount(componentId: string, axis?: ChartAxisConfiguration): void {
|
|
181
|
-
if (axis?.tickCount === undefined) return;
|
|
182
|
-
if (!Number.isInteger(axis.tickCount) || axis.tickCount <= 0) {
|
|
183
|
-
throw new Error(
|
|
184
|
-
`[cinder/${componentId}] rule=invalid-tick-count tickCount="${axis.tickCount}": expected a positive integer.`,
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export function dataTableClass(visibility: ChartDataTableVisibility): string | undefined {
|
|
190
|
-
return visibility === 'screen-reader-only' ? 'cinder-sr-only' : undefined;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export function formatXValue(
|
|
194
|
-
value: NormalizedXValue,
|
|
195
|
-
axis: ChartAxisConfiguration | undefined,
|
|
196
|
-
context: ChartFormatterContext,
|
|
197
|
-
): string {
|
|
198
|
-
return axis?.format ? axis.format(value.raw, context) : value.label;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
export function formatNumericValue(
|
|
202
|
-
value: number,
|
|
203
|
-
axis: ChartAxisConfiguration | undefined,
|
|
204
|
-
formatter: ((value: number, context: ChartFormatterContext) => string) | undefined,
|
|
205
|
-
context: ChartFormatterContext,
|
|
206
|
-
): string {
|
|
207
|
-
if (formatter) return formatter(value, context);
|
|
208
|
-
if (axis?.format) return axis.format(value, context);
|
|
209
|
-
// Locale `undefined` inherits the browser's current locale rather than
|
|
210
|
-
// baking en-US formatting into every consumer.
|
|
211
|
-
return new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 }).format(value);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function normalizeXValue(value: ChartXValue): NormalizedXValue {
|
|
215
|
-
if (value instanceof Date) {
|
|
216
|
-
const comparable = value.getTime();
|
|
217
|
-
return {
|
|
218
|
-
raw: value,
|
|
219
|
-
key: `date:${comparable}`,
|
|
220
|
-
label: value.toISOString(),
|
|
221
|
-
comparable,
|
|
222
|
-
kind: 'date',
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
if (typeof value === 'number') {
|
|
226
|
-
return {
|
|
227
|
-
raw: value,
|
|
228
|
-
key: `number:${value}`,
|
|
229
|
-
label: String(value),
|
|
230
|
-
comparable: value,
|
|
231
|
-
kind: 'number',
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
return { raw: value, key: `string:${value}`, label: value, comparable: value, kind: 'string' };
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export function assertUniqueSeriesIds(componentId: string, series: Array<{ id: string }>): void {
|
|
238
|
-
const seen = new Set<string>();
|
|
239
|
-
for (const item of series) {
|
|
240
|
-
if (seen.has(item.id)) {
|
|
241
|
-
throw new Error(
|
|
242
|
-
`[cinder/${componentId}] rule=duplicate-series-id series="${item.id}": duplicate series ids are not supported.`,
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
seen.add(item.id);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
export function toggleSeriesId(hiddenSeriesIds: string[], seriesId: string): string[] {
|
|
250
|
-
return hiddenSeriesIds.includes(seriesId)
|
|
251
|
-
? hiddenSeriesIds.filter((id) => id !== seriesId)
|
|
252
|
-
: [...hiddenSeriesIds, seriesId];
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export function createCartesianModel(options: {
|
|
256
|
-
componentId: 'line-chart' | 'area-chart';
|
|
257
|
-
series: ChartCartesianSeries[];
|
|
258
|
-
hiddenSeriesIds: string[];
|
|
259
|
-
width: number;
|
|
260
|
-
height: number;
|
|
261
|
-
xAxis?: ChartAxisConfiguration | undefined;
|
|
262
|
-
yAxis?: ChartAxisConfiguration | undefined;
|
|
263
|
-
stackedArea?: boolean;
|
|
264
|
-
}): CartesianChartModel {
|
|
265
|
-
const {
|
|
266
|
-
componentId,
|
|
267
|
-
series,
|
|
268
|
-
hiddenSeriesIds,
|
|
269
|
-
width,
|
|
270
|
-
height,
|
|
271
|
-
xAxis,
|
|
272
|
-
yAxis,
|
|
273
|
-
stackedArea = false,
|
|
274
|
-
} = options;
|
|
275
|
-
assertUniqueSeriesIds(componentId, series);
|
|
276
|
-
assertValidChartNumber(componentId, 'invalid-height', height, 'height');
|
|
277
|
-
assertValidTickCount(componentId, xAxis);
|
|
278
|
-
assertValidTickCount(componentId, yAxis);
|
|
279
|
-
|
|
280
|
-
const geometry = createGeometry(width, height);
|
|
281
|
-
const allKinds = new Set<string>();
|
|
282
|
-
const xValuesByKey = new Map<string, NormalizedXValue>();
|
|
283
|
-
|
|
284
|
-
const normalizedSeries = series.map((item, seriesIndex) => {
|
|
285
|
-
const seenX = new Set<string>();
|
|
286
|
-
const color = item.color ?? chartPaletteColor(seriesIndex);
|
|
287
|
-
const points: NormalizedPoint[] = item.data.map((point, pointIndex) => {
|
|
288
|
-
const x = normalizeXValue(point.x);
|
|
289
|
-
allKinds.add(x.kind);
|
|
290
|
-
if (seenX.has(x.key)) {
|
|
291
|
-
throw new Error(
|
|
292
|
-
`[cinder/${componentId}] rule=duplicate-x series="${item.id}" x="${x.label}": duplicate x values are not supported.`,
|
|
293
|
-
);
|
|
294
|
-
}
|
|
295
|
-
seenX.add(x.key);
|
|
296
|
-
xValuesByKey.set(x.key, x);
|
|
297
|
-
const y = normalizeNumericValue(componentId, item.id, x.label, point.y);
|
|
298
|
-
return {
|
|
299
|
-
seriesId: item.id,
|
|
300
|
-
seriesLabel: item.label,
|
|
301
|
-
color,
|
|
302
|
-
x,
|
|
303
|
-
y,
|
|
304
|
-
originalY: point.y,
|
|
305
|
-
index: pointIndex,
|
|
306
|
-
};
|
|
307
|
-
});
|
|
308
|
-
return { id: item.id, label: item.label, color, points };
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
if (allKinds.size > 1) {
|
|
312
|
-
throw new Error(
|
|
313
|
-
`[cinder/${componentId}] rule=mixed-x-domain-kind: all x values must share one domain kind.`,
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (stackedArea) {
|
|
318
|
-
for (const item of normalizedSeries) {
|
|
319
|
-
for (const point of item.points) {
|
|
320
|
-
if ((point.y ?? 0) < 0) {
|
|
321
|
-
throw new Error(
|
|
322
|
-
`[cinder/${componentId}] rule=negative-stacked-area series="${item.id}" x="${point.x.label}": stacked areas do not support negative values.`,
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
const sortedXValues = sortXValues([...xValuesByKey.values()]);
|
|
330
|
-
// Index by key so we can preserve canonical x-domain order when sorting a
|
|
331
|
-
// series' own points (string domains use insertion order, numeric/date sort
|
|
332
|
-
// by `comparable`). Avoids `Number(stringKey)` returning NaN.
|
|
333
|
-
const orderByKey = new Map(sortedXValues.map((value, index) => [value.key, index]));
|
|
334
|
-
|
|
335
|
-
// Visible-only domain values. Hidden series no longer compress the visible
|
|
336
|
-
// chart against invisible data, and the legend toggle's effect on scale is
|
|
337
|
-
// consistent with the table and targets.
|
|
338
|
-
const visibleNumericValues: number[] = [];
|
|
339
|
-
for (const item of normalizedSeries) {
|
|
340
|
-
if (hiddenSeriesIds.includes(item.id)) continue;
|
|
341
|
-
for (const point of item.points) {
|
|
342
|
-
if (point.y !== null) visibleNumericValues.push(point.y);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const stackedTotalsByKey = new Map(sortedXValues.map((value) => [value.key, 0]));
|
|
347
|
-
if (stackedArea) {
|
|
348
|
-
for (const item of normalizedSeries) {
|
|
349
|
-
if (hiddenSeriesIds.includes(item.id)) continue;
|
|
350
|
-
for (const point of item.points) {
|
|
351
|
-
if (point.y === null) continue;
|
|
352
|
-
stackedTotalsByKey.set(point.x.key, (stackedTotalsByKey.get(point.x.key) ?? 0) + point.y);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
const domainValues = stackedArea ? [0, ...stackedTotalsByKey.values()] : visibleNumericValues;
|
|
357
|
-
const [yMinimum, yMaximum] = createPaddedDomain(domainValues);
|
|
358
|
-
|
|
359
|
-
// Split scales into two correctly-typed variables so the use site can
|
|
360
|
-
// discriminate on `kind` without `as` casts.
|
|
361
|
-
const xStringScale: BandlikeScale | undefined =
|
|
362
|
-
sortedXValues[0]?.kind === 'string'
|
|
363
|
-
? createPointScale(
|
|
364
|
-
sortedXValues.map((value) => value.key),
|
|
365
|
-
[0, geometry.plotWidth],
|
|
366
|
-
0.5,
|
|
367
|
-
)
|
|
368
|
-
: undefined;
|
|
369
|
-
const xNumericScale: LinearScale | undefined =
|
|
370
|
-
sortedXValues[0] && sortedXValues[0].kind !== 'string'
|
|
371
|
-
? createLinearScale(createNumericDomain(sortedXValues), [0, geometry.plotWidth])
|
|
372
|
-
: undefined;
|
|
373
|
-
const yScale = createLinearScale([yMinimum, yMaximum], [geometry.plotHeight, 0]);
|
|
374
|
-
|
|
375
|
-
function scaleX(value: NormalizedXValue): number {
|
|
376
|
-
if (value.kind === 'string') return xStringScale?.(value.key) ?? 0;
|
|
377
|
-
return xNumericScale?.(Number(value.comparable)) ?? 0;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// Build x-axis ticks placed at their true scaled positions so labels and
|
|
381
|
-
// points line up for numeric and date domains.
|
|
382
|
-
const tickCount = xAxis?.tickCount ?? sortedXValues.length;
|
|
383
|
-
const xTicks: ChartXTick[] = buildXAxisTicks(sortedXValues, tickCount, xAxis, scaleX);
|
|
384
|
-
|
|
385
|
-
const targets: ChartTarget[] = [];
|
|
386
|
-
const tableRows: CartesianChartModel['tableRows'] = [];
|
|
387
|
-
const stackedOffsetsByKey = new Map(sortedXValues.map((value) => [value.key, 0]));
|
|
388
|
-
const renderedSeries = normalizedSeries.map((item) => {
|
|
389
|
-
const hidden = hiddenSeriesIds.includes(item.id);
|
|
390
|
-
const points = item.points
|
|
391
|
-
.filter((point) => point.y !== null)
|
|
392
|
-
.toSorted((a, b) => (orderByKey.get(a.x.key) ?? 0) - (orderByKey.get(b.x.key) ?? 0));
|
|
393
|
-
|
|
394
|
-
const placedPoints: PlacedPoint[] = points.map((point) => {
|
|
395
|
-
const lowerValue = stackedArea ? (stackedOffsetsByKey.get(point.x.key) ?? 0) : 0;
|
|
396
|
-
const upperValue = lowerValue + (point.y ?? 0);
|
|
397
|
-
return {
|
|
398
|
-
...point,
|
|
399
|
-
pixelX: scaleX(point.x),
|
|
400
|
-
pixelY: yScale(stackedArea ? upperValue : (point.y ?? 0)),
|
|
401
|
-
};
|
|
402
|
-
});
|
|
403
|
-
const coordinates = placedPoints.map((placed) => ({
|
|
404
|
-
x: placed.pixelX,
|
|
405
|
-
y: placed.pixelY,
|
|
406
|
-
y0: yScale(stackedArea ? (stackedOffsetsByKey.get(placed.x.key) ?? 0) : 0),
|
|
407
|
-
}));
|
|
408
|
-
|
|
409
|
-
if (!hidden) {
|
|
410
|
-
for (const placed of placedPoints) {
|
|
411
|
-
const xLabel = formatXValue(placed.x, xAxis, {
|
|
412
|
-
seriesId: item.id,
|
|
413
|
-
seriesLabel: item.label,
|
|
414
|
-
index: placed.index,
|
|
415
|
-
});
|
|
416
|
-
const valueLabel = formatNumericValue(
|
|
417
|
-
placed.y ?? 0,
|
|
418
|
-
yAxis,
|
|
419
|
-
series.find((entry) => entry.id === item.id)?.valueFormatter,
|
|
420
|
-
{ seriesId: item.id, seriesLabel: item.label, index: placed.index },
|
|
421
|
-
);
|
|
422
|
-
targets.push({
|
|
423
|
-
id: `${item.id}-${placed.x.key}`,
|
|
424
|
-
seriesId: item.id,
|
|
425
|
-
seriesLabel: item.label,
|
|
426
|
-
xLabel,
|
|
427
|
-
valueLabel,
|
|
428
|
-
x: placed.pixelX,
|
|
429
|
-
y: placed.pixelY,
|
|
430
|
-
color: item.color,
|
|
431
|
-
});
|
|
432
|
-
tableRows.push({
|
|
433
|
-
id: `${item.id}-${placed.x.key}`,
|
|
434
|
-
seriesLabel: item.label,
|
|
435
|
-
xLabel,
|
|
436
|
-
valueLabel,
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
if (stackedArea) {
|
|
440
|
-
for (const placed of placedPoints) {
|
|
441
|
-
stackedOffsetsByKey.set(
|
|
442
|
-
placed.x.key,
|
|
443
|
-
(stackedOffsetsByKey.get(placed.x.key) ?? 0) + (placed.y ?? 0),
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
return {
|
|
449
|
-
...item,
|
|
450
|
-
hidden,
|
|
451
|
-
points: hidden ? [] : placedPoints,
|
|
452
|
-
path: hidden ? '' : createLinePath(coordinates),
|
|
453
|
-
areaPath: hidden
|
|
454
|
-
? ''
|
|
455
|
-
: createAreaPath(coordinates, stackedArea ? undefined : geometry.plotHeight),
|
|
456
|
-
};
|
|
457
|
-
});
|
|
458
|
-
|
|
459
|
-
targets.sort((a, b) => a.x - b.x);
|
|
460
|
-
|
|
461
|
-
return {
|
|
462
|
-
geometry,
|
|
463
|
-
xTicks,
|
|
464
|
-
yTicks: yScale.ticks(yAxis?.tickCount ?? 5),
|
|
465
|
-
normalizedSeries: renderedSeries,
|
|
466
|
-
tableRows,
|
|
467
|
-
targets,
|
|
468
|
-
empty: targets.length === 0,
|
|
469
|
-
yDomain: [yMinimum, yMaximum],
|
|
470
|
-
};
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
function buildXAxisTicks(
|
|
474
|
-
sortedXValues: NormalizedXValue[],
|
|
475
|
-
tickCount: number,
|
|
476
|
-
xAxis: ChartAxisConfiguration | undefined,
|
|
477
|
-
scaleX: (value: NormalizedXValue) => number,
|
|
478
|
-
): ChartXTick[] {
|
|
479
|
-
if (sortedXValues.length === 0) return [];
|
|
480
|
-
const safeTickCount = Math.max(1, Math.min(tickCount, sortedXValues.length));
|
|
481
|
-
if (safeTickCount >= sortedXValues.length) {
|
|
482
|
-
return sortedXValues.map((value, index) => ({
|
|
483
|
-
label: formatXValue(value, xAxis, { index }),
|
|
484
|
-
x: scaleX(value),
|
|
485
|
-
}));
|
|
486
|
-
}
|
|
487
|
-
if (safeTickCount === 1) {
|
|
488
|
-
const value = sortedXValues[0];
|
|
489
|
-
return value ? [{ label: formatXValue(value, xAxis, { index: 0 }), x: scaleX(value) }] : [];
|
|
490
|
-
}
|
|
491
|
-
// Sample evenly across the sorted x values.
|
|
492
|
-
const step = (sortedXValues.length - 1) / (safeTickCount - 1);
|
|
493
|
-
const ticks: ChartXTick[] = [];
|
|
494
|
-
for (let i = 0; i < safeTickCount; i++) {
|
|
495
|
-
const sourceIndex = Math.round(i * step);
|
|
496
|
-
const value = sortedXValues[sourceIndex];
|
|
497
|
-
if (!value) continue;
|
|
498
|
-
ticks.push({
|
|
499
|
-
label: formatXValue(value, xAxis, { index: i }),
|
|
500
|
-
x: scaleX(value),
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
return ticks;
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
export function createBarModel(options: {
|
|
507
|
-
data: BarChartDatum[];
|
|
508
|
-
categoryKey: string;
|
|
509
|
-
series: BarChartSeries[];
|
|
510
|
-
hiddenSeriesIds: string[];
|
|
511
|
-
width: number;
|
|
512
|
-
height: number;
|
|
513
|
-
orientation: BarChartOrientation;
|
|
514
|
-
mode: BarChartMode;
|
|
515
|
-
xAxis?: ChartAxisConfiguration | undefined;
|
|
516
|
-
yAxis?: ChartAxisConfiguration | undefined;
|
|
517
|
-
}): BarChartModel {
|
|
518
|
-
const {
|
|
519
|
-
data,
|
|
520
|
-
categoryKey,
|
|
521
|
-
series,
|
|
522
|
-
hiddenSeriesIds,
|
|
523
|
-
width,
|
|
524
|
-
height,
|
|
525
|
-
orientation,
|
|
526
|
-
mode,
|
|
527
|
-
xAxis,
|
|
528
|
-
yAxis,
|
|
529
|
-
} = options;
|
|
530
|
-
assertUniqueSeriesIds('bar-chart', series);
|
|
531
|
-
assertValidChartNumber('bar-chart', 'invalid-height', height, 'height');
|
|
532
|
-
assertValidTickCount('bar-chart', xAxis);
|
|
533
|
-
assertValidTickCount('bar-chart', yAxis);
|
|
534
|
-
|
|
535
|
-
const categories: NormalizedXValue[] = [];
|
|
536
|
-
const seenCategories = new Set<string>();
|
|
537
|
-
const categoryKinds = new Set<string>();
|
|
538
|
-
const visibleSeries = series.filter((item) => !hiddenSeriesIds.includes(item.id));
|
|
539
|
-
const visibleValues: number[] = [0];
|
|
540
|
-
// Build a key-keyed lookup once so the render loop is O(categories), not
|
|
541
|
-
// O(categories * rows) with allocations per probe.
|
|
542
|
-
const datumByKey = new Map<string, BarChartDatum>();
|
|
543
|
-
|
|
544
|
-
for (const datum of data) {
|
|
545
|
-
if (!(categoryKey in datum)) {
|
|
546
|
-
throw new Error(
|
|
547
|
-
`[cinder/bar-chart] rule=invalid-bar-category key="${categoryKey}": category key is missing.`,
|
|
548
|
-
);
|
|
549
|
-
}
|
|
550
|
-
const rawCategory = datum[categoryKey];
|
|
551
|
-
if (
|
|
552
|
-
!(
|
|
553
|
-
typeof rawCategory === 'string' ||
|
|
554
|
-
typeof rawCategory === 'number' ||
|
|
555
|
-
rawCategory instanceof Date
|
|
556
|
-
)
|
|
557
|
-
) {
|
|
558
|
-
throw new Error(
|
|
559
|
-
`[cinder/bar-chart] rule=invalid-bar-category key="${categoryKey}": category must be string, number, or Date.`,
|
|
560
|
-
);
|
|
561
|
-
}
|
|
562
|
-
const category = normalizeXValue(rawCategory);
|
|
563
|
-
categoryKinds.add(category.kind);
|
|
564
|
-
if (seenCategories.has(category.key)) {
|
|
565
|
-
throw new Error(
|
|
566
|
-
`[cinder/bar-chart] rule=duplicate-category key="${categoryKey}" category="${category.label}": duplicate categories are not supported.`,
|
|
567
|
-
);
|
|
568
|
-
}
|
|
569
|
-
seenCategories.add(category.key);
|
|
570
|
-
categories.push(category);
|
|
571
|
-
datumByKey.set(category.key, datum);
|
|
572
|
-
for (const item of series) {
|
|
573
|
-
if (!(item.valueKey in datum)) {
|
|
574
|
-
throw new Error(
|
|
575
|
-
`[cinder/bar-chart] rule=missing-bar-value-key key="${item.valueKey}": value key is missing.`,
|
|
576
|
-
);
|
|
577
|
-
}
|
|
578
|
-
const value = datum[item.valueKey];
|
|
579
|
-
if (!(typeof value === 'number' || value === null || value === undefined)) {
|
|
580
|
-
throw new Error(
|
|
581
|
-
`[cinder/bar-chart] rule=invalid-bar-value key="${item.valueKey}" category="${category.label}": value must be number, null, or undefined.`,
|
|
582
|
-
);
|
|
583
|
-
}
|
|
584
|
-
const numericValue = normalizeNumericValue('bar-chart', item.id, category.label, value);
|
|
585
|
-
if (numericValue !== null && !hiddenSeriesIds.includes(item.id))
|
|
586
|
-
visibleValues.push(numericValue);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
if (categoryKinds.size > 1) {
|
|
591
|
-
throw new Error(
|
|
592
|
-
`[cinder/bar-chart] rule=mixed-bar-category-kind key="${categoryKey}": category values must share one domain kind.`,
|
|
593
|
-
);
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
const sortedCategories = sortXValues(categories);
|
|
597
|
-
const categoryLabels = sortedCategories.map((category, index) =>
|
|
598
|
-
formatXValue(category, orientation === 'vertical' ? xAxis : yAxis, { index }),
|
|
599
|
-
);
|
|
600
|
-
const horizontalCategoryLabelLayout =
|
|
601
|
-
orientation === 'horizontal'
|
|
602
|
-
? createHorizontalCategoryLabelLayout(categoryLabels, width)
|
|
603
|
-
: undefined;
|
|
604
|
-
const geometry = createGeometry(width, height, horizontalCategoryLabelLayout?.marginLeft);
|
|
605
|
-
// Domain is computed from visible series only — same convention as cartesian
|
|
606
|
-
// charts — so the value scale shrinks correctly when a series is hidden.
|
|
607
|
-
const valueDomain = createPaddedDomain(
|
|
608
|
-
mode === 'stacked'
|
|
609
|
-
? createStackedBarDomainValues(datumByKey, sortedCategories, visibleSeries)
|
|
610
|
-
: visibleValues,
|
|
611
|
-
);
|
|
612
|
-
if (sortedCategories.length === 0) {
|
|
613
|
-
const tickAxis = orientation === 'vertical' ? yAxis : xAxis;
|
|
614
|
-
return {
|
|
615
|
-
geometry,
|
|
616
|
-
categories: [],
|
|
617
|
-
yTicks: createTicks(valueDomain, tickAxis?.tickCount ?? 5),
|
|
618
|
-
categoryTicks: [],
|
|
619
|
-
bars: [],
|
|
620
|
-
tableRows: [],
|
|
621
|
-
targets: [],
|
|
622
|
-
empty: true,
|
|
623
|
-
valueDomain,
|
|
624
|
-
};
|
|
625
|
-
}
|
|
626
|
-
const valueScale = createLinearScale(
|
|
627
|
-
valueDomain,
|
|
628
|
-
orientation === 'vertical' ? [geometry.plotHeight, 0] : [0, geometry.plotWidth],
|
|
629
|
-
);
|
|
630
|
-
const categoryScale = createBandScale(
|
|
631
|
-
sortedCategories.map((category) => category.key),
|
|
632
|
-
orientation === 'vertical' ? [0, geometry.plotWidth] : [0, geometry.plotHeight],
|
|
633
|
-
0.18,
|
|
634
|
-
);
|
|
635
|
-
const groupScale = createBandScale(
|
|
636
|
-
visibleSeries.map((item) => item.id),
|
|
637
|
-
[0, categoryScale.bandwidth()],
|
|
638
|
-
0.12,
|
|
639
|
-
);
|
|
640
|
-
const categoryTicks = sortedCategories.map((category, index) => {
|
|
641
|
-
const categoryPosition = categoryScale(category.key) ?? 0;
|
|
642
|
-
return {
|
|
643
|
-
categoryKey: category.key,
|
|
644
|
-
label:
|
|
645
|
-
horizontalCategoryLabelLayout?.labels[index] ?? categoryLabels[index] ?? category.label,
|
|
646
|
-
fullLabel: categoryLabels[index] ?? category.label,
|
|
647
|
-
x:
|
|
648
|
-
orientation === 'vertical'
|
|
649
|
-
? categoryPosition + categoryScale.bandwidth() / 2
|
|
650
|
-
: -HORIZONTAL_CATEGORY_LABEL_GAP,
|
|
651
|
-
y:
|
|
652
|
-
orientation === 'vertical'
|
|
653
|
-
? geometry.plotHeight + 20
|
|
654
|
-
: categoryPosition + categoryScale.bandwidth() / 2,
|
|
655
|
-
};
|
|
656
|
-
});
|
|
657
|
-
|
|
658
|
-
const bars: BarChartModel['bars'] = [];
|
|
659
|
-
const targets: ChartTarget[] = [];
|
|
660
|
-
const tableRows: BarChartModel['tableRows'] = [];
|
|
661
|
-
for (const [categoryIndex, category] of sortedCategories.entries()) {
|
|
662
|
-
const datum = datumByKey.get(category.key);
|
|
663
|
-
if (!datum) continue;
|
|
664
|
-
let positiveOffset = 0;
|
|
665
|
-
let negativeOffset = 0;
|
|
666
|
-
const rowValues: BarChartModel['tableRows'][number]['values'] = [];
|
|
667
|
-
visibleSeries.forEach((item) => {
|
|
668
|
-
const rawValue = datum[item.valueKey];
|
|
669
|
-
if (typeof rawValue !== 'number') return;
|
|
670
|
-
const value = rawValue;
|
|
671
|
-
const seriesColorIndex = Math.max(
|
|
672
|
-
0,
|
|
673
|
-
series.findIndex((entry) => entry.id === item.id),
|
|
674
|
-
);
|
|
675
|
-
const color = item.color ?? chartPaletteColor(seriesColorIndex);
|
|
676
|
-
const categoryPosition = categoryScale(category.key) ?? 0;
|
|
677
|
-
let x = 0;
|
|
678
|
-
let y = 0;
|
|
679
|
-
let barWidth = 0;
|
|
680
|
-
let barHeight = 0;
|
|
681
|
-
if (orientation === 'vertical') {
|
|
682
|
-
const groupX = mode === 'grouped' ? (groupScale(item.id) ?? 0) : 0;
|
|
683
|
-
const start = mode === 'stacked' ? (value >= 0 ? positiveOffset : negativeOffset) : 0;
|
|
684
|
-
const end = start + value;
|
|
685
|
-
if (mode === 'stacked') {
|
|
686
|
-
if (value >= 0) positiveOffset = end;
|
|
687
|
-
else negativeOffset = end;
|
|
688
|
-
}
|
|
689
|
-
const y0 = valueScale(start);
|
|
690
|
-
const y1 = valueScale(end);
|
|
691
|
-
x = categoryPosition + groupX;
|
|
692
|
-
y = Math.min(y0, y1);
|
|
693
|
-
barWidth = mode === 'grouped' ? groupScale.bandwidth() : categoryScale.bandwidth();
|
|
694
|
-
barHeight = Math.abs(y1 - y0);
|
|
695
|
-
} else {
|
|
696
|
-
const groupY = mode === 'grouped' ? (groupScale(item.id) ?? 0) : 0;
|
|
697
|
-
const start = mode === 'stacked' ? (value >= 0 ? positiveOffset : negativeOffset) : 0;
|
|
698
|
-
const end = start + value;
|
|
699
|
-
if (mode === 'stacked') {
|
|
700
|
-
if (value >= 0) positiveOffset = end;
|
|
701
|
-
else negativeOffset = end;
|
|
702
|
-
}
|
|
703
|
-
const x0 = valueScale(start);
|
|
704
|
-
const x1 = valueScale(end);
|
|
705
|
-
x = Math.min(x0, x1);
|
|
706
|
-
y = categoryPosition + groupY;
|
|
707
|
-
barWidth = Math.abs(x1 - x0);
|
|
708
|
-
barHeight = mode === 'grouped' ? groupScale.bandwidth() : categoryScale.bandwidth();
|
|
709
|
-
}
|
|
710
|
-
const valueLabel = formatNumericValue(
|
|
711
|
-
value,
|
|
712
|
-
orientation === 'vertical' ? yAxis : xAxis,
|
|
713
|
-
item.valueFormatter,
|
|
714
|
-
{ seriesId: item.id, seriesLabel: item.label, index: categoryIndex },
|
|
715
|
-
);
|
|
716
|
-
const categoryLabel = formatXValue(category, orientation === 'vertical' ? xAxis : yAxis, {
|
|
717
|
-
seriesId: item.id,
|
|
718
|
-
seriesLabel: item.label,
|
|
719
|
-
index: categoryIndex,
|
|
720
|
-
});
|
|
721
|
-
rowValues.push({ seriesId: item.id, seriesLabel: item.label, valueLabel });
|
|
722
|
-
const bar = {
|
|
723
|
-
id: `${item.id}-${category.key}`,
|
|
724
|
-
seriesId: item.id,
|
|
725
|
-
seriesLabel: item.label,
|
|
726
|
-
categoryLabel,
|
|
727
|
-
valueLabel,
|
|
728
|
-
x,
|
|
729
|
-
y,
|
|
730
|
-
width: barWidth,
|
|
731
|
-
height: barHeight,
|
|
732
|
-
color,
|
|
733
|
-
hidden: false,
|
|
734
|
-
};
|
|
735
|
-
bars.push(bar);
|
|
736
|
-
targets.push({ ...bar, x: x + barWidth / 2, y: y + barHeight / 2, xLabel: categoryLabel });
|
|
737
|
-
});
|
|
738
|
-
if (rowValues.length > 0) {
|
|
739
|
-
tableRows.push({
|
|
740
|
-
categoryKey: category.key,
|
|
741
|
-
categoryLabel: formatXValue(category, orientation === 'vertical' ? xAxis : yAxis, {
|
|
742
|
-
index: tableRows.length,
|
|
743
|
-
}),
|
|
744
|
-
values: rowValues,
|
|
745
|
-
});
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
// Sort targets by the dominant axis for binary-search nearestTarget. For
|
|
750
|
-
// vertical bars the dominant axis is x; for horizontal bars it is y.
|
|
751
|
-
targets.sort((a, b) => (orientation === 'vertical' ? a.x - b.x : a.y - b.y));
|
|
752
|
-
|
|
753
|
-
return {
|
|
754
|
-
geometry,
|
|
755
|
-
categories: sortedCategories,
|
|
756
|
-
yTicks: valueScale.ticks((orientation === 'vertical' ? yAxis : xAxis)?.tickCount ?? 5),
|
|
757
|
-
categoryTicks,
|
|
758
|
-
bars,
|
|
759
|
-
tableRows,
|
|
760
|
-
targets,
|
|
761
|
-
empty: targets.length === 0,
|
|
762
|
-
valueDomain,
|
|
763
|
-
};
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* Locates the target closest to a (x, y) pointer. Uses binary search on the
|
|
768
|
-
* provided `axis` (defaults to `x`) — targets MUST be sorted ascending on that
|
|
769
|
-
* axis (createCartesianModel and createBarModel guarantee this). Falls back to
|
|
770
|
-
* a linear scan when targets are empty.
|
|
771
|
-
*/
|
|
772
|
-
export function nearestTarget(
|
|
773
|
-
targets: readonly ChartTarget[],
|
|
774
|
-
x: number,
|
|
775
|
-
y: number,
|
|
776
|
-
axis: 'x' | 'y' = 'x',
|
|
777
|
-
): ChartTarget | undefined {
|
|
778
|
-
if (targets.length === 0) return undefined;
|
|
779
|
-
if (targets.length === 1) return targets[0];
|
|
780
|
-
const pointerKey = axis === 'x' ? x : y;
|
|
781
|
-
// Binary search for the leftmost target whose key is >= pointer, then
|
|
782
|
-
// compare the adjacent dominant-axis buckets. The nearest target can live in
|
|
783
|
-
// either bucket when the pointer is between two x/y positions.
|
|
784
|
-
let low = 0;
|
|
785
|
-
let high = targets.length - 1;
|
|
786
|
-
while (low < high) {
|
|
787
|
-
const mid = (low + high) >> 1;
|
|
788
|
-
const midKey = axis === 'x' ? (targets[mid]?.x ?? 0) : (targets[mid]?.y ?? 0);
|
|
789
|
-
if (midKey < pointerKey) low = mid + 1;
|
|
790
|
-
else high = mid;
|
|
791
|
-
}
|
|
792
|
-
const candidates: ChartTarget[] = [];
|
|
793
|
-
|
|
794
|
-
function addBucket(start: number): void {
|
|
795
|
-
const target = targets[start];
|
|
796
|
-
if (!target) return;
|
|
797
|
-
const bucketKey = axis === 'x' ? target.x : target.y;
|
|
798
|
-
for (let index = start; index >= 0; index--) {
|
|
799
|
-
const candidate = targets[index];
|
|
800
|
-
if (!candidate) continue;
|
|
801
|
-
const candidateKey = axis === 'x' ? candidate.x : candidate.y;
|
|
802
|
-
if (candidateKey !== bucketKey) break;
|
|
803
|
-
candidates.push(candidate);
|
|
804
|
-
}
|
|
805
|
-
for (let index = start + 1; index < targets.length; index++) {
|
|
806
|
-
const candidate = targets[index];
|
|
807
|
-
if (!candidate) continue;
|
|
808
|
-
const candidateKey = axis === 'x' ? candidate.x : candidate.y;
|
|
809
|
-
if (candidateKey !== bucketKey) break;
|
|
810
|
-
candidates.push(candidate);
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
addBucket(low);
|
|
815
|
-
if (low > 0) {
|
|
816
|
-
addBucket(low - 1);
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
let nearest: ChartTarget | undefined;
|
|
820
|
-
let nearestDistance = Number.POSITIVE_INFINITY;
|
|
821
|
-
for (const target of candidates) {
|
|
822
|
-
const targetDistance = Math.hypot(target.x - x, target.y - y);
|
|
823
|
-
if (targetDistance >= nearestDistance) continue;
|
|
824
|
-
nearest = target;
|
|
825
|
-
nearestDistance = targetDistance;
|
|
826
|
-
}
|
|
827
|
-
return nearest;
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
export function legendVisible(legendPosition: ChartLegendPosition, seriesCount: number): boolean {
|
|
831
|
-
return legendPosition !== 'none' && seriesCount > 0;
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
const DEFAULT_CHART_MARGIN_LEFT = 48;
|
|
835
|
-
const HORIZONTAL_CATEGORY_LABEL_CHARACTER_WIDTH = 12;
|
|
836
|
-
const HORIZONTAL_CATEGORY_LABEL_GAP = 8;
|
|
837
|
-
const HORIZONTAL_CATEGORY_LABEL_OUTER_PADDING = 8;
|
|
838
|
-
const MAXIMUM_HORIZONTAL_CATEGORY_LABEL_FRACTION = 0.4;
|
|
839
|
-
|
|
840
|
-
function createGeometry(
|
|
841
|
-
width: number,
|
|
842
|
-
height: number,
|
|
843
|
-
marginLeft = DEFAULT_CHART_MARGIN_LEFT,
|
|
844
|
-
): ChartGeometry {
|
|
845
|
-
const marginTop = 16;
|
|
846
|
-
const marginRight = 16;
|
|
847
|
-
const marginBottom = 36;
|
|
848
|
-
return {
|
|
849
|
-
plotWidth: Math.max(1, width - marginLeft - marginRight),
|
|
850
|
-
plotHeight: Math.max(1, height - marginTop - marginBottom),
|
|
851
|
-
marginTop,
|
|
852
|
-
marginRight,
|
|
853
|
-
marginBottom,
|
|
854
|
-
marginLeft,
|
|
855
|
-
};
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
function createHorizontalCategoryLabelLayout(
|
|
859
|
-
labels: string[],
|
|
860
|
-
chartWidth: number,
|
|
861
|
-
): { labels: string[]; marginLeft: number } {
|
|
862
|
-
const maximumMarginLeft = Math.max(
|
|
863
|
-
DEFAULT_CHART_MARGIN_LEFT,
|
|
864
|
-
Math.floor(chartWidth * MAXIMUM_HORIZONTAL_CATEGORY_LABEL_FRACTION),
|
|
865
|
-
);
|
|
866
|
-
let longestLabelWidth = 0;
|
|
867
|
-
for (const label of labels) {
|
|
868
|
-
longestLabelWidth = Math.max(longestLabelWidth, estimateHorizontalCategoryLabelWidth(label));
|
|
869
|
-
}
|
|
870
|
-
const requestedMarginLeft =
|
|
871
|
-
longestLabelWidth + HORIZONTAL_CATEGORY_LABEL_GAP + HORIZONTAL_CATEGORY_LABEL_OUTER_PADDING;
|
|
872
|
-
const marginLeft = Math.min(
|
|
873
|
-
maximumMarginLeft,
|
|
874
|
-
Math.max(DEFAULT_CHART_MARGIN_LEFT, requestedMarginLeft),
|
|
875
|
-
);
|
|
876
|
-
const availableLabelWidth = Math.max(
|
|
877
|
-
0,
|
|
878
|
-
marginLeft - HORIZONTAL_CATEGORY_LABEL_GAP - HORIZONTAL_CATEGORY_LABEL_OUTER_PADDING,
|
|
879
|
-
);
|
|
880
|
-
|
|
881
|
-
return {
|
|
882
|
-
marginLeft,
|
|
883
|
-
labels: labels.map((label) => truncateHorizontalCategoryLabel(label, availableLabelWidth)),
|
|
884
|
-
};
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
function estimateHorizontalCategoryLabelWidth(label: string): number {
|
|
888
|
-
return Array.from(label).length * HORIZONTAL_CATEGORY_LABEL_CHARACTER_WIDTH;
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
function truncateHorizontalCategoryLabel(label: string, availableWidth: number): string {
|
|
892
|
-
if (estimateHorizontalCategoryLabelWidth(label) <= availableWidth) return label;
|
|
893
|
-
|
|
894
|
-
const availableCharacterCount = Math.floor(
|
|
895
|
-
availableWidth / HORIZONTAL_CATEGORY_LABEL_CHARACTER_WIDTH,
|
|
896
|
-
);
|
|
897
|
-
if (availableCharacterCount <= 0) return '';
|
|
898
|
-
if (availableCharacterCount === 1) return '…';
|
|
899
|
-
|
|
900
|
-
return `${Array.from(label)
|
|
901
|
-
.slice(0, availableCharacterCount - 1)
|
|
902
|
-
.join('')}…`;
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
function normalizeNumericValue(
|
|
906
|
-
componentId: string,
|
|
907
|
-
seriesId: string,
|
|
908
|
-
xLabel: string,
|
|
909
|
-
value: ChartNumericValue,
|
|
910
|
-
): number | null {
|
|
911
|
-
if (value === null || value === undefined) return null;
|
|
912
|
-
if (!Number.isFinite(value)) {
|
|
913
|
-
throw new Error(
|
|
914
|
-
`[cinder/${componentId}] rule=non-finite-y series="${seriesId}" x="${xLabel}": y values must be finite numbers.`,
|
|
915
|
-
);
|
|
916
|
-
}
|
|
917
|
-
return value;
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
function sortXValues(values: NormalizedXValue[]): NormalizedXValue[] {
|
|
921
|
-
if (values[0]?.kind === 'string') return values;
|
|
922
|
-
return [...values].toSorted((a, b) => Number(a.comparable) - Number(b.comparable));
|
|
923
|
-
}
|
|
924
|
-
|
|
925
|
-
function createPaddedDomain(values: number[]): [number, number] {
|
|
926
|
-
if (values.length === 0) return [-1, 1];
|
|
927
|
-
// Avoid spread-call argument-limit cliffs on large arrays.
|
|
928
|
-
let minimum = values[0]!;
|
|
929
|
-
let maximum = values[0]!;
|
|
930
|
-
for (let index = 1; index < values.length; index++) {
|
|
931
|
-
const value = values[index]!;
|
|
932
|
-
if (value < minimum) minimum = value;
|
|
933
|
-
if (value > maximum) maximum = value;
|
|
934
|
-
}
|
|
935
|
-
if (minimum === maximum) return [minimum - 1, maximum + 1];
|
|
936
|
-
return [Math.min(0, minimum), Math.max(0, maximum)];
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
type LinearScale = ((value: number) => number) & {
|
|
940
|
-
ticks: (count: number) => number[];
|
|
941
|
-
};
|
|
942
|
-
|
|
943
|
-
type BandlikeScale = ((value: string) => number | undefined) & {
|
|
944
|
-
bandwidth?: () => number;
|
|
945
|
-
};
|
|
946
|
-
|
|
947
|
-
type BandScale = ((value: string) => number | undefined) & {
|
|
948
|
-
bandwidth: () => number;
|
|
949
|
-
};
|
|
950
|
-
|
|
951
|
-
function createLinearScale(domain: [number, number], range: [number, number]): LinearScale {
|
|
952
|
-
const [domainMinimum, domainMaximum] =
|
|
953
|
-
domain[0] === domain[1] ? [domain[0] - 1, domain[1] + 1] : domain;
|
|
954
|
-
const [rangeMinimum, rangeMaximum] = range;
|
|
955
|
-
const domainSpan = domainMaximum - domainMinimum;
|
|
956
|
-
const rangeSpan = rangeMaximum - rangeMinimum;
|
|
957
|
-
// Build the scale by composing the call signature with the `ticks` method
|
|
958
|
-
// up front, so the intersection type holds without an `as` cast.
|
|
959
|
-
const scale = Object.assign(
|
|
960
|
-
(value: number): number => rangeMinimum + ((value - domainMinimum) / domainSpan) * rangeSpan,
|
|
961
|
-
{
|
|
962
|
-
ticks: (count: number): number[] => createTicks([domainMinimum, domainMaximum], count),
|
|
963
|
-
},
|
|
964
|
-
) satisfies LinearScale;
|
|
965
|
-
return scale;
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
function createPointScale(
|
|
969
|
-
domain: string[],
|
|
970
|
-
range: [number, number],
|
|
971
|
-
padding: number,
|
|
972
|
-
): BandlikeScale {
|
|
973
|
-
const positions = new Map<string, number>();
|
|
974
|
-
|
|
975
|
-
const [rangeMinimum, rangeMaximum] = range;
|
|
976
|
-
if (domain.length === 1) {
|
|
977
|
-
positions.set(domain[0] ?? '', rangeMinimum + (rangeMaximum - rangeMinimum) / 2);
|
|
978
|
-
} else {
|
|
979
|
-
const step = (rangeMaximum - rangeMinimum) / Math.max(1, domain.length - 1 + padding * 2);
|
|
980
|
-
const start = rangeMinimum + step * padding;
|
|
981
|
-
domain.forEach((value, index) => positions.set(value, start + step * index));
|
|
982
|
-
}
|
|
983
|
-
|
|
984
|
-
return (value: string) => positions.get(value);
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
function createBandScale(domain: string[], range: [number, number], padding: number): BandScale {
|
|
988
|
-
const positions = new Map<string, number>();
|
|
989
|
-
const [rangeMinimum, rangeMaximum] = range;
|
|
990
|
-
const availableSize = Math.max(0, rangeMaximum - rangeMinimum);
|
|
991
|
-
const step =
|
|
992
|
-
domain.length === 0
|
|
993
|
-
? 0
|
|
994
|
-
: availableSize / Math.max(1, domain.length + padding * (domain.length - 1));
|
|
995
|
-
const bandWidth = Math.max(0, step * (1 - padding));
|
|
996
|
-
domain.forEach((value, index) => positions.set(value, rangeMinimum + step * index));
|
|
997
|
-
|
|
998
|
-
// Same Object.assign + satisfies pattern as createLinearScale — the
|
|
999
|
-
// intersection type is satisfied at construction, not asserted after.
|
|
1000
|
-
const scale = Object.assign((value: string) => positions.get(value), {
|
|
1001
|
-
bandwidth: (): number => bandWidth,
|
|
1002
|
-
}) satisfies BandScale;
|
|
1003
|
-
return scale;
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
function createNumericDomain(values: NormalizedXValue[]): [number, number] {
|
|
1007
|
-
if (values.length === 0) return [0, 1];
|
|
1008
|
-
let minimum = Number(values[0]!.comparable);
|
|
1009
|
-
let maximum = minimum;
|
|
1010
|
-
for (let index = 1; index < values.length; index++) {
|
|
1011
|
-
const numeric = Number(values[index]!.comparable);
|
|
1012
|
-
if (numeric < minimum) minimum = numeric;
|
|
1013
|
-
if (numeric > maximum) maximum = numeric;
|
|
1014
|
-
}
|
|
1015
|
-
return minimum === maximum ? [minimum - 1, maximum + 1] : [minimum, maximum];
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
function createTicks(domain: [number, number], count: number): number[] {
|
|
1019
|
-
const safeCount = Math.max(1, Math.floor(count));
|
|
1020
|
-
if (safeCount === 1) return [domain[0]];
|
|
1021
|
-
const step = (domain[1] - domain[0]) / (safeCount - 1);
|
|
1022
|
-
return Array.from({ length: safeCount }, (_, index) => domain[0] + step * index);
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
function createLinePath(points: Array<{ x: number; y: number }>): string {
|
|
1026
|
-
if (points.length === 0) return '';
|
|
1027
|
-
const [firstPoint, ...remainingPoints] = points;
|
|
1028
|
-
return [
|
|
1029
|
-
`M${formatPathNumber(firstPoint?.x ?? 0)},${formatPathNumber(firstPoint?.y ?? 0)}`,
|
|
1030
|
-
...remainingPoints.map((point) => `L${formatPathNumber(point.x)},${formatPathNumber(point.y)}`),
|
|
1031
|
-
].join('');
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
|
-
function createAreaPath(
|
|
1035
|
-
points: Array<{ x: number; y: number; y0?: number }>,
|
|
1036
|
-
baseline: number | undefined,
|
|
1037
|
-
): string {
|
|
1038
|
-
if (points.length === 0) return '';
|
|
1039
|
-
const firstPoint = points[0];
|
|
1040
|
-
const lastPoint = points.at(-1);
|
|
1041
|
-
if (!firstPoint || !lastPoint) return '';
|
|
1042
|
-
const lowerPoints = points
|
|
1043
|
-
.toReversed()
|
|
1044
|
-
.map((point) => `L${formatPathNumber(point.x)},${formatPathNumber(point.y0 ?? baseline ?? 0)}`);
|
|
1045
|
-
return [
|
|
1046
|
-
`M${formatPathNumber(firstPoint.x)},${formatPathNumber(firstPoint.y0 ?? baseline ?? 0)}`,
|
|
1047
|
-
`L${formatPathNumber(firstPoint.x)},${formatPathNumber(firstPoint.y)}`,
|
|
1048
|
-
...points.slice(1).map((point) => `L${formatPathNumber(point.x)},${formatPathNumber(point.y)}`),
|
|
1049
|
-
...lowerPoints,
|
|
1050
|
-
'Z',
|
|
1051
|
-
].join('');
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
|
-
function createStackedBarDomainValues(
|
|
1055
|
-
datumByKey: Map<string, BarChartDatum>,
|
|
1056
|
-
categories: NormalizedXValue[],
|
|
1057
|
-
series: BarChartSeries[],
|
|
1058
|
-
): number[] {
|
|
1059
|
-
const values = [0];
|
|
1060
|
-
for (const category of categories) {
|
|
1061
|
-
const datum = datumByKey.get(category.key);
|
|
1062
|
-
if (!datum) continue;
|
|
1063
|
-
let positiveTotal = 0;
|
|
1064
|
-
let negativeTotal = 0;
|
|
1065
|
-
for (const item of series) {
|
|
1066
|
-
const value = datum[item.valueKey];
|
|
1067
|
-
if (typeof value !== 'number') continue;
|
|
1068
|
-
if (value >= 0) positiveTotal += value;
|
|
1069
|
-
else negativeTotal += value;
|
|
1070
|
-
}
|
|
1071
|
-
values.push(positiveTotal, negativeTotal);
|
|
1072
|
-
}
|
|
1073
|
-
return values;
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
function formatPathNumber(value: number): string {
|
|
1077
|
-
return Number(value.toFixed(3)).toString();
|
|
1078
|
-
}
|
|
1
|
+
export { createBarModel } from './chart-bar-model.ts';
|
|
2
|
+
export { createCartesianModel } from './chart-cartesian-model.ts';
|
|
3
|
+
export { createChartGeometry, observeChartFontLoading } from './chart-layout.ts';
|
|
4
|
+
export type { ChartGeometryOptions } from './chart-layout.ts';
|
|
5
|
+
export * from './chart-model-utilities.ts';
|
|
6
|
+
export {
|
|
7
|
+
MAXIMUM_RENDERED_SERIES_POINTS,
|
|
8
|
+
createAreaPath,
|
|
9
|
+
createBandScale,
|
|
10
|
+
createLinePath,
|
|
11
|
+
createLinearScale,
|
|
12
|
+
createNumericDomain,
|
|
13
|
+
createPaddedDomain,
|
|
14
|
+
createPointScale,
|
|
15
|
+
createStackedBarDomainValues,
|
|
16
|
+
createTicks,
|
|
17
|
+
decimatePlacedPoints,
|
|
18
|
+
decimationIndices,
|
|
19
|
+
normalizeNumericValue,
|
|
20
|
+
sortXValues,
|
|
21
|
+
} from './chart-scale.ts';
|
|
22
|
+
export type { BandScale, BandlikeScale, LinearScale } from './chart-scale.ts';
|