@opendata-ai/openchart-engine 8.0.0-rc.9 → 8.1.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.
- package/dist/index.d.ts +175 -12
- package/dist/index.js +917 -168
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/__test-fixtures__/specs.ts +0 -182
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +0 -2401
- package/src/__tests__/annotation-behavior.test.ts +0 -852
- package/src/__tests__/axes.test.ts +0 -1579
- package/src/__tests__/compile-animation.test.ts +0 -161
- package/src/__tests__/compile-chart.test.ts +0 -1303
- package/src/__tests__/compile-footnotes.test.ts +0 -277
- package/src/__tests__/compile-layer.test.ts +0 -969
- package/src/__tests__/compile-snapshot.test.ts +0 -156
- package/src/__tests__/compound-labels.test.ts +0 -147
- package/src/__tests__/dimensions.test.ts +0 -918
- package/src/__tests__/encoding-sugar.test.ts +0 -390
- package/src/__tests__/facet.test.ts +0 -215
- package/src/__tests__/helpers/invariants.ts +0 -299
- package/src/__tests__/known-bugs.test.ts +0 -566
- package/src/__tests__/layout-fuzz.test.ts +0 -358
- package/src/__tests__/layout-invariants.test.ts +0 -535
- package/src/__tests__/legend.test.ts +0 -721
- package/src/__tests__/mark-keys.test.ts +0 -294
- package/src/__tests__/ranking-recipes.test.ts +0 -260
- package/src/__tests__/rotation.test.ts +0 -83
- package/src/__tests__/scales.test.ts +0 -323
- package/src/__tests__/series-search.test.ts +0 -92
- package/src/__tests__/spec-sugar.test.ts +0 -801
- package/src/__tests__/you-draw-it.test.ts +0 -160
- package/src/annotations/__tests__/compute.test.ts +0 -1672
- package/src/annotations/__tests__/geometry.test.ts +0 -66
- package/src/annotations/__tests__/placement-fuzz.test.ts +0 -256
- package/src/annotations/__tests__/placement-invariants.test.ts +0 -345
- package/src/annotations/__tests__/placement.test.ts +0 -316
- package/src/annotations/__tests__/resolve-text.test.ts +0 -788
- package/src/annotations/__tests__/rich-text.test.ts +0 -91
- package/src/annotations/__tests__/thinning.test.ts +0 -285
- package/src/annotations/collisions.ts +0 -326
- package/src/annotations/compute.ts +0 -333
- package/src/annotations/constants.ts +0 -162
- package/src/annotations/geometry.ts +0 -376
- package/src/annotations/move.ts +0 -135
- package/src/annotations/placement.ts +0 -636
- package/src/annotations/position.ts +0 -131
- package/src/annotations/resolve-range.ts +0 -117
- package/src/annotations/resolve-refline.ts +0 -157
- package/src/annotations/resolve-text.ts +0 -545
- package/src/annotations/rich-text.ts +0 -65
- package/src/annotations/thinning.ts +0 -190
- package/src/barlist/__tests__/compile-barlist.test.ts +0 -200
- package/src/barlist/compile-barlist.ts +0 -391
- package/src/barlist/types.ts +0 -28
- package/src/charts/__tests__/post-process.test.ts +0 -237
- package/src/charts/__tests__/registry.test.ts +0 -116
- package/src/charts/__tests__/utils.test.ts +0 -195
- package/src/charts/_shared/__tests__/density-filter.test.ts +0 -32
- package/src/charts/_shared/density-filter.ts +0 -26
- package/src/charts/_shared/format-label-value.ts +0 -16
- package/src/charts/bar/__tests__/compute.test.ts +0 -934
- package/src/charts/bar/__tests__/gradient-orient.test.ts +0 -127
- package/src/charts/bar/__tests__/labels.test.ts +0 -292
- package/src/charts/bar/compute.ts +0 -539
- package/src/charts/bar/index.ts +0 -60
- package/src/charts/bar/labels.ts +0 -311
- package/src/charts/beeswarm/__tests__/compute.test.ts +0 -280
- package/src/charts/beeswarm/__tests__/dodge.test.ts +0 -148
- package/src/charts/beeswarm/compute.ts +0 -203
- package/src/charts/beeswarm/dodge.ts +0 -155
- package/src/charts/beeswarm/index.ts +0 -32
- package/src/charts/builtin.ts +0 -75
- package/src/charts/calendar/__tests__/compute.test.ts +0 -417
- package/src/charts/calendar/compute.ts +0 -319
- package/src/charts/calendar/index.ts +0 -32
- package/src/charts/column/__tests__/compute.test.ts +0 -540
- package/src/charts/column/__tests__/labels.test.ts +0 -179
- package/src/charts/column/compute.ts +0 -493
- package/src/charts/column/index.ts +0 -53
- package/src/charts/column/labels.ts +0 -229
- package/src/charts/dot/__tests__/compute.test.ts +0 -380
- package/src/charts/dot/__tests__/labels.test.ts +0 -98
- package/src/charts/dot/compute.ts +0 -285
- package/src/charts/dot/index.ts +0 -62
- package/src/charts/dot/labels.ts +0 -121
- package/src/charts/line/__tests__/compute.test.ts +0 -1194
- package/src/charts/line/__tests__/curves.test.ts +0 -51
- package/src/charts/line/__tests__/labels.test.ts +0 -94
- package/src/charts/line/__tests__/mark-options.test.ts +0 -175
- package/src/charts/line/area.ts +0 -484
- package/src/charts/line/compute.ts +0 -356
- package/src/charts/line/curves.ts +0 -85
- package/src/charts/line/index.ts +0 -176
- package/src/charts/line/labels.ts +0 -177
- package/src/charts/parliament/__tests__/compute.test.ts +0 -411
- package/src/charts/parliament/compute.ts +0 -313
- package/src/charts/parliament/index.ts +0 -32
- package/src/charts/pie/__tests__/compute.test.ts +0 -384
- package/src/charts/pie/__tests__/labels.test.ts +0 -132
- package/src/charts/pie/compute.ts +0 -324
- package/src/charts/pie/index.ts +0 -49
- package/src/charts/pie/labels.ts +0 -143
- package/src/charts/post-process.ts +0 -229
- package/src/charts/range/__tests__/compute.test.ts +0 -486
- package/src/charts/range/compute.ts +0 -273
- package/src/charts/range/index.ts +0 -82
- package/src/charts/range/labels.ts +0 -169
- package/src/charts/rect/__tests__/compute.test.ts +0 -120
- package/src/charts/rect/index.ts +0 -127
- package/src/charts/registry.ts +0 -72
- package/src/charts/rule/index.ts +0 -139
- package/src/charts/scatter/__tests__/compute.test.ts +0 -488
- package/src/charts/scatter/__tests__/trendline.test.ts +0 -191
- package/src/charts/scatter/compute.ts +0 -192
- package/src/charts/scatter/index.ts +0 -46
- package/src/charts/scatter/trendline.ts +0 -100
- package/src/charts/text/__tests__/compute.test.ts +0 -307
- package/src/charts/text/index.ts +0 -141
- package/src/charts/tick/__tests__/compute.test.ts +0 -75
- package/src/charts/tick/index.ts +0 -99
- package/src/charts/utils.ts +0 -170
- package/src/charts/waffle/__tests__/compute.test.ts +0 -305
- package/src/charts/waffle/compute.ts +0 -197
- package/src/charts/waffle/index.ts +0 -31
- package/src/compile/__tests__/color-scale-range.test.ts +0 -158
- package/src/compile/__tests__/color-scale-reverse.test.ts +0 -89
- package/src/compile/__tests__/data-clip.test.ts +0 -127
- package/src/compile/__tests__/fill-patterns.test.ts +0 -154
- package/src/compile/__tests__/size-scale.test.ts +0 -108
- package/src/compile/__tests__/watermark-obstacle.test.ts +0 -93
- package/src/compile/color-scale-range.ts +0 -101
- package/src/compile/data-clip.ts +0 -48
- package/src/compile/fill-patterns.ts +0 -74
- package/src/compile/layer.ts +0 -630
- package/src/compile/size-scale.ts +0 -141
- package/src/compile/spec-sugar.ts +0 -758
- package/src/compile/watermark-obstacle.ts +0 -54
- package/src/compile/you-draw-it.ts +0 -123
- package/src/compile.ts +0 -1503
- package/src/compiler/__tests__/a11y-warnings.test.ts +0 -148
- package/src/compiler/__tests__/animation.test.ts +0 -177
- package/src/compiler/__tests__/compile.test.ts +0 -87
- package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +0 -20
- package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +0 -22
- package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +0 -14
- package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +0 -34
- package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +0 -22
- package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +0 -41
- package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +0 -38
- package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +0 -28
- package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +0 -28
- package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +0 -30
- package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +0 -29
- package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +0 -32
- package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +0 -25
- package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +0 -21
- package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +0 -23
- package/src/compiler/__tests__/keys.test.ts +0 -83
- package/src/compiler/__tests__/normalize-highlight.test.ts +0 -103
- package/src/compiler/__tests__/normalize.test.ts +0 -399
- package/src/compiler/__tests__/sparkline-defaults.test.ts +0 -153
- package/src/compiler/__tests__/validate-did-you-mean.test.ts +0 -170
- package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
- package/src/compiler/__tests__/validate-near-miss.test.ts +0 -251
- package/src/compiler/__tests__/validate.test.ts +0 -629
- package/src/compiler/__tests__/vl-parity.test.ts +0 -68
- package/src/compiler/a11y-warnings.ts +0 -110
- package/src/compiler/animation.ts +0 -142
- package/src/compiler/index.ts +0 -47
- package/src/compiler/keys.ts +0 -55
- package/src/compiler/normalize.ts +0 -734
- package/src/compiler/sparkline-defaults.ts +0 -138
- package/src/compiler/types.ts +0 -241
- package/src/compiler/validate.ts +0 -2041
- package/src/endpoint-labels/__tests__/compute.test.ts +0 -587
- package/src/endpoint-labels/compute.ts +0 -621
- package/src/endpoint-labels/constants.ts +0 -54
- package/src/endpoint-labels/format.ts +0 -60
- package/src/endpoint-labels/predict.ts +0 -139
- package/src/format/__tests__/field-format.test.ts +0 -164
- package/src/format/field-format.ts +0 -27
- package/src/geo/__tests__/compile-map.test.ts +0 -1128
- package/src/geo/compile-map.ts +0 -1376
- package/src/geo/join.ts +0 -115
- package/src/geo/projections.ts +0 -65
- package/src/geo/types.ts +0 -26
- package/src/graphs/__tests__/community.test.ts +0 -228
- package/src/graphs/__tests__/compile-graph.test.ts +0 -396
- package/src/graphs/__tests__/encoding.test.ts +0 -415
- package/src/graphs/community.ts +0 -92
- package/src/graphs/compile-graph.ts +0 -329
- package/src/graphs/encoding.ts +0 -356
- package/src/graphs/types.ts +0 -106
- package/src/index.ts +0 -177
- package/src/layout/__tests__/metrics.test.ts +0 -97
- package/src/layout/__tests__/scales-highlight.test.ts +0 -192
- package/src/layout/axes/rotation.ts +0 -152
- package/src/layout/axes/thinning.ts +0 -98
- package/src/layout/axes/ticks.ts +0 -409
- package/src/layout/axes.ts +0 -757
- package/src/layout/dimensions.ts +0 -912
- package/src/layout/facet.ts +0 -91
- package/src/layout/gridlines.ts +0 -84
- package/src/layout/metrics.ts +0 -132
- package/src/layout/plan.ts +0 -631
- package/src/layout/scales.ts +0 -1053
- package/src/layout/shared.ts +0 -44
- package/src/legend/__tests__/continuous.test.ts +0 -318
- package/src/legend/__tests__/redundancy.test.ts +0 -108
- package/src/legend/__tests__/size.test.ts +0 -348
- package/src/legend/__tests__/suppression.test.ts +0 -382
- package/src/legend/compute.ts +0 -748
- package/src/legend/continuous.ts +0 -327
- package/src/legend/size.ts +0 -407
- package/src/legend/suppression.ts +0 -226
- package/src/legend/wrap.ts +0 -117
- package/src/sankey/__tests__/compile-sankey.test.ts +0 -458
- package/src/sankey/__tests__/layout.test.ts +0 -165
- package/src/sankey/__tests__/node-label-wrap.test.ts +0 -114
- package/src/sankey/__tests__/node-sort.test.ts +0 -45
- package/src/sankey/compile-sankey.ts +0 -706
- package/src/sankey/layout.ts +0 -184
- package/src/sankey/types.ts +0 -41
- package/src/tables/__tests__/bar-column.test.ts +0 -147
- package/src/tables/__tests__/category-colors.test.ts +0 -230
- package/src/tables/__tests__/compile-table.test.ts +0 -236
- package/src/tables/__tests__/format-cells.test.ts +0 -143
- package/src/tables/__tests__/heatmap.test.ts +0 -148
- package/src/tables/__tests__/pagination.test.ts +0 -78
- package/src/tables/__tests__/search.test.ts +0 -94
- package/src/tables/__tests__/sort.test.ts +0 -107
- package/src/tables/__tests__/sparkline.test.ts +0 -122
- package/src/tables/bar-column.ts +0 -94
- package/src/tables/category-colors.ts +0 -78
- package/src/tables/compile-table.ts +0 -427
- package/src/tables/format-cells.ts +0 -115
- package/src/tables/heatmap.ts +0 -141
- package/src/tables/pagination.ts +0 -46
- package/src/tables/search.ts +0 -66
- package/src/tables/sort.ts +0 -69
- package/src/tables/sparkline.ts +0 -113
- package/src/tables/utils.ts +0 -16
- package/src/tilemap/__tests__/compile-tilemap.test.ts +0 -576
- package/src/tilemap/compile-tilemap.ts +0 -755
- package/src/tilemap/layout.ts +0 -172
- package/src/tilemap/types.ts +0 -33
- package/src/tooltips/__tests__/compute.test.ts +0 -582
- package/src/tooltips/compute.ts +0 -700
- package/src/transforms/__tests__/aggregate.test.ts +0 -159
- package/src/transforms/__tests__/bin.test.ts +0 -88
- package/src/transforms/__tests__/calculate.test.ts +0 -146
- package/src/transforms/__tests__/conditional.test.ts +0 -127
- package/src/transforms/__tests__/filter-relative.test.ts +0 -202
- package/src/transforms/__tests__/filter.test.ts +0 -59
- package/src/transforms/__tests__/fold.test.ts +0 -79
- package/src/transforms/__tests__/index.test.ts +0 -93
- package/src/transforms/__tests__/predicates.test.ts +0 -193
- package/src/transforms/__tests__/timeunit.test.ts +0 -129
- package/src/transforms/__tests__/window.test.ts +0 -286
- package/src/transforms/aggregate.ts +0 -131
- package/src/transforms/bin.ts +0 -87
- package/src/transforms/calculate.ts +0 -60
- package/src/transforms/conditional.ts +0 -46
- package/src/transforms/filter.ts +0 -122
- package/src/transforms/fold.ts +0 -49
- package/src/transforms/index.ts +0 -60
- package/src/transforms/predicates.ts +0 -123
- package/src/transforms/timeunit.ts +0 -88
- package/src/transforms/window.ts +0 -185
|
@@ -1,721 +0,0 @@
|
|
|
1
|
-
import type { LayoutStrategy, Rect, ResolvedTheme } from '@opendata-ai/openchart-core';
|
|
2
|
-
import { 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 { INLINE_TICK_OVERHANG_PAD } from '../layout/shared';
|
|
7
|
-
import { computeLegend } from '../legend/compute';
|
|
8
|
-
|
|
9
|
-
const specWithColor: NormalizedChartSpec = {
|
|
10
|
-
markType: 'line',
|
|
11
|
-
markDef: { type: 'line' },
|
|
12
|
-
data: [
|
|
13
|
-
{ date: '2020', value: 10, country: 'US' },
|
|
14
|
-
{ date: '2021', value: 20, country: 'UK' },
|
|
15
|
-
{ date: '2022', value: 30, country: 'Germany' },
|
|
16
|
-
],
|
|
17
|
-
encoding: {
|
|
18
|
-
x: { field: 'date', type: 'temporal' },
|
|
19
|
-
y: { field: 'value', type: 'quantitative' },
|
|
20
|
-
color: { field: 'country', type: 'nominal' },
|
|
21
|
-
},
|
|
22
|
-
chrome: {},
|
|
23
|
-
annotations: [],
|
|
24
|
-
responsive: true,
|
|
25
|
-
theme: {},
|
|
26
|
-
darkMode: 'off',
|
|
27
|
-
// density: 'none' historically short-circuited the legend auto-suppression
|
|
28
|
-
// table, but post-fix that switch only governs end-of-line labels. Keep
|
|
29
|
-
// density: 'none' here AND set legend.show: true so the basic legend tests
|
|
30
|
-
// still test what they want — generic legend rendering with an explicit
|
|
31
|
-
// opt-in. Tests that need to exercise auto-suppression rules use
|
|
32
|
-
// `lineWithLabels` (density: 'auto', no explicit legend.show) further down.
|
|
33
|
-
labels: { density: 'none', format: '', prefix: '' },
|
|
34
|
-
legend: { show: true },
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const specWithoutColor: NormalizedChartSpec = {
|
|
38
|
-
...specWithColor,
|
|
39
|
-
encoding: {
|
|
40
|
-
x: { field: 'date', type: 'temporal' },
|
|
41
|
-
y: { field: 'value', type: 'quantitative' },
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const chartArea: Rect = { x: 50, y: 50, width: 500, height: 300 };
|
|
46
|
-
const theme: ResolvedTheme = resolveTheme();
|
|
47
|
-
|
|
48
|
-
const fullStrategy: LayoutStrategy = {
|
|
49
|
-
labelMode: 'all',
|
|
50
|
-
legendPosition: 'right',
|
|
51
|
-
annotationPosition: 'inline',
|
|
52
|
-
axisLabelDensity: 'full',
|
|
53
|
-
chromeMode: 'full',
|
|
54
|
-
legendMaxHeight: -1,
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const compactStrategy: LayoutStrategy = {
|
|
58
|
-
labelMode: 'none',
|
|
59
|
-
legendPosition: 'top',
|
|
60
|
-
annotationPosition: 'tooltip-only',
|
|
61
|
-
axisLabelDensity: 'minimal',
|
|
62
|
-
chromeMode: 'full',
|
|
63
|
-
legendMaxHeight: -1,
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
describe('computeLegend', () => {
|
|
67
|
-
it('derives entries from color encoding unique values', () => {
|
|
68
|
-
const legend = computeLegend(specWithColor, fullStrategy, theme, chartArea);
|
|
69
|
-
expect(legend.entries).toHaveLength(3);
|
|
70
|
-
expect(legend.entries.map((e) => e.label)).toEqual(['US', 'UK', 'Germany']);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('assigns distinct colors from the theme palette', () => {
|
|
74
|
-
const legend = computeLegend(specWithColor, fullStrategy, theme, chartArea);
|
|
75
|
-
const colors = legend.entries.map((e) => e.color);
|
|
76
|
-
const uniqueColors = new Set(colors);
|
|
77
|
-
expect(uniqueColors.size).toBe(3);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('returns empty entries when no color encoding', () => {
|
|
81
|
-
const legend = computeLegend(specWithoutColor, fullStrategy, theme, chartArea);
|
|
82
|
-
expect(legend.entries).toHaveLength(0);
|
|
83
|
-
expect(legend.bounds.width).toBe(0);
|
|
84
|
-
expect(legend.bounds.height).toBe(0);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('positions legend on right at full width', () => {
|
|
88
|
-
const legend = computeLegend(specWithColor, fullStrategy, theme, chartArea);
|
|
89
|
-
expect(legend.position).toBe('right');
|
|
90
|
-
expect(legend.bounds.width).toBeGreaterThan(0);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('positions legend on top at compact width', () => {
|
|
94
|
-
const legend = computeLegend(specWithColor, compactStrategy, theme, chartArea);
|
|
95
|
-
expect(legend.position).toBe('top');
|
|
96
|
-
expect(legend.bounds.height).toBeGreaterThan(0);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('returns empty entries when show is false', () => {
|
|
100
|
-
const specHidden: NormalizedChartSpec = {
|
|
101
|
-
...specWithColor,
|
|
102
|
-
legend: { show: false },
|
|
103
|
-
hiddenSeries: [],
|
|
104
|
-
seriesStyles: {},
|
|
105
|
-
};
|
|
106
|
-
const legend = computeLegend(specHidden, fullStrategy, theme, chartArea);
|
|
107
|
-
expect(legend.entries).toHaveLength(0);
|
|
108
|
-
expect(legend.bounds.width).toBe(0);
|
|
109
|
-
expect(legend.bounds.height).toBe(0);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('still shows legend when show is true', () => {
|
|
113
|
-
const specShown: NormalizedChartSpec = {
|
|
114
|
-
...specWithColor,
|
|
115
|
-
legend: { show: true },
|
|
116
|
-
hiddenSeries: [],
|
|
117
|
-
seriesStyles: {},
|
|
118
|
-
};
|
|
119
|
-
const legend = computeLegend(specShown, fullStrategy, theme, chartArea);
|
|
120
|
-
expect(legend.entries).toHaveLength(3);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('with columns: 3 and 6 series, shows all 6 entries across 2 rows', () => {
|
|
124
|
-
const sixSeriesSpec: NormalizedChartSpec = {
|
|
125
|
-
...specWithColor,
|
|
126
|
-
data: [
|
|
127
|
-
{ date: '2020', value: 10, country: 'A' },
|
|
128
|
-
{ date: '2020', value: 10, country: 'B' },
|
|
129
|
-
{ date: '2020', value: 10, country: 'C' },
|
|
130
|
-
{ date: '2020', value: 10, country: 'D' },
|
|
131
|
-
{ date: '2020', value: 10, country: 'E' },
|
|
132
|
-
{ date: '2020', value: 10, country: 'F' },
|
|
133
|
-
],
|
|
134
|
-
legend: { columns: 3 },
|
|
135
|
-
hiddenSeries: [],
|
|
136
|
-
seriesStyles: {},
|
|
137
|
-
};
|
|
138
|
-
const legend = computeLegend(sixSeriesSpec, compactStrategy, theme, chartArea);
|
|
139
|
-
// All 6 entries visible (no overflow indicator)
|
|
140
|
-
expect(legend.entries).toHaveLength(6);
|
|
141
|
-
expect(legend.entries.every((e) => !e.overflow)).toBe(true);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('with symbolLimit: 3 and 6 series, truncates to 3 entries + overflow', () => {
|
|
145
|
-
const sixSeriesSpec: NormalizedChartSpec = {
|
|
146
|
-
...specWithColor,
|
|
147
|
-
data: [
|
|
148
|
-
{ date: '2020', value: 10, country: 'A' },
|
|
149
|
-
{ date: '2020', value: 10, country: 'B' },
|
|
150
|
-
{ date: '2020', value: 10, country: 'C' },
|
|
151
|
-
{ date: '2020', value: 10, country: 'D' },
|
|
152
|
-
{ date: '2020', value: 10, country: 'E' },
|
|
153
|
-
{ date: '2020', value: 10, country: 'F' },
|
|
154
|
-
],
|
|
155
|
-
legend: { symbolLimit: 3 },
|
|
156
|
-
hiddenSeries: [],
|
|
157
|
-
seriesStyles: {},
|
|
158
|
-
};
|
|
159
|
-
const legend = computeLegend(sixSeriesSpec, compactStrategy, theme, chartArea);
|
|
160
|
-
// 3 real entries + 1 overflow indicator
|
|
161
|
-
expect(legend.entries).toHaveLength(4);
|
|
162
|
-
expect(legend.entries[3].label).toBe('+3 more');
|
|
163
|
-
expect(legend.entries[3].overflow).toBe(true);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('with symbolLimit on right-positioned legend, truncates entries', () => {
|
|
167
|
-
const sixSeriesSpec: NormalizedChartSpec = {
|
|
168
|
-
...specWithColor,
|
|
169
|
-
data: [
|
|
170
|
-
{ date: '2020', value: 10, country: 'A' },
|
|
171
|
-
{ date: '2020', value: 10, country: 'B' },
|
|
172
|
-
{ date: '2020', value: 10, country: 'C' },
|
|
173
|
-
{ date: '2020', value: 10, country: 'D' },
|
|
174
|
-
{ date: '2020', value: 10, country: 'E' },
|
|
175
|
-
{ date: '2020', value: 10, country: 'F' },
|
|
176
|
-
],
|
|
177
|
-
legend: { symbolLimit: 2 },
|
|
178
|
-
hiddenSeries: [],
|
|
179
|
-
seriesStyles: {},
|
|
180
|
-
};
|
|
181
|
-
const legend = computeLegend(sixSeriesSpec, fullStrategy, theme, chartArea);
|
|
182
|
-
// 2 real entries + 1 overflow indicator
|
|
183
|
-
expect(legend.entries).toHaveLength(3);
|
|
184
|
-
expect(legend.entries[2].label).toBe('+4 more');
|
|
185
|
-
expect(legend.entries[2].overflow).toBe(true);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it('symbolLimit: 0 is clamped to 1 (minimum 1 entry)', () => {
|
|
189
|
-
const sixSeriesSpec: NormalizedChartSpec = {
|
|
190
|
-
...specWithColor,
|
|
191
|
-
data: [
|
|
192
|
-
{ date: '2020', value: 10, country: 'A' },
|
|
193
|
-
{ date: '2020', value: 10, country: 'B' },
|
|
194
|
-
{ date: '2020', value: 10, country: 'C' },
|
|
195
|
-
],
|
|
196
|
-
encoding: {
|
|
197
|
-
x: { field: 'date', type: 'temporal' },
|
|
198
|
-
y: { field: 'value', type: 'quantitative' },
|
|
199
|
-
color: { field: 'country', type: 'nominal' },
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
const compactStrategy: LayoutStrategy = {
|
|
203
|
-
legend: { symbolLimit: 0 },
|
|
204
|
-
hiddenSeries: [],
|
|
205
|
-
seriesStyles: {},
|
|
206
|
-
};
|
|
207
|
-
const legend = computeLegend(sixSeriesSpec, compactStrategy, theme, chartArea);
|
|
208
|
-
// symbolLimit: 0 gets clamped to 1, so 1 real entry + overflow
|
|
209
|
-
expect(legend.entries.length).toBeGreaterThanOrEqual(1);
|
|
210
|
-
expect(legend.entries.filter((e) => !e.overflow).length).toBeGreaterThanOrEqual(1);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('symbolLimit greater than entry count shows all entries', () => {
|
|
214
|
-
const largeLimit: LayoutStrategy = {
|
|
215
|
-
legend: { symbolLimit: 100 },
|
|
216
|
-
hiddenSeries: [],
|
|
217
|
-
seriesStyles: {},
|
|
218
|
-
};
|
|
219
|
-
const legend = computeLegend(specWithColor, largeLimit, theme, chartArea);
|
|
220
|
-
// All 3 entries shown, no overflow
|
|
221
|
-
expect(legend.entries).toHaveLength(3);
|
|
222
|
-
expect(legend.entries.every((e) => !e.overflow)).toBe(true);
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
it('with maxRows: 3 and 8 long-named entries, shows more entries than default maxRows of 2', () => {
|
|
226
|
-
// Use long series names so entries overflow 2 rows but fit in 3
|
|
227
|
-
const longNameData = [
|
|
228
|
-
{ date: '2020', value: 10, country: 'Home price to income ratio' },
|
|
229
|
-
{ date: '2020', value: 10, country: 'Tuition to income ratio' },
|
|
230
|
-
{ date: '2020', value: 10, country: 'Health premium to income' },
|
|
231
|
-
{ date: '2020', value: 10, country: 'Childcare cost to income' },
|
|
232
|
-
{ date: '2020', value: 10, country: 'Transportation expenses' },
|
|
233
|
-
{ date: '2020', value: 10, country: 'Food and groceries cost' },
|
|
234
|
-
{ date: '2020', value: 10, country: 'Utilities and services' },
|
|
235
|
-
{ date: '2020', value: 10, country: 'Insurance and benefits' },
|
|
236
|
-
];
|
|
237
|
-
const maxRowsSpec: NormalizedChartSpec = {
|
|
238
|
-
...specWithColor,
|
|
239
|
-
data: longNameData,
|
|
240
|
-
legend: { maxRows: 3 },
|
|
241
|
-
hiddenSeries: [],
|
|
242
|
-
seriesStyles: {},
|
|
243
|
-
};
|
|
244
|
-
const defaultSpec: NormalizedChartSpec = {
|
|
245
|
-
...specWithColor,
|
|
246
|
-
data: longNameData,
|
|
247
|
-
};
|
|
248
|
-
const legendDefault = computeLegend(defaultSpec, compactStrategy, theme, chartArea);
|
|
249
|
-
const legendMaxRows = computeLegend(maxRowsSpec, compactStrategy, theme, chartArea);
|
|
250
|
-
const defaultVisible = legendDefault.entries.filter((e) => !e.overflow).length;
|
|
251
|
-
const maxRowsVisible = legendMaxRows.entries.filter((e) => !e.overflow).length;
|
|
252
|
-
expect(maxRowsVisible).toBeGreaterThan(defaultVisible);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it('uses explicit domain+range colors in legend entries', () => {
|
|
256
|
-
const specExplicit: NormalizedChartSpec = {
|
|
257
|
-
...specWithColor,
|
|
258
|
-
data: [
|
|
259
|
-
{ date: '2020', value: 10, country: 'UK' },
|
|
260
|
-
{ date: '2021', value: 20, country: 'US' },
|
|
261
|
-
{ date: '2022', value: 30, country: 'Germany' },
|
|
262
|
-
],
|
|
263
|
-
encoding: {
|
|
264
|
-
x: { field: 'date', type: 'temporal' },
|
|
265
|
-
y: { field: 'value', type: 'quantitative' },
|
|
266
|
-
color: {
|
|
267
|
-
field: 'country',
|
|
268
|
-
type: 'nominal',
|
|
269
|
-
scale: {
|
|
270
|
-
domain: ['US', 'UK', 'Germany'],
|
|
271
|
-
range: ['#ff0000', '#0000ff', '#00ff00'],
|
|
272
|
-
},
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
};
|
|
276
|
-
const legend = computeLegend(specExplicit, compactStrategy, theme, chartArea);
|
|
277
|
-
// Data order is UK, US, Germany but domain order is US, UK, Germany
|
|
278
|
-
// Legend should match colors to domain indices, not data order
|
|
279
|
-
const ukEntry = legend.entries.find((e) => e.label === 'UK')!;
|
|
280
|
-
const usEntry = legend.entries.find((e) => e.label === 'US')!;
|
|
281
|
-
const deEntry = legend.entries.find((e) => e.label === 'Germany')!;
|
|
282
|
-
expect(usEntry.color).toBe('#ff0000');
|
|
283
|
-
expect(ukEntry.color).toBe('#0000ff');
|
|
284
|
-
expect(deEntry.color).toBe('#00ff00');
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
it('orders legend entries by explicit domain, not data order', () => {
|
|
288
|
-
const specExplicit: NormalizedChartSpec = {
|
|
289
|
-
...specWithColor,
|
|
290
|
-
data: [
|
|
291
|
-
{ date: '2020', value: 10, country: 'Germany' },
|
|
292
|
-
{ date: '2021', value: 20, country: 'UK' },
|
|
293
|
-
{ date: '2022', value: 30, country: 'US' },
|
|
294
|
-
],
|
|
295
|
-
encoding: {
|
|
296
|
-
x: { field: 'date', type: 'temporal' },
|
|
297
|
-
y: { field: 'value', type: 'quantitative' },
|
|
298
|
-
color: {
|
|
299
|
-
field: 'country',
|
|
300
|
-
type: 'nominal',
|
|
301
|
-
scale: {
|
|
302
|
-
domain: ['US', 'UK', 'Germany'],
|
|
303
|
-
},
|
|
304
|
-
},
|
|
305
|
-
},
|
|
306
|
-
};
|
|
307
|
-
const legend = computeLegend(specExplicit, compactStrategy, theme, chartArea);
|
|
308
|
-
expect(legend.entries.map((e) => e.label)).toEqual(['US', 'UK', 'Germany']);
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
it('uses correct swatch shape for chart type', () => {
|
|
312
|
-
const lineLegend = computeLegend(specWithColor, fullStrategy, theme, chartArea);
|
|
313
|
-
expect(lineLegend.entries[0].shape).toBe('line');
|
|
314
|
-
|
|
315
|
-
const barSpec: NormalizedChartSpec = {
|
|
316
|
-
...specWithColor,
|
|
317
|
-
markType: 'bar',
|
|
318
|
-
markDef: { type: 'bar' },
|
|
319
|
-
encoding: {
|
|
320
|
-
x: { field: 'value', type: 'quantitative' },
|
|
321
|
-
y: { field: 'date', type: 'nominal' },
|
|
322
|
-
color: { field: 'country', type: 'nominal' },
|
|
323
|
-
},
|
|
324
|
-
};
|
|
325
|
-
const barLegend = computeLegend(barSpec, fullStrategy, theme, chartArea);
|
|
326
|
-
expect(barLegend.entries[0].shape).toBe('square');
|
|
327
|
-
|
|
328
|
-
const scatterSpec: NormalizedChartSpec = {
|
|
329
|
-
...specWithColor,
|
|
330
|
-
markType: 'point',
|
|
331
|
-
markDef: { type: 'point' },
|
|
332
|
-
encoding: {
|
|
333
|
-
x: { field: 'value', type: 'quantitative' },
|
|
334
|
-
y: { field: 'value', type: 'quantitative' },
|
|
335
|
-
color: { field: 'country', type: 'nominal' },
|
|
336
|
-
},
|
|
337
|
-
};
|
|
338
|
-
const scatterLegend = computeLegend(scatterSpec, fullStrategy, theme, chartArea);
|
|
339
|
-
expect(scatterLegend.entries[0].shape).toBe('circle');
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
describe('auto-suppression for line/area with endpoint labels', () => {
|
|
343
|
-
/** Line spec with labels enabled for suppression tests. */
|
|
344
|
-
const lineWithLabels: NormalizedChartSpec = {
|
|
345
|
-
...specWithColor,
|
|
346
|
-
labels: { density: 'auto', format: '', prefix: '' },
|
|
347
|
-
// Drop the explicit legend opt-in inherited from specWithColor so the
|
|
348
|
-
// auto-suppression truth table actually applies.
|
|
349
|
-
legend: undefined as unknown as NormalizedChartSpec['legend'],
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
it('suppresses legend for multi-series line chart with default labels', () => {
|
|
353
|
-
const legend = computeLegend(lineWithLabels, fullStrategy, theme, chartArea);
|
|
354
|
-
expect(legend.entries).toHaveLength(0);
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
it('preserves legend when legend.show is explicitly true', () => {
|
|
358
|
-
const spec: NormalizedChartSpec = {
|
|
359
|
-
...lineWithLabels,
|
|
360
|
-
legend: { show: true },
|
|
361
|
-
hiddenSeries: [],
|
|
362
|
-
seriesStyles: {},
|
|
363
|
-
};
|
|
364
|
-
const legend = computeLegend(spec, fullStrategy, theme, chartArea);
|
|
365
|
-
expect(legend.entries).toHaveLength(3);
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
it('preserves legend when any legend config is present (e.g. position)', () => {
|
|
369
|
-
const spec: NormalizedChartSpec = {
|
|
370
|
-
...lineWithLabels,
|
|
371
|
-
legend: { position: 'top' },
|
|
372
|
-
hiddenSeries: [],
|
|
373
|
-
seriesStyles: {},
|
|
374
|
-
};
|
|
375
|
-
const legend = computeLegend(spec, fullStrategy, theme, chartArea);
|
|
376
|
-
expect(legend.entries).toHaveLength(3);
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it('still applies the truth table when labels density is none', () => {
|
|
380
|
-
// labels.density: 'none' is the legacy switch for end-of-line labels
|
|
381
|
-
// only — it must not short-circuit the legend / endpoint-column truth
|
|
382
|
-
// table. So a multi-series line with density: 'none' and no explicit
|
|
383
|
-
// legend.show still gets the legend auto-suppressed (cell 1).
|
|
384
|
-
const spec: NormalizedChartSpec = {
|
|
385
|
-
...lineWithLabels,
|
|
386
|
-
labels: { density: 'none', format: '', prefix: '' },
|
|
387
|
-
};
|
|
388
|
-
const legend = computeLegend(spec, fullStrategy, theme, chartArea);
|
|
389
|
-
expect(legend.entries).toHaveLength(0);
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
it('preserves legend when density is none AND legend.show is explicitly true', () => {
|
|
393
|
-
const spec: NormalizedChartSpec = {
|
|
394
|
-
...lineWithLabels,
|
|
395
|
-
labels: { density: 'none', format: '', prefix: '' },
|
|
396
|
-
legend: { show: true },
|
|
397
|
-
};
|
|
398
|
-
const legend = computeLegend(spec, fullStrategy, theme, chartArea);
|
|
399
|
-
expect(legend.entries).toHaveLength(3);
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
it('preserves legend at compact breakpoint where labelMode is none', () => {
|
|
403
|
-
const legend = computeLegend(lineWithLabels, compactStrategy, theme, chartArea);
|
|
404
|
-
expect(legend.entries).toHaveLength(3);
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
it('preserves legend for explicitly stacked area chart (stack: "zero")', () => {
|
|
408
|
-
const areaSpec: NormalizedChartSpec = {
|
|
409
|
-
...lineWithLabels,
|
|
410
|
-
markType: 'area',
|
|
411
|
-
markDef: { type: 'area' },
|
|
412
|
-
encoding: {
|
|
413
|
-
x: { field: 'date', type: 'temporal' },
|
|
414
|
-
y: { field: 'value', type: 'quantitative', stack: 'zero' },
|
|
415
|
-
color: { field: 'country', type: 'nominal' },
|
|
416
|
-
},
|
|
417
|
-
};
|
|
418
|
-
const legend = computeLegend(areaSpec, fullStrategy, theme, chartArea);
|
|
419
|
-
expect(legend.entries).toHaveLength(3);
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
it('preserves legend for default (stacked) area chart', () => {
|
|
423
|
-
// v8: area stacks by default (Vega-Lite aligned). Stacked areas keep
|
|
424
|
-
// the traditional legend rather than auto-suppressing for endpoint labels.
|
|
425
|
-
const areaSpec: NormalizedChartSpec = {
|
|
426
|
-
...lineWithLabels,
|
|
427
|
-
markType: 'area',
|
|
428
|
-
markDef: { type: 'area' },
|
|
429
|
-
};
|
|
430
|
-
const legend = computeLegend(areaSpec, fullStrategy, theme, chartArea);
|
|
431
|
-
expect(legend.entries).toHaveLength(3);
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
it('suppresses legend for explicit overlap area chart (stack: null)', () => {
|
|
435
|
-
const areaSpec: NormalizedChartSpec = {
|
|
436
|
-
...lineWithLabels,
|
|
437
|
-
markType: 'area',
|
|
438
|
-
markDef: { type: 'area' },
|
|
439
|
-
encoding: {
|
|
440
|
-
x: { field: 'date', type: 'temporal' },
|
|
441
|
-
y: { field: 'value', type: 'quantitative', stack: null },
|
|
442
|
-
color: { field: 'country', type: 'nominal' },
|
|
443
|
-
},
|
|
444
|
-
};
|
|
445
|
-
const legend = computeLegend(areaSpec, fullStrategy, theme, chartArea);
|
|
446
|
-
expect(legend.entries).toHaveLength(0);
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
it('preserves legend for bar chart (not line/area)', () => {
|
|
450
|
-
const barSpec: NormalizedChartSpec = {
|
|
451
|
-
...lineWithLabels,
|
|
452
|
-
markType: 'bar',
|
|
453
|
-
markDef: { type: 'bar' },
|
|
454
|
-
encoding: {
|
|
455
|
-
x: { field: 'date', type: 'nominal' },
|
|
456
|
-
y: { field: 'value', type: 'quantitative' },
|
|
457
|
-
color: { field: 'country', type: 'nominal' },
|
|
458
|
-
},
|
|
459
|
-
};
|
|
460
|
-
const legend = computeLegend(barSpec, fullStrategy, theme, chartArea);
|
|
461
|
-
expect(legend.entries).toHaveLength(3);
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
it('does not suppress for single-series line (no color encoding)', () => {
|
|
465
|
-
const noColorWithLabels: NormalizedChartSpec = {
|
|
466
|
-
...specWithoutColor,
|
|
467
|
-
labels: { density: 'auto', format: '', prefix: '' },
|
|
468
|
-
};
|
|
469
|
-
const legend = computeLegend(noColorWithLabels, fullStrategy, theme, chartArea);
|
|
470
|
-
expect(legend.entries).toHaveLength(0);
|
|
471
|
-
expect(legend.bounds.width).toBe(0);
|
|
472
|
-
});
|
|
473
|
-
});
|
|
474
|
-
|
|
475
|
-
describe('top legend spacing', () => {
|
|
476
|
-
const topLegendSpec = {
|
|
477
|
-
mark: 'bar' as const,
|
|
478
|
-
data: [
|
|
479
|
-
{ name: 'A', value: 10, group: 'X' },
|
|
480
|
-
{ name: 'A', value: 20, group: 'Y' },
|
|
481
|
-
{ name: 'B', value: 30, group: 'X' },
|
|
482
|
-
{ name: 'B', value: 25, group: 'Y' },
|
|
483
|
-
],
|
|
484
|
-
encoding: {
|
|
485
|
-
x: { field: 'name', type: 'nominal' as const },
|
|
486
|
-
y: { field: 'value', type: 'quantitative' as const },
|
|
487
|
-
color: { field: 'group', type: 'nominal' as const },
|
|
488
|
-
},
|
|
489
|
-
legend: { position: 'top' as const },
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
it('places the legend exactly 8px above the chart area at standard width', () => {
|
|
493
|
-
const layout = compileChart(topLegendSpec, { width: 600, height: 400 });
|
|
494
|
-
|
|
495
|
-
expect(layout.legend.position).toBe('top');
|
|
496
|
-
expect(layout.legend.entries.length).toBeGreaterThan(0);
|
|
497
|
-
expect(layout.legend.bounds.height).toBeGreaterThan(0);
|
|
498
|
-
|
|
499
|
-
const legendBottom = layout.legend.bounds.y + layout.legend.bounds.height;
|
|
500
|
-
const gap = layout.area.y - legendBottom;
|
|
501
|
-
expect(gap).toBe(8);
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
it('eliminates legend gap on narrow viewports (< 420px)', () => {
|
|
505
|
-
const layout = compileChart(topLegendSpec, { width: 360, height: 400 });
|
|
506
|
-
|
|
507
|
-
expect(layout.legend.position).toBe('top');
|
|
508
|
-
expect(layout.legend.entries.length).toBeGreaterThan(0);
|
|
509
|
-
|
|
510
|
-
const legendBottom = layout.legend.bounds.y + layout.legend.bounds.height;
|
|
511
|
-
const gap = layout.area.y - legendBottom;
|
|
512
|
-
expect(gap).toBe(0);
|
|
513
|
-
});
|
|
514
|
-
|
|
515
|
-
// Inline y ticks (line/area) draw their topmost label ABOVE chartArea.y,
|
|
516
|
-
// inside the inlineTickOverhang zone the margin stack reserved. The legend
|
|
517
|
-
// must sit above that zone, not flush against chartArea.y.
|
|
518
|
-
const inlineTopLegendSpec = {
|
|
519
|
-
mark: 'line' as const,
|
|
520
|
-
data: [
|
|
521
|
-
{ date: '2020-01-01', value: 10, country: 'US' },
|
|
522
|
-
{ date: '2021-01-01', value: 40, country: 'US' },
|
|
523
|
-
{ date: '2020-01-01', value: 15, country: 'UK' },
|
|
524
|
-
{ date: '2021-01-01', value: 35, country: 'UK' },
|
|
525
|
-
],
|
|
526
|
-
encoding: {
|
|
527
|
-
x: { field: 'date', type: 'temporal' as const },
|
|
528
|
-
y: { field: 'value', type: 'quantitative' as const },
|
|
529
|
-
color: { field: 'country', type: 'nominal' as const },
|
|
530
|
-
},
|
|
531
|
-
legend: { show: true, position: 'top' as const },
|
|
532
|
-
};
|
|
533
|
-
|
|
534
|
-
it('top legend clears the inline y-tick overhang at compact width', () => {
|
|
535
|
-
const layout = compileChart(inlineTopLegendSpec, { width: 360, height: 400 });
|
|
536
|
-
|
|
537
|
-
expect(layout.legend.position).toBe('top');
|
|
538
|
-
expect(layout.legend.entries.length).toBeGreaterThan(0);
|
|
539
|
-
|
|
540
|
-
const inlineTickOverhang = layout.theme.fonts.sizes.axisTick + INLINE_TICK_OVERHANG_PAD;
|
|
541
|
-
const legendBottom = layout.legend.bounds.y + layout.legend.bounds.height;
|
|
542
|
-
expect(legendBottom).toBeLessThanOrEqual(layout.area.y - inlineTickOverhang + 1e-6);
|
|
543
|
-
|
|
544
|
-
// Pinned placement. Before the fix the legend bottom sat flush at
|
|
545
|
-
// chartArea.y (legend bottom was 62 = chartArea.y), overlapping the
|
|
546
|
-
// topmost inline tick label; it now sits the 17px overhang higher,
|
|
547
|
-
// while the 6px minimum gap above the legend shifts chartArea down.
|
|
548
|
-
expect(layout.legend.bounds.y).toBe(31);
|
|
549
|
-
});
|
|
550
|
-
|
|
551
|
-
it('gutter y-ticks keep the legend flush at the legendGap (no overhang shift)', () => {
|
|
552
|
-
// Bar charts use gutter y-tick labels, so inlineTickOverhang is 0 and
|
|
553
|
-
// the legend keeps its legendGap-only offset from the chart area.
|
|
554
|
-
const layout = compileChart(topLegendSpec, { width: 600, height: 400 });
|
|
555
|
-
|
|
556
|
-
const legendBottom = layout.legend.bounds.y + layout.legend.bounds.height;
|
|
557
|
-
expect(layout.area.y - legendBottom).toBe(8);
|
|
558
|
-
|
|
559
|
-
// Pinned placement: 17 before the placement fix; the +6 comes from the
|
|
560
|
-
// TOP_LEGEND_GAP_ABOVE margin (not from any overhang shift — gutter
|
|
561
|
-
// ticks have no overhang, so the legend-to-chart gap above stays 8).
|
|
562
|
-
expect(layout.legend.bounds.y).toBe(23);
|
|
563
|
-
});
|
|
564
|
-
});
|
|
565
|
-
|
|
566
|
-
describe('entryPositions contract', () => {
|
|
567
|
-
it('emits entryPositions for horizontal legend', () => {
|
|
568
|
-
const legend = computeLegend(specWithColor, compactStrategy, theme, chartArea);
|
|
569
|
-
expect(legend.entries.length).toBeGreaterThan(0);
|
|
570
|
-
expect(legend.entryPositions).toBeDefined();
|
|
571
|
-
expect(legend.entryPositions).toHaveLength(legend.entries.length);
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
it('entryPositions have correct structure', () => {
|
|
575
|
-
const legend = computeLegend(specWithColor, compactStrategy, theme, chartArea);
|
|
576
|
-
for (const pos of legend.entryPositions!) {
|
|
577
|
-
expect(pos).toHaveProperty('x');
|
|
578
|
-
expect(pos).toHaveProperty('y');
|
|
579
|
-
expect(pos).toHaveProperty('labelX');
|
|
580
|
-
expect(pos).toHaveProperty('labelY');
|
|
581
|
-
expect(pos).toHaveProperty('width');
|
|
582
|
-
expect(pos).toHaveProperty('row');
|
|
583
|
-
expect(pos.labelX).toBeGreaterThan(pos.x);
|
|
584
|
-
}
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
it('emits entryPositions for right-positioned legend', () => {
|
|
588
|
-
const legend = computeLegend(specWithColor, fullStrategy, theme, chartArea);
|
|
589
|
-
expect(legend.entryPositions).toBeDefined();
|
|
590
|
-
expect(legend.entryPositions).toHaveLength(legend.entries.length);
|
|
591
|
-
for (const pos of legend.entryPositions!) {
|
|
592
|
-
expect(pos.row).toBeGreaterThanOrEqual(0);
|
|
593
|
-
}
|
|
594
|
-
});
|
|
595
|
-
|
|
596
|
-
it('emits rowHeight', () => {
|
|
597
|
-
const legend = computeLegend(specWithColor, compactStrategy, theme, chartArea);
|
|
598
|
-
expect(legend.rowHeight).toBeDefined();
|
|
599
|
-
expect(legend.rowHeight).toBeGreaterThan(0);
|
|
600
|
-
});
|
|
601
|
-
});
|
|
602
|
-
|
|
603
|
-
describe('rows missing the color field', () => {
|
|
604
|
-
// A layered spec flattens every layer's rows into `spec.data`, but the
|
|
605
|
-
// color encoding belongs to one layer. Rows from sibling layers (e.g. a
|
|
606
|
-
// diagonal reference-line) have no value for the color field and must not
|
|
607
|
-
// manufacture a phantom "undefined" legend entry.
|
|
608
|
-
it('ignores rows whose color field is undefined', () => {
|
|
609
|
-
const spec: NormalizedChartSpec = {
|
|
610
|
-
...specWithColor,
|
|
611
|
-
data: [
|
|
612
|
-
{ date: '2020', value: 10, country: 'US' },
|
|
613
|
-
{ date: '2021', value: 20, country: 'UK' },
|
|
614
|
-
// rule-layer rows: no `country` field
|
|
615
|
-
{ date: '2020', value: 0 },
|
|
616
|
-
{ date: '2022', value: 40 },
|
|
617
|
-
],
|
|
618
|
-
};
|
|
619
|
-
const legend = computeLegend(spec, fullStrategy, theme, chartArea);
|
|
620
|
-
expect(legend.entries.map((e) => e.label)).toEqual(['US', 'UK']);
|
|
621
|
-
});
|
|
622
|
-
|
|
623
|
-
it('keeps an explicit color domain authoritative despite phantom rows', () => {
|
|
624
|
-
const spec: NormalizedChartSpec = {
|
|
625
|
-
...specWithColor,
|
|
626
|
-
data: [
|
|
627
|
-
{ date: '2020', value: 10, tier: 'One F from takeover' },
|
|
628
|
-
{ date: '2021', value: 20, tier: 'One year behind' },
|
|
629
|
-
{ date: '2022', value: 30, tier: 'Other D/F campus' },
|
|
630
|
-
// diagonal reference-line layer, no `tier`
|
|
631
|
-
{ date: '2020', value: 0 },
|
|
632
|
-
{ date: '2022', value: 40 },
|
|
633
|
-
],
|
|
634
|
-
encoding: {
|
|
635
|
-
x: { field: 'date', type: 'temporal' },
|
|
636
|
-
y: { field: 'value', type: 'quantitative' },
|
|
637
|
-
color: {
|
|
638
|
-
field: 'tier',
|
|
639
|
-
type: 'nominal',
|
|
640
|
-
scale: {
|
|
641
|
-
domain: ['One F from takeover', 'One year behind', 'Other D/F campus'],
|
|
642
|
-
range: ['#fb7185', '#f59e0b', '#64748b'],
|
|
643
|
-
},
|
|
644
|
-
},
|
|
645
|
-
},
|
|
646
|
-
};
|
|
647
|
-
const legend = computeLegend(spec, fullStrategy, theme, chartArea);
|
|
648
|
-
expect(legend.entries.map((e) => e.label)).toEqual([
|
|
649
|
-
'One F from takeover',
|
|
650
|
-
'One year behind',
|
|
651
|
-
'Other D/F campus',
|
|
652
|
-
]);
|
|
653
|
-
expect(legend.entries).toHaveLength(3);
|
|
654
|
-
});
|
|
655
|
-
});
|
|
656
|
-
|
|
657
|
-
describe('swatch colors match mark colors', () => {
|
|
658
|
-
const energySpec = {
|
|
659
|
-
mark: 'waffle' as const,
|
|
660
|
-
data: [
|
|
661
|
-
{ source: 'Fossil fuels', share: 60 },
|
|
662
|
-
{ source: 'Renewables', share: 30 },
|
|
663
|
-
{ source: 'Nuclear', share: 10 },
|
|
664
|
-
],
|
|
665
|
-
encoding: {
|
|
666
|
-
theta: { field: 'share', type: 'quantitative' as const },
|
|
667
|
-
color: { field: 'source', type: 'nominal' as const },
|
|
668
|
-
},
|
|
669
|
-
};
|
|
670
|
-
|
|
671
|
-
/**
|
|
672
|
-
* Map each category to the fill its marks actually render with. Waffle
|
|
673
|
-
* cells carry the category in `data`, not `seriesKey`, so key off the
|
|
674
|
-
* color field.
|
|
675
|
-
*/
|
|
676
|
-
function markFillByLabel(layout: ReturnType<typeof compileChart>): Map<string, string> {
|
|
677
|
-
const fills = new Map<string, string>();
|
|
678
|
-
for (const mark of layout.marks) {
|
|
679
|
-
const label = mark.data?.source;
|
|
680
|
-
const fill = 'fill' in mark ? mark.fill : undefined;
|
|
681
|
-
if (typeof label === 'string' && typeof fill === 'string') fills.set(label, fill);
|
|
682
|
-
}
|
|
683
|
-
return fills;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
it('mutes the swatches of non-highlighted series alongside their marks', () => {
|
|
687
|
-
const layout = compileChart(
|
|
688
|
-
{
|
|
689
|
-
...energySpec,
|
|
690
|
-
encoding: {
|
|
691
|
-
...energySpec.encoding,
|
|
692
|
-
color: { ...energySpec.encoding.color, highlight: 'Renewables' },
|
|
693
|
-
},
|
|
694
|
-
},
|
|
695
|
-
{ width: 600, height: 400 },
|
|
696
|
-
);
|
|
697
|
-
|
|
698
|
-
const fills = markFillByLabel(layout);
|
|
699
|
-
expect(layout.legend.entries.length).toBe(3);
|
|
700
|
-
for (const entry of layout.legend.entries) {
|
|
701
|
-
expect(entry.color).toBe(fills.get(entry.label));
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
// The muted pair share one gray; the highlighted series does not.
|
|
705
|
-
const highlighted = layout.legend.entries.find((e) => e.label === 'Renewables');
|
|
706
|
-
const muted = layout.legend.entries.filter((e) => e.label !== 'Renewables');
|
|
707
|
-
expect(muted.map((e) => e.color)).toEqual([muted[0].color, muted[0].color]);
|
|
708
|
-
expect(highlighted?.color).not.toBe(muted[0].color);
|
|
709
|
-
});
|
|
710
|
-
|
|
711
|
-
it('matches mark colors when no highlight is active', () => {
|
|
712
|
-
const layout = compileChart(energySpec, { width: 600, height: 400 });
|
|
713
|
-
|
|
714
|
-
const fills = markFillByLabel(layout);
|
|
715
|
-
expect(layout.legend.entries.length).toBe(3);
|
|
716
|
-
for (const entry of layout.legend.entries) {
|
|
717
|
-
expect(entry.color).toBe(fills.get(entry.label));
|
|
718
|
-
}
|
|
719
|
-
});
|
|
720
|
-
});
|
|
721
|
-
});
|