Skip to content

Commit

Permalink
Merge pull request #2788 from plotly/better-layout-animations
Browse files Browse the repository at this point in the history
Better axis range animations
  • Loading branch information
etpinard authored Jul 11, 2018
2 parents 5cfc6b1 + 19300f9 commit 9a92782
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 85 deletions.
24 changes: 1 addition & 23 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,29 +292,7 @@ exports.plot = function(gd, data, layout, config) {
return;
}

var subplots = fullLayout._subplots.cartesian;
var modules = fullLayout._modules;
var setPositionsArray = [];

// position and range calculations for traces that
// depend on each other ie bars (stacked or grouped)
// and boxes (grouped) push each other out of the way

var subplotInfo, i, j;

for(j = 0; j < modules.length; j++) {
Lib.pushUnique(setPositionsArray, modules[j].setPositions);
}

if(setPositionsArray.length) {
for(i = 0; i < subplots.length; i++) {
subplotInfo = fullLayout._plots[subplots[i]];

for(j = 0; j < setPositionsArray.length; j++) {
setPositionsArray[j](gd, subplotInfo);
}
}
}
Plots.doSetPositions(gd);

// calc and autorange for errorbars
Registry.getComponentMethod('errorbars', 'calc')(gd);
Expand Down
23 changes: 20 additions & 3 deletions src/plots/cartesian/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,32 @@ exports.finalizeSubplots = function(layoutIn, layoutOut) {
}
};

/**
* Cartesian.plot
*
* @param {DOM div | object} gd
* @param {array | null} (optional) traces
* array of traces indices to plot
* if undefined, plots all cartesian traces,
* if null, plots no traces
* @param {object} (optional) transitionOpts
* transition option object
* @param {function} (optional) makeOnCompleteCallback
* transition make callback function from Plots.transition
*/
exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
var fullLayout = gd._fullLayout;
var subplots = fullLayout._subplots.cartesian;
var calcdata = gd.calcdata;
var i;

// If traces is not provided, then it's a complete replot and missing
// traces are removed
if(!Array.isArray(traces)) {
if(traces === null) {
// this means no updates required, must return here
// so that plotOne doesn't remove the trace layers
return;
} else if(!Array.isArray(traces)) {
// If traces is not provided, then it's a complete replot and missing
// traces are removed
traces = [];
for(i = 0; i < calcdata.length; i++) traces.push(i);
}
Expand Down
33 changes: 29 additions & 4 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,7 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
// There's nothing to do if this module is not defined:
if(!module) continue;

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

plots.supplyDefaults(gd);

plots.doCalcdata(gd);

plots.doSetPositions(gd);
Registry.getComponentMethod('errorbars', 'calc')(gd);

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

function executeTransitions() {

gd.emit('plotly_transitioning', []);

return new Promise(function(resolve) {
Expand Down Expand Up @@ -2333,6 +2331,9 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
if(hasAxisTransition) {
traceTransitionOpts = Lib.extendFlat({}, transitionOpts);
traceTransitionOpts.duration = 0;
// This means do not transition traces,
// this happens on layout-only (e.g. axis range) animations
transitionedTraces = null;
} else {
traceTransitionOpts = transitionOpts;
}
Expand Down Expand Up @@ -2561,6 +2562,30 @@ function clearAxesCalc(axList) {
}
}

plots.doSetPositions = function(gd) {
var fullLayout = gd._fullLayout;
var subplots = fullLayout._subplots.cartesian;
var modules = fullLayout._modules;
var methods = [];
var i, j;

// position and range calculations for traces that
// depend on each other ie bars (stacked or grouped)
// and boxes (grouped) push each other out of the way

for(j = 0; j < modules.length; j++) {
Lib.pushUnique(methods, modules[j].setPositions);
}
if(!methods.length) return;

for(i = 0; i < subplots.length; i++) {
var subplotInfo = fullLayout._plots[subplots[i]];
for(j = 0; j < methods.length; j++) {
methods[j](gd, subplotInfo);
}
}
};

plots.rehover = function(gd) {
if(gd._fullLayout._rehover) {
gd._fullLayout._rehover();
Expand Down
Loading

0 comments on commit 9a92782

Please sign in to comment.