@opendata-ai/openchart-engine 7.2.4 → 7.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +84 -36
- package/dist/index.js +4010 -2871
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +139 -63
- package/src/__tests__/axes.test.ts +67 -1
- package/src/__tests__/compile-chart.test.ts +6 -0
- package/src/__tests__/compile-layer.test.ts +49 -0
- package/src/__tests__/dimensions.test.ts +7 -2
- package/src/__tests__/helpers/invariants.ts +267 -0
- package/src/__tests__/known-bugs.test.ts +113 -0
- package/src/__tests__/layout-fuzz.test.ts +342 -0
- package/src/__tests__/layout-invariants.test.ts +533 -0
- package/src/__tests__/legend.test.ts +37 -0
- package/src/annotations/__tests__/compute.test.ts +8 -15
- package/src/annotations/__tests__/placement-fuzz.test.ts +256 -0
- package/src/annotations/__tests__/placement-invariants.test.ts +345 -0
- package/src/annotations/__tests__/placement.test.ts +316 -0
- package/src/annotations/collisions.ts +19 -7
- package/src/annotations/compute.ts +257 -16
- package/src/annotations/geometry.ts +73 -15
- package/src/annotations/placement.ts +588 -0
- package/src/annotations/resolve-text.ts +58 -10
- package/src/charts/__tests__/post-process.test.ts +3 -3
- package/src/charts/post-process.ts +21 -7
- package/src/compile/layer.ts +31 -9
- package/src/compile.ts +85 -148
- package/src/index.ts +9 -0
- package/src/layout/axes.ts +93 -1
- package/src/layout/dimensions.ts +80 -98
- package/src/layout/plan.ts +524 -0
- package/src/layout/shared.ts +44 -0
- package/src/legend/compute.ts +315 -78
- package/src/legend/wrap.ts +11 -7
- package/src/sankey/__tests__/node-label-wrap.test.ts +2 -2
- package/src/tables/__tests__/heatmap.test.ts +24 -20
- package/src/tables/heatmap.ts +25 -9
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import type { Rect } from '@opendata-ai/openchart-core';
|
|
2
|
+
import { overlapArea } from '@opendata-ai/openchart-core';
|
|
3
|
+
import { describe, it } from 'vitest';
|
|
4
|
+
import type { PlacementObstacle, PlacementResult } from '../placement';
|
|
5
|
+
import { findBestPlacement, samplePolylineObstacles } from '../placement';
|
|
6
|
+
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Seeded PRNG (mulberry32) - deterministic, no Date.now or Math.random
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
function mulberry32(seed: number) {
|
|
12
|
+
return () => {
|
|
13
|
+
let t = (seed += 0x6d2b79f5);
|
|
14
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
15
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
16
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Random helpers
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
function randRange(rng: () => number, min: number, max: number): number {
|
|
25
|
+
return min + rng() * (max - min);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function randInt(rng: () => number, min: number, max: number): number {
|
|
29
|
+
return Math.floor(randRange(rng, min, max + 1));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function randPick<T>(rng: () => number, arr: T[]): T {
|
|
33
|
+
return arr[Math.floor(rng() * arr.length)];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Case generator
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
type ChartType = 'line' | 'column' | 'scatter';
|
|
41
|
+
|
|
42
|
+
interface FuzzCase {
|
|
43
|
+
caseIndex: number;
|
|
44
|
+
containerWidth: number;
|
|
45
|
+
containerHeight: number;
|
|
46
|
+
chartArea: Rect;
|
|
47
|
+
svgRect: Rect;
|
|
48
|
+
chartType: ChartType;
|
|
49
|
+
dataPoints: Array<{ x: number; y: number }>;
|
|
50
|
+
annotationIndices: number[];
|
|
51
|
+
obstacles: PlacementObstacle[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function generateCase(rng: () => number, caseIndex: number): FuzzCase {
|
|
55
|
+
const containerWidth = randInt(rng, 320, 1200);
|
|
56
|
+
const containerHeight = randInt(rng, 240, 600);
|
|
57
|
+
const inset = 50;
|
|
58
|
+
|
|
59
|
+
const svgRect: Rect = { x: 0, y: 0, width: containerWidth, height: containerHeight };
|
|
60
|
+
const chartArea: Rect = {
|
|
61
|
+
x: inset,
|
|
62
|
+
y: inset,
|
|
63
|
+
width: containerWidth - inset * 2,
|
|
64
|
+
height: containerHeight - inset * 2,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const chartType = randPick<ChartType>(rng, ['line', 'column', 'scatter']);
|
|
68
|
+
const numPoints = randInt(rng, 5, 60);
|
|
69
|
+
|
|
70
|
+
// Generate random data points within the chart area
|
|
71
|
+
const dataPoints: Array<{ x: number; y: number }> = [];
|
|
72
|
+
for (let i = 0; i < numPoints; i++) {
|
|
73
|
+
dataPoints.push({
|
|
74
|
+
x: randRange(rng, chartArea.x, chartArea.x + chartArea.width),
|
|
75
|
+
y: randRange(rng, chartArea.y, chartArea.y + chartArea.height),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Sort by x for line charts (polyline needs ordered points)
|
|
80
|
+
if (chartType === 'line') {
|
|
81
|
+
dataPoints.sort((a, b) => a.x - b.x);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Generate obstacles based on chart type
|
|
85
|
+
let obstacles: PlacementObstacle[] = [];
|
|
86
|
+
|
|
87
|
+
if (chartType === 'line') {
|
|
88
|
+
obstacles = samplePolylineObstacles(dataPoints, 'line', 24, 3);
|
|
89
|
+
} else if (chartType === 'column') {
|
|
90
|
+
// Rect obstacles for column bars
|
|
91
|
+
const barWidth = Math.max(4, (chartArea.width / numPoints) * 0.6);
|
|
92
|
+
for (const pt of dataPoints) {
|
|
93
|
+
const barHeight = chartArea.y + chartArea.height - pt.y;
|
|
94
|
+
obstacles.push({
|
|
95
|
+
x: pt.x - barWidth / 2,
|
|
96
|
+
y: pt.y,
|
|
97
|
+
width: barWidth,
|
|
98
|
+
height: barHeight,
|
|
99
|
+
kind: 'mark',
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
// Scatter: point obstacles as small rects
|
|
104
|
+
const pointRadius = 4;
|
|
105
|
+
for (const pt of dataPoints) {
|
|
106
|
+
obstacles.push({
|
|
107
|
+
x: pt.x - pointRadius,
|
|
108
|
+
y: pt.y - pointRadius,
|
|
109
|
+
width: pointRadius * 2,
|
|
110
|
+
height: pointRadius * 2,
|
|
111
|
+
kind: 'mark',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Pick 1-5 annotation anchor indices from the data points
|
|
117
|
+
const numAnnotations = randInt(rng, 1, 5);
|
|
118
|
+
const annotationIndices: number[] = [];
|
|
119
|
+
for (let i = 0; i < numAnnotations; i++) {
|
|
120
|
+
annotationIndices.push(randInt(rng, 0, numPoints - 1));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
caseIndex,
|
|
125
|
+
containerWidth,
|
|
126
|
+
containerHeight,
|
|
127
|
+
chartArea,
|
|
128
|
+
svgRect,
|
|
129
|
+
chartType,
|
|
130
|
+
dataPoints,
|
|
131
|
+
annotationIndices,
|
|
132
|
+
obstacles,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
// Test
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
const ANNOTATION_STYLE = { fontSize: 12, fontWeight: 600, lineHeight: 1.2 };
|
|
141
|
+
|
|
142
|
+
describe('placement engine fuzz', () => {
|
|
143
|
+
it('passes 200 fuzz cases', () => {
|
|
144
|
+
const rng = mulberry32(42);
|
|
145
|
+
|
|
146
|
+
for (let i = 0; i < 200; i++) {
|
|
147
|
+
const tc = generateCase(rng, i);
|
|
148
|
+
const results: PlacementResult[] = [];
|
|
149
|
+
// Copy obstacles so we can append placed annotation bounds
|
|
150
|
+
const liveObstacles: PlacementObstacle[] = [...tc.obstacles];
|
|
151
|
+
|
|
152
|
+
for (const ptIdx of tc.annotationIndices) {
|
|
153
|
+
const anchor = tc.dataPoints[ptIdx];
|
|
154
|
+
const text = `Annotation ${ptIdx}`;
|
|
155
|
+
|
|
156
|
+
const result = findBestPlacement(
|
|
157
|
+
anchor.x,
|
|
158
|
+
anchor.y,
|
|
159
|
+
text,
|
|
160
|
+
ANNOTATION_STYLE,
|
|
161
|
+
undefined, // no subtitle
|
|
162
|
+
undefined,
|
|
163
|
+
liveObstacles,
|
|
164
|
+
tc.chartArea,
|
|
165
|
+
tc.svgRect,
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
results.push(result);
|
|
169
|
+
|
|
170
|
+
// Add this placed annotation as an obstacle for subsequent annotations
|
|
171
|
+
liveObstacles.push({
|
|
172
|
+
...result.bounds,
|
|
173
|
+
kind: 'annotation',
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const caseInfo = () =>
|
|
178
|
+
JSON.stringify(
|
|
179
|
+
{
|
|
180
|
+
caseIndex: tc.caseIndex,
|
|
181
|
+
containerWidth: tc.containerWidth,
|
|
182
|
+
containerHeight: tc.containerHeight,
|
|
183
|
+
chartType: tc.chartType,
|
|
184
|
+
numDataPoints: tc.dataPoints.length,
|
|
185
|
+
annotationIndices: tc.annotationIndices,
|
|
186
|
+
chartArea: tc.chartArea,
|
|
187
|
+
svgRect: tc.svgRect,
|
|
188
|
+
},
|
|
189
|
+
null,
|
|
190
|
+
2,
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
// Invariant 1: Pairwise non-overlap when all placements found clean spots
|
|
194
|
+
const allClean = results.every((r) => r.score < 0.1);
|
|
195
|
+
if (allClean && results.length > 1) {
|
|
196
|
+
for (let a = 0; a < results.length; a++) {
|
|
197
|
+
for (let b = a + 1; b < results.length; b++) {
|
|
198
|
+
const overlap = overlapArea(results[a].bounds, results[b].bounds);
|
|
199
|
+
if (overlap >= 0.5) {
|
|
200
|
+
throw new Error(
|
|
201
|
+
`Invariant 1 failed: pairwise overlap ${overlap.toFixed(2)}px^2 between annotation ${a} and ${b} (both scored < 0.1).\n${caseInfo()}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Invariant 2: Bounds within SVG (2px margin tolerance)
|
|
209
|
+
const margin = 2;
|
|
210
|
+
for (let r = 0; r < results.length; r++) {
|
|
211
|
+
const b = results[r].bounds;
|
|
212
|
+
const inBounds =
|
|
213
|
+
b.x >= tc.svgRect.x - margin &&
|
|
214
|
+
b.y >= tc.svgRect.y - margin &&
|
|
215
|
+
b.x + b.width <= tc.svgRect.x + tc.svgRect.width + margin &&
|
|
216
|
+
b.y + b.height <= tc.svgRect.y + tc.svgRect.height + margin;
|
|
217
|
+
|
|
218
|
+
if (!inBounds) {
|
|
219
|
+
// Only fail if the placement engine scored it well (low score means
|
|
220
|
+
// it thought this was a good placement). High-score placements may
|
|
221
|
+
// be pushed out of bounds when the chart is too crowded.
|
|
222
|
+
if (results[r].score < 1) {
|
|
223
|
+
throw new Error(
|
|
224
|
+
`Invariant 2 failed: annotation ${r} bounds outside SVG rect (score=${results[r].score.toFixed(3)}).\n` +
|
|
225
|
+
`bounds: ${JSON.stringify(b)}\nsvgRect: ${JSON.stringify(tc.svgRect)}\n${caseInfo()}`,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Invariant 3: Line avoidance - when chart type is 'line', verify
|
|
232
|
+
// low-score annotations don't overlap line obstacles near their anchor
|
|
233
|
+
if (tc.chartType === 'line') {
|
|
234
|
+
for (let r = 0; r < results.length; r++) {
|
|
235
|
+
if (results[r].score >= 0.1) continue; // only check clean placements
|
|
236
|
+
|
|
237
|
+
const anchor = tc.dataPoints[tc.annotationIndices[r]];
|
|
238
|
+
// Check line obstacles near the anchor (within 30px horizontally)
|
|
239
|
+
const nearbyLineObs = tc.obstacles.filter(
|
|
240
|
+
(obs) =>
|
|
241
|
+
obs.kind === 'line' && obs.x < anchor.x + 30 && obs.x + obs.width > anchor.x - 30,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
for (const obs of nearbyLineObs) {
|
|
245
|
+
const overlap = overlapArea(results[r].bounds, obs);
|
|
246
|
+
if (overlap > 5) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
`Invariant 3 failed: annotation ${r} overlaps nearby line obstacle by ${overlap.toFixed(2)}px^2 (score=${results[r].score.toFixed(3)}).\n${caseInfo()}`,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
});
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { overlapArea } from '@opendata-ai/openchart-core';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { findBestPlacement, type PlacementObstacle, samplePolylineObstacles } from '../placement';
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Shared fixtures
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
const CHART_AREA = { x: 50, y: 20, width: 500, height: 300 };
|
|
10
|
+
const SVG_RECT = { x: 0, y: 0, width: 600, height: 360 };
|
|
11
|
+
const DEFAULT_STYLE = { fontSize: 12, fontWeight: 400, lineHeight: 1.3 };
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// 1. Pairwise non-overlap
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
describe('pairwise non-overlap', () => {
|
|
18
|
+
it('two annotations at the same point do not overlap', () => {
|
|
19
|
+
const anchorX = 300;
|
|
20
|
+
const anchorY = 170;
|
|
21
|
+
|
|
22
|
+
const r1 = findBestPlacement(
|
|
23
|
+
anchorX,
|
|
24
|
+
anchorY,
|
|
25
|
+
'Label A',
|
|
26
|
+
DEFAULT_STYLE,
|
|
27
|
+
undefined,
|
|
28
|
+
undefined,
|
|
29
|
+
[],
|
|
30
|
+
CHART_AREA,
|
|
31
|
+
SVG_RECT,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
// Feed the first result's bounds as an obstacle for the second
|
|
35
|
+
const obstacle: PlacementObstacle = { ...r1.bounds, kind: 'annotation' };
|
|
36
|
+
const r2 = findBestPlacement(
|
|
37
|
+
anchorX,
|
|
38
|
+
anchorY,
|
|
39
|
+
'Label B',
|
|
40
|
+
DEFAULT_STYLE,
|
|
41
|
+
undefined,
|
|
42
|
+
undefined,
|
|
43
|
+
[obstacle],
|
|
44
|
+
CHART_AREA,
|
|
45
|
+
SVG_RECT,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(overlapArea(r1.bounds, r2.bounds)).toBe(0);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('three annotations at the same point do not overlap each other', () => {
|
|
52
|
+
const anchorX = 250;
|
|
53
|
+
const anchorY = 160;
|
|
54
|
+
const obstacles: PlacementObstacle[] = [];
|
|
55
|
+
|
|
56
|
+
const results = [];
|
|
57
|
+
for (const label of ['Alpha', 'Beta', 'Gamma']) {
|
|
58
|
+
const r = findBestPlacement(
|
|
59
|
+
anchorX,
|
|
60
|
+
anchorY,
|
|
61
|
+
label,
|
|
62
|
+
DEFAULT_STYLE,
|
|
63
|
+
undefined,
|
|
64
|
+
undefined,
|
|
65
|
+
obstacles,
|
|
66
|
+
CHART_AREA,
|
|
67
|
+
SVG_RECT,
|
|
68
|
+
);
|
|
69
|
+
results.push(r);
|
|
70
|
+
obstacles.push({ ...r.bounds, kind: 'annotation' });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Check all pairs
|
|
74
|
+
for (let i = 0; i < results.length; i++) {
|
|
75
|
+
for (let j = i + 1; j < results.length; j++) {
|
|
76
|
+
expect(
|
|
77
|
+
overlapArea(results[i].bounds, results[j].bounds),
|
|
78
|
+
`annotations ${i} and ${j} should not overlap`,
|
|
79
|
+
).toBe(0);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('two annotations at nearby points (within 20px) do not overlap', () => {
|
|
85
|
+
const r1 = findBestPlacement(
|
|
86
|
+
200,
|
|
87
|
+
150,
|
|
88
|
+
'Nearby A',
|
|
89
|
+
DEFAULT_STYLE,
|
|
90
|
+
undefined,
|
|
91
|
+
undefined,
|
|
92
|
+
[],
|
|
93
|
+
CHART_AREA,
|
|
94
|
+
SVG_RECT,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const obstacle: PlacementObstacle = { ...r1.bounds, kind: 'annotation' };
|
|
98
|
+
const r2 = findBestPlacement(
|
|
99
|
+
215,
|
|
100
|
+
155,
|
|
101
|
+
'Nearby B',
|
|
102
|
+
DEFAULT_STYLE,
|
|
103
|
+
undefined,
|
|
104
|
+
undefined,
|
|
105
|
+
[obstacle],
|
|
106
|
+
CHART_AREA,
|
|
107
|
+
SVG_RECT,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(overlapArea(r1.bounds, r2.bounds)).toBe(0);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// 2. Bounds within SVG
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
describe('bounds within SVG', () => {
|
|
119
|
+
const MARGIN = 2;
|
|
120
|
+
|
|
121
|
+
function expectWithinSvg(label: string, anchorX: number, anchorY: number) {
|
|
122
|
+
const r = findBestPlacement(
|
|
123
|
+
anchorX,
|
|
124
|
+
anchorY,
|
|
125
|
+
label,
|
|
126
|
+
DEFAULT_STYLE,
|
|
127
|
+
undefined,
|
|
128
|
+
undefined,
|
|
129
|
+
[],
|
|
130
|
+
CHART_AREA,
|
|
131
|
+
SVG_RECT,
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
expect(r.bounds.x).toBeGreaterThanOrEqual(SVG_RECT.x - MARGIN);
|
|
135
|
+
expect(r.bounds.y).toBeGreaterThanOrEqual(SVG_RECT.y - MARGIN);
|
|
136
|
+
expect(r.bounds.x + r.bounds.width).toBeLessThanOrEqual(SVG_RECT.x + SVG_RECT.width + MARGIN);
|
|
137
|
+
expect(r.bounds.y + r.bounds.height).toBeLessThanOrEqual(SVG_RECT.y + SVG_RECT.height + MARGIN);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
it('annotation near top-left corner stays within SVG', () => {
|
|
141
|
+
expectWithinSvg('Top Left', CHART_AREA.x + 10, CHART_AREA.y + 10);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('annotation near bottom-right corner stays within SVG', () => {
|
|
145
|
+
expectWithinSvg(
|
|
146
|
+
'Bottom Right',
|
|
147
|
+
CHART_AREA.x + CHART_AREA.width - 10,
|
|
148
|
+
CHART_AREA.y + CHART_AREA.height - 10,
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('annotation near right edge stays within SVG', () => {
|
|
153
|
+
expectWithinSvg(
|
|
154
|
+
'Right Edge',
|
|
155
|
+
CHART_AREA.x + CHART_AREA.width - 5,
|
|
156
|
+
CHART_AREA.y + CHART_AREA.height / 2,
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('annotation at center is well within SVG', () => {
|
|
161
|
+
const cx = CHART_AREA.x + CHART_AREA.width / 2;
|
|
162
|
+
const cy = CHART_AREA.y + CHART_AREA.height / 2;
|
|
163
|
+
const r = findBestPlacement(
|
|
164
|
+
cx,
|
|
165
|
+
cy,
|
|
166
|
+
'Center',
|
|
167
|
+
DEFAULT_STYLE,
|
|
168
|
+
undefined,
|
|
169
|
+
undefined,
|
|
170
|
+
[],
|
|
171
|
+
CHART_AREA,
|
|
172
|
+
SVG_RECT,
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// Center placement should be comfortably inside -- use a stricter margin
|
|
176
|
+
const STRICT = 10;
|
|
177
|
+
expect(r.bounds.x).toBeGreaterThanOrEqual(SVG_RECT.x + STRICT);
|
|
178
|
+
expect(r.bounds.y).toBeGreaterThanOrEqual(SVG_RECT.y + STRICT);
|
|
179
|
+
expect(r.bounds.x + r.bounds.width).toBeLessThanOrEqual(SVG_RECT.x + SVG_RECT.width - STRICT);
|
|
180
|
+
expect(r.bounds.y + r.bounds.height).toBeLessThanOrEqual(SVG_RECT.y + SVG_RECT.height - STRICT);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// ---------------------------------------------------------------------------
|
|
185
|
+
// 3. Line avoidance
|
|
186
|
+
// ---------------------------------------------------------------------------
|
|
187
|
+
|
|
188
|
+
describe('line avoidance', () => {
|
|
189
|
+
it('annotation placed on a line avoids nearby line obstacles', () => {
|
|
190
|
+
// Create a simple ascending line spanning the chart
|
|
191
|
+
const points = [];
|
|
192
|
+
for (let i = 0; i <= 10; i++) {
|
|
193
|
+
points.push({
|
|
194
|
+
x: CHART_AREA.x + (i / 10) * CHART_AREA.width,
|
|
195
|
+
y: CHART_AREA.y + CHART_AREA.height - (i / 10) * CHART_AREA.height,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const bucketWidth = 24;
|
|
200
|
+
const lineObstacles = samplePolylineObstacles(points, 'line', bucketWidth);
|
|
201
|
+
|
|
202
|
+
// Pick an anchor that sits on the line (midpoint)
|
|
203
|
+
const anchorX = CHART_AREA.x + CHART_AREA.width / 2;
|
|
204
|
+
const anchorY = CHART_AREA.y + CHART_AREA.height / 2;
|
|
205
|
+
|
|
206
|
+
const result = findBestPlacement(
|
|
207
|
+
anchorX,
|
|
208
|
+
anchorY,
|
|
209
|
+
'On the line',
|
|
210
|
+
DEFAULT_STYLE,
|
|
211
|
+
undefined,
|
|
212
|
+
undefined,
|
|
213
|
+
lineObstacles,
|
|
214
|
+
CHART_AREA,
|
|
215
|
+
SVG_RECT,
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
// Check that the result doesn't overlap line obstacles within +/-2 buckets of anchor
|
|
219
|
+
const nearbyObstacles = lineObstacles.filter((obs) => {
|
|
220
|
+
const obsMidX = obs.x + obs.width / 2;
|
|
221
|
+
return Math.abs(obsMidX - anchorX) <= bucketWidth * 2;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
for (const obs of nearbyObstacles) {
|
|
225
|
+
expect(
|
|
226
|
+
overlapArea(result.bounds, obs),
|
|
227
|
+
`annotation should not overlap nearby line obstacle at x=${obs.x}`,
|
|
228
|
+
).toBe(0);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('annotation avoids a steep line segment', () => {
|
|
233
|
+
// Steep V-shape line where the anchor is at the valley
|
|
234
|
+
const points = [
|
|
235
|
+
{ x: CHART_AREA.x + 100, y: CHART_AREA.y + 50 },
|
|
236
|
+
{ x: CHART_AREA.x + 200, y: CHART_AREA.y + 250 },
|
|
237
|
+
{ x: CHART_AREA.x + 300, y: CHART_AREA.y + 50 },
|
|
238
|
+
];
|
|
239
|
+
|
|
240
|
+
const bucketWidth = 24;
|
|
241
|
+
const lineObstacles = samplePolylineObstacles(points, 'line', bucketWidth);
|
|
242
|
+
|
|
243
|
+
// Anchor at the valley
|
|
244
|
+
const anchorX = CHART_AREA.x + 200;
|
|
245
|
+
const anchorY = CHART_AREA.y + 250;
|
|
246
|
+
|
|
247
|
+
const result = findBestPlacement(
|
|
248
|
+
anchorX,
|
|
249
|
+
anchorY,
|
|
250
|
+
'Valley',
|
|
251
|
+
DEFAULT_STYLE,
|
|
252
|
+
undefined,
|
|
253
|
+
undefined,
|
|
254
|
+
lineObstacles,
|
|
255
|
+
CHART_AREA,
|
|
256
|
+
SVG_RECT,
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
const nearbyObstacles = lineObstacles.filter((obs) => {
|
|
260
|
+
const obsMidX = obs.x + obs.width / 2;
|
|
261
|
+
return Math.abs(obsMidX - anchorX) <= bucketWidth * 2;
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
for (const obs of nearbyObstacles) {
|
|
265
|
+
expect(
|
|
266
|
+
overlapArea(result.bounds, obs),
|
|
267
|
+
`annotation should not overlap nearby line obstacle at x=${obs.x}`,
|
|
268
|
+
).toBe(0);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// ---------------------------------------------------------------------------
|
|
274
|
+
// 4. Determinism
|
|
275
|
+
// ---------------------------------------------------------------------------
|
|
276
|
+
|
|
277
|
+
describe('determinism', () => {
|
|
278
|
+
it('identical inputs produce identical outputs', () => {
|
|
279
|
+
const anchorX = 300;
|
|
280
|
+
const anchorY = 170;
|
|
281
|
+
const text = 'Deterministic';
|
|
282
|
+
const obstacles: PlacementObstacle[] = [
|
|
283
|
+
{ x: 280, y: 140, width: 40, height: 20, kind: 'mark' },
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
const r1 = findBestPlacement(
|
|
287
|
+
anchorX,
|
|
288
|
+
anchorY,
|
|
289
|
+
text,
|
|
290
|
+
DEFAULT_STYLE,
|
|
291
|
+
undefined,
|
|
292
|
+
undefined,
|
|
293
|
+
obstacles,
|
|
294
|
+
CHART_AREA,
|
|
295
|
+
SVG_RECT,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
const r2 = findBestPlacement(
|
|
299
|
+
anchorX,
|
|
300
|
+
anchorY,
|
|
301
|
+
text,
|
|
302
|
+
DEFAULT_STYLE,
|
|
303
|
+
undefined,
|
|
304
|
+
undefined,
|
|
305
|
+
obstacles,
|
|
306
|
+
CHART_AREA,
|
|
307
|
+
SVG_RECT,
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
expect(r1.labelX).toBe(r2.labelX);
|
|
311
|
+
expect(r1.labelY).toBe(r2.labelY);
|
|
312
|
+
expect(r1.textAnchor).toBe(r2.textAnchor);
|
|
313
|
+
expect(r1.bounds).toEqual(r2.bounds);
|
|
314
|
+
expect(r1.score).toBe(r2.score);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it('deterministic with multiple obstacles', () => {
|
|
318
|
+
const obstacles: PlacementObstacle[] = [
|
|
319
|
+
{ x: 100, y: 100, width: 60, height: 30, kind: 'mark' },
|
|
320
|
+
{ x: 200, y: 80, width: 50, height: 40, kind: 'line' },
|
|
321
|
+
{ x: 150, y: 200, width: 80, height: 20, kind: 'legend' },
|
|
322
|
+
];
|
|
323
|
+
|
|
324
|
+
const args = [
|
|
325
|
+
250,
|
|
326
|
+
150,
|
|
327
|
+
'Stable',
|
|
328
|
+
DEFAULT_STYLE,
|
|
329
|
+
undefined,
|
|
330
|
+
undefined,
|
|
331
|
+
obstacles,
|
|
332
|
+
CHART_AREA,
|
|
333
|
+
SVG_RECT,
|
|
334
|
+
] as const;
|
|
335
|
+
|
|
336
|
+
const r1 = findBestPlacement(...args);
|
|
337
|
+
const r2 = findBestPlacement(...args);
|
|
338
|
+
|
|
339
|
+
expect(r1.labelX).toBe(r2.labelX);
|
|
340
|
+
expect(r1.labelY).toBe(r2.labelY);
|
|
341
|
+
expect(r1.textAnchor).toBe(r2.textAnchor);
|
|
342
|
+
expect(r1.bounds).toEqual(r2.bounds);
|
|
343
|
+
expect(r1.score).toBe(r2.score);
|
|
344
|
+
});
|
|
345
|
+
});
|