-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from 1 commit
cb37565
069b0e2
08d6736
712652b
d82447a
62145e9
ec9ffa1
1262172
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to |
||
}); | ||
|
||
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); | ||
|
There was a problem hiding this comment.
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 adiv
, by callingnewPlot
again. I think the removal belongs inPlotly.plot
instead, next to where you add the handler:Then it would handle both cases.