@opendata-ai/openchart-engine 8.0.0-rc.2 → 8.0.0-rc.20

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 (271) hide show
  1. package/dist/index.d.ts +140 -2
  2. package/dist/index.js +1403 -210
  3. package/dist/index.js.map +1 -1
  4. package/package.json +3 -4
  5. package/src/__test-fixtures__/specs.ts +0 -182
  6. package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +0 -2339
  7. package/src/__tests__/annotation-behavior.test.ts +0 -721
  8. package/src/__tests__/axes.test.ts +0 -1579
  9. package/src/__tests__/compile-animation.test.ts +0 -161
  10. package/src/__tests__/compile-chart.test.ts +0 -1303
  11. package/src/__tests__/compile-footnotes.test.ts +0 -277
  12. package/src/__tests__/compile-layer.test.ts +0 -969
  13. package/src/__tests__/compile-snapshot.test.ts +0 -156
  14. package/src/__tests__/compound-labels.test.ts +0 -147
  15. package/src/__tests__/dimensions.test.ts +0 -918
  16. package/src/__tests__/encoding-sugar.test.ts +0 -390
  17. package/src/__tests__/facet.test.ts +0 -215
  18. package/src/__tests__/helpers/invariants.ts +0 -299
  19. package/src/__tests__/known-bugs.test.ts +0 -566
  20. package/src/__tests__/layout-fuzz.test.ts +0 -358
  21. package/src/__tests__/layout-invariants.test.ts +0 -535
  22. package/src/__tests__/legend.test.ts +0 -721
  23. package/src/__tests__/mark-keys.test.ts +0 -294
  24. package/src/__tests__/ranking-recipes.test.ts +0 -260
  25. package/src/__tests__/rotation.test.ts +0 -83
  26. package/src/__tests__/scales.test.ts +0 -323
  27. package/src/__tests__/series-search.test.ts +0 -92
  28. package/src/__tests__/spec-sugar.test.ts +0 -801
  29. package/src/__tests__/you-draw-it.test.ts +0 -160
  30. package/src/annotations/__tests__/compute.test.ts +0 -1672
  31. package/src/annotations/__tests__/geometry.test.ts +0 -66
  32. package/src/annotations/__tests__/placement-fuzz.test.ts +0 -256
  33. package/src/annotations/__tests__/placement-invariants.test.ts +0 -345
  34. package/src/annotations/__tests__/placement.test.ts +0 -316
  35. package/src/annotations/__tests__/resolve-text.test.ts +0 -788
  36. package/src/annotations/__tests__/rich-text.test.ts +0 -91
  37. package/src/annotations/__tests__/thinning.test.ts +0 -285
  38. package/src/annotations/collisions.ts +0 -326
  39. package/src/annotations/compute.ts +0 -333
  40. package/src/annotations/constants.ts +0 -162
  41. package/src/annotations/geometry.ts +0 -370
  42. package/src/annotations/move.ts +0 -135
  43. package/src/annotations/placement.ts +0 -636
  44. package/src/annotations/position.ts +0 -131
  45. package/src/annotations/resolve-range.ts +0 -117
  46. package/src/annotations/resolve-refline.ts +0 -157
  47. package/src/annotations/resolve-text.ts +0 -539
  48. package/src/annotations/rich-text.ts +0 -65
  49. package/src/annotations/thinning.ts +0 -190
  50. package/src/barlist/__tests__/compile-barlist.test.ts +0 -200
  51. package/src/barlist/compile-barlist.ts +0 -391
  52. package/src/barlist/types.ts +0 -28
  53. package/src/charts/__tests__/post-process.test.ts +0 -237
  54. package/src/charts/__tests__/registry.test.ts +0 -116
  55. package/src/charts/__tests__/utils.test.ts +0 -195
  56. package/src/charts/_shared/__tests__/density-filter.test.ts +0 -32
  57. package/src/charts/_shared/density-filter.ts +0 -26
  58. package/src/charts/_shared/format-label-value.ts +0 -16
  59. package/src/charts/bar/__tests__/compute.test.ts +0 -934
  60. package/src/charts/bar/__tests__/gradient-orient.test.ts +0 -127
  61. package/src/charts/bar/__tests__/labels.test.ts +0 -292
  62. package/src/charts/bar/compute.ts +0 -539
  63. package/src/charts/bar/index.ts +0 -60
  64. package/src/charts/bar/labels.ts +0 -311
  65. package/src/charts/beeswarm/__tests__/compute.test.ts +0 -280
  66. package/src/charts/beeswarm/__tests__/dodge.test.ts +0 -148
  67. package/src/charts/beeswarm/compute.ts +0 -203
  68. package/src/charts/beeswarm/dodge.ts +0 -155
  69. package/src/charts/beeswarm/index.ts +0 -32
  70. package/src/charts/builtin.ts +0 -75
  71. package/src/charts/calendar/__tests__/compute.test.ts +0 -417
  72. package/src/charts/calendar/compute.ts +0 -319
  73. package/src/charts/calendar/index.ts +0 -32
  74. package/src/charts/column/__tests__/compute.test.ts +0 -540
  75. package/src/charts/column/__tests__/labels.test.ts +0 -179
  76. package/src/charts/column/compute.ts +0 -493
  77. package/src/charts/column/index.ts +0 -53
  78. package/src/charts/column/labels.ts +0 -229
  79. package/src/charts/dot/__tests__/compute.test.ts +0 -380
  80. package/src/charts/dot/__tests__/labels.test.ts +0 -98
  81. package/src/charts/dot/compute.ts +0 -285
  82. package/src/charts/dot/index.ts +0 -62
  83. package/src/charts/dot/labels.ts +0 -121
  84. package/src/charts/line/__tests__/compute.test.ts +0 -1194
  85. package/src/charts/line/__tests__/curves.test.ts +0 -51
  86. package/src/charts/line/__tests__/labels.test.ts +0 -94
  87. package/src/charts/line/__tests__/mark-options.test.ts +0 -175
  88. package/src/charts/line/area.ts +0 -484
  89. package/src/charts/line/compute.ts +0 -356
  90. package/src/charts/line/curves.ts +0 -85
  91. package/src/charts/line/index.ts +0 -176
  92. package/src/charts/line/labels.ts +0 -177
  93. package/src/charts/parliament/__tests__/compute.test.ts +0 -411
  94. package/src/charts/parliament/compute.ts +0 -313
  95. package/src/charts/parliament/index.ts +0 -32
  96. package/src/charts/pie/__tests__/compute.test.ts +0 -384
  97. package/src/charts/pie/__tests__/labels.test.ts +0 -132
  98. package/src/charts/pie/compute.ts +0 -324
  99. package/src/charts/pie/index.ts +0 -49
  100. package/src/charts/pie/labels.ts +0 -143
  101. package/src/charts/post-process.ts +0 -229
  102. package/src/charts/range/__tests__/compute.test.ts +0 -486
  103. package/src/charts/range/compute.ts +0 -273
  104. package/src/charts/range/index.ts +0 -82
  105. package/src/charts/range/labels.ts +0 -169
  106. package/src/charts/rect/__tests__/compute.test.ts +0 -120
  107. package/src/charts/rect/index.ts +0 -127
  108. package/src/charts/registry.ts +0 -72
  109. package/src/charts/rule/index.ts +0 -139
  110. package/src/charts/scatter/__tests__/compute.test.ts +0 -488
  111. package/src/charts/scatter/__tests__/trendline.test.ts +0 -191
  112. package/src/charts/scatter/compute.ts +0 -192
  113. package/src/charts/scatter/index.ts +0 -46
  114. package/src/charts/scatter/trendline.ts +0 -100
  115. package/src/charts/text/__tests__/compute.test.ts +0 -307
  116. package/src/charts/text/index.ts +0 -141
  117. package/src/charts/tick/__tests__/compute.test.ts +0 -75
  118. package/src/charts/tick/index.ts +0 -99
  119. package/src/charts/utils.ts +0 -170
  120. package/src/charts/waffle/__tests__/compute.test.ts +0 -305
  121. package/src/charts/waffle/compute.ts +0 -197
  122. package/src/charts/waffle/index.ts +0 -31
  123. package/src/compile/__tests__/color-scale-range.test.ts +0 -158
  124. package/src/compile/__tests__/color-scale-reverse.test.ts +0 -89
  125. package/src/compile/__tests__/data-clip.test.ts +0 -127
  126. package/src/compile/__tests__/fill-patterns.test.ts +0 -154
  127. package/src/compile/__tests__/size-scale.test.ts +0 -108
  128. package/src/compile/__tests__/watermark-obstacle.test.ts +0 -93
  129. package/src/compile/color-scale-range.ts +0 -101
  130. package/src/compile/data-clip.ts +0 -48
  131. package/src/compile/fill-patterns.ts +0 -74
  132. package/src/compile/layer.ts +0 -626
  133. package/src/compile/size-scale.ts +0 -139
  134. package/src/compile/spec-sugar.ts +0 -758
  135. package/src/compile/watermark-obstacle.ts +0 -54
  136. package/src/compile/you-draw-it.ts +0 -123
  137. package/src/compile.ts +0 -1450
  138. package/src/compiler/__tests__/a11y-warnings.test.ts +0 -148
  139. package/src/compiler/__tests__/animation.test.ts +0 -177
  140. package/src/compiler/__tests__/compile.test.ts +0 -87
  141. package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +0 -19
  142. package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +0 -20
  143. package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +0 -22
  144. package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +0 -19
  145. package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +0 -14
  146. package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +0 -34
  147. package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +0 -22
  148. package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +0 -41
  149. package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +0 -38
  150. package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +0 -28
  151. package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +0 -28
  152. package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +0 -30
  153. package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +0 -29
  154. package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +0 -32
  155. package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +0 -25
  156. package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +0 -19
  157. package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +0 -19
  158. package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +0 -21
  159. package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +0 -23
  160. package/src/compiler/__tests__/keys.test.ts +0 -83
  161. package/src/compiler/__tests__/normalize-highlight.test.ts +0 -103
  162. package/src/compiler/__tests__/normalize.test.ts +0 -399
  163. package/src/compiler/__tests__/sparkline-defaults.test.ts +0 -153
  164. package/src/compiler/__tests__/validate-did-you-mean.test.ts +0 -86
  165. package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
  166. package/src/compiler/__tests__/validate-near-miss.test.ts +0 -89
  167. package/src/compiler/__tests__/validate.test.ts +0 -629
  168. package/src/compiler/__tests__/vl-parity.test.ts +0 -68
  169. package/src/compiler/a11y-warnings.ts +0 -110
  170. package/src/compiler/animation.ts +0 -142
  171. package/src/compiler/index.ts +0 -47
  172. package/src/compiler/keys.ts +0 -55
  173. package/src/compiler/normalize.ts +0 -733
  174. package/src/compiler/sparkline-defaults.ts +0 -138
  175. package/src/compiler/types.ts +0 -241
  176. package/src/compiler/validate.ts +0 -1810
  177. package/src/endpoint-labels/__tests__/compute.test.ts +0 -587
  178. package/src/endpoint-labels/compute.ts +0 -621
  179. package/src/endpoint-labels/constants.ts +0 -54
  180. package/src/endpoint-labels/format.ts +0 -60
  181. package/src/endpoint-labels/predict.ts +0 -139
  182. package/src/format/__tests__/field-format.test.ts +0 -164
  183. package/src/format/field-format.ts +0 -27
  184. package/src/geo/__tests__/compile-map.test.ts +0 -621
  185. package/src/geo/compile-map.ts +0 -856
  186. package/src/geo/join.ts +0 -115
  187. package/src/geo/projections.ts +0 -45
  188. package/src/geo/types.ts +0 -24
  189. package/src/graphs/__tests__/community.test.ts +0 -228
  190. package/src/graphs/__tests__/compile-graph.test.ts +0 -396
  191. package/src/graphs/__tests__/encoding.test.ts +0 -415
  192. package/src/graphs/community.ts +0 -92
  193. package/src/graphs/compile-graph.ts +0 -329
  194. package/src/graphs/encoding.ts +0 -356
  195. package/src/graphs/types.ts +0 -106
  196. package/src/index.ts +0 -177
  197. package/src/layout/__tests__/metrics.test.ts +0 -97
  198. package/src/layout/__tests__/scales-highlight.test.ts +0 -192
  199. package/src/layout/axes/rotation.ts +0 -152
  200. package/src/layout/axes/thinning.ts +0 -98
  201. package/src/layout/axes/ticks.ts +0 -409
  202. package/src/layout/axes.ts +0 -757
  203. package/src/layout/dimensions.ts +0 -912
  204. package/src/layout/facet.ts +0 -91
  205. package/src/layout/gridlines.ts +0 -84
  206. package/src/layout/metrics.ts +0 -132
  207. package/src/layout/plan.ts +0 -631
  208. package/src/layout/scales.ts +0 -1053
  209. package/src/layout/shared.ts +0 -44
  210. package/src/legend/__tests__/continuous.test.ts +0 -318
  211. package/src/legend/__tests__/redundancy.test.ts +0 -108
  212. package/src/legend/__tests__/size.test.ts +0 -348
  213. package/src/legend/__tests__/suppression.test.ts +0 -382
  214. package/src/legend/compute.ts +0 -747
  215. package/src/legend/continuous.ts +0 -327
  216. package/src/legend/size.ts +0 -407
  217. package/src/legend/suppression.ts +0 -226
  218. package/src/legend/wrap.ts +0 -117
  219. package/src/sankey/__tests__/compile-sankey.test.ts +0 -458
  220. package/src/sankey/__tests__/layout.test.ts +0 -165
  221. package/src/sankey/__tests__/node-label-wrap.test.ts +0 -114
  222. package/src/sankey/__tests__/node-sort.test.ts +0 -45
  223. package/src/sankey/compile-sankey.ts +0 -706
  224. package/src/sankey/layout.ts +0 -184
  225. package/src/sankey/types.ts +0 -41
  226. package/src/tables/__tests__/bar-column.test.ts +0 -147
  227. package/src/tables/__tests__/category-colors.test.ts +0 -230
  228. package/src/tables/__tests__/compile-table.test.ts +0 -236
  229. package/src/tables/__tests__/format-cells.test.ts +0 -143
  230. package/src/tables/__tests__/heatmap.test.ts +0 -148
  231. package/src/tables/__tests__/pagination.test.ts +0 -78
  232. package/src/tables/__tests__/search.test.ts +0 -94
  233. package/src/tables/__tests__/sort.test.ts +0 -107
  234. package/src/tables/__tests__/sparkline.test.ts +0 -122
  235. package/src/tables/bar-column.ts +0 -94
  236. package/src/tables/category-colors.ts +0 -78
  237. package/src/tables/compile-table.ts +0 -427
  238. package/src/tables/format-cells.ts +0 -115
  239. package/src/tables/heatmap.ts +0 -141
  240. package/src/tables/pagination.ts +0 -46
  241. package/src/tables/search.ts +0 -66
  242. package/src/tables/sort.ts +0 -69
  243. package/src/tables/sparkline.ts +0 -113
  244. package/src/tables/utils.ts +0 -16
  245. package/src/tilemap/__tests__/compile-tilemap.test.ts +0 -576
  246. package/src/tilemap/compile-tilemap.ts +0 -755
  247. package/src/tilemap/layout.ts +0 -172
  248. package/src/tilemap/types.ts +0 -33
  249. package/src/tooltips/__tests__/compute.test.ts +0 -582
  250. package/src/tooltips/compute.ts +0 -700
  251. package/src/transforms/__tests__/aggregate.test.ts +0 -159
  252. package/src/transforms/__tests__/bin.test.ts +0 -88
  253. package/src/transforms/__tests__/calculate.test.ts +0 -146
  254. package/src/transforms/__tests__/conditional.test.ts +0 -127
  255. package/src/transforms/__tests__/filter-relative.test.ts +0 -202
  256. package/src/transforms/__tests__/filter.test.ts +0 -59
  257. package/src/transforms/__tests__/fold.test.ts +0 -79
  258. package/src/transforms/__tests__/index.test.ts +0 -93
  259. package/src/transforms/__tests__/predicates.test.ts +0 -193
  260. package/src/transforms/__tests__/timeunit.test.ts +0 -129
  261. package/src/transforms/__tests__/window.test.ts +0 -286
  262. package/src/transforms/aggregate.ts +0 -131
  263. package/src/transforms/bin.ts +0 -87
  264. package/src/transforms/calculate.ts +0 -60
  265. package/src/transforms/conditional.ts +0 -46
  266. package/src/transforms/filter.ts +0 -122
  267. package/src/transforms/fold.ts +0 -49
  268. package/src/transforms/index.ts +0 -60
  269. package/src/transforms/predicates.ts +0 -123
  270. package/src/transforms/timeunit.ts +0 -88
  271. package/src/transforms/window.ts +0 -185
@@ -1,66 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { computeArrowheadPoints } from '../geometry';
3
-
4
- describe('computeArrowheadPoints', () => {
5
- it('produces a downward-pointing arrow for a downward tangent', () => {
6
- const head = computeArrowheadPoints(100, 200, 0, 1);
7
- expect(head.tip).toEqual({ x: 100, y: 200 });
8
- // Base should be above the tip (tangent points down)
9
- expect(head.baseLeft.y).toBeLessThan(head.tip.y);
10
- expect(head.baseRight.y).toBeLessThan(head.tip.y);
11
- // Base corners symmetric around the tangent axis
12
- expect(head.baseLeft.x).toBeLessThan(head.tip.x);
13
- expect(head.baseRight.x).toBeGreaterThan(head.tip.x);
14
- });
15
-
16
- it('produces a rightward-pointing arrow for a rightward tangent', () => {
17
- const head = computeArrowheadPoints(200, 100, 1, 0);
18
- expect(head.tip).toEqual({ x: 200, y: 100 });
19
- // Base should be to the left of the tip
20
- expect(head.baseLeft.x).toBeLessThan(head.tip.x);
21
- expect(head.baseRight.x).toBeLessThan(head.tip.x);
22
- });
23
-
24
- it('respects custom length and halfWidth', () => {
25
- const defaultHead = computeArrowheadPoints(100, 200, 0, 1);
26
- const bigHead = computeArrowheadPoints(100, 200, 0, 1, 16, 8);
27
-
28
- // Bigger arrow should have base further from tip
29
- const defaultBaseDist = Math.abs(defaultHead.baseLeft.y - defaultHead.tip.y);
30
- const bigBaseDist = Math.abs(bigHead.baseLeft.y - bigHead.tip.y);
31
- expect(bigBaseDist).toBeGreaterThan(defaultBaseDist);
32
-
33
- // Bigger arrow should have wider base
34
- const defaultWidth = Math.abs(defaultHead.baseLeft.x - defaultHead.baseRight.x);
35
- const bigWidth = Math.abs(bigHead.baseLeft.x - bigHead.baseRight.x);
36
- expect(bigWidth).toBeGreaterThan(defaultWidth);
37
- });
38
-
39
- it('handles diagonal tangent', () => {
40
- const head = computeArrowheadPoints(100, 100, 1, 1);
41
- expect(head.tip).toEqual({ x: 100, y: 100 });
42
- // Base points should be behind the tip along the diagonal
43
- const baseMidX = (head.baseLeft.x + head.baseRight.x) / 2;
44
- const baseMidY = (head.baseLeft.y + head.baseRight.y) / 2;
45
- expect(baseMidX).toBeLessThan(head.tip.x);
46
- expect(baseMidY).toBeLessThan(head.tip.y);
47
- });
48
-
49
- it('uses defaults of length=7, halfWidth=3.5', () => {
50
- const head = computeArrowheadPoints(100, 200, 0, 1);
51
- // For a pure downward tangent, base is 7px above tip
52
- expect(head.baseLeft.y).toBeCloseTo(193);
53
- expect(head.baseRight.y).toBeCloseTo(193);
54
- // Base corners are 3.5px to each side
55
- expect(head.baseLeft.x).toBeCloseTo(96.5);
56
- expect(head.baseRight.x).toBeCloseTo(103.5);
57
- });
58
-
59
- it('handles zero-length tangent gracefully', () => {
60
- const head = computeArrowheadPoints(100, 100, 0, 0);
61
- expect(head.tip).toEqual({ x: 100, y: 100 });
62
- // Should not produce NaN
63
- expect(Number.isFinite(head.baseLeft.x)).toBe(true);
64
- expect(Number.isFinite(head.baseLeft.y)).toBe(true);
65
- });
66
- });
@@ -1,256 +0,0 @@
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
- });
@@ -1,345 +0,0 @@
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
- });