@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.
- package/dist/index.d.ts +149 -12
- package/dist/index.js +804 -155
- 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,969 +0,0 @@
|
|
|
1
|
-
import type { ChartSpec, LayerSpec } from '@opendata-ai/openchart-core';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
import { compileChart, compileLayer } from '../compile';
|
|
4
|
-
import { flattenLayers } from '../compiler/normalize';
|
|
5
|
-
|
|
6
|
-
// ---------------------------------------------------------------------------
|
|
7
|
-
// Test data
|
|
8
|
-
// ---------------------------------------------------------------------------
|
|
9
|
-
|
|
10
|
-
const sharedData = [
|
|
11
|
-
{ month: 'Jan', sales: 100, target: 80, region: 'West' },
|
|
12
|
-
{ month: 'Feb', sales: 150, target: 120, region: 'West' },
|
|
13
|
-
{ month: 'Mar', sales: 130, target: 140, region: 'East' },
|
|
14
|
-
{ month: 'Apr', sales: 200, target: 160, region: 'East' },
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
const compileOpts = { width: 600, height: 400 };
|
|
18
|
-
|
|
19
|
-
// ---------------------------------------------------------------------------
|
|
20
|
-
// flattenLayers
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
|
|
23
|
-
describe('flattenLayers', () => {
|
|
24
|
-
it('flattens a simple two-layer spec into two ChartSpecs', () => {
|
|
25
|
-
const spec: LayerSpec = {
|
|
26
|
-
layer: [
|
|
27
|
-
{
|
|
28
|
-
mark: 'bar',
|
|
29
|
-
data: sharedData,
|
|
30
|
-
encoding: {
|
|
31
|
-
x: { field: 'sales', type: 'quantitative' },
|
|
32
|
-
y: { field: 'month', type: 'nominal' },
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
mark: 'line',
|
|
37
|
-
data: sharedData,
|
|
38
|
-
encoding: {
|
|
39
|
-
x: { field: 'month', type: 'nominal' },
|
|
40
|
-
y: { field: 'target', type: 'quantitative' },
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const leaves = flattenLayers(spec);
|
|
47
|
-
expect(leaves).toHaveLength(2);
|
|
48
|
-
expect(leaves[0].mark).toBe('bar');
|
|
49
|
-
expect(leaves[1].mark).toBe('line');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('inherits shared data from parent when child has none', () => {
|
|
53
|
-
// When children have data: [], they keep it. But if data is undefined, parent is used.
|
|
54
|
-
const specNoChildData: LayerSpec = {
|
|
55
|
-
data: sharedData,
|
|
56
|
-
layer: [
|
|
57
|
-
{
|
|
58
|
-
mark: 'bar',
|
|
59
|
-
encoding: {
|
|
60
|
-
x: { field: 'sales', type: 'quantitative' },
|
|
61
|
-
y: { field: 'month', type: 'nominal' },
|
|
62
|
-
},
|
|
63
|
-
} as ChartSpec,
|
|
64
|
-
{
|
|
65
|
-
mark: 'line',
|
|
66
|
-
encoding: {
|
|
67
|
-
x: { field: 'month', type: 'nominal' },
|
|
68
|
-
y: { field: 'target', type: 'quantitative' },
|
|
69
|
-
},
|
|
70
|
-
} as ChartSpec,
|
|
71
|
-
],
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const leaves = flattenLayers(specNoChildData);
|
|
75
|
-
expect(leaves).toHaveLength(2);
|
|
76
|
-
expect(leaves[0].data).toEqual(sharedData);
|
|
77
|
-
expect(leaves[1].data).toEqual(sharedData);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('inherits shared encoding from parent, child channels override', () => {
|
|
81
|
-
const spec: LayerSpec = {
|
|
82
|
-
data: sharedData,
|
|
83
|
-
encoding: {
|
|
84
|
-
x: { field: 'month', type: 'nominal' },
|
|
85
|
-
color: { field: 'region', type: 'nominal' },
|
|
86
|
-
},
|
|
87
|
-
layer: [
|
|
88
|
-
{
|
|
89
|
-
mark: 'bar',
|
|
90
|
-
encoding: {
|
|
91
|
-
y: { field: 'sales', type: 'quantitative' },
|
|
92
|
-
},
|
|
93
|
-
} as ChartSpec,
|
|
94
|
-
{
|
|
95
|
-
mark: 'line',
|
|
96
|
-
encoding: {
|
|
97
|
-
y: { field: 'target', type: 'quantitative' },
|
|
98
|
-
// Override x from parent
|
|
99
|
-
x: { field: 'month', type: 'ordinal' },
|
|
100
|
-
},
|
|
101
|
-
} as ChartSpec,
|
|
102
|
-
],
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const leaves = flattenLayers(spec);
|
|
106
|
-
expect(leaves).toHaveLength(2);
|
|
107
|
-
|
|
108
|
-
// First leaf: inherits x and color from parent, has own y
|
|
109
|
-
expect(leaves[0].encoding.x?.field).toBe('month');
|
|
110
|
-
expect(leaves[0].encoding.x?.type).toBe('nominal');
|
|
111
|
-
expect(leaves[0].encoding.y?.field).toBe('sales');
|
|
112
|
-
expect(leaves[0].encoding.color).toEqual({ field: 'region', type: 'nominal' });
|
|
113
|
-
|
|
114
|
-
// Second leaf: overrides x, inherits color from parent, has own y
|
|
115
|
-
expect(leaves[1].encoding.x?.type).toBe('ordinal');
|
|
116
|
-
expect(leaves[1].encoding.y?.field).toBe('target');
|
|
117
|
-
expect(leaves[1].encoding.color).toEqual({ field: 'region', type: 'nominal' });
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('chains parent transforms before child transforms', () => {
|
|
121
|
-
const spec: LayerSpec = {
|
|
122
|
-
data: sharedData,
|
|
123
|
-
transform: [{ filter: { field: 'region', equal: 'West' } }],
|
|
124
|
-
layer: [
|
|
125
|
-
{
|
|
126
|
-
mark: 'bar',
|
|
127
|
-
transform: [{ filter: { field: 'sales', gt: 100 } }],
|
|
128
|
-
encoding: {
|
|
129
|
-
x: { field: 'sales', type: 'quantitative' },
|
|
130
|
-
y: { field: 'month', type: 'nominal' },
|
|
131
|
-
},
|
|
132
|
-
} as ChartSpec,
|
|
133
|
-
],
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const leaves = flattenLayers(spec);
|
|
137
|
-
expect(leaves).toHaveLength(1);
|
|
138
|
-
expect(leaves[0].transform).toHaveLength(2);
|
|
139
|
-
// Parent transform first
|
|
140
|
-
expect(leaves[0].transform![0]).toEqual({ filter: { field: 'region', equal: 'West' } });
|
|
141
|
-
// Child transform second
|
|
142
|
-
expect(leaves[0].transform![1]).toEqual({ filter: { field: 'sales', gt: 100 } });
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it('handles nested layers recursively', () => {
|
|
146
|
-
const spec: LayerSpec = {
|
|
147
|
-
data: sharedData,
|
|
148
|
-
layer: [
|
|
149
|
-
{
|
|
150
|
-
// Nested LayerSpec
|
|
151
|
-
layer: [
|
|
152
|
-
{
|
|
153
|
-
mark: 'bar',
|
|
154
|
-
encoding: {
|
|
155
|
-
x: { field: 'sales', type: 'quantitative' },
|
|
156
|
-
y: { field: 'month', type: 'nominal' },
|
|
157
|
-
},
|
|
158
|
-
} as ChartSpec,
|
|
159
|
-
{
|
|
160
|
-
mark: 'line',
|
|
161
|
-
encoding: {
|
|
162
|
-
x: { field: 'month', type: 'nominal' },
|
|
163
|
-
y: { field: 'target', type: 'quantitative' },
|
|
164
|
-
},
|
|
165
|
-
} as ChartSpec,
|
|
166
|
-
],
|
|
167
|
-
} as LayerSpec,
|
|
168
|
-
{
|
|
169
|
-
mark: 'point',
|
|
170
|
-
encoding: {
|
|
171
|
-
x: { field: 'month', type: 'nominal' },
|
|
172
|
-
y: { field: 'sales', type: 'quantitative' },
|
|
173
|
-
},
|
|
174
|
-
} as ChartSpec,
|
|
175
|
-
],
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
const leaves = flattenLayers(spec);
|
|
179
|
-
expect(leaves).toHaveLength(3);
|
|
180
|
-
expect(leaves[0].mark).toBe('bar');
|
|
181
|
-
expect(leaves[1].mark).toBe('line');
|
|
182
|
-
expect(leaves[2].mark).toBe('point');
|
|
183
|
-
// All should inherit data from the top-level parent
|
|
184
|
-
for (const leaf of leaves) {
|
|
185
|
-
expect(leaf.data).toEqual(sharedData);
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// ---------------------------------------------------------------------------
|
|
191
|
-
// compileLayer
|
|
192
|
-
// ---------------------------------------------------------------------------
|
|
193
|
-
|
|
194
|
-
describe('compileLayer', () => {
|
|
195
|
-
it('compiles a basic two-layer spec into a single ChartLayout', () => {
|
|
196
|
-
const spec: LayerSpec = {
|
|
197
|
-
layer: [
|
|
198
|
-
{
|
|
199
|
-
mark: 'bar' as const,
|
|
200
|
-
data: [
|
|
201
|
-
{ name: 'A', value: 10 },
|
|
202
|
-
{ name: 'B', value: 30 },
|
|
203
|
-
{ name: 'C', value: 20 },
|
|
204
|
-
],
|
|
205
|
-
encoding: {
|
|
206
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
207
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
208
|
-
},
|
|
209
|
-
},
|
|
210
|
-
{
|
|
211
|
-
mark: 'bar' as const,
|
|
212
|
-
data: [
|
|
213
|
-
{ name: 'A', value: 15 },
|
|
214
|
-
{ name: 'B', value: 25 },
|
|
215
|
-
{ name: 'C', value: 18 },
|
|
216
|
-
],
|
|
217
|
-
encoding: {
|
|
218
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
219
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
],
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
const layout = compileLayer(spec, compileOpts);
|
|
226
|
-
|
|
227
|
-
// Should have a valid chart area
|
|
228
|
-
expect(layout.area.width).toBeGreaterThan(0);
|
|
229
|
-
expect(layout.area.height).toBeGreaterThan(0);
|
|
230
|
-
|
|
231
|
-
// Should have marks from both layers
|
|
232
|
-
expect(layout.marks.length).toBeGreaterThan(0);
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('produces combined marks from all layers', () => {
|
|
236
|
-
const spec: LayerSpec = {
|
|
237
|
-
layer: [
|
|
238
|
-
{
|
|
239
|
-
mark: 'bar' as const,
|
|
240
|
-
data: [
|
|
241
|
-
{ name: 'A', value: 10 },
|
|
242
|
-
{ name: 'B', value: 20 },
|
|
243
|
-
],
|
|
244
|
-
encoding: {
|
|
245
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
246
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
247
|
-
},
|
|
248
|
-
},
|
|
249
|
-
{
|
|
250
|
-
mark: 'bar' as const,
|
|
251
|
-
data: [
|
|
252
|
-
{ name: 'A', value: 5 },
|
|
253
|
-
{ name: 'B', value: 15 },
|
|
254
|
-
],
|
|
255
|
-
encoding: {
|
|
256
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
257
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
258
|
-
},
|
|
259
|
-
},
|
|
260
|
-
],
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
const layout = compileLayer(spec, compileOpts);
|
|
264
|
-
// Two layers each with 2 data points should produce marks from both
|
|
265
|
-
expect(layout.marks.length).toBeGreaterThanOrEqual(4);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
it('uses layer-level chrome over leaf chrome', () => {
|
|
269
|
-
const spec: LayerSpec = {
|
|
270
|
-
chrome: {
|
|
271
|
-
title: 'Layer Title',
|
|
272
|
-
subtitle: 'Combined view',
|
|
273
|
-
},
|
|
274
|
-
layer: [
|
|
275
|
-
{
|
|
276
|
-
mark: 'bar' as const,
|
|
277
|
-
data: [{ name: 'A', value: 10 }],
|
|
278
|
-
encoding: {
|
|
279
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
280
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
281
|
-
},
|
|
282
|
-
chrome: {
|
|
283
|
-
title: 'Bar Title',
|
|
284
|
-
},
|
|
285
|
-
},
|
|
286
|
-
],
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
const layout = compileLayer(spec, compileOpts);
|
|
290
|
-
// The layer-level chrome should be used
|
|
291
|
-
expect(layout.chrome.title?.text).toBe('Layer Title');
|
|
292
|
-
expect(layout.chrome.subtitle?.text).toBe('Combined view');
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
it('unions scale domains across layers for shared scales', () => {
|
|
296
|
-
// Layer 1 has values 0-50, layer 2 has values 0-100.
|
|
297
|
-
// The shared scale should encompass 0-100.
|
|
298
|
-
const spec: LayerSpec = {
|
|
299
|
-
layer: [
|
|
300
|
-
{
|
|
301
|
-
mark: 'bar' as const,
|
|
302
|
-
data: [{ name: 'A', value: 50 }],
|
|
303
|
-
encoding: {
|
|
304
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
305
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
306
|
-
},
|
|
307
|
-
},
|
|
308
|
-
{
|
|
309
|
-
mark: 'bar' as const,
|
|
310
|
-
data: [{ name: 'A', value: 100 }],
|
|
311
|
-
encoding: {
|
|
312
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
313
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
314
|
-
},
|
|
315
|
-
},
|
|
316
|
-
],
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
const layout = compileLayer(spec, compileOpts);
|
|
320
|
-
// Layout should compile without error and have marks
|
|
321
|
-
expect(layout.marks.length).toBeGreaterThan(0);
|
|
322
|
-
expect(layout.area.width).toBeGreaterThan(0);
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
it('unions the y domain even when x is nominal', () => {
|
|
326
|
-
// The channel loop used to `return` on the first channel that couldn't take
|
|
327
|
-
// part. x is visited first, so a nominal x abandoned y as well: each leaf
|
|
328
|
-
// then re-fit y to its own rows and every bar rendered full-height,
|
|
329
|
-
// regardless of value. Bars-plus-labels is the most common layered shape
|
|
330
|
-
// there is, so the function missed its own headline case.
|
|
331
|
-
const spec: LayerSpec = {
|
|
332
|
-
layer: [
|
|
333
|
-
{
|
|
334
|
-
mark: 'bar' as const,
|
|
335
|
-
data: [{ n: 'A', v: 50 }],
|
|
336
|
-
encoding: {
|
|
337
|
-
x: { field: 'n', type: 'nominal' as const },
|
|
338
|
-
y: { field: 'v', type: 'quantitative' as const },
|
|
339
|
-
},
|
|
340
|
-
},
|
|
341
|
-
{
|
|
342
|
-
mark: 'bar' as const,
|
|
343
|
-
data: [{ n: 'B', v: 100 }],
|
|
344
|
-
encoding: {
|
|
345
|
-
x: { field: 'n', type: 'nominal' as const },
|
|
346
|
-
y: { field: 'v', type: 'quantitative' as const },
|
|
347
|
-
},
|
|
348
|
-
},
|
|
349
|
-
],
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
const rects = compileLayer(spec, compileOpts).marks.filter((m) => m.type === 'rect');
|
|
353
|
-
expect(rects).toHaveLength(2);
|
|
354
|
-
|
|
355
|
-
// Sharing a zero-based domain, the 50-bar is exactly half the 100-bar.
|
|
356
|
-
const [half, full] = rects.map((r) => (r as { height: number }).height);
|
|
357
|
-
expect(half).toBeCloseTo(full / 2, 1);
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
it('gives layered bars the same geometry as the equivalent single chart', () => {
|
|
361
|
-
// The shared domain is pinned onto each leaf as `scale.domain`, and an
|
|
362
|
-
// explicit domain skips the `zero !== false` baselining a normal scale
|
|
363
|
-
// applies. Without folding zero back in, the union lands as a literal
|
|
364
|
-
// [50, 100] and the 50-bar collapses to a 1px sliver against its baseline.
|
|
365
|
-
const encoding = {
|
|
366
|
-
x: { field: 'n', type: 'nominal' as const },
|
|
367
|
-
y: { field: 'v', type: 'quantitative' as const },
|
|
368
|
-
};
|
|
369
|
-
const heights = (layout: { marks: Array<{ type: string }> }) =>
|
|
370
|
-
layout.marks
|
|
371
|
-
.filter((m) => m.type === 'rect')
|
|
372
|
-
.map((r) => Number((r as unknown as { height: number }).height.toFixed(1)));
|
|
373
|
-
|
|
374
|
-
const single = compileChart(
|
|
375
|
-
{
|
|
376
|
-
mark: 'bar',
|
|
377
|
-
data: [
|
|
378
|
-
{ n: 'A', v: 50 },
|
|
379
|
-
{ n: 'B', v: 100 },
|
|
380
|
-
],
|
|
381
|
-
encoding,
|
|
382
|
-
},
|
|
383
|
-
compileOpts,
|
|
384
|
-
);
|
|
385
|
-
const layered = compileLayer(
|
|
386
|
-
{
|
|
387
|
-
layer: [
|
|
388
|
-
{ mark: 'bar' as const, data: [{ n: 'A', v: 50 }], encoding },
|
|
389
|
-
{ mark: 'bar' as const, data: [{ n: 'B', v: 100 }], encoding },
|
|
390
|
-
],
|
|
391
|
-
},
|
|
392
|
-
compileOpts,
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
expect(heights(layered)).toEqual(heights(single));
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
it('compiles a single-layer LayerSpec identically to a ChartSpec', () => {
|
|
399
|
-
const chartData = [
|
|
400
|
-
{ name: 'A', value: 10 },
|
|
401
|
-
{ name: 'B', value: 30 },
|
|
402
|
-
];
|
|
403
|
-
|
|
404
|
-
const layerSpec: LayerSpec = {
|
|
405
|
-
layer: [
|
|
406
|
-
{
|
|
407
|
-
mark: 'bar' as const,
|
|
408
|
-
data: chartData,
|
|
409
|
-
encoding: {
|
|
410
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
411
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
412
|
-
},
|
|
413
|
-
},
|
|
414
|
-
],
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
const layout = compileLayer(layerSpec, compileOpts);
|
|
418
|
-
expect(layout.area.width).toBeGreaterThan(0);
|
|
419
|
-
expect(layout.marks.length).toBeGreaterThan(0);
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
// -------------------------------------------------------------------------
|
|
423
|
-
// Independent y-scales (dual-axis)
|
|
424
|
-
// -------------------------------------------------------------------------
|
|
425
|
-
|
|
426
|
-
it('produces a y2 axis when resolve.scale.y is independent', () => {
|
|
427
|
-
const spec: LayerSpec = {
|
|
428
|
-
resolve: { scale: { y: 'independent' } },
|
|
429
|
-
layer: [
|
|
430
|
-
{
|
|
431
|
-
mark: 'bar' as const,
|
|
432
|
-
data: [
|
|
433
|
-
{ year: '2025', revenue: 10_000_000 },
|
|
434
|
-
{ year: '2026', revenue: 15_000_000 },
|
|
435
|
-
],
|
|
436
|
-
encoding: {
|
|
437
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
438
|
-
y: { field: 'revenue', type: 'quantitative' as const, axis: { title: 'Revenue ($)' } },
|
|
439
|
-
},
|
|
440
|
-
},
|
|
441
|
-
{
|
|
442
|
-
mark: 'line' as const,
|
|
443
|
-
data: [
|
|
444
|
-
{ year: '2025', enrollment: 30_000 },
|
|
445
|
-
{ year: '2026', enrollment: 40_000 },
|
|
446
|
-
],
|
|
447
|
-
encoding: {
|
|
448
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
449
|
-
y: {
|
|
450
|
-
field: 'enrollment',
|
|
451
|
-
type: 'quantitative' as const,
|
|
452
|
-
axis: { title: 'Enrollment' },
|
|
453
|
-
},
|
|
454
|
-
},
|
|
455
|
-
},
|
|
456
|
-
],
|
|
457
|
-
};
|
|
458
|
-
|
|
459
|
-
const layout = compileLayer(spec, compileOpts);
|
|
460
|
-
|
|
461
|
-
expect(layout.axes.y2).toBeDefined();
|
|
462
|
-
expect(layout.axes.y2!.orient).toBe('right');
|
|
463
|
-
expect(layout.axes.y).toBeDefined();
|
|
464
|
-
expect(layout.axes.x).toBeDefined();
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
it('positions y2 axis title on the right side of the chart', () => {
|
|
468
|
-
const spec: LayerSpec = {
|
|
469
|
-
resolve: { scale: { y: 'independent' } },
|
|
470
|
-
layer: [
|
|
471
|
-
{
|
|
472
|
-
mark: 'bar' as const,
|
|
473
|
-
data: [
|
|
474
|
-
{ year: '2025', revenue: 10_000_000 },
|
|
475
|
-
{ year: '2026', revenue: 15_000_000 },
|
|
476
|
-
],
|
|
477
|
-
encoding: {
|
|
478
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
479
|
-
y: { field: 'revenue', type: 'quantitative' as const, axis: { title: 'Revenue ($)' } },
|
|
480
|
-
},
|
|
481
|
-
},
|
|
482
|
-
{
|
|
483
|
-
mark: 'line' as const,
|
|
484
|
-
data: [
|
|
485
|
-
{ year: '2025', enrollment: 30_000 },
|
|
486
|
-
{ year: '2026', enrollment: 40_000 },
|
|
487
|
-
],
|
|
488
|
-
encoding: {
|
|
489
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
490
|
-
y: {
|
|
491
|
-
field: 'enrollment',
|
|
492
|
-
type: 'quantitative' as const,
|
|
493
|
-
axis: { title: 'Enrollment' },
|
|
494
|
-
},
|
|
495
|
-
},
|
|
496
|
-
},
|
|
497
|
-
],
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
const layout = compileLayer(spec, compileOpts);
|
|
501
|
-
const y1Title = layout.axes.y!.titlePosition;
|
|
502
|
-
const y2Title = layout.axes.y2!.titlePosition;
|
|
503
|
-
|
|
504
|
-
expect(y1Title).toBeDefined();
|
|
505
|
-
expect(y2Title).toBeDefined();
|
|
506
|
-
|
|
507
|
-
// y1 title should be left of chart area, y2 title should be right
|
|
508
|
-
expect(y1Title!.x).toBeLessThan(layout.area.x);
|
|
509
|
-
expect(y2Title!.x).toBeGreaterThan(layout.area.x + layout.area.width);
|
|
510
|
-
|
|
511
|
-
// y2 title should rotate clockwise (90), y1 counter-clockwise (-90)
|
|
512
|
-
expect(y1Title!.angle).toBe(-90);
|
|
513
|
-
expect(y2Title!.angle).toBe(90);
|
|
514
|
-
});
|
|
515
|
-
|
|
516
|
-
it('tags layer-1 marks with yScale y2', () => {
|
|
517
|
-
const spec: LayerSpec = {
|
|
518
|
-
resolve: { scale: { y: 'independent' } },
|
|
519
|
-
layer: [
|
|
520
|
-
{
|
|
521
|
-
mark: 'bar' as const,
|
|
522
|
-
data: [{ year: '2025', revenue: 10_000_000 }],
|
|
523
|
-
encoding: {
|
|
524
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
525
|
-
y: { field: 'revenue', type: 'quantitative' as const },
|
|
526
|
-
},
|
|
527
|
-
},
|
|
528
|
-
{
|
|
529
|
-
mark: 'line' as const,
|
|
530
|
-
data: [{ year: '2025', enrollment: 30_000 }],
|
|
531
|
-
encoding: {
|
|
532
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
533
|
-
y: { field: 'enrollment', type: 'quantitative' as const },
|
|
534
|
-
},
|
|
535
|
-
},
|
|
536
|
-
],
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
const layout = compileLayer(spec, compileOpts);
|
|
540
|
-
const y2Marks = layout.marks.filter((m) => m.yScale === 'y2');
|
|
541
|
-
expect(y2Marks.length).toBeGreaterThan(0);
|
|
542
|
-
});
|
|
543
|
-
|
|
544
|
-
it('strips gridlines from y2 axis', () => {
|
|
545
|
-
const spec: LayerSpec = {
|
|
546
|
-
resolve: { scale: { y: 'independent' } },
|
|
547
|
-
layer: [
|
|
548
|
-
{
|
|
549
|
-
mark: 'bar' as const,
|
|
550
|
-
data: [{ year: '2025', revenue: 10_000_000 }],
|
|
551
|
-
encoding: {
|
|
552
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
553
|
-
y: { field: 'revenue', type: 'quantitative' as const },
|
|
554
|
-
},
|
|
555
|
-
},
|
|
556
|
-
{
|
|
557
|
-
mark: 'line' as const,
|
|
558
|
-
data: [{ year: '2025', enrollment: 30_000 }],
|
|
559
|
-
encoding: {
|
|
560
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
561
|
-
y: { field: 'enrollment', type: 'quantitative' as const },
|
|
562
|
-
},
|
|
563
|
-
},
|
|
564
|
-
],
|
|
565
|
-
};
|
|
566
|
-
|
|
567
|
-
const layout = compileLayer(spec, compileOpts);
|
|
568
|
-
expect(layout.axes.y2!.gridlines).toEqual([]);
|
|
569
|
-
});
|
|
570
|
-
|
|
571
|
-
it('produces no y2 axis when resolve is absent', () => {
|
|
572
|
-
const spec: LayerSpec = {
|
|
573
|
-
layer: [
|
|
574
|
-
{
|
|
575
|
-
mark: 'bar' as const,
|
|
576
|
-
data: [{ name: 'A', value: 10 }],
|
|
577
|
-
encoding: {
|
|
578
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
579
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
580
|
-
},
|
|
581
|
-
},
|
|
582
|
-
{
|
|
583
|
-
mark: 'bar' as const,
|
|
584
|
-
data: [{ name: 'A', value: 20 }],
|
|
585
|
-
encoding: {
|
|
586
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
587
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
588
|
-
},
|
|
589
|
-
},
|
|
590
|
-
],
|
|
591
|
-
};
|
|
592
|
-
|
|
593
|
-
const layout = compileLayer(spec, compileOpts);
|
|
594
|
-
expect(layout.axes.y2).toBeUndefined();
|
|
595
|
-
});
|
|
596
|
-
|
|
597
|
-
it('produces mixed mark types from bar + line layers', () => {
|
|
598
|
-
const spec: LayerSpec = {
|
|
599
|
-
resolve: { scale: { y: 'independent' } },
|
|
600
|
-
layer: [
|
|
601
|
-
{
|
|
602
|
-
mark: 'bar' as const,
|
|
603
|
-
data: [
|
|
604
|
-
{ year: '2025', revenue: 10_000_000 },
|
|
605
|
-
{ year: '2026', revenue: 15_000_000 },
|
|
606
|
-
],
|
|
607
|
-
encoding: {
|
|
608
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
609
|
-
y: { field: 'revenue', type: 'quantitative' as const },
|
|
610
|
-
},
|
|
611
|
-
},
|
|
612
|
-
{
|
|
613
|
-
mark: 'line' as const,
|
|
614
|
-
data: [
|
|
615
|
-
{ year: '2025', enrollment: 30_000 },
|
|
616
|
-
{ year: '2026', enrollment: 40_000 },
|
|
617
|
-
],
|
|
618
|
-
encoding: {
|
|
619
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
620
|
-
y: { field: 'enrollment', type: 'quantitative' as const },
|
|
621
|
-
},
|
|
622
|
-
},
|
|
623
|
-
],
|
|
624
|
-
};
|
|
625
|
-
|
|
626
|
-
const layout = compileLayer(spec, compileOpts);
|
|
627
|
-
const markTypes = new Set(layout.marks.map((m) => m.type));
|
|
628
|
-
expect(markTypes.has('rect')).toBe(true);
|
|
629
|
-
expect(markTypes.has('line')).toBe(true);
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
it('throws on >2 layers with independent y-scales', () => {
|
|
633
|
-
const spec: LayerSpec = {
|
|
634
|
-
resolve: { scale: { y: 'independent' } },
|
|
635
|
-
layer: [
|
|
636
|
-
{
|
|
637
|
-
mark: 'bar' as const,
|
|
638
|
-
data: [{ year: '2025', a: 10 }],
|
|
639
|
-
encoding: {
|
|
640
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
641
|
-
y: { field: 'a', type: 'quantitative' as const },
|
|
642
|
-
},
|
|
643
|
-
},
|
|
644
|
-
{
|
|
645
|
-
mark: 'line' as const,
|
|
646
|
-
data: [{ year: '2025', b: 20 }],
|
|
647
|
-
encoding: {
|
|
648
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
649
|
-
y: { field: 'b', type: 'quantitative' as const },
|
|
650
|
-
},
|
|
651
|
-
},
|
|
652
|
-
{
|
|
653
|
-
mark: 'area' as const,
|
|
654
|
-
data: [{ year: '2025', c: 30 }],
|
|
655
|
-
encoding: {
|
|
656
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
657
|
-
y: { field: 'c', type: 'quantitative' as const },
|
|
658
|
-
},
|
|
659
|
-
},
|
|
660
|
-
],
|
|
661
|
-
};
|
|
662
|
-
|
|
663
|
-
expect(() => compileLayer(spec, compileOpts)).toThrow(/at most 2 layers/);
|
|
664
|
-
});
|
|
665
|
-
|
|
666
|
-
it('throws on mismatched x-field types across layers', () => {
|
|
667
|
-
const spec: LayerSpec = {
|
|
668
|
-
resolve: { scale: { y: 'independent' } },
|
|
669
|
-
layer: [
|
|
670
|
-
{
|
|
671
|
-
mark: 'bar' as const,
|
|
672
|
-
data: [{ year: '2025', a: 10 }],
|
|
673
|
-
encoding: {
|
|
674
|
-
x: { field: 'year', type: 'nominal' as const },
|
|
675
|
-
y: { field: 'a', type: 'quantitative' as const },
|
|
676
|
-
},
|
|
677
|
-
},
|
|
678
|
-
{
|
|
679
|
-
mark: 'line' as const,
|
|
680
|
-
data: [{ year: '2025-01-01', b: 20 }],
|
|
681
|
-
encoding: {
|
|
682
|
-
x: { field: 'year', type: 'temporal' as const },
|
|
683
|
-
y: { field: 'b', type: 'quantitative' as const },
|
|
684
|
-
},
|
|
685
|
-
},
|
|
686
|
-
],
|
|
687
|
-
};
|
|
688
|
-
|
|
689
|
-
expect(() => compileLayer(spec, compileOpts)).toThrow(/matching x-field types/);
|
|
690
|
-
});
|
|
691
|
-
|
|
692
|
-
it('deduplicates legend entries when layers share a color field', () => {
|
|
693
|
-
const spec: LayerSpec = {
|
|
694
|
-
layer: [
|
|
695
|
-
{
|
|
696
|
-
mark: 'bar' as const,
|
|
697
|
-
data: [
|
|
698
|
-
{ name: 'A', value: 10, cat: 'X' },
|
|
699
|
-
{ name: 'B', value: 20, cat: 'Y' },
|
|
700
|
-
],
|
|
701
|
-
encoding: {
|
|
702
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
703
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
704
|
-
color: { field: 'cat', type: 'nominal' as const },
|
|
705
|
-
},
|
|
706
|
-
},
|
|
707
|
-
{
|
|
708
|
-
mark: 'bar' as const,
|
|
709
|
-
data: [
|
|
710
|
-
{ name: 'A', value: 5, cat: 'X' },
|
|
711
|
-
{ name: 'B', value: 15, cat: 'Y' },
|
|
712
|
-
],
|
|
713
|
-
encoding: {
|
|
714
|
-
x: { field: 'value', type: 'quantitative' as const },
|
|
715
|
-
y: { field: 'name', type: 'nominal' as const },
|
|
716
|
-
color: { field: 'cat', type: 'nominal' as const },
|
|
717
|
-
},
|
|
718
|
-
},
|
|
719
|
-
],
|
|
720
|
-
};
|
|
721
|
-
|
|
722
|
-
const layout = compileLayer(spec, compileOpts);
|
|
723
|
-
// Should have exactly 2 legend entries (X, Y), not 4
|
|
724
|
-
const uniqueLabels = new Set(layout.legend.entries.map((e) => e.label));
|
|
725
|
-
expect(uniqueLabels.size).toBe(2);
|
|
726
|
-
expect(uniqueLabels.has('X')).toBe(true);
|
|
727
|
-
expect(uniqueLabels.has('Y')).toBe(true);
|
|
728
|
-
});
|
|
729
|
-
|
|
730
|
-
/**
|
|
731
|
-
* `legends` is the slot the renderer iterates; `legend` is the back-compat
|
|
732
|
-
* alias. The merge used to rebuild only `legend`, leaving `legends[0]` as the
|
|
733
|
-
* primary leaf's *pre-merge* legend -- so the second layer's series silently
|
|
734
|
-
* vanished from the drawn key while `layout.legend` still listed it.
|
|
735
|
-
*
|
|
736
|
-
* The leaves must color on DIFFERENT fields to exercise this: sharing one field
|
|
737
|
-
* makes the primary spec's concatenated data yield both entries on its own, and
|
|
738
|
-
* the merge becomes a no-op that proves nothing.
|
|
739
|
-
*/
|
|
740
|
-
it('merges layer legend entries into `legends`, not just `legend`', () => {
|
|
741
|
-
const spec: LayerSpec = {
|
|
742
|
-
layer: [
|
|
743
|
-
{
|
|
744
|
-
mark: 'bar' as const,
|
|
745
|
-
data: [{ name: 'A', value: 10, cat: 'Bars' }],
|
|
746
|
-
encoding: {
|
|
747
|
-
x: { field: 'name', type: 'ordinal' as const },
|
|
748
|
-
y: { field: 'value', type: 'quantitative' as const },
|
|
749
|
-
color: { field: 'cat', type: 'nominal' as const },
|
|
750
|
-
},
|
|
751
|
-
},
|
|
752
|
-
{
|
|
753
|
-
mark: 'line' as const,
|
|
754
|
-
data: [{ name: 'A', value: 8, series: 'Trend' }],
|
|
755
|
-
encoding: {
|
|
756
|
-
x: { field: 'name', type: 'ordinal' as const },
|
|
757
|
-
y: { field: 'value', type: 'quantitative' as const },
|
|
758
|
-
color: { field: 'series', type: 'nominal' as const },
|
|
759
|
-
},
|
|
760
|
-
},
|
|
761
|
-
],
|
|
762
|
-
};
|
|
763
|
-
|
|
764
|
-
const layout = compileLayer(spec, compileOpts);
|
|
765
|
-
const rendered = layout.legends[0];
|
|
766
|
-
expect('entries' in rendered).toBe(true);
|
|
767
|
-
if (!('entries' in rendered)) return;
|
|
768
|
-
|
|
769
|
-
const labels = rendered.entries.map((e) => e.label);
|
|
770
|
-
expect(labels).toContain('Bars');
|
|
771
|
-
expect(labels).toContain('Trend');
|
|
772
|
-
// What renders is what `legend` advertises.
|
|
773
|
-
expect(labels).toEqual(layout.legend.entries.map((e) => e.label));
|
|
774
|
-
});
|
|
775
|
-
|
|
776
|
-
it('remaps x-coordinates when area is layer 0 and bars are layer 1', () => {
|
|
777
|
-
// Inverse ordering: area/line on left axis, bars on right. The x-remapping
|
|
778
|
-
// logic must detect that layer 1 has bars and remap layer 0's area mark instead.
|
|
779
|
-
const years = ['2020', '2021', '2022', '2023'];
|
|
780
|
-
const spec: LayerSpec = {
|
|
781
|
-
resolve: { scale: { y: 'independent' } },
|
|
782
|
-
layer: [
|
|
783
|
-
{
|
|
784
|
-
mark: 'area' as const,
|
|
785
|
-
data: years.map((y, i) => ({ year: y, temp: 60 + i * 3 })),
|
|
786
|
-
encoding: {
|
|
787
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
788
|
-
y: { field: 'temp', type: 'quantitative' as const },
|
|
789
|
-
},
|
|
790
|
-
},
|
|
791
|
-
{
|
|
792
|
-
mark: 'bar' as const,
|
|
793
|
-
data: years.map((y, i) => ({ year: y, precip: i * 0.5 })),
|
|
794
|
-
encoding: {
|
|
795
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
796
|
-
y: { field: 'precip', type: 'quantitative' as const },
|
|
797
|
-
},
|
|
798
|
-
},
|
|
799
|
-
],
|
|
800
|
-
};
|
|
801
|
-
|
|
802
|
-
const layout = compileLayer(spec, compileOpts);
|
|
803
|
-
|
|
804
|
-
// y2 axis belongs to the bar layer (layer 1)
|
|
805
|
-
expect(layout.axes.y2).toBeDefined();
|
|
806
|
-
expect(layout.axes.y2!.orient).toBe('right');
|
|
807
|
-
|
|
808
|
-
// Area marks come from layer 0 (left axis, no yScale tag)
|
|
809
|
-
const areaMarks = layout.marks.filter((m) => m.type === 'area');
|
|
810
|
-
expect(areaMarks.length).toBeGreaterThan(0);
|
|
811
|
-
for (const m of areaMarks) {
|
|
812
|
-
expect(m.yScale).toBeUndefined();
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
// Rect marks come from layer 1 (right axis, yScale: 'y2')
|
|
816
|
-
const rectMarks = layout.marks.filter((m) => m.type === 'rect');
|
|
817
|
-
expect(rectMarks.length).toBeGreaterThan(0);
|
|
818
|
-
for (const m of rectMarks) {
|
|
819
|
-
expect(m.yScale).toBe('y2');
|
|
820
|
-
}
|
|
821
|
-
});
|
|
822
|
-
|
|
823
|
-
it('offsets layer-1 discrete mark tooltip keys by layer-0 mark count', () => {
|
|
824
|
-
// Bars in layer 0 and layer 1. Each bar layer has 2 bars.
|
|
825
|
-
// Layer 0 bar at index 0 -> key 'rect-0', layer 1 bar at index 0 -> key 'rect-2'
|
|
826
|
-
// (offset by 2, the number of marks in layer 0).
|
|
827
|
-
const spec: LayerSpec = {
|
|
828
|
-
resolve: { scale: { y: 'independent' } },
|
|
829
|
-
layer: [
|
|
830
|
-
{
|
|
831
|
-
mark: 'bar' as const,
|
|
832
|
-
data: [
|
|
833
|
-
{ year: '2020', a: 10 },
|
|
834
|
-
{ year: '2021', a: 20 },
|
|
835
|
-
],
|
|
836
|
-
encoding: {
|
|
837
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
838
|
-
y: { field: 'a', type: 'quantitative' as const },
|
|
839
|
-
},
|
|
840
|
-
},
|
|
841
|
-
{
|
|
842
|
-
mark: 'bar' as const,
|
|
843
|
-
data: [
|
|
844
|
-
{ year: '2020', b: 5 },
|
|
845
|
-
{ year: '2021', b: 15 },
|
|
846
|
-
],
|
|
847
|
-
encoding: {
|
|
848
|
-
x: { field: 'year', type: 'ordinal' as const },
|
|
849
|
-
y: { field: 'b', type: 'quantitative' as const },
|
|
850
|
-
},
|
|
851
|
-
},
|
|
852
|
-
],
|
|
853
|
-
};
|
|
854
|
-
|
|
855
|
-
const layout = compileLayer(spec, compileOpts);
|
|
856
|
-
const l0RectCount = layout.marks
|
|
857
|
-
.slice(0, layout.marks.length)
|
|
858
|
-
.filter((m, i) => m.type === 'rect' && i < layout.marks.length / 2).length;
|
|
859
|
-
|
|
860
|
-
// Every rect mark in the combined array should have a tooltip descriptor
|
|
861
|
-
// under its actual combined-array index key.
|
|
862
|
-
const rectMarks = layout.marks.filter((m) => m.type === 'rect');
|
|
863
|
-
for (let i = 0; i < rectMarks.length; i++) {
|
|
864
|
-
const globalIndex = layout.marks.indexOf(rectMarks[i]);
|
|
865
|
-
expect(layout.tooltipDescriptors.has(`rect-${globalIndex}`)).toBe(true);
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
// No stale 'l1-rect-N' prefixed keys should exist
|
|
869
|
-
for (const key of layout.tooltipDescriptors.keys()) {
|
|
870
|
-
expect(key).not.toMatch(/^l1-/);
|
|
871
|
-
}
|
|
872
|
-
|
|
873
|
-
void l0RectCount; // used for conceptual clarity above
|
|
874
|
-
});
|
|
875
|
-
|
|
876
|
-
it('positions leaf-layer marks in the primary layout coordinate space', () => {
|
|
877
|
-
// Regression: leaves were compiled without the layer-level chrome, legend,
|
|
878
|
-
// and theme that buildPrimarySpec merges into the primary spec, so their
|
|
879
|
-
// marks landed in a different chart area than the rendered axes. A scatter
|
|
880
|
-
// with a title + top legend drew every bubble up-and-left of where the
|
|
881
|
-
// axis said it should be.
|
|
882
|
-
const spec: LayerSpec = {
|
|
883
|
-
chrome: { title: 'Scatter with diagonal', subtitle: 'primary vs leaf areas' },
|
|
884
|
-
legend: { position: 'top' },
|
|
885
|
-
layer: [
|
|
886
|
-
{
|
|
887
|
-
mark: { type: 'point', filled: true },
|
|
888
|
-
data: [
|
|
889
|
-
{ x: 10, y: 10, group: 'a' },
|
|
890
|
-
{ x: 50, y: 50, group: 'b' },
|
|
891
|
-
{ x: 90, y: 90, group: 'a' },
|
|
892
|
-
],
|
|
893
|
-
encoding: {
|
|
894
|
-
x: {
|
|
895
|
-
field: 'x',
|
|
896
|
-
type: 'quantitative' as const,
|
|
897
|
-
scale: { domain: [0, 100], nice: false },
|
|
898
|
-
},
|
|
899
|
-
y: {
|
|
900
|
-
field: 'y',
|
|
901
|
-
type: 'quantitative' as const,
|
|
902
|
-
scale: { domain: [0, 100], nice: false },
|
|
903
|
-
},
|
|
904
|
-
color: { field: 'group', type: 'nominal' as const },
|
|
905
|
-
},
|
|
906
|
-
},
|
|
907
|
-
{
|
|
908
|
-
mark: { type: 'rule', stroke: '#888', strokeWidth: 1 },
|
|
909
|
-
data: [{ x: 0, y: 0, x2: 100, y2: 100 }],
|
|
910
|
-
encoding: {
|
|
911
|
-
x: {
|
|
912
|
-
field: 'x',
|
|
913
|
-
type: 'quantitative' as const,
|
|
914
|
-
scale: { domain: [0, 100], nice: false },
|
|
915
|
-
},
|
|
916
|
-
y: {
|
|
917
|
-
field: 'y',
|
|
918
|
-
type: 'quantitative' as const,
|
|
919
|
-
scale: { domain: [0, 100], nice: false },
|
|
920
|
-
},
|
|
921
|
-
x2: { field: 'x2' },
|
|
922
|
-
y2: { field: 'y2' },
|
|
923
|
-
},
|
|
924
|
-
},
|
|
925
|
-
],
|
|
926
|
-
};
|
|
927
|
-
|
|
928
|
-
const layout = compileLayer(spec, compileOpts);
|
|
929
|
-
|
|
930
|
-
// Axis-implied pixel position for a data value, derived from rendered ticks.
|
|
931
|
-
const xTicks = layout.axes.x?.ticks ?? [];
|
|
932
|
-
const yTicks = layout.axes.y?.ticks ?? [];
|
|
933
|
-
expect(xTicks.length).toBeGreaterThanOrEqual(2);
|
|
934
|
-
expect(yTicks.length).toBeGreaterThanOrEqual(2);
|
|
935
|
-
const xFirst = xTicks[0];
|
|
936
|
-
const xLast = xTicks[xTicks.length - 1];
|
|
937
|
-
const yFirst = yTicks[0];
|
|
938
|
-
const yLast = yTicks[yTicks.length - 1];
|
|
939
|
-
const axisX = (v: number) =>
|
|
940
|
-
xFirst.position +
|
|
941
|
-
((v - (xFirst.value as number)) / ((xLast.value as number) - (xFirst.value as number))) *
|
|
942
|
-
(xLast.position - xFirst.position);
|
|
943
|
-
const axisY = (v: number) =>
|
|
944
|
-
yFirst.position +
|
|
945
|
-
((v - (yFirst.value as number)) / ((yLast.value as number) - (yFirst.value as number))) *
|
|
946
|
-
(yLast.position - yFirst.position);
|
|
947
|
-
|
|
948
|
-
const points = layout.marks.filter((m) => m.type === 'point');
|
|
949
|
-
expect(points.length).toBe(3);
|
|
950
|
-
const expected = [10, 50, 90];
|
|
951
|
-
points.forEach((mark, i) => {
|
|
952
|
-
const pm = mark as unknown as { cx: number; cy: number };
|
|
953
|
-
expect(Math.abs(pm.cx - axisX(expected[i]))).toBeLessThan(1.5);
|
|
954
|
-
expect(Math.abs(pm.cy - axisY(expected[i]))).toBeLessThan(1.5);
|
|
955
|
-
});
|
|
956
|
-
|
|
957
|
-
// The diagonal rule from the second leaf must span the same coordinate space.
|
|
958
|
-
const rule = layout.marks.find((m) => m.type === 'rule') as unknown as
|
|
959
|
-
| { x1: number; y1: number; x2: number; y2: number }
|
|
960
|
-
| undefined;
|
|
961
|
-
expect(rule).toBeDefined();
|
|
962
|
-
if (rule) {
|
|
963
|
-
expect(Math.abs(rule.x1 - axisX(0))).toBeLessThan(1.5);
|
|
964
|
-
expect(Math.abs(rule.x2 - axisX(100))).toBeLessThan(1.5);
|
|
965
|
-
expect(Math.abs(rule.y1 - axisY(0))).toBeLessThan(1.5);
|
|
966
|
-
expect(Math.abs(rule.y2 - axisY(100))).toBeLessThan(1.5);
|
|
967
|
-
}
|
|
968
|
-
});
|
|
969
|
-
});
|