Skip to content

Commit b1589d1

Browse files
committed
better autosizing rules
1 parent c839734 commit b1589d1

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

R/plotly.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
139139
# we always deal with a _list_ of traces and _list_ of layouts
140140
# since they can each have different data
141141
layout = list(
142-
width = width,
143-
height = height,
144-
# sane margin defaults (mainly for RStudio)
145-
margin = list(b = 40, l = 60, t = 25, r = 10)
142+
width = width,
143+
height = height,
144+
# sane margin defaults (mainly for RStudio)
145+
margin = list(b = 40, l = 60, t = 25, r = 10)
146146
),
147147
source = source
148148
)
@@ -228,7 +228,7 @@ plot_geo <- function(data = data.frame(), ...) {
228228
#'
229229

230230
as_widget <- function(x, ...) {
231-
if (inherits(x, "htmlwidget")) return(x)
231+
if (inherits(x, "htmlwidget")) x <- x[["x"]]
232232
# add plotly class mainly for printing method
233233
# customize the JSON serializer (for htmlwidgets)
234234
attr(x, 'TOJSON_FUNC') <- to_JSON

R/plotly_build.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ plotly_build.plotly <- function(p) {
327327
p <- verify_webgl(p)
328328
# make sure plots don't get sent out of the network (for enterprise)
329329
p$x$base_url <- get_domain()
330-
p
330+
331+
as_widget(p)
331332
}
332333

333334
# ----------------------------------------------------------------

inst/htmlwidgets/plotly.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ HTMLWidgets.widget({
1414
},
1515

1616
resize: function(el, width, height, instance) {
17-
if (instance.autosize) {
18-
Plotly.relayout(el.id, {width: width, height: height});
19-
}
17+
var width = instance.width || width;
18+
var height = instance.height || height;
19+
Plotly.relayout(el.id, {width: width, height: height});
2020
},
2121

2222
renderValue: function(el, x, instance) {
@@ -30,14 +30,16 @@ HTMLWidgets.widget({
3030
}
3131

3232
var graphDiv = document.getElementById(el.id);
33+
var layout = x.layout || {};
3334

3435
// if no plot exists yet, create one with a particular configuration
3536
if (!instance.plotly) {
36-
var plot = Plotly.plot(graphDiv, x.data, x.layout, x.config);
37+
var plot = Plotly.plot(graphDiv, x.data, layout, x.config);
3738
instance.plotly = true;
38-
instance.autosize = x.layout.autosize;
39+
instance.height = layout.height;
40+
instance.width = layout.width;
3941
} else {
40-
var plot = Plotly.newPlot(graphDiv, x.data, x.layout);
42+
var plot = Plotly.newPlot(graphDiv, x.data, layout);
4143
}
4244

4345
sendEventData = function(eventType) {

0 commit comments

Comments
 (0)