Skip to content

Commit 49307f8

Browse files
authored
Merge pull request #2212 from plotly/resize-id
allow use of element ID in Plots.resize
2 parents 265b7f3 + f28e96a commit 49307f8

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/plots/plots.js

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ plots.getSubplotCalcData = function(calcData, type, subplotId) {
191191
// the text is at first, so it needs to draw it,
192192
// then wait a little, then draw it again
193193
plots.redrawText = function(gd) {
194+
gd = Lib.getGraphDiv(gd);
194195

195196
// do not work if polar is present
196197
if((gd.data && gd.data[0] && gd.data[0].r)) return;
@@ -211,6 +212,8 @@ plots.redrawText = function(gd) {
211212

212213
// resize plot about the container size
213214
plots.resize = function(gd) {
215+
gd = Lib.getGraphDiv(gd);
216+
214217
return new Promise(function(resolve, reject) {
215218

216219
function isHidden(gd) {

test/jasmine/tests/plot_promise_test.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ describe('Plotly.___ methods', function() {
461461
Plotly.plot(initialDiv, data, {}).then(done);
462462
});
463463

464+
afterEach(destroyGraphDiv);
465+
464466
it('should return a resolved promise of the gd', function(done) {
465467
Plotly.Plots.resize(initialDiv).then(function(gd) {
466468
expect(gd).toBeDefined();
@@ -469,14 +471,30 @@ describe('Plotly.___ methods', function() {
469471
}).then(done);
470472
});
471473

472-
it('should return a rejected promise with no argument', function(done) {
473-
Plotly.Plots.resize().then(function() {
474+
it('should return a rejected promise if gd is hidden', function(done) {
475+
initialDiv.style.display = 'none';
476+
Plotly.Plots.resize(initialDiv).then(function() {
477+
expect(1).toBe(0, 'We were supposed to get an error.');
478+
}, function(err) {
479+
expect(err).toBeDefined();
480+
expect(err.message).toBe('Resize must be passed a displayed plot div element.');
481+
}).then(done);
482+
});
483+
484+
it('should return a rejected promise if gd is detached from the DOM', function(done) {
485+
destroyGraphDiv();
486+
Plotly.Plots.resize(initialDiv).then(function() {
474487
expect(1).toBe(0, 'We were supposed to get an error.');
475488
}, function(err) {
476489
expect(err).toBeDefined();
477490
expect(err.message).toBe('Resize must be passed a displayed plot div element.');
478491
}).then(done);
479492
});
493+
494+
it('errors before even generating a promise if gd is not defined', function() {
495+
expect(function() { Plotly.Plots.resize(); })
496+
.toThrow(new Error('DOM element provided is null or undefined'));
497+
});
480498
});
481499

482500
});

test/jasmine/tests/plots_test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var d3 = require('d3');
66
var createGraphDiv = require('../assets/create_graph_div');
77
var destroyGraphDiv = require('../assets/destroy_graph_div');
88
var supplyAllDefaults = require('../assets/supply_defaults');
9+
var failTest = require('../assets/fail_test');
910

1011

1112
describe('Test Plots', function() {
@@ -369,7 +370,7 @@ describe('Test Plots', function() {
369370
.then(done);
370371
});
371372

372-
afterEach(destroyGraphDiv);
373+
afterAll(destroyGraphDiv);
373374

374375
it('should resize the plot clip', function() {
375376
var uid = gd._fullLayout._uid;
@@ -385,6 +386,7 @@ describe('Test Plots', function() {
385386

386387
it('should resize the main svgs', function() {
387388
var mainSvgs = document.getElementsByClassName('main-svg');
389+
expect(mainSvgs.length).toBe(2);
388390

389391
for(var i = 0; i < mainSvgs.length; i++) {
390392
var svg = mainSvgs[i],
@@ -397,6 +399,9 @@ describe('Test Plots', function() {
397399
});
398400

399401
it('should update the axis scales', function() {
402+
var mainSvgs = document.getElementsByClassName('main-svg');
403+
expect(mainSvgs.length).toBe(2);
404+
400405
var fullLayout = gd._fullLayout,
401406
plotinfo = fullLayout._plots.xy;
402407

@@ -406,6 +411,18 @@ describe('Test Plots', function() {
406411
expect(plotinfo.xaxis._length).toEqual(240);
407412
expect(plotinfo.yaxis._length).toEqual(220);
408413
});
414+
415+
it('should allow resizing by plot ID', function(done) {
416+
var mainSvgs = document.getElementsByClassName('main-svg');
417+
expect(mainSvgs.length).toBe(2);
418+
419+
expect(typeof gd.id).toBe('string');
420+
expect(gd.id).toBeTruthy();
421+
422+
Plotly.Plots.resize(gd.id)
423+
.catch(failTest)
424+
.then(done);
425+
});
409426
});
410427

411428
describe('Plots.purge', function() {

0 commit comments

Comments
 (0)