Skip to content

Commit ecebe14

Browse files
committed
fix axis range animations
... by skipping Cartesian.plot step when no trace data need to be transitioned.
1 parent 5d9cf65 commit ecebe14

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/plots/cartesian/index.js

Lines changed: 20 additions & 3 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,6 +2333,9 @@ plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts)
23332333
if(hasAxisTransition) {
23342334
traceTransitionOpts = Lib.extendFlat({}, transitionOpts);
23352335
traceTransitionOpts.duration = 0;
2336+
// This means do not transition traces,
2337+
// this happens on layout-only (e.g. axis range) animations
2338+
transitionedTraces = null;
23362339
} else {
23372340
traceTransitionOpts = transitionOpts;
23382341
}

test/jasmine/tests/animate_test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Plotly = require('@lib/index');
22
var Lib = require('@src/lib');
3+
var Registry = require('@src/registry');
34
var Plots = Plotly.Plots;
45

56
var createGraphDiv = require('../assets/create_graph_div');
@@ -836,4 +837,43 @@ describe('animating scatter traces', function() {
836837
expect(gd.calcdata[0][0].y).toEqual(3);
837838
}).catch(failTest).then(done);
838839
});
840+
841+
it('should animate axis ranges using the less number of steps', function(done) {
842+
Plotly.plot(gd, [{
843+
y: [1, 2, 1]
844+
}, {
845+
type: 'bar',
846+
y: [2, 1, 2]
847+
}])
848+
.then(function() {
849+
spyOn(gd._fullData[0]._module.basePlotModule, 'transitionAxes').and.callThrough();
850+
spyOn(gd._fullData[0]._module, 'plot').and.callThrough();
851+
spyOn(gd._fullData[1]._module, 'plot').and.callThrough();
852+
spyOn(Registry, 'call').and.callThrough();
853+
854+
return Plotly.animate('graph', {
855+
layout: {
856+
xaxis: {range: [0.45, 0.55]},
857+
yaxis: {range: [0.45, 0.55]}
858+
}
859+
}, {
860+
transition: {duration: 500},
861+
frame: {redraw: false}
862+
});
863+
})
864+
.then(function() {
865+
// the only redraw should occur during Cartesian.transitionAxes,
866+
// where Registry.call('relayout') is called leading to a _module.plot call
867+
expect(gd._fullData[0]._module.basePlotModule.transitionAxes).toHaveBeenCalledTimes(1);
868+
expect(gd._fullData[0]._module.plot).toHaveBeenCalledTimes(1);
869+
expect(gd._fullData[1]._module.plot).toHaveBeenCalledTimes(1);
870+
expect(Registry.call).toHaveBeenCalledTimes(1);
871+
872+
var calls = Registry.call.calls.all();
873+
expect(calls.length).toBe(1, 'just one Registry.call call');
874+
expect(calls[0].args[0]).toBe('relayout', 'called Registry.call with');
875+
})
876+
.catch(failTest)
877+
.then(done);
878+
});
839879
});

0 commit comments

Comments
 (0)