Search code examples
r-highcharter

R highcharter - trim row labels but not in tooltip




I want to display a highcharter stacked bar chart where the row labels are trimmed that the first five characters are not shown. However, in the tooltip the full category names should be shown.

In the example above as categories at the xAxis I would like to have only "2012", "2013",.., whereas in the tooltip the whole category names should be displayed. Here is my code

bs.table = data.frame(
  Closing.Date = c("Line 2012", "Year 2013", "Year 2014", "Year 2015", "Year 2016"),
  Non.Current.Assets = c(40.4, 30.3, 20.4, 34.5, 20),
  Current.Assets = c(3.2, 3.3, 2.4, 3.5, 2)
)

hc <- highchart() %>% 
  hc_chart(type = "bar") %>%
  hc_plotOptions(series = list(stacking = "normal")) %>%
  hc_xAxis(categories = bs.table$Closing.Date,
      lineColor = 'transparent',
      tickWidth = 0,
      labels = list(enable = TRUE, 
                     align = 'left',
                     x = 5,
                    style = list(fontSize = '1em',color = '#fff'))) %>%
  hc_add_series(name ="Non Current Assets",
            data = bs.table$Current.Assets,
            stack = "Assets",
            dataLabels = list(enabled = TRUE, 
                              inside = TRUE, 
                              align = "right", 
                              style = list(fontSize = '1em',color = '#fff'))) %>%
  hc_add_series(name = "Current Assets",
            data = bs.table$Non.Current.Assets,
            stack = "Assets",
            dataLabels = list(enabled = TRUE, inside = FALSE, align = "right", 
                              style = list(fontSize = '1em',color = '#fff')) ) %>%
  hc_legend(enabled = FALSE) %>%
  hc_tooltip(shared = TRUE,
          headerFormat = '<b>Statement {point.x}</b><br>',
          pointFormat = '<b>{series.name}:</b> {point.y} <br>',
          footerFormat = '<b>Total:  {point.total} </b>')

Many thanks in advance!


Solution

  • Could you not just change the column name before you create the chart?

    # function to get year    
    substrRight <- function(x, n){
       substr(x, nchar(x)-n+1, nchar(x))
    }
    
    # create year column
    bs.table$year = substrRight(as.character(bs.table$Closing.Date), 4)
    
    # alter x axis to use this column
    hc <- highchart() %>% 
      hc_chart(type = "bar") %>%
      hc_plotOptions(series = list(stacking = "normal")) %>%
      hc_xAxis(categories = bs.table$year,
               lineColor = 'transparent',
               tickWidth = 0,
               labels = list(enable = TRUE, 
                             align = 'left',
                             x = 5,
                             style = list(fontSize = '1em',color = '#fff'))) %>%
      hc_add_series(name ="Non Current Assets",
                    data = bs.table$Current.Assets,
                    stack = "Assets",
                    dataLabels = list(enabled = TRUE, 
                                      inside = TRUE, 
                                      align = "right", 
                                      style = list(fontSize = '1em',color = '#fff'))) %>%
      hc_add_series(name = "Current Assets",
                    data = bs.table$Non.Current.Assets,
                    stack = "Assets",
                    dataLabels = list(enabled = TRUE, inside = FALSE, align = "right", 
                                      style = list(fontSize = '1em',color = '#fff')) ) %>%
      hc_legend(enabled = FALSE) %>%
      hc_tooltip(shared = TRUE,
                 headerFormat = '<b>Statement {point.x}</b><br>',
                 pointFormat = '<b>{series.name}:</b> {point.y} <br>',
                 footerFormat = '<b>Total:  {point.total} </b>')
    

    Edit

    This is a sort of workaround that would nearly give you what you want:

    highchart() %>% 
      hc_chart(type = "bar") %>%
      hc_xAxis(categories = bs.table$year,
               lineColor = 'transparent',
               tickWidth = 0,
               labels = list(enable = TRUE, 
                             align = 'left',
                             x = 5,
                             style = list(fontSize = '1em',color = '#fff'))) %>%
      hc_plotOptions(series = list(stacking = "normal")) %>%
      hc_add_series(name = "Current Assets", bs.table, "column", hcaes(x = year, y = Current.Assets, stuff = Closing.Date),
                    tooltip = list(pointFormat = "<b>{point.stuff}</b><br> <b>{series.name}:</b> {point.y} <br>"),
                    dataLabels = list(enabled = TRUE, 
                                      inside = TRUE, 
                                      align = "right", 
                                      style = list(fontSize = '1em',color = '#fff'))) %>%
      hc_add_series(name ="Non Current Assets", bs.table, "column", hcaes(x = year, y = Non.Current.Assets),
                    tooltip = list(pointFormat = "<b>{point.stuff}</b><br>"),
                    dataLabels = list(enabled = TRUE, inside = FALSE, align = "right",
                                      style = list(fontSize = '1em',color = '#fff')) ) %>% 
      hc_legend(enabled = FALSE) %>%
      hc_tooltip(shared = TRUE,
                 headerFormat = '<b>Statement </b>',
                 footerFormat = '<b>Total:  {point.total} </b>')