@opendata-ai/openchart-engine 8.0.0-rc.10 → 8.0.0-rc.11

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 (270) hide show
  1. package/dist/index.js +2 -3
  2. package/dist/index.js.map +1 -1
  3. package/package.json +3 -4
  4. package/src/__test-fixtures__/specs.ts +0 -182
  5. package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +0 -2401
  6. package/src/__tests__/annotation-behavior.test.ts +0 -852
  7. package/src/__tests__/axes.test.ts +0 -1579
  8. package/src/__tests__/compile-animation.test.ts +0 -161
  9. package/src/__tests__/compile-chart.test.ts +0 -1303
  10. package/src/__tests__/compile-footnotes.test.ts +0 -277
  11. package/src/__tests__/compile-layer.test.ts +0 -969
  12. package/src/__tests__/compile-snapshot.test.ts +0 -156
  13. package/src/__tests__/compound-labels.test.ts +0 -147
  14. package/src/__tests__/dimensions.test.ts +0 -918
  15. package/src/__tests__/encoding-sugar.test.ts +0 -390
  16. package/src/__tests__/facet.test.ts +0 -359
  17. package/src/__tests__/helpers/invariants.ts +0 -299
  18. package/src/__tests__/known-bugs.test.ts +0 -566
  19. package/src/__tests__/layout-fuzz.test.ts +0 -358
  20. package/src/__tests__/layout-invariants.test.ts +0 -535
  21. package/src/__tests__/legend.test.ts +0 -721
  22. package/src/__tests__/mark-keys.test.ts +0 -294
  23. package/src/__tests__/ranking-recipes.test.ts +0 -260
  24. package/src/__tests__/rotation.test.ts +0 -83
  25. package/src/__tests__/scales.test.ts +0 -323
  26. package/src/__tests__/series-search.test.ts +0 -92
  27. package/src/__tests__/spec-sugar.test.ts +0 -801
  28. package/src/__tests__/you-draw-it.test.ts +0 -160
  29. package/src/annotations/__tests__/compute.test.ts +0 -1672
  30. package/src/annotations/__tests__/geometry.test.ts +0 -66
  31. package/src/annotations/__tests__/placement-fuzz.test.ts +0 -256
  32. package/src/annotations/__tests__/placement-invariants.test.ts +0 -345
  33. package/src/annotations/__tests__/placement.test.ts +0 -316
  34. package/src/annotations/__tests__/resolve-text.test.ts +0 -788
  35. package/src/annotations/__tests__/rich-text.test.ts +0 -91
  36. package/src/annotations/__tests__/thinning.test.ts +0 -285
  37. package/src/annotations/collisions.ts +0 -326
  38. package/src/annotations/compute.ts +0 -333
  39. package/src/annotations/constants.ts +0 -162
  40. package/src/annotations/geometry.ts +0 -376
  41. package/src/annotations/move.ts +0 -135
  42. package/src/annotations/placement.ts +0 -636
  43. package/src/annotations/position.ts +0 -131
  44. package/src/annotations/resolve-range.ts +0 -117
  45. package/src/annotations/resolve-refline.ts +0 -157
  46. package/src/annotations/resolve-text.ts +0 -545
  47. package/src/annotations/rich-text.ts +0 -65
  48. package/src/annotations/thinning.ts +0 -190
  49. package/src/barlist/__tests__/compile-barlist.test.ts +0 -200
  50. package/src/barlist/compile-barlist.ts +0 -391
  51. package/src/barlist/types.ts +0 -28
  52. package/src/charts/__tests__/post-process.test.ts +0 -237
  53. package/src/charts/__tests__/registry.test.ts +0 -116
  54. package/src/charts/__tests__/utils.test.ts +0 -195
  55. package/src/charts/_shared/__tests__/density-filter.test.ts +0 -32
  56. package/src/charts/_shared/density-filter.ts +0 -26
  57. package/src/charts/_shared/format-label-value.ts +0 -16
  58. package/src/charts/bar/__tests__/compute.test.ts +0 -934
  59. package/src/charts/bar/__tests__/gradient-orient.test.ts +0 -127
  60. package/src/charts/bar/__tests__/labels.test.ts +0 -292
  61. package/src/charts/bar/compute.ts +0 -539
  62. package/src/charts/bar/index.ts +0 -60
  63. package/src/charts/bar/labels.ts +0 -311
  64. package/src/charts/beeswarm/__tests__/compute.test.ts +0 -280
  65. package/src/charts/beeswarm/__tests__/dodge.test.ts +0 -148
  66. package/src/charts/beeswarm/compute.ts +0 -203
  67. package/src/charts/beeswarm/dodge.ts +0 -155
  68. package/src/charts/beeswarm/index.ts +0 -32
  69. package/src/charts/builtin.ts +0 -75
  70. package/src/charts/calendar/__tests__/compute.test.ts +0 -417
  71. package/src/charts/calendar/compute.ts +0 -319
  72. package/src/charts/calendar/index.ts +0 -32
  73. package/src/charts/column/__tests__/compute.test.ts +0 -540
  74. package/src/charts/column/__tests__/labels.test.ts +0 -179
  75. package/src/charts/column/compute.ts +0 -493
  76. package/src/charts/column/index.ts +0 -53
  77. package/src/charts/column/labels.ts +0 -229
  78. package/src/charts/dot/__tests__/compute.test.ts +0 -380
  79. package/src/charts/dot/__tests__/labels.test.ts +0 -98
  80. package/src/charts/dot/compute.ts +0 -285
  81. package/src/charts/dot/index.ts +0 -62
  82. package/src/charts/dot/labels.ts +0 -121
  83. package/src/charts/line/__tests__/compute.test.ts +0 -1194
  84. package/src/charts/line/__tests__/curves.test.ts +0 -51
  85. package/src/charts/line/__tests__/labels.test.ts +0 -94
  86. package/src/charts/line/__tests__/mark-options.test.ts +0 -175
  87. package/src/charts/line/area.ts +0 -484
  88. package/src/charts/line/compute.ts +0 -356
  89. package/src/charts/line/curves.ts +0 -85
  90. package/src/charts/line/index.ts +0 -176
  91. package/src/charts/line/labels.ts +0 -177
  92. package/src/charts/parliament/__tests__/compute.test.ts +0 -411
  93. package/src/charts/parliament/compute.ts +0 -313
  94. package/src/charts/parliament/index.ts +0 -32
  95. package/src/charts/pie/__tests__/compute.test.ts +0 -384
  96. package/src/charts/pie/__tests__/labels.test.ts +0 -132
  97. package/src/charts/pie/compute.ts +0 -324
  98. package/src/charts/pie/index.ts +0 -49
  99. package/src/charts/pie/labels.ts +0 -143
  100. package/src/charts/post-process.ts +0 -229
  101. package/src/charts/range/__tests__/compute.test.ts +0 -486
  102. package/src/charts/range/compute.ts +0 -273
  103. package/src/charts/range/index.ts +0 -82
  104. package/src/charts/range/labels.ts +0 -169
  105. package/src/charts/rect/__tests__/compute.test.ts +0 -120
  106. package/src/charts/rect/index.ts +0 -127
  107. package/src/charts/registry.ts +0 -72
  108. package/src/charts/rule/index.ts +0 -139
  109. package/src/charts/scatter/__tests__/compute.test.ts +0 -488
  110. package/src/charts/scatter/__tests__/trendline.test.ts +0 -191
  111. package/src/charts/scatter/compute.ts +0 -192
  112. package/src/charts/scatter/index.ts +0 -46
  113. package/src/charts/scatter/trendline.ts +0 -100
  114. package/src/charts/text/__tests__/compute.test.ts +0 -307
  115. package/src/charts/text/index.ts +0 -141
  116. package/src/charts/tick/__tests__/compute.test.ts +0 -75
  117. package/src/charts/tick/index.ts +0 -99
  118. package/src/charts/utils.ts +0 -170
  119. package/src/charts/waffle/__tests__/compute.test.ts +0 -305
  120. package/src/charts/waffle/compute.ts +0 -197
  121. package/src/charts/waffle/index.ts +0 -31
  122. package/src/compile/__tests__/color-scale-range.test.ts +0 -158
  123. package/src/compile/__tests__/color-scale-reverse.test.ts +0 -89
  124. package/src/compile/__tests__/data-clip.test.ts +0 -127
  125. package/src/compile/__tests__/fill-patterns.test.ts +0 -154
  126. package/src/compile/__tests__/size-scale.test.ts +0 -108
  127. package/src/compile/__tests__/watermark-obstacle.test.ts +0 -93
  128. package/src/compile/color-scale-range.ts +0 -101
  129. package/src/compile/data-clip.ts +0 -48
  130. package/src/compile/fill-patterns.ts +0 -74
  131. package/src/compile/layer.ts +0 -630
  132. package/src/compile/size-scale.ts +0 -141
  133. package/src/compile/spec-sugar.ts +0 -767
  134. package/src/compile/watermark-obstacle.ts +0 -54
  135. package/src/compile/you-draw-it.ts +0 -123
  136. package/src/compile.ts +0 -1548
  137. package/src/compiler/__tests__/a11y-warnings.test.ts +0 -148
  138. package/src/compiler/__tests__/animation.test.ts +0 -177
  139. package/src/compiler/__tests__/compile.test.ts +0 -87
  140. package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +0 -19
  141. package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +0 -20
  142. package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +0 -22
  143. package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +0 -19
  144. package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +0 -14
  145. package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +0 -34
  146. package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +0 -22
  147. package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +0 -41
  148. package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +0 -38
  149. package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +0 -28
  150. package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +0 -28
  151. package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +0 -30
  152. package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +0 -29
  153. package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +0 -32
  154. package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +0 -25
  155. package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +0 -19
  156. package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +0 -19
  157. package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +0 -21
  158. package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +0 -23
  159. package/src/compiler/__tests__/keys.test.ts +0 -83
  160. package/src/compiler/__tests__/normalize-highlight.test.ts +0 -103
  161. package/src/compiler/__tests__/normalize.test.ts +0 -399
  162. package/src/compiler/__tests__/sparkline-defaults.test.ts +0 -153
  163. package/src/compiler/__tests__/validate-did-you-mean.test.ts +0 -170
  164. package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
  165. package/src/compiler/__tests__/validate-near-miss.test.ts +0 -251
  166. package/src/compiler/__tests__/validate.test.ts +0 -731
  167. package/src/compiler/__tests__/vl-parity.test.ts +0 -68
  168. package/src/compiler/a11y-warnings.ts +0 -110
  169. package/src/compiler/animation.ts +0 -142
  170. package/src/compiler/index.ts +0 -47
  171. package/src/compiler/keys.ts +0 -55
  172. package/src/compiler/normalize.ts +0 -734
  173. package/src/compiler/sparkline-defaults.ts +0 -138
  174. package/src/compiler/types.ts +0 -241
  175. package/src/compiler/validate.ts +0 -2093
  176. package/src/endpoint-labels/__tests__/compute.test.ts +0 -587
  177. package/src/endpoint-labels/compute.ts +0 -621
  178. package/src/endpoint-labels/constants.ts +0 -54
  179. package/src/endpoint-labels/format.ts +0 -60
  180. package/src/endpoint-labels/predict.ts +0 -139
  181. package/src/format/__tests__/field-format.test.ts +0 -164
  182. package/src/format/field-format.ts +0 -27
  183. package/src/geo/__tests__/compile-map.test.ts +0 -1128
  184. package/src/geo/compile-map.ts +0 -1376
  185. package/src/geo/join.ts +0 -115
  186. package/src/geo/projections.ts +0 -65
  187. package/src/geo/types.ts +0 -26
  188. package/src/graphs/__tests__/community.test.ts +0 -228
  189. package/src/graphs/__tests__/compile-graph.test.ts +0 -396
  190. package/src/graphs/__tests__/encoding.test.ts +0 -415
  191. package/src/graphs/community.ts +0 -92
  192. package/src/graphs/compile-graph.ts +0 -329
  193. package/src/graphs/encoding.ts +0 -356
  194. package/src/graphs/types.ts +0 -106
  195. package/src/index.ts +0 -177
  196. package/src/layout/__tests__/metrics.test.ts +0 -97
  197. package/src/layout/__tests__/scales-highlight.test.ts +0 -192
  198. package/src/layout/axes/rotation.ts +0 -152
  199. package/src/layout/axes/thinning.ts +0 -98
  200. package/src/layout/axes/ticks.ts +0 -409
  201. package/src/layout/axes.ts +0 -757
  202. package/src/layout/dimensions.ts +0 -912
  203. package/src/layout/facet.ts +0 -91
  204. package/src/layout/gridlines.ts +0 -84
  205. package/src/layout/metrics.ts +0 -132
  206. package/src/layout/plan.ts +0 -631
  207. package/src/layout/scales.ts +0 -1053
  208. package/src/layout/shared.ts +0 -44
  209. package/src/legend/__tests__/continuous.test.ts +0 -318
  210. package/src/legend/__tests__/redundancy.test.ts +0 -108
  211. package/src/legend/__tests__/size.test.ts +0 -348
  212. package/src/legend/__tests__/suppression.test.ts +0 -382
  213. package/src/legend/compute.ts +0 -748
  214. package/src/legend/continuous.ts +0 -327
  215. package/src/legend/size.ts +0 -407
  216. package/src/legend/suppression.ts +0 -226
  217. package/src/legend/wrap.ts +0 -117
  218. package/src/sankey/__tests__/compile-sankey.test.ts +0 -458
  219. package/src/sankey/__tests__/layout.test.ts +0 -165
  220. package/src/sankey/__tests__/node-label-wrap.test.ts +0 -114
  221. package/src/sankey/__tests__/node-sort.test.ts +0 -45
  222. package/src/sankey/compile-sankey.ts +0 -706
  223. package/src/sankey/layout.ts +0 -184
  224. package/src/sankey/types.ts +0 -41
  225. package/src/tables/__tests__/bar-column.test.ts +0 -147
  226. package/src/tables/__tests__/category-colors.test.ts +0 -230
  227. package/src/tables/__tests__/compile-table.test.ts +0 -236
  228. package/src/tables/__tests__/format-cells.test.ts +0 -143
  229. package/src/tables/__tests__/heatmap.test.ts +0 -148
  230. package/src/tables/__tests__/pagination.test.ts +0 -78
  231. package/src/tables/__tests__/search.test.ts +0 -94
  232. package/src/tables/__tests__/sort.test.ts +0 -107
  233. package/src/tables/__tests__/sparkline.test.ts +0 -122
  234. package/src/tables/bar-column.ts +0 -94
  235. package/src/tables/category-colors.ts +0 -78
  236. package/src/tables/compile-table.ts +0 -427
  237. package/src/tables/format-cells.ts +0 -115
  238. package/src/tables/heatmap.ts +0 -141
  239. package/src/tables/pagination.ts +0 -46
  240. package/src/tables/search.ts +0 -66
  241. package/src/tables/sort.ts +0 -69
  242. package/src/tables/sparkline.ts +0 -113
  243. package/src/tables/utils.ts +0 -16
  244. package/src/tilemap/__tests__/compile-tilemap.test.ts +0 -576
  245. package/src/tilemap/compile-tilemap.ts +0 -755
  246. package/src/tilemap/layout.ts +0 -172
  247. package/src/tilemap/types.ts +0 -33
  248. package/src/tooltips/__tests__/compute.test.ts +0 -582
  249. package/src/tooltips/compute.ts +0 -700
  250. package/src/transforms/__tests__/aggregate.test.ts +0 -159
  251. package/src/transforms/__tests__/bin.test.ts +0 -88
  252. package/src/transforms/__tests__/calculate.test.ts +0 -146
  253. package/src/transforms/__tests__/conditional.test.ts +0 -127
  254. package/src/transforms/__tests__/filter-relative.test.ts +0 -202
  255. package/src/transforms/__tests__/filter.test.ts +0 -59
  256. package/src/transforms/__tests__/fold.test.ts +0 -79
  257. package/src/transforms/__tests__/index.test.ts +0 -93
  258. package/src/transforms/__tests__/predicates.test.ts +0 -193
  259. package/src/transforms/__tests__/timeunit.test.ts +0 -129
  260. package/src/transforms/__tests__/window.test.ts +0 -286
  261. package/src/transforms/aggregate.ts +0 -131
  262. package/src/transforms/bin.ts +0 -87
  263. package/src/transforms/calculate.ts +0 -60
  264. package/src/transforms/conditional.ts +0 -46
  265. package/src/transforms/filter.ts +0 -122
  266. package/src/transforms/fold.ts +0 -49
  267. package/src/transforms/index.ts +0 -60
  268. package/src/transforms/predicates.ts +0 -123
  269. package/src/transforms/timeunit.ts +0 -88
  270. package/src/transforms/window.ts +0 -185
@@ -1,148 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { DEFAULT_DODGE_PADDING, dodgeOffsets } from '../dodge';
3
-
4
- // ---------------------------------------------------------------------------
5
- // Fixtures
6
- // ---------------------------------------------------------------------------
7
-
8
- /** Deterministic pseudo-random sequence (mulberry32) for repeatable fixtures. */
9
- function seededRandom(seed: number): () => number {
10
- let a = seed;
11
- return () => {
12
- a |= 0;
13
- a = (a + 0x6d2b79f5) | 0;
14
- let t = Math.imul(a ^ (a >>> 15), 1 | a);
15
- t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
16
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
17
- };
18
- }
19
-
20
- /** Clustered value-axis positions in [0, 500] that force many collisions. */
21
- function makePositions(count: number, seed = 42): number[] {
22
- const rand = seededRandom(seed);
23
- return Array.from({ length: count }, () => {
24
- // Sum of two draws clusters values around the middle (triangular-ish)
25
- return ((rand() + rand()) / 2) * 500;
26
- });
27
- }
28
-
29
- /** Assert every pair of circles keeps at least (r1 + r2) separation. */
30
- function expectNoOverlap(
31
- positions: readonly number[],
32
- offsets: readonly number[],
33
- radii: readonly number[],
34
- ): void {
35
- const epsilon = 1e-6;
36
- for (let i = 0; i < positions.length; i++) {
37
- for (let j = i + 1; j < positions.length; j++) {
38
- const dx = positions[i] - positions[j];
39
- const dy = offsets[i] - offsets[j];
40
- const dist = Math.sqrt(dx * dx + dy * dy);
41
- expect(dist).toBeGreaterThanOrEqual(radii[i] + radii[j] - epsilon);
42
- }
43
- }
44
- }
45
-
46
- // ---------------------------------------------------------------------------
47
- // Tests
48
- // ---------------------------------------------------------------------------
49
-
50
- describe('dodgeOffsets', () => {
51
- it('leaves a single dot at offset 0', () => {
52
- expect(dodgeOffsets([100], [4])).toEqual([0]);
53
- });
54
-
55
- it('leaves non-colliding dots at offset 0', () => {
56
- const offsets = dodgeOffsets([0, 100, 200], [4, 4, 4]);
57
- expect(offsets).toEqual([0, 0, 0]);
58
- });
59
-
60
- it('produces zero overlapping circles at 300 clustered points', () => {
61
- const positions = makePositions(300);
62
- const radii = positions.map(() => 4);
63
- const offsets = dodgeOffsets(positions, radii);
64
- expectNoOverlap(positions, offsets, radii);
65
- });
66
-
67
- it('respects per-dot radii (sized dots stay collision-free)', () => {
68
- const positions = makePositions(150, 7);
69
- const rand = seededRandom(99);
70
- const radii = positions.map(() => 2 + rand() * 8);
71
- const offsets = dodgeOffsets(positions, radii);
72
- expectNoOverlap(positions, offsets, radii);
73
- });
74
-
75
- it('is deterministic: same input yields identical output', () => {
76
- const positions = makePositions(200, 3);
77
- const radii = positions.map(() => 4);
78
- const first = dodgeOffsets(positions, radii);
79
- const second = dodgeOffsets(positions, radii);
80
- expect(second).toEqual(first);
81
- });
82
-
83
- it('keeps the first placed dot on the lane center and breaks symmetric ties to the negative side', () => {
84
- // Two dots at the same position: the first (by index) stays at 0, the
85
- // second must move; both interval edges have equal magnitude, so the
86
- // documented tie-break picks the negative side.
87
- const offsets = dodgeOffsets([100, 100], [4, 4]);
88
- expect(offsets[0]).toBe(0);
89
- expect(offsets[1]).toBeLessThan(0);
90
- expect(Math.abs(offsets[1])).toBeCloseTo(8 + DEFAULT_DODGE_PADDING, 6);
91
- });
92
-
93
- it('places dots at the minimal offset avoiding collisions', () => {
94
- // Three dots at one position: center, one side, other side; the third
95
- // dot must not leapfrog to a farther slot.
96
- const offsets = dodgeOffsets([50, 50, 50], [4, 4, 4]);
97
- expect(offsets[0]).toBe(0);
98
- const sep = 8 + DEFAULT_DODGE_PADDING;
99
- expect(Math.abs(offsets[1])).toBeCloseTo(sep, 6);
100
- expect(Math.abs(offsets[2])).toBeCloseTo(sep, 6);
101
- expect(Math.sign(offsets[1])).not.toBe(Math.sign(offsets[2]));
102
- });
103
-
104
- it('maps offsets back to input order, not sweep order', () => {
105
- // Descending input positions: the sweep runs ascending, but each offset
106
- // must land on its own input index.
107
- const positions = [300, 100, 200];
108
- const offsets = dodgeOffsets(positions, [4, 4, 4]);
109
- expect(offsets).toEqual([0, 0, 0]);
110
- });
111
-
112
- it('dodges only within a lane', () => {
113
- // Same position, different lanes: no collision, both stay centered.
114
- const separate = dodgeOffsets([100, 100], [4, 4], ['a', 'b']);
115
- expect(separate).toEqual([0, 0]);
116
-
117
- // Same lane collides as usual.
118
- const together = dodgeOffsets([100, 100], [4, 4], ['a', 'a']);
119
- expect(together[1]).not.toBe(0);
120
- });
121
-
122
- it('keeps lanes independent and collision-free in mixed input', () => {
123
- const rand = seededRandom(11);
124
- const lanesPool = ['a', 'b', 'c', 'd'];
125
- const positions = makePositions(200, 5);
126
- const radii = positions.map(() => 4);
127
- const lanes = positions.map(() => lanesPool[Math.floor(rand() * lanesPool.length)]);
128
- const offsets = dodgeOffsets(positions, radii, lanes);
129
-
130
- for (const lane of lanesPool) {
131
- const idx = positions.map((_, i) => i).filter((i) => lanes[i] === lane);
132
- expectNoOverlap(
133
- idx.map((i) => positions[i]),
134
- idx.map((i) => offsets[i]),
135
- idx.map((i) => radii[i]),
136
- );
137
- }
138
- });
139
-
140
- it('honors a custom padding', () => {
141
- const offsets = dodgeOffsets([100, 100], [4, 4], undefined, 5);
142
- expect(Math.abs(offsets[1])).toBeCloseTo(13, 6);
143
- });
144
-
145
- it('returns an empty array for empty input', () => {
146
- expect(dodgeOffsets([], [])).toEqual([]);
147
- });
148
- });
@@ -1,203 +0,0 @@
1
- /**
2
- * Beeswarm chart mark computation.
3
- *
4
- * One quantitative value axis positions every observation; the deterministic
5
- * dodge layout (see ./dodge) spreads colliding dots along the cross axis so
6
- * each dot stays visible. An optional nominal channel on the other axis
7
- * groups the dots into lanes (one swarm per category via a band scale).
8
- * The cross axis has no scale: offsets are pure pixel-space.
9
- *
10
- * Orientation follows the bar convention: quantitative x = horizontal swarm,
11
- * quantitative y = vertical swarm.
12
- */
13
-
14
- import type {
15
- Encoding,
16
- GradientDef,
17
- LayoutStrategy,
18
- MarkAria,
19
- PointMark,
20
- Rect,
21
- } from '@opendata-ai/openchart-core';
22
- import type { ScaleBand, ScaleLinear } from 'd3-scale';
23
- import { buildSizeScale, SIZE_SCALE_DEFAULTS } from '../../compile/size-scale';
24
- import { dedupeKeys, serializeKeyValue } from '../../compiler/keys';
25
- import type { NormalizedChartSpec } from '../../compiler/types';
26
- import type { ResolvedScales } from '../../layout/scales';
27
- import { getColor, getSequentialColor } from '../utils';
28
- import { dodgeOffsets } from './dodge';
29
-
30
- // ---------------------------------------------------------------------------
31
- // Constants
32
- // ---------------------------------------------------------------------------
33
-
34
- /**
35
- * Default dot radius (px). Smaller than the scatter default (5) because
36
- * beeswarms carry hundreds of dots and the dodge layout trades radius for
37
- * swarm height: at r=4 a 300-dot swarm stays inside a typical chart area.
38
- */
39
- const DEFAULT_DOT_RADIUS = 4;
40
-
41
- // ---------------------------------------------------------------------------
42
- // Public API
43
- // ---------------------------------------------------------------------------
44
-
45
- /**
46
- * Compute beeswarm marks from a normalized chart spec.
47
- *
48
- * The quantitative channel is the value axis (x = horizontal swarm,
49
- * y = vertical). The opposite channel, when present, must be nominal/ordinal
50
- * and lanes the dots via its band scale; otherwise all dots share one lane
51
- * centered on the cross axis. Radii come from the optional `size` encoding
52
- * (sqrt scale, area-proportional) or the default dot radius; the dodge
53
- * layout consumes per-dot radii so sized swarms stay collision-free.
54
- */
55
- export function computeBeeswarmMarks(
56
- spec: NormalizedChartSpec,
57
- scales: ResolvedScales,
58
- chartArea: Rect,
59
- _strategy: LayoutStrategy,
60
- ): PointMark[] {
61
- const encoding = spec.encoding as Encoding;
62
-
63
- // Orientation: prefer a quantitative x (horizontal swarm), else quantitative y.
64
- const isHorizontal = encoding.x?.type === 'quantitative';
65
- const valueChannel = isHorizontal ? encoding.x : encoding.y;
66
- const laneChannel = isHorizontal ? encoding.y : encoding.x;
67
- const valueScale = isHorizontal ? scales.x : scales.y;
68
- const laneScale = isHorizontal ? scales.y : scales.x;
69
-
70
- if (!valueChannel || valueChannel.type !== 'quantitative' || !valueScale) {
71
- return [];
72
- }
73
-
74
- const positionScale = valueScale.scale as ScaleLinear<number, number>;
75
-
76
- // Lane centers: band scale when the nominal channel is present, else the
77
- // middle of the cross axis. The cross axis itself has no scale: dodge
78
- // offsets are pixel-space around each lane center.
79
- const bandScale =
80
- laneScale && laneScale.type === 'band' ? (laneScale.scale as ScaleBand<string>) : undefined;
81
- const defaultLaneCenter = isHorizontal
82
- ? chartArea.y + chartArea.height / 2
83
- : chartArea.x + chartArea.width / 2;
84
-
85
- const colorEnc = encoding.color && 'field' in encoding.color ? encoding.color : undefined;
86
- const isSequentialColor = colorEnc?.type === 'quantitative';
87
- const colorField = colorEnc?.field;
88
- const sizeEnc = encoding.size && 'field' in encoding.size ? encoding.size : undefined;
89
- const sizeField = sizeEnc?.field;
90
-
91
- // Sized dots: sqrt (area-proportional), same override surface as scatter.
92
- // Shared builder so the marks and any size legend resolve one scale.
93
- const resolvedSize = buildSizeScale(sizeEnc, spec.data, SIZE_SCALE_DEFAULTS.beeswarm);
94
- const sizeScale = resolvedSize?.scale;
95
-
96
- // Resolve every renderable dot first: dodge needs the full position/radius
97
- // arrays before any mark can be placed.
98
- interface ResolvedDot {
99
- row: Record<string, unknown>;
100
- value: number;
101
- position: number;
102
- radius: number;
103
- lane: string;
104
- laneCenter: number;
105
- }
106
- const dots: ResolvedDot[] = [];
107
-
108
- for (const row of spec.data) {
109
- const raw = row[valueChannel.field];
110
- // Drop null/undefined explicitly: the validator tolerates them (it only
111
- // rejects non-numeric or non-finite present values), but Number(null) is 0,
112
- // which would silently place a phantom dot at value 0 instead of skipping.
113
- if (raw == null) continue;
114
- const value = Number(raw);
115
- if (!Number.isFinite(value)) continue;
116
-
117
- const position = positionScale(value);
118
- if (!Number.isFinite(position)) continue;
119
-
120
- let lane = '__default__';
121
- let laneCenter = defaultLaneCenter;
122
- if (bandScale && laneChannel) {
123
- lane = String(row[laneChannel.field] ?? '');
124
- const bandStart = bandScale(lane);
125
- if (bandStart === undefined) continue;
126
- laneCenter = bandStart + bandScale.bandwidth() / 2;
127
- }
128
-
129
- let radius = DEFAULT_DOT_RADIUS;
130
- if (sizeScale && sizeField) {
131
- const sizeVal = Number(row[sizeField]);
132
- if (Number.isFinite(sizeVal)) {
133
- radius = sizeScale(sizeVal);
134
- }
135
- }
136
-
137
- dots.push({ row: row as Record<string, unknown>, value, position, radius, lane, laneCenter });
138
- }
139
-
140
- const offsets = dodgeOffsets(
141
- dots.map((d) => d.position),
142
- dots.map((d) => d.radius),
143
- dots.map((d) => d.lane),
144
- );
145
-
146
- const keyEnc = encoding.key && 'field' in encoding.key ? encoding.key : undefined;
147
- const keyField = keyEnc?.field;
148
- const marks: PointMark[] = [];
149
-
150
- for (let i = 0; i < dots.length; i++) {
151
- const dot = dots[i];
152
- const cross = dot.laneCenter + offsets[i];
153
-
154
- let color: string | GradientDef;
155
- if (isSequentialColor && colorField) {
156
- const colorVal = Number(dot.row[colorField]);
157
- color = Number.isFinite(colorVal)
158
- ? getSequentialColor(scales, colorVal)
159
- : getColor(scales, '__default__');
160
- } else if (colorField) {
161
- color = getColor(scales, String(dot.row[colorField] ?? '__default__'));
162
- } else {
163
- color = getColor(scales, '__default__');
164
- }
165
-
166
- const labelParts = [`${valueChannel.field}=${dot.row[valueChannel.field]}`];
167
- if (laneChannel && dot.lane !== '__default__') {
168
- labelParts.push(`${laneChannel.field}=${dot.lane}`);
169
- }
170
- if (sizeField && dot.row[sizeField] != null) {
171
- labelParts.push(`${sizeField}=${dot.row[sizeField]}`);
172
- }
173
-
174
- const aria: MarkAria = {
175
- label: `Data point: ${labelParts.join(', ')}`,
176
- };
177
-
178
- marks.push({
179
- type: 'point',
180
- cx: isHorizontal ? dot.position : cross,
181
- cy: isHorizontal ? cross : dot.position,
182
- r: dot.radius,
183
- fill: color,
184
- stroke: '#ffffff',
185
- strokeWidth: 1,
186
- data: dot.row,
187
- aria,
188
- });
189
- }
190
-
191
- // Stamp keys: encoding.key field when set, else lane|value composite
192
- const rawKeys = marks.map((m, i) => {
193
- if (keyField) return serializeKeyValue(m.data[keyField]);
194
- const valueKey = serializeKeyValue(m.data[valueChannel.field]);
195
- return dots[i].lane !== '__default__' ? `${dots[i].lane}|${valueKey}` : valueKey;
196
- });
197
- const keys = dedupeKeys(rawKeys);
198
- for (let i = 0; i < marks.length; i++) {
199
- marks[i].key = keys[i];
200
- }
201
-
202
- return marks;
203
- }
@@ -1,155 +0,0 @@
1
- /**
2
- * Deterministic sweep-line dodge for beeswarm layouts.
3
- *
4
- * Port of Observable Plot's dodge transform (anchor "middle"): dots are
5
- * placed in value order, each at the smallest signed cross-axis offset that
6
- * avoids every previously placed circle. Pure array math with stable sorts
7
- * and explicit tie-breaking, so the same input always produces the same
8
- * output: no force simulation, no iteration tuning, no animation frames.
9
- */
10
-
11
- // ---------------------------------------------------------------------------
12
- // Constants
13
- // ---------------------------------------------------------------------------
14
-
15
- /** Default extra gap (px) enforced between circle edges. */
16
- export const DEFAULT_DODGE_PADDING = 1;
17
-
18
- // ---------------------------------------------------------------------------
19
- // Public API
20
- // ---------------------------------------------------------------------------
21
-
22
- /**
23
- * Compute cross-axis offsets that resolve circle collisions along a value axis.
24
- *
25
- * Dots are processed in ascending value-axis order (ties broken by input
26
- * index, so the layout is deterministic). For each dot, every already-placed
27
- * dot that overlaps it horizontally contributes a blocked interval of
28
- * cross-axis offsets; the dot takes the smallest-magnitude offset outside all
29
- * blocked intervals. Candidates are 0 and the blocked-interval edges; when a
30
- * positive and a negative candidate have equal magnitude, the negative side
31
- * wins (up in a horizontal swarm, left in a vertical one).
32
- *
33
- * When `lanes` is provided, dots dodge only against dots in the same lane
34
- * (one independent swarm per category).
35
- *
36
- * @param positions - Value-axis pixel positions, one per dot.
37
- * @param radii - Per-dot radii in pixels, parallel to `positions`.
38
- * @param lanes - Optional lane key per dot; dots in different lanes never collide.
39
- * @param padding - Extra pixel gap between circle edges. Defaults to 1.
40
- * @returns Signed cross-axis pixel offsets (centered on 0), parallel to `positions`.
41
- */
42
- export function dodgeOffsets(
43
- positions: readonly number[],
44
- radii: readonly number[],
45
- lanes?: readonly string[],
46
- padding: number = DEFAULT_DODGE_PADDING,
47
- ): number[] {
48
- const n = positions.length;
49
- const offsets = new Array<number>(n).fill(0);
50
-
51
- // Group indices by lane (single implicit lane when none provided)
52
- const groups = new Map<string, number[]>();
53
- for (let i = 0; i < n; i++) {
54
- const lane = lanes?.[i] ?? '__default__';
55
- const group = groups.get(lane);
56
- if (group) {
57
- group.push(i);
58
- } else {
59
- groups.set(lane, [i]);
60
- }
61
- }
62
-
63
- for (const group of groups.values()) {
64
- dodgeLane(group, positions, radii, padding, offsets);
65
- }
66
-
67
- return offsets;
68
- }
69
-
70
- // ---------------------------------------------------------------------------
71
- // Per-lane sweep
72
- // ---------------------------------------------------------------------------
73
-
74
- /**
75
- * Place one lane's dots, writing results into the shared offsets array.
76
- *
77
- * The backward scan's early `break` (line ~101) keeps this near-linear when
78
- * value positions are spread out: once the horizontal gap to an earlier dot
79
- * exceeds any possible collision distance, no earlier dot can collide either.
80
- * The degenerate case is many dots sharing one identical value position — the
81
- * gap stays 0, the break never fires, and the scan touches every placed dot,
82
- * so a lane of N coincident points is O(N^2). The overplotting guard caps the
83
- * swarm at ~2000 points, which bounds the worst case; real data spreads out
84
- * enough that this stays linear in practice.
85
- */
86
- function dodgeLane(
87
- indices: number[],
88
- positions: readonly number[],
89
- radii: readonly number[],
90
- padding: number,
91
- offsets: number[],
92
- ): void {
93
- // Sweep order: ascending value position, ties broken by input index so the
94
- // sort (and therefore the layout) is fully deterministic.
95
- const order = [...indices].sort((a, b) => positions[a] - positions[b] || a - b);
96
-
97
- // Already-placed dots in sweep order. Because placement follows ascending
98
- // positions, the backward scan below can stop as soon as the horizontal gap
99
- // exceeds the largest possible collision distance.
100
- const placed: number[] = [];
101
- let maxPlacedRadius = 0;
102
-
103
- for (const i of order) {
104
- const p = positions[i];
105
- const r = radii[i];
106
-
107
- // Blocked cross-axis intervals from placed dots that overlap horizontally.
108
- const intervals: Array<[number, number]> = [];
109
- for (let k = placed.length - 1; k >= 0; k--) {
110
- const j = placed[k];
111
- const dx = p - positions[j];
112
- if (dx > r + maxPlacedRadius + padding) break; // no earlier dot can reach
113
- const sep = r + radii[j] + padding;
114
- if (dx * dx >= sep * sep) continue;
115
- const dy = Math.sqrt(sep * sep - dx * dx);
116
- intervals.push([offsets[j] - dy, offsets[j] + dy]);
117
- }
118
-
119
- offsets[i] = minimalFreeOffset(intervals);
120
- placed.push(i);
121
- if (r > maxPlacedRadius) maxPlacedRadius = r;
122
- }
123
- }
124
-
125
- /**
126
- * Smallest-magnitude offset outside all blocked open intervals.
127
- *
128
- * The feasible set is the complement of a union of open intervals, so the
129
- * minimal-|offset| point is either 0 or an interval edge. Candidates sort by
130
- * (|value|, value): equal magnitudes resolve to the negative side.
131
- */
132
- function minimalFreeOffset(intervals: Array<[number, number]>): number {
133
- if (intervals.length === 0) return 0;
134
-
135
- const candidates: number[] = [0];
136
- for (const [lo, hi] of intervals) {
137
- candidates.push(lo, hi);
138
- }
139
- candidates.sort((a, b) => Math.abs(a) - Math.abs(b) || a - b);
140
-
141
- for (const candidate of candidates) {
142
- let blocked = false;
143
- for (const [lo, hi] of intervals) {
144
- if (candidate > lo && candidate < hi) {
145
- blocked = true;
146
- break;
147
- }
148
- }
149
- if (!blocked) return candidate;
150
- }
151
-
152
- // Unreachable: interval edges are never strictly inside their own interval,
153
- // so at least one candidate is always free. Kept for type safety.
154
- return 0;
155
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * Beeswarm chart module.
3
- *
4
- * Exports the beeswarm renderer and computation functions. Beeswarms emit
5
- * plain PointMark[] (rendered by the existing point mark path) with no
6
- * per-dot value labels: a swarm carries hundreds of observations, so labels
7
- * would only recreate the overplotting the layout exists to solve.
8
- */
9
-
10
- import type { Mark } from '@opendata-ai/openchart-core';
11
- import type { ChartRenderer } from '../registry';
12
- import { computeBeeswarmMarks } from './compute';
13
-
14
- // ---------------------------------------------------------------------------
15
- // Beeswarm chart renderer
16
- // ---------------------------------------------------------------------------
17
-
18
- /**
19
- * Beeswarm chart renderer.
20
- *
21
- * Produces one dodged PointMark per observation.
22
- */
23
- export const beeswarmRenderer: ChartRenderer = (spec, scales, chartArea, strategy, _theme) => {
24
- return computeBeeswarmMarks(spec, scales, chartArea, strategy) as Mark[];
25
- };
26
-
27
- // ---------------------------------------------------------------------------
28
- // Public exports
29
- // ---------------------------------------------------------------------------
30
-
31
- export { computeBeeswarmMarks } from './compute';
32
- export { DEFAULT_DODGE_PADDING, dodgeOffsets } from './dodge';
@@ -1,75 +0,0 @@
1
- /**
2
- * Built-in chart renderer registration.
3
- *
4
- * Each chart type (line, bar, column, scatter, pie, etc.) has a renderer that
5
- * produces marks from a normalized spec. This module wires them into the
6
- * registry. Importing it is a side effect: the built-ins register as soon as
7
- * it's loaded.
8
- *
9
- * Tests that call `clearRenderers()` can call `registerBuiltinRenderers()` to
10
- * restore the default set rather than leaving the registry empty for later
11
- * tests in the same process.
12
- */
13
-
14
- import { barRenderer } from './bar';
15
- import { beeswarmRenderer } from './beeswarm';
16
- import { calendarRenderer } from './calendar';
17
- import { columnRenderer } from './column';
18
- import { dotRenderer } from './dot';
19
- import { areaRenderer, lineRenderer } from './line';
20
- import { parliamentRenderer } from './parliament';
21
- import { donutRenderer, pieRenderer } from './pie';
22
- import { rangeRenderer } from './range';
23
- import { rectRenderer } from './rect';
24
- import { type ChartRenderer, registerChartRenderer } from './registry';
25
- import { ruleRenderer } from './rule';
26
- import { scatterRenderer } from './scatter';
27
- import { textRenderer } from './text';
28
- import { tickRenderer } from './tick';
29
- import { waffleRenderer } from './waffle';
30
-
31
- // Mark type mapping from old chart types:
32
- // - 'bar' -> barRenderer (horizontal bars, old 'bar')
33
- // - 'bar:vertical' is handled by columnRenderer (old 'column')
34
- // - 'arc' -> pieRenderer (old 'pie'); donutRenderer is also registered
35
- // - 'point' -> scatterRenderer (old 'scatter')
36
- // - 'circle' -> dotRenderer (old 'dot')
37
- // - 'line' and 'area' unchanged
38
- // - 'text', 'rule', 'tick' are new Vega-Lite mark types
39
- //
40
- // For 'bar', orientation is resolved at compile time to dispatch to the right
41
- // renderer. We register both barRenderer and columnRenderer; the compile
42
- // function picks based on orientation.
43
- const builtinRenderers: Record<string, ChartRenderer> = {
44
- line: lineRenderer,
45
- area: areaRenderer,
46
- bar: barRenderer, // horizontal bars
47
- 'bar:vertical': columnRenderer, // vertical bars (old 'column')
48
- point: scatterRenderer, // old 'scatter'
49
- arc: pieRenderer, // old 'pie' (donut handled via innerRadius)
50
- 'arc:donut': donutRenderer, // old 'donut'
51
- circle: dotRenderer, // old 'dot'
52
- lollipop: dotRenderer, // semantic alias for dot/circle
53
- beeswarm: beeswarmRenderer, // dodged distribution dots
54
- text: textRenderer,
55
- rule: ruleRenderer,
56
- tick: tickRenderer,
57
- rect: rectRenderer, // heatmap cells (band x band); see charts/rect
58
- range: rangeRenderer, // dumbbell / arrow / range bar (orientation resolved internally)
59
- waffle: waffleRenderer, // unit grid for part-to-whole counts
60
- calendar: calendarRenderer, // weeks x weekdays daily heatmap (no positional scales)
61
- parliament: parliamentRenderer, // hemicycle seat dots (no positional scales)
62
- };
63
-
64
- /**
65
- * Register all built-in chart renderers. Called once on module load; also
66
- * exported so tests that clear the registry can restore the defaults.
67
- */
68
- export function registerBuiltinRenderers(): void {
69
- for (const [type, renderer] of Object.entries(builtinRenderers)) {
70
- registerChartRenderer(type, renderer);
71
- }
72
- }
73
-
74
- // Side effect: register on first import.
75
- registerBuiltinRenderers();