Skip to content

Commit

Permalink
fix: Make bars with one axis thicker (#8894)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions Bot <vega-actions-bot@users.noreply.github.com>
Co-authored-by: Kanit Wongsuphasawat <kanitw@gmail.com>
  • Loading branch information
3 people committed May 11, 2023
1 parent 726c97c commit 309af9a
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/compiled/bar_1d.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"x": {"scale": "x", "field": "sum_people"},
"x2": {"scale": "x", "value": 0},
"yc": {"signal": "height", "mult": 0.5},
"height": {"value": 18}
"height": {"signal": "0.9 * height"}
}
}
}
Expand Down
Binary file added examples/compiled/bar_1d_default_size.png
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_1d_default_size.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions examples/compiled/bar_1d_default_size.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"background": "white",
"padding": 5,
"width": 200,
"height": 200,
"style": "cell",
"data": [
{
"name": "source_0",
"url": "data/population.json",
"format": {"type": "json"},
"transform": [
{"type": "filter", "expr": "datum.year == 2000"},
{
"type": "aggregate",
"groupby": [],
"ops": ["sum"],
"fields": ["people"],
"as": ["sum_people"]
},
{
"type": "filter",
"expr": "isValid(datum[\"sum_people\"]) && isFinite(+datum[\"sum_people\"])"
}
]
}
],
"marks": [
{
"name": "marks",
"type": "rect",
"style": ["bar"],
"from": {"data": "source_0"},
"encode": {
"update": {
"fill": {"value": "#4c78a8"},
"ariaRoleDescription": {"value": "bar"},
"description": {
"signal": "\"population: \" + (format(datum[\"sum_people\"], \"\"))"
},
"x": {"scale": "x", "field": "sum_people"},
"x2": {"scale": "x", "value": 0},
"yc": {"signal": "height", "mult": 0.5},
"height": {"signal": "0.9 * height"}
}
}
}
],
"scales": [
{
"name": "x",
"type": "linear",
"domain": {"data": "source_0", "field": "sum_people"},
"range": [0, {"signal": "width"}],
"nice": true,
"zero": true
}
],
"axes": [
{
"scale": "x",
"orient": "bottom",
"grid": true,
"tickCount": {"signal": "ceil(width/40)"},
"domain": false,
"labels": false,
"aria": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "x",
"orient": "bottom",
"grid": false,
"title": "population",
"labelFlush": true,
"labelOverlap": true,
"tickCount": {"signal": "ceil(width/40)"},
"zindex": 0
}
]
}
Binary file modified examples/compiled/bar_1d_step_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/compiled/bar_1d_step_config.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/compiled/bar_1d_step_config.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"x": {"scale": "x", "field": "sum_people"},
"x2": {"scale": "x", "value": 0},
"yc": {"signal": "height", "mult": 0.5},
"height": {"value": 13}
"height": {"signal": "0.9 * height"}
}
}
}
Expand Down
Binary file modified examples/compiled/facet_bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/compiled/facet_bullet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions examples/compiled/facet_bullet.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@
"x": {"scale": "child_x", "field": "ranges\\.2\\._end"},
"x2": {"scale": "child_x", "field": "ranges\\.2\\._start"},
"yc": {"signal": "child_height", "mult": 0.5},
"height": {"value": 18}
"height": {"signal": "1 * height"}
}
}
},
Expand All @@ -363,7 +363,7 @@
"x": {"scale": "child_x", "field": "ranges\\.1\\._end"},
"x2": {"scale": "child_x", "field": "ranges\\.1\\._start"},
"yc": {"signal": "child_height", "mult": 0.5},
"height": {"value": 18}
"height": {"signal": "1 * height"}
}
}
},
Expand All @@ -382,7 +382,7 @@
"x": {"scale": "child_x", "field": "ranges\\.0\\._end"},
"x2": {"scale": "child_x", "field": "ranges\\.0\\._start"},
"yc": {"signal": "child_height", "mult": 0.5},
"height": {"value": 18}
"height": {"signal": "1 * height"}
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/stacked_bar_1d.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"x": {"scale": "x", "field": "sum_Acceleration_end"},
"x2": {"scale": "x", "field": "sum_Acceleration_start"},
"yc": {"signal": "height", "mult": 0.5},
"height": {"value": 18}
"height": {"signal": "0.9 * height"}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions examples/specs/bar_1d_default_size.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": { "url": "data/population.json"},
"transform": [{
"filter": "datum.year == 2000"
}],
"mark": "bar",
"encoding": {
"x": {
"aggregate": "sum", "field": "people",
"title": "population"
}
},
"height": 200
}
2 changes: 1 addition & 1 deletion examples/specs/facet_bullet.vl.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
}]
},
"resolve": {"scale": {"x": "independent"}},
"config": {"tick": {"thickness": 2}}
"config": {"tick": {"thickness": 2}, "scale": {"barBandPaddingInner": 0}}
}
2 changes: 1 addition & 1 deletion examples/specs/normalized/facet_bullet_normalized.vl.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@
]
},
"resolve": {"scale": {"x": "independent"}},
"config": {"tick": {"thickness": 2}}
"config": {"tick": {"thickness": 2}, "scale": {"barBandPaddingInner": 0}}
}
28 changes: 26 additions & 2 deletions src/compile/mark/encode/position-rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {pointPositionDefaultRef} from './position-point';
import {rangePosition} from './position-range';
import * as ref from './valueref';
import {getOffsetScaleChannel} from '../../../channel';
import {getFirstDefined} from '../../../util';
import {Mark} from '../../../mark';
import {isExprRef} from '../../../expr';

export function rectPosition(model: UnitModel, channel: 'x' | 'y' | 'theta' | 'radius'): VgEncodeEntry {
const {config, encoding, markDef} = model;
Expand Down Expand Up @@ -75,7 +78,9 @@ function defaultSizeRef(
scaleName: string,
scale: ScaleComponent,
config: Config,
bandSize: BandSize
bandSize: BandSize,
hasFieldDef: boolean,
mark: Mark
): VgValueRef {
if (isRelativeBandSize(bandSize)) {
if (scale) {
Expand Down Expand Up @@ -110,6 +115,17 @@ function defaultSizeRef(
return {value: scaleRange.step - 2};
}
}
if (!hasFieldDef) {
const {bandPaddingInner, barBandPaddingInner, rectBandPaddingInner} = config.scale;
const padding = getFirstDefined(bandPaddingInner, mark === 'bar' ? barBandPaddingInner : rectBandPaddingInner); // this part is like paddingInner in scale.ts
if (isSignalRef(padding)) {
return {signal: `(1 - (${padding.signal})) * ${sizeChannel}`};
} else if (isNumber(padding)) {
return {signal: `${1 - padding} * ${sizeChannel}`};
} else if (isExprRef(padding)) {
return {signal: `(1 - (${padding.expr})) * ${sizeChannel}`};
}
}
const defaultStep = getViewConfigDiscreteStep(config.view, sizeChannel);
return {value: defaultStep - 2};
}
Expand Down Expand Up @@ -155,7 +171,15 @@ function positionAndSize(
const bandSize = getBandSize({channel, fieldDef, markDef, config, scaleType: scale?.get('type'), useVlSizeChannel});

sizeMixins = sizeMixins || {
[vgSizeChannel]: defaultSizeRef(vgSizeChannel, offsetScaleName || scaleName, offsetScale || scale, config, bandSize)
[vgSizeChannel]: defaultSizeRef(
vgSizeChannel,
offsetScaleName || scaleName,
offsetScale || scale,
config,
bandSize,
!!fieldDef,
markDef.type
)
};

/*
Expand Down
Loading

0 comments on commit 309af9a

Please sign in to comment.