Skip to content

Commit 435a656

Browse files
authored
Merge pull request #3120 from plotly/autosize-edits
implied edits connecting height/width/autosize
2 parents 33067fb + 2e6eb23 commit 435a656

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

src/plot_api/plot_api.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,8 +1922,24 @@ function _relayout(gd, aobj) {
19221922
// back to its initial value as computed during the first pass in Plots.plotAutoSize.
19231923
//
19241924
// To do so, we must manually set them back here using the _initialAutoSize cache.
1925-
if(['width', 'height'].indexOf(ai) !== -1 && vi === null) {
1926-
fullLayout[ai] = gd._initialAutoSize[ai];
1925+
// can't use impliedEdits for this because behavior depends on vi
1926+
if(['width', 'height'].indexOf(ai) !== -1) {
1927+
if(vi) {
1928+
doextra('autosize', null);
1929+
// currently we don't support autosize one dim only - so
1930+
// explicitly set the other one. Note that doextra will
1931+
// ignore this if the same relayout call also provides oppositeAttr
1932+
var oppositeAttr = ai === 'height' ? 'width' : 'height';
1933+
doextra(oppositeAttr, fullLayout[oppositeAttr]);
1934+
}
1935+
else {
1936+
fullLayout[ai] = gd._initialAutoSize[ai];
1937+
}
1938+
}
1939+
else if(ai === 'autosize') {
1940+
// depends on vi here too, so again can't use impliedEdits
1941+
doextra('width', vi ? null : fullLayout.width);
1942+
doextra('height', vi ? null : fullLayout.height);
19271943
}
19281944
// check autorange vs range
19291945
else if(pleafPlus.match(AX_RANGE_RE)) {

test/jasmine/tests/config_test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ describe('config argument', function() {
4747
width: layoutWidth
4848
};
4949
var relayout = {
50-
width: relayoutWidth
50+
width: relayoutWidth,
51+
// didn't need this before #3120 - but since we're now
52+
// implicitly clearing autosize when edit width, if you really
53+
// want height to re-autosize you need to explicitly re-add
54+
// autosize
55+
autosize: autosize
5156
};
5257

5358
var layout2 = Lib.extendDeep({}, layout);

test/jasmine/tests/plot_api_test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,43 @@ describe('Test plot api', function() {
510510
.catch(failTest)
511511
.then(done);
512512
});
513+
514+
it('updates autosize/width/height correctly', function(done) {
515+
function assertSizeAndThen(width, height, auto, msg, next) {
516+
return function() {
517+
expect(gd._fullLayout.width).toBe(width, msg);
518+
expect(gd._fullLayout.height).toBe(height, msg);
519+
expect(gd._fullLayout.autosize).toBe(auto, msg);
520+
if(!auto) {
521+
expect(gd.layout.width).toBe(width, msg);
522+
expect(gd.layout.height).toBe(height, msg);
523+
}
524+
525+
if(next) return Plotly.relayout(gd, next);
526+
};
527+
}
528+
529+
// give gd css sizing to be picked up by autosize
530+
gd.style.width = '543px';
531+
gd.style.height = '432px';
532+
533+
Plotly.newPlot(gd, [{y: [1, 3, 2]}])
534+
.then(assertSizeAndThen(543, 432, true, 'initial',
535+
{autosize: false}))
536+
.then(assertSizeAndThen(543, 432, false, 'autosize false with no sizes',
537+
{autosize: true}))
538+
.then(assertSizeAndThen(543, 432, true, 'back to autosized',
539+
{width: 600}))
540+
.then(assertSizeAndThen(600, 432, false, 'explicit width causes explicit height',
541+
{width: null}))
542+
.then(assertSizeAndThen(543, 432, true, 'removed width',
543+
{height: 500, width: 700}))
544+
.then(assertSizeAndThen(700, 500, false, 'explicit height and width',
545+
{autosize: true}))
546+
.then(assertSizeAndThen(543, 432, true, 'final back to autosize'))
547+
.catch(failTest)
548+
.then(done);
549+
});
513550
});
514551

515552
describe('Plotly.relayout subroutines switchboard', function() {

0 commit comments

Comments
 (0)