Skip to content

Commit ca04720

Browse files
authoredJun 16, 2020
Merge pull request #4925 from plotly/px-autosize
only take container computed size in px
2 parents fbb5742 + d21c0bf commit ca04720

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
 

‎src/plots/plots.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,15 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut, formatObj) {
15661566
)(layoutIn, layoutOut, coerce);
15671567
};
15681568

1569+
function getComputedSize(attr) {
1570+
return (
1571+
(typeof attr === 'string') &&
1572+
(attr.substr(attr.length - 2) === 'px') &&
1573+
parseFloat(attr)
1574+
);
1575+
}
1576+
1577+
15691578
plots.plotAutoSize = function plotAutoSize(gd, layout, fullLayout) {
15701579
var context = gd._context || {};
15711580
var frameMargins = context.frameMargins;
@@ -1592,8 +1601,8 @@ plots.plotAutoSize = function plotAutoSize(gd, layout, fullLayout) {
15921601
// but don't enforce any ratio restrictions
15931602
var computedStyle = isPlotDiv ? window.getComputedStyle(gd) : {};
15941603

1595-
newWidth = parseFloat(computedStyle.width) || parseFloat(computedStyle.maxWidth) || fullLayout.width;
1596-
newHeight = parseFloat(computedStyle.height) || parseFloat(computedStyle.maxHeight) || fullLayout.height;
1604+
newWidth = getComputedSize(computedStyle.width) || getComputedSize(computedStyle.maxWidth) || fullLayout.width;
1605+
newHeight = getComputedSize(computedStyle.height) || getComputedSize(computedStyle.maxHeight) || fullLayout.height;
15971606

15981607
if(isNumeric(frameMargins) && frameMargins > 0) {
15991608
var factor = 1 - 2 * frameMargins;

‎test/jasmine/tests/config_test.js

+27
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,33 @@ describe('config argument', function() {
193193

194194
testAutosize(autosize, config, layoutHeight, relayoutHeight, done);
195195
});
196+
197+
[
198+
{display: 'none', dflt: true},
199+
{display: '', dflt: false}
200+
].forEach(function(spec) {
201+
it('ignores percent sizes when container is hidden', function(done) {
202+
gd.style.width = '100%';
203+
gd.style.height = '100%';
204+
gd.style.display = spec.display;
205+
206+
var dfltWidth = Plots.layoutAttributes.width.dflt;
207+
var dfltHeight = Plots.layoutAttributes.height.dflt;
208+
209+
Plotly.plot(gd, data, {autosize: true})
210+
.then(function() {
211+
if(spec.dflt) {
212+
expect(gd._fullLayout.width).toBe(dfltWidth);
213+
expect(gd._fullLayout.height).toBe(dfltHeight);
214+
} else {
215+
expect(gd._fullLayout.width).not.toBe(dfltWidth);
216+
expect(gd._fullLayout.height).not.toBe(dfltHeight);
217+
}
218+
})
219+
.catch(failTest)
220+
.then(done);
221+
});
222+
});
196223
});
197224

198225
describe('showLink attribute', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.