Skip to content

Commit 432f354

Browse files
kanitwGitHub Actions Bot
and
GitHub Actions Bot
authored
fix(#9167): make 1D bars respect orient for stack calculation (#9168)
* docs(example): bar_1d_dimension only * chore: update examples [CI] * fix: make 1D bar respect orientation for stack calculation (#9167) * chore: update examples [CI] --------- Co-authored-by: GitHub Actions Bot <[email protected]>
1 parent 38ca377 commit 432f354

6 files changed

+137
-2
lines changed
2.24 KB
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"$schema": "https://vega.github.io/schema/vega/v5.json",
3+
"description": "A simple bar chart with embedded data.",
4+
"background": "white",
5+
"padding": 5,
6+
"width": 20,
7+
"height": 200,
8+
"style": "cell",
9+
"data": [
10+
{
11+
"name": "source_0",
12+
"values": [
13+
{"b": 0},
14+
{"b": 10},
15+
{"b": 10},
16+
{"b": 10},
17+
{"b": 10},
18+
{"b": 20}
19+
]
20+
},
21+
{
22+
"name": "data_0",
23+
"source": "source_0",
24+
"transform": [
25+
{
26+
"type": "filter",
27+
"expr": "isValid(datum[\"b\"]) && isFinite(+datum[\"b\"])"
28+
}
29+
]
30+
}
31+
],
32+
"marks": [
33+
{
34+
"name": "marks",
35+
"type": "rect",
36+
"style": ["bar"],
37+
"from": {"data": "data_0"},
38+
"encode": {
39+
"update": {
40+
"fill": {"value": "#4c78a8"},
41+
"ariaRoleDescription": {"value": "bar"},
42+
"description": {"signal": "\"b: \" + (format(datum[\"b\"], \"\"))"},
43+
"x": {"field": {"group": "width"}},
44+
"x2": {"value": 0},
45+
"yc": {"scale": "y", "field": "b"},
46+
"height": {"value": 5}
47+
}
48+
}
49+
}
50+
],
51+
"scales": [
52+
{
53+
"name": "y",
54+
"type": "linear",
55+
"domain": {"data": "data_0", "field": "b"},
56+
"range": [{"signal": "height"}, 0],
57+
"nice": true,
58+
"zero": false,
59+
"padding": 5
60+
}
61+
],
62+
"axes": [
63+
{
64+
"scale": "y",
65+
"orient": "left",
66+
"grid": true,
67+
"tickCount": {"signal": "ceil(height/40)"},
68+
"domain": false,
69+
"labels": false,
70+
"aria": false,
71+
"maxExtent": 0,
72+
"minExtent": 0,
73+
"ticks": false,
74+
"zindex": 0
75+
},
76+
{
77+
"scale": "y",
78+
"orient": "left",
79+
"grid": false,
80+
"title": "b",
81+
"labelOverlap": true,
82+
"tickCount": {"signal": "ceil(height/40)"},
83+
"zindex": 0
84+
}
85+
]
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
3+
"description": "A simple bar chart with embedded data.",
4+
"data": {
5+
"values": [
6+
{"b": 0},
7+
{"b": 10},
8+
{"b": 10},
9+
{"b": 10},
10+
{"b": 10},
11+
{"b": 20}
12+
]
13+
},
14+
"mark": {"type": "bar", "orient": "horizontal"},
15+
"encoding": {
16+
"y": {"field": "b", "type": "quantitative"}
17+
}
18+
}

src/stack.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function potentialStackedChannel(
8787
): 'x' | 'y' | 'theta' | 'radius' | undefined {
8888
const y = x === 'x' ? 'y' : 'radius';
8989

90-
const isCartesian = x === 'x';
90+
const isCartesianBarOrArea = x === 'x' && ['bar', 'area'].includes(mark);
9191

9292
const xDef = encoding[x];
9393
const yDef = encoding[y];
@@ -106,7 +106,7 @@ function potentialStackedChannel(
106106
return xAggregate ? x : y;
107107
}
108108

109-
if (isCartesian && ['bar', 'area'].includes(mark)) {
109+
if (isCartesianBarOrArea) {
110110
if (orient === 'vertical') {
111111
return y;
112112
} else if (orient === 'horizontal') {
@@ -119,8 +119,14 @@ function potentialStackedChannel(
119119
return y;
120120
}
121121
} else if (isUnbinnedQuantitative(xDef)) {
122+
if (isCartesianBarOrArea && orient === 'vertical') {
123+
return undefined;
124+
}
122125
return x;
123126
} else if (isUnbinnedQuantitative(yDef)) {
127+
if (isCartesianBarOrArea && orient === 'horizontal') {
128+
return undefined;
129+
}
124130
return y;
125131
}
126132
return undefined;

test/stack.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ describe('stack', () => {
5656
}
5757
});
5858

59+
it("doesn't stacked the dimension field on a 1D vertical bar with dimension only", () => {
60+
const spec: TopLevel<NormalizedUnitSpec> = {
61+
data: {url: 'data/barley.json'},
62+
mark: {type: 'bar', orient: 'vertical'},
63+
encoding: {
64+
x: {field: 'yield', type: 'quantitative'}
65+
}
66+
};
67+
const stackProps = stack(spec.mark, spec.encoding);
68+
expect(stackProps).toBeNull();
69+
});
70+
71+
it("doesn't stacked the dimension field on a 1D horizontal bar with dimension only", () => {
72+
const spec: TopLevel<NormalizedUnitSpec> = {
73+
data: {url: 'data/barley.json'},
74+
mark: {type: 'bar', orient: 'horizontal'},
75+
encoding: {
76+
y: {field: 'yield', type: 'quantitative'}
77+
}
78+
};
79+
const stackProps = stack(spec.mark, spec.encoding);
80+
expect(stackProps).toBeNull();
81+
});
82+
5983
it('should be disabled when stack is false', () => {
6084
for (const mark of STACKABLE_NON_POLAR_MARKS) {
6185
const spec: TopLevel<NormalizedUnitSpec> = {

0 commit comments

Comments
 (0)