Search code examples
rhighchartstooltip

Conditionally show/hide Tooltip of Highcharter


I am drawing a simple Highchart Area plot, where I want to show/hide the Tooltip conditionally, based on some value of underlying series (in below case, it is based on the value of z)

Below is my R code :

library(highcharter)
highchart() %>%  
hc_chart(type = "area", plotBorderWidth = 0.5, plotBorderColor = '#4572A7') %>%    # https://gist.github.com/mulhoon/63b5d5a98ef0ab8c2b89
hc_xAxis(categories = as.character(c(1.00, 2.00, 3.00)), lineWidth = 1, gridLineWidth = 0, minorGridLineWidth = 0) %>%
hc_xAxis(lineWidth = 1, gridLineWidth = 0, minorGridLineWidth = 0, labels = list(format = '{value}%')) %>%  # https://stackoverflow.com/questions/17246187/displaying-percentage-in-y-axis-of-highcharts-column-chart
hc_add_series(name = 'foo', data = list(list(y = 3, z = 1), list(y = 4, z = 0), list(y = 5, z = 1))) %>%
hc_tooltip(formatter = "function(){

                if (this.point.z == 1) {
                    return 'ON';
                }
            }") %>%
hc_plotOptions(series = list(marker = list(enabled = 'false', radius = 1, states = list(hover = list(enabled = 'false', radius = .1, color = '#4572A7')))))

Basically, what I want is : When the value of z = 1 then show the Tooltip, otherwise do not show. However above code is failing since it is not showing the Tooltip at all.

Any idea on how to implement above conditional show of Tooltip?

Thanks for any pointer.


Solution

  • I altered the code after the formatter argument to suit your needs.

    library(highcharter)
    highchart() %>%  
      hc_chart(type = "area", plotBorderWidth = 0.5, plotBorderColor = '#4572A7') %>%    # https://gist.github.com/mulhoon/63b5d5a98ef0ab8c2b89
      hc_xAxis(categories = as.character(c(1.00, 2.00, 3.00)), lineWidth = 1, gridLineWidth = 0, minorGridLineWidth = 0) %>%
      hc_xAxis(lineWidth = 1, gridLineWidth = 0, minorGridLineWidth = 0, labels = list(format = '{value}%')) %>%  # https://stackoverflow.com/questions/17246187/displaying-percentage-in-y-axis-of-highcharts-column-chart
      hc_add_series(name = 'foo', data = list(list(y = 3, z = 1), list(y = 4, z = 0), list(y = 5, z = 1))) %>%
      hc_tooltip(formatter = JS("function(){
    
                 if (this.point.z == 1) {
                 return 'ON';
                 } else { 
                  return false;
                 }
                 }")) %>%
    hc_plotOptions(series = list(marker = list(enabled = 'false', radius = 1, states = list(hover = list(enabled = 'false', radius = .1, color = '#4572A7')))))