Skip to content

Commit c820abb

Browse files
authored
Merge pull request #3811 from plotly/better-did-margin-change-check
Better did-margin-change check
2 parents 29f94b6 + d5595d7 commit c820abb

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

Diff for: src/plot_api/plot_api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ function plot(gd, data, layout, config) {
198198
* start async-friendly code - now we're actually drawing things
199199
*/
200200

201-
var oldmargins = JSON.stringify(fullLayout._size);
201+
var oldMargins = Lib.extendFlat({}, fullLayout._size);
202202

203203
// draw framework first so that margin-pushing
204204
// components can position themselves correctly
@@ -311,7 +311,7 @@ function plot(gd, data, layout, config) {
311311

312312
// in case the margins changed, draw margin pushers again
313313
function marginPushersAgain() {
314-
if(JSON.stringify(fullLayout._size) === oldmargins) return;
314+
if(!Plots.didMarginChange(oldMargins, fullLayout._size)) return;
315315

316316
return Lib.syncOrAsync([
317317
marginPushers,

Diff for: src/plots/plots.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,9 @@ plots.autoMargin = function(gd, id, o) {
18021802
pushMarginIds[id] = 1;
18031803
}
18041804

1805-
if(!fullLayout._replotting) plots.doAutoMargin(gd);
1805+
if(!fullLayout._replotting) {
1806+
plots.doAutoMargin(gd);
1807+
}
18061808
}
18071809
};
18081810

@@ -1812,8 +1814,8 @@ plots.doAutoMargin = function(gd) {
18121814
initMargins(fullLayout);
18131815

18141816
var gs = fullLayout._size;
1815-
var oldmargins = JSON.stringify(gs);
18161817
var margin = fullLayout.margin;
1818+
var oldMargins = Lib.extendFlat({}, gs);
18171819

18181820
// adjust margins for outside components
18191821
// fullLayout.margin is the requested margin,
@@ -1892,10 +1894,7 @@ plots.doAutoMargin = function(gd) {
18921894
gs.h = Math.round(height) - gs.t - gs.b;
18931895

18941896
// if things changed and we're not already redrawing, trigger a redraw
1895-
if(!fullLayout._replotting &&
1896-
oldmargins !== '{}' &&
1897-
oldmargins !== JSON.stringify(fullLayout._size)
1898-
) {
1897+
if(!fullLayout._replotting && plots.didMarginChange(oldMargins, gs)) {
18991898
if('_redrawFromAutoMarginCount' in fullLayout) {
19001899
fullLayout._redrawFromAutoMarginCount++;
19011900
} else {
@@ -1905,6 +1904,22 @@ plots.doAutoMargin = function(gd) {
19051904
}
19061905
};
19071906

1907+
var marginKeys = ['l', 'r', 't', 'b', 'p', 'w', 'h'];
1908+
1909+
plots.didMarginChange = function(margin0, margin1) {
1910+
for(var i = 0; i < marginKeys.length; i++) {
1911+
var k = marginKeys[i];
1912+
var m0 = margin0[k];
1913+
var m1 = margin1[k];
1914+
// use 1px tolerance in case we old/new differ only
1915+
// by rounding errors, which can lead to infinite loops
1916+
if(!isNumeric(m0) || Math.abs(m1 - m0) > 1) {
1917+
return true;
1918+
}
1919+
}
1920+
return false;
1921+
};
1922+
19081923
/**
19091924
* JSONify the graph data and layout
19101925
*

Diff for: test/jasmine/tests/plots_test.js

+69
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Plotly = require('@lib/index');
22
var Plots = require('@src/plots/plots');
33
var Lib = require('@src/lib');
4+
var Registry = require('@src/registry');
45

56
var d3 = require('d3');
67
var createGraphDiv = require('../assets/create_graph_div');
@@ -936,6 +937,74 @@ describe('Test Plots', function() {
936937
.then(done);
937938
});
938939
});
940+
941+
describe('Test Plots.doAutoMargin', function() {
942+
afterEach(destroyGraphDiv);
943+
944+
it('should trigger a replot when necessary', function(done) {
945+
var gd = createGraphDiv();
946+
var r0;
947+
var w0;
948+
949+
function _assert(msg, exp) {
950+
var fullLayout = gd._fullLayout;
951+
952+
expect(fullLayout._size.r).toBe(exp.r);
953+
expect(fullLayout._size.w).toBe(exp.w);
954+
955+
expect(Registry.call).toHaveBeenCalledTimes(exp.plotCallCnt);
956+
Registry.call.calls.reset();
957+
}
958+
959+
Plotly.newPlot(gd, [{
960+
y: [1, 2, 1],
961+
name: 'A trace name long enough to push the right margin'
962+
}], {
963+
showlegend: true
964+
})
965+
.then(function() {
966+
r0 = gd._fullLayout._size.r;
967+
w0 = gd._fullLayout._size.w;
968+
spyOn(Registry, 'call');
969+
})
970+
.then(function() {
971+
return Plots.doAutoMargin(gd);
972+
})
973+
.then(function() {
974+
_assert('after doAutoMargin() with identical margins', {
975+
r: r0,
976+
w: w0,
977+
plotCallCnt: 0
978+
});
979+
})
980+
.then(function() {
981+
gd._fullLayout._pushmargin.legend.r.size += 2;
982+
return Plots.doAutoMargin(gd);
983+
})
984+
.then(function() {
985+
_assert('after doAutoMargin() with bigger margins', {
986+
r: r0 + 2,
987+
w: w0 - 2,
988+
plotCallCnt: 1
989+
});
990+
})
991+
.then(function() {
992+
gd._fullLayout._pushmargin.legend.r.size += 1;
993+
return Plots.doAutoMargin(gd);
994+
})
995+
.then(function() {
996+
// see https://github.com/plotly/plotly.js/issues/3561#issuecomment-485953778
997+
// for more info
998+
_assert('after doAutoMargin() with bigger margins under tolerance', {
999+
r: r0 + 3,
1000+
w: w0 - 3,
1001+
plotCallCnt: 0
1002+
});
1003+
})
1004+
.catch(failTest)
1005+
.then(done);
1006+
});
1007+
});
9391008
});
9401009

9411010
describe('grids', function() {

0 commit comments

Comments
 (0)