From e07f80c5e576db2bd6295e0505af78022f8da737 Mon Sep 17 00:00:00 2001 From: Carson Date: Wed, 28 Aug 2019 14:54:43 -0500 Subject: [PATCH 1/2] make a deep copy of x.layout.width/x.layout.height for use in the resize method, closes #1553 --- inst/htmlwidgets/plotly.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/inst/htmlwidgets/plotly.js b/inst/htmlwidgets/plotly.js index 7deca6f8d1..6bff021dbd 100644 --- a/inst/htmlwidgets/plotly.js +++ b/inst/htmlwidgets/plotly.js @@ -17,6 +17,12 @@ HTMLWidgets.widget({ renderValue: function(el, x, instance) { + // Make a deep copy of user data that we need for the resize method + // (Plotly.relayout() mutates the plot input object -- https://codepen.io/cpsievert/pen/WNeOrjj) + instance.width = JSON.parse(JSON.stringify(x.layout.width || null)); + instance.height = JSON.parse(JSON.stringify(x.layout.height || null)); + instance.autosize = JSON.parse(JSON.stringify(x.layout.autosize || true)); + /* / 'inform the world' about highlighting options this is so other / crosstalk libraries have a chance to respond to special settings @@ -153,17 +159,9 @@ HTMLWidgets.widget({ var plot = Plotly.plot(graphDiv, x); instance.plotly = true; - instance.autosize = x.layout.autosize || true; - instance.width = x.layout.width; - instance.height = x.layout.height; } else { - // new x data could contain a new height/width... - // attach to instance so that resize logic knows about the new size - instance.width = x.layout.width || instance.width; - instance.height = x.layout.height || instance.height; - // this is essentially equivalent to Plotly.newPlot(), but avoids creating // a new webgl context // https://github.com/plotly/plotly.js/blob/2b24f9def901831e61282076cf3f835598d56f0e/src/plot_api/plot_api.js#L531-L532 From a0cd2f2cc32e9ed22a8801b23540e7014fbe6fe3 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 30 Aug 2019 14:52:06 -0500 Subject: [PATCH 2/2] no need to be making deep copies --- inst/htmlwidgets/plotly.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/inst/htmlwidgets/plotly.js b/inst/htmlwidgets/plotly.js index 6bff021dbd..e74f32b405 100644 --- a/inst/htmlwidgets/plotly.js +++ b/inst/htmlwidgets/plotly.js @@ -17,11 +17,13 @@ HTMLWidgets.widget({ renderValue: function(el, x, instance) { - // Make a deep copy of user data that we need for the resize method - // (Plotly.relayout() mutates the plot input object -- https://codepen.io/cpsievert/pen/WNeOrjj) - instance.width = JSON.parse(JSON.stringify(x.layout.width || null)); - instance.height = JSON.parse(JSON.stringify(x.layout.height || null)); - instance.autosize = JSON.parse(JSON.stringify(x.layout.autosize || true)); + // Plotly.relayout() mutates the plot input object, so make sure to + // keep a reference to the user-supplied width/height *before* + // we call Plotly.plot(); + var lay = x.layout || {}; + instance.width = lay.width; + instance.height = lay.height; + instance.autosize = lay.autosize || true; /* / 'inform the world' about highlighting options this is so other