Skip to content

Commit 9a92782

Browse files
authored
Merge pull request #2788 from plotly/better-layout-animations
Better axis range animations
2 parents 5cfc6b1 + 19300f9 commit 9a92782

File tree

5 files changed

+182
-85
lines changed

5 files changed

+182
-85
lines changed

src/plot_api/plot_api.js

+1-23
Original file line numberDiff line numberDiff line change
@@ -292,29 +292,7 @@ exports.plot = function(gd, data, layout, config) {
292292
return;
293293
}
294294

295-
var subplots = fullLayout._subplots.cartesian;
296-
var modules = fullLayout._modules;
297-
var setPositionsArray = [];
298-
299-
// position and range calculations for traces that
300-
// depend on each other ie bars (stacked or grouped)
301-
// and boxes (grouped) push each other out of the way
302-
303-
var subplotInfo, i, j;
304-
305-
for(j = 0; j < modules.length; j++) {
306-
Lib.pushUnique(setPositionsArray, modules[j].setPositions);
307-
}
308-
309-
if(setPositionsArray.length) {
310-
for(i = 0; i < subplots.length; i++) {
311-
subplotInfo = fullLayout._plots[subplots[i]];
312-
313-
for(j = 0; j < setPositionsArray.length; j++) {
314-
setPositionsArray[j](gd, subplotInfo);
315-
}
316-
}
317-
}
295+
Plots.doSetPositions(gd);
318296

319297
// calc and autorange for errorbars
320298
Registry.getComponentMethod('errorbars', 'calc')(gd);

src/plots/cartesian/index.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,32 @@ exports.finalizeSubplots = function(layoutIn, layoutOut) {
122122
}
123123
};
124124

125+
/**
126+
* Cartesian.plot
127+
*
128+
* @param {DOM div | object} gd
129+
* @param {array | null} (optional) traces
130+
* array of traces indices to plot
131+
* if undefined, plots all cartesian traces,
132+
* if null, plots no traces
133+
* @param {object} (optional) transitionOpts
134+
* transition option object
135+
* @param {function} (optional) makeOnCompleteCallback
136+
* transition make callback function from Plots.transition
137+
*/
125138
exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
126139
var fullLayout = gd._fullLayout;
127140
var subplots = fullLayout._subplots.cartesian;
128141
var calcdata = gd.calcdata;
129142
var i;
130143

131-
// If traces is not provided, then it's a complete replot and missing
132-
// traces are removed
133-
if(!Array.isArray(traces)) {
144+
if(traces === null) {
145+
// this means no updates required, must return here
146+
// so that plotOne doesn't remove the trace layers
147+
return;
148+
} else if(!Array.isArray(traces)) {
149+
// If traces is not provided, then it's a complete replot and missing
150+
// traces are removed
134151
traces = [];
135152
for(i = 0; i < calcdata.length; i++) traces.push(i);
136153
}

src/plots/plots.js

+29-4
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
22022202
// There's nothing to do if this module is not defined:
22032203
if(!module) continue;
22042204

2205-
// Don't register the trace as transitioned if it doens't know what to do.
2205+
// Don't register the trace as transitioned if it doesn't know what to do.
22062206
// If it *is* registered, it will receive a callback that it's responsible
22072207
// for calling in order to register the transition as having completed.
22082208
if(module.animatable) {
@@ -2238,9 +2238,8 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
22382238
delete gd.calcdata;
22392239

22402240
plots.supplyDefaults(gd);
2241-
22422241
plots.doCalcdata(gd);
2243-
2242+
plots.doSetPositions(gd);
22442243
Registry.getComponentMethod('errorbars', 'calc')(gd);
22452244

22462245
return Promise.resolve();
@@ -2265,7 +2264,6 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
22652264
var aborted = false;
22662265

22672266
function executeTransitions() {
2268-
22692267
gd.emit('plotly_transitioning', []);
22702268

22712269
return new Promise(function(resolve) {
@@ -2333,6 +2331,9 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
23332331
if(hasAxisTransition) {
23342332
traceTransitionOpts = Lib.extendFlat({}, transitionOpts);
23352333
traceTransitionOpts.duration = 0;
2334+
// This means do not transition traces,
2335+
// this happens on layout-only (e.g. axis range) animations
2336+
transitionedTraces = null;
23362337
} else {
23372338
traceTransitionOpts = transitionOpts;
23382339
}
@@ -2561,6 +2562,30 @@ function clearAxesCalc(axList) {
25612562
}
25622563
}
25632564

2565+
plots.doSetPositions = function(gd) {
2566+
var fullLayout = gd._fullLayout;
2567+
var subplots = fullLayout._subplots.cartesian;
2568+
var modules = fullLayout._modules;
2569+
var methods = [];
2570+
var i, j;
2571+
2572+
// position and range calculations for traces that
2573+
// depend on each other ie bars (stacked or grouped)
2574+
// and boxes (grouped) push each other out of the way
2575+
2576+
for(j = 0; j < modules.length; j++) {
2577+
Lib.pushUnique(methods, modules[j].setPositions);
2578+
}
2579+
if(!methods.length) return;
2580+
2581+
for(i = 0; i < subplots.length; i++) {
2582+
var subplotInfo = fullLayout._plots[subplots[i]];
2583+
for(j = 0; j < methods.length; j++) {
2584+
methods[j](gd, subplotInfo);
2585+
}
2586+
}
2587+
};
2588+
25642589
plots.rehover = function(gd) {
25652590
if(gd._fullLayout._rehover) {
25662591
gd._fullLayout._rehover();

0 commit comments

Comments
 (0)