Skip to content

Commit 0f92220

Browse files
committed
Add supplyTransitionDefaults to coerce input
1 parent a94b482 commit 0f92220

File tree

4 files changed

+143
-20
lines changed

4 files changed

+143
-20
lines changed

src/plot_api/plot_api.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -2536,22 +2536,7 @@ Plotly.transition = function(gd, data, layout, traceIndices, transitionConfig) {
25362536
var i, traceIdx;
25372537
var fullLayout = gd._fullLayout;
25382538

2539-
transitionConfig = Lib.extendFlat({
2540-
ease: 'cubic-in-out',
2541-
duration: 500,
2542-
delay: 0,
2543-
cascade: 0
2544-
}, transitionConfig || {});
2545-
2546-
// Create a single transition to be passed around:
2547-
if(transitionConfig.duration > 0) {
2548-
gd._currentTransition = d3.transition()
2549-
.duration(transitionConfig.duration)
2550-
.delay(transitionConfig.delay)
2551-
.ease(transitionConfig.ease);
2552-
} else {
2553-
gd._currentTransition = null;
2554-
}
2539+
transitionConfig = Plots.supplyTransitionDefaults(transitionConfig);
25552540

25562541
var dataLength = Array.isArray(data) ? data.length : 0;
25572542

@@ -2665,7 +2650,7 @@ Plotly.transition = function(gd, data, layout, traceIndices, transitionConfig) {
26652650
basePlotModules[j].plot(gd, transitionedTraces, traceTransitionConfig);
26662651
}
26672652

2668-
gd._transitionData._completionTimeout = setTimeout(completeTransition, transitionConfig.duration);
2653+
gd._transitionData._completionTimeout = setTimeout(completeTransition, transitionConfig.duration + transitionConfig.delay);
26692654

26702655
if(!hasAxisTransition && !hasTraceTransition) {
26712656
return false;

src/plots/plots.js

+16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Lib = require('../lib');
1717
var Color = require('../components/color');
1818

1919
var plots = module.exports = {};
20+
var transitionAttrs = require('./transition_attributes');
2021

2122
var modules = plots.modules = {},
2223
allTypes = plots.allTypes = [],
@@ -725,6 +726,21 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout) {
725726
}
726727
};
727728

729+
plots.supplyTransitionDefaults = function(config) {
730+
var configOut = {};
731+
732+
function coerce(attr, dflt) {
733+
return Lib.coerce(config, configOut, transitionAttrs, attr, dflt);
734+
}
735+
736+
coerce('duration');
737+
coerce('ease');
738+
coerce('delay');
739+
coerce('redraw');
740+
741+
return configOut;
742+
};
743+
728744
plots.supplyTraceDefaults = function(traceIn, traceIndex, layout) {
729745
var traceOut = {},
730746
defaultColor = Color.defaults[traceIndex % Color.defaults.length];

src/plots/transition_attributes.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = {
12+
duration: {
13+
valType: 'number',
14+
role: 'info',
15+
dflt: 500,
16+
description: [
17+
'The duration of the transition, in milliseconds. If equal to zero,',
18+
'updates are synchronous.'
19+
].join(' ')
20+
},
21+
ease: {
22+
valType: 'enumerated',
23+
dflt: 'cubic-in-out',
24+
values: [
25+
"linear",
26+
"quad",
27+
"cubic",
28+
"sin",
29+
"exp",
30+
"circle",
31+
"elastic",
32+
"back",
33+
"bounce",
34+
"linear-in",
35+
"quad-in",
36+
"cubic-in",
37+
"sin-in",
38+
"exp-in",
39+
"circle-in",
40+
"elastic-in",
41+
"back-in",
42+
"bounce-in",
43+
"linear-out",
44+
"quad-out",
45+
"cubic-out",
46+
"sin-out",
47+
"exp-out",
48+
"circle-out",
49+
"elastic-out",
50+
"back-out",
51+
"bounce-out",
52+
"linear-in-out",
53+
"quad-in-out",
54+
"cubic-in-out",
55+
"sin-in-out",
56+
"exp-in-out",
57+
"circle-in-out",
58+
"elastic-in-out",
59+
"back-in-out",
60+
"bounce-in-out"
61+
],
62+
role: 'info',
63+
description: 'The easing function used for the transition'
64+
},
65+
delay: {
66+
valType: 'number',
67+
role: 'info',
68+
dflt: 0,
69+
description: [
70+
'The duration of the transition, in milliseconds. If equal to zero,',
71+
'updates are synchronous.'
72+
].join(' ')
73+
},
74+
redraw: {
75+
valType: 'boolean',
76+
role: 'info',
77+
dflt: true,
78+
description: [
79+
'Redraw the plot at completion of the transition. This is desirable',
80+
'for transitions that include properties that cannot be transitioned,',
81+
'but may significantly slow down updates that do not require a full',
82+
'redraw of the plot'
83+
].join(' ')
84+
},
85+
};

test/jasmine/tests/transition_test.js

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
11
var Plotly = require('@lib/index');
22
var Lib = require('@src/lib');
3+
var Plots = Plotly.Plots;
34

45
var createGraphDiv = require('../assets/create_graph_div');
56
var destroyGraphDiv = require('../assets/destroy_graph_div');
67
var fail = require('../assets/fail_test');
78

89
var mock = require('@mocks/animation');
910

10-
describe('Transition API', function() {
11+
describe('Plots.supplyTransitionDefaults', function() {
12+
'use strict';
13+
14+
it('supplies transition defaults', function() {
15+
expect(Plots.supplyTransitionDefaults({})).toEqual({
16+
duration: 500,
17+
ease: 'cubic-in-out',
18+
redraw: true,
19+
delay: 0,
20+
});
21+
});
22+
23+
it('uses provided values', function() {
24+
expect(Plots.supplyTransitionDefaults({
25+
duration: 100,
26+
ease: 'quad-in-out',
27+
redraw: false,
28+
delay: 50,
29+
})).toEqual({
30+
duration: 100,
31+
ease: 'quad-in-out',
32+
redraw: false,
33+
delay: 50,
34+
});
35+
});
36+
37+
});
38+
39+
describe('Plotly.transition', function() {
1140
'use strict';
1241

1342
var gd;
@@ -26,11 +55,18 @@ describe('Transition API', function() {
2655

2756
it('resolves without waiting for transition to complete', function(done) {
2857
var t1 = Date.now();
29-
var duration = 100;
58+
var duration = 50;
59+
var calls = 0;
60+
// Callback to exit only after called twice:
61+
function end() {if (++calls === 2) done();}
62+
63+
// Not testing this, but make sure not to exit before the transition is all done:
64+
gd.on('plotly_endtransition', end);
65+
3066
Plotly.transition(gd, null, {'xaxis.range': [0.2, 0.3]}, null, {duration: duration}).then(function() {
3167
var t2 = Date.now();
3268
expect(t2 - t1).toBeLessThan(duration);
33-
}).catch(fail).then(done);
69+
}).catch(fail).then(end);
3470
});
3571

3672
it('emits plotly_begintransition on transition start', function(done) {
@@ -49,4 +85,5 @@ describe('Transition API', function() {
4985
Plotly.transition(gd, null, {'xaxis.range': [0.2, 0.3]}, null, {duration: 50});
5086
gd.on('plotly_endtransition', done);
5187
});
88+
5289
});

0 commit comments

Comments
 (0)