dreamRs / apexcharter

:bar_chart: R Htmlwidget for ApexCharts.js
https://dreamrs.github.io/apexcharter
Other
138 stars 15 forks source link

updating format of axis in shiny #43

Closed bklingen closed 3 years ago

bklingen commented 3 years ago

Updating the maximum of the x-axis works perfectly fine in shiny (press the "Update Max" button), but updating the format of the x-axis label labels does not seem to work. Pressing the "Update Format" button does not redraw the graph, in fact, the graph disappears.

library(shiny)
library(apexcharter)

x <- seq(-3,3,length.out=100)
df <- data.frame(x, y=dnorm(x))

ui <- fluidPage(
  actionButton("max", "Update Max"),
  actionButton("format", "Update Format"),
  apexchartOutput("plot")
)

server <- function(session, input, output) {

  output$plot <- renderApexchart({    
    apex(data=df, type='spline', mapping=aes(x=x, y=y)) %>%
      ax_xaxis(type='numeric',
        labels = list(formatter = format_num(".1f"))
      )
  })

  observeEvent(input$max, {
    apexchartProxy("plot") %>% 
      ax_proxy_options(
        list(
          xaxis = list(max = 4)
        )
      )
  })

  observeEvent(input$format, {
    apexchartProxy("plot") %>% 
      ax_proxy_options(
        list(
          xaxis = list(labels = 
            list(formatter = format_num(".2f"))
          )
        )
      )
  })

}

shinyApp(ui = ui, server = server)
pvictor commented 3 years ago

That's a bug, format_num return a JavaScript function as character and proxy doesn't know that it need to be evaluated.

It's fixed if you re-install from GitHub.

Victor

bklingen commented 3 years ago

Great, thanks, it works perfectly now! Just stating that

formatter = htmlwidgets::JS("function(value) {return value.toFixed(2);}")

works as well to update the format.