@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,918 +0,0 @@
1
- import type { LayoutStrategy, LegendLayout } from '@opendata-ai/openchart-core';
2
- import { adaptTheme, resolveTheme } from '@opendata-ai/openchart-core';
3
- import { describe, expect, it } from 'vitest';
4
- import { compileChart } from '../compile';
5
- import type { NormalizedChartSpec } from '../compiler/types';
6
- import { computeDimensions } from '../layout/dimensions';
7
- import { INLINE_TICK_OVERHANG_PAD } from '../layout/shared';
8
- import { legendGap } from '../legend/wrap';
9
-
10
- const baseSpec: NormalizedChartSpec = {
11
- markType: 'line',
12
- markDef: { type: 'line' },
13
- data: [
14
- { date: '2020-01-01', value: 10 },
15
- { date: '2021-01-01', value: 20 },
16
- ],
17
- encoding: {
18
- x: { field: 'date', type: 'temporal' },
19
- y: { field: 'value', type: 'quantitative' },
20
- },
21
- chrome: { title: { text: 'Test Chart' } },
22
- annotations: [],
23
- responsive: true,
24
- theme: {},
25
- darkMode: 'off',
26
- labels: { density: 'auto', format: '' },
27
- };
28
-
29
- const lightTheme = resolveTheme(baseSpec.theme);
30
- const darkTheme = adaptTheme(lightTheme);
31
-
32
- const emptyLegend: LegendLayout = {
33
- position: 'top',
34
- entries: [],
35
- bounds: { x: 0, y: 0, width: 0, height: 0 },
36
- labelStyle: {
37
- fontFamily: 'Inter',
38
- fontSize: 11,
39
- fontWeight: 400,
40
- fill: '#333',
41
- lineHeight: 1.3,
42
- },
43
- swatchSize: 12,
44
- swatchGap: 6,
45
- entryGap: 16,
46
- };
47
-
48
- const rightLegend: LegendLayout = {
49
- ...emptyLegend,
50
- position: 'right',
51
- entries: [{ label: 'US', color: '#1b7fa3', shape: 'line' }],
52
- bounds: { x: 500, y: 0, width: 100, height: 200 },
53
- };
54
-
55
- const topLegend: LegendLayout = {
56
- ...emptyLegend,
57
- position: 'top',
58
- entries: [{ label: 'US', color: '#1b7fa3', shape: 'line' }],
59
- bounds: { x: 0, y: 0, width: 400, height: 28 },
60
- };
61
-
62
- describe('computeDimensions', () => {
63
- it('computes chart area within total dimensions', () => {
64
- const dims = computeDimensions(baseSpec, { width: 600, height: 400 }, emptyLegend, lightTheme);
65
-
66
- expect(dims.total).toEqual({ x: 0, y: 0, width: 600, height: 400 });
67
- expect(dims.chartArea.width).toBeLessThan(600);
68
- expect(dims.chartArea.height).toBeLessThan(400);
69
- expect(dims.chartArea.width).toBeGreaterThan(0);
70
- expect(dims.chartArea.height).toBeGreaterThan(0);
71
- });
72
-
73
- it('accounts for chrome height in chart area', () => {
74
- const noChrome: NormalizedChartSpec = { ...baseSpec, chrome: {} };
75
- const withChrome = baseSpec;
76
-
77
- const dimsNoChrome = computeDimensions(
78
- noChrome,
79
- { width: 600, height: 400 },
80
- emptyLegend,
81
- lightTheme,
82
- );
83
- const dimsWithChrome = computeDimensions(
84
- withChrome,
85
- { width: 600, height: 400 },
86
- emptyLegend,
87
- lightTheme,
88
- );
89
-
90
- // With chrome, the chart area should be shorter (less height available)
91
- expect(dimsWithChrome.chartArea.height).toBeLessThan(dimsNoChrome.chartArea.height);
92
- // Chrome should have nonzero top height
93
- expect(dimsWithChrome.chrome.topHeight).toBeGreaterThan(0);
94
- });
95
-
96
- it('reserves space for right-positioned legend', () => {
97
- const withoutLegend = computeDimensions(
98
- baseSpec,
99
- { width: 600, height: 400 },
100
- emptyLegend,
101
- lightTheme,
102
- );
103
- const withLegend = computeDimensions(
104
- baseSpec,
105
- { width: 600, height: 400 },
106
- rightLegend,
107
- lightTheme,
108
- );
109
-
110
- expect(withLegend.chartArea.width).toBeLessThan(withoutLegend.chartArea.width);
111
- });
112
-
113
- it('reserves space for top-positioned legend', () => {
114
- const withoutLegend = computeDimensions(
115
- baseSpec,
116
- { width: 600, height: 400 },
117
- emptyLegend,
118
- lightTheme,
119
- );
120
- const withLegend = computeDimensions(
121
- baseSpec,
122
- { width: 600, height: 400 },
123
- topLegend,
124
- lightTheme,
125
- );
126
-
127
- expect(withLegend.chartArea.height).toBeLessThan(withoutLegend.chartArea.height);
128
- });
129
-
130
- it('reserves enough left margin for y-axis title to clear tick labels', () => {
131
- const specWithYTitle: NormalizedChartSpec = {
132
- ...baseSpec,
133
- encoding: {
134
- x: { field: 'date', type: 'temporal' },
135
- y: { field: 'value', type: 'quantitative', axis: { title: 'Share of districts' } },
136
- },
137
- };
138
- const specWithoutYTitle: NormalizedChartSpec = {
139
- ...baseSpec,
140
- encoding: {
141
- x: { field: 'date', type: 'temporal' },
142
- y: { field: 'value', type: 'quantitative' },
143
- },
144
- };
145
-
146
- const dimsWithTitle = computeDimensions(
147
- specWithYTitle,
148
- { width: 600, height: 400 },
149
- emptyLegend,
150
- lightTheme,
151
- );
152
- const dimsWithoutTitle = computeDimensions(
153
- specWithoutYTitle,
154
- { width: 600, height: 400 },
155
- emptyLegend,
156
- lightTheme,
157
- );
158
-
159
- // A chart with a y-axis title needs more left margin than one without
160
- expect(dimsWithTitle.margins.left).toBeGreaterThan(dimsWithoutTitle.margins.left);
161
- // The difference should be at least enough for the rotated title glyph
162
- // plus breathing room (halfGlyph ~7 + trailing pad 4 = 11px minimum)
163
- expect(dimsWithTitle.margins.left - dimsWithoutTitle.margins.left).toBeGreaterThanOrEqual(11);
164
- });
165
-
166
- it('y-axis title margin scales with tick label width (gutter ticks)', () => {
167
- // Bar chart: y-tick labels sit in a left gutter, so the title must clear
168
- // them. Wider tick labels ("2,000,000" vs "9") push the title further out.
169
- const barBase: NormalizedChartSpec = {
170
- ...baseSpec,
171
- markType: 'bar',
172
- markDef: { type: 'bar' },
173
- };
174
- const smallValues: NormalizedChartSpec = {
175
- ...barBase,
176
- data: [
177
- { cat: 'a', value: 5 },
178
- { cat: 'b', value: 9 },
179
- ],
180
- encoding: {
181
- x: { field: 'cat', type: 'nominal' },
182
- y: { field: 'value', type: 'quantitative', axis: { title: 'Count' } },
183
- },
184
- };
185
- const largeValues: NormalizedChartSpec = {
186
- ...barBase,
187
- data: [
188
- { cat: 'a', value: 1_500_000 },
189
- { cat: 'b', value: 2_000_000 },
190
- ],
191
- encoding: {
192
- x: { field: 'cat', type: 'nominal' },
193
- y: {
194
- field: 'value',
195
- type: 'quantitative',
196
- axis: { title: 'Revenue ($)', format: ',.0f' },
197
- },
198
- },
199
- };
200
-
201
- const dimsSmall = computeDimensions(
202
- smallValues,
203
- { width: 600, height: 400 },
204
- emptyLegend,
205
- lightTheme,
206
- );
207
- const dimsLarge = computeDimensions(
208
- largeValues,
209
- { width: 600, height: 400 },
210
- emptyLegend,
211
- lightTheme,
212
- );
213
-
214
- // Explicit format ",.0f" on large values produces "2,000,000" tick labels
215
- // that are much wider than the abbreviated "0.0" from small values,
216
- // so the y-axis title margin should grow to keep clearance
217
- expect(dimsLarge.margins.left).toBeGreaterThan(dimsSmall.margins.left);
218
- });
219
-
220
- it('y-axis title margin ignores tick label width for inline ticks (line)', () => {
221
- // Line chart with continuous y: tick labels render inline (above their
222
- // gridline inside the plot), not in a left gutter. The title only needs to
223
- // clear the chart edge, so wide tick values must NOT inflate the margin —
224
- // that dead padding is the exact bug this guards against.
225
- const smallValues: NormalizedChartSpec = {
226
- ...baseSpec,
227
- data: [
228
- { date: '2020-01-01', value: 5 },
229
- { date: '2021-01-01', value: 9 },
230
- ],
231
- encoding: {
232
- x: { field: 'date', type: 'temporal' },
233
- y: { field: 'value', type: 'quantitative', axis: { title: 'Count' } },
234
- },
235
- };
236
- const largeValues: NormalizedChartSpec = {
237
- ...baseSpec,
238
- data: [
239
- { date: '2020-01-01', value: 1_500_000 },
240
- { date: '2021-01-01', value: 2_000_000 },
241
- ],
242
- encoding: {
243
- x: { field: 'date', type: 'temporal' },
244
- y: {
245
- field: 'value',
246
- type: 'quantitative',
247
- axis: { title: 'Revenue ($)', format: ',.0f' },
248
- },
249
- },
250
- };
251
-
252
- const dimsSmall = computeDimensions(
253
- smallValues,
254
- { width: 600, height: 400 },
255
- emptyLegend,
256
- lightTheme,
257
- );
258
- const dimsLarge = computeDimensions(
259
- largeValues,
260
- { width: 600, height: 400 },
261
- emptyLegend,
262
- lightTheme,
263
- );
264
-
265
- expect(dimsLarge.margins.left).toBe(dimsSmall.margins.left);
266
- });
267
-
268
- it('reserves gutter margin when an explicit band scale forces gutter ticks (line)', () => {
269
- // A line chart's y-tick position is decided by the RESOLVED scale type, not
270
- // the field type. An explicit `scale.type: 'band'` on a quantitative field
271
- // resolves to a categorical (gutter) axis, so computeAxes draws a gutter
272
- // title. The margin reservation must agree — reserving the inline (narrow)
273
- // margin here would let the title collide with the gutter tick labels.
274
- const bandScaleLine: NormalizedChartSpec = {
275
- ...baseSpec,
276
- data: [
277
- { date: '2020-01-01', value: 1_500_000 },
278
- { date: '2021-01-01', value: 2_000_000 },
279
- ],
280
- encoding: {
281
- x: { field: 'date', type: 'temporal' },
282
- y: {
283
- field: 'value',
284
- type: 'quantitative',
285
- scale: { type: 'band' },
286
- axis: { title: 'Revenue ($)', format: ',.0f' },
287
- },
288
- },
289
- };
290
- const inlineLine: NormalizedChartSpec = {
291
- ...baseSpec,
292
- data: [
293
- { date: '2020-01-01', value: 1_500_000 },
294
- { date: '2021-01-01', value: 2_000_000 },
295
- ],
296
- encoding: {
297
- x: { field: 'date', type: 'temporal' },
298
- y: {
299
- field: 'value',
300
- type: 'quantitative',
301
- axis: { title: 'Revenue ($)', format: ',.0f' },
302
- },
303
- },
304
- };
305
-
306
- const bandDims = computeDimensions(
307
- bandScaleLine,
308
- { width: 600, height: 400 },
309
- emptyLegend,
310
- lightTheme,
311
- );
312
- const inlineDims = computeDimensions(
313
- inlineLine,
314
- { width: 600, height: 400 },
315
- emptyLegend,
316
- lightTheme,
317
- );
318
-
319
- // The gutter (band-scale) reservation includes the wide tick-label width, so
320
- // it must exceed the inline reservation that omits it.
321
- expect(bandDims.margins.left).toBeGreaterThan(inlineDims.margins.left);
322
- });
323
-
324
- it('applies dark mode theme adaptation', () => {
325
- const lightDims = computeDimensions(
326
- baseSpec,
327
- { width: 600, height: 400 },
328
- emptyLegend,
329
- lightTheme,
330
- );
331
- const darkDims = computeDimensions(
332
- baseSpec,
333
- { width: 600, height: 400, darkMode: true },
334
- emptyLegend,
335
- darkTheme,
336
- );
337
-
338
- expect(lightDims.theme.isDark).toBe(false);
339
- expect(darkDims.theme.isDark).toBe(true);
340
- // Both modes use transparent background — the dark adaptation changes text/axis
341
- // colors while keeping transparency so the host surface shows through.
342
- expect(darkDims.theme.colors.background).toBe('transparent');
343
- expect(lightDims.theme.colors.background).toBe('transparent');
344
- // Dark mode uses a different text color
345
- expect(darkDims.theme.colors.text).not.toBe(lightDims.theme.colors.text);
346
- });
347
-
348
- it('prevents negative chart area dimensions', () => {
349
- // Tiny container
350
- const dims = computeDimensions(baseSpec, { width: 50, height: 30 }, emptyLegend, lightTheme);
351
- expect(dims.chartArea.width).toBeGreaterThanOrEqual(0);
352
- expect(dims.chartArea.height).toBeGreaterThanOrEqual(0);
353
- });
354
-
355
- it('reserves extra bottom space for rotated x-axis labels', () => {
356
- const rotatedSpec: NormalizedChartSpec = {
357
- ...baseSpec,
358
- markType: 'bar',
359
- markDef: { type: 'bar', orient: 'vertical' },
360
- data: [
361
- { category: 'California', value: 10 },
362
- { category: 'New York', value: 20 },
363
- { category: 'Massachusetts', value: 15 },
364
- ],
365
- encoding: {
366
- x: { field: 'category', type: 'nominal', axis: { labelAngle: -90 } },
367
- y: { field: 'value', type: 'quantitative' },
368
- },
369
- };
370
- const normalSpec: NormalizedChartSpec = {
371
- ...baseSpec,
372
- markType: 'bar',
373
- markDef: { type: 'bar', orient: 'vertical' },
374
- data: rotatedSpec.data,
375
- encoding: {
376
- x: { field: 'category', type: 'nominal' },
377
- y: { field: 'value', type: 'quantitative' },
378
- },
379
- };
380
-
381
- const dimsRotated = computeDimensions(
382
- rotatedSpec,
383
- { width: 600, height: 400 },
384
- emptyLegend,
385
- lightTheme,
386
- );
387
- const dimsNormal = computeDimensions(
388
- normalSpec,
389
- { width: 600, height: 400 },
390
- emptyLegend,
391
- lightTheme,
392
- );
393
-
394
- // Rotated labels should reserve more bottom space, shrinking the chart area
395
- expect(dimsRotated.chartArea.height).toBeLessThan(dimsNormal.chartArea.height);
396
- expect(dimsRotated.margins.bottom).toBeGreaterThan(dimsNormal.margins.bottom);
397
- });
398
-
399
- it('does not change bottom space for small tick angles', () => {
400
- const smallAngleSpec: NormalizedChartSpec = {
401
- ...baseSpec,
402
- encoding: {
403
- x: { field: 'date', type: 'temporal', axis: { labelAngle: 5 } },
404
- y: { field: 'value', type: 'quantitative' },
405
- },
406
- };
407
-
408
- const dimsSmall = computeDimensions(
409
- smallAngleSpec,
410
- { width: 600, height: 400 },
411
- emptyLegend,
412
- lightTheme,
413
- );
414
- const dimsNone = computeDimensions(
415
- baseSpec,
416
- { width: 600, height: 400 },
417
- emptyLegend,
418
- lightTheme,
419
- );
420
-
421
- // Small angles (< 10 degrees) should not trigger rotated label logic
422
- expect(dimsSmall.margins.bottom).toBe(dimsNone.margins.bottom);
423
- });
424
-
425
- it('does not reserve annotation margin when strategy is tooltip-only', () => {
426
- const specWithAnnotations: NormalizedChartSpec = {
427
- ...baseSpec,
428
- annotations: [{ type: 'text', x: '2021-01-01', y: 20, text: 'Right-edge annotation' }],
429
- };
430
-
431
- const inlineStrategy: LayoutStrategy = {
432
- labelMode: 'all',
433
- legendPosition: 'right',
434
- annotationPosition: 'inline',
435
- axisLabelDensity: 'full',
436
- };
437
- const tooltipOnlyStrategy: LayoutStrategy = {
438
- labelMode: 'none',
439
- legendPosition: 'top',
440
- annotationPosition: 'tooltip-only',
441
- axisLabelDensity: 'minimal',
442
- };
443
-
444
- const dimsInline = computeDimensions(
445
- specWithAnnotations,
446
- { width: 600, height: 400 },
447
- emptyLegend,
448
- lightTheme,
449
- inlineStrategy,
450
- );
451
- const dimsTooltipOnly = computeDimensions(
452
- specWithAnnotations,
453
- { width: 600, height: 400 },
454
- emptyLegend,
455
- lightTheme,
456
- tooltipOnlyStrategy,
457
- );
458
- const dimsNoAnnotations = computeDimensions(
459
- baseSpec,
460
- { width: 600, height: 400 },
461
- emptyLegend,
462
- lightTheme,
463
- );
464
-
465
- // Inline strategy should reserve extra right margin for the annotation
466
- expect(dimsInline.margins.right).toBeGreaterThan(dimsNoAnnotations.margins.right);
467
- // Tooltip-only should NOT reserve extra margin (annotations are hidden)
468
- expect(dimsTooltipOnly.margins.right).toBe(dimsNoAnnotations.margins.right);
469
- });
470
-
471
- it('clamps y-axis label margin on narrow containers to preserve chart area', () => {
472
- const longLabelSpec: NormalizedChartSpec = {
473
- ...baseSpec,
474
- markType: 'bar',
475
- markDef: { type: 'bar' },
476
- data: [
477
- {
478
- category: 'This is a very long category label that would consume lots of space',
479
- value: 10,
480
- },
481
- { category: 'Another extremely verbose category name', value: 20 },
482
- ],
483
- encoding: {
484
- x: { field: 'value', type: 'quantitative' },
485
- y: { field: 'category', type: 'nominal' },
486
- },
487
- };
488
-
489
- const narrowDims = computeDimensions(
490
- longLabelSpec,
491
- { width: 350, height: 300 },
492
- emptyLegend,
493
- lightTheme,
494
- );
495
-
496
- // On narrow viewports, left margin should be clamped so the chart area
497
- // retains at least ~45% of the container width
498
- expect(narrowDims.chartArea.width).toBeGreaterThanOrEqual(350 * 0.4);
499
- });
500
-
501
- describe('metrics bar', () => {
502
- const fourMetrics = [
503
- { label: 'CLOSE', value: '$186.10', delta: '+1.4%', deltaTone: 'up' as const },
504
- { label: 'ALL-TIME HIGH', value: '$202.00' },
505
- { label: '3-YR RETURN', value: '+1228%', secondary: '10.3x' },
506
- { label: 'AVG MONTHLY', value: '$104.95' },
507
- ];
508
-
509
- it('reserves space and emits cells when metrics fit', () => {
510
- const spec: NormalizedChartSpec = { ...baseSpec, metrics: fourMetrics };
511
- const dims = computeDimensions(spec, { width: 800, height: 500 }, emptyLegend, lightTheme);
512
-
513
- expect(dims.metrics).toBeDefined();
514
- expect(dims.metrics?.cells).toHaveLength(4);
515
- // First cell sits at the container left padding (aligned with title /
516
- // eyebrow), not indented to the chart area's left gutter.
517
- expect(dims.metrics?.cells[0].x).toBeLessThanOrEqual(dims.chartArea.x);
518
- expect(dims.metrics?.cells[0].x).toBeGreaterThan(0);
519
- // Cells span the metrics area evenly
520
- const totalSpan =
521
- (dims.metrics?.cells[3].x ?? 0) +
522
- (dims.metrics?.cells[3].cellWidth ?? 0) -
523
- (dims.metrics?.cells[0].x ?? 0);
524
- const cellWidth = totalSpan / 4;
525
- expect(dims.metrics?.cells[1].x).toBeCloseTo((dims.metrics?.cells[0].x ?? 0) + cellWidth, 1);
526
- });
527
-
528
- it('shrinks chart-area top to accommodate metric bar', () => {
529
- const noMetrics = computeDimensions(
530
- baseSpec,
531
- { width: 800, height: 500 },
532
- emptyLegend,
533
- lightTheme,
534
- );
535
- const withMetrics = computeDimensions(
536
- { ...baseSpec, metrics: fourMetrics },
537
- { width: 800, height: 500 },
538
- emptyLegend,
539
- lightTheme,
540
- );
541
-
542
- expect(withMetrics.chartArea.height).toBeLessThan(noMetrics.chartArea.height);
543
- expect(withMetrics.margins.top).toBeGreaterThan(noMetrics.margins.top);
544
- });
545
-
546
- it('strips metric bar on narrow widths', () => {
547
- const spec: NormalizedChartSpec = { ...baseSpec, metrics: fourMetrics };
548
- const dims = computeDimensions(spec, { width: 400, height: 400 }, emptyLegend, lightTheme);
549
- expect(dims.metrics).toBeUndefined();
550
-
551
- // Top margin should match the no-metrics case (no leftover reservation)
552
- const noMetrics = computeDimensions(
553
- baseSpec,
554
- { width: 400, height: 400 },
555
- emptyLegend,
556
- lightTheme,
557
- );
558
- expect(dims.margins.top).toBe(noMetrics.margins.top);
559
- });
560
-
561
- it('strips metric bar when value text would overflow', () => {
562
- const oversizeMetrics = [
563
- { label: 'A', value: 'this is an extraordinarily long monetary value that cannot fit' },
564
- { label: 'B', value: 'another impossibly long string to force overflow detection' },
565
- { label: 'C', value: 'still more text that pushes well past the cell width' },
566
- { label: 'D', value: 'and one more lengthy figure for good measure here too' },
567
- ];
568
- const spec: NormalizedChartSpec = { ...baseSpec, metrics: oversizeMetrics };
569
- const dims = computeDimensions(spec, { width: 600, height: 400 }, emptyLegend, lightTheme);
570
- expect(dims.metrics).toBeUndefined();
571
- });
572
-
573
- it('returns undefined when metrics array is empty', () => {
574
- const spec: NormalizedChartSpec = { ...baseSpec, metrics: [] };
575
- const dims = computeDimensions(spec, { width: 800, height: 500 }, emptyLegend, lightTheme);
576
- expect(dims.metrics).toBeUndefined();
577
- });
578
- });
579
-
580
- it('avoids doubling axisMargin and legendGap when top legend is present', () => {
581
- const dimsWithTopLegend = computeDimensions(
582
- baseSpec,
583
- { width: 600, height: 400 },
584
- topLegend,
585
- lightTheme,
586
- );
587
- const dimsNoLegend = computeDimensions(
588
- baseSpec,
589
- { width: 600, height: 400 },
590
- emptyLegend,
591
- lightTheme,
592
- );
593
-
594
- // Without a top legend, the full topAxisGap (axisMargin + inlineTickOverhang)
595
- // separates chrome from chart area. With a top legend, legendGap already
596
- // provides separation, so only inlineTickOverhang is added (not the full
597
- // topAxisGap). This means the chart area gains back ~axisMargin (6px)
598
- // that would otherwise be redundant spacing.
599
- //
600
- // The top margin with legend includes: gapAbove(6) + legendHeight(28)
601
- // + legendGap(8) + inlineTickOverhang(17) instead of the no-legend
602
- // topAxisGap(23). Net: margin delta = 6 + 28 + 8 + 17 - 23 = 36px.
603
- // If axisMargin were doubling up: 6 + 28 + 8 + 23 - 23 = 42px.
604
- const topMarginDelta = dimsWithTopLegend.margins.top - dimsNoLegend.margins.top;
605
- expect(topMarginDelta).toBeLessThan(topLegend.bounds.height + legendGap(600) + 1);
606
- // With a legend present, the chart area should still be shorter
607
- expect(dimsWithTopLegend.chartArea.height).toBeLessThan(dimsNoLegend.chartArea.height);
608
- });
609
-
610
- it('returns effectiveAxisGap on the primary return path', () => {
611
- const overhang = lightTheme.fonts.sizes.axisTick + INLINE_TICK_OVERHANG_PAD;
612
-
613
- // With a top legend only the inline-tick overhang separates legend and
614
- // chart area (placeLegend consumes this to lift the legend above it).
615
- const withTopLegend = computeDimensions(
616
- baseSpec,
617
- { width: 600, height: 400 },
618
- topLegend,
619
- lightTheme,
620
- );
621
- expect(withTopLegend.effectiveAxisGap).toBe(overhang);
622
-
623
- // Without a top legend the full topAxisGap applies.
624
- const noLegend = computeDimensions(
625
- baseSpec,
626
- { width: 600, height: 400 },
627
- emptyLegend,
628
- lightTheme,
629
- );
630
- expect(noLegend.effectiveAxisGap).toBe(lightTheme.spacing.axisMargin + overhang);
631
- });
632
-
633
- it('returns effectiveAxisGap on the chrome-strip guardrail path', () => {
634
- // A cramped height forces the guardrail early-return with stripped chrome.
635
- const talkativeSpec: NormalizedChartSpec = {
636
- ...baseSpec,
637
- chrome: {
638
- title: { text: 'A chart title that occupies chrome space' },
639
- subtitle: { text: 'A subtitle that gets stripped in compact chrome' },
640
- source: { text: 'Source: somewhere' },
641
- },
642
- };
643
- const dims = computeDimensions(
644
- talkativeSpec,
645
- { width: 600, height: 120 },
646
- topLegend,
647
- lightTheme,
648
- );
649
- // Prove the guardrail fired: compact chrome drops the subtitle.
650
- expect(dims.chrome.subtitle).toBeUndefined();
651
- expect(dims.effectiveAxisGap).toBe(lightTheme.fonts.sizes.axisTick + INLINE_TICK_OVERHANG_PAD);
652
- });
653
-
654
- it('tightens legend gap on narrow viewports', () => {
655
- const wideDims = computeDimensions(
656
- baseSpec,
657
- { width: 600, height: 400 },
658
- topLegend,
659
- lightTheme,
660
- );
661
- const narrowDims = computeDimensions(
662
- baseSpec,
663
- { width: 360, height: 400 },
664
- topLegend,
665
- lightTheme,
666
- );
667
-
668
- // Narrow viewport should have more chart height available (smaller legend gap)
669
- expect(narrowDims.chartArea.height).toBeGreaterThanOrEqual(wideDims.chartArea.height - 10);
670
- });
671
-
672
- it('exposes xAxisHeight on the layout dimensions', () => {
673
- const dims = computeDimensions(baseSpec, { width: 600, height: 400 }, emptyLegend, lightTheme);
674
- // Default x-axis (no rotation, no title): 26px reservation.
675
- expect(dims.xAxisHeight).toBeGreaterThan(0);
676
- });
677
-
678
- it('reserves extra bottom space for a bottom legend so it sits below the x-axis', () => {
679
- // Defect-3 regression: bottom-positioned legends used to render in the
680
- // same band as the x-axis tick row. The reservation now flows through
681
- // chrome.bottomHeight (via bottomLegendReservation) so chrome stacks
682
- // below the legend band rather than colliding with it. The base bottom
683
- // margin already includes xAxisHeight, so the additional reservation
684
- // collapses to legendHeight + gap.
685
- const bottomLegend: LegendLayout = {
686
- ...emptyLegend,
687
- position: 'bottom',
688
- entries: [{ label: 'US', color: '#1b7fa3', shape: 'line' }],
689
- bounds: { x: 0, y: 0, width: 400, height: 28 },
690
- };
691
-
692
- const dimsNoLegend = computeDimensions(
693
- baseSpec,
694
- { width: 600, height: 400 },
695
- emptyLegend,
696
- lightTheme,
697
- );
698
- const dimsBottom = computeDimensions(
699
- baseSpec,
700
- { width: 600, height: 400 },
701
- bottomLegend,
702
- lightTheme,
703
- );
704
-
705
- // Reservation is legendHeight + gap, threaded through chrome.bottomHeight.
706
- const gap = legendGap(600);
707
- const expectedExtra = bottomLegend.bounds.height + gap;
708
- expect(dimsBottom.margins.bottom - dimsNoLegend.margins.bottom).toBeCloseTo(expectedExtra, 5);
709
- // The reservation lives on chrome.bottomHeight, not as a separate margin
710
- // delta, so renderers reading chrome.source.y get the legend offset baked
711
- // in for free.
712
- expect(dimsBottom.chrome.bottomHeight - dimsNoLegend.chrome.bottomHeight).toBeCloseTo(
713
- expectedExtra,
714
- 5,
715
- );
716
- });
717
- });
718
-
719
- describe('bottom legend placement (defect-3 regression)', () => {
720
- it('places the bottom legend below the x-axis tick row, not over it', () => {
721
- // Multi-series area with explicit bottom legend should render the legend
722
- // beneath the x-axis ticks. Asserts:
723
- // legend.bounds.y >= chartArea.y + chartArea.height + xAxisHeight + gap
724
- const spec = {
725
- mark: 'area' as const,
726
- data: [
727
- { year: '2020', value: 10, series: 'A' },
728
- { year: '2021', value: 20, series: 'A' },
729
- { year: '2022', value: 15, series: 'A' },
730
- { year: '2020', value: 8, series: 'B' },
731
- { year: '2021', value: 18, series: 'B' },
732
- { year: '2022', value: 12, series: 'B' },
733
- { year: '2020', value: 5, series: 'C' },
734
- { year: '2021', value: 12, series: 'C' },
735
- { year: '2022', value: 9, series: 'C' },
736
- ],
737
- encoding: {
738
- x: { field: 'year', type: 'temporal' as const },
739
- y: { field: 'value', type: 'quantitative' as const },
740
- color: { field: 'series', type: 'nominal' as const },
741
- },
742
- legend: { position: 'bottom' as const, show: true },
743
- };
744
-
745
- const layout = compileChart(spec, { width: 800, height: 500 });
746
-
747
- expect(layout.legend.entries.length).toBeGreaterThan(0);
748
- expect(layout.legend.position).toBe('bottom');
749
-
750
- // Recompute the gap the engine uses internally to make a tight assertion.
751
- const gap = legendGap(800);
752
-
753
- // Use the same axis-height fallback dimensions.ts uses for an unrotated
754
- // x-axis without an axis title (26px). Asserting `>=` means the legend
755
- // top is at or below the bottom of the x-axis tick row.
756
- const xAxisHeight = 26;
757
- const chartBottom = layout.area.y + layout.area.height;
758
- const minLegendY = chartBottom + xAxisHeight + gap;
759
-
760
- expect(layout.legend.bounds.y).toBeGreaterThanOrEqual(minLegendY - 0.5);
761
- });
762
-
763
- it('positions bottom chrome (source) below the bottom legend band', () => {
764
- // Follow-up regression: a bottom legend reserved space below the x-axis
765
- // tick row, but source/byline/footer chrome was still positioned via
766
- // chartBottom + xAxisExtent + chartToFooter, landing in the same band as
767
- // the legend swatches. Threading bottomLegendReservation into computeChrome
768
- // now shifts chrome.source.y so it stacks below the legend.
769
- const spec = {
770
- mark: 'area' as const,
771
- data: [
772
- { year: '2020', value: 10, series: 'A' },
773
- { year: '2021', value: 20, series: 'A' },
774
- { year: '2022', value: 15, series: 'A' },
775
- { year: '2020', value: 8, series: 'B' },
776
- { year: '2021', value: 18, series: 'B' },
777
- { year: '2022', value: 12, series: 'B' },
778
- ],
779
- encoding: {
780
- x: { field: 'year', type: 'temporal' as const },
781
- y: { field: 'value', type: 'quantitative' as const },
782
- color: { field: 'series', type: 'nominal' as const },
783
- },
784
- legend: { position: 'bottom' as const, show: true },
785
- chrome: {
786
- source: 'World Bank, 2024',
787
- byline: 'OpenChart Newsroom',
788
- },
789
- };
790
-
791
- const layout = compileChart(spec, { width: 800, height: 500 });
792
-
793
- // Sanity: legend and chrome both rendered.
794
- expect(layout.legend.position).toBe('bottom');
795
- expect(layout.legend.entries.length).toBeGreaterThan(0);
796
- expect(layout.chrome.source).toBeDefined();
797
- expect(layout.chrome.byline).toBeDefined();
798
-
799
- // Compute chrome's absolute y the same way the renderer does:
800
- // bottomOffset = chartBottom + xAxisExtent (= 26 for default unrotated x-axis).
801
- const xAxisExtent = 26;
802
- const bottomOffset = layout.area.y + layout.area.height + xAxisExtent;
803
- const sourceAbsoluteY = bottomOffset + layout.chrome.source!.y;
804
- const bylineAbsoluteY = bottomOffset + layout.chrome.byline!.y;
805
-
806
- // Legend's bottom edge.
807
- const legendBottom = layout.legend.bounds.y + layout.legend.bounds.height;
808
-
809
- // Source must land below the legend band, with at least a small gap
810
- // (chartToFooter ≈ 12px) for breathing room.
811
- expect(sourceAbsoluteY).toBeGreaterThan(legendBottom);
812
- expect(bylineAbsoluteY).toBeGreaterThan(sourceAbsoluteY);
813
- });
814
-
815
- // Guards issue 2 (narrow-width space efficiency). The responsive metrics
816
- // (scaled container padding, halved horizontal padding, collapsed legend/
817
- // chrome gaps below the compact breakpoint) already keep the plot area's
818
- // share of a phone-sized container within a couple of points of desktop.
819
- // These floors lock that in: if a future change stops scaling padding down
820
- // on narrow viewports, the plot share drops and the test fails.
821
- describe('plot-area share stays efficient at narrow widths', () => {
822
- const titledSpec: NormalizedChartSpec = {
823
- ...baseSpec,
824
- markType: 'bar',
825
- markDef: { type: 'bar' },
826
- encoding: {
827
- x: { field: 'date', type: 'nominal' },
828
- y: { field: 'value', type: 'quantitative' },
829
- },
830
- chrome: {
831
- title: { text: 'A representative chart title' },
832
- subtitle: { text: 'A subtitle line' },
833
- source: { text: 'Source: somewhere' },
834
- },
835
- };
836
-
837
- it('reserves at least 85% of container width for the plot at 375px', () => {
838
- const dims = computeDimensions(
839
- titledSpec,
840
- { width: 375, height: 500 },
841
- emptyLegend,
842
- lightTheme,
843
- );
844
- expect(dims.chartArea.width / 375).toBeGreaterThanOrEqual(0.85);
845
- });
846
-
847
- it('reserves at least 55% of container height for the plot at 375px', () => {
848
- const dims = computeDimensions(
849
- titledSpec,
850
- { width: 375, height: 500 },
851
- emptyLegend,
852
- lightTheme,
853
- );
854
- expect(dims.chartArea.height / 500).toBeGreaterThanOrEqual(0.55);
855
- });
856
-
857
- it('narrow plot width share is no worse than desktop', () => {
858
- const narrow = computeDimensions(
859
- titledSpec,
860
- { width: 375, height: 500 },
861
- emptyLegend,
862
- lightTheme,
863
- );
864
- const desktop = computeDimensions(
865
- titledSpec,
866
- { width: 700, height: 500 },
867
- emptyLegend,
868
- lightTheme,
869
- );
870
- // Narrow viewports must not waste more horizontal space than desktop:
871
- // the halved horizontal padding keeps the share on par (allow 2pt slack).
872
- expect(narrow.chartArea.width / 375).toBeGreaterThanOrEqual(
873
- desktop.chartArea.width / 700 - 0.02,
874
- );
875
- });
876
- });
877
-
878
- it('keeps the inline y-axis title inside the container on narrow, large-font charts', () => {
879
- // Regression (mobile): the inline y-tick fix reserved a narrow left margin,
880
- // but computeAxes still positioned the rotated y-title with the GUTTER
881
- // offset (tick-label width + viewport floor). On a phone at deck-scale
882
- // fonts that pushed the title's rotation center to x ≈ -1, clipping the
883
- // whole title off the left edge. The title offset now honors the inline
884
- // flag, so titlePosition.x stays a comfortable distance inside x = 0.
885
- const spec = {
886
- mark: { type: 'line' as const, point: true },
887
- data: [
888
- { year: '2009-10', pct: 94.2 },
889
- { year: '2015-16', pct: 93.8 },
890
- { year: '2020-21', pct: 95.0 },
891
- ],
892
- encoding: {
893
- x: { field: 'year', type: 'ordinal' as const },
894
- y: {
895
- field: 'pct',
896
- type: 'quantitative' as const,
897
- axis: { title: 'Kindergartners with 2-dose MMR', grid: true, format: '.0f' },
898
- scale: { domain: [90, 96], nice: false },
899
- },
900
- },
901
- theme: { fonts: { sizes: { body: 21, axisTick: 18 } } },
902
- chrome: { title: 'T', subtitle: 'S', source: 'Src' },
903
- };
904
-
905
- for (const width of [320, 360, 390]) {
906
- const layout = compileChart(spec, { width, height: 600 });
907
- const yAxis = layout.axes.y;
908
- expect(yAxis?.tickPosition).toBe('inline');
909
- const title = yAxis?.titlePosition;
910
- expect(title).toBeDefined();
911
- // The title is rotated -90° anchored middle; its glyph box straddles the
912
- // rotation center by ~half the title font. The left edge must stay on the
913
- // canvas (x >= 0). Assert the rotation center clears half a glyph.
914
- const halfGlyph = Math.ceil(layout.theme.fonts.sizes.body / 2);
915
- expect(title!.x - halfGlyph).toBeGreaterThanOrEqual(0);
916
- }
917
- });
918
- });