echarts4r icon indicating copy to clipboard operation
echarts4r copied to clipboard

Width of plot output too small when plot is rendered in the background

Open DavZim opened this issue 3 years ago • 7 comments

When the user clicks on a tab 1 with an echarts4r plot and then moves on to another tab 2 before tab 1 has finished rendering it, the plot on tab 1 will have an incorrect width.

MWE

Given the following app:

  1. Start the app
  2. Click on tab Plot1 then immediately go to tab Plot2
  3. Wait until Plot2 is rendered (so that Plot1 is rendered in the background)
  4. goto Plot1 and you see that the plot does not cover the whole width of the screen until the window is resized.
library(shiny)
library(echarts4r)

ui <- fluidPage(
  tabsetPanel(
    tabPanel("Origin"),
    tabPanel("Plot1", echarts4rOutput("plot1")),
    tabPanel("Plot2", echarts4rOutput("plot2"))
  )  
)

server <- function(input, output, session) {
  output$plot1 <- renderEcharts4r({
    Sys.sleep(1)
    df <- data.frame(
      x = seq(50),
      y = rnorm(50, 10, 3)
    )
    df |> 
      e_charts(x) |> 
      e_line(y)
  })
  
  output$plot2 <- renderEcharts4r({
    Sys.sleep(1)
    df <- data.frame(
      x = seq(20),
      y = rnorm(20, 10, 3)
    )
    df |> 
      e_charts(x) |> 
      e_area(y)
  })
}

shinyApp(ui, server)

This is what I see: image

This is what I expect to see (which is what I see when I wait until plot1 is rendered before moving on or when I resize the window): image

DavZim avatar Nov 29 '22 06:11 DavZim

Using width like echarts4rOutput("plot1", width=999) is not responsive, but helps.

helgasoft avatar Nov 29 '22 18:11 helgasoft

It should be responsive I'm surprised it's not right. Shouldn't tabPanel call the resize event when a tab is toggled? That is implemented in echarts4r

JohnCoene avatar Nov 29 '22 19:11 JohnCoene

You could use resize proxy but really should not have to

JohnCoene avatar Nov 29 '22 19:11 JohnCoene

I tried the above code but used a static ggplot instead and the resizing issue does not appear.

DavZim avatar Nov 30 '22 01:11 DavZim

Any updates on this issue? This behavior happens in Echarts4r Shiny demo as well.

In the Loading tab, click on "Generate" and then switch to a different tab while it renders in the background. When you return, this happens:

image

VIRADUS avatar Feb 22 '24 18:02 VIRADUS

Hello, yeah that's a problem, I'm working on it but I think it has to do with htmlwidgets, ... gonna try to find the issue

munoztd0 avatar Feb 23 '24 09:02 munoztd0

Yes, I don't think it's on echarts4r's end. I suspect it's a combination of shiny and htmlwidgets.

The graph is initialised with the wrong width and the only way this gets redrawn is with if a resize event gets fired and it does not.

I don't fully understand why it gets initialised with the wrong width.

JohnCoene avatar Feb 23 '24 10:02 JohnCoene