Skip to content

Commit

Permalink
Merge branch 'kw/9177-grouped-bar-discrete-bandSize' into kw/tick-bar
Browse files Browse the repository at this point in the history
  • Loading branch information
kanitw committed Nov 16, 2023
2 parents 090e41d + 4deb363 commit df8602e
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 11 deletions.
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 @@ -209,7 +209,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`}

Check warning on line 216 in src/compile/mark/encode/position-rect.ts

View check run for this annotation

Codecov / codecov/patch

src/compile/mark/encode/position-rect.ts#L216

Added line #L216 was not covered by tests
: isRelativeBandSize(bandSize)
? (1 - bandSize.band) / 2
: 0;

Check warning on line 219 in src/compile/mark/encode/position-rect.ts

View check run for this annotation

Codecov / codecov/patch

src/compile/mark/encode/position-rect.ts#L219

Added line #L219 was not covered by tests

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

const posRef = ref.midPointRefWithPositionInvalidTest({
channel,
Expand All @@ -221,15 +230,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
});

if (vgSizeChannel) {
Expand Down

0 comments on commit df8602e

Please sign in to comment.