Skip to content

Commit

Permalink
fix: make 1D bar respect orientation for stack calculation (#9167)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanitw committed Nov 8, 2023
1 parent 558d6e2 commit 0007da5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function potentialStackedChannel(
): 'x' | 'y' | 'theta' | 'radius' | undefined {
const y = x === 'x' ? 'y' : 'radius';

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

const xDef = encoding[x];
const yDef = encoding[y];
Expand All @@ -106,7 +106,7 @@ function potentialStackedChannel(
return xAggregate ? x : y;
}

if (isCartesian && ['bar', 'area'].includes(mark)) {
if (isCartesianBarOrArea) {
if (orient === 'vertical') {
return y;
} else if (orient === 'horizontal') {
Expand All @@ -119,8 +119,14 @@ function potentialStackedChannel(
return y;
}
} else if (isUnbinnedQuantitative(xDef)) {
if (isCartesianBarOrArea && orient === 'vertical') {
return undefined;
}
return x;
} else if (isUnbinnedQuantitative(yDef)) {
if (isCartesianBarOrArea && orient === 'horizontal') {
return undefined;
}
return y;
}
return undefined;
Expand Down
24 changes: 24 additions & 0 deletions test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ describe('stack', () => {
}
});

it("doesn't stacked the dimension field on a 1D vertical bar with dimension only", () => {
const spec: TopLevel<NormalizedUnitSpec> = {
data: {url: 'data/barley.json'},
mark: {type: 'bar', orient: 'vertical'},
encoding: {
x: {field: 'yield', type: 'quantitative'}
}
};
const stackProps = stack(spec.mark, spec.encoding);
expect(stackProps).toBeNull();
});

it("doesn't stacked the dimension field on a 1D horizontal bar with dimension only", () => {
const spec: TopLevel<NormalizedUnitSpec> = {
data: {url: 'data/barley.json'},
mark: {type: 'bar', orient: 'horizontal'},
encoding: {
y: {field: 'yield', type: 'quantitative'}
}
};
const stackProps = stack(spec.mark, spec.encoding);
expect(stackProps).toBeNull();
});

it('should be disabled when stack is false', () => {
for (const mark of STACKABLE_NON_POLAR_MARKS) {
const spec: TopLevel<NormalizedUnitSpec> = {
Expand Down

0 comments on commit 0007da5

Please sign in to comment.