Skip to content

Commit 25a54a2

Browse files
add additional automargin options
1 parent b194d8c commit 25a54a2

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-1
lines changed

src/plots/cartesian/axes.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,8 @@ axes.drawOne = function(gd, ax, opts) {
26222622
rangeSliderPush = Registry.getComponentMethod('rangeslider', 'autoMarginOpts')(gd, ax);
26232623
}
26242624

2625+
keepSelectedAutoMargin(ax.automargin, push);
2626+
26252627
Plots.autoMargin(gd, axAutoMarginID(ax), push);
26262628
Plots.autoMargin(gd, axMirrorAutoMarginID(ax), mirrorPush);
26272629
Plots.autoMargin(gd, rangeSliderAutoMarginID(ax), rangeSliderPush);
@@ -2636,6 +2638,35 @@ axes.drawOne = function(gd, ax, opts) {
26362638
return Lib.syncOrAsync(seq);
26372639
};
26382640

2641+
function keepSelectedAutoMargin(automargin, push) {
2642+
if(typeof automargin === 'boolean') return push;
2643+
2644+
var keepMargin = [];
2645+
var mapping = {
2646+
width: ['x', 'r', 'l'],
2647+
height: ['y', 't', 'b'],
2648+
right: ['r'],
2649+
left: ['l'],
2650+
top: ['t'],
2651+
bottom: ['b']
2652+
};
2653+
2654+
Object.keys(mapping).forEach(function(key) {
2655+
if(automargin.includes(key)) {
2656+
mapping[key].forEach(function(item) {
2657+
keepMargin.push(item);
2658+
});
2659+
}
2660+
});
2661+
2662+
Object.keys(push).forEach(function(key) {
2663+
if(key.length !== 2 && !keepMargin.includes(key)) {
2664+
push[key] = 0;
2665+
}
2666+
});
2667+
return push;
2668+
}
2669+
26392670
function getBoundaryVals(ax, vals) {
26402671
var out = [];
26412672
var i;

src/plots/cartesian/layout_attributes.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,14 @@ module.exports = {
625625
description: 'Determines whether or not the tick labels are drawn.'
626626
},
627627
automargin: {
628-
valType: 'boolean',
628+
valType: 'enumerated',
629+
values: [
630+
true, false, 'height', 'width',
631+
'top', 'bottom', 'left', 'right',
632+
'top left', 'top width', 'top right',
633+
'left height', 'right height',
634+
'bottom left', 'bottom width', 'bottom right',
635+
],
629636
dflt: false,
630637
editType: 'ticks',
631638
description: [

test/jasmine/tests/axes_test.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4225,6 +4225,132 @@ describe('Test axes', function() {
42254225
.then(done, done.fail);
42264226
});
42274227

4228+
it('should handle partial automargin', function(done) {
4229+
var initialSize;
4230+
4231+
function assertSize(msg, actual, exp) {
4232+
for(var k in exp) {
4233+
var parts = exp[k].split('|');
4234+
var op = parts[0];
4235+
4236+
var method = {
4237+
'=': 'toBe',
4238+
grew: 'toBeGreaterThan',
4239+
}[op];
4240+
4241+
var val = initialSize[k];
4242+
var msgk = msg + ' ' + k + (parts[1] ? ' |' + parts[1] : '');
4243+
var args = op === '~=' ? [val, 1.1, msgk] : [val, msgk, ''];
4244+
4245+
expect(actual[k])[method](args[0], args[1], args[2]);
4246+
}
4247+
}
4248+
4249+
function check(msg, relayoutObj, exp) {
4250+
return function() {
4251+
return Plotly.relayout(gd, relayoutObj).then(function() {
4252+
var gs = Lib.extendDeep({}, gd._fullLayout._size);
4253+
assertSize(msg, gs, exp);
4254+
});
4255+
};
4256+
}
4257+
4258+
Plotly.newPlot(gd, [{
4259+
x: [
4260+
'short label 1', 'loooooong label 1',
4261+
'short label 2', 'loooooong label 2',
4262+
'short label 3', 'loooooong label 3',
4263+
'short label 4', 'loooooongloooooongloooooong label 4',
4264+
'short label 5', 'loooooong label 5'
4265+
],
4266+
y: [
4267+
'short label 1', 'loooooong label 1',
4268+
'short label 2', 'loooooong label 2',
4269+
'short label 3', 'loooooong label 3',
4270+
'short label 4', 'loooooong label 4',
4271+
'short label 5', 'loooooong label 5'
4272+
]
4273+
}], {
4274+
margin: {l: 0, r: 0, b: 0, t: 0},
4275+
width: 600, height: 600
4276+
})
4277+
.then(function() {
4278+
expect(gd._fullLayout.xaxis._tickAngles.xtick).toBe(30);
4279+
4280+
var gs = gd._fullLayout._size;
4281+
initialSize = Lib.extendDeep({}, gs);
4282+
})
4283+
.then(check('automargin y', {'yaxis.automargin': true, 'yaxis.tickangle': 30, 'yaxis.ticklen': 30}, {
4284+
t: 'grew', l: 'grew',
4285+
b: '=', r: '='
4286+
}))
4287+
.then(check('automargin not left', {'yaxis.automargin': 'right height'}, {
4288+
t: 'grew', l: '=',
4289+
b: '=', r: '='
4290+
}))
4291+
.then(check('automargin keep left height', {'yaxis.automargin': 'left height'}, {
4292+
t: 'grew', l: 'grew',
4293+
b: '=', r: '='
4294+
}))
4295+
.then(check('automargin keep bottom right', {'yaxis.automargin': 'bottom right'}, {
4296+
t: '=', l: '=',
4297+
b: '=', r: '='
4298+
}))
4299+
.then(check('automargin keep height', {'yaxis.automargin': 'height'}, {
4300+
t: 'grew', l: '=',
4301+
b: '=', r: '='
4302+
}))
4303+
.then(check('automargin keep top', {'yaxis.automargin': 'top'}, {
4304+
t: 'grew', l: '=',
4305+
b: '=', r: '='
4306+
}))
4307+
.then(check('automargin not top', {'yaxis.automargin': 'bottom width'}, {
4308+
t: '=', l: 'grew',
4309+
b: '=', r: '='
4310+
}))
4311+
.then(check('automargin keep left', {'yaxis.automargin': 'left'}, {
4312+
t: '=', l: 'grew',
4313+
b: '=', r: '='
4314+
}))
4315+
.then(check('automargin keep width', {'yaxis.automargin': 'width'}, {
4316+
t: '=', l: 'grew',
4317+
b: '=', r: '='
4318+
}))
4319+
.then(check('automargin x', {'xaxis.automargin': true, 'yaxis.automargin': false}, {
4320+
t: '=', l: '=',
4321+
b: 'grew', r: 'grew'
4322+
}))
4323+
.then(check('automargin not bottom', {'xaxis.automargin': 'top width'}, {
4324+
t: '=', l: '=',
4325+
b: '=', r: 'grew'
4326+
}))
4327+
.then(check('automargin keep right', {'xaxis.automargin': 'right'}, {
4328+
t: '=', l: '=',
4329+
b: '=', r: 'grew'
4330+
}))
4331+
.then(check('automargin keep bottom', {'xaxis.automargin': 'bottom'}, {
4332+
t: '=', l: '=',
4333+
b: 'grew', r: '='
4334+
}))
4335+
.then(check('automargin keep top right', {'xaxis.automargin': 'top right'}, {
4336+
t: '=', l: '=',
4337+
b: '=', r: 'grew'
4338+
}))
4339+
.then(check('automargin keep top left', {'xaxis.automargin': 'top left'}, {
4340+
t: '=', l: '=',
4341+
b: '=', r: '='
4342+
}))
4343+
.then(check('automargin keep bottom left', {'xaxis.automargin': 'bottom left'}, {
4344+
t: '=', l: '=',
4345+
b: 'grew', r: '='
4346+
}))
4347+
.then(check('turn off automargin', {'xaxis.automargin': false, 'yaxis.automargin': false}, {
4348+
t: '=', l: '=',
4349+
b: '=', r: '='
4350+
}))
4351+
.then(done, done.fail);
4352+
});
4353+
42284354
it('should handle cases with free+mirror axes', function(done) {
42294355
Plotly.newPlot(gd, [{
42304356
y: [1, 2, 1]

0 commit comments

Comments
 (0)