@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,588 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Annotation placement engine: typed obstacles, candidate generation,
|
|
3
|
+
* scored search, and two-pass deterministic placement.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { overlapArea, type Rect } from '@opendata-ai/openchart-core';
|
|
7
|
+
import {
|
|
8
|
+
type AnnotationMeasureTextFn,
|
|
9
|
+
computeTextBlockBounds,
|
|
10
|
+
heuristicMeasure,
|
|
11
|
+
unionRects,
|
|
12
|
+
} from './geometry';
|
|
13
|
+
|
|
14
|
+
export type ObstacleKind =
|
|
15
|
+
| 'mark'
|
|
16
|
+
| 'line'
|
|
17
|
+
| 'area-fill'
|
|
18
|
+
| 'legend'
|
|
19
|
+
| 'data-label'
|
|
20
|
+
| 'endpoint-label'
|
|
21
|
+
| 'watermark'
|
|
22
|
+
| 'axis-band'
|
|
23
|
+
| 'annotation';
|
|
24
|
+
|
|
25
|
+
export interface PlacementObstacle extends Rect {
|
|
26
|
+
kind: ObstacleKind;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Sample a polyline into bucket-based obstacle rects. Each bucket spans a
|
|
31
|
+
* fixed x-width and covers the min/max y of the line within that bucket,
|
|
32
|
+
* padded vertically. Interpolates segment y-values at bucket edges so steep
|
|
33
|
+
* segments produce gap-free coverage.
|
|
34
|
+
*/
|
|
35
|
+
function lerpY(p0: { x: number; y: number }, p1: { x: number; y: number }, x: number): number {
|
|
36
|
+
const dx = p1.x - p0.x;
|
|
37
|
+
if (Math.abs(dx) < 1e-9) return (p0.y + p1.y) / 2;
|
|
38
|
+
const t = (x - p0.x) / dx;
|
|
39
|
+
return p0.y + t * (p1.y - p0.y);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fillBucketsFromSegments(
|
|
43
|
+
points: Array<{ x: number; y: number }>,
|
|
44
|
+
buckets: Array<{ minY: number; maxY: number }>,
|
|
45
|
+
xMin: number,
|
|
46
|
+
bucketWidth: number,
|
|
47
|
+
): void {
|
|
48
|
+
const bucketCount = buckets.length;
|
|
49
|
+
|
|
50
|
+
function updateBucket(b: number, y: number): void {
|
|
51
|
+
if (b < 0 || b >= bucketCount) return;
|
|
52
|
+
if (y < buckets[b].minY) buckets[b].minY = y;
|
|
53
|
+
if (y > buckets[b].maxY) buckets[b].maxY = y;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
57
|
+
const p0 = points[i];
|
|
58
|
+
const p1 = points[i + 1];
|
|
59
|
+
const segXMin = Math.min(p0.x, p1.x);
|
|
60
|
+
const segXMax = Math.max(p0.x, p1.x);
|
|
61
|
+
const bStart = Math.max(0, Math.floor((segXMin - xMin) / bucketWidth));
|
|
62
|
+
const bEnd = Math.min(bucketCount - 1, Math.floor((segXMax - xMin) / bucketWidth));
|
|
63
|
+
for (let b = bStart; b <= bEnd; b++) {
|
|
64
|
+
const bx0 = xMin + b * bucketWidth;
|
|
65
|
+
const bx1 = xMin + (b + 1) * bucketWidth;
|
|
66
|
+
updateBucket(b, lerpY(p0, p1, Math.max(bx0, segXMin)));
|
|
67
|
+
updateBucket(b, lerpY(p0, p1, Math.min(bx1, segXMax)));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (const p of points) {
|
|
72
|
+
const b = Math.min(Math.floor((p.x - xMin) / bucketWidth), bucketCount - 1);
|
|
73
|
+
updateBucket(b, p.y);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function samplePolylineObstacles(
|
|
78
|
+
points: Array<{ x: number; y: number }>,
|
|
79
|
+
kind: 'line' | 'area-fill' = 'line',
|
|
80
|
+
bucketWidth = 24,
|
|
81
|
+
pad = 3,
|
|
82
|
+
): PlacementObstacle[] {
|
|
83
|
+
if (points.length === 0) return [];
|
|
84
|
+
|
|
85
|
+
if (points.length === 1) {
|
|
86
|
+
const p = points[0];
|
|
87
|
+
return [
|
|
88
|
+
{
|
|
89
|
+
x: p.x - pad,
|
|
90
|
+
y: p.y - pad,
|
|
91
|
+
width: pad * 2,
|
|
92
|
+
height: pad * 2,
|
|
93
|
+
kind,
|
|
94
|
+
},
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let xMin = Infinity;
|
|
99
|
+
let xMax = -Infinity;
|
|
100
|
+
for (const p of points) {
|
|
101
|
+
if (p.x < xMin) xMin = p.x;
|
|
102
|
+
if (p.x > xMax) xMax = p.x;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const span = xMax - xMin;
|
|
106
|
+
if (span <= 0) {
|
|
107
|
+
// All points at same x: single vertical rect
|
|
108
|
+
let yMin = Infinity;
|
|
109
|
+
let yMax = -Infinity;
|
|
110
|
+
for (const p of points) {
|
|
111
|
+
if (p.y < yMin) yMin = p.y;
|
|
112
|
+
if (p.y > yMax) yMax = p.y;
|
|
113
|
+
}
|
|
114
|
+
return [
|
|
115
|
+
{
|
|
116
|
+
x: xMin - pad,
|
|
117
|
+
y: yMin - pad,
|
|
118
|
+
width: pad * 2,
|
|
119
|
+
height: yMax - yMin + pad * 2,
|
|
120
|
+
kind,
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const bucketCount = Math.ceil(span / bucketWidth);
|
|
126
|
+
const buckets: Array<{ minY: number; maxY: number }> = [];
|
|
127
|
+
for (let i = 0; i < bucketCount; i++) {
|
|
128
|
+
buckets.push({ minY: Infinity, maxY: -Infinity });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
fillBucketsFromSegments(points, buckets, xMin, bucketWidth);
|
|
132
|
+
|
|
133
|
+
const obstacles: PlacementObstacle[] = [];
|
|
134
|
+
for (let b = 0; b < bucketCount; b++) {
|
|
135
|
+
const bucket = buckets[b];
|
|
136
|
+
if (bucket.minY === Infinity) continue;
|
|
137
|
+
obstacles.push({
|
|
138
|
+
x: xMin + b * bucketWidth,
|
|
139
|
+
y: bucket.minY - pad,
|
|
140
|
+
width: bucketWidth,
|
|
141
|
+
height: bucket.maxY - bucket.minY + pad * 2,
|
|
142
|
+
kind,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return obstacles;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Sample an area mark's fill region into bucket-based obstacles. The top
|
|
151
|
+
* boundary produces 'line' obstacles (hard), and the fill between top and
|
|
152
|
+
* bottom produces 'area-fill' obstacles (soft).
|
|
153
|
+
*/
|
|
154
|
+
export function sampleAreaObstacles(
|
|
155
|
+
topPoints: Array<{ x: number; y: number }>,
|
|
156
|
+
bottomPoints: Array<{ x: number; y: number }>,
|
|
157
|
+
bucketWidth = 24,
|
|
158
|
+
pad = 3,
|
|
159
|
+
): PlacementObstacle[] {
|
|
160
|
+
const lineObs = samplePolylineObstacles(topPoints, 'line', bucketWidth, pad);
|
|
161
|
+
|
|
162
|
+
// For the fill region, sample both top and bottom boundaries and create
|
|
163
|
+
// fill obstacles spanning from the top boundary down to the bottom boundary
|
|
164
|
+
// within each bucket.
|
|
165
|
+
if (topPoints.length < 2 || bottomPoints.length < 2) return lineObs;
|
|
166
|
+
|
|
167
|
+
let xMin = Infinity;
|
|
168
|
+
let xMax = -Infinity;
|
|
169
|
+
for (const p of topPoints) {
|
|
170
|
+
if (p.x < xMin) xMin = p.x;
|
|
171
|
+
if (p.x > xMax) xMax = p.x;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const span = xMax - xMin;
|
|
175
|
+
if (span <= 0) return lineObs;
|
|
176
|
+
|
|
177
|
+
const bucketCount = Math.ceil(span / bucketWidth);
|
|
178
|
+
const topBuckets: Array<{ minY: number; maxY: number }> = [];
|
|
179
|
+
const bottomBuckets: Array<{ minY: number; maxY: number }> = [];
|
|
180
|
+
for (let i = 0; i < bucketCount; i++) {
|
|
181
|
+
topBuckets.push({ minY: Infinity, maxY: -Infinity });
|
|
182
|
+
bottomBuckets.push({ minY: Infinity, maxY: -Infinity });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
fillBucketsFromSegments(topPoints, topBuckets, xMin, bucketWidth);
|
|
186
|
+
fillBucketsFromSegments(bottomPoints, bottomBuckets, xMin, bucketWidth);
|
|
187
|
+
|
|
188
|
+
const fillObs: PlacementObstacle[] = [];
|
|
189
|
+
for (let b = 0; b < bucketCount; b++) {
|
|
190
|
+
const top = topBuckets[b];
|
|
191
|
+
const bottom = bottomBuckets[b];
|
|
192
|
+
if (top.minY === Infinity || bottom.minY === Infinity) continue;
|
|
193
|
+
|
|
194
|
+
const fillTop = Math.min(top.minY, top.maxY);
|
|
195
|
+
const fillBottom = Math.max(bottom.minY, bottom.maxY);
|
|
196
|
+
if (fillBottom <= fillTop) continue;
|
|
197
|
+
|
|
198
|
+
fillObs.push({
|
|
199
|
+
x: xMin + b * bucketWidth,
|
|
200
|
+
y: fillTop,
|
|
201
|
+
width: bucketWidth,
|
|
202
|
+
height: fillBottom - fillTop,
|
|
203
|
+
kind: 'area-fill',
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return [...lineObs, ...fillObs];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Normalize a plain Rect[] to PlacementObstacle[] with a default kind. */
|
|
211
|
+
export function normalizeObstacles(
|
|
212
|
+
rects: Rect[],
|
|
213
|
+
kind: ObstacleKind = 'mark',
|
|
214
|
+
): PlacementObstacle[] {
|
|
215
|
+
return rects.map((r) => ({ ...r, kind }));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
// Scored placement search
|
|
220
|
+
// ---------------------------------------------------------------------------
|
|
221
|
+
|
|
222
|
+
const OBSTACLE_WEIGHTS: Record<ObstacleKind, number> = {
|
|
223
|
+
annotation: 30,
|
|
224
|
+
line: 20,
|
|
225
|
+
mark: 8,
|
|
226
|
+
'data-label': 8,
|
|
227
|
+
'endpoint-label': 8,
|
|
228
|
+
legend: 6,
|
|
229
|
+
watermark: 6,
|
|
230
|
+
'axis-band': 6,
|
|
231
|
+
'area-fill': 1.5,
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const W_SVG = 50;
|
|
235
|
+
const W_CHART = 6;
|
|
236
|
+
const W_DIST = 2;
|
|
237
|
+
const EPSILON = 0.001;
|
|
238
|
+
const CLEAN_THRESHOLD = EPSILON * 16;
|
|
239
|
+
|
|
240
|
+
interface DirectionDef {
|
|
241
|
+
ux: number;
|
|
242
|
+
uy: number;
|
|
243
|
+
textAnchor: 'start' | 'middle' | 'end';
|
|
244
|
+
attach:
|
|
245
|
+
| 'bottom-center'
|
|
246
|
+
| 'bottom-left'
|
|
247
|
+
| 'left-middle'
|
|
248
|
+
| 'top-left'
|
|
249
|
+
| 'top-center'
|
|
250
|
+
| 'top-right'
|
|
251
|
+
| 'right-middle'
|
|
252
|
+
| 'bottom-right';
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const D = Math.SQRT1_2;
|
|
256
|
+
|
|
257
|
+
const DIRECTIONS: DirectionDef[] = [
|
|
258
|
+
{ ux: 0, uy: -1, textAnchor: 'middle', attach: 'bottom-center' }, // N
|
|
259
|
+
{ ux: D, uy: -D, textAnchor: 'start', attach: 'bottom-left' }, // NE
|
|
260
|
+
{ ux: 1, uy: 0, textAnchor: 'start', attach: 'left-middle' }, // E
|
|
261
|
+
{ ux: D, uy: D, textAnchor: 'start', attach: 'top-left' }, // SE
|
|
262
|
+
{ ux: 0, uy: 1, textAnchor: 'middle', attach: 'top-center' }, // S
|
|
263
|
+
{ ux: -D, uy: D, textAnchor: 'end', attach: 'top-right' }, // SW
|
|
264
|
+
{ ux: -1, uy: 0, textAnchor: 'end', attach: 'right-middle' }, // W
|
|
265
|
+
{ ux: -D, uy: -D, textAnchor: 'end', attach: 'bottom-right' }, // NW
|
|
266
|
+
];
|
|
267
|
+
|
|
268
|
+
// Preferred direction orderings: away from data vertically, right over left
|
|
269
|
+
const LOWER_HALF_ORDER = [1, 0, 7, 2, 3, 4, 5, 6]; // NE, N, NW, E, SE, S, SW, W
|
|
270
|
+
const UPPER_HALF_ORDER = [3, 4, 5, 2, 1, 0, 7, 6]; // SE, S, SW, E, NE, N, NW, W
|
|
271
|
+
|
|
272
|
+
export interface PlacementCandidate {
|
|
273
|
+
labelX: number;
|
|
274
|
+
labelY: number;
|
|
275
|
+
textAnchor: 'start' | 'middle' | 'end';
|
|
276
|
+
box: Rect;
|
|
277
|
+
directionIndex: number;
|
|
278
|
+
ring: 1 | 2 | 3;
|
|
279
|
+
score: number;
|
|
280
|
+
candidateIdx: number;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export interface CandidateScoreBreakdown {
|
|
284
|
+
direction: string;
|
|
285
|
+
ring: 1 | 2 | 3;
|
|
286
|
+
box: Rect;
|
|
287
|
+
perObstacle: Array<{ kind: ObstacleKind; overlap: number; contribution: number }>;
|
|
288
|
+
outsideSvg: number;
|
|
289
|
+
outsideChart: number;
|
|
290
|
+
distance: number;
|
|
291
|
+
tiebreak: number;
|
|
292
|
+
total: number;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function computeAttachmentPoint(
|
|
296
|
+
anchorX: number,
|
|
297
|
+
anchorY: number,
|
|
298
|
+
dir: DirectionDef,
|
|
299
|
+
radius: number,
|
|
300
|
+
): { x: number; y: number } {
|
|
301
|
+
return {
|
|
302
|
+
x: anchorX + dir.ux * radius,
|
|
303
|
+
y: anchorY + dir.uy * radius,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function labelPositionFromAttachment(
|
|
308
|
+
attachPt: { x: number; y: number },
|
|
309
|
+
dir: DirectionDef,
|
|
310
|
+
boxHeight: number,
|
|
311
|
+
fontSize: number,
|
|
312
|
+
): { labelX: number; labelY: number } {
|
|
313
|
+
const labelX = attachPt.x;
|
|
314
|
+
let labelY: number;
|
|
315
|
+
|
|
316
|
+
const attach = dir.attach;
|
|
317
|
+
if (attach === 'left-middle' || attach === 'right-middle') {
|
|
318
|
+
labelY = attachPt.y - boxHeight / 2 + fontSize;
|
|
319
|
+
} else if (attach === 'top-left' || attach === 'top-center' || attach === 'top-right') {
|
|
320
|
+
labelY = attachPt.y + fontSize;
|
|
321
|
+
} else {
|
|
322
|
+
labelY = attachPt.y - boxHeight + fontSize;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return { labelX, labelY };
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function areaOutside(box: Rect, container: Rect): number {
|
|
329
|
+
const insideX = Math.max(
|
|
330
|
+
0,
|
|
331
|
+
Math.min(box.x + box.width, container.x + container.width) - Math.max(box.x, container.x),
|
|
332
|
+
);
|
|
333
|
+
const insideY = Math.max(
|
|
334
|
+
0,
|
|
335
|
+
Math.min(box.y + box.height, container.y + container.height) - Math.max(box.y, container.y),
|
|
336
|
+
);
|
|
337
|
+
const insideArea = insideX * insideY;
|
|
338
|
+
const boxArea = box.width * box.height;
|
|
339
|
+
return Math.max(0, boxArea - insideArea);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function inflateRect(r: Rect, pad: number): Rect {
|
|
343
|
+
return {
|
|
344
|
+
x: r.x - pad,
|
|
345
|
+
y: r.y - pad,
|
|
346
|
+
width: r.width + pad * 2,
|
|
347
|
+
height: r.height + pad * 2,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function scoreCandidate(
|
|
352
|
+
box: Rect,
|
|
353
|
+
obstacles: Rect[],
|
|
354
|
+
svgRect: Rect,
|
|
355
|
+
chartArea: Rect,
|
|
356
|
+
anchorX: number,
|
|
357
|
+
anchorY: number,
|
|
358
|
+
chartDiagonal: number,
|
|
359
|
+
candidateIndex: number,
|
|
360
|
+
debug?: boolean,
|
|
361
|
+
): { score: number; breakdown?: CandidateScoreBreakdown } {
|
|
362
|
+
const boxArea = box.width * box.height || 1;
|
|
363
|
+
const inflated = inflateRect(box, 3);
|
|
364
|
+
|
|
365
|
+
let obstacleScore = 0;
|
|
366
|
+
const perObstacle: CandidateScoreBreakdown['perObstacle'] = [];
|
|
367
|
+
|
|
368
|
+
for (const obs of obstacles) {
|
|
369
|
+
const kind = 'kind' in obs ? (obs as PlacementObstacle).kind : 'mark';
|
|
370
|
+
const overlap = overlapArea(inflated, obs);
|
|
371
|
+
if (overlap > 0) {
|
|
372
|
+
const weight = OBSTACLE_WEIGHTS[kind] ?? 8;
|
|
373
|
+
const contribution = (weight * overlap) / boxArea;
|
|
374
|
+
obstacleScore += contribution;
|
|
375
|
+
if (debug) {
|
|
376
|
+
perObstacle.push({ kind, overlap, contribution });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const outsideSvg = (W_SVG * areaOutside(box, svgRect)) / boxArea;
|
|
382
|
+
const outsideChart = (W_CHART * areaOutside(box, chartArea) * 0.3) / boxArea;
|
|
383
|
+
|
|
384
|
+
const nearestX = Math.max(box.x, Math.min(anchorX, box.x + box.width));
|
|
385
|
+
const nearestY = Math.max(box.y, Math.min(anchorY, box.y + box.height));
|
|
386
|
+
const dx = anchorX - nearestX;
|
|
387
|
+
const dy = anchorY - nearestY;
|
|
388
|
+
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
389
|
+
const distance = (W_DIST * dist) / (chartDiagonal || 1);
|
|
390
|
+
|
|
391
|
+
const tiebreak = candidateIndex * EPSILON;
|
|
392
|
+
const score = obstacleScore + outsideSvg + outsideChart + distance + tiebreak;
|
|
393
|
+
|
|
394
|
+
if (debug) {
|
|
395
|
+
return {
|
|
396
|
+
score,
|
|
397
|
+
breakdown: {
|
|
398
|
+
direction: '',
|
|
399
|
+
ring: 1,
|
|
400
|
+
box,
|
|
401
|
+
perObstacle,
|
|
402
|
+
outsideSvg,
|
|
403
|
+
outsideChart,
|
|
404
|
+
distance,
|
|
405
|
+
tiebreak,
|
|
406
|
+
total: score,
|
|
407
|
+
},
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return { score };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export interface PlacementResult {
|
|
415
|
+
labelX: number;
|
|
416
|
+
labelY: number;
|
|
417
|
+
textAnchor: 'start' | 'middle' | 'end';
|
|
418
|
+
bounds: Rect;
|
|
419
|
+
score: number;
|
|
420
|
+
debug?: CandidateScoreBreakdown[];
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Run the scored placement search for a single auto annotation.
|
|
425
|
+
* Generates candidates around the anchor point, scores each, returns the best.
|
|
426
|
+
*/
|
|
427
|
+
export function findBestPlacement(
|
|
428
|
+
anchorX: number,
|
|
429
|
+
anchorY: number,
|
|
430
|
+
text: string,
|
|
431
|
+
style: { fontSize: number; fontWeight: number; lineHeight: number },
|
|
432
|
+
subtitleText: string | undefined,
|
|
433
|
+
subtitleStyle: { fontSize: number; fontWeight: number; lineHeight: number } | undefined,
|
|
434
|
+
obstacles: Rect[],
|
|
435
|
+
chartArea: Rect,
|
|
436
|
+
svgRect: Rect,
|
|
437
|
+
measure: AnnotationMeasureTextFn = heuristicMeasure,
|
|
438
|
+
debug = false,
|
|
439
|
+
): PlacementResult {
|
|
440
|
+
const isUpperHalf = anchorY < chartArea.y + chartArea.height / 2;
|
|
441
|
+
const dirOrder = isUpperHalf ? UPPER_HALF_ORDER : LOWER_HALF_ORDER;
|
|
442
|
+
const chartDiagonal = Math.sqrt(chartArea.width ** 2 + chartArea.height ** 2);
|
|
443
|
+
|
|
444
|
+
// Compute box dimensions for candidate generation
|
|
445
|
+
const sampleBounds = computeTextBlockBounds(
|
|
446
|
+
0,
|
|
447
|
+
style.fontSize,
|
|
448
|
+
text,
|
|
449
|
+
{
|
|
450
|
+
...style,
|
|
451
|
+
textAnchor: 'start',
|
|
452
|
+
},
|
|
453
|
+
measure,
|
|
454
|
+
);
|
|
455
|
+
const boxHeight = sampleBounds.height;
|
|
456
|
+
|
|
457
|
+
const r1 = 12;
|
|
458
|
+
const r2 = r1 + boxHeight + 8;
|
|
459
|
+
const r3 = 2 * r2;
|
|
460
|
+
|
|
461
|
+
let nextCandidateIdx = 0;
|
|
462
|
+
|
|
463
|
+
function generateCandidates(radii: number[]): PlacementCandidate[] {
|
|
464
|
+
const candidates: PlacementCandidate[] = [];
|
|
465
|
+
|
|
466
|
+
for (const radius of radii) {
|
|
467
|
+
const ring = radius === r1 ? 1 : radius === r2 ? 2 : 3;
|
|
468
|
+
for (const di of dirOrder) {
|
|
469
|
+
const dir = DIRECTIONS[di];
|
|
470
|
+
const attachPt = computeAttachmentPoint(anchorX, anchorY, dir, radius);
|
|
471
|
+
const { labelX, labelY } = labelPositionFromAttachment(
|
|
472
|
+
attachPt,
|
|
473
|
+
dir,
|
|
474
|
+
boxHeight,
|
|
475
|
+
style.fontSize,
|
|
476
|
+
);
|
|
477
|
+
|
|
478
|
+
let box = computeTextBlockBounds(
|
|
479
|
+
labelX,
|
|
480
|
+
labelY,
|
|
481
|
+
text,
|
|
482
|
+
{
|
|
483
|
+
...style,
|
|
484
|
+
textAnchor: dir.textAnchor,
|
|
485
|
+
},
|
|
486
|
+
measure,
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
// Union with subtitle bounds if present
|
|
490
|
+
if (subtitleText && subtitleStyle) {
|
|
491
|
+
const primaryLineCount = text.split('\n').length;
|
|
492
|
+
const subtitleY = labelY + style.fontSize * style.lineHeight * primaryLineCount + 2;
|
|
493
|
+
const subBounds = computeTextBlockBounds(
|
|
494
|
+
labelX,
|
|
495
|
+
subtitleY,
|
|
496
|
+
subtitleText,
|
|
497
|
+
{
|
|
498
|
+
...subtitleStyle,
|
|
499
|
+
textAnchor: dir.textAnchor,
|
|
500
|
+
},
|
|
501
|
+
measure,
|
|
502
|
+
);
|
|
503
|
+
box = unionRects(box, subBounds);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const idx = nextCandidateIdx++;
|
|
507
|
+
const { score } = scoreCandidate(
|
|
508
|
+
box,
|
|
509
|
+
obstacles,
|
|
510
|
+
svgRect,
|
|
511
|
+
chartArea,
|
|
512
|
+
anchorX,
|
|
513
|
+
anchorY,
|
|
514
|
+
chartDiagonal,
|
|
515
|
+
idx,
|
|
516
|
+
);
|
|
517
|
+
|
|
518
|
+
candidates.push({
|
|
519
|
+
labelX,
|
|
520
|
+
labelY,
|
|
521
|
+
textAnchor: dir.textAnchor,
|
|
522
|
+
box,
|
|
523
|
+
directionIndex: di,
|
|
524
|
+
ring: ring as 1 | 2 | 3,
|
|
525
|
+
score,
|
|
526
|
+
candidateIdx: idx,
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return candidates;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Ring 1 + Ring 2 (16 candidates)
|
|
535
|
+
let candidates = generateCandidates([r1, r2]);
|
|
536
|
+
let best = candidates.reduce((a, b) => (a.score <= b.score ? a : b));
|
|
537
|
+
|
|
538
|
+
// Ring 3 if no clean placement found
|
|
539
|
+
if (best.score > CLEAN_THRESHOLD) {
|
|
540
|
+
const ring3 = generateCandidates([r3]);
|
|
541
|
+
candidates = [...candidates, ...ring3];
|
|
542
|
+
best = candidates.reduce((a, b) => (a.score <= b.score ? a : b));
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
let debugBreakdowns: CandidateScoreBreakdown[] | undefined;
|
|
546
|
+
if (debug) {
|
|
547
|
+
const dirNames = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
|
|
548
|
+
debugBreakdowns = candidates.map((c) => {
|
|
549
|
+
const { breakdown } = scoreCandidate(
|
|
550
|
+
c.box,
|
|
551
|
+
obstacles,
|
|
552
|
+
svgRect,
|
|
553
|
+
chartArea,
|
|
554
|
+
anchorX,
|
|
555
|
+
anchorY,
|
|
556
|
+
chartDiagonal,
|
|
557
|
+
c.candidateIdx,
|
|
558
|
+
true,
|
|
559
|
+
);
|
|
560
|
+
return {
|
|
561
|
+
...breakdown!,
|
|
562
|
+
direction: dirNames[c.directionIndex],
|
|
563
|
+
ring: c.ring,
|
|
564
|
+
};
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return {
|
|
569
|
+
labelX: best.labelX,
|
|
570
|
+
labelY: best.labelY,
|
|
571
|
+
textAnchor: best.textAnchor,
|
|
572
|
+
bounds: best.box,
|
|
573
|
+
score: best.score,
|
|
574
|
+
debug: debugBreakdowns,
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** Check if an annotation should use the placement search (pure-auto, non-drop-line). */
|
|
579
|
+
export function isAutoPlacement(annotation: {
|
|
580
|
+
anchor?: string;
|
|
581
|
+
offset?: { dx?: number; dy?: number };
|
|
582
|
+
connector?: string | boolean;
|
|
583
|
+
}): boolean {
|
|
584
|
+
if (annotation.offset) return false;
|
|
585
|
+
if (annotation.anchor && annotation.anchor !== 'auto') return false;
|
|
586
|
+
if (annotation.connector === 'drop-line') return false;
|
|
587
|
+
return true;
|
|
588
|
+
}
|