Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply bandPosition to offset channel for grouped bar (so band size works with grouped bars) #9178

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/compiled/bar_grouped_discrete_bandsize.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
142 changes: 142 additions & 0 deletions examples/compiled/bar_grouped_discrete_bandsize.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"background": "white",
"padding": 5,
"height": 200,
"style": "cell",
"data": [
{
"name": "source_0",
"values": [
{"category": "A", "group": "x", "value": 0.1},
{"category": "A", "group": "y", "value": 0.6},
{"category": "A", "group": "z", "value": 0.9},
{"category": "B", "group": "x", "value": 0.7},
{"category": "B", "group": "y", "value": 0.2},
{"category": "B", "group": "z", "value": 1.1},
{"category": "C", "group": "x", "value": 0.6},
{"category": "C", "group": "y", "value": 0.1},
{"category": "C", "group": "z", "value": 0.2}
]
},
{
"name": "data_0",
"source": "source_0",
"transform": [
{
"type": "stack",
"groupby": ["category", "group"],
"field": "value",
"sort": {"field": [], "order": []},
"as": ["value_start", "value_end"],
"offset": "zero"
},
{
"type": "filter",
"expr": "isValid(datum[\"value\"]) && isFinite(+datum[\"value\"])"
}
]
}
],
"signals": [
{
"name": "x_step",
"update": "20 * bandspace(domain('xOffset').length, 0, 0) / (1-0.2)"
},
{
"name": "width",
"update": "bandspace(domain('x').length, 0.2, 0.2) * x_step"
}
],
"marks": [
{
"name": "marks",
"type": "rect",
"style": ["bar"],
"from": {"data": "data_0"},
"encode": {
"update": {
"fill": {"scale": "color", "field": "group"},
"ariaRoleDescription": {"value": "bar"},
"description": {
"signal": "\"category: \" + (isValid(datum[\"category\"]) ? datum[\"category\"] : \"\"+datum[\"category\"]) + \"; value: \" + (format(datum[\"value\"], \"\")) + \"; group: \" + (isValid(datum[\"group\"]) ? datum[\"group\"] : \"\"+datum[\"group\"])"
},
"x": {
"scale": "x",
"field": "category",
"offset": {"scale": "xOffset", "field": "group", "band": 0.25}
},
"width": {"signal": "max(0.25, 0.5 * bandwidth('xOffset'))"},
"y": {"scale": "y", "field": "value_end"},
"y2": {"scale": "y", "field": "value_start"}
}
}
}
],
"scales": [
{
"name": "x",
"type": "band",
"domain": {"data": "data_0", "field": "category", "sort": true},
"range": {"step": {"signal": "x_step"}},
"paddingInner": 0.2,
"paddingOuter": 0.2
},
{
"name": "y",
"type": "linear",
"domain": {"data": "data_0", "fields": ["value_start", "value_end"]},
"range": [{"signal": "height"}, 0],
"nice": true,
"zero": true
},
{
"name": "xOffset",
"type": "band",
"domain": {"data": "data_0", "field": "group", "sort": true},
"range": {"step": 20}
},
{
"name": "color",
"type": "ordinal",
"domain": {"data": "data_0", "field": "group", "sort": true},
"range": "category"
}
],
"axes": [
{
"scale": "y",
"orient": "left",
"gridScale": "x",
"grid": true,
"tickCount": {"signal": "ceil(height/40)"},
"domain": false,
"labels": false,
"aria": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "x",
"orient": "bottom",
"grid": false,
"title": "category",
"labelAlign": "right",
"labelAngle": 270,
"labelBaseline": "middle",
"zindex": 0
},
{
"scale": "y",
"orient": "left",
"grid": false,
"title": "value",
"labelOverlap": true,
"tickCount": {"signal": "ceil(height/40)"},
"zindex": 0
}
],
"legends": [{"fill": "color", "symbolType": "square", "title": "group"}]
}
26 changes: 26 additions & 0 deletions examples/specs/bar_grouped_discrete_bandsize.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"category":"A", "group": "x", "value":0.1},
{"category":"A", "group": "y", "value":0.6},
{"category":"A", "group": "z", "value":0.9},
{"category":"B", "group": "x", "value":0.7},
{"category":"B", "group": "y", "value":0.2},
{"category":"B", "group": "z", "value":1.1},
{"category":"C", "group": "x", "value":0.6},
{"category":"C", "group": "y", "value":0.1},
{"category":"C", "group": "z", "value":0.2}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "category"},
"y": {"field": "value", "type": "quantitative"},
"xOffset": {"field": "group"},
"color": {"field": "group"}
},
"config": {
"bar": {"discreteBandSize": {"band": 0.5}}
}
}
2 changes: 1 addition & 1 deletion src/compile/mark/encode/offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function positionOffset({
markDef: MarkDef<Mark, SignalRef>;
encoding?: Encoding<string>;
model?: UnitModel;
bandPosition?: number;
bandPosition?: number | SignalRef;
}): Offset {
const channel = `${baseChannel}Offset` as
| 'xOffset'
Expand Down
21 changes: 11 additions & 10 deletions src/compile/mark/encode/position-rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,16 @@ function positionAndSize(

const vgChannel = vgAlignedPositionChannel(channel, markDef, config, defaultBandAlign);
const center = vgChannel === 'xc' || vgChannel === 'yc';
const {offset, offsetType} = positionOffset({channel, markDef, encoding, model, bandPosition: center ? 0.5 : 0});

const bandPosition = center
? 0.5
: isSignalRef(bandSize)
? {signal: `(1-${bandSize})/2`}
: isRelativeBandSize(bandSize)
? (1 - bandSize.band) / 2
: 0;

const {offset, offsetType} = positionOffset({channel, markDef, encoding, model, bandPosition});

const posRef = ref.midPointRefWithPositionInvalidTest({
channel,
Expand All @@ -215,15 +224,7 @@ function positionAndSize(
stack,
offset,
defaultRef: pointPositionDefaultRef({model, defaultPos: 'mid', channel, scaleName, scale}),
bandPosition: center
? offsetType === 'encoding'
? 0
: 0.5
: isSignalRef(bandSize)
? {signal: `(1-${bandSize})/2`}
: isRelativeBandSize(bandSize)
? (1 - bandSize.band) / 2
: 0
bandPosition: offsetType === 'encoding' ? 0 : bandPosition
Copy link
Contributor

@richardliu-db richardliu-db Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested different combinations of center and offsetType, and found that this is not consistent with the old version.

The bandPosition used by defaultRef is signal-relative value in the old implementation, but 0 in the new implementation, when center is false and offsetType check is true.

This nested ternary expression is already very complex. Consider making it a function and testing it in unit test.

});

if (vgSizeChannel) {
Expand Down
39 changes: 39 additions & 0 deletions test/compile/mark/bar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ describe('Mark: Bar', () => {
expect(props.height).toBeUndefined();
});

it('draws vertical grouped bar, with relative band size from config correctly applied for x/width', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: 'bar',
encoding: {
x: {field: 'Origin', type: 'nominal'},
xOffset: {field: 'SubOrigin', type: 'nominal'},
y: {type: 'quantitative', field: 'Acceleration', aggregate: 'mean'}
},
config: {
bar: {discreteBandSize: {band: 0.5}}
}
});
const props = bar.encodeEntry(model);
expect(props.x).toEqual({scale: 'x', field: 'Origin', offset: {scale: 'xOffset', field: 'SubOrigin', band: 0.25}});
expect(props.width).toEqual({signal: `max(0.25, 0.5 * bandwidth('xOffset'))`});
expect(props.y).toEqual({scale: 'y', field: 'mean_Acceleration'});
expect(props.y2).toEqual({scale: 'y', value: 0});
expect(props.height).toBeUndefined();
});

it('draws vertical grouped bar, with relative band size correctly applied for x/width', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
data: {url: 'data/cars.json'},
mark: {type: 'bar', width: {band: 0.5}},
encoding: {
x: {field: 'Origin', type: 'nominal'},
xOffset: {field: 'SubOrigin', type: 'nominal'},
y: {type: 'quantitative', field: 'Acceleration', aggregate: 'mean'}
}
});
const props = bar.encodeEntry(model);
expect(props.x).toEqual({scale: 'x', field: 'Origin', offset: {scale: 'xOffset', field: 'SubOrigin', band: 0.25}});
expect(props.width).toEqual({signal: `max(0.25, 0.5 * bandwidth('xOffset'))`});
expect(props.y).toEqual({scale: 'y', field: 'mean_Acceleration'});
expect(props.y2).toEqual({scale: 'y', value: 0});
expect(props.height).toBeUndefined();
});

it('should draw horizontal bar, with y from zero to field value and bar with quantitative x, x2, and y', () => {
const x: PositionFieldDef<string> = {field: 'q_start', type: 'quantitative'};
const x2: SecondaryFieldDef<string> = {field: 'q_end'};
Expand Down