Skip to content

Commit fa85e21

Browse files
authored
Merge pull request #2437 from plotly/react-autosize
Autosize / autorange fixes
2 parents 161b602 + 247c626 commit fa85e21

File tree

90 files changed

+837
-651
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+837
-651
lines changed

Diff for: src/components/grid/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var gridAttrs = {
5454
valType: 'info_array',
5555
freeLength: true,
5656
dimensions: 2,
57-
items: {valType: 'enumerated', values: [counterRegex('xy').toString(), '']},
57+
items: {valType: 'enumerated', values: [counterRegex('xy').toString(), ''], editType: 'plot'},
5858
role: 'info',
5959
editType: 'plot',
6060
description: [
@@ -69,7 +69,7 @@ var gridAttrs = {
6969
xaxes: {
7070
valType: 'info_array',
7171
freeLength: true,
72-
items: {valType: 'enumerated', values: [cartesianIdRegex.x.toString(), '']},
72+
items: {valType: 'enumerated', values: [cartesianIdRegex.x.toString(), ''], editType: 'plot'},
7373
role: 'info',
7474
editType: 'plot',
7575
description: [
@@ -83,7 +83,7 @@ var gridAttrs = {
8383
yaxes: {
8484
valType: 'info_array',
8585
freeLength: true,
86-
items: {valType: 'enumerated', values: [cartesianIdRegex.y.toString(), '']},
86+
items: {valType: 'enumerated', values: [cartesianIdRegex.y.toString(), ''], editType: 'plot'},
8787
role: 'info',
8888
editType: 'plot',
8989
description: [

Diff for: src/components/rangeslider/calc_autorange.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
'use strict';
1010

11-
var Axes = require('../../plots/cartesian/axes');
11+
var listAxes = require('../../plots/cartesian/axis_ids').list;
12+
var getAutoRange = require('../../plots/cartesian/autorange').getAutoRange;
1213
var constants = require('./constants');
1314

1415
module.exports = function calcAutorange(gd) {
15-
var axes = Axes.list(gd, 'x', true);
16+
var axes = listAxes(gd, 'x', true);
1617

1718
// Compute new slider range using axis autorange if necessary.
1819
//
@@ -28,7 +29,7 @@ module.exports = function calcAutorange(gd) {
2829

2930
if(opts && opts.visible && opts.autorange && ax._min.length && ax._max.length) {
3031
opts._input.autorange = true;
31-
opts._input.range = opts.range = Axes.getAutoRange(ax);
32+
opts._input.range = opts.range = getAutoRange(ax);
3233
}
3334
}
3435
};

Diff for: src/plot_api/plot_api.js

+52-31
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var cartesianConstants = require('../plots/cartesian/constants');
4040
var axisConstraints = require('../plots/cartesian/constraints');
4141
var enforceAxisConstraints = axisConstraints.enforce;
4242
var cleanAxisConstraints = axisConstraints.clean;
43-
var axisIds = require('../plots/cartesian/axis_ids');
43+
var doAutoRange = require('../plots/cartesian/autorange').doAutoRange;
4444

4545
var numericNameWarningCount = 0;
4646
var numericNameWarningCountLimit = 5;
@@ -283,7 +283,7 @@ exports.plot = function(gd, data, layout, config) {
283283

284284
function positionAndAutorange() {
285285
if(!recalc) {
286-
enforceAxisConstraints(gd);
286+
doAutoRangeAndConstraints();
287287
return;
288288
}
289289

@@ -332,7 +332,7 @@ exports.plot = function(gd, data, layout, config) {
332332
var ax = axList[i];
333333
cleanAxisConstraints(gd, ax);
334334

335-
Axes.doAutoRange(ax);
335+
doAutoRange(ax);
336336
}
337337

338338
enforceAxisConstraints(gd);
@@ -1846,7 +1846,7 @@ function _relayout(gd, aobj) {
18461846
var axId;
18471847

18481848
function recordAlteredAxis(pleafPlus) {
1849-
var axId = axisIds.name2id(pleafPlus.split('.')[0]);
1849+
var axId = Axes.name2id(pleafPlus.split('.')[0]);
18501850
rangesAltered[axId] = 1;
18511851
return axId;
18521852
}
@@ -1857,20 +1857,21 @@ function _relayout(gd, aobj) {
18571857
throw new Error('cannot set ' + ai + 'and a parent attribute simultaneously');
18581858
}
18591859

1860-
var p = Lib.nestedProperty(layout, ai),
1861-
vi = aobj[ai],
1862-
plen = p.parts.length,
1863-
// p.parts may end with an index integer if the property is an array
1864-
pend = typeof p.parts[plen - 1] === 'string' ? (plen - 1) : (plen - 2),
1865-
// last property in chain (leaf node)
1866-
pleaf = p.parts[pend],
1867-
// leaf plus immediate parent
1868-
pleafPlus = p.parts[pend - 1] + '.' + pleaf,
1869-
// trunk nodes (everything except the leaf)
1870-
ptrunk = p.parts.slice(0, pend).join('.'),
1871-
parentIn = Lib.nestedProperty(gd.layout, ptrunk).get(),
1872-
parentFull = Lib.nestedProperty(fullLayout, ptrunk).get(),
1873-
vOld = p.get();
1860+
var p = Lib.nestedProperty(layout, ai);
1861+
var vi = aobj[ai];
1862+
var plen = p.parts.length;
1863+
// p.parts may end with an index integer if the property is an array
1864+
var pend = plen - 1;
1865+
while(pend > 0 && typeof p.parts[plen - 1] !== 'string') { pend--; }
1866+
// last property in chain (leaf node)
1867+
var pleaf = p.parts[pend];
1868+
// leaf plus immediate parent
1869+
var pleafPlus = p.parts[pend - 1] + '.' + pleaf;
1870+
// trunk nodes (everything except the leaf)
1871+
var ptrunk = p.parts.slice(0, pend).join('.');
1872+
var parentIn = Lib.nestedProperty(gd.layout, ptrunk).get();
1873+
var parentFull = Lib.nestedProperty(fullLayout, ptrunk).get();
1874+
var vOld = p.get();
18741875

18751876
if(vi === undefined) continue;
18761877

@@ -2093,25 +2094,18 @@ function _relayout(gd, aobj) {
20932094
flags.calc = true;
20942095
for(var groupAxId in group) {
20952096
if(!rangesAltered[groupAxId]) {
2096-
axisIds.getFromId(gd, groupAxId)._constraintShrinkable = true;
2097+
Axes.getFromId(gd, groupAxId)._constraintShrinkable = true;
20972098
}
20982099
}
20992100
}
21002101
}
21012102
}
21022103

2103-
var oldWidth = fullLayout.width,
2104-
oldHeight = fullLayout.height;
2105-
2106-
// calculate autosizing
2107-
if(gd.layout.autosize) Plots.plotAutoSize(gd, gd.layout, fullLayout);
2108-
2109-
// avoid unnecessary redraws
2110-
var hasSizechanged = aobj.height || aobj.width ||
2111-
(fullLayout.width !== oldWidth) ||
2112-
(fullLayout.height !== oldHeight);
2113-
2114-
if(hasSizechanged) flags.calc = true;
2104+
// If the autosize changed or height or width was explicitly specified,
2105+
// this triggers a redraw
2106+
// TODO: do we really need special aobj.height/width handling here?
2107+
// couldn't editType do this?
2108+
if(updateAutosize(gd) || aobj.height || aobj.width) flags.plot = true;
21152109

21162110
if(flags.plot || flags.calc) {
21172111
flags.layoutReplot = true;
@@ -2128,6 +2122,22 @@ function _relayout(gd, aobj) {
21282122
};
21292123
}
21302124

2125+
/*
2126+
* updateAutosize: we made a change, does it change the autosize result?
2127+
* puts the new size into fullLayout
2128+
* returns true if either height or width changed
2129+
*/
2130+
function updateAutosize(gd) {
2131+
var fullLayout = gd._fullLayout;
2132+
var oldWidth = fullLayout.width;
2133+
var oldHeight = fullLayout.height;
2134+
2135+
// calculate autosizing
2136+
if(gd.layout.autosize) Plots.plotAutoSize(gd, gd.layout, fullLayout);
2137+
2138+
return (fullLayout.width !== oldWidth) || (fullLayout.height !== oldHeight);
2139+
}
2140+
21312141
// for editing annotations or shapes - is it on autoscaled axes?
21322142
function refAutorange(gd, obj, axLetter) {
21332143
if(!Lib.isPlainObject(obj)) return false;
@@ -2313,6 +2323,17 @@ exports.react = function(gd, data, layout, config) {
23132323
var restyleFlags = diffData(gd, oldFullData, newFullData, immutable);
23142324
var relayoutFlags = diffLayout(gd, oldFullLayout, newFullLayout, immutable);
23152325

2326+
// TODO: how to translate this part of relayout to Plotly.react?
2327+
// // Setting width or height to null must reset the graph's width / height
2328+
// // back to its initial value as computed during the first pass in Plots.plotAutoSize.
2329+
// //
2330+
// // To do so, we must manually set them back here using the _initialAutoSize cache.
2331+
// if(['width', 'height'].indexOf(ai) !== -1 && vi === null) {
2332+
// fullLayout[ai] = gd._initialAutoSize[ai];
2333+
// }
2334+
2335+
if(updateAutosize(gd)) relayoutFlags.layoutReplot = true;
2336+
23162337
// clear calcdata if required
23172338
if(restyleFlags.calc || relayoutFlags.calc) gd.calcdata = undefined;
23182339
if(relayoutFlags.margins) helpers.clearAxisAutomargins(gd);

0 commit comments

Comments
 (0)