@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,450 @@
1
+ import type {
2
+ ChartAxisConfiguration,
3
+ ChartCartesianSeries,
4
+ ChartTheme,
5
+ ChartXAxisConfiguration,
6
+ } from '../../components/chart.types.ts';
7
+ import { createChartGeometry } from './chart-layout.ts';
8
+ import {
9
+ assertUniqueSeriesIds,
10
+ assertValidChartNumber,
11
+ assertValidTickCount,
12
+ chartPaletteColor,
13
+ formatNumericValue,
14
+ formatXValue,
15
+ normalizeXValue,
16
+ resolveChartTheme,
17
+ type CartesianChartModel,
18
+ type ChartTarget,
19
+ type ChartXTick,
20
+ type NormalizedPoint,
21
+ type NormalizedXValue,
22
+ type PlacedPoint,
23
+ } from './chart-model-utilities.ts';
24
+ import {
25
+ createAreaPath,
26
+ createLinearScale,
27
+ createLinePath,
28
+ createNumericDomain,
29
+ createPaddedDomain,
30
+ createPointScale,
31
+ createTicks,
32
+ decimatePlacedPoints,
33
+ decimationIndicesForLayers,
34
+ MAXIMUM_RENDERED_SERIES_POINTS,
35
+ normalizeNumericValue,
36
+ sortXValues,
37
+ type BandlikeScale,
38
+ type LinearScale,
39
+ } from './chart-scale.ts';
40
+
41
+ const DEFAULT_MAXIMUM_X_TICK_COUNT = 8;
42
+
43
+ export function createCartesianModel(options: {
44
+ componentId: 'line-chart' | 'area-chart';
45
+ series: ChartCartesianSeries[];
46
+ hiddenSeriesIds: string[];
47
+ width: number;
48
+ height: number;
49
+ xAxis?: ChartXAxisConfiguration | undefined;
50
+ yAxis?: ChartAxisConfiguration | undefined;
51
+ stackedArea?: boolean;
52
+ theme?: ChartTheme | undefined;
53
+ measureText?: boolean | undefined;
54
+ measurementElement?: Element | undefined;
55
+ measurementVersion?: number | undefined;
56
+ }): CartesianChartModel {
57
+ const {
58
+ componentId,
59
+ series,
60
+ hiddenSeriesIds,
61
+ width,
62
+ height,
63
+ xAxis,
64
+ yAxis,
65
+ stackedArea = false,
66
+ theme,
67
+ measureText = false,
68
+ measurementElement,
69
+ measurementVersion = 0,
70
+ } = options;
71
+ assertUniqueSeriesIds(componentId, series);
72
+ assertValidChartNumber(componentId, 'invalid-height', height, 'height');
73
+ assertValidTickCount(componentId, xAxis);
74
+ assertValidTickCount(componentId, yAxis);
75
+
76
+ const resolvedTheme = resolveChartTheme(theme);
77
+ let geometry = createChartGeometry(width, height, {
78
+ measureText,
79
+ measurementElement,
80
+ measurementVersion,
81
+ });
82
+ const allKinds = new Set<string>();
83
+ const xValuesByKey = new Map<string, NormalizedXValue>();
84
+
85
+ const normalizedSeries = series.map((item, seriesIndex) => {
86
+ const seenX = new Set<string>();
87
+ const color = item.color ?? chartPaletteColor(seriesIndex, resolvedTheme.palette);
88
+ const points: NormalizedPoint[] = item.data.map((point, pointIndex) => {
89
+ const x = normalizeXValue(point.x);
90
+ allKinds.add(x.kind);
91
+ if (seenX.has(x.key)) {
92
+ throw new Error(
93
+ `[cinder/${componentId}] rule=duplicate-x series="${item.id}" x="${x.label}": duplicate x values are not supported.`,
94
+ );
95
+ }
96
+ seenX.add(x.key);
97
+ xValuesByKey.set(x.key, x);
98
+ const y = normalizeNumericValue(componentId, item.id, x.label, point.y);
99
+ return {
100
+ seriesId: item.id,
101
+ seriesLabel: item.label,
102
+ color,
103
+ x,
104
+ y,
105
+ originalY: point.y,
106
+ index: pointIndex,
107
+ };
108
+ });
109
+ return { id: item.id, label: item.label, color, points };
110
+ });
111
+
112
+ if (allKinds.size > 1) {
113
+ throw new Error(
114
+ `[cinder/${componentId}] rule=mixed-x-domain-kind: all x values must share one domain kind.`,
115
+ );
116
+ }
117
+
118
+ if (stackedArea) {
119
+ for (const item of normalizedSeries) {
120
+ for (const point of item.points) {
121
+ if ((point.y ?? 0) < 0) {
122
+ throw new Error(
123
+ `[cinder/${componentId}] rule=negative-stacked-area series="${item.id}" x="${point.x.label}": stacked areas do not support negative values.`,
124
+ );
125
+ }
126
+ }
127
+ }
128
+ }
129
+
130
+ const sortedXValues = sortXValues([...xValuesByKey.values()]);
131
+ // Index by key so we can preserve canonical x-domain order when sorting a
132
+ // series' own points (string domains use insertion order, numeric/date sort
133
+ // by `comparable`). Avoids `Number(stringKey)` returning NaN.
134
+ const orderByKey = new Map(sortedXValues.map((value, index) => [value.key, index]));
135
+
136
+ // Visible-only domain values. Hidden series no longer compress the visible
137
+ // chart against invisible data, and the legend toggle's effect on scale is
138
+ // consistent with the table and targets.
139
+ const visibleNumericValues: number[] = [];
140
+ for (const item of normalizedSeries) {
141
+ if (hiddenSeriesIds.includes(item.id)) continue;
142
+ for (const point of item.points) {
143
+ if (point.y !== null) visibleNumericValues.push(point.y);
144
+ }
145
+ }
146
+
147
+ const stackedTotalsByKey = new Map(sortedXValues.map((value) => [value.key, 0]));
148
+ if (stackedArea) {
149
+ for (const item of normalizedSeries) {
150
+ if (hiddenSeriesIds.includes(item.id)) continue;
151
+ for (const point of item.points) {
152
+ if (point.y === null) continue;
153
+ stackedTotalsByKey.set(point.x.key, (stackedTotalsByKey.get(point.x.key) ?? 0) + point.y);
154
+ }
155
+ }
156
+ }
157
+ const domainValues =
158
+ stackedArea || componentId === 'area-chart'
159
+ ? [0, ...(stackedArea ? stackedTotalsByKey.values() : visibleNumericValues)]
160
+ : visibleNumericValues;
161
+ const [yMinimum, yMaximum] = createPaddedDomain(domainValues);
162
+ const tickCount =
163
+ xAxis?.tickCount ?? Math.min(sortedXValues.length, DEFAULT_MAXIMUM_X_TICK_COUNT);
164
+ const preliminaryXTicks = buildXAxisTicks(sortedXValues, tickCount, xAxis, () => 0);
165
+ const yTicks = createTicks([yMinimum, yMaximum], yAxis?.tickCount ?? 5);
166
+ geometry = createChartGeometry(width, height, {
167
+ xTickLabels: preliminaryXTicks.map((tick) => tick.label),
168
+ yTickLabels: yTicks.map((tick, index) => formatNumericValue(tick, yAxis, undefined, { index })),
169
+ xAxis,
170
+ yAxis,
171
+ measureText,
172
+ measurementElement,
173
+ measurementVersion,
174
+ });
175
+
176
+ // Split scales into two correctly-typed variables so the use site can
177
+ // discriminate on `kind` without `as` casts.
178
+ const xStringScale: BandlikeScale | undefined =
179
+ sortedXValues[0]?.kind === 'string'
180
+ ? createPointScale(
181
+ sortedXValues.map((value) => value.key),
182
+ [0, geometry.plotWidth],
183
+ 0.5,
184
+ )
185
+ : undefined;
186
+ const xNumericScale: LinearScale | undefined =
187
+ sortedXValues[0] && sortedXValues[0].kind !== 'string'
188
+ ? createLinearScale(createNumericDomain(sortedXValues), [0, geometry.plotWidth])
189
+ : undefined;
190
+ const yScale = createLinearScale([yMinimum, yMaximum], [geometry.plotHeight, 0]);
191
+
192
+ function scaleX(value: NormalizedXValue): number {
193
+ if (value.kind === 'string') return xStringScale?.(value.key) ?? 0;
194
+ return xNumericScale?.(Number(value.comparable)) ?? 0;
195
+ }
196
+
197
+ // Build x-axis ticks placed at their true scaled positions so labels and
198
+ // points line up for numeric and date domains.
199
+ const xTicks: ChartXTick[] = buildXAxisTicks(sortedXValues, tickCount, xAxis, scaleX);
200
+
201
+ const targets: ChartTarget[] = [];
202
+ const tableRows: CartesianChartModel['tableRows'] = [];
203
+ const stackedOffsetsByKey = new Map(sortedXValues.map((value) => [value.key, 0]));
204
+ const normalizedPointsBySeriesId = stackedArea
205
+ ? new Map(
206
+ normalizedSeries.map((item) => [
207
+ item.id,
208
+ new Map(item.points.map((point) => [point.x.key, point])),
209
+ ]),
210
+ )
211
+ : undefined;
212
+ const stackedRenderXValues = stackedArea
213
+ ? sortedXValues.filter((value) =>
214
+ normalizedSeries.some(
215
+ (item) =>
216
+ !hiddenSeriesIds.includes(item.id) &&
217
+ normalizedPointsBySeriesId?.get(item.id)?.has(value.key),
218
+ ),
219
+ )
220
+ : undefined;
221
+ const stackedRenderDomain = stackedRenderXValues ?? [];
222
+ const stackedVisibleSeries = stackedArea
223
+ ? normalizedSeries.filter((item) => !hiddenSeriesIds.includes(item.id))
224
+ : [];
225
+ const stackedLayerIndexBySeriesId = new Map(
226
+ stackedVisibleSeries.map((item, layerIndex) => [item.id, layerIndex]),
227
+ );
228
+ const stackedBoundaryLayers = stackedArea
229
+ ? (() => {
230
+ const cumulativeValuesByKey = new Map(stackedRenderDomain.map((value) => [value.key, 0]));
231
+ return stackedVisibleSeries.map((item) =>
232
+ stackedRenderDomain.map((value) => {
233
+ const point = normalizedPointsBySeriesId?.get(item.id)?.get(value.key);
234
+ if (point?.y === null || point?.y === undefined) return null;
235
+ const cumulativeValue = (cumulativeValuesByKey.get(value.key) ?? 0) + point.y;
236
+ cumulativeValuesByKey.set(value.key, cumulativeValue);
237
+ return cumulativeValue;
238
+ }),
239
+ );
240
+ })()
241
+ : undefined;
242
+ const stackedRenderSelections = stackedBoundaryLayers
243
+ ? decimationIndicesForLayers(stackedBoundaryLayers)
244
+ : undefined;
245
+ const renderedSeries = normalizedSeries.map((item) => {
246
+ const hidden = hiddenSeriesIds.includes(item.id);
247
+ const points = item.points
248
+ .slice()
249
+ .sort((a, b) => (orderByKey.get(a.x.key) ?? 0) - (orderByKey.get(b.x.key) ?? 0));
250
+
251
+ const placedPoints: PlacedPoint[] = points.map((point) => {
252
+ const lowerValue = stackedArea ? (stackedOffsetsByKey.get(point.x.key) ?? 0) : 0;
253
+ const upperValue = lowerValue + (point.y ?? 0);
254
+ return {
255
+ ...point,
256
+ pixelX: scaleX(point.x),
257
+ pixelY: yScale(stackedArea ? upperValue : (point.y ?? 0)),
258
+ pixelY0: yScale(stackedArea ? lowerValue : 0),
259
+ };
260
+ });
261
+ const placedPointsByKey = stackedRenderSelections
262
+ ? new Map(placedPoints.map((point) => [point.x.key, point]))
263
+ : undefined;
264
+ const renderPoints = stackedRenderSelections
265
+ ? stackedRenderSelections.map(({ sourceIndex, forceGapLayerIndices }) => {
266
+ const x = stackedRenderDomain[sourceIndex]!;
267
+ const layerIndex = stackedLayerIndexBySeriesId.get(item.id);
268
+ const forceGap = layerIndex !== undefined && forceGapLayerIndices.includes(layerIndex);
269
+ if (!forceGap) {
270
+ const placedPoint = placedPointsByKey?.get(x.key);
271
+ if (placedPoint) return placedPoint;
272
+ }
273
+ return {
274
+ seriesId: item.id,
275
+ seriesLabel: item.label,
276
+ color: item.color,
277
+ x,
278
+ y: null,
279
+ originalY: null,
280
+ index: -1,
281
+ pixelX: scaleX(x),
282
+ pixelY: 0,
283
+ pixelY0: stackedArea ? yScale(stackedOffsetsByKey.get(x.key) ?? 0) : yScale(0),
284
+ } satisfies PlacedPoint;
285
+ })
286
+ : decimatePlacedPoints(placedPoints);
287
+ const coordinates = renderPoints.map((placed) => ({
288
+ x: placed.pixelX,
289
+ y: placed.y === null ? null : placed.pixelY,
290
+ y0: placed.pixelY0,
291
+ }));
292
+
293
+ if (!hidden) {
294
+ for (const placed of placedPoints) {
295
+ if (placed.y === null) continue;
296
+ const xLabel = formatXValue(placed.x, xAxis, {
297
+ seriesId: item.id,
298
+ seriesLabel: item.label,
299
+ index: placed.index,
300
+ });
301
+ const valueLabel = formatNumericValue(
302
+ placed.y ?? 0,
303
+ yAxis,
304
+ series.find((entry) => entry.id === item.id)?.valueFormatter,
305
+ { seriesId: item.id, seriesLabel: item.label, index: placed.index },
306
+ );
307
+ targets.push({
308
+ id: `${item.id}-${placed.x.key}`,
309
+ seriesId: item.id,
310
+ seriesLabel: item.label,
311
+ xLabel,
312
+ valueLabel,
313
+ x: placed.pixelX,
314
+ y: placed.pixelY,
315
+ color: item.color,
316
+ });
317
+ tableRows.push({
318
+ id: `${item.id}-${placed.x.key}`,
319
+ seriesId: item.id,
320
+ seriesLabel: item.label,
321
+ xLabel,
322
+ valueLabel,
323
+ });
324
+ }
325
+ if (stackedArea) {
326
+ for (const placed of placedPoints) {
327
+ stackedOffsetsByKey.set(
328
+ placed.x.key,
329
+ (stackedOffsetsByKey.get(placed.x.key) ?? 0) + (placed.y ?? 0),
330
+ );
331
+ }
332
+ }
333
+ }
334
+ return {
335
+ ...item,
336
+ hidden,
337
+ points: hidden ? [] : renderPoints,
338
+ path: hidden ? '' : createLinePath(coordinates),
339
+ areaPath: hidden
340
+ ? ''
341
+ : createAreaPath(coordinates, stackedArea ? undefined : geometry.plotHeight),
342
+ };
343
+ });
344
+
345
+ targets.sort((a, b) => a.x - b.x);
346
+
347
+ return {
348
+ geometry,
349
+ xTicks,
350
+ yTicks,
351
+ normalizedSeries: renderedSeries,
352
+ tableRows: evenlySampleTableRows(tableRows, MAXIMUM_RENDERED_SERIES_POINTS),
353
+ targets,
354
+ empty: targets.length === 0,
355
+ yDomain: [yMinimum, yMaximum],
356
+ theme: resolvedTheme,
357
+ marks: series.map((item) => ({
358
+ seriesId: item.id,
359
+ descriptors:
360
+ componentId === 'line-chart'
361
+ ? [
362
+ { type: 'line' as const, data: item.data },
363
+ { type: 'point' as const, data: item.data },
364
+ ]
365
+ : [
366
+ { type: 'area' as const, data: item.data },
367
+ { type: 'line' as const, data: item.data },
368
+ ],
369
+ })),
370
+ };
371
+ }
372
+
373
+ function evenlySampleTableRows(
374
+ rows: CartesianChartModel['tableRows'],
375
+ maximumRows: number,
376
+ ): CartesianChartModel['tableRows'] {
377
+ if (rows.length <= maximumRows) return rows;
378
+ if (maximumRows <= 0) return [];
379
+
380
+ const rowsBySeriesId = new Map<string, CartesianChartModel['tableRows']>();
381
+ for (const row of rows) {
382
+ const seriesRows = rowsBySeriesId.get(row.seriesId);
383
+ if (seriesRows) seriesRows.push(row);
384
+ else rowsBySeriesId.set(row.seriesId, [row]);
385
+ }
386
+ const seriesGroups = [...rowsBySeriesId.values()];
387
+ if (seriesGroups.length >= maximumRows) {
388
+ return seriesGroups.slice(0, maximumRows).map((group) => group[0]!);
389
+ }
390
+
391
+ const remainingRows = maximumRows - seriesGroups.length;
392
+ const capacities = seriesGroups.map((group) => group.length - 1);
393
+ const totalCapacity = capacities.reduce((total, capacity) => total + capacity, 0);
394
+ const exactShares = capacities.map((capacity) => (remainingRows * capacity) / totalCapacity);
395
+ const allocations = exactShares.map((share) => 1 + Math.floor(share));
396
+ let undistributedRows = maximumRows - allocations.reduce((total, count) => total + count, 0);
397
+ const allocationOrder = exactShares
398
+ .map((share, index) => ({ index, remainder: share - Math.floor(share) }))
399
+ .sort((a, b) => b.remainder - a.remainder || a.index - b.index);
400
+ for (const { index } of allocationOrder) {
401
+ if (undistributedRows === 0) break;
402
+ if (allocations[index]! >= seriesGroups[index]!.length) continue;
403
+ allocations[index]! += 1;
404
+ undistributedRows -= 1;
405
+ }
406
+
407
+ return seriesGroups.flatMap((group, index) => evenlySampleRows(group, allocations[index]!));
408
+ }
409
+
410
+ function evenlySampleRows<T>(rows: T[], requestedRows: number): T[] {
411
+ if (rows.length <= requestedRows) return rows;
412
+ if (requestedRows <= 1) return rows[0] === undefined ? [] : [rows[0]];
413
+ return Array.from({ length: requestedRows }, (_, index) => {
414
+ const sourceIndex = Math.round((index * (rows.length - 1)) / (requestedRows - 1));
415
+ return rows[sourceIndex]!;
416
+ });
417
+ }
418
+
419
+ function buildXAxisTicks(
420
+ sortedXValues: NormalizedXValue[],
421
+ tickCount: number,
422
+ xAxis: ChartAxisConfiguration | undefined,
423
+ scaleX: (value: NormalizedXValue) => number,
424
+ ): ChartXTick[] {
425
+ if (sortedXValues.length === 0) return [];
426
+ const safeTickCount = Math.max(1, Math.min(tickCount, sortedXValues.length));
427
+ if (safeTickCount >= sortedXValues.length) {
428
+ return sortedXValues.map((value, index) => ({
429
+ label: formatXValue(value, xAxis, { index }),
430
+ x: scaleX(value),
431
+ }));
432
+ }
433
+ if (safeTickCount === 1) {
434
+ const value = sortedXValues[0];
435
+ return value ? [{ label: formatXValue(value, xAxis, { index: 0 }), x: scaleX(value) }] : [];
436
+ }
437
+ // Sample evenly across the sorted x values.
438
+ const step = (sortedXValues.length - 1) / (safeTickCount - 1);
439
+ const ticks: ChartXTick[] = [];
440
+ for (let i = 0; i < safeTickCount; i++) {
441
+ const sourceIndex = Math.round(i * step);
442
+ const value = sortedXValues[sourceIndex];
443
+ if (!value) continue;
444
+ ticks.push({
445
+ label: formatXValue(value, xAxis, { index: sourceIndex }),
446
+ x: scaleX(value),
447
+ });
448
+ }
449
+ return ticks;
450
+ }
@@ -57,13 +57,20 @@ export class ChartInteraction {
57
57
  this.focusedTarget = undefined;
58
58
  return;
59
59
  }
60
- if (!this.#includesTarget(this.pointerTarget, targets))
61
- this.pointerTarget = undefined;
62
- if (!this.#includesTarget(this.focusedTarget, targets))
63
- this.focusedTarget = undefined;
60
+ const refreshedPointerTarget = this.#refreshTarget(this.pointerTarget, targets);
61
+ const refreshedFocusedTarget = this.#refreshTarget(this.focusedTarget, targets);
62
+ if (refreshedPointerTarget !== this.pointerTarget)
63
+ this.pointerTarget = refreshedPointerTarget;
64
+ if (refreshedFocusedTarget !== this.focusedTarget)
65
+ this.focusedTarget = refreshedFocusedTarget;
64
66
  }
65
- #includesTarget(candidate, targets) {
66
- return Boolean(candidate && targets.some((target) => target.id === candidate.id));
67
+ #refreshTarget(candidate, targets) {
68
+ if (!candidate)
69
+ return undefined;
70
+ const latest = targets.find((target) => target.id === candidate.id);
71
+ if (!latest)
72
+ return undefined;
73
+ return chartTargetsEqual(candidate, latest) ? candidate : latest;
67
74
  }
68
75
  /**
69
76
  * Updates `pointerTarget` from a PointerEvent fired on the SVG hit surface.
@@ -148,3 +155,15 @@ export class ChartInteraction {
148
155
  return toggleSeriesId(hiddenSeriesIds, seriesId);
149
156
  }
150
157
  }
158
+ function chartTargetsEqual(left, right) {
159
+ return (left.id === right.id &&
160
+ left.seriesId === right.seriesId &&
161
+ left.seriesLabel === right.seriesLabel &&
162
+ left.xLabel === right.xLabel &&
163
+ left.valueLabel === right.valueLabel &&
164
+ left.x === right.x &&
165
+ left.y === right.y &&
166
+ left.width === right.width &&
167
+ left.height === right.height &&
168
+ left.color === right.color);
169
+ }