Skip to content

Commit bcab36e

Browse files
authored
Merge pull request #2758 from plotly/plots-resize-sanitize-margins
Sanitize margin after 'autosize' relayouts
2 parents edc166f + 410e2ae commit bcab36e

File tree

2 files changed

+77
-50
lines changed

2 files changed

+77
-50
lines changed

src/plots/plots.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ plots.supplyDefaults = function(gd, opts) {
345345

346346
if(!newLayout.width) newFullLayout.width = oldWidth;
347347
if(!newLayout.height) newFullLayout.height = oldHeight;
348+
plots.sanitizeMargins(newFullLayout);
348349
}
349350
else {
350351

@@ -357,7 +358,7 @@ plots.supplyDefaults = function(gd, opts) {
357358
initialAutoSize = missingWidthOrHeight && (autosize || autosizable);
358359

359360
if(initialAutoSize) plots.plotAutoSize(gd, newLayout, newFullLayout);
360-
else if(missingWidthOrHeight) plots.sanitizeMargins(gd);
361+
else if(missingWidthOrHeight) plots.sanitizeMargins(newFullLayout);
361362

362363
// for backwards-compatibility with Plotly v1.x.x
363364
if(!autosize && missingWidthOrHeight) {

test/jasmine/tests/plots_test.js

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -289,74 +289,100 @@ describe('Test Plots', function() {
289289
});
290290
});
291291

292-
describe('Plots.resize', function() {
292+
describe('Plots.resize:', function() {
293293
var gd;
294294

295-
beforeAll(function(done) {
296-
gd = createGraphDiv();
295+
describe('on graph div DOM style changes', function() {
296+
beforeAll(function(done) {
297+
gd = createGraphDiv();
297298

298-
Plotly.plot(gd, [{ x: [1, 2, 3], y: [2, 3, 4] }])
299-
.then(function() {
300-
gd.style.width = '400px';
301-
gd.style.height = '400px';
299+
Plotly.plot(gd, [{ x: [1, 2, 3], y: [2, 3, 4] }])
300+
.then(function() {
301+
gd.style.width = '400px';
302+
gd.style.height = '400px';
302303

303-
return Plotly.Plots.resize(gd);
304-
})
305-
.then(done);
306-
});
304+
return Plotly.Plots.resize(gd);
305+
})
306+
.then(done);
307+
});
307308

308-
afterAll(destroyGraphDiv);
309+
afterAll(destroyGraphDiv);
309310

310-
it('should resize the plot clip', function() {
311-
var uid = gd._fullLayout._uid;
311+
it('should resize the plot clip', function() {
312+
var uid = gd._fullLayout._uid;
312313

313-
var plotClip = document.getElementById('clip' + uid + 'xyplot'),
314-
clipRect = plotClip.children[0],
315-
clipWidth = +clipRect.getAttribute('width'),
316-
clipHeight = +clipRect.getAttribute('height');
314+
var plotClip = document.getElementById('clip' + uid + 'xyplot'),
315+
clipRect = plotClip.children[0],
316+
clipWidth = +clipRect.getAttribute('width'),
317+
clipHeight = +clipRect.getAttribute('height');
317318

318-
expect(clipWidth).toBe(240);
319-
expect(clipHeight).toBe(220);
320-
});
319+
expect(clipWidth).toBe(240);
320+
expect(clipHeight).toBe(220);
321+
});
321322

322-
it('should resize the main svgs', function() {
323-
var mainSvgs = document.getElementsByClassName('main-svg');
324-
expect(mainSvgs.length).toBe(2);
323+
it('should resize the main svgs', function() {
324+
var mainSvgs = document.getElementsByClassName('main-svg');
325+
expect(mainSvgs.length).toBe(2);
325326

326-
for(var i = 0; i < mainSvgs.length; i++) {
327-
var svg = mainSvgs[i],
328-
svgWidth = +svg.getAttribute('width'),
329-
svgHeight = +svg.getAttribute('height');
327+
for(var i = 0; i < mainSvgs.length; i++) {
328+
var svg = mainSvgs[i],
329+
svgWidth = +svg.getAttribute('width'),
330+
svgHeight = +svg.getAttribute('height');
330331

331-
expect(svgWidth).toBe(400);
332-
expect(svgHeight).toBe(400);
333-
}
334-
});
332+
expect(svgWidth).toBe(400);
333+
expect(svgHeight).toBe(400);
334+
}
335+
});
336+
337+
it('should update the axis scales', function() {
338+
var mainSvgs = document.getElementsByClassName('main-svg');
339+
expect(mainSvgs.length).toBe(2);
335340

336-
it('should update the axis scales', function() {
337-
var mainSvgs = document.getElementsByClassName('main-svg');
338-
expect(mainSvgs.length).toBe(2);
341+
var fullLayout = gd._fullLayout,
342+
plotinfo = fullLayout._plots.xy;
339343

340-
var fullLayout = gd._fullLayout,
341-
plotinfo = fullLayout._plots.xy;
344+
expect(fullLayout.xaxis._length).toEqual(240);
345+
expect(fullLayout.yaxis._length).toEqual(220);
342346

343-
expect(fullLayout.xaxis._length).toEqual(240);
344-
expect(fullLayout.yaxis._length).toEqual(220);
347+
expect(plotinfo.xaxis._length).toEqual(240);
348+
expect(plotinfo.yaxis._length).toEqual(220);
349+
});
350+
351+
it('should allow resizing by plot ID', function(done) {
352+
var mainSvgs = document.getElementsByClassName('main-svg');
353+
expect(mainSvgs.length).toBe(2);
354+
355+
expect(typeof gd.id).toBe('string');
356+
expect(gd.id).toBeTruthy();
345357

346-
expect(plotinfo.xaxis._length).toEqual(240);
347-
expect(plotinfo.yaxis._length).toEqual(220);
358+
Plotly.Plots.resize(gd.id)
359+
.catch(failTest)
360+
.then(done);
361+
});
348362
});
349363

350-
it('should allow resizing by plot ID', function(done) {
351-
var mainSvgs = document.getElementsByClassName('main-svg');
352-
expect(mainSvgs.length).toBe(2);
364+
describe('on styled graph div', function() {
365+
afterEach(destroyGraphDiv);
353366

354-
expect(typeof gd.id).toBe('string');
355-
expect(gd.id).toBeTruthy();
367+
it('should sanitize margins', function(done) {
368+
gd = createGraphDiv();
369+
gd.style.width = '150px';
370+
gd.style.height = '150px';
356371

357-
Plotly.Plots.resize(gd.id)
358-
.catch(failTest)
359-
.then(done);
372+
function _assert(exp) {
373+
var margin = gd._fullLayout.margin || {};
374+
for(var k in exp) {
375+
expect(margin[k]).toBe(exp[k], ' - margin.' + k);
376+
}
377+
}
378+
379+
Plotly.plot(gd, [], {})
380+
.then(function() { _assert({l: 74, r: 74, t: 82, b: 66}); })
381+
.then(function() { return Plotly.Plots.resize(gd); })
382+
.then(function() { _assert({l: 74, r: 74, t: 82, b: 66}); })
383+
.catch(failTest)
384+
.then(done);
385+
});
360386
});
361387
});
362388

0 commit comments

Comments
 (0)