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 1 commit
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
21 changes: 21 additions & 0 deletions src/lib/clear_responsive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

/**
* Clear responsive handlers (if any).
*
* @param {DOM node or object} gd : graph div object
*/
module.exports = function clearResponsive(gd) {
if(gd._responsiveChartHandler) {
window.removeEventListener('resize', gd._responsiveChartHandler);
delete gd._responsiveChartHandler;
}
};
2 changes: 2 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ lib.clearThrottle = throttleModule.clear;

lib.getGraphDiv = require('./get_graph_div');

lib.clearResponsive = require('./clear_responsive');

lib.makeTraceGroups = require('./make_trace_groups');

lib._ = require('./localize');
Expand Down
26 changes: 12 additions & 14 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ exports.plot = function(gd, data, layout, config) {
gd.calcdata[i][0].trace = gd._fullData[i];
}

// 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);
}

/*
* start async-friendly code - now we're actually drawing things
*/
Expand Down Expand Up @@ -385,15 +394,6 @@ 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);
}
}
}

Expand Down Expand Up @@ -2243,6 +2243,9 @@ exports.react = function(gd, data, layout, config) {
gd._context = undefined;
setPlotContext(gd, config);
configChanged = diffConfig(oldConfig, gd._context);

// remove responsive handler
Lib.clearResponsive(gd);
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm actually there's another way to turn off config.responsive on a div, by calling newPlot again. I think the removal belongs in Plotly.plot instead, next to where you add the handler:

if(gd._context.responsive) {
    if(!gd. _responsiveChartHandler) { ...attach }
}
else {
    Lib.clearResponsive(gd);
}

Then it would handle both cases.

}

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

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

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

Expand Down
3 changes: 3 additions & 0 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,9 @@ plots.purge = function(gd) {
// remove any planned throttles
Lib.clearThrottle();

// remove responsive handler
Lib.clearResponsive(gd);

// data and layout
delete gd.data;
delete gd.layout;
Expand Down
76 changes: 69 additions & 7 deletions test/jasmine/tests/config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,12 @@ describe('config argument', function() {

describe('responsive figure', function() {
var gd;
var startWidth = 960, startHeight = 400;
var newWidth = 400, newHeight = 700;
var data = [{x: [1, 2, 3, 4], y: [5, 10, 2, 8]}];

beforeEach(function() {
viewport.reset();
viewport.set(startWidth, startHeight);
gd = createGraphDiv();

// Make the graph fill the parent
Expand All @@ -546,10 +548,27 @@ describe('config argument', function() {

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

function testResponsive(promise) {
return promise
.then(function() {
checkLayoutSize(startWidth, startHeight);
})
// Resize viewport
.then(function() {
viewport.set(newWidth, newHeight);
})
// Wait for resize to happen (Plotly.resize has a 100ms timeout)
.then(delay(200))
// Check final figure size
.then(function() {
checkLayoutSize(newWidth, newHeight);
})
.catch(failTest);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good idea to pull this function out! Nice and 🌴. You can probably simplify it a bit though:

function testResponsive() {
    checkLayoutSize(startWidth, startHeight);
    viewport.set(newWidth, newHeight);

    return delay(200)()
    .then(function() {
        checkLayoutSize(newWidth, newHeight);
    })
    .catch(failTest);
}

And use it like:

Plotly.plot(gd, data, {}, {responsive: true})
.then(testResponsive)
.then(done);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call :) Thank you again


function checkLayoutSize(width, height) {
expect(gd._fullLayout.width).toBe(width);
expect(gd._fullLayout.height).toBe(height);
Expand All @@ -560,17 +579,60 @@ describe('config argument', function() {
}

it('should resize when the viewport width/height changes', function(done) {
var newWidth = 400, newHeight = 700;
Plotly.newPlot(gd, data, {}, {responsive: true})
var promise = Plotly.plot(gd, data, {}, {responsive: true});

testResponsive(promise)
.then(done);
});

it('should still be responsive if the plot is redrawn', function(done) {
var promise = Plotly.plot(gd, data, {}, {responsive: true})
.then(function() {
Plotly.restyle(gd, 'y[0]', data[0].y[0] * 2);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You need to return these Plotly.* calls inside .then - as with any function that itself returns a promise - so the new promise gets added to the chain.

});

testResponsive(promise)
.then(done);
});

it('should still be responsive if the plot is purged and replotted', function(done) {
var promise = Plotly.plot(gd, data, {}, {responsive: true})
.then(function() {
Plotly.newPlot(gd, data, {}, {responsive: true});
});

testResponsive(promise)
.then(done);
});

it('should become responsive if configured to be so by Plotly.react', function(done) {
var promise = Plotly.plot(gd, data, {}, {responsive: false})
.then(function() {
Plotly.react(gd, data, {}, {responsive: true});
});

testResponsive(promise)
.then(done);
});

it('should stop being responsive if configured to be so by Plotly.react', function(done) {
Plotly.plot(gd, data, {}, {responsive: true})
// Check initial size
.then(function() {
checkLayoutSize(startWidth, startHeight);
})
// Turn off responsiveness
.then(function() {
Plotly.react(gd, data, {}, {responsive: false});
})
// Resize viewport
.then(function() {
viewport.set(newWidth, newHeight);
})
// Wait for resize to happen (Plotly.resize has a 100ms timeout)
.then(delay(200))
// Check final figure size
// Check final figure size hasn't changed
.then(function() {
checkLayoutSize(newWidth, newHeight);
checkLayoutSize(startWidth, startHeight);
})
.catch(failTest)
.then(done);
Expand Down