Skip to content

Commit a05bd81

Browse files
first relayout test
1 parent 05b909a commit a05bd81

File tree

2 files changed

+144
-5
lines changed

2 files changed

+144
-5
lines changed

src/plots/cartesian/axes.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,19 +2211,27 @@ axes.doTicks = function(gd, axid, skipTitle) {
22112211
}
22122212

22132213
function doAutoMargins() {
2214-
if(!ax.automargin) { return; }
2215-
var marginPush = ax.titlefont.size +
2216-
(axLetter === 'x' ? ax._boundingBox.height : ax._boundingBox.width);
22172214

2218-
if(!ax._marginPush || ax._marginPush < marginPush) {
2219-
ax._marginPush = marginPush;
2215+
var marginPush = 0;
2216+
if(ax.automargin) {
2217+
marginPush = ax.titlefont.size +
2218+
(axLetter === 'x' ? ax._boundingBox.height : ax._boundingBox.width);
2219+
}
2220+
2221+
if(!fullLayout._replotting ||
2222+
!ax._marginPush || ax._marginPush[ax.side[0]] < marginPush) {
2223+
2224+
ax._marginPush = {r: 0, l: 0, t: 0, b: 0};
2225+
ax._marginPush[ax.side[0]] = marginPush;
2226+
22202227
var pushParams = {
22212228
x: ax.side[0] === 'r' ? ax.domain[1] : ax.domain[0],
22222229
y: ax.side[0] === 't' ? ax.domain[1] : ax.domain[0],
22232230
r: 0, l: 0, t: 0, b: 0};
22242231
pushParams[ax.side[0]] = marginPush;
22252232
Plots.autoMargin(gd, ax._name, pushParams);
22262233
}
2234+
22272235
}
22282236

22292237
var done = Lib.syncOrAsync([

test/jasmine/tests/axes_test.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,137 @@ describe('Test axes', function() {
24472447
});
24482448
});
24492449
});
2450+
2451+
describe('automargin', function() {
2452+
var data = [{
2453+
x: [
2454+
'short label 1', 'loooooong label 1',
2455+
'short label 2', 'loooooong label 2',
2456+
'short label 3', 'loooooong label 3',
2457+
'short label 4', 'loooooongloooooongloooooong label 4',
2458+
'short label 5', 'loooooong label 5'
2459+
],
2460+
y: [
2461+
'short label 1', 'loooooong label 1',
2462+
'short label 2', 'loooooong label 2',
2463+
'short label 3', 'loooooong label 3',
2464+
'short label 4', 'loooooong label 4',
2465+
'short label 5', 'loooooong label 5'
2466+
]
2467+
}],
2468+
gd;
2469+
2470+
beforeEach(function() {
2471+
gd = createGraphDiv();
2472+
});
2473+
2474+
afterEach(destroyGraphDiv);
2475+
2476+
it('should grow and shrink margins', function(done) {
2477+
2478+
Plotly.plot(gd, data)
2479+
.then(function() {
2480+
var size = gd._fullLayout._size;
2481+
// these are the defaults
2482+
expect(size.l).toBe(80);
2483+
expect(size.r).toBe(80);
2484+
expect(size.b).toBe(80);
2485+
expect(size.t).toBe(100);
2486+
expect(gd._fullLayout.xaxis._lastangle).toBe(30);
2487+
})
2488+
.then(function() {
2489+
return Plotly.relayout(gd, {'yaxis.automargin': true});
2490+
})
2491+
.then(function() {
2492+
var size = gd._fullLayout._size;
2493+
expect(size.l).toBe(133); // left side grows
2494+
expect(size.r).toBe(80);
2495+
expect(size.b).toBe(80);
2496+
expect(size.t).toBe(100);
2497+
})
2498+
.then(function() {
2499+
return Plotly.relayout(gd, {'xaxis.automargin': true});
2500+
})
2501+
.then(function() {
2502+
var size = gd._fullLayout._size;
2503+
expect(size.l).toBe(133);
2504+
expect(size.r).toBe(80);
2505+
expect(size.b).toBe(154); // bottom grows
2506+
expect(size.t).toBe(100);
2507+
})
2508+
.then(function() {
2509+
return Plotly.relayout(gd, {'xaxis.tickangle': 45});
2510+
})
2511+
.then(function() {
2512+
var size = gd._fullLayout._size;
2513+
expect(size.l).toBe(133);
2514+
expect(size.r).toBe(80);
2515+
expect(size.b).toBe(200); // bottom grows more
2516+
expect(size.t).toBe(100);
2517+
})
2518+
.then(function() {
2519+
return Plotly.relayout(gd, {'xaxis.tickangle': 30});
2520+
})
2521+
.then(function() {
2522+
var size = gd._fullLayout._size;
2523+
expect(size.l).toBe(133);
2524+
expect(size.r).toBe(80);
2525+
expect(size.b).toBe(154); // bottom shrinks back
2526+
expect(size.t).toBe(100);
2527+
})
2528+
.then(function() {
2529+
return Plotly.relayout(gd, {'yaxis.ticklen': 30});
2530+
})
2531+
.then(function() {
2532+
var size = gd._fullLayout._size;
2533+
expect(size.l).toBe(165); // left grows for tick length
2534+
expect(size.r).toBe(80);
2535+
expect(size.b).toBe(154);
2536+
expect(size.t).toBe(100);
2537+
})
2538+
.then(function() {
2539+
return Plotly.relayout(gd, {'yaxis.titlefont.size': 30});
2540+
})
2541+
.then(function() {
2542+
var size = gd._fullLayout._size;
2543+
expect(size.l).toBe(181); // left grows for axis title font size
2544+
expect(size.r).toBe(80);
2545+
expect(size.b).toBe(154);
2546+
expect(size.t).toBe(100);
2547+
})
2548+
.then(function() {
2549+
return Plotly.relayout(gd, {
2550+
'yaxis.side': 'right',
2551+
'xaxis.side': 'top'
2552+
});
2553+
})
2554+
.then(function() {
2555+
var size = gd._fullLayout._size;
2556+
// left-right and top-bottom swap
2557+
expect(size.l).toBe(80);
2558+
expect(size.r).toBe(181);
2559+
expect(size.b).toBe(80);
2560+
expect(size.t).toBe(154);
2561+
})
2562+
.then(function() {
2563+
return Plotly.relayout(gd, {
2564+
'xaxis.automargin': false,
2565+
'yaxis.automargin': false
2566+
});
2567+
})
2568+
.then(function() {
2569+
var size = gd._fullLayout._size;
2570+
// back to the defaults
2571+
expect(size.l).toBe(80);
2572+
expect(size.r).toBe(80);
2573+
expect(size.b).toBe(80);
2574+
expect(size.t).toBe(100);
2575+
})
2576+
.catch(failTest)
2577+
.then(done);
2578+
2579+
});
2580+
});
24502581
});
24512582

24522583
function getZoomInButton(gd) {

0 commit comments

Comments
 (0)