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

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 (159) hide show
  1. package/dist/index.d.ts +123 -14
  2. package/dist/index.js +16166 -10027
  3. package/dist/index.js.map +1 -1
  4. package/package.json +11 -4
  5. package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +170 -56
  6. package/src/__tests__/annotation-behavior.test.ts +721 -0
  7. package/src/__tests__/axes.test.ts +50 -3
  8. package/src/__tests__/compile-chart.test.ts +30 -0
  9. package/src/__tests__/compile-footnotes.test.ts +277 -0
  10. package/src/__tests__/compile-layer.test.ts +120 -1
  11. package/src/__tests__/helpers/invariants.ts +54 -20
  12. package/src/__tests__/known-bugs.test.ts +455 -2
  13. package/src/__tests__/legend.test.ts +69 -4
  14. package/src/__tests__/ranking-recipes.test.ts +260 -0
  15. package/src/__tests__/scales.test.ts +9 -3
  16. package/src/__tests__/series-search.test.ts +92 -0
  17. package/src/__tests__/spec-sugar.test.ts +801 -0
  18. package/src/__tests__/you-draw-it.test.ts +160 -0
  19. package/src/annotations/__tests__/compute.test.ts +207 -92
  20. package/src/annotations/__tests__/geometry.test.ts +66 -0
  21. package/src/annotations/__tests__/resolve-text.test.ts +527 -27
  22. package/src/annotations/__tests__/rich-text.test.ts +91 -0
  23. package/src/annotations/__tests__/thinning.test.ts +148 -0
  24. package/src/annotations/collisions.ts +130 -84
  25. package/src/annotations/compute.ts +83 -85
  26. package/src/annotations/constants.ts +116 -6
  27. package/src/annotations/geometry.ts +222 -77
  28. package/src/annotations/move.ts +135 -0
  29. package/src/annotations/placement.ts +62 -14
  30. package/src/annotations/resolve-range.ts +2 -0
  31. package/src/annotations/resolve-refline.ts +2 -0
  32. package/src/annotations/resolve-text.ts +312 -111
  33. package/src/annotations/rich-text.ts +65 -0
  34. package/src/annotations/thinning.ts +84 -10
  35. package/src/barlist/compile-barlist.ts +16 -6
  36. package/src/charts/_shared/format-label-value.ts +9 -8
  37. package/src/charts/bar/__tests__/compute.test.ts +57 -10
  38. package/src/charts/bar/__tests__/labels.test.ts +5 -4
  39. package/src/charts/bar/compute.ts +2 -6
  40. package/src/charts/bar/index.ts +8 -1
  41. package/src/charts/bar/labels.ts +3 -3
  42. package/src/charts/beeswarm/__tests__/compute.test.ts +280 -0
  43. package/src/charts/beeswarm/__tests__/dodge.test.ts +148 -0
  44. package/src/charts/beeswarm/compute.ts +203 -0
  45. package/src/charts/beeswarm/dodge.ts +155 -0
  46. package/src/charts/beeswarm/index.ts +32 -0
  47. package/src/charts/builtin.ts +12 -1
  48. package/src/charts/calendar/__tests__/compute.test.ts +417 -0
  49. package/src/charts/calendar/compute.ts +319 -0
  50. package/src/charts/calendar/index.ts +32 -0
  51. package/src/charts/column/__tests__/compute.test.ts +21 -7
  52. package/src/charts/column/__tests__/labels.test.ts +76 -1
  53. package/src/charts/column/compute.ts +2 -6
  54. package/src/charts/column/index.ts +10 -2
  55. package/src/charts/column/labels.ts +95 -21
  56. package/src/charts/dot/index.ts +8 -1
  57. package/src/charts/dot/labels.ts +3 -3
  58. package/src/charts/line/__tests__/compute.test.ts +15 -19
  59. package/src/charts/line/area.ts +8 -23
  60. package/src/charts/line/compute.ts +32 -2
  61. package/src/charts/parliament/__tests__/compute.test.ts +411 -0
  62. package/src/charts/parliament/compute.ts +313 -0
  63. package/src/charts/parliament/index.ts +32 -0
  64. package/src/charts/pie/__tests__/compute.test.ts +103 -0
  65. package/src/charts/pie/compute.ts +63 -8
  66. package/src/charts/pie/labels.ts +1 -0
  67. package/src/charts/range/__tests__/compute.test.ts +486 -0
  68. package/src/charts/range/compute.ts +273 -0
  69. package/src/charts/range/index.ts +82 -0
  70. package/src/charts/range/labels.ts +169 -0
  71. package/src/charts/rect/__tests__/compute.test.ts +120 -0
  72. package/src/charts/rect/index.ts +127 -0
  73. package/src/charts/scatter/compute.ts +7 -21
  74. package/src/charts/text/__tests__/compute.test.ts +307 -0
  75. package/src/charts/text/index.ts +44 -7
  76. package/src/charts/tick/__tests__/compute.test.ts +75 -0
  77. package/src/charts/tick/index.ts +7 -4
  78. package/src/charts/utils.ts +5 -3
  79. package/src/charts/waffle/__tests__/compute.test.ts +305 -0
  80. package/src/charts/waffle/compute.ts +197 -0
  81. package/src/charts/waffle/index.ts +31 -0
  82. package/src/compile/__tests__/color-scale-reverse.test.ts +89 -0
  83. package/src/compile/__tests__/data-clip.test.ts +68 -0
  84. package/src/compile/__tests__/fill-patterns.test.ts +154 -0
  85. package/src/compile/__tests__/size-scale.test.ts +108 -0
  86. package/src/compile/color-scale-range.ts +54 -33
  87. package/src/compile/data-clip.ts +21 -6
  88. package/src/compile/fill-patterns.ts +74 -0
  89. package/src/compile/layer.ts +117 -21
  90. package/src/compile/size-scale.ts +139 -0
  91. package/src/compile/spec-sugar.ts +758 -0
  92. package/src/compile/you-draw-it.ts +123 -0
  93. package/src/compile.ts +310 -103
  94. package/src/compiler/__tests__/a11y-warnings.test.ts +148 -0
  95. package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +19 -0
  96. package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +20 -0
  97. package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +22 -0
  98. package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +19 -0
  99. package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +14 -0
  100. package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +34 -0
  101. package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +22 -0
  102. package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +41 -0
  103. package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +38 -0
  104. package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +28 -0
  105. package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +28 -0
  106. package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +30 -0
  107. package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +29 -0
  108. package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +32 -0
  109. package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +25 -0
  110. package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +19 -0
  111. package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +19 -0
  112. package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +21 -0
  113. package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +23 -0
  114. package/src/compiler/__tests__/keys.test.ts +3 -9
  115. package/src/compiler/__tests__/normalize.test.ts +65 -1
  116. package/src/compiler/__tests__/validate-did-you-mean.test.ts +86 -0
  117. package/src/compiler/__tests__/validate-near-miss.test.ts +89 -0
  118. package/src/compiler/__tests__/validate.test.ts +156 -0
  119. package/src/compiler/__tests__/vl-parity.test.ts +68 -0
  120. package/src/compiler/a11y-warnings.ts +110 -0
  121. package/src/compiler/keys.ts +5 -4
  122. package/src/compiler/normalize.ts +150 -7
  123. package/src/compiler/types.ts +11 -0
  124. package/src/compiler/validate.ts +664 -18
  125. package/src/endpoint-labels/__tests__/compute.test.ts +88 -0
  126. package/src/endpoint-labels/compute.ts +40 -12
  127. package/src/endpoint-labels/format.ts +45 -15
  128. package/src/endpoint-labels/predict.ts +50 -36
  129. package/src/format/__tests__/field-format.test.ts +164 -0
  130. package/src/format/field-format.ts +27 -0
  131. package/src/geo/__tests__/compile-map.test.ts +621 -0
  132. package/src/geo/compile-map.ts +856 -0
  133. package/src/geo/join.ts +115 -0
  134. package/src/geo/projections.ts +45 -0
  135. package/src/geo/types.ts +24 -0
  136. package/src/graphs/__tests__/compile-graph.test.ts +25 -0
  137. package/src/graphs/compile-graph.ts +27 -18
  138. package/src/index.ts +20 -2
  139. package/src/layout/axes/ticks.ts +59 -10
  140. package/src/layout/axes.ts +35 -6
  141. package/src/layout/dimensions.ts +139 -83
  142. package/src/layout/facet.ts +14 -3
  143. package/src/layout/plan.ts +83 -33
  144. package/src/layout/scales.ts +132 -15
  145. package/src/legend/__tests__/continuous.test.ts +318 -0
  146. package/src/legend/__tests__/size.test.ts +348 -0
  147. package/src/legend/__tests__/suppression.test.ts +17 -1
  148. package/src/legend/compute.ts +119 -13
  149. package/src/legend/continuous.ts +327 -0
  150. package/src/legend/size.ts +407 -0
  151. package/src/legend/suppression.ts +8 -7
  152. package/src/sankey/compile-sankey.ts +31 -19
  153. package/src/tables/format-cells.ts +16 -7
  154. package/src/tilemap/__tests__/compile-tilemap.test.ts +39 -0
  155. package/src/tilemap/compile-tilemap.ts +24 -7
  156. package/src/tooltips/__tests__/compute.test.ts +2 -2
  157. package/src/tooltips/compute.ts +422 -41
  158. package/src/transforms/__tests__/conditional.test.ts +18 -0
  159. package/src/transforms/aggregate.ts +2 -1
@@ -0,0 +1,411 @@
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
+ });