Skip to content

add support for responsive charts #2974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"karma-jasmine-spec-tags": "^1.0.1",
"karma-spec-reporter": "0.0.32",
"karma-verbose-reporter": "0.0.6",
"karma-viewport": "^1.0.2",
"madge": "^3.2.0",
"minify-stream": "^1.2.0",
"minimist": "^1.2.0",
Expand Down
14 changes: 14 additions & 0 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,15 @@ function emitAfterPlot(gd) {
fullLayout._redrawFromAutoMarginCount--;
} else {
gd.emit('plotly_afterplot');

// make the figure responsive
if(gd._context.responsive && !gd._responsiveChartHandler) {
// Keep a reference to the resize handler to purge it down the road
gd._responsiveChartHandler = function() {Plots.resize(gd);};

// Listen to window resize
window.addEventListener('resize', gd._responsiveChartHandler);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks great, but I'm a little surprised to see it so late in the sequence... resize has an internal delay, and calls Plotly.relayout which will wait for any pending drawing on the plot to finish before making its changes. So to avoid missing events that happen halfway through an async draw (mathjax or maps) I'd have thought we should put this in the synchronous block.

Copy link
Contributor Author

@antoinerg antoinerg Sep 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have to be this late. To be honest, I wasn't sure where it'd be best to put it in the sequence. Should I put it at line 193?

for(var i = 0; i < gd.calcdata.length; i++) {
gd.calcdata[i][0].trace = gd._fullData[i];
}
/*
* start async-friendly code - now we're actually drawing things
*/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I put it at line 193?

Seems like a good place!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh one more thing occurs to me: a user could in principle start with responsive and then turn it off using Plotly.react... not sure why you'd want to do this, but it should work!

}
}

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

// remove responsive handler
if(gd._responsiveChartHandler) {
window.removeEventListener('resize', gd._responsiveChartHandler);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to delete gd._responsiveChartHandler so it would reattach if we make a new plot in this div later on. The rest of this happens in Plots.purge (called a few lines down), and the delete and window.removeEventListener need to be in the same place (since there are a couple of places we call Plots.purge outside Plotly.purge, so seems like it would be safer to put both in Plots.purge


// remove gl contexts
Plots.cleanPlot([], {}, fullData, fullLayout, calcdata);

Expand Down
3 changes: 3 additions & 0 deletions src/plot_api/plot_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ module.exports = {
*/
autosizable: false,

// responsive: determines whether to change the layout size when window is resized
responsive: false,

// set the length of the undo/redo queue
queueLength: 0,

Expand Down
3 changes: 3 additions & 0 deletions test/jasmine/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"env": {
"browser": true,
"jasmine": true
},
"globals": {
"viewport": true
}
}
2 changes: 1 addition & 1 deletion test/jasmine/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func.defaultConfig = {

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

// list of files / patterns to load in the browser
//
Expand Down
47 changes: 47 additions & 0 deletions test/jasmine/tests/config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var destroyGraphDiv = require('../assets/destroy_graph_div');
var click = require('../assets/click');
var mouseEvent = require('../assets/mouse_event');
var failTest = require('../assets/fail_test');
var delay = require('../assets/delay');

describe('config argument', function() {

Expand Down Expand Up @@ -529,4 +530,50 @@ describe('config argument', function() {
});
});
});

describe('responsive figure', function() {
var gd;
var data = [{x: [1, 2, 3, 4], y: [5, 10, 2, 8]}];

beforeEach(function() {
viewport.reset();
gd = createGraphDiv();

// Make the graph fill the parent
gd.style.width = '100%';
gd.style.height = '100%';
});

afterEach(function() {
destroyGraphDiv();
// Reset window size
viewport.reset();
});

function checkLayoutSize(width, height) {
expect(gd._fullLayout.width).toBe(width);
expect(gd._fullLayout.height).toBe(height);

var svg = document.getElementsByClassName('main-svg')[0];
expect(+svg.getAttribute('width')).toBe(width);
expect(+svg.getAttribute('height')).toBe(height);
}

it('should resize when the viewport width/height changes', function(done) {
var newWidth = 400, newHeight = 700;
Plotly.newPlot(gd, data, {}, {responsive: true})
// Resize viewport
.then(function() {
viewport.set(newWidth, newHeight);
})
// Wait for resize to happen (Plotly.Plots.resize has a 100ms timeout)
.then(delay(200))
// Check final figure size
.then(function() {
checkLayoutSize(newWidth, newHeight);
})
.catch(failTest)
.then(done);
});
});
});