@lostgradient/cinder 0.22.0 → 0.23.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.
Files changed (164) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/components.json +1 -1
  3. package/dist/_internal/chart/chart-bar-model.d.ts +24 -0
  4. package/dist/_internal/chart/chart-cartesian-model.d.ts +16 -0
  5. package/dist/_internal/chart/chart-layout.d.ts +23 -0
  6. package/dist/_internal/chart/chart-model-utilities.d.ts +157 -0
  7. package/dist/_internal/chart/chart-scale.d.ts +37 -0
  8. package/dist/_internal/chart/chart-utilities.d.ts +7 -164
  9. package/dist/_internal/chart/heatmap-utilities.d.ts +5 -4
  10. package/dist/components/_internal/chart-tooltip.css +21 -0
  11. package/dist/components/_internal/chart-tooltip.svelte.d.ts +11 -0
  12. package/dist/components/approval-card/index.js +5 -5
  13. package/dist/components/area-chart/area-chart.css +27 -23
  14. package/dist/components/area-chart/area-chart.schema.js +38 -1
  15. package/dist/components/area-chart/area-chart.types.d.ts +6 -2
  16. package/dist/components/area-chart/index.js +1589 -768
  17. package/dist/components/badge/badge.css +2 -2
  18. package/dist/components/badge/badge.schema.js +2 -2
  19. package/dist/components/badge/badge.types.d.ts +3 -3
  20. package/dist/components/badge/index.js +4 -4
  21. package/dist/components/bar-chart/bar-chart.css +27 -21
  22. package/dist/components/bar-chart/bar-chart.schema.js +38 -1
  23. package/dist/components/bar-chart/bar-chart.types.d.ts +7 -3
  24. package/dist/components/bar-chart/index.js +1544 -740
  25. package/dist/components/chart.types.d.ts +98 -2
  26. package/dist/components/json-schema-editor/index.js +4 -4
  27. package/dist/components/line-chart/index.js +1587 -785
  28. package/dist/components/line-chart/line-chart.css +25 -24
  29. package/dist/components/line-chart/line-chart.schema.js +38 -1
  30. package/dist/components/line-chart/line-chart.types.d.ts +6 -2
  31. package/dist/components/matrix-chart/index.js +1038 -429
  32. package/dist/components/matrix-chart/matrix-chart.css +12 -6
  33. package/dist/components/matrix-chart/matrix-chart.schema.js +26 -1
  34. package/dist/components/matrix-chart/matrix-chart.types.d.ts +5 -1
  35. package/dist/components/payload-inspector/index.js +4 -4
  36. package/dist/components/run-step-timeline/index.js +6 -6
  37. package/dist/components/sparkbar/index.js +1291 -4
  38. package/dist/components/sparkbar/sparkbar.css +5 -4
  39. package/dist/components/sparkbar/sparkbar.schema.js +26 -1
  40. package/dist/components/sparkbar/sparkbar.types.d.ts +5 -0
  41. package/dist/components/spectrogram/index.js +1076 -437
  42. package/dist/components/spectrogram/spectrogram.css +7 -6
  43. package/dist/components/spectrogram/spectrogram.schema.js +26 -1
  44. package/dist/components/spectrogram/spectrogram.types.d.ts +5 -1
  45. package/dist/components/spectrum-chart/index.js +1037 -423
  46. package/dist/components/spectrum-chart/spectrum-chart.css +8 -7
  47. package/dist/components/spectrum-chart/spectrum-chart.schema.js +26 -1
  48. package/dist/components/spectrum-chart/spectrum-chart.types.d.ts +5 -1
  49. package/dist/components/statistic/index.js +1284 -2
  50. package/dist/components/statistic/statistic.css +6 -5
  51. package/dist/components/statistic/statistic.schema.js +26 -1
  52. package/dist/components/statistic/statistic.types.d.ts +5 -0
  53. package/dist/components/statistic-group/index.js +1284 -2
  54. package/dist/components/waveform/index.js +1004 -404
  55. package/dist/components/waveform/waveform.css +8 -7
  56. package/dist/components/waveform/waveform.schema.js +26 -1
  57. package/dist/components/waveform/waveform.types.d.ts +5 -1
  58. package/dist/index.d.ts +1 -1
  59. package/dist/index.js +17730 -16540
  60. package/dist/server/components/approval-card/index.js +3 -3
  61. package/dist/server/components/area-chart/index.js +3 -3
  62. package/dist/server/components/badge/index.js +2 -2
  63. package/dist/server/components/bar-chart/index.js +3 -3
  64. package/dist/server/components/json-schema-editor/index.js +2 -2
  65. package/dist/server/components/line-chart/index.js +3 -3
  66. package/dist/server/components/matrix-chart/index.js +3 -3
  67. package/dist/server/components/payload-inspector/index.js +3 -3
  68. package/dist/server/components/run-step-timeline/index.js +2 -2
  69. package/dist/server/components/sparkbar/index.js +3 -2
  70. package/dist/server/components/spectrogram/index.js +3 -3
  71. package/dist/server/components/spectrum-chart/index.js +2 -2
  72. package/dist/server/components/statistic/index.js +4 -3
  73. package/dist/server/components/statistic-group/index.js +4 -3
  74. package/dist/server/components/waveform/index.js +2 -2
  75. package/dist/server/index.js +21 -21
  76. package/dist/server/{index.server-3dkwzkd3.js → index.server-10r7yf13.js} +110 -27
  77. package/dist/server/{index.server-93943z3e.js → index.server-151dt1tx.js} +1 -1
  78. package/dist/server/{index.server-pp2n6def.js → index.server-2y0xz53g.js} +37 -14
  79. package/dist/server/{index.server-bd8rcgf0.js → index.server-4hvjet4z.js} +1 -1
  80. package/dist/server/{index.server-bsm39gpy.js → index.server-5d83fkyg.js} +75 -21
  81. package/dist/server/{index.server-4wweyb2e.js → index.server-8dh2rtnc.js} +1 -1
  82. package/dist/server/{index.server-abqbstkt.js → index.server-9gj583qs.js} +41 -14
  83. package/dist/server/{index.server-2nmwsacy.js → index.server-9scm7zf9.js} +4 -4
  84. package/dist/server/{index.server-5j6jv7rm.js → index.server-9vwxyy06.js} +108 -29
  85. package/dist/server/{index.server-abjsjsw8.js → index.server-afpjvfz1.js} +111 -35
  86. package/dist/server/{index.server-86sw5ea6.js → index.server-dw9bz17h.js} +4 -4
  87. package/dist/server/{index.server-d26jzn2m.js → index.server-en31rprd.js} +3 -3
  88. package/dist/server/{index.server-dx58qant.js → index.server-f3a0ecqg.js} +72 -9
  89. package/dist/server/{index.server-d63pwytk.js → index.server-kz8aeeq5.js} +18 -10
  90. package/dist/server/index.server-mmmrgvyd.js +1274 -0
  91. package/dist/server/{index.server-1221p7e4.js → index.server-scahngwv.js} +13 -1
  92. package/dist/server/{index.server-nj8kk5bv.js → index.server-t73q59c1.js} +1 -1
  93. package/dist/server/{index.server-5schb2vt.js → index.server-tdrncj5e.js} +16 -6
  94. package/dist/server/{index.server-wm8957be.js → index.server-veqke4sq.js} +10 -1
  95. package/dist/server/{index.server-fphvzcfb.js → index.server-wqajghn9.js} +1 -1
  96. package/dist/server/{index.server-yd9shwqq.js → index.server-zjcjzcv6.js} +1 -1
  97. package/package.json +1 -1
  98. package/src/_internal/chart/chart-bar-model.ts +330 -0
  99. package/src/_internal/chart/chart-cartesian-model.ts +450 -0
  100. package/src/_internal/chart/chart-interaction.svelte.ts +25 -6
  101. package/src/_internal/chart/chart-layout.ts +348 -0
  102. package/src/_internal/chart/chart-model-utilities.ts +357 -0
  103. package/src/_internal/chart/chart-scale.ts +487 -0
  104. package/src/_internal/chart/chart-utilities.ts +22 -1078
  105. package/src/_internal/chart/heatmap-utilities.ts +21 -17
  106. package/src/components/_internal/chart-tooltip.css +21 -0
  107. package/src/components/_internal/chart-tooltip.svelte +44 -0
  108. package/src/components/approval-card/approval-card.svelte +1 -1
  109. package/src/components/area-chart/area-chart.css +27 -23
  110. package/src/components/area-chart/area-chart.examples.json +1 -1
  111. package/src/components/area-chart/area-chart.schema.json +37 -0
  112. package/src/components/area-chart/area-chart.schema.ts +40 -0
  113. package/src/components/area-chart/area-chart.svelte +103 -35
  114. package/src/components/area-chart/area-chart.types.ts +7 -1
  115. package/src/components/badge/badge.css +2 -2
  116. package/src/components/badge/badge.schema.json +1 -1
  117. package/src/components/badge/badge.schema.ts +1 -1
  118. package/src/components/badge/badge.svelte +2 -2
  119. package/src/components/badge/badge.types.ts +3 -3
  120. package/src/components/bar-chart/bar-chart.css +27 -21
  121. package/src/components/bar-chart/bar-chart.schema.json +37 -0
  122. package/src/components/bar-chart/bar-chart.schema.ts +40 -0
  123. package/src/components/bar-chart/bar-chart.svelte +94 -33
  124. package/src/components/bar-chart/bar-chart.types.ts +9 -2
  125. package/src/components/chart.types.ts +112 -2
  126. package/src/components/line-chart/line-chart.css +25 -24
  127. package/src/components/line-chart/line-chart.examples.json +1 -1
  128. package/src/components/line-chart/line-chart.schema.json +37 -0
  129. package/src/components/line-chart/line-chart.schema.ts +40 -0
  130. package/src/components/line-chart/line-chart.svelte +95 -40
  131. package/src/components/line-chart/line-chart.types.ts +7 -1
  132. package/src/components/matrix-chart/matrix-chart.css +12 -6
  133. package/src/components/matrix-chart/matrix-chart.schema.json +25 -0
  134. package/src/components/matrix-chart/matrix-chart.schema.ts +25 -0
  135. package/src/components/matrix-chart/matrix-chart.svelte +30 -12
  136. package/src/components/matrix-chart/matrix-chart.types.ts +5 -1
  137. package/src/components/run-step-timeline/run-step-timeline.svelte +2 -2
  138. package/src/components/sparkbar/sparkbar.css +5 -4
  139. package/src/components/sparkbar/sparkbar.schema.json +25 -0
  140. package/src/components/sparkbar/sparkbar.schema.ts +26 -0
  141. package/src/components/sparkbar/sparkbar.svelte +8 -1
  142. package/src/components/sparkbar/sparkbar.types.ts +5 -0
  143. package/src/components/spectrogram/spectrogram.css +7 -6
  144. package/src/components/spectrogram/spectrogram.schema.json +25 -0
  145. package/src/components/spectrogram/spectrogram.schema.ts +25 -0
  146. package/src/components/spectrogram/spectrogram.svelte +73 -28
  147. package/src/components/spectrogram/spectrogram.types.ts +5 -1
  148. package/src/components/spectrum-chart/spectrum-chart.css +8 -7
  149. package/src/components/spectrum-chart/spectrum-chart.schema.json +25 -0
  150. package/src/components/spectrum-chart/spectrum-chart.schema.ts +26 -0
  151. package/src/components/spectrum-chart/spectrum-chart.svelte +38 -12
  152. package/src/components/spectrum-chart/spectrum-chart.types.ts +5 -1
  153. package/src/components/statistic/statistic.css +6 -5
  154. package/src/components/statistic/statistic.schema.json +25 -0
  155. package/src/components/statistic/statistic.schema.ts +26 -0
  156. package/src/components/statistic/statistic.svelte +6 -1
  157. package/src/components/statistic/statistic.types.ts +5 -0
  158. package/src/components/waveform/waveform.css +8 -7
  159. package/src/components/waveform/waveform.schema.json +25 -0
  160. package/src/components/waveform/waveform.schema.ts +26 -0
  161. package/src/components/waveform/waveform.svelte +16 -3
  162. package/src/components/waveform/waveform.types.ts +5 -1
  163. package/src/index.ts +8 -0
  164. package/dist/server/index.server-6vbpz6dd.js +0 -690
@@ -0,0 +1,487 @@
1
+ import type {
2
+ BarChartDatum,
3
+ BarChartSeries,
4
+ ChartNumericValue,
5
+ } from '../../components/chart.types.ts';
6
+ import type { NormalizedXValue, PlacedPoint } from './chart-model-utilities.ts';
7
+
8
+ export const MAXIMUM_RENDERED_SERIES_POINTS = 2_000;
9
+
10
+ export type LayerDecimationSelection = {
11
+ sourceIndex: number;
12
+ forceGapLayerIndices: readonly number[];
13
+ };
14
+
15
+ function evenlySample<T>(values: readonly T[], count: number): T[] {
16
+ if (values.length <= count) return [...values];
17
+ if (count <= 1) return values[0] === undefined ? [] : [values[0]];
18
+ return Array.from({ length: count }, (_, index) => {
19
+ const sourceIndex = Math.round((index * (values.length - 1)) / (count - 1));
20
+ return values[sourceIndex]!;
21
+ });
22
+ }
23
+
24
+ function boundedGapHeavyIndices(points: PlacedPoint[], limit: number): number[] {
25
+ const finiteRuns: Array<{ end: number; representative: number }> = [];
26
+ let index = 0;
27
+ while (index < points.length) {
28
+ while (index < points.length && points[index]?.y === null) index += 1;
29
+ if (index >= points.length) break;
30
+
31
+ let representative = index;
32
+ let maximumMagnitude = Math.abs(points[index]?.y ?? 0);
33
+ while (index + 1 < points.length && points[index + 1]?.y !== null) {
34
+ index += 1;
35
+ const magnitude = Math.abs(points[index]?.y ?? 0);
36
+ if (magnitude > maximumMagnitude) {
37
+ maximumMagnitude = magnitude;
38
+ representative = index;
39
+ }
40
+ }
41
+ finiteRuns.push({ end: index, representative });
42
+ index += 1;
43
+ }
44
+
45
+ const maximumFiniteRuns = Math.max(1, Math.floor((limit + 1) / 2));
46
+ const sampledRuns = evenlySample(finiteRuns, maximumFiniteRuns);
47
+ const selected: number[] = [];
48
+ sampledRuns.forEach((run, runIndex) => {
49
+ const previousRun = sampledRuns[runIndex - 1];
50
+ if (previousRun) selected.push(previousRun.end + 1);
51
+ selected.push(run.representative);
52
+ });
53
+ return selected.sort((a, b) => a - b);
54
+ }
55
+
56
+ function layerExtremaIndices(
57
+ layers: ReadonlyArray<ReadonlyArray<number | null>>,
58
+ startIndex: number,
59
+ endIndex: number,
60
+ ): number[] {
61
+ const extrema = new Set<number>();
62
+ for (const layer of layers) {
63
+ let minimumIndex: number | undefined;
64
+ let maximumIndex: number | undefined;
65
+ let minimumValue = Number.POSITIVE_INFINITY;
66
+ let maximumValue = Number.NEGATIVE_INFINITY;
67
+ for (let index = startIndex; index < endIndex; index++) {
68
+ const value = layer[index];
69
+ if (value == null) continue;
70
+ if (value < minimumValue) {
71
+ minimumValue = value;
72
+ minimumIndex = index;
73
+ }
74
+ if (value > maximumValue) {
75
+ maximumValue = value;
76
+ maximumIndex = index;
77
+ }
78
+ }
79
+ if (minimumIndex !== undefined) extrema.add(minimumIndex);
80
+ if (maximumIndex !== undefined) extrema.add(maximumIndex);
81
+ }
82
+ return [...extrema];
83
+ }
84
+
85
+ function boundedGapHeavyLayerSelections(
86
+ layers: ReadonlyArray<ReadonlyArray<number | null>>,
87
+ pointCount: number,
88
+ limit: number,
89
+ ): LayerDecimationSelection[] {
90
+ const finiteIndices: number[] = [];
91
+ for (let index = 0; index < pointCount; index++) {
92
+ if (layers.some((layer) => layer[index] != null)) finiteIndices.push(index);
93
+ }
94
+
95
+ const maximumFiniteSamples = Math.max(1, Math.floor((limit + 1) / 2));
96
+ const retainedIndices = new Set<number>();
97
+ const priorityIndices = [
98
+ finiteIndices[0],
99
+ finiteIndices.at(-1),
100
+ ...layerExtremaIndices(layers, 0, pointCount),
101
+ ];
102
+ for (const index of priorityIndices) {
103
+ if (index !== undefined && retainedIndices.size < maximumFiniteSamples) {
104
+ retainedIndices.add(index);
105
+ }
106
+ }
107
+ const extremaPerBucket = Math.max(1, layers.length * 2);
108
+ const bucketCount = Math.floor((maximumFiniteSamples - retainedIndices.size) / extremaPerBucket);
109
+ for (let bucketIndex = 0; bucketIndex < bucketCount; bucketIndex++) {
110
+ const startIndex = Math.floor((bucketIndex * pointCount) / bucketCount);
111
+ const endIndex = Math.floor(((bucketIndex + 1) * pointCount) / bucketCount);
112
+ for (const index of layerExtremaIndices(layers, startIndex, endIndex)) {
113
+ if (retainedIndices.size < maximumFiniteSamples) retainedIndices.add(index);
114
+ }
115
+ }
116
+ const remainingCapacity = maximumFiniteSamples - retainedIndices.size;
117
+ if (remainingCapacity > 0) {
118
+ const coverageCandidates = finiteIndices.filter((index) => !retainedIndices.has(index));
119
+ for (const index of evenlySample(coverageCandidates, remainingCapacity)) {
120
+ retainedIndices.add(index);
121
+ }
122
+ }
123
+ const sampledIndices = [...retainedIndices].sort((a, b) => a - b);
124
+ const selectedSourceIndices: number[] = [];
125
+ sampledIndices.forEach((sourceIndex, selectionIndex) => {
126
+ const previousSourceIndex = sampledIndices[selectionIndex - 1];
127
+ if (previousSourceIndex !== undefined && sourceIndex > previousSourceIndex + 1) {
128
+ if (layers.some((layer) => layerHasOmittedGap(layer, previousSourceIndex, sourceIndex))) {
129
+ selectedSourceIndices.push(
130
+ previousSourceIndex + Math.floor((sourceIndex - previousSourceIndex) / 2),
131
+ );
132
+ }
133
+ }
134
+ selectedSourceIndices.push(sourceIndex);
135
+ });
136
+ return selectedSourceIndices.map((sourceIndex, selectionIndex) => {
137
+ const previousSourceIndex = selectedSourceIndices[selectionIndex - 1];
138
+ const forceGapLayerIndices: number[] = [];
139
+ if (previousSourceIndex !== undefined && sourceIndex > previousSourceIndex + 1) {
140
+ layers.forEach((layer, layerIndex) => {
141
+ if (layerHasOmittedGap(layer, previousSourceIndex, sourceIndex)) {
142
+ forceGapLayerIndices.push(layerIndex);
143
+ }
144
+ });
145
+ }
146
+ return { sourceIndex, forceGapLayerIndices };
147
+ });
148
+ }
149
+
150
+ function layerHasOmittedGap(
151
+ layer: ReadonlyArray<number | null>,
152
+ startIndex: number,
153
+ endIndex: number,
154
+ ): boolean {
155
+ if (layer[startIndex] == null || layer[endIndex] == null) return false;
156
+ for (let gapIndex = startIndex + 1; gapIndex < endIndex; gapIndex++) {
157
+ if (layer[gapIndex] == null) return true;
158
+ }
159
+ return false;
160
+ }
161
+
162
+ function addGapBoundaryIndices(
163
+ selected: Set<number>,
164
+ pointCount: number,
165
+ valueAt: (index: number) => number | null | undefined,
166
+ ): void {
167
+ for (let index = 1; index < pointCount; index++) {
168
+ const previousIsNull = valueAt(index - 1) == null;
169
+ const currentIsNull = valueAt(index) == null;
170
+ if (previousIsNull === currentIsNull) continue;
171
+ selected.add(index - 1);
172
+ selected.add(index);
173
+ }
174
+ }
175
+
176
+ export function decimationIndices(
177
+ points: PlacedPoint[],
178
+ maximumPoints = MAXIMUM_RENDERED_SERIES_POINTS,
179
+ ): number[] {
180
+ if (points.length <= maximumPoints || maximumPoints < 2) {
181
+ return points.map((_, index) => index);
182
+ }
183
+ const limit = Math.max(2, Math.floor(maximumPoints));
184
+ const interiorLength = points.length - 2;
185
+ const selected = new Set<number>([0, points.length - 1]);
186
+ addGapBoundaryIndices(selected, points.length, (index) => points[index]?.y);
187
+
188
+ // Every finite/null transition is structural. When the transitions alone
189
+ // exceed the budget, sample complete finite runs and retain a real null
190
+ // between them. This remains bounded without drawing across an omitted gap.
191
+ const remainingExtremaBudget = limit - selected.size;
192
+ const bucketCount = Math.floor(remainingExtremaBudget / 2);
193
+ if (bucketCount < 1) return boundedGapHeavyIndices(points, limit);
194
+
195
+ for (let bucketIndex = 0; bucketIndex < bucketCount; bucketIndex++) {
196
+ const start = 1 + Math.floor((bucketIndex * interiorLength) / bucketCount);
197
+ const end = 1 + Math.floor(((bucketIndex + 1) * interiorLength) / bucketCount);
198
+ if (end <= start) continue;
199
+ let minimumIndex: number | undefined;
200
+ let maximumIndex: number | undefined;
201
+ let minimumValue = Number.POSITIVE_INFINITY;
202
+ let maximumValue = Number.NEGATIVE_INFINITY;
203
+ for (let index = start; index < end; index++) {
204
+ const point = points[index];
205
+ if (!point) continue;
206
+ if (point.y === null) continue;
207
+ if (point.y < minimumValue) {
208
+ minimumValue = point.y;
209
+ minimumIndex = index;
210
+ }
211
+ if (point.y > maximumValue) {
212
+ maximumValue = point.y;
213
+ maximumIndex = index;
214
+ }
215
+ }
216
+ if (minimumIndex !== undefined) selected.add(minimumIndex);
217
+ if (maximumIndex !== undefined) selected.add(maximumIndex);
218
+ }
219
+
220
+ return [...selected].sort((a, b) => a - b);
221
+ }
222
+
223
+ export function decimationIndicesForLayers(
224
+ layers: ReadonlyArray<ReadonlyArray<number | null>>,
225
+ maximumPoints = MAXIMUM_RENDERED_SERIES_POINTS,
226
+ ): LayerDecimationSelection[] {
227
+ const pointCount = layers[0]?.length ?? 0;
228
+ if (pointCount <= maximumPoints || maximumPoints < 2) {
229
+ return Array.from({ length: pointCount }, (_, sourceIndex) => ({
230
+ sourceIndex,
231
+ forceGapLayerIndices: [],
232
+ }));
233
+ }
234
+ const limit = Math.max(2, Math.floor(maximumPoints));
235
+ const interiorLength = pointCount - 2;
236
+ const selected = new Set<number>([0, pointCount - 1]);
237
+ for (const layer of layers) {
238
+ addGapBoundaryIndices(selected, pointCount, (index) => layer[index]);
239
+ }
240
+
241
+ // Gap boundaries are shared across layers before extrema are bucketed, so
242
+ // every retained layer uses the same x keys without collapsing separate
243
+ // discontinuities into one marker.
244
+ const candidatesPerBucket = Math.max(1, layers.length * 2);
245
+ const bucketCount = Math.floor((limit - selected.size) / candidatesPerBucket);
246
+ if (bucketCount < 1) return boundedGapHeavyLayerSelections(layers, pointCount, limit);
247
+
248
+ for (let bucketIndex = 0; bucketIndex < bucketCount; bucketIndex++) {
249
+ const start = 1 + Math.floor((bucketIndex * interiorLength) / bucketCount);
250
+ const end = 1 + Math.floor(((bucketIndex + 1) * interiorLength) / bucketCount);
251
+ if (end <= start) continue;
252
+ for (const layer of layers) {
253
+ let minimumIndex: number | undefined;
254
+ let maximumIndex: number | undefined;
255
+ let minimumValue = Number.POSITIVE_INFINITY;
256
+ let maximumValue = Number.NEGATIVE_INFINITY;
257
+ for (let index = start; index < end; index++) {
258
+ const value = layer[index];
259
+ if (value === null || value === undefined) continue;
260
+ if (value < minimumValue) {
261
+ minimumValue = value;
262
+ minimumIndex = index;
263
+ }
264
+ if (value > maximumValue) {
265
+ maximumValue = value;
266
+ maximumIndex = index;
267
+ }
268
+ }
269
+ if (minimumIndex !== undefined) selected.add(minimumIndex);
270
+ if (maximumIndex !== undefined) selected.add(maximumIndex);
271
+ }
272
+ }
273
+
274
+ return [...selected]
275
+ .sort((a, b) => a - b)
276
+ .map((sourceIndex) => ({ sourceIndex, forceGapLayerIndices: [] }));
277
+ }
278
+
279
+ export function decimatePlacedPoints(
280
+ points: PlacedPoint[],
281
+ maximumPoints = MAXIMUM_RENDERED_SERIES_POINTS,
282
+ ): PlacedPoint[] {
283
+ return decimationIndices(points, maximumPoints).map((index) => points[index]!);
284
+ }
285
+ export function normalizeNumericValue(
286
+ componentId: string,
287
+ seriesId: string,
288
+ xLabel: string,
289
+ value: ChartNumericValue,
290
+ ): number | null {
291
+ if (value === null || value === undefined) return null;
292
+ if (!Number.isFinite(value)) {
293
+ throw new Error(
294
+ `[cinder/${componentId}] rule=non-finite-y series="${seriesId}" x="${xLabel}": y values must be finite numbers.`,
295
+ );
296
+ }
297
+ return value;
298
+ }
299
+
300
+ export function sortXValues(values: NormalizedXValue[]): NormalizedXValue[] {
301
+ if (values[0]?.kind === 'string') return values;
302
+ return values.slice().sort((a, b) => Number(a.comparable) - Number(b.comparable));
303
+ }
304
+
305
+ export function createPaddedDomain(values: number[]): [number, number] {
306
+ if (values.length === 0) return [-1, 1];
307
+ // Avoid spread-call argument-limit cliffs on large arrays.
308
+ let minimum = values[0]!;
309
+ let maximum = values[0]!;
310
+ for (let index = 1; index < values.length; index++) {
311
+ const value = values[index]!;
312
+ if (value < minimum) minimum = value;
313
+ if (value > maximum) maximum = value;
314
+ }
315
+ if (minimum === maximum) return [minimum - 1, maximum + 1];
316
+ return [Math.min(0, minimum), Math.max(0, maximum)];
317
+ }
318
+
319
+ export type LinearScale = ((value: number) => number) & {
320
+ ticks: (count: number) => number[];
321
+ };
322
+
323
+ export type BandlikeScale = ((value: string) => number | undefined) & {
324
+ bandwidth?: () => number;
325
+ };
326
+
327
+ export type BandScale = ((value: string) => number | undefined) & {
328
+ bandwidth: () => number;
329
+ };
330
+
331
+ export function createLinearScale(domain: [number, number], range: [number, number]): LinearScale {
332
+ const [domainMinimum, domainMaximum] =
333
+ domain[0] === domain[1] ? [domain[0] - 1, domain[1] + 1] : domain;
334
+ const [rangeMinimum, rangeMaximum] = range;
335
+ const domainSpan = domainMaximum - domainMinimum;
336
+ const rangeSpan = rangeMaximum - rangeMinimum;
337
+ // Build the scale by composing the call signature with the `ticks` method
338
+ // up front, so the intersection type holds without an `as` cast.
339
+ const scale = Object.assign(
340
+ (value: number): number => rangeMinimum + ((value - domainMinimum) / domainSpan) * rangeSpan,
341
+ {
342
+ ticks: (count: number): number[] => createTicks([domainMinimum, domainMaximum], count),
343
+ },
344
+ ) satisfies LinearScale;
345
+ return scale;
346
+ }
347
+
348
+ export function createPointScale(
349
+ domain: string[],
350
+ range: [number, number],
351
+ padding: number,
352
+ ): BandlikeScale {
353
+ const positions = new Map<string, number>();
354
+
355
+ const [rangeMinimum, rangeMaximum] = range;
356
+ if (domain.length === 1) {
357
+ positions.set(domain[0] ?? '', rangeMinimum + (rangeMaximum - rangeMinimum) / 2);
358
+ } else {
359
+ const step = (rangeMaximum - rangeMinimum) / Math.max(1, domain.length - 1 + padding * 2);
360
+ const start = rangeMinimum + step * padding;
361
+ domain.forEach((value, index) => positions.set(value, start + step * index));
362
+ }
363
+
364
+ return (value: string) => positions.get(value);
365
+ }
366
+
367
+ export function createBandScale(
368
+ domain: string[],
369
+ range: [number, number],
370
+ padding: number,
371
+ ): BandScale {
372
+ const positions = new Map<string, number>();
373
+ const [rangeMinimum, rangeMaximum] = range;
374
+ const availableSize = Math.max(0, rangeMaximum - rangeMinimum);
375
+ const step =
376
+ domain.length === 0
377
+ ? 0
378
+ : availableSize / Math.max(1, domain.length + padding * (domain.length - 1));
379
+ const bandWidth = Math.max(0, step * (1 - padding));
380
+ domain.forEach((value, index) => positions.set(value, rangeMinimum + step * index));
381
+
382
+ // Same Object.assign + satisfies pattern as createLinearScale — the
383
+ // intersection type is satisfied at construction, not asserted after.
384
+ const scale = Object.assign((value: string) => positions.get(value), {
385
+ bandwidth: (): number => bandWidth,
386
+ }) satisfies BandScale;
387
+ return scale;
388
+ }
389
+
390
+ export function createNumericDomain(values: NormalizedXValue[]): [number, number] {
391
+ if (values.length === 0) return [0, 1];
392
+ let minimum = Number(values[0]!.comparable);
393
+ let maximum = minimum;
394
+ for (let index = 1; index < values.length; index++) {
395
+ const numeric = Number(values[index]!.comparable);
396
+ if (numeric < minimum) minimum = numeric;
397
+ if (numeric > maximum) maximum = numeric;
398
+ }
399
+ return minimum === maximum ? [minimum - 1, maximum + 1] : [minimum, maximum];
400
+ }
401
+
402
+ export function createTicks(domain: [number, number], count: number): number[] {
403
+ const safeCount = Math.max(1, Math.floor(count));
404
+ if (safeCount === 1) return [domain[0]];
405
+ const step = (domain[1] - domain[0]) / (safeCount - 1);
406
+ return Array.from({ length: safeCount }, (_, index) => domain[0] + step * index);
407
+ }
408
+
409
+ export function createLinePath(points: Array<{ x: number; y: number | null }>): string {
410
+ if (points.length === 0) return '';
411
+ const commands: string[] = [];
412
+ for (const [index, point] of points.entries()) {
413
+ if (point.y === null) continue;
414
+ const previous = points[index - 1];
415
+ commands.push(
416
+ previous?.y === null || commands.length === 0
417
+ ? `M${formatPathNumber(point.x)},${formatPathNumber(point.y)}`
418
+ : `L${formatPathNumber(point.x)},${formatPathNumber(point.y)}`,
419
+ );
420
+ }
421
+ return commands.join('');
422
+ }
423
+
424
+ export function createAreaPath(
425
+ points: Array<{ x: number; y: number | null; y0?: number }>,
426
+ baseline: number | undefined,
427
+ ): string {
428
+ const segments: Array<Array<{ x: number; y: number; y0?: number }>> = [];
429
+ let segment: Array<{ x: number; y: number; y0?: number }> = [];
430
+ for (const point of points) {
431
+ if (point.y === null) {
432
+ if (segment.length > 0) segments.push(segment);
433
+ segment = [];
434
+ continue;
435
+ }
436
+ segment.push({ ...point, y: point.y });
437
+ }
438
+ if (segment.length > 0) segments.push(segment);
439
+
440
+ return segments
441
+ .map((visiblePoints) => {
442
+ const firstPoint = visiblePoints[0]!;
443
+ const lowerPoints: string[] = [];
444
+ for (let index = visiblePoints.length - 1; index >= 0; index--) {
445
+ const point = visiblePoints[index]!;
446
+ lowerPoints.push(
447
+ `L${formatPathNumber(point.x)},${formatPathNumber(point.y0 ?? baseline ?? 0)}`,
448
+ );
449
+ }
450
+ return [
451
+ `M${formatPathNumber(firstPoint.x)},${formatPathNumber(firstPoint.y0 ?? baseline ?? 0)}`,
452
+ `L${formatPathNumber(firstPoint.x)},${formatPathNumber(firstPoint.y)}`,
453
+ ...visiblePoints
454
+ .slice(1)
455
+ .map((point) => `L${formatPathNumber(point.x)},${formatPathNumber(point.y)}`),
456
+ ...lowerPoints,
457
+ 'Z',
458
+ ].join('');
459
+ })
460
+ .join('');
461
+ }
462
+
463
+ export function createStackedBarDomainValues(
464
+ datumByKey: Map<string, BarChartDatum>,
465
+ categories: NormalizedXValue[],
466
+ series: BarChartSeries[],
467
+ ): number[] {
468
+ const values = [0];
469
+ for (const category of categories) {
470
+ const datum = datumByKey.get(category.key);
471
+ if (!datum) continue;
472
+ let positiveTotal = 0;
473
+ let negativeTotal = 0;
474
+ for (const item of series) {
475
+ const value = datum[item.valueKey];
476
+ if (typeof value !== 'number') continue;
477
+ if (value >= 0) positiveTotal += value;
478
+ else negativeTotal += value;
479
+ }
480
+ values.push(positiveTotal, negativeTotal);
481
+ }
482
+ return values;
483
+ }
484
+
485
+ function formatPathNumber(value: number): string {
486
+ return Number(value.toFixed(3)).toString();
487
+ }