jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
718 stars 147 forks source link

hc_theme_sparkline_vb affectting theme options of other charts during export #751

Closed PaulC91 closed 1 year ago

PaulC91 commented 2 years ago

Hi Joshua!

I've been using your super nice hc sparklines inside shinydashboard value boxes as demonstrated in your blogpost: https://jkunst.com/blog/posts/2020-06-26-valuebox-and-sparklines/

But I've come across a strange bug when adding export options to other regular highcharts on the dashboard. When exporting the regular chart, some theme elements of hc_theme_sparkline_vb() seem to be applied to the exported chart resulting in white coloured bars (bar chart in my case with a white background so bars are no longer visible). Chart margins also look to be reduced to 0 like in the sparkline charts.

I've tried various methods of trying to override the sparkline themes that are being applied to the exported chart, like setting colors via hc_exporting(chartOptions = list(colors = "#000000")) for example but nothing is working and data geoms on the chart always appear white.

I've added a full reprex below with a value sparkline chart and regular chart inside a shinydashboard with the problem occurring when exporting the regular chart. When i remove the value sparkline from the dashboard the export works fine.

Any advice on how to resolve this would be appreciated!

library(shiny)
library(shinydashboard)
library(highcharter)

hc_test <- highchart() %>%
  hc_xAxis(categories = month.abb) %>%
  hc_add_series(name = "Tokyo", data = sample(1:12), type = "column", showInLegend = FALSE)

valueBoxSpark <- function(value, title, sparkobj = NULL, subtitle, info = NULL, 
                          icon = NULL, color = "aqua", width = 3, href = NULL){

  shinydashboard:::validateColor(color)

  if (!is.null(icon))
    shinydashboard:::tagAssert(icon, type = "i")

  info_icon <- tags$small(
    tags$i(
      class = "fa fa-info-circle fa-lg",
      title = info,
      `data-toggle` = "tooltip",
      style = "color: rgba(255, 255, 255, 0.75);"
    ),
    # bs3 pull-right 
    # bs4 float-right
    class = "pull-right float-right"
  )

  boxContent <- div(
    class = paste0("small-box bg-", color),
    div(
      class = "inner",
      tags$small(title),
      if (!is.null(sparkobj)) info_icon,
      h3(value),
      if (!is.null(sparkobj)) sparkobj
      # p(subtitle)
    ),
    # bs3 icon-large
    # bs4 icon
    if (!is.null(icon)) div(class = "icon-large icon", icon, style = "z-index; 0")
  )

  if (!is.null(href)) 
    boxContent <- a(href = href, boxContent)

  div(
    class = if (!is.null(width)) paste0("col-sm-", width), 
    boxContent
  )
}

ui <- dashboardPage(
  header = dashboardHeader(),
  sidebar = dashboardSidebar(),
  body = dashboardBody(
    column(6, valueBoxOutput("vb", width = 12)),
    column(6, highchartOutput("chart"))
  )
)

server <- function(input, output, session) {
  output$vb <- renderValueBox({
    hc <- hc_test %>%
      hc_size(height = 100) %>%
      hc_credits(enabled = FALSE) %>%
      hc_exporting(enabled = FALSE) %>% 
      hc_add_theme(hc_theme_sparkline_vb())

    valueBoxSpark(
      value = 100,
      title = "TEST",
      sparkobj = hc,
      info = "test info",
      width = 12,
      color = "red",
      href = NULL
    )
  })

  output$chart <- renderHighchart({
    hc_test %>%
      hc_exporting(enabled = TRUE) %>% 
      hc_add_theme(hc_theme_smpl())
  })
}

shinyApp(ui, server)
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Feel free to reopen it if you find it necessary.