Skip to content

Commit 9f7a8e7

Browse files
committed
improve autosize in edge case
- provide fixed non-zero width/height when autosize:true and container's size is zero - when setting container's widht/height, ignore autosize:true if width/height is explicitly specified by the user. - 🔒 down in test that explicit width/height win over autosize:true
1 parent 5cc37e3 commit 9f7a8e7

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
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/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: (fullLayout.autosize && !gd._context._hasZeroWidth && !gd.layout.width) ? '100%' : fullLayout.width + 'px',
58+
height: (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

+38
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,44 @@ describe('config argument', function() {
174174
.then(done);
175175
});
176176

177+
it('should provide a fixed non-zero width/height when autosize: true and container\' size is zero', function(done) {
178+
gd.style.display = 'inline-block';
179+
Plotly.plot(gd, data, {autosize: true})
180+
.then(function() {
181+
checkLayoutSize(700, 450);
182+
expect(gd.clientWidth).toBe(700);
183+
expect(gd.clientHeight).toBe(450);
184+
})
185+
.then(function() {
186+
return Plotly.newPlot(gd, data, {autosize: true});
187+
})
188+
// It is important to test newPlot to make sure an initially zero size container
189+
// is still considered to have zero size after a plot is drawn into it.
190+
.then(function() {
191+
checkLayoutSize(700, 450);
192+
expect(gd.clientWidth).toBe(700);
193+
expect(gd.clientHeight).toBe(450);
194+
})
195+
.catch(failTest)
196+
.then(done);
197+
});
198+
199+
// The following test is to guarantee we're not breaking the existing behaviour which may not be ideal.
200+
// In a future version, one may prefer autosize:true winning over an explicit width/height when embedded in a webpage.
201+
it('should use the explicitly provided width/height even if autosize:true', function(done) {
202+
gd.style.width = '1000px';
203+
gd.style.height = '500px';
204+
Plotly.plot(gd, data, {autosize: true, width: 1200, height: 700})
205+
.then(function() {
206+
expect(gd.clientWidth).toBe(1000);
207+
expect(gd.clientHeight).toBe(500);
208+
// The plot should overflow the container!
209+
checkLayoutSize(1200, 700);
210+
})
211+
.catch(failTest)
212+
.then(done);
213+
});
214+
177215
it('should respect attribute autosizable: false', function(done) {
178216
var autosize = false;
179217
var config = {

0 commit comments

Comments
 (0)