Skip to content

Commit bf714d0

Browse files
authored
Merge pull request #1054 from plotly/plot-call-signature
Modify Plotly.plot to accept frames #1014
2 parents 91c2d42 + 0bc4c6a commit bf714d0

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/plot_api/plot_api.js

+17
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ var subroutines = require('./subroutines');
4747
*
4848
*/
4949
Plotly.plot = function(gd, data, layout, config) {
50+
var frames;
51+
5052
gd = helpers.getGraphDiv(gd);
5153

5254
// Events.init is idempotent and bails early if gd has already been init'd
5355
Events.init(gd);
5456

57+
if(Lib.isPlainObject(data)) {
58+
var obj = data;
59+
data = obj.data;
60+
layout = obj.layout;
61+
config = obj.config;
62+
frames = obj.frames;
63+
}
64+
5565
var okToPlot = Events.triggerHandler(gd, 'plotly_beforeplot', [data, layout, config]);
5666
if(okToPlot === false) return Promise.reject();
5767

@@ -62,6 +72,12 @@ Plotly.plot = function(gd, data, layout, config) {
6272
'but this container doesn\'t yet have a plot.', gd);
6373
}
6474

75+
function addFrames() {
76+
if(frames) {
77+
return Plotly.addFrames(gd, frames);
78+
}
79+
}
80+
6581
// transfer configuration options to gd until we move over to
6682
// a more OO like model
6783
setPlotContext(gd, config);
@@ -329,6 +345,7 @@ Plotly.plot = function(gd, data, layout, config) {
329345

330346
Lib.syncOrAsync([
331347
Plots.previousPromises,
348+
addFrames,
332349
drawFramework,
333350
marginPushers,
334351
marginPushersAgain,

test/jasmine/tests/plot_api_test.js

+64
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var subroutines = require('@src/plot_api/subroutines');
1111
var d3 = require('d3');
1212
var createGraphDiv = require('../assets/create_graph_div');
1313
var destroyGraphDiv = require('../assets/destroy_graph_div');
14+
var fail = require('../assets/fail_test');
1415

1516

1617
describe('Test plot api', function() {
@@ -22,6 +23,69 @@ describe('Test plot api', function() {
2223
});
2324
});
2425

26+
describe('Plotly.plot', function() {
27+
var gd;
28+
29+
beforeEach(function() {
30+
gd = createGraphDiv();
31+
});
32+
33+
afterEach(destroyGraphDiv);
34+
35+
it('accepts gd, data, layout, and config as args', function(done) {
36+
Plotly.plot(gd,
37+
[{x: [1, 2, 3], y: [1, 2, 3]}],
38+
{width: 500, height: 500},
39+
{editable: true}
40+
).then(function() {
41+
expect(gd.layout.width).toEqual(500);
42+
expect(gd.layout.height).toEqual(500);
43+
expect(gd.data.length).toEqual(1);
44+
expect(gd._context.editable).toBe(true);
45+
}).catch(fail).then(done);
46+
});
47+
48+
it('accepts gd and an object as args', function(done) {
49+
Plotly.plot(gd, {
50+
data: [{x: [1, 2, 3], y: [1, 2, 3]}],
51+
layout: {width: 500, height: 500},
52+
config: {editable: true},
53+
frames: [{y: [2, 1, 0], name: 'frame1'}]
54+
}).then(function() {
55+
expect(gd.layout.width).toEqual(500);
56+
expect(gd.layout.height).toEqual(500);
57+
expect(gd.data.length).toEqual(1);
58+
expect(gd._transitionData._frames.length).toEqual(1);
59+
expect(gd._context.editable).toBe(true);
60+
}).catch(fail).then(done);
61+
});
62+
63+
it('allows adding more frames to the initial set', function(done) {
64+
Plotly.plot(gd, {
65+
data: [{x: [1, 2, 3], y: [1, 2, 3]}],
66+
layout: {width: 500, height: 500},
67+
config: {editable: true},
68+
frames: [{y: [7, 7, 7], name: 'frame1'}]
69+
}).then(function() {
70+
expect(gd.layout.width).toEqual(500);
71+
expect(gd.layout.height).toEqual(500);
72+
expect(gd.data.length).toEqual(1);
73+
expect(gd._transitionData._frames.length).toEqual(1);
74+
expect(gd._context.editable).toBe(true);
75+
76+
return Plotly.addFrames(gd, [
77+
{y: [8, 8, 8], name: 'frame2'},
78+
{y: [9, 9, 9], name: 'frame3'}
79+
]);
80+
}).then(function() {
81+
expect(gd._transitionData._frames.length).toEqual(3);
82+
expect(gd._transitionData._frames[0].name).toEqual('frame1');
83+
expect(gd._transitionData._frames[1].name).toEqual('frame2');
84+
expect(gd._transitionData._frames[2].name).toEqual('frame3');
85+
}).catch(fail).then(done);
86+
});
87+
});
88+
2589
describe('Plotly.relayout', function() {
2690
var gd;
2791

0 commit comments

Comments
 (0)