Skip to content

Commit cb37565

Browse files
committed
add responsive option to config
`responsive: true` will trigger Plotly.resize whenver the window is resized
1 parent 854bbc9 commit cb37565

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"karma-jasmine-spec-tags": "^1.0.1",
143143
"karma-spec-reporter": "0.0.32",
144144
"karma-verbose-reporter": "0.0.6",
145+
"karma-viewport": "^1.0.2",
145146
"madge": "^3.2.0",
146147
"minify-stream": "^1.2.0",
147148
"minimist": "^1.2.0",

src/plot_api/plot_api.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,15 @@ function emitAfterPlot(gd) {
385385
fullLayout._redrawFromAutoMarginCount--;
386386
} else {
387387
gd.emit('plotly_afterplot');
388+
389+
// make the figure responsive
390+
if(gd._context.responsive && !gd._responsiveChartHandler) {
391+
// Keep a reference to the resize handler to purge it down the road
392+
gd._responsiveChartHandler = function() {Plots.resize(gd);};
393+
394+
// Listen to window resize
395+
window.addEventListener('resize', gd._responsiveChartHandler);
396+
}
388397
}
389398
}
390399

@@ -3155,6 +3164,11 @@ exports.purge = function purge(gd) {
31553164
var fullData = gd._fullData || [];
31563165
var calcdata = gd.calcdata || [];
31573166

3167+
// remove responsive handler
3168+
if(gd._responsiveChartHandler) {
3169+
window.removeEventListener('resize', gd._responsiveChartHandler);
3170+
}
3171+
31583172
// remove gl contexts
31593173
Plots.cleanPlot([], {}, fullData, fullLayout, calcdata);
31603174

src/plot_api/plot_config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ module.exports = {
5757
*/
5858
autosizable: false,
5959

60+
// responsive: determines whether to change the layout size when window is resized
61+
responsive: false,
62+
6063
// set the length of the undo/redo queue
6164
queueLength: 0,
6265

test/jasmine/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"env": {
44
"browser": true,
55
"jasmine": true
6+
},
7+
"globals": {
8+
"viewport": true
69
}
710
}

test/jasmine/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func.defaultConfig = {
137137

138138
// frameworks to use
139139
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
140-
frameworks: ['jasmine', 'jasmine-spec-tags', 'browserify'],
140+
frameworks: ['jasmine', 'jasmine-spec-tags', 'browserify', 'viewport'],
141141

142142
// list of files / patterns to load in the browser
143143
//

test/jasmine/tests/config_test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
66
var click = require('../assets/click');
77
var mouseEvent = require('../assets/mouse_event');
88
var failTest = require('../assets/fail_test');
9+
var delay = require('../assets/delay');
910

1011
describe('config argument', function() {
1112

@@ -529,4 +530,50 @@ describe('config argument', function() {
529530
});
530531
});
531532
});
533+
534+
describe('responsive figure', function() {
535+
var gd;
536+
var data = [{x: [1, 2, 3, 4], y: [5, 10, 2, 8]}];
537+
538+
beforeEach(function() {
539+
viewport.reset();
540+
gd = createGraphDiv();
541+
542+
// Make the graph fill the parent
543+
gd.style.width = '100%';
544+
gd.style.height = '100%';
545+
});
546+
547+
afterEach(function() {
548+
destroyGraphDiv();
549+
// Reset window size
550+
viewport.reset();
551+
});
552+
553+
function checkLayoutSize(width, height) {
554+
expect(gd._fullLayout.width).toBe(width);
555+
expect(gd._fullLayout.height).toBe(height);
556+
557+
var svg = document.getElementsByClassName('main-svg')[0];
558+
expect(+svg.getAttribute('width')).toBe(width);
559+
expect(+svg.getAttribute('height')).toBe(height);
560+
}
561+
562+
it('should resize when the viewport width/height changes', function(done) {
563+
var newWidth = 400, newHeight = 700;
564+
Plotly.newPlot(gd, data, {}, {responsive: true})
565+
// Resize viewport
566+
.then(function() {
567+
viewport.set(newWidth, newHeight);
568+
})
569+
// Wait for resize to happen (Plotly.resize has a 100ms timeout)
570+
.then(delay(200))
571+
// Check final figure size
572+
.then(function() {
573+
checkLayoutSize(newWidth, newHeight);
574+
})
575+
.catch(failTest)
576+
.then(done);
577+
});
578+
});
532579
});

0 commit comments

Comments
 (0)