@opendata-ai/openchart-engine 8.0.0-rc.8 → 8.0.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.
Files changed (271) hide show
  1. package/dist/index.d.ts +149 -12
  2. package/dist/index.js +894 -171
  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 -852
  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 -376
  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 -545
  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 -141
  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 -170
  165. package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
  166. package/src/compiler/__tests__/validate-near-miss.test.ts +0 -251
  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 -734
  174. package/src/compiler/sparkline-defaults.ts +0 -138
  175. package/src/compiler/types.ts +0 -241
  176. package/src/compiler/validate.ts +0 -2041
  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 -1004
  185. package/src/geo/compile-map.ts +0 -1341
  186. package/src/geo/join.ts +0 -115
  187. package/src/geo/projections.ts +0 -45
  188. package/src/geo/types.ts +0 -26
  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,177 +0,0 @@
1
- /**
2
- * Line chart label computation.
3
- *
4
- * Produces end-of-line labels (series name at the last data point)
5
- * and feeds them through the core collision engine. At compact
6
- * breakpoints, labels are suppressed in favor of the legend.
7
- *
8
- * Respects the spec's label density setting:
9
- * - 'all': show every label, skip collision detection
10
- * - 'auto': existing behavior (collision detection)
11
- * - 'endpoints': first and last data point labels per series
12
- * - 'none': return empty map
13
- */
14
-
15
- import type {
16
- LabelCandidate,
17
- LabelDensity,
18
- LayoutStrategy,
19
- LineMark,
20
- ResolvedLabel,
21
- } from '@opendata-ai/openchart-core';
22
- import {
23
- EXTENDED_OFFSET_STRATEGIES,
24
- estimateTextWidth,
25
- resolveCollisions,
26
- } from '@opendata-ai/openchart-core';
27
-
28
- import type { NormalizedChartSpec } from '../../compiler/types';
29
- import { countColorSeries, resolveSuppression } from '../../legend/suppression';
30
-
31
- // ---------------------------------------------------------------------------
32
- // Constants
33
- // ---------------------------------------------------------------------------
34
-
35
- /** Default label font size. */
36
- const LABEL_FONT_SIZE = 11;
37
-
38
- /** Default label font weight. */
39
- const LABEL_FONT_WEIGHT = 600;
40
-
41
- /** Horizontal offset from last point to label. */
42
- const LABEL_OFFSET_X = 6;
43
-
44
- // ---------------------------------------------------------------------------
45
- // Public API
46
- // ---------------------------------------------------------------------------
47
-
48
- /**
49
- * Compute end-of-line labels for line marks.
50
- *
51
- * For each series, places a label at the position of the last data point.
52
- * At compact breakpoints (labelMode === 'none'), all labels are hidden
53
- * so the legend takes over. Labels go through collision detection to
54
- * avoid overlap.
55
- *
56
- * Returns a Map keyed by seriesKey so callers can look up labels by
57
- * mark identity instead of relying on positional indices.
58
- *
59
- * @param marks - Line marks (only processes marks with type === 'line').
60
- * @param strategy - Layout strategy from the responsive breakpoint.
61
- * @param density - Label density mode from spec.labels.density.
62
- * @param labelOffsets - Per-series user offsets applied after collision resolution.
63
- * @param spec - Optional normalized spec. When provided, the shared suppression
64
- * truth table gates rendering: end-of-line labels are the last-resort series
65
- * identifier and only render when both the traditional legend and endpoint
66
- * labels column are off.
67
- * @returns Map of seriesKey -> ResolvedLabel after collision detection.
68
- */
69
- export function computeLineLabels(
70
- marks: LineMark[],
71
- strategy: LayoutStrategy,
72
- density: LabelDensity = 'auto',
73
- labelOffsets?: Record<string, { dx?: number; dy?: number }>,
74
- spec?: NormalizedChartSpec,
75
- ): Map<string, ResolvedLabel> {
76
- const result = new Map<string, ResolvedLabel>();
77
-
78
- // 'none': no labels
79
- if (density === 'none') return result;
80
-
81
- // At compact breakpoint, suppress inline labels entirely
82
- if (strategy.labelMode === 'none') {
83
- return result;
84
- }
85
-
86
- // Suppression truth table: when the spec is available, defer to the shared
87
- // helper. End-of-line labels render only when both the legend and endpoint
88
- // column are off — they're the last-resort series identifier.
89
- if (spec) {
90
- // The 'none' branches above already returned, so by here labelMode and
91
- // density are not 'none'. Pass false explicitly to keep the helper pure.
92
- const suppression = resolveSuppression(spec, {
93
- seriesCount: countColorSeries(spec),
94
- labelsHiddenByStrategy: false,
95
- labelsDensityNone: false,
96
- });
97
- if (!suppression.showEndOfLineLabels) {
98
- // Single-series charts have no series-name end-of-line label to hide
99
- // (computeLineLabels returns nothing when seriesKey is empty). For
100
- // multi-series, the suppression result tells us to skip explicitly.
101
- const isMultiSeries = !!spec.encoding.color && countColorSeries(spec) >= 2;
102
- if (isMultiSeries) return result;
103
- }
104
- }
105
-
106
- const candidates: LabelCandidate[] = [];
107
- const seriesOrder: string[] = [];
108
-
109
- for (const mark of marks) {
110
- if (mark.points.length === 0) continue;
111
-
112
- const labelText = mark.seriesKey ?? '';
113
- if (!labelText) continue;
114
-
115
- const lastPoint = mark.points[mark.points.length - 1];
116
- const textWidth = estimateTextWidth(labelText, LABEL_FONT_SIZE, LABEL_FONT_WEIGHT);
117
- const textHeight = LABEL_FONT_SIZE * 1.2;
118
-
119
- candidates.push({
120
- text: labelText,
121
- anchorX: lastPoint.x + LABEL_OFFSET_X,
122
- anchorY: lastPoint.y - textHeight / 2,
123
- width: textWidth,
124
- height: textHeight,
125
- priority: 'data',
126
- style: {
127
- fontFamily: 'system-ui, -apple-system, sans-serif',
128
- fontSize: LABEL_FONT_SIZE,
129
- fontWeight: LABEL_FONT_WEIGHT,
130
- fill: mark.stroke,
131
- lineHeight: 1.2,
132
- textAnchor: 'start',
133
- dominantBaseline: 'central',
134
- },
135
- });
136
-
137
- seriesOrder.push(labelText);
138
- }
139
-
140
- if (candidates.length === 0) return result;
141
-
142
- // 'all': skip collision detection, mark everything visible
143
- if (density === 'all') {
144
- for (let i = 0; i < candidates.length; i++) {
145
- const c = candidates[i];
146
- const seriesKey = seriesOrder[i];
147
- const userOffset = labelOffsets?.[seriesKey];
148
- result.set(seriesKey, {
149
- text: c.text,
150
- x: c.anchorX + (userOffset?.dx ?? 0),
151
- y: c.anchorY + (userOffset?.dy ?? 0),
152
- style: c.style,
153
- visible: true,
154
- });
155
- }
156
- return result;
157
- }
158
-
159
- // 'endpoints': for line charts, endpoints means showing just the end-of-line
160
- // label (which is what we already compute). This is the same as 'auto' for lines
161
- // since we only compute the endpoint label per series.
162
-
163
- const resolved = resolveCollisions(candidates, EXTENDED_OFFSET_STRATEGIES);
164
- for (let i = 0; i < resolved.length; i++) {
165
- const seriesKey = seriesOrder[i];
166
- const label = resolved[i];
167
- // Apply user-provided per-series label offset after collision resolution
168
- const userOffset = labelOffsets?.[seriesKey];
169
- if (userOffset) {
170
- label.x += userOffset.dx ?? 0;
171
- label.y += userOffset.dy ?? 0;
172
- }
173
- result.set(seriesKey, label);
174
- }
175
-
176
- return result;
177
- }
@@ -1,411 +0,0 @@
1
- import type {
2
- ChartLayout,
3
- PointMark,
4
- RuleMarkLayout,
5
- TextMarkLayout,
6
- } from '@opendata-ai/openchart-core';
7
- import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
8
-
9
- import { compileChart } from '../../../compile';
10
-
11
- // ---------------------------------------------------------------------------
12
- // Shared fixtures
13
- // ---------------------------------------------------------------------------
14
-
15
- /** US House 2023: 213 Democrats, 222 Republicans = 435 seats, majority 218. */
16
- const usHouse = [
17
- { party: 'Democratic', seats: 213 },
18
- { party: 'Republican', seats: 222 },
19
- ];
20
-
21
- const partyColors = { range: ['#1b7fa3', '#c44e52'] };
22
-
23
- function compileHouse(overrides: Record<string, unknown> = {}): ChartLayout {
24
- return compileChart(
25
- {
26
- mark: 'parliament',
27
- data: usHouse,
28
- encoding: {
29
- theta: { field: 'seats', type: 'quantitative' },
30
- color: { field: 'party', type: 'nominal', scale: partyColors },
31
- },
32
- ...overrides,
33
- },
34
- { width: 640, height: 400 },
35
- );
36
- }
37
-
38
- function seatMarks(layout: ChartLayout): PointMark[] {
39
- return layout.marks.filter((m): m is PointMark => m.type === 'point');
40
- }
41
-
42
- // ---------------------------------------------------------------------------
43
- // Seat counts (acceptance: rendered dots per party == data)
44
- // ---------------------------------------------------------------------------
45
-
46
- describe('parliament seat counts', () => {
47
- it('renders exactly one seat dot per seat in the data', () => {
48
- const layout = compileHouse();
49
- expect(seatMarks(layout)).toHaveLength(435);
50
- });
51
-
52
- it('renders the correct number of seats per party', () => {
53
- const layout = compileHouse();
54
- const byParty = new Map<string, number>();
55
- for (const seat of seatMarks(layout)) {
56
- const party = String(seat.data.party);
57
- byParty.set(party, (byParty.get(party) ?? 0) + 1);
58
- }
59
- expect(byParty.get('Democratic')).toBe(213);
60
- expect(byParty.get('Republican')).toBe(222);
61
- });
62
-
63
- it('rounds fractional seat values to whole seats', () => {
64
- const layout = compileChart(
65
- {
66
- mark: 'parliament',
67
- data: [
68
- { party: 'A', seats: 10.4 },
69
- { party: 'B', seats: 5.6 },
70
- ],
71
- encoding: {
72
- theta: { field: 'seats', type: 'quantitative' },
73
- color: { field: 'party', type: 'nominal', scale: { range: ['#111', '#222'] } },
74
- },
75
- },
76
- { width: 640, height: 400 },
77
- );
78
- // 10.4 -> 10, 5.6 -> 6 = 16 seats total.
79
- expect(seatMarks(layout)).toHaveLength(16);
80
- });
81
-
82
- it('assigns each party its scale color to every one of its seats', () => {
83
- const layout = compileHouse();
84
- const dem = seatMarks(layout).filter((s) => s.data.party === 'Democratic');
85
- const gop = seatMarks(layout).filter((s) => s.data.party === 'Republican');
86
- expect(dem.every((s) => s.fill === '#1b7fa3')).toBe(true);
87
- expect(gop.every((s) => s.fill === '#c44e52')).toBe(true);
88
- });
89
-
90
- it('groups each party into a contiguous left-to-right block', () => {
91
- const layout = compileHouse();
92
- // Seat marks are emitted party-by-party in data order, so the first 213
93
- // are Democratic and the rest Republican.
94
- const parties = seatMarks(layout).map((s) => String(s.data.party));
95
- const firstGop = parties.indexOf('Republican');
96
- expect(firstGop).toBe(213);
97
- expect(parties.slice(0, 213).every((p) => p === 'Democratic')).toBe(true);
98
- expect(parties.slice(213).every((p) => p === 'Republican')).toBe(true);
99
- });
100
- });
101
-
102
- // ---------------------------------------------------------------------------
103
- // Hemicycle geometry
104
- // ---------------------------------------------------------------------------
105
-
106
- describe('parliament geometry', () => {
107
- it('keeps every seat dot inside the chart bounds', () => {
108
- const layout = compileHouse();
109
- const svgW = layout.dimensions.width;
110
- const svgH = layout.dimensions.height;
111
- for (const seat of seatMarks(layout)) {
112
- expect(seat.cx - seat.r).toBeGreaterThanOrEqual(0);
113
- expect(seat.cx + seat.r).toBeLessThanOrEqual(svgW);
114
- expect(seat.cy - seat.r).toBeGreaterThanOrEqual(0);
115
- expect(seat.cy + seat.r).toBeLessThanOrEqual(svgH);
116
- }
117
- });
118
-
119
- it('arranges seats in a hemicycle (dots span a wide arc, not a single column)', () => {
120
- const layout = compileHouse();
121
- const xs = seatMarks(layout).map((s) => s.cx);
122
- const spread = Math.max(...xs) - Math.min(...xs);
123
- // A real hemicycle spans most of the width; a degenerate column would be ~0.
124
- expect(spread).toBeGreaterThan(layout.dimensions.width / 2);
125
- });
126
-
127
- it('gives every seat a positive radius', () => {
128
- const layout = compileHouse();
129
- expect(seatMarks(layout).every((s) => s.r > 0)).toBe(true);
130
- });
131
-
132
- it('assigns sequential animation indices for the entrance stagger', () => {
133
- const layout = compileHouse();
134
- const indices = seatMarks(layout).map((s) => s.animationIndex);
135
- expect(indices[0]).toBe(0);
136
- expect(indices[indices.length - 1]).toBe(434);
137
- });
138
- });
139
-
140
- // ---------------------------------------------------------------------------
141
- // Majority line
142
- // ---------------------------------------------------------------------------
143
-
144
- describe('parliament majority line', () => {
145
- it('draws a majority rule and a "218 to win" label by default', () => {
146
- const layout = compileHouse();
147
- const rule = layout.marks.find((m): m is RuleMarkLayout => m.type === 'rule');
148
- const label = layout.marks.find((m): m is TextMarkLayout => m.type === 'textMark');
149
- expect(rule).toBeDefined();
150
- expect(label).toBeDefined();
151
- expect(label?.text).toBe('218 to win');
152
- // The label sits at `lineTop - 6`, which assumes the SVG default (alphabetic)
153
- // baseline. Stamping a dominantBaseline would shift it off the rule.
154
- expect(label?.dominantBaseline).toBeUndefined();
155
- });
156
-
157
- it('omits the majority line when majorityLine is false', () => {
158
- const layout = compileHouse({ mark: { type: 'parliament', majorityLine: false } });
159
- expect(layout.marks.some((m) => m.type === 'rule')).toBe(false);
160
- expect(layout.marks.some((m) => m.type === 'textMark')).toBe(false);
161
- });
162
-
163
- it('honors an explicit majority seat count and label override', () => {
164
- const layout = compileHouse({
165
- mark: { type: 'parliament', majorityLine: { seats: 300, label: 'Supermajority' } },
166
- });
167
- const label = layout.marks.find((m): m is TextMarkLayout => m.type === 'textMark');
168
- expect(label?.text).toBe('Supermajority');
169
- });
170
-
171
- it('accepts the object majorityLine form through the typed spec API', () => {
172
- // Authored as a fully-typed ChartSpec (not the Record<string, unknown>
173
- // overrides shim), so this covers the public type surface: MarkDef.
174
- // majorityLine must accept { seats, label }, not just boolean.
175
- const layout = compileChart(
176
- {
177
- mark: { type: 'parliament', majorityLine: { seats: 290 } },
178
- data: usHouse,
179
- encoding: {
180
- theta: { field: 'seats', type: 'quantitative' },
181
- color: { field: 'party', type: 'nominal', scale: partyColors },
182
- },
183
- },
184
- { width: 640, height: 400 },
185
- );
186
- const label = layout.marks.find((m): m is TextMarkLayout => m.type === 'textMark');
187
- expect(label?.text).toBe('290 to win');
188
- });
189
-
190
- describe('out-of-range majorityLine.seats', () => {
191
- let warnSpy: ReturnType<typeof vi.spyOn>;
192
- beforeEach(() => {
193
- warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
194
- });
195
- afterEach(() => {
196
- warnSpy.mockRestore();
197
- });
198
- const warned = (): string[] => warnSpy.mock.calls.map((c) => String(c[0]));
199
-
200
- it('warns and falls back to the default when seats is above the chamber', () => {
201
- // 435-seat house; 999 would draw the marker off the seat arc. The mark
202
- // falls back to the simple majority (218) and a warning surfaces through
203
- // the shared compile warnings channel (emitted once, deduped).
204
- const layout = compileHouse({
205
- mark: { type: 'parliament', majorityLine: { seats: 999 } },
206
- });
207
- const warning = warned().find((w) => w.includes('majorityLine.seats'));
208
- expect(warning).toBeDefined();
209
- expect(warning).toContain('999');
210
- expect(warning).toContain('1..435');
211
- expect(warning).toContain('218');
212
-
213
- // The rendered label uses the fallback, not the out-of-range value.
214
- const label = layout.marks.find((m): m is TextMarkLayout => m.type === 'textMark');
215
- expect(label?.text).toBe('218 to win');
216
- });
217
-
218
- it('warns for seats below 1 (e.g. { seats: 0 })', () => {
219
- compileHouse({ mark: { type: 'parliament', majorityLine: { seats: 0 } } });
220
- expect(warned().some((w) => w.includes('majorityLine.seats'))).toBe(true);
221
- });
222
-
223
- it('does not warn for an in-range seats override', () => {
224
- compileHouse({ mark: { type: 'parliament', majorityLine: { seats: 290 } } });
225
- expect(warned().filter((w) => w.includes('majorityLine.seats'))).toEqual([]);
226
- });
227
- });
228
-
229
- it('computes the default majority as floor(total/2)+1 for an odd chamber', () => {
230
- // 3 + 2 = 5 seats, majority = 3.
231
- const layout = compileChart(
232
- {
233
- mark: 'parliament',
234
- data: [
235
- { party: 'A', seats: 3 },
236
- { party: 'B', seats: 2 },
237
- ],
238
- encoding: {
239
- theta: { field: 'seats', type: 'quantitative' },
240
- color: { field: 'party', type: 'nominal', scale: { range: ['#111', '#222'] } },
241
- },
242
- },
243
- { width: 640, height: 400 },
244
- );
245
- const label = layout.marks.find((m): m is TextMarkLayout => m.type === 'textMark');
246
- expect(label?.text).toBe('3 to win');
247
- });
248
- });
249
-
250
- // ---------------------------------------------------------------------------
251
- // Tooltips, legend, a11y
252
- // ---------------------------------------------------------------------------
253
-
254
- describe('parliament tooltips and accessibility', () => {
255
- it('shares one tooltip per party across all its seats', () => {
256
- const layout = compileHouse();
257
- const seats = seatMarks(layout);
258
- // Every seat mark has a matching point-${index} descriptor.
259
- const demIndices: number[] = [];
260
- layout.marks.forEach((m, i) => {
261
- if (m.type === 'point' && m.data.party === 'Democratic') demIndices.push(i);
262
- });
263
- const contents = demIndices.map((i) => layout.tooltipDescriptors.get(`point-${i}`));
264
- expect(contents.every((c) => c !== undefined)).toBe(true);
265
- // All Democratic seats share the same tooltip object (single hover target).
266
- expect(new Set(contents).size).toBe(1);
267
- expect(contents[0]?.title).toBe('Democratic');
268
- expect(seats.length).toBe(435);
269
- });
270
-
271
- it('reports seat count and share in the party tooltip', () => {
272
- const layout = compileHouse();
273
- const firstDem = layout.marks.findIndex(
274
- (m) => m.type === 'point' && m.data.party === 'Democratic',
275
- );
276
- const content = layout.tooltipDescriptors.get(`point-${firstDem}`);
277
- expect(content?.fields.some((f) => f.value === '213')).toBe(true);
278
- expect(content?.fields.some((f) => f.label === 'Share' && f.value.includes('%'))).toBe(true);
279
- });
280
-
281
- it('gives no tooltip to the majority line or label', () => {
282
- const layout = compileHouse();
283
- const ruleIndex = layout.marks.findIndex((m) => m.type === 'rule');
284
- const labelIndex = layout.marks.findIndex((m) => m.type === 'textMark');
285
- expect(layout.tooltipDescriptors.get(`rule-${ruleIndex}`)).toBeUndefined();
286
- expect(layout.tooltipDescriptors.get(`textMark-${labelIndex}`)).toBeUndefined();
287
- });
288
-
289
- it('builds a per-party legend', () => {
290
- const layout = compileHouse();
291
- const labels = layout.legend.entries.map((e) => e.label);
292
- expect(labels).toContain('Democratic');
293
- expect(labels).toContain('Republican');
294
- });
295
-
296
- it('describes the chamber size and majority threshold in the alt text', () => {
297
- const layout = compileHouse();
298
- expect(layout.a11y.altText).toContain('435 seats');
299
- expect(layout.a11y.altText).toContain('218');
300
- });
301
-
302
- it('labels a representative seat per party for screen readers, rest decorative', () => {
303
- const layout = compileHouse();
304
- const dem = seatMarks(layout).filter((s) => s.data.party === 'Democratic');
305
- const labeled = dem.filter((s) => !s.aria.decorative);
306
- expect(labeled).toHaveLength(1);
307
- expect(labeled[0].aria.label).toContain('213 seats');
308
- });
309
- });
310
-
311
- // ---------------------------------------------------------------------------
312
- // Edge cases and errors
313
- // ---------------------------------------------------------------------------
314
-
315
- describe('parliament edge cases', () => {
316
- it('rejects empty data with a clear spec error', () => {
317
- expect(() =>
318
- compileChart(
319
- {
320
- mark: 'parliament',
321
- data: [],
322
- encoding: {
323
- theta: { field: 'seats', type: 'quantitative' },
324
- color: { field: 'party', type: 'nominal', scale: { range: ['#111'] } },
325
- },
326
- },
327
- { width: 640, height: 400 },
328
- ),
329
- ).toThrow(/non-empty array/);
330
- });
331
-
332
- it('produces no seat marks when all seat values are zero', () => {
333
- const layout = compileChart(
334
- {
335
- mark: 'parliament',
336
- data: [
337
- { party: 'A', seats: 0 },
338
- { party: 'B', seats: 0 },
339
- ],
340
- encoding: {
341
- theta: { field: 'seats', type: 'quantitative' },
342
- color: { field: 'party', type: 'nominal', scale: { range: ['#111', '#222'] } },
343
- },
344
- },
345
- { width: 640, height: 400 },
346
- );
347
- expect(seatMarks(layout)).toHaveLength(0);
348
- });
349
-
350
- it('ignores negative seat values rather than crashing', () => {
351
- const layout = compileChart(
352
- {
353
- mark: 'parliament',
354
- data: [
355
- { party: 'A', seats: 10 },
356
- { party: 'B', seats: -5 },
357
- ],
358
- encoding: {
359
- theta: { field: 'seats', type: 'quantitative' },
360
- color: { field: 'party', type: 'nominal', scale: { range: ['#111', '#222'] } },
361
- },
362
- },
363
- { width: 640, height: 400 },
364
- );
365
- // Only the 10 valid seats render; the negative party contributes none.
366
- expect(seatMarks(layout)).toHaveLength(10);
367
- });
368
-
369
- it('handles a multi-party chamber (8 parties) with all seats accounted for', () => {
370
- const euStyle = [
371
- { party: 'Left', seats: 39 },
372
- { party: 'Greens', seats: 71 },
373
- { party: 'S&D', seats: 139 },
374
- { party: 'Renew', seats: 102 },
375
- { party: 'EPP', seats: 178 },
376
- { party: 'ECR', seats: 69 },
377
- { party: 'ID', seats: 49 },
378
- { party: 'NI', seats: 58 },
379
- ];
380
- const total = euStyle.reduce((s, p) => s + p.seats, 0);
381
- const layout = compileChart(
382
- {
383
- mark: 'parliament',
384
- data: euStyle,
385
- encoding: {
386
- theta: { field: 'seats', type: 'quantitative' },
387
- color: {
388
- field: 'party',
389
- type: 'nominal',
390
- scale: {
391
- range: [
392
- '#8b1a1a',
393
- '#3d9970',
394
- '#c44e52',
395
- '#f0a202',
396
- '#1b7fa3',
397
- '#2c3e88',
398
- '#5b6ee1',
399
- '#888',
400
- ],
401
- },
402
- },
403
- },
404
- },
405
- { width: 800, height: 460 },
406
- );
407
- expect(seatMarks(layout)).toHaveLength(total);
408
- const parties = new Set(seatMarks(layout).map((s) => String(s.data.party)));
409
- expect(parties.size).toBe(8);
410
- });
411
- });