Skip to content

Commit edb6773

Browse files
committed
Implement animate and update binding computation
1 parent 7ef0404 commit edb6773

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

src/plots/command.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
var Plotly = require('../plotly');
1313
var Lib = require('../lib');
14-
var helpers = require('../plot_api/helpers');
1514

1615
exports.executeAPICommand = function(gd, method, args) {
1716
var apiMethod = Plotly[method];
@@ -37,10 +36,16 @@ exports.computeAPICommandBindings = function(gd, method, args) {
3736
case 'relayout':
3837
bindings = computeLayoutBindings(gd, args);
3938
break;
40-
case 'animate':
39+
case 'update':
4140
bindings = computeDataBindings(gd, [args[0], args[2]])
4241
.concat(computeLayoutBindings(gd, [args[1]]));
4342
break;
43+
case 'animate':
44+
// This case could be analyzed more in-depth, but for a start,
45+
// we'll assume that the only relevant modification an animation
46+
// makes that's meaningfully tracked is the frame:
47+
bindings = ['layout._currentFrame'];
48+
break;
4449
default:
4550
// We'll elect to fail-non-fatal since this is a correct
4651
// answer and since this is not a validation method.
@@ -49,7 +54,7 @@ exports.computeAPICommandBindings = function(gd, method, args) {
4954
return bindings;
5055
};
5156

52-
function computeLayoutBindings (gd, args) {
57+
function computeLayoutBindings(gd, args) {
5358
var bindings = [];
5459

5560
var astr = args[0];
@@ -62,15 +67,15 @@ function computeLayoutBindings (gd, args) {
6267
return bindings;
6368
}
6469

65-
crawl(aobj, function (path, attrName, attr) {
70+
crawl(aobj, function(path) {
6671
bindings.push('layout' + path);
6772
});
6873

6974
return bindings;
7075
}
7176

72-
function computeDataBindings (gd, args) {
73-
var i, traces, astr, attr, val, traces, aobj;
77+
function computeDataBindings(gd, args) {
78+
var i, traces, astr, val, aobj;
7479
var bindings = [];
7580

7681
// Logic copied from Plotly.restyle:
@@ -91,33 +96,29 @@ function computeDataBindings (gd, args) {
9196
return bindings;
9297
}
9398

94-
if (traces === undefined) {
99+
if(traces === undefined) {
95100
traces = [];
96-
for (i = 0; i < gd.data.length; i++) {
101+
for(i = 0; i < gd.data.length; i++) {
97102
traces.push(i);
98103
}
99104
}
100105

101-
crawl(aobj, function (path, attrName, attr) {
106+
crawl(aobj, function(path, attrName, attr) {
102107
var nAttr;
103-
if (Array.isArray(attr)) {
108+
if(Array.isArray(attr)) {
104109
nAttr = Math.min(attr.length, traces.length);
105110
} else {
106111
nAttr = traces.length;
107112
}
108-
for (var j = 0; j < nAttr; j++) {
113+
for(var j = 0; j < nAttr; j++) {
109114
bindings.push('data[' + traces[j] + ']' + path);
110115
}
111116
});
112117

113118
return bindings;
114119
}
115120

116-
function crawl(attrs, callback, path, depth) {
117-
if(depth === undefined) {
118-
depth = 0;
119-
}
120-
121+
function crawl(attrs, callback, path) {
121122
if(path === undefined) {
122123
path = '';
123124
}
@@ -130,7 +131,7 @@ function crawl(attrs, callback, path, depth) {
130131
var thisPath = path + '.' + attrName;
131132

132133
if(Lib.isPlainObject(attr)) {
133-
crawl(attr, callback, thisPath, depth + 1);
134+
crawl(attr, callback, thisPath);
134135
} else {
135136
// Only execute the callback on leaf nodes:
136137
callback(thisPath, attrName, attr);

test/jasmine/tests/command_test.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var Plotly = require('@lib/index');
22
var PlotlyInternal = require('@src/plotly');
3-
var Lib = require('@src/lib');
43
var Plots = Plotly.Plots;
54
var createGraphDiv = require('../assets/create_graph_div');
65
var destroyGraphDiv = require('../assets/destroy_graph_div');
@@ -220,8 +219,6 @@ describe('Plots.computeAPICommandBindings', function() {
220219
}
221220
}, [1, 0]]);
222221

223-
// The results are definitely not completely intuitive, so this
224-
// is based upon empirical results with a codepen example:
225222
expect(result).toEqual([
226223
'data[1].y',
227224
'data[1].marker.size',
@@ -243,8 +240,6 @@ describe('Plots.computeAPICommandBindings', function() {
243240
}
244241
}, [1]]);
245242

246-
// The results are definitely not completely intuitive, so this
247-
// is based upon empirical results with a codepen example:
248243
expect(result).toEqual([
249244
'data[1].y',
250245
'data[1].marker.size',
@@ -264,31 +259,31 @@ describe('Plots.computeAPICommandBindings', function() {
264259
});
265260

266261
describe('with aobj notation', function() {
267-
it('and a single attribute', function () {
262+
it('and a single attribute', function() {
268263
var result = Plots.computeAPICommandBindings(gd, 'relayout', [{height: 500}]);
269264
expect(result).toEqual(['layout.height']);
270265
});
271266

272-
it('and two attributes', function () {
267+
it('and two attributes', function() {
273268
var result = Plots.computeAPICommandBindings(gd, 'relayout', [{height: 500, width: 100}]);
274269
expect(result).toEqual(['layout.height', 'layout.width']);
275270
});
276271
});
277272

278273
describe('with astr + val notation', function() {
279-
it('and an attribute', function () {
274+
it('and an attribute', function() {
280275
var result = Plots.computeAPICommandBindings(gd, 'relayout', ['width', 100]);
281276
expect(result).toEqual(['layout.width']);
282277
});
283278

284-
it('and nested atributes', function () {
279+
it('and nested atributes', function() {
285280
var result = Plots.computeAPICommandBindings(gd, 'relayout', ['margin.l', 10]);
286281
expect(result).toEqual(['layout.margin.l']);
287282
});
288283
});
289284

290285
describe('with mixed notation', function() {
291-
it('containing aob + astr', function () {
286+
it('containing aob + astr', function() {
292287
var result = Plots.computeAPICommandBindings(gd, 'relayout', [{
293288
'width': 100,
294289
'margin.l': 10
@@ -297,4 +292,37 @@ describe('Plots.computeAPICommandBindings', function() {
297292
});
298293
});
299294
});
295+
296+
describe('update', function() {
297+
it('computes bindings', function() {
298+
var result = Plots.computeAPICommandBindings(gd, 'update', [{
299+
y: [[3, 4, 5]],
300+
'marker.size': [10, 20, 25],
301+
'line.color': 'red',
302+
line: {
303+
width: [2, 8]
304+
}
305+
}, {
306+
'margin.l': 50,
307+
width: 10
308+
}, [1]]);
309+
310+
expect(result).toEqual([
311+
'data[1].y',
312+
'data[1].marker.size',
313+
'data[1].line.color',
314+
'data[1].line.width',
315+
'layout.margin.l',
316+
'layout.width'
317+
]);
318+
});
319+
});
320+
321+
describe('animate', function() {
322+
it('computes bindings', function() {
323+
var result = Plots.computeAPICommandBindings(gd, 'animate', [{}]);
324+
325+
expect(result).toEqual(['layout._currentFrame']);
326+
});
327+
});
300328
});

0 commit comments

Comments
 (0)