@opendata-ai/openchart-engine 6.28.6 → 7.0.2
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/README.md +1 -0
- package/dist/index.d.ts +8 -11
- package/dist/index.js +12307 -11338
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/__test-fixtures__/specs.ts +3 -0
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +452 -377
- package/src/__tests__/axes.test.ts +75 -0
- package/src/__tests__/compile-chart.test.ts +304 -0
- package/src/__tests__/dimensions.test.ts +224 -0
- package/src/__tests__/legend.test.ts +44 -3
- package/src/annotations/__tests__/compute.test.ts +111 -0
- package/src/annotations/__tests__/resolve-text.test.ts +288 -0
- package/src/annotations/constants.ts +20 -0
- package/src/annotations/resolve-text.ts +161 -7
- package/src/charts/bar/compute.ts +24 -0
- package/src/charts/bar/labels.ts +1 -0
- package/src/charts/column/compute.ts +33 -1
- package/src/charts/column/labels.ts +1 -0
- package/src/charts/dot/labels.ts +1 -0
- package/src/charts/line/__tests__/compute.test.ts +153 -3
- package/src/charts/line/area.ts +111 -23
- package/src/charts/line/compute.ts +40 -10
- package/src/charts/line/index.ts +34 -7
- package/src/charts/line/labels.ts +29 -0
- package/src/charts/pie/labels.ts +1 -0
- package/src/compile/layer.ts +498 -0
- package/src/compile.ts +221 -586
- package/src/compiler/__tests__/sparkline-defaults.test.ts +153 -0
- package/src/compiler/normalize.ts +12 -1
- package/src/compiler/sparkline-defaults.ts +138 -0
- package/src/compiler/types.ts +8 -0
- package/src/endpoint-labels/__tests__/compute.test.ts +438 -0
- package/src/endpoint-labels/compute.ts +417 -0
- package/src/endpoint-labels/constants.ts +54 -0
- package/src/endpoint-labels/format.ts +30 -0
- package/src/endpoint-labels/predict.ts +108 -0
- package/src/graphs/compile-graph.ts +1 -0
- package/src/layout/axes.ts +27 -2
- package/src/layout/dimensions.ts +282 -34
- package/src/layout/metrics.ts +118 -0
- package/src/layout/scales.ts +41 -4
- package/src/legend/__tests__/suppression.test.ts +294 -0
- package/src/legend/compute.ts +50 -40
- package/src/legend/suppression.ts +204 -0
- package/src/sankey/compile-sankey.ts +2 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the endpoint-labels compute module.
|
|
3
|
+
*
|
|
4
|
+
* Covers:
|
|
5
|
+
* - Multi-series produces N entries
|
|
6
|
+
* - Long labels wrap to multiple lines
|
|
7
|
+
* - `endpointLabels: false` returns an empty layout
|
|
8
|
+
* - Bidirectional collision sweep displaces overlapping entries
|
|
9
|
+
* - `showLeader: true` when an entry is displaced past the threshold
|
|
10
|
+
* - Entries clamp at the chart top/bottom edges
|
|
11
|
+
* - Marker positions are correct (right edge of chart area, on the line)
|
|
12
|
+
* - Single-series produces an empty layout
|
|
13
|
+
* - Compact strategy returns empty
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { AreaMark, LineMark, Mark, Rect, ResolvedTheme } from '@opendata-ai/openchart-core';
|
|
17
|
+
import { resolveTheme } from '@opendata-ai/openchart-core';
|
|
18
|
+
import { describe, expect, it } from 'vitest';
|
|
19
|
+
|
|
20
|
+
import type { NormalizedChartSpec } from '../../compiler/types';
|
|
21
|
+
import { bidirectionalSweep, computeEndpointLabels } from '../compute';
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Fixtures
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
const chartArea: Rect = { x: 50, y: 20, width: 500, height: 300 };
|
|
28
|
+
const theme: ResolvedTheme = resolveTheme();
|
|
29
|
+
|
|
30
|
+
function makeSpec(overrides: Partial<NormalizedChartSpec> = {}): NormalizedChartSpec {
|
|
31
|
+
return {
|
|
32
|
+
markType: 'line',
|
|
33
|
+
markDef: { type: 'line' },
|
|
34
|
+
data: [
|
|
35
|
+
{ date: '2020', value: 10, country: 'US' },
|
|
36
|
+
{ date: '2021', value: 40, country: 'US' },
|
|
37
|
+
{ date: '2020', value: 5, country: 'UK' },
|
|
38
|
+
{ date: '2021', value: 35, country: 'UK' },
|
|
39
|
+
],
|
|
40
|
+
encoding: {
|
|
41
|
+
x: { field: 'date', type: 'temporal' },
|
|
42
|
+
y: { field: 'value', type: 'quantitative' },
|
|
43
|
+
color: { field: 'country', type: 'nominal' },
|
|
44
|
+
},
|
|
45
|
+
chrome: {},
|
|
46
|
+
annotations: [],
|
|
47
|
+
responsive: true,
|
|
48
|
+
theme: {},
|
|
49
|
+
darkMode: 'off',
|
|
50
|
+
labels: { density: 'auto', format: '', prefix: '' },
|
|
51
|
+
hiddenSeries: [],
|
|
52
|
+
seriesStyles: {},
|
|
53
|
+
watermark: true,
|
|
54
|
+
display: 'full',
|
|
55
|
+
userExplicit: {
|
|
56
|
+
chrome: false,
|
|
57
|
+
legend: false,
|
|
58
|
+
endpointLabels: false,
|
|
59
|
+
xAxis: false,
|
|
60
|
+
yAxis: false,
|
|
61
|
+
labels: false,
|
|
62
|
+
animation: false,
|
|
63
|
+
watermark: false,
|
|
64
|
+
crosshair: false,
|
|
65
|
+
},
|
|
66
|
+
...overrides,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function makeLineMark(
|
|
71
|
+
seriesKey: string,
|
|
72
|
+
lastY: number,
|
|
73
|
+
value: number,
|
|
74
|
+
stroke = '#3366cc',
|
|
75
|
+
): LineMark {
|
|
76
|
+
const lastX = chartArea.x + chartArea.width;
|
|
77
|
+
return {
|
|
78
|
+
type: 'line',
|
|
79
|
+
points: [
|
|
80
|
+
{ x: chartArea.x, y: lastY + 20 },
|
|
81
|
+
{ x: lastX, y: lastY },
|
|
82
|
+
],
|
|
83
|
+
stroke,
|
|
84
|
+
strokeWidth: 2,
|
|
85
|
+
seriesKey,
|
|
86
|
+
data: [
|
|
87
|
+
{ date: '2020', value: value - 5, country: seriesKey },
|
|
88
|
+
{ date: '2021', value, country: seriesKey },
|
|
89
|
+
],
|
|
90
|
+
dataPoints: [
|
|
91
|
+
{
|
|
92
|
+
x: chartArea.x,
|
|
93
|
+
y: lastY + 20,
|
|
94
|
+
datum: { date: '2020', value: value - 5, country: seriesKey },
|
|
95
|
+
},
|
|
96
|
+
{ x: lastX, y: lastY, datum: { date: '2021', value, country: seriesKey } },
|
|
97
|
+
],
|
|
98
|
+
aria: { label: seriesKey },
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function makeAreaMark(seriesKey: string, lastY: number, value: number, fill = '#3366cc'): AreaMark {
|
|
103
|
+
const lastX = chartArea.x + chartArea.width;
|
|
104
|
+
return {
|
|
105
|
+
type: 'area',
|
|
106
|
+
topPoints: [
|
|
107
|
+
{ x: chartArea.x, y: lastY + 20 },
|
|
108
|
+
{ x: lastX, y: lastY },
|
|
109
|
+
],
|
|
110
|
+
bottomPoints: [
|
|
111
|
+
{ x: chartArea.x, y: chartArea.y + chartArea.height },
|
|
112
|
+
{ x: lastX, y: chartArea.y + chartArea.height },
|
|
113
|
+
],
|
|
114
|
+
path: '',
|
|
115
|
+
topPath: '',
|
|
116
|
+
fill,
|
|
117
|
+
fillOpacity: 0.3,
|
|
118
|
+
stroke: fill,
|
|
119
|
+
strokeWidth: 2,
|
|
120
|
+
seriesKey,
|
|
121
|
+
data: [
|
|
122
|
+
{ date: '2020', value: value - 5, country: seriesKey },
|
|
123
|
+
{ date: '2021', value, country: seriesKey },
|
|
124
|
+
],
|
|
125
|
+
dataPoints: [
|
|
126
|
+
{
|
|
127
|
+
x: chartArea.x,
|
|
128
|
+
y: lastY + 20,
|
|
129
|
+
datum: { date: '2020', value: value - 5, country: seriesKey },
|
|
130
|
+
},
|
|
131
|
+
{ x: lastX, y: lastY, datum: { date: '2021', value, country: seriesKey } },
|
|
132
|
+
],
|
|
133
|
+
aria: { label: seriesKey },
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
// Tests
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
describe('computeEndpointLabels', () => {
|
|
142
|
+
it('produces one entry per series for a multi-series line chart', () => {
|
|
143
|
+
const spec = makeSpec();
|
|
144
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 200, 35, '#cc6633')];
|
|
145
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
146
|
+
|
|
147
|
+
expect(layout.entries).toHaveLength(2);
|
|
148
|
+
const keys = layout.entries.map((e) => e.seriesKey).sort();
|
|
149
|
+
expect(keys).toEqual(['UK', 'US']);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('returns an empty layout when endpointLabels: false', () => {
|
|
153
|
+
const spec = makeSpec({ endpointLabels: false });
|
|
154
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 200, 35)];
|
|
155
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
156
|
+
|
|
157
|
+
expect(layout.entries).toHaveLength(0);
|
|
158
|
+
expect(layout.bounds.width).toBe(0);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('returns an empty layout for a single-series chart', () => {
|
|
162
|
+
const spec = makeSpec();
|
|
163
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40)];
|
|
164
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
165
|
+
|
|
166
|
+
expect(layout.entries).toHaveLength(0);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('returns an empty layout when strategy.labelMode is "none" (compact breakpoint)', () => {
|
|
170
|
+
const spec = makeSpec();
|
|
171
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 200, 35)];
|
|
172
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea, {
|
|
173
|
+
labelMode: 'none',
|
|
174
|
+
legendPosition: 'top',
|
|
175
|
+
annotationPosition: 'tooltip-only',
|
|
176
|
+
axisLabelDensity: 'minimal',
|
|
177
|
+
chromeMode: 'full',
|
|
178
|
+
legendMaxHeight: -1,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
expect(layout.entries).toHaveLength(0);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('wraps long series names to multiple lines', () => {
|
|
185
|
+
const longName = 'A really long multi-word series name that should wrap';
|
|
186
|
+
const spec = makeSpec({
|
|
187
|
+
data: [
|
|
188
|
+
{ date: '2020', value: 10, country: longName },
|
|
189
|
+
{ date: '2021', value: 40, country: longName },
|
|
190
|
+
{ date: '2020', value: 5, country: 'UK' },
|
|
191
|
+
{ date: '2021', value: 35, country: 'UK' },
|
|
192
|
+
],
|
|
193
|
+
endpointLabels: { width: 80 },
|
|
194
|
+
});
|
|
195
|
+
const marks: Mark[] = [makeLineMark(longName, 100, 40), makeLineMark('UK', 200, 35)];
|
|
196
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
197
|
+
|
|
198
|
+
const longEntry = layout.entries.find((e) => e.seriesKey === longName);
|
|
199
|
+
expect(longEntry).toBeDefined();
|
|
200
|
+
expect(longEntry!.labelLines.length).toBeGreaterThan(1);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('displaces overlapping entries via the bidirectional collision sweep', () => {
|
|
204
|
+
// Two series whose last data points are very close together (same y).
|
|
205
|
+
const spec = makeSpec();
|
|
206
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 102, 35, '#cc6633')];
|
|
207
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
208
|
+
|
|
209
|
+
expect(layout.entries).toHaveLength(2);
|
|
210
|
+
const [a, b] = layout.entries;
|
|
211
|
+
// Their labelY values should be separated by at least their height (no overlap).
|
|
212
|
+
const distance = Math.abs(a.labelY - b.labelY);
|
|
213
|
+
// Each entry has at least one line of label height.
|
|
214
|
+
expect(distance).toBeGreaterThanOrEqual(11 * 1.25 - 0.5);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('marks displaced entries with showLeader: true when opted in', () => {
|
|
218
|
+
// Leaders are off by default; opt in via endpointLabels.showLeader.
|
|
219
|
+
const spec = makeSpec({ endpointLabels: { showLeader: true } });
|
|
220
|
+
// Force overlap at the same y to guarantee displacement.
|
|
221
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 100, 35, '#cc6633')];
|
|
222
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
223
|
+
|
|
224
|
+
const anyDisplaced = layout.entries.some((e) => e.showLeader);
|
|
225
|
+
expect(anyDisplaced).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('keeps showLeader off by default even when displaced', () => {
|
|
229
|
+
// Default (no showLeader config): displacement does not produce a leader.
|
|
230
|
+
const spec = makeSpec();
|
|
231
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 100, 35, '#cc6633')];
|
|
232
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
233
|
+
|
|
234
|
+
expect(layout.entries.every((e) => !e.showLeader)).toBe(true);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('does not flag undisplaced entries with showLeader', () => {
|
|
238
|
+
// Two series far apart vertically — no collision, no leader, even when opted in.
|
|
239
|
+
const spec = makeSpec({ endpointLabels: { showLeader: true } });
|
|
240
|
+
const marks: Mark[] = [makeLineMark('US', 50, 40), makeLineMark('UK', 280, 35, '#cc6633')];
|
|
241
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
242
|
+
|
|
243
|
+
expect(layout.entries.every((e) => !e.showLeader)).toBe(true);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('clamps entries inside the chart area at the top edge', () => {
|
|
247
|
+
const spec = makeSpec();
|
|
248
|
+
// Series whose last point is at the very top of the chart area.
|
|
249
|
+
const marks: Mark[] = [
|
|
250
|
+
makeLineMark('US', chartArea.y, 40),
|
|
251
|
+
makeLineMark('UK', chartArea.y + 5, 35, '#cc6633'),
|
|
252
|
+
];
|
|
253
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
254
|
+
|
|
255
|
+
for (const entry of layout.entries) {
|
|
256
|
+
expect(entry.labelY).toBeGreaterThanOrEqual(chartArea.y - 0.0001);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('clamps entries inside the chart area at the bottom edge', () => {
|
|
261
|
+
const spec = makeSpec();
|
|
262
|
+
const bottomY = chartArea.y + chartArea.height;
|
|
263
|
+
const marks: Mark[] = [
|
|
264
|
+
makeLineMark('US', bottomY, 40),
|
|
265
|
+
makeLineMark('UK', bottomY - 5, 35, '#cc6633'),
|
|
266
|
+
];
|
|
267
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
268
|
+
|
|
269
|
+
// Some line height; height = 11 * 1.25 ≈ 13.75
|
|
270
|
+
const labelLineHeight = 11 * 1.25;
|
|
271
|
+
for (const entry of layout.entries) {
|
|
272
|
+
expect(entry.labelY + labelLineHeight).toBeLessThanOrEqual(bottomY + 0.5);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('attaches a marker on the line at the chart right edge', () => {
|
|
277
|
+
const spec = makeSpec();
|
|
278
|
+
const lastX = chartArea.x + chartArea.width;
|
|
279
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 200, 35, '#cc6633')];
|
|
280
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
281
|
+
|
|
282
|
+
for (const entry of layout.entries) {
|
|
283
|
+
expect(entry.marker).toBeDefined();
|
|
284
|
+
expect(entry.marker!.x).toBe(lastX);
|
|
285
|
+
// Marker y is at the actual data point (not displaced labelY).
|
|
286
|
+
expect(entry.marker!.y).toBe(entry.dataY);
|
|
287
|
+
// Open-circle convention: fill = background, stroke = series color.
|
|
288
|
+
expect(entry.marker!.stroke).toBe(entry.color);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('omits the marker when showMarker is explicitly false', () => {
|
|
293
|
+
const spec = makeSpec({ endpointLabels: { showMarker: false } });
|
|
294
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 200, 35, '#cc6633')];
|
|
295
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
296
|
+
|
|
297
|
+
for (const entry of layout.entries) {
|
|
298
|
+
expect(entry.marker).toBeUndefined();
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('formats values via the spec format string', () => {
|
|
303
|
+
const spec = makeSpec({ endpointLabels: { format: '$.2f' } });
|
|
304
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 200, 35, '#cc6633')];
|
|
305
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
306
|
+
|
|
307
|
+
for (const entry of layout.entries) {
|
|
308
|
+
expect(entry.value.startsWith('$')).toBe(true);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('positions the column to the right of the chart area', () => {
|
|
313
|
+
const spec = makeSpec();
|
|
314
|
+
const marks: Mark[] = [makeLineMark('US', 100, 40), makeLineMark('UK', 200, 35, '#cc6633')];
|
|
315
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
316
|
+
|
|
317
|
+
expect(layout.bounds.x).toBeGreaterThanOrEqual(chartArea.x + chartArea.width);
|
|
318
|
+
expect(layout.bounds.width).toBeGreaterThan(0);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('handles area marks (overlap, not stacked)', () => {
|
|
322
|
+
const spec = makeSpec({
|
|
323
|
+
markType: 'area',
|
|
324
|
+
markDef: { type: 'area' },
|
|
325
|
+
});
|
|
326
|
+
const marks: Mark[] = [makeAreaMark('US', 100, 40), makeAreaMark('UK', 200, 35, '#cc6633')];
|
|
327
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
328
|
+
|
|
329
|
+
expect(layout.entries).toHaveLength(2);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('dedupes by seriesKey when both an area and a derived line exist for a series', () => {
|
|
333
|
+
// Defect-1 regression: the area renderer emits BOTH an AreaMark AND a
|
|
334
|
+
// derived LineMark per series (see linesFromAreas in
|
|
335
|
+
// packages/engine/src/charts/line/index.ts). Without dedupe, each series
|
|
336
|
+
// produces two endpoint entries.
|
|
337
|
+
const spec = makeSpec({
|
|
338
|
+
markType: 'area',
|
|
339
|
+
markDef: { type: 'area' },
|
|
340
|
+
});
|
|
341
|
+
const lineColor = '#3366cc';
|
|
342
|
+
const areaColor = '#ddee99'; // fake gradient-derived color, distinct from the line stroke
|
|
343
|
+
const marks: Mark[] = [
|
|
344
|
+
makeAreaMark('US', 100, 40, areaColor),
|
|
345
|
+
makeAreaMark('UK', 200, 35, areaColor),
|
|
346
|
+
makeLineMark('US', 100, 40, lineColor),
|
|
347
|
+
makeLineMark('UK', 200, 35, lineColor),
|
|
348
|
+
];
|
|
349
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
350
|
+
|
|
351
|
+
// Single entry per series.
|
|
352
|
+
expect(layout.entries).toHaveLength(2);
|
|
353
|
+
const keys = layout.entries.map((e) => e.seriesKey).sort();
|
|
354
|
+
expect(keys).toEqual(['UK', 'US']);
|
|
355
|
+
|
|
356
|
+
// Line marks win — entry color should match the line stroke, not the
|
|
357
|
+
// area-derived gradient color.
|
|
358
|
+
for (const entry of layout.entries) {
|
|
359
|
+
expect(entry.color).toBe(lineColor);
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it('bidirectional sweep produces non-overlapping tops when stack fits in area (deterministic fuzz)', () => {
|
|
364
|
+
// Seeded LCG so the fuzz is reproducible without dragging in a dep.
|
|
365
|
+
const rand = (() => {
|
|
366
|
+
let s = 0x12345678 >>> 0;
|
|
367
|
+
return () => {
|
|
368
|
+
s = (Math.imul(s, 1664525) + 1013904223) >>> 0;
|
|
369
|
+
return s / 0x100000000;
|
|
370
|
+
};
|
|
371
|
+
})();
|
|
372
|
+
|
|
373
|
+
const areaTop = 0;
|
|
374
|
+
const areaBottom = 600;
|
|
375
|
+
const areaHeight = areaBottom - areaTop;
|
|
376
|
+
|
|
377
|
+
for (let trial = 0; trial < 200; trial++) {
|
|
378
|
+
const n = 2 + Math.floor(rand() * 9); // 2..10 entries
|
|
379
|
+
const heights: number[] = [];
|
|
380
|
+
let totalHeight = 0;
|
|
381
|
+
for (let i = 0; i < n; i++) {
|
|
382
|
+
const h = 12 + Math.floor(rand() * 30); // 12..41
|
|
383
|
+
heights.push(h);
|
|
384
|
+
totalHeight += h;
|
|
385
|
+
}
|
|
386
|
+
// Skip trials whose stack can't fit — the algorithm explicitly cannot
|
|
387
|
+
// promise non-overlap when the chart is too short for the entries.
|
|
388
|
+
if (totalHeight > areaHeight) continue;
|
|
389
|
+
|
|
390
|
+
const sweepEntries = heights.map((h, idx) => ({
|
|
391
|
+
naturalTop: areaTop + rand() * (areaBottom - h),
|
|
392
|
+
height: h,
|
|
393
|
+
index: idx,
|
|
394
|
+
}));
|
|
395
|
+
const tops = bidirectionalSweep(sweepEntries, areaTop, areaBottom);
|
|
396
|
+
|
|
397
|
+
// Resort by final top to validate the sorted-stack invariant the
|
|
398
|
+
// algorithm guarantees: every entry sits inside [areaTop, areaBottom-h]
|
|
399
|
+
// and adjacent entries never overlap.
|
|
400
|
+
const sortedFinals = sweepEntries
|
|
401
|
+
.map((e) => ({ top: tops[e.index], height: e.height }))
|
|
402
|
+
.sort((a, b) => a.top - b.top);
|
|
403
|
+
|
|
404
|
+
for (let i = 0; i < sortedFinals.length; i++) {
|
|
405
|
+
const { top, height } = sortedFinals[i];
|
|
406
|
+
expect(top).toBeGreaterThanOrEqual(areaTop - 1e-6);
|
|
407
|
+
expect(top + height).toBeLessThanOrEqual(areaBottom + 1e-6);
|
|
408
|
+
if (i + 1 < sortedFinals.length) {
|
|
409
|
+
const next = sortedFinals[i + 1];
|
|
410
|
+
expect(top + height).toBeLessThanOrEqual(next.top + 1e-6);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('dedupe prefers line mark even when area appears later in the marks array', () => {
|
|
417
|
+
// Defect-1 regression: area marks listed AFTER line marks should not
|
|
418
|
+
// overwrite the line's canonical stroke color in the endpoint entry.
|
|
419
|
+
const spec = makeSpec({
|
|
420
|
+
markType: 'area',
|
|
421
|
+
markDef: { type: 'area' },
|
|
422
|
+
});
|
|
423
|
+
const lineColor = '#3366cc';
|
|
424
|
+
const areaColor = '#ddee99';
|
|
425
|
+
const marks: Mark[] = [
|
|
426
|
+
makeLineMark('US', 100, 40, lineColor),
|
|
427
|
+
makeLineMark('UK', 200, 35, lineColor),
|
|
428
|
+
makeAreaMark('US', 100, 40, areaColor),
|
|
429
|
+
makeAreaMark('UK', 200, 35, areaColor),
|
|
430
|
+
];
|
|
431
|
+
const layout = computeEndpointLabels(spec, marks, theme, chartArea);
|
|
432
|
+
|
|
433
|
+
expect(layout.entries).toHaveLength(2);
|
|
434
|
+
for (const entry of layout.entries) {
|
|
435
|
+
expect(entry.color).toBe(lineColor);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
});
|