@opendata-ai/openchart-vanilla 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 (147) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/{chunk-UZ7J3NOF.js → chunk-6BUCYCLD.js} +7314 -5605
  3. package/dist/chunk-6BUCYCLD.js.map +1 -0
  4. package/dist/chunk-DCODCLWI.js +234 -0
  5. package/dist/chunk-DCODCLWI.js.map +1 -0
  6. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  7. package/dist/chunk-EKAUQAAX.js.map +1 -0
  8. package/dist/chunk-RM5XS6NR.js +113 -0
  9. package/dist/chunk-RM5XS6NR.js.map +1 -0
  10. package/dist/chunk-T32UHMZA.js +282 -0
  11. package/dist/chunk-T32UHMZA.js.map +1 -0
  12. package/dist/export-gif.d.ts +113 -0
  13. package/dist/export-gif.js +21 -0
  14. package/dist/export-gif.js.map +1 -0
  15. package/dist/index.d.ts +309 -27
  16. package/dist/index.js +2020 -836
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-CL-nA0f-.d.ts} +85 -2
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.d.ts +2 -0
  21. package/dist/static.js +8 -69
  22. package/dist/static.js.map +1 -1
  23. package/dist/story/index.d.ts +25 -9
  24. package/dist/story/index.js +9 -7
  25. package/dist/story/index.js.map +1 -1
  26. package/dist/styles.css +1 -1
  27. package/dist/styles.css.map +1 -1
  28. package/package.json +14 -6
  29. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  30. package/dist/chunk-UZ7J3NOF.js.map +0 -1
  31. package/src/__test-fixtures__/dom.ts +0 -70
  32. package/src/__test-fixtures__/specs.ts +0 -214
  33. package/src/__tests__/animation.test.ts +0 -358
  34. package/src/__tests__/annotations-render.test.ts +0 -201
  35. package/src/__tests__/compound-labels.test.ts +0 -122
  36. package/src/__tests__/continuous-legend.test.ts +0 -91
  37. package/src/__tests__/crosshair.test.ts +0 -130
  38. package/src/__tests__/edit-events.test.ts +0 -950
  39. package/src/__tests__/events.test.ts +0 -387
  40. package/src/__tests__/export.test.ts +0 -241
  41. package/src/__tests__/font-race.test.ts +0 -199
  42. package/src/__tests__/gradient-ids.test.ts +0 -159
  43. package/src/__tests__/gradient-utils.test.ts +0 -125
  44. package/src/__tests__/known-bugs-render.test.ts +0 -183
  45. package/src/__tests__/map.test.ts +0 -636
  46. package/src/__tests__/mount.test.ts +0 -357
  47. package/src/__tests__/pattern-fills.test.ts +0 -105
  48. package/src/__tests__/resize-timing.test.ts +0 -218
  49. package/src/__tests__/sankey.test.ts +0 -133
  50. package/src/__tests__/selection-events.test.ts +0 -869
  51. package/src/__tests__/series-search.test.ts +0 -311
  52. package/src/__tests__/static.test.ts +0 -141
  53. package/src/__tests__/svg-renderer.test.ts +0 -895
  54. package/src/__tests__/table-keyboard.test.ts +0 -361
  55. package/src/__tests__/table-mount.test.ts +0 -639
  56. package/src/__tests__/tilemap.test.ts +0 -158
  57. package/src/__tests__/tooltip.test.ts +0 -337
  58. package/src/__tests__/transition.test.ts +0 -1792
  59. package/src/__tests__/update-scrolly-matrix.test.ts +0 -563
  60. package/src/__tests__/you-draw-it.test.ts +0 -273
  61. package/src/animation-vars.ts +0 -42
  62. package/src/animation.ts +0 -76
  63. package/src/barlist-mount.ts +0 -383
  64. package/src/barlist-renderer.ts +0 -267
  65. package/src/dom-helpers.ts +0 -17
  66. package/src/export.ts +0 -482
  67. package/src/gradient-utils.ts +0 -138
  68. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  69. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  70. package/src/graph/__tests__/interaction.test.ts +0 -205
  71. package/src/graph/__tests__/keyboard.test.ts +0 -653
  72. package/src/graph/__tests__/search.test.ts +0 -88
  73. package/src/graph/__tests__/simulation.test.ts +0 -233
  74. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  75. package/src/graph/__tests__/zoom.test.ts +0 -203
  76. package/src/graph/canvas-renderer.ts +0 -747
  77. package/src/graph/interaction.ts +0 -367
  78. package/src/graph/keyboard.ts +0 -208
  79. package/src/graph/search.ts +0 -50
  80. package/src/graph/simulation-worker-url.ts +0 -12
  81. package/src/graph/simulation-worker.ts +0 -278
  82. package/src/graph/simulation.ts +0 -422
  83. package/src/graph/spatial-index.ts +0 -121
  84. package/src/graph/types.ts +0 -47
  85. package/src/graph/worker-protocol.ts +0 -70
  86. package/src/graph/zoom.ts +0 -134
  87. package/src/graph-mount.ts +0 -888
  88. package/src/index.ts +0 -76
  89. package/src/interactions/chart-events.ts +0 -139
  90. package/src/interactions/crosshair.ts +0 -233
  91. package/src/interactions/drag-handler.ts +0 -175
  92. package/src/interactions/editing-drags.ts +0 -515
  93. package/src/interactions/index.ts +0 -25
  94. package/src/interactions/keyboard-nav.ts +0 -111
  95. package/src/interactions/legend-interaction.ts +0 -46
  96. package/src/interactions/selection.ts +0 -280
  97. package/src/interactions/tooltip-events.ts +0 -72
  98. package/src/map-camera.ts +0 -111
  99. package/src/map-mount.ts +0 -792
  100. package/src/map-renderer.ts +0 -460
  101. package/src/map-transition.ts +0 -99
  102. package/src/measure-text.ts +0 -134
  103. package/src/mount.ts +0 -1382
  104. package/src/pattern-utils.ts +0 -110
  105. package/src/renderers/annotations.ts +0 -388
  106. package/src/renderers/axes.ts +0 -351
  107. package/src/renderers/brand.ts +0 -87
  108. package/src/renderers/chrome.ts +0 -196
  109. package/src/renderers/endpoint-labels.ts +0 -150
  110. package/src/renderers/legend.ts +0 -311
  111. package/src/renderers/marks.ts +0 -561
  112. package/src/renderers/metrics.ts +0 -63
  113. package/src/renderers/svg-dom.ts +0 -40
  114. package/src/renderers/table-cells.ts +0 -446
  115. package/src/resize-observer.ts +0 -46
  116. package/src/sankey-mount.ts +0 -622
  117. package/src/sankey-renderer.ts +0 -614
  118. package/src/series-search.ts +0 -392
  119. package/src/static.ts +0 -251
  120. package/src/story/__tests__/camera-math.test.ts +0 -145
  121. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  122. package/src/story/__tests__/progress-math.test.ts +0 -95
  123. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  124. package/src/story/__tests__/tween.test.ts +0 -159
  125. package/src/story/camera-math.ts +0 -125
  126. package/src/story/create-chart-story.ts +0 -353
  127. package/src/story/crossfade.ts +0 -70
  128. package/src/story/index.ts +0 -39
  129. package/src/story/progress-math.ts +0 -85
  130. package/src/story/resolve-camera-target.ts +0 -119
  131. package/src/story/scroll-driver.ts +0 -206
  132. package/src/story/story-camera.ts +0 -49
  133. package/src/story/tween.ts +0 -112
  134. package/src/story/types.ts +0 -76
  135. package/src/svg-ids.ts +0 -23
  136. package/src/svg-renderer.ts +0 -383
  137. package/src/table-keyboard.ts +0 -266
  138. package/src/table-mount.ts +0 -567
  139. package/src/table-renderer.ts +0 -414
  140. package/src/text-edit-overlay.ts +0 -255
  141. package/src/theme-tokens.ts +0 -51
  142. package/src/tilemap-mount.ts +0 -511
  143. package/src/tilemap-renderer.ts +0 -396
  144. package/src/tooltip.ts +0 -177
  145. package/src/transition.ts +0 -2816
  146. package/src/you-draw-it.ts +0 -480
  147. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,201 +0,0 @@
1
- /**
2
- * Annotation connector + marker rendering.
3
- *
4
- * Covers the Phase 2 redesign contract: one quadratic curve, an open-V stroked
5
- * arrowhead (not a filled triangle), exactly one endpoint marker per annotated
6
- * point, and the two connector voices (ink for arrowed emphasis, gray hairline
7
- * for quiet leaders).
8
- */
9
-
10
- import type { Annotation, ChartSpec, CompileOptions } from '@opendata-ai/openchart-engine';
11
- import { compileChart } from '@opendata-ai/openchart-engine';
12
- import { afterEach, describe, expect, it } from 'vitest';
13
- import { createContainer } from '../__test-fixtures__/dom';
14
- import { renderChartSVG } from '../svg-renderer';
15
-
16
- const COMPILE_OPTS: CompileOptions = { width: 600, height: 400 };
17
-
18
- function renderAnnotated(annotations: Annotation[]) {
19
- const spec: ChartSpec = {
20
- mark: 'line',
21
- data: [
22
- { date: '2020-01-01', value: 10 },
23
- { date: '2020-02-01', value: 30 },
24
- { date: '2020-03-01', value: 20 },
25
- ],
26
- encoding: {
27
- x: { field: 'date', type: 'temporal' },
28
- y: { field: 'value', type: 'quantitative' },
29
- },
30
- annotations,
31
- };
32
- const container = createContainer(COMPILE_OPTS.width, COMPILE_OPTS.height);
33
- const layout = compileChart(spec, COMPILE_OPTS);
34
- renderChartSVG(layout, container);
35
- return { container, layout };
36
- }
37
-
38
- afterEach(() => {
39
- document.body.innerHTML = '';
40
- });
41
-
42
- describe('annotation connectors', () => {
43
- it('renders the curve as a single quadratic path', () => {
44
- const { container } = renderAnnotated([
45
- {
46
- type: 'text',
47
- x: '2020-02-01',
48
- y: 30,
49
- text: 'Peak',
50
- connector: 'curve',
51
- offset: { dx: 60, dy: -50 },
52
- },
53
- ]);
54
-
55
- const path = container.querySelector('path.oc-annotation-connector');
56
- expect(path).not.toBeNull();
57
- const d = path!.getAttribute('d') ?? '';
58
- expect(d).toMatch(/^M [\d.-]+ [\d.-]+ Q /);
59
- expect(d).not.toContain('C ');
60
- });
61
-
62
- it('renders the arrowhead as a stroked open-V polyline, not a filled polygon', () => {
63
- const { container } = renderAnnotated([
64
- {
65
- type: 'text',
66
- x: '2020-02-01',
67
- y: 30,
68
- text: 'Peak',
69
- connector: 'curve',
70
- offset: { dx: 60, dy: -50 },
71
- },
72
- ]);
73
-
74
- expect(container.querySelector('polygon.oc-annotation-connector')).toBeNull();
75
-
76
- const arrow = container.querySelector('polyline.oc-annotation-connector');
77
- expect(arrow).not.toBeNull();
78
- expect(arrow!.getAttribute('fill')).toBe('none');
79
- expect(arrow!.getAttribute('stroke-linecap')).toBe('round');
80
- // "baseLeft tip baseRight" — three points, so the V opens away from the tip.
81
- expect((arrow!.getAttribute('points') ?? '').split(' ')).toHaveLength(3);
82
- });
83
-
84
- it('draws exactly one endpoint marker (the old bullseye pair is gone)', () => {
85
- const { container } = renderAnnotated([
86
- {
87
- type: 'text',
88
- x: '2020-02-01',
89
- y: 30,
90
- text: 'Peak',
91
- dot: true,
92
- offset: { dx: 60, dy: -50 },
93
- },
94
- ]);
95
-
96
- // The bullseye was a ring circle with a filled dot circle stacked on the same
97
- // centre. Asserting on `.oc-annotation-endpoint-ring`/`-dot` proved nothing:
98
- // the renderer never emitted those classes, so querySelector returned null no
99
- // matter what it drew. Count the circles the renderer actually puts at the
100
- // data point instead, and pin the single survivor to the open-ring look.
101
- const layer = container.querySelector('g.oc-annotations')!;
102
- const dots = [...layer.querySelectorAll('circle')];
103
- expect(dots).toHaveLength(1);
104
-
105
- const dot = dots[0];
106
- expect(dot.getAttribute('class')).toBe('oc-annotation-dot');
107
- // Open ring, not a filled bullseye centre: the interior takes the surface fill
108
- // and the colour lives in the stroke.
109
- expect(dot.getAttribute('fill')).toBe('#ffffff');
110
- expect(dot.getAttribute('stroke')).toBe('#8f8f8f');
111
- expect(Number(dot.getAttribute('stroke-width'))).toBeGreaterThan(0);
112
- });
113
-
114
- it('gives the arrowed curve the label ink and the plain leader a gray hairline', () => {
115
- const { layout: arrowed } = renderAnnotated([
116
- {
117
- type: 'text',
118
- x: '2020-02-01',
119
- y: 30,
120
- text: 'Peak',
121
- connector: 'curve',
122
- offset: { dx: 60, dy: -50 },
123
- },
124
- ]);
125
- const { layout: quiet } = renderAnnotated([
126
- {
127
- type: 'text',
128
- x: '2020-02-01',
129
- y: 30,
130
- text: 'Peak',
131
- connector: 'straight',
132
- offset: { dx: 60, dy: -50 },
133
- },
134
- ]);
135
-
136
- const arrowConnector = arrowed.annotations[0].label?.connector;
137
- const quietConnector = quiet.annotations[0].label?.connector;
138
- expect(arrowConnector?.stroke).toBe(arrowed.annotations[0].label?.style.fill);
139
- expect(quietConnector?.stroke).not.toBe(quiet.annotations[0].label?.style.fill);
140
- // The quiet leader and its marker read as one system.
141
- expect(quietConnector?.stroke).toBe(quiet.annotations[0].dot?.stroke);
142
- });
143
- });
144
-
145
- describe('annotation rich text', () => {
146
- it('emits one tspan per span and bolds only the marked run', () => {
147
- const { container } = renderAnnotated([
148
- { type: 'text', x: '2020-02-01', y: 30, text: 'Inflation peaked at **8.5%**' },
149
- ]);
150
-
151
- const spans = [...container.querySelectorAll('text.oc-annotation-label tspan')];
152
- expect(spans.map((s) => s.textContent)).toEqual(['Inflation peaked at ', '8.5%']);
153
- expect(spans[0].getAttribute('font-weight')).toBeNull();
154
- expect(spans[1].getAttribute('font-weight')).toBe('700');
155
- // The delimiters are syntax, never glyphs.
156
- expect(container.querySelector('text.oc-annotation-label')!.textContent).not.toContain('**');
157
- });
158
-
159
- it('composes multi-line with bold spans, resetting x on each new line', () => {
160
- const { container } = renderAnnotated([
161
- { type: 'text', x: '2020-02-01', y: 30, text: 'Line **one**\n**Line** two' },
162
- ]);
163
-
164
- const spans = [...container.querySelectorAll('text.oc-annotation-label tspan')];
165
- expect(spans.map((s) => s.textContent)).toEqual(['Line ', 'one', 'Line', ' two']);
166
- // First span of each line carries the x reset; continuation spans must not.
167
- expect(spans[0].getAttribute('x')).not.toBeNull();
168
- expect(spans[1].getAttribute('x')).toBeNull();
169
- expect(spans[2].getAttribute('x')).not.toBeNull();
170
- expect(spans[3].getAttribute('x')).toBeNull();
171
- // Only the second line advances.
172
- expect(spans[0].getAttribute('dy')).toBe('0');
173
- expect(Number(spans[2].getAttribute('dy'))).toBeGreaterThan(0);
174
- });
175
-
176
- it('parses bold spans in the subtitle too', () => {
177
- const { container } = renderAnnotated([
178
- {
179
- type: 'text',
180
- x: '2020-02-01',
181
- y: 30,
182
- text: 'Peak',
183
- subtitle: 'Revised **upward** in March',
184
- },
185
- ]);
186
-
187
- const spans = [...container.querySelectorAll('text.oc-annotation-subtitle tspan')];
188
- expect(spans.map((s) => s.textContent)).toEqual(['Revised ', 'upward', ' in March']);
189
- expect(spans[1].getAttribute('font-weight')).toBe('700');
190
- });
191
-
192
- it('renders an unmatched delimiter literally', () => {
193
- const { container } = renderAnnotated([
194
- { type: 'text', x: '2020-02-01', y: 30, text: 'Up **50% YoY' },
195
- ]);
196
-
197
- const label = container.querySelector('text.oc-annotation-label')!;
198
- expect(label.textContent).toBe('Up **50% YoY');
199
- expect(label.querySelector('tspan[font-weight]')).toBeNull();
200
- });
201
- });
@@ -1,122 +0,0 @@
1
- import type { ChartSpec } from '@opendata-ai/openchart-engine';
2
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
3
- import { createContainer } from '../__test-fixtures__/dom';
4
- import { createChart } from '../mount';
5
-
6
- // ---------------------------------------------------------------------------
7
- // Fixtures
8
- // ---------------------------------------------------------------------------
9
-
10
- const albumData = [
11
- { album: 'Abbey Road', artist: 'The Beatles', sales: 31 },
12
- { album: 'Thriller', artist: 'Michael Jackson', sales: 66 },
13
- { album: 'Back in Black', artist: 'AC/DC', sales: 50 },
14
- ];
15
-
16
- const barWithLabelField: ChartSpec = {
17
- mark: 'bar',
18
- data: albumData,
19
- encoding: {
20
- x: { field: 'sales', type: 'quantitative' },
21
- y: {
22
- field: 'album',
23
- type: 'nominal',
24
- axis: { labelField: 'artist' },
25
- },
26
- },
27
- };
28
-
29
- const barWithoutLabelField: ChartSpec = {
30
- mark: 'bar',
31
- data: albumData,
32
- encoding: {
33
- x: { field: 'sales', type: 'quantitative' },
34
- y: { field: 'album', type: 'nominal' },
35
- },
36
- };
37
-
38
- // ---------------------------------------------------------------------------
39
- // Tests
40
- // ---------------------------------------------------------------------------
41
-
42
- describe('compound axis labels rendering', () => {
43
- let container: HTMLDivElement;
44
-
45
- beforeEach(() => {
46
- container = createContainer();
47
- });
48
-
49
- afterEach(() => {
50
- document.body.innerHTML = '';
51
- });
52
-
53
- it('renders tspan elements when subtitle is present', () => {
54
- const chart = createChart(container, barWithLabelField);
55
-
56
- const yAxisTicks = container.querySelectorAll('.oc-axis-y .oc-axis-tick');
57
- expect(yAxisTicks.length).toBeGreaterThan(0);
58
-
59
- // At least one tick should have tspan children (compound label)
60
- let hasTspan = false;
61
- for (const tick of yAxisTicks) {
62
- const tspans = tick.querySelectorAll('tspan');
63
- if (tspans.length > 0) {
64
- hasTspan = true;
65
- break;
66
- }
67
- }
68
- expect(hasTspan).toBe(true);
69
-
70
- chart.destroy();
71
- });
72
-
73
- it('primary tspan has the tick label font-weight', () => {
74
- const chart = createChart(container, barWithLabelField);
75
-
76
- const yAxisTicks = container.querySelectorAll('.oc-axis-y .oc-axis-tick');
77
- for (const tick of yAxisTicks) {
78
- const tspans = tick.querySelectorAll('tspan');
79
- if (tspans.length >= 2) {
80
- const primarySpan = tspans[0];
81
- const fontWeight = primarySpan.getAttribute('font-weight');
82
- expect(fontWeight).not.toBeNull();
83
- }
84
- }
85
-
86
- chart.destroy();
87
- });
88
-
89
- it('secondary tspan has normal weight (400) and reduced opacity', () => {
90
- const chart = createChart(container, barWithLabelField);
91
-
92
- const yAxisTicks = container.querySelectorAll('.oc-axis-y .oc-axis-tick');
93
- for (const tick of yAxisTicks) {
94
- const tspans = tick.querySelectorAll('tspan');
95
- if (tspans.length >= 2) {
96
- const subtitleSpan = tspans[1];
97
- expect(subtitleSpan.getAttribute('font-weight')).toBe('400');
98
- expect(subtitleSpan.getAttribute('fill-opacity')).toBe('0.6');
99
- expect(subtitleSpan.getAttribute('dx')).toBe('0.5em');
100
- }
101
- }
102
-
103
- chart.destroy();
104
- });
105
-
106
- it('renders plain textContent without tspans when labelField is omitted', () => {
107
- const chart = createChart(container, barWithoutLabelField);
108
-
109
- const yAxisTicks = container.querySelectorAll('.oc-axis-y .oc-axis-tick');
110
- expect(yAxisTicks.length).toBeGreaterThan(0);
111
-
112
- for (const tick of yAxisTicks) {
113
- const tspans = tick.querySelectorAll('tspan');
114
- // No tspan elements when there's no subtitle
115
- expect(tspans.length).toBe(0);
116
- // Should have plain text content
117
- expect(tick.textContent).toBeTruthy();
118
- }
119
-
120
- chart.destroy();
121
- });
122
- });
@@ -1,91 +0,0 @@
1
- /**
2
- * Continuous color legend rendering: gradient bar for sequential color
3
- * scales, binned swatch row for threshold scales, opt-out via
4
- * legend: { show: false }, and shared-counter gradient IDs.
5
- */
6
-
7
- import type { ChartSpec } from '@opendata-ai/openchart-core';
8
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
9
- import { createContainer } from '../__test-fixtures__/dom';
10
- import { createChart } from '../mount';
11
-
12
- const heatmapSpec: ChartSpec = {
13
- mark: 'bar',
14
- data: [
15
- { month: 'Jan', value: 4 },
16
- { month: 'Feb', value: 25 },
17
- { month: 'Mar', value: 47 },
18
- { month: 'Apr', value: 90 },
19
- ],
20
- encoding: {
21
- x: { field: 'month', type: 'nominal' },
22
- y: { field: 'value', type: 'quantitative' },
23
- color: { field: 'value', type: 'quantitative' },
24
- },
25
- };
26
-
27
- describe('continuous color legend rendering', () => {
28
- let container: HTMLDivElement;
29
-
30
- beforeEach(() => {
31
- container = createContainer();
32
- });
33
-
34
- afterEach(() => {
35
- document.body.innerHTML = '';
36
- });
37
-
38
- it('renders a gradient bar legend by default for sequential color', () => {
39
- const chart = createChart(container, heatmapSpec);
40
-
41
- const legend = container.querySelector('.oc-legend--continuous');
42
- expect(legend).not.toBeNull();
43
-
44
- const bar = legend!.querySelector('.oc-legend-gradient-bar');
45
- expect(bar).not.toBeNull();
46
- const fill = bar!.getAttribute('fill') ?? '';
47
- const match = fill.match(/^url\(#(oc-legend-gradient-\d+)\)$/);
48
- expect(match).not.toBeNull();
49
-
50
- // The referenced gradient def exists with stops.
51
- const grad = container.querySelector(`#${match![1]}`);
52
- expect(grad).not.toBeNull();
53
- expect(grad!.querySelectorAll('stop').length).toBeGreaterThanOrEqual(2);
54
-
55
- // Min and max labels render.
56
- const labels = Array.from(legend!.querySelectorAll('text')).map((t) => t.textContent);
57
- expect(labels).toContain('4');
58
- expect(labels).toContain('90');
59
-
60
- chart.destroy();
61
- });
62
-
63
- it('renders binned swatches for a threshold scale', () => {
64
- const spec: ChartSpec = {
65
- ...heatmapSpec,
66
- encoding: {
67
- ...heatmapSpec.encoding,
68
- color: {
69
- field: 'value',
70
- type: 'quantitative',
71
- scale: { type: 'threshold', domain: [20, 40, 60, 80] },
72
- },
73
- },
74
- };
75
- const chart = createChart(container, spec);
76
-
77
- const legend = container.querySelector('.oc-legend--continuous');
78
- expect(legend).not.toBeNull();
79
- expect(legend!.querySelectorAll('.oc-legend-bin')).toHaveLength(5);
80
- const labels = Array.from(legend!.querySelectorAll('text')).map((t) => t.textContent);
81
- expect(labels).toEqual(['20', '40', '60', '80']);
82
-
83
- chart.destroy();
84
- });
85
-
86
- it('is removed by legend: { show: false }', () => {
87
- const chart = createChart(container, { ...heatmapSpec, legend: { show: false } });
88
- expect(container.querySelector('.oc-legend--continuous')).toBeNull();
89
- chart.destroy();
90
- });
91
- });
@@ -1,130 +0,0 @@
1
- import type { ChartSpec } from '@opendata-ai/openchart-engine';
2
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
3
- import { createContainer } from '../__test-fixtures__/dom';
4
- import { barSpec } from '../__test-fixtures__/specs';
5
- import { createChart } from '../mount';
6
-
7
- // ---------------------------------------------------------------------------
8
- // Fixtures
9
- // ---------------------------------------------------------------------------
10
-
11
- const lineSpecWithCrosshair: ChartSpec = {
12
- mark: 'line',
13
- data: [
14
- { date: '2020-01-01', value: 10, country: 'US' },
15
- { date: '2021-01-01', value: 40, country: 'US' },
16
- { date: '2020-01-01', value: 15, country: 'UK' },
17
- { date: '2021-01-01', value: 35, country: 'UK' },
18
- ],
19
- encoding: {
20
- x: { field: 'date', type: 'temporal' },
21
- y: { field: 'value', type: 'quantitative' },
22
- color: { field: 'country', type: 'nominal' },
23
- },
24
- crosshair: true,
25
- };
26
-
27
- const lineSpecNoCrosshair: ChartSpec = {
28
- mark: 'line',
29
- data: [
30
- { date: '2020-01-01', value: 10, country: 'US' },
31
- { date: '2021-01-01', value: 40, country: 'US' },
32
- { date: '2020-01-01', value: 15, country: 'UK' },
33
- { date: '2021-01-01', value: 35, country: 'UK' },
34
- ],
35
- encoding: {
36
- x: { field: 'date', type: 'temporal' },
37
- y: { field: 'value', type: 'quantitative' },
38
- color: { field: 'country', type: 'nominal' },
39
- },
40
- };
41
-
42
- // ---------------------------------------------------------------------------
43
- // Tests
44
- // ---------------------------------------------------------------------------
45
-
46
- describe('crosshair', () => {
47
- let container: HTMLDivElement;
48
-
49
- beforeEach(() => {
50
- container = createContainer();
51
- });
52
-
53
- afterEach(() => {
54
- document.body.innerHTML = '';
55
- });
56
-
57
- it('creates a crosshair line element when crosshair: true on a line chart', () => {
58
- const chart = createChart(container, lineSpecWithCrosshair);
59
-
60
- const crosshair = container.querySelector('[data-crosshair]');
61
- expect(crosshair).not.toBeNull();
62
- expect(crosshair?.tagName.toLowerCase()).toBe('line');
63
- expect(crosshair?.getAttribute('class')).toBe('oc-crosshair');
64
-
65
- chart.destroy();
66
- });
67
-
68
- it('crosshair is hidden initially (display: none)', () => {
69
- const chart = createChart(container, lineSpecWithCrosshair);
70
-
71
- const crosshair = container.querySelector('[data-crosshair]') as SVGLineElement;
72
- expect(crosshair).not.toBeNull();
73
- expect(crosshair.style.display).toBe('none');
74
-
75
- chart.destroy();
76
- });
77
-
78
- it('crosshair defaults to on for line charts (omitted spec)', () => {
79
- const chart = createChart(container, lineSpecNoCrosshair);
80
-
81
- const crosshair = container.querySelector('[data-crosshair]');
82
- expect(crosshair).not.toBeNull();
83
-
84
- chart.destroy();
85
- });
86
-
87
- it('does not create crosshair when explicitly disabled', () => {
88
- const chart = createChart(container, { ...lineSpecNoCrosshair, crosshair: false });
89
-
90
- const crosshair = container.querySelector('[data-crosshair]');
91
- expect(crosshair).toBeNull();
92
-
93
- chart.destroy();
94
- });
95
-
96
- it('does not create crosshair on bar chart (no voronoi overlay)', () => {
97
- const barWithCrosshair: ChartSpec = {
98
- ...barSpec,
99
- crosshair: true,
100
- };
101
- const chart = createChart(container, barWithCrosshair);
102
-
103
- const crosshair = container.querySelector('[data-crosshair]');
104
- expect(crosshair).toBeNull();
105
-
106
- chart.destroy();
107
- });
108
-
109
- it('crosshair has correct stroke attributes', () => {
110
- const chart = createChart(container, lineSpecWithCrosshair);
111
-
112
- const crosshair = container.querySelector('[data-crosshair]') as SVGLineElement;
113
- expect(crosshair).not.toBeNull();
114
- expect(crosshair.getAttribute('stroke-dasharray')).toBe('3,3');
115
- expect(crosshair.getAttribute('stroke-width')).toBe('1');
116
- expect(crosshair.getAttribute('pointer-events')).toBe('none');
117
-
118
- chart.destroy();
119
- });
120
-
121
- it('cleanup removes crosshair on destroy', () => {
122
- const chart = createChart(container, lineSpecWithCrosshair);
123
-
124
- expect(container.querySelector('[data-crosshair]')).not.toBeNull();
125
-
126
- chart.destroy();
127
-
128
- expect(container.querySelector('[data-crosshair]')).toBeNull();
129
- });
130
- });