@opendata-ai/openchart-engine 8.0.0-rc.9 → 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 +804 -155
  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 -2401
  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 -630
  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 -1503
  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 -1128
  185. package/src/geo/compile-map.ts +0 -1376
  186. package/src/geo/join.ts +0 -115
  187. package/src/geo/projections.ts +0 -65
  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 -748
  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,399 +0,0 @@
1
- import type {
2
- ChartSpec,
3
- GraphSpec,
4
- LayerSpec,
5
- MapSpec,
6
- RangeAnnotation,
7
- RefLineAnnotation,
8
- SankeySpec,
9
- TableSpec,
10
- TextAnnotation,
11
- } from '@opendata-ai/openchart-core';
12
- import { describe, expect, it } from 'vitest';
13
- import type { NormalizedMapSpec } from '../../geo/types';
14
- import type { NormalizedSankeySpec } from '../../sankey/types';
15
- import { normalizeSpec } from '../normalize';
16
- import type { NormalizedChartSpec, NormalizedGraphSpec, NormalizedTableSpec } from '../types';
17
-
18
- // ---------------------------------------------------------------------------
19
- // Test data
20
- // ---------------------------------------------------------------------------
21
-
22
- const lineSpec: ChartSpec = {
23
- mark: 'line',
24
- data: [
25
- { date: '2020-01-01', value: 10, country: 'US' },
26
- { date: '2021-01-01', value: 20, country: 'UK' },
27
- ],
28
- encoding: {
29
- x: { field: 'date', type: 'temporal' },
30
- y: { field: 'value', type: 'quantitative' },
31
- },
32
- chrome: {
33
- title: 'GDP Growth',
34
- subtitle: { text: 'Over time', style: { fontSize: 16 } },
35
- },
36
- };
37
-
38
- // ---------------------------------------------------------------------------
39
- // Tests
40
- // ---------------------------------------------------------------------------
41
-
42
- describe('normalizeSpec', () => {
43
- describe('chart spec normalization', () => {
44
- it('fills default values for optionals', () => {
45
- const warnings: string[] = [];
46
- const result = normalizeSpec(lineSpec, warnings) as NormalizedChartSpec;
47
-
48
- expect(result.responsive).toBe(true);
49
- expect(result.darkMode).toBe('off');
50
- expect(result.annotations).toEqual([]);
51
- expect(result.theme).toEqual({});
52
- expect(result.labels).toEqual({
53
- density: 'auto',
54
- format: '',
55
- prefix: '',
56
- offsets: undefined,
57
- });
58
- });
59
-
60
- it('watermark defaults to true when not specified', () => {
61
- const result = normalizeSpec(lineSpec) as NormalizedChartSpec;
62
- expect(result.watermark).toBe(true);
63
- });
64
-
65
- it('watermark respects explicit false', () => {
66
- const spec: ChartSpec = { ...lineSpec, watermark: false };
67
- const result = normalizeSpec(spec) as NormalizedChartSpec;
68
- expect(result.watermark).toBe(false);
69
- });
70
-
71
- it('preserves explicit values', () => {
72
- const spec: ChartSpec = {
73
- ...lineSpec,
74
- responsive: false,
75
- darkMode: 'force',
76
- };
77
- const result = normalizeSpec(spec) as NormalizedChartSpec;
78
-
79
- expect(result.responsive).toBe(false);
80
- expect(result.darkMode).toBe('force');
81
- });
82
-
83
- it('normalizes chrome strings to ChromeText objects', () => {
84
- const result = normalizeSpec(lineSpec) as NormalizedChartSpec;
85
-
86
- // Plain string becomes ChromeText
87
- expect(result.chrome.title).toEqual({ text: 'GDP Growth' });
88
- // ChromeText with style is preserved
89
- expect(result.chrome.subtitle).toEqual({
90
- text: 'Over time',
91
- style: { fontSize: 16 },
92
- });
93
- // Undefined fields stay undefined
94
- expect(result.chrome.source).toBeUndefined();
95
- });
96
-
97
- it('infers encoding types from data when not specified', () => {
98
- const warnings: string[] = [];
99
- const spec: ChartSpec = {
100
- mark: 'point',
101
- data: [
102
- { x: 10, y: 20 },
103
- { x: 30, y: 40 },
104
- ],
105
- encoding: {
106
- // No type specified, should be inferred as quantitative
107
- x: { field: 'x', type: 'quantitative' },
108
- // biome-ignore lint/suspicious/noExplicitAny: intentionally omitting `type` to test inference
109
- y: { field: 'y' } as any,
110
- },
111
- };
112
-
113
- const result = normalizeSpec(spec, warnings) as NormalizedChartSpec;
114
- expect(result.encoding.y?.type).toBe('quantitative');
115
- expect(warnings.some((w) => w.includes('Inferred'))).toBe(true);
116
- });
117
-
118
- it('infers temporal type from date strings', () => {
119
- const warnings: string[] = [];
120
- const spec: ChartSpec = {
121
- mark: 'line',
122
- data: [
123
- { date: '2020-01-01', value: 10 },
124
- { date: '2021-06-15', value: 20 },
125
- ],
126
- encoding: {
127
- // biome-ignore lint/suspicious/noExplicitAny: intentionally omitting `type` to test inference
128
- x: { field: 'date' } as any,
129
- y: { field: 'value', type: 'quantitative' },
130
- },
131
- };
132
-
133
- const result = normalizeSpec(spec, warnings) as NormalizedChartSpec;
134
- expect(result.encoding.x?.type).toBe('temporal');
135
- });
136
-
137
- it('warns on type mismatch (temporal declared as nominal)', () => {
138
- const warnings: string[] = [];
139
- const spec: ChartSpec = {
140
- mark: 'line',
141
- data: [
142
- { date: '2020-01-01', value: 10 },
143
- { date: '2021-06-15', value: 20 },
144
- ],
145
- encoding: {
146
- x: { field: 'date', type: 'nominal' },
147
- y: { field: 'value', type: 'quantitative' },
148
- },
149
- };
150
-
151
- normalizeSpec(spec, warnings);
152
- expect(warnings.some((w) => w.includes('looks temporal but was declared as nominal'))).toBe(
153
- true,
154
- );
155
- });
156
-
157
- it('preserves explicit label config', () => {
158
- const spec: ChartSpec = {
159
- ...lineSpec,
160
- labels: { density: 'none', format: ',.0f' },
161
- };
162
- const result = normalizeSpec(spec) as NormalizedChartSpec;
163
- expect(result.labels).toEqual({
164
- density: 'none',
165
- format: ',.0f',
166
- prefix: '',
167
- offsets: undefined,
168
- });
169
- });
170
-
171
- it('fills partial label config with defaults', () => {
172
- const spec: ChartSpec = {
173
- ...lineSpec,
174
- labels: { density: 'endpoints' },
175
- };
176
- const result = normalizeSpec(spec) as NormalizedChartSpec;
177
- expect(result.labels).toEqual({
178
- density: 'endpoints',
179
- format: '',
180
- prefix: '',
181
- offsets: undefined,
182
- });
183
- });
184
-
185
- it('normalizes annotations with default styles', () => {
186
- const spec: ChartSpec = {
187
- ...lineSpec,
188
- annotations: [
189
- { type: 'refline', y: 15 },
190
- { type: 'text', x: '2020-01-01', y: 10, text: 'Start' },
191
- { type: 'range', y1: 10, y2: 20 },
192
- ],
193
- };
194
-
195
- const result = normalizeSpec(spec) as NormalizedChartSpec;
196
- const refline = result.annotations[0] as RefLineAnnotation;
197
- expect(refline.style).toBe('dashed');
198
- expect(refline.strokeWidth).toBe(1);
199
-
200
- // Normalize must NOT stamp typography onto text annotations. The
201
- // annotation layer owns fontSize (DEFAULT_ANNOTATION_FONT_SIZE) and
202
- // fontWeight (the lede rule, which promotes a subtitle-bearing primary
203
- // line to bold only when the author left the weight unset). Defaulting
204
- // them here makes `fontWeight` always defined, so the lede rule can
205
- // never fire and the size default can never apply.
206
- const text = result.annotations[1] as TextAnnotation;
207
- expect(text.fontSize).toBeUndefined();
208
- expect(text.fontWeight).toBeUndefined();
209
- expect(text.opacity).toBe(1);
210
-
211
- const range = result.annotations[2] as RangeAnnotation;
212
- expect(range.opacity).toBe(0.1);
213
- });
214
-
215
- it('preserves author-set annotation typography through normalization', () => {
216
- const spec: ChartSpec = {
217
- ...lineSpec,
218
- annotations: [
219
- { type: 'text', x: '2020-01-01', y: 10, text: 'Start', fontSize: 18, fontWeight: 300 },
220
- ],
221
- };
222
-
223
- const text = (normalizeSpec(spec) as NormalizedChartSpec).annotations[0] as TextAnnotation;
224
- expect(text.fontSize).toBe(18);
225
- expect(text.fontWeight).toBe(300);
226
- });
227
- });
228
-
229
- describe('table spec normalization', () => {
230
- it('fills default values', () => {
231
- const spec: TableSpec = {
232
- type: 'table',
233
- data: [{ name: 'Alice', age: 30 }],
234
- columns: [{ key: 'name' }, { key: 'age' }],
235
- };
236
-
237
- const result = normalizeSpec(spec) as NormalizedTableSpec;
238
- expect(result.search).toBe(false);
239
- expect(result.pagination).toBe(false);
240
- expect(result.stickyFirstColumn).toBe(false);
241
- expect(result.compact).toBe(false);
242
- expect(result.responsive).toBe(true);
243
- expect(result.darkMode).toBe('off');
244
- });
245
-
246
- it('watermark defaults to true when not specified', () => {
247
- const spec: TableSpec = {
248
- type: 'table',
249
- data: [{ name: 'Alice', age: 30 }],
250
- columns: [{ key: 'name' }, { key: 'age' }],
251
- };
252
- const result = normalizeSpec(spec) as NormalizedTableSpec;
253
- expect(result.watermark).toBe(true);
254
- });
255
-
256
- it('watermark respects explicit false', () => {
257
- const spec: TableSpec = {
258
- type: 'table',
259
- data: [{ name: 'Alice', age: 30 }],
260
- columns: [{ key: 'name' }, { key: 'age' }],
261
- watermark: false,
262
- };
263
- const result = normalizeSpec(spec) as NormalizedTableSpec;
264
- expect(result.watermark).toBe(false);
265
- });
266
- });
267
-
268
- describe('graph spec normalization', () => {
269
- it('fills default values', () => {
270
- const spec: GraphSpec = {
271
- type: 'graph',
272
- nodes: [{ id: 'a' }, { id: 'b' }],
273
- edges: [{ source: 'a', target: 'b' }],
274
- };
275
-
276
- const result = normalizeSpec(spec) as NormalizedGraphSpec;
277
- expect(result.encoding).toEqual({});
278
- expect(result.layout).toEqual({ type: 'force', chargeStrength: -300, linkDistance: 30 });
279
- expect(result.annotations).toEqual([]);
280
- expect(result.darkMode).toBe('off');
281
- });
282
-
283
- it('watermark defaults to true when not specified', () => {
284
- const spec: GraphSpec = {
285
- type: 'graph',
286
- nodes: [{ id: 'a' }, { id: 'b' }],
287
- edges: [{ source: 'a', target: 'b' }],
288
- };
289
- const result = normalizeSpec(spec) as NormalizedGraphSpec;
290
- expect(result.watermark).toBe(true);
291
- });
292
-
293
- it('watermark respects explicit false', () => {
294
- const spec: GraphSpec = {
295
- type: 'graph',
296
- nodes: [{ id: 'a' }, { id: 'b' }],
297
- edges: [{ source: 'a', target: 'b' }],
298
- watermark: false,
299
- };
300
- const result = normalizeSpec(spec) as NormalizedGraphSpec;
301
- expect(result.watermark).toBe(false);
302
- });
303
- });
304
-
305
- describe('sankey spec normalization', () => {
306
- const baseSankeySpec: SankeySpec = {
307
- type: 'sankey',
308
- data: [{ source: 'A', target: 'B', value: 10 }],
309
- encoding: {
310
- source: { field: 'source' },
311
- target: { field: 'target' },
312
- value: { field: 'value' },
313
- },
314
- };
315
-
316
- it('watermark defaults to true when not specified', () => {
317
- const result = normalizeSpec(baseSankeySpec) as NormalizedSankeySpec;
318
- expect(result.watermark).toBe(true);
319
- });
320
-
321
- it('watermark respects explicit false', () => {
322
- const spec: SankeySpec = { ...baseSankeySpec, watermark: false };
323
- const result = normalizeSpec(spec) as NormalizedSankeySpec;
324
- expect(result.watermark).toBe(false);
325
- });
326
- });
327
-
328
- describe('layer spec normalization', () => {
329
- const baseLeaf: ChartSpec = {
330
- mark: 'line',
331
- data: [{ x: 1, y: 2 }],
332
- encoding: {
333
- x: { field: 'x', type: 'quantitative' },
334
- y: { field: 'y', type: 'quantitative' },
335
- },
336
- };
337
-
338
- it('watermark defaults to true for layer leaves', () => {
339
- const layerSpec: LayerSpec = { layer: [baseLeaf] };
340
- const result = normalizeSpec(layerSpec) as NormalizedChartSpec;
341
- expect(result.watermark).toBe(true);
342
- });
343
-
344
- it('layer-level watermark: false propagates to leaves', () => {
345
- const layerSpec: LayerSpec = { layer: [baseLeaf], watermark: false };
346
- const result = normalizeSpec(layerSpec) as NormalizedChartSpec;
347
- expect(result.watermark).toBe(false);
348
- });
349
-
350
- it('leaf-level watermark overrides layer-level', () => {
351
- const leaf: ChartSpec = { ...baseLeaf, watermark: true };
352
- const layerSpec: LayerSpec = { layer: [leaf], watermark: false };
353
- const result = normalizeSpec(layerSpec) as NormalizedChartSpec;
354
- // Leaf explicitly sets true, which should be preserved
355
- expect(result.watermark).toBe(true);
356
- });
357
- });
358
-
359
- describe('map spec normalization', () => {
360
- const mapSpec: MapSpec = {
361
- type: 'map',
362
- geo: {
363
- features: {
364
- type: 'Topology',
365
- objects: { x: { type: 'GeometryCollection', geometries: [] } },
366
- arcs: [],
367
- },
368
- },
369
- data: [],
370
- encoding: {
371
- key: { field: 'id', type: 'nominal' },
372
- color: { field: 'value', type: 'quantitative' },
373
- },
374
- };
375
-
376
- it('adds focus: null when not specified', () => {
377
- const result = normalizeSpec(mapSpec) as NormalizedMapSpec;
378
- expect(result.geo.focus).toBeNull();
379
- });
380
-
381
- it('preserves explicit focus value', () => {
382
- const specWithFocus: MapSpec = {
383
- ...mapSpec,
384
- geo: { ...mapSpec.geo, focus: '36' },
385
- };
386
- const result = normalizeSpec(specWithFocus) as NormalizedMapSpec;
387
- expect(result.geo.focus).toBe('36');
388
- });
389
-
390
- it('preserves focus: null (explicit clear)', () => {
391
- const specWithNull: MapSpec = {
392
- ...mapSpec,
393
- geo: { ...mapSpec.geo, focus: null },
394
- };
395
- const result = normalizeSpec(specWithNull) as NormalizedMapSpec;
396
- expect(result.geo.focus).toBeNull();
397
- });
398
- });
399
- });
@@ -1,153 +0,0 @@
1
- /**
2
- * Tests for sparkline default resolution.
3
- *
4
- * Focus: the trend signal must be honest about noisy series and short
5
- * series. Naive `last - first` would mislabel `[100, 50, 200, 60, 101]`
6
- * as up; a regression slope with deadband correctly reads it as neutral.
7
- */
8
-
9
- import type { MarkDef, ResolvedTheme } from '@opendata-ai/openchart-core';
10
- import { describe, expect, it } from 'vitest';
11
- import {
12
- buildSparklineAreaGradient,
13
- computeTrend,
14
- computeTrendFromData,
15
- hasExplicitColor,
16
- trendColor,
17
- } from '../sparkline-defaults';
18
-
19
- const baseTheme = {
20
- colors: {
21
- categorical: ['#1b7fa3', '#aaa'],
22
- positive: '#16a34a',
23
- negative: '#dc2626',
24
- },
25
- } as unknown as ResolvedTheme;
26
-
27
- describe('computeTrend', () => {
28
- it('classifies clear up-trend as "up"', () => {
29
- expect(computeTrend([1, 2, 3, 4, 5])).toBe('up');
30
- });
31
-
32
- it('classifies clear down-trend as "down"', () => {
33
- expect(computeTrend([5, 4, 3, 2, 1])).toBe('down');
34
- });
35
-
36
- it('classifies flat series as "neutral"', () => {
37
- expect(computeTrend([10, 10, 10, 10])).toBe('neutral');
38
- });
39
-
40
- it('classifies noisy non-monotonic series with no meaningful net trend as "neutral"', () => {
41
- // Symmetric oscillation around the mean — the regression slope works
42
- // out to (almost) zero. Naive `last - first` heuristics would classify
43
- // this incorrectly; the regression sees no net direction.
44
- // Palindromic series — mirrors around the midpoint, so the regression
45
- // slope is mathematically zero regardless of the noise amplitude.
46
- expect(computeTrend([100, 105, 95, 102, 95, 105, 100])).toBe('neutral');
47
- });
48
-
49
- it('classifies a real trend with noise as the net direction', () => {
50
- // ~10% net rise over 20 steps with realistic chop — should still read up.
51
- const series = [
52
- 100, 102, 99, 103, 101, 104, 103, 106, 104, 107, 105, 108, 107, 109, 108, 110, 109, 111, 110,
53
- 112,
54
- ];
55
- expect(computeTrend(series)).toBe('up');
56
- });
57
-
58
- it('returns "neutral" for empty series', () => {
59
- expect(computeTrend([])).toBe('neutral');
60
- });
61
-
62
- it('returns "neutral" for single-point series', () => {
63
- expect(computeTrend([42])).toBe('neutral');
64
- });
65
-
66
- it('returns "neutral" when all values are non-finite', () => {
67
- expect(computeTrend([NaN, NaN, Infinity])).toBe('neutral');
68
- });
69
-
70
- it('skips non-finite values and classifies remaining', () => {
71
- expect(computeTrend([NaN, 1, 2, 3, 4, NaN])).toBe('up');
72
- });
73
-
74
- it('treats very small relative slope as "neutral" (deadband)', () => {
75
- // Slope is real but tiny relative to the mean.
76
- expect(computeTrend([1000, 1000.5, 1001, 1001.2])).toBe('neutral');
77
- });
78
- });
79
-
80
- describe('computeTrendFromData', () => {
81
- it('reads y values from data rows', () => {
82
- const data = [
83
- { date: 'a', value: 1 },
84
- { date: 'b', value: 2 },
85
- { date: 'c', value: 3 },
86
- ];
87
- expect(computeTrendFromData(data, 'value')).toBe('up');
88
- });
89
-
90
- it('returns "neutral" when yField is missing', () => {
91
- expect(computeTrendFromData([{ a: 1 }, { a: 2 }], undefined)).toBe('neutral');
92
- });
93
-
94
- it('coerces string numbers and skips non-numeric values', () => {
95
- const data = [{ v: '5' }, { v: 'not a number' }, { v: '4' }, { v: '3' }];
96
- expect(computeTrendFromData(data, 'v')).toBe('down');
97
- });
98
- });
99
-
100
- describe('trendColor', () => {
101
- it('returns positive for up', () => {
102
- expect(trendColor('up', baseTheme)).toBe('#16a34a');
103
- });
104
- it('returns negative for down', () => {
105
- expect(trendColor('down', baseTheme)).toBe('#dc2626');
106
- });
107
- it('returns palette[0] for neutral', () => {
108
- expect(trendColor('neutral', baseTheme)).toBe('#1b7fa3');
109
- });
110
- });
111
-
112
- describe('hasExplicitColor', () => {
113
- it('returns both false when markDef has no fill/stroke and no color encoding', () => {
114
- expect(hasExplicitColor({ type: 'line' }, false)).toEqual({ fill: false, stroke: false });
115
- });
116
- it('returns fill=true when markDef.fill is set, leaves stroke false', () => {
117
- expect(hasExplicitColor({ type: 'line', fill: '#ff00ff' } as MarkDef, false)).toEqual({
118
- fill: true,
119
- stroke: false,
120
- });
121
- });
122
- it('returns stroke=true when markDef.stroke is set, leaves fill false', () => {
123
- expect(hasExplicitColor({ type: 'line', stroke: '#ff00ff' } as MarkDef, false)).toEqual({
124
- fill: false,
125
- stroke: true,
126
- });
127
- });
128
- it('returns both true when both markDef.fill and markDef.stroke are set', () => {
129
- expect(
130
- hasExplicitColor({ type: 'line', fill: '#ff00ff', stroke: '#00ffff' } as MarkDef, false),
131
- ).toEqual({ fill: true, stroke: true });
132
- });
133
- it('returns both true when encoding.color is present (color scale drives both)', () => {
134
- expect(hasExplicitColor({ type: 'line' }, true)).toEqual({ fill: true, stroke: true });
135
- });
136
- });
137
-
138
- describe('buildSparklineAreaGradient', () => {
139
- it('builds a top-to-bottom gradient with 0.35 -> 0 opacity in the trend color', () => {
140
- const grad = buildSparklineAreaGradient('#16a34a');
141
- expect(grad).toEqual({
142
- gradient: 'linear',
143
- x1: 0,
144
- y1: 0,
145
- x2: 0,
146
- y2: 1,
147
- stops: [
148
- { offset: 0, color: '#16a34a', opacity: 0.35 },
149
- { offset: 1, color: '#16a34a', opacity: 0 },
150
- ],
151
- });
152
- });
153
- });