Skip to content

Commit cc21e5c

Browse files
authored
Merge pull request #3090 from plotly/fix-autosize
improve autosize in edge cases
2 parents 3f7a2d3 + af4a2ce commit cc21e5c

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

src/plot_api/plot_api.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ function setPlotContext(gd, config) {
488488
if(context.setBackground === 'transparent' || typeof context.setBackground !== 'function') {
489489
context.setBackground = setBackground;
490490
}
491+
492+
// Check if gd has a specified widht/height to begin with
493+
context._hasZeroHeight = context._hasZeroHeight || gd.clientHeight === 0;
494+
context._hasZeroWidth = context._hasZeroWidth || gd.clientWidth === 0;
491495
}
492496

493497
function plotPolar(gd, data, layout) {
@@ -3248,9 +3252,6 @@ function makePlotFramework(gd) {
32483252
var gd3 = d3.select(gd);
32493253
var fullLayout = gd._fullLayout;
32503254

3251-
// Check if gd has a specified height
3252-
fullLayout._hasZeroHeight = gd.clientHeight === 0;
3253-
32543255
// Plot container
32553256
fullLayout._container = gd3.selectAll('.plot-container').data([0]);
32563257
fullLayout._container.enter().insert('div', ':first-child')

src/plot_api/plot_config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ module.exports = {
5757
*/
5858
autosizable: false,
5959

60-
// responsive: determines whether to change the layout size when window is resized
60+
/*
61+
* responsive: determines whether to change the layout size when window is resized.
62+
* In v2, this option will be removed and will always be true.
63+
*/
6164
responsive: false,
6265

6366
// set the length of the undo/redo queue

src/plot_api/subroutines.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ function lsInner(gd) {
5454
var i, subplot, plotinfo, xa, ya;
5555

5656
fullLayout._paperdiv.style({
57-
width: (fullLayout.autosize) ? '100%' : fullLayout.width + 'px',
58-
height: (fullLayout.autosize && !fullLayout._hasZeroHeight) ? '100%' : fullLayout.height + 'px'
57+
width: (gd._context.responsive && fullLayout.autosize && !gd._context._hasZeroWidth && !gd.layout.width) ? '100%' : fullLayout.width + 'px',
58+
height: (gd._context.responsive && fullLayout.autosize && !gd._context._hasZeroHeight && !gd.layout.height) ? '100%' : fullLayout.height + 'px'
5959
})
6060
.selectAll('.main-svg')
6161
.call(Drawing.setSize, fullLayout.width, fullLayout.height);

test/jasmine/tests/config_test.js

+45
Original file line numberDiff line numberDiff line change
@@ -705,5 +705,50 @@ describe('config argument', function() {
705705
.then(testResponsive)
706706
.then(done);
707707
});
708+
709+
it('should provide a fixed non-zero width/height when autosize/responsive: true and container\' size is zero', function(done) {
710+
fillParent(1, 1, function() {
711+
this.style.display = 'inline-block';
712+
this.style.width = null;
713+
this.style.height = null;
714+
});
715+
Plotly.plot(gd, data, {autosize: true}, {responsive: true})
716+
.then(function() {
717+
checkLayoutSize(700, 450);
718+
expect(gd.clientWidth).toBe(700);
719+
expect(gd.clientHeight).toBe(450);
720+
})
721+
.then(function() {
722+
return Plotly.newPlot(gd, data, {autosize: true}, {responsive: true});
723+
})
724+
// It is important to test newPlot to make sure an initially zero size container
725+
// is still considered to have zero size after a plot is drawn into it.
726+
.then(function() {
727+
checkLayoutSize(700, 450);
728+
expect(gd.clientWidth).toBe(700);
729+
expect(gd.clientHeight).toBe(450);
730+
})
731+
.catch(failTest)
732+
.then(done);
733+
});
734+
735+
// The following test is to guarantee we're not breaking the existing (although maybe not ideal) behaviour.
736+
// In a future version, one may prefer responsive/autosize:true winning over an explicit width/height when embedded in a webpage.
737+
it('should use the explicitly provided width/height even if autosize/responsive:true', function(done) {
738+
fillParent(1, 1, function() {
739+
this.style.width = '1000px';
740+
this.style.height = '500px';
741+
});
742+
743+
Plotly.plot(gd, data, {autosize: true, width: 1200, height: 700}, {responsive: true})
744+
.then(function() {
745+
expect(gd.clientWidth).toBe(1000);
746+
expect(gd.clientHeight).toBe(500);
747+
// The plot should overflow the container!
748+
checkLayoutSize(1200, 700);
749+
})
750+
.catch(failTest)
751+
.then(done);
752+
});
708753
});
709754
});

0 commit comments

Comments
 (0)