Skip to content

Commit 4a1c617

Browse files
committed
Add supplyTransitionDefaults to coerce input
1 parent 061d0d9 commit 4a1c617

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
@@ -2538,22 +2538,7 @@ Plotly.transition = function(gd, data, layout, traceIndices, transitionConfig) {
25382538
var i, traceIdx;
25392539
var fullLayout = gd._fullLayout;
25402540

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

25582543
var dataLength = Array.isArray(data) ? data.length : 0;
25592544

@@ -2667,7 +2652,7 @@ Plotly.transition = function(gd, data, layout, traceIndices, transitionConfig) {
26672652
basePlotModules[j].plot(gd, transitionedTraces, traceTransitionConfig);
26682653
}
26692654

2670-
gd._transitionData._completionTimeout = setTimeout(completeTransition, transitionConfig.duration);
2655+
gd._transitionData._completionTimeout = setTimeout(completeTransition, transitionConfig.duration + transitionConfig.delay);
26712656

26722657
if(!hasAxisTransition && !hasTraceTransition) {
26732658
return false;

src/plots/plots.js

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

2020
var plots = module.exports = {};
21+
var transitionAttrs = require('./transition_attributes');
2122

2223
// Expose registry methods on Plots for backward-compatibility
2324
Lib.extendFlat(plots, Registry);
@@ -618,6 +619,21 @@ plots.supplyDataDefaults = function(dataIn, dataOut, layout) {
618619
}
619620
};
620621

622+
plots.supplyTransitionDefaults = function(config) {
623+
var configOut = {};
624+
625+
function coerce(attr, dflt) {
626+
return Lib.coerce(config, configOut, transitionAttrs, attr, dflt);
627+
}
628+
629+
coerce('duration');
630+
coerce('ease');
631+
coerce('delay');
632+
coerce('redraw');
633+
634+
return configOut;
635+
};
636+
621637
plots.supplyTraceDefaults = function(traceIn, traceIndex, layout) {
622638
var traceOut = {},
623639
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)