dreamRs / apexcharter

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

Brush does not work #55

Closed meersel closed 2 years ago

meersel commented 2 years ago

Hi,

I want to use the brush option in Apexchart. However the brush char is not shown.

See example code below.

_library(shiny) library(apexcharter) data("economics", package = "ggplot2")

ui <- fluidPage( fluidRow( column( width = 8, offset = 2, tags$h2("Apexchart brush example in Shiny", class = "text-center"), apexchartOutput("brush_1"), apexchartOutput("brush_2", height = "130px") ) ) )

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

output$brush_1 <- renderApexchart({ apex( data = economics, mapping = aes(x = date, y = psavert), type = "line" ) %>% ax_chart( id = "target-chart", toolbar = list( autoSelected = "pan", show = FALSE ) ) })

output$brush_2 <- renderApexchart({ apex( data = economics, mapping = aes(x = date, y = psavert), type = "line" ) %>% ax_chart( brush = list( target = "target-chart", # <-- use target id here enabled = TRUE ), offsetY = -20, selection = list( enabled = TRUE, # <-- enable selection and define starting range xaxis = list( min = format_date(economics$date[1]), max = format_date(economics$date[100]) ) ) ) %>% ax_xaxis(labels = list(show = FALSE)) %>% ax_yaxis(labels = list(show = FALSE)) })

}_

Please advice what I need to do ?

Regards,

Miguel

pvictor commented 2 years ago

Hello,

Yes unfortunately this method don't work in Shiny, here's an alternative:

library(shiny)
library(apexcharter)
data("economics", package = "ggplot2")

ui <- fluidPage(
  fluidRow(
    column(
      width = 8, offset = 2,
      tags$h2("Apexchart brush example (alternative) in Shiny", class = "text-center"),
      apexchartOutput("brush_1"),
      apexchartOutput("brush_2", height = "130px")
    )
  )
)

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

  output$brush_1 <- renderApexchart({
    apex(
      data = economics,
      mapping = aes(x = date, y = psavert),
      type = "line"
    ) %>%
      ax_chart(
        toolbar = list(
          autoSelected = "pan",
          show = FALSE
        )
      )
  })

  output$brush_2 <- renderApexchart({
    apex(
      data = economics,
      mapping = aes(x = date, y = psavert),
      type = "line"
    ) %>%
      ax_chart(
        brush = list(
          enabled = TRUE
        ),
        offsetY = -20,
        selection = list(
          enabled = TRUE
        )
      ) %>%
      ax_xaxis(labels = list(show = FALSE)) %>%
      ax_yaxis(labels = list(show = FALSE)) %>% 
      set_input_selection(
        inputId = "brush", 
        xmin = format_date(economics$date[1]),
        xmax = format_date(economics$date[100])
      )
  })

  observeEvent(input$brush, {
    apexchartProxy("brush_1") %>% 
      ax_proxy_options(list(
        xaxis = list(
          min = as.numeric(input$brush$x$min) * 1000,
          max = as.numeric(input$brush$x$max) * 1000
        )
      ))
  })

}

shinyApp(ui, server)

Best,

Victor

meersel commented 2 years ago

Hi Victor,

Thank you for you answer. Thanks for the workaround.

The Brush option also does not work in RMarkdown HTML document. In the past it used to work In RMarkdown documents.

Any workarounds for RMarkdown files ?

pvictor commented 2 years ago

Ah yes, thanks for reporting this. If you re-install from GitHub it should work again.