Skip to content

Commit

Permalink
fix: make 1D tick mark responsive to width/height (#9411)
Browse files Browse the repository at this point in the history
## PR Description

See fixed example + relevant unit tests

---------

Co-authored-by: GitHub Actions Bot <[email protected]>
  • Loading branch information
kanitw and GitHub Actions Bot authored Aug 28, 2024
1 parent ef70cb8 commit c0c7b6b
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/compiled/bar_tooltip_aggregate.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"xc": {"signal": "width", "mult": 0.5},
"yc": {"scale": "y", "field": "mean_data"},
"width": {"value": 15},
"width": {"signal": "(1 - 0.25) * width"},
"height": {"value": 1}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/bar_tooltip_groupby.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"xc": {"signal": "width", "mult": 0.5},
"yc": {"scale": "y", "field": "mean_data"},
"width": {"value": 15},
"width": {"signal": "(1 - 0.25) * width"},
"height": {"value": 1}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/compiled/boxplot_1D_horizontal_custom_mark.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
},
"xc": {"scale": "x", "field": "lower_whisker_Body Mass (g)"},
"yc": {"signal": "height", "mult": 0.5},
"height": {"value": 15},
"height": {"signal": "(1 - 0.25) * height"},
"width": {"value": 1}
}
}
Expand All @@ -233,7 +233,7 @@
},
"xc": {"scale": "x", "field": "upper_whisker_Body Mass (g)"},
"yc": {"signal": "height", "mult": 0.5},
"height": {"value": 15},
"height": {"signal": "(1 - 0.25) * height"},
"width": {"value": 1}
}
}
Expand Down
Binary file modified examples/compiled/errorbar_2d_vertical_ticks.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/errorbar_2d_vertical_ticks.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/compiled/errorbar_2d_vertical_ticks.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"xc": {"scale": "x", "field": "year_Year"},
"yc": {"scale": "y", "field": "lower_Miles_per_Gallon"},
"width": {"value": 15},
"width": {"signal": "(1 - 0.25) * width"},
"height": {"value": 1}
}
}
Expand All @@ -91,7 +91,7 @@
},
"xc": {"scale": "x", "field": "year_Year"},
"yc": {"scale": "y", "field": "upper_Miles_per_Gallon"},
"width": {"value": 15},
"width": {"signal": "(1 - 0.25) * width"},
"height": {"value": 1}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/facet_bullet.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@
},
"xc": {"scale": "child_x", "field": "markers\\.0"},
"yc": {"signal": "child_height", "mult": 0.5},
"height": {"value": 15},
"height": {"signal": "(1 - 0.25) * child_height"},
"width": {"value": 2}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/tick_dot.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"xc": {"scale": "x", "field": "precipitation"},
"yc": {"signal": "height", "mult": 0.5},
"height": {"value": 15},
"height": {"signal": "(1 - 0.25) * height"},
"width": {"value": 1}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compiled/tick_sort.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"xc": {"scale": "x", "field": "Horsepower"},
"yc": {"signal": "height", "mult": 0.5},
"height": {"value": 15},
"height": {"signal": "(1 - 0.25) * height"},
"width": {"value": 1}
}
}
Expand Down
Binary file added examples/compiled/tick_strip_1D_with_height.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/tick_strip_1D_with_height.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions examples/compiled/tick_strip_1D_with_height.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"$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/cars.json",
"format": {"type": "json"},
"transform": [
{
"type": "filter",
"expr": "isValid(datum[\"Horsepower\"]) && isFinite(+datum[\"Horsepower\"])"
}
]
}
],
"marks": [
{
"name": "marks",
"type": "rect",
"style": ["tick"],
"from": {"data": "source_0"},
"encode": {
"update": {
"opacity": {"value": 0.7},
"fill": {"value": "#4c78a8"},
"ariaRoleDescription": {"value": "tick"},
"description": {
"signal": "\"Horsepower: \" + (format(datum[\"Horsepower\"], \"\"))"
},
"xc": {"scale": "x", "field": "Horsepower"},
"yc": {"signal": "height", "mult": 0.5},
"height": {"signal": "(1 - 0.25) * height"},
"width": {"value": 1}
}
}
}
],
"scales": [
{
"name": "x",
"type": "linear",
"domain": {"data": "source_0", "field": "Horsepower"},
"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": "Horsepower",
"labelFlush": true,
"labelOverlap": true,
"tickCount": {"signal": "ceil(width/40)"},
"zindex": 0
}
]
}
9 changes: 9 additions & 0 deletions examples/specs/tick_strip_1D_with_height.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"url": "data/cars.json"},
"height": 200,
"mark": "tick",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"}
}
}
8 changes: 3 additions & 5 deletions src/compile/mark/tick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {isNumber} from 'vega-util';
import {getViewConfigDiscreteStep} from '../../config';
import {isVgRangeStep, VgValueRef} from '../../vega.schema';
import {exprFromSignalRefOrValue, getMarkPropOrConfig, signalOrValueRef} from '../common';
import {UnitModel} from '../unit';
Expand Down Expand Up @@ -51,26 +50,25 @@ function defaultSize(model: UnitModel): VgValueRef {

// Use offset scale if exists
const scale = model.getScaleComponent(offsetScaleChannel) || model.getScaleComponent(positionChannel);
const scaleName = model.scaleName(offsetScaleChannel) || model.scaleName(positionChannel);

const markPropOrConfig =
getMarkPropOrConfig('size', markDef, config, {vgChannel: vgSizeChannel}) ?? config.tick.bandSize;

if (markPropOrConfig !== undefined) {
return signalOrValueRef(markPropOrConfig);
} else if (scale?.get('type') === 'band') {
const scaleName = model.scaleName(offsetScaleChannel) || model.scaleName(positionChannel);
return {scale: scaleName, band: 1};
}

const scaleRange = scale?.get('range');
const {tickBandPaddingInner} = config.scale;

const step =
scaleRange && isVgRangeStep(scaleRange) ? scaleRange.step : getViewConfigDiscreteStep(config.view, vgSizeChannel);
const step = scaleRange && isVgRangeStep(scaleRange) ? scaleRange.step : model[vgSizeChannel];

if (isNumber(step) && isNumber(tickBandPaddingInner)) {
return {value: step * (1 - tickBandPaddingInner)};
} else {
return {signal: `${exprFromSignalRefOrValue(tickBandPaddingInner)} * ${exprFromSignalRefOrValue(step)}`};
return {signal: `(1 - ${exprFromSignalRefOrValue(tickBandPaddingInner)}) * ${exprFromSignalRefOrValue(step)}`};
}
}
28 changes: 28 additions & 0 deletions test/compile/mark/tick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,32 @@ describe('Mark: Tick', () => {
expect(props.height).toEqual({field: 'Acceleration', scale: SIZE});
});
});

describe('vertical ticks without Y', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: 'tick',
encoding: {
x: {field: 'Horsepower', type: 'quantitative'}
},
data: {url: 'data/cars.json'}
});
const props = tick.encodeEntry(model);
it('sets mark height to (1-tickBandPaddingInner) * plot_height', () => {
expect(props.height).toEqual({signal: '(1 - 0.25) * height'});
});
});

describe('horizontal ticks without X', () => {
const model = parseUnitModelWithScaleAndLayoutSize({
mark: 'tick',
encoding: {
y: {field: 'Horsepower', type: 'quantitative'}
},
data: {url: 'data/cars.json'}
});
const props = tick.encodeEntry(model);
it('sets mark width to (1-tickBandPaddingInner) * plot_width', () => {
expect(props.width).toEqual({signal: '(1 - 0.25) * width'});
});
});
});

0 comments on commit c0c7b6b

Please sign in to comment.