Skip to content

Commit

Permalink
fix: make 1D tick mark responsive to width/height
Browse files Browse the repository at this point in the history
  • Loading branch information
kanitw committed Aug 27, 2024
1 parent 07184dd commit 0216dbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
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 0216dbd

Please sign in to comment.