Search code examples
rhighchartsrcharts

Highcharts (rCharts) onclick tooltip


I'm trying to reproduce this jsfiddle where the tooltip only appears when the point is clicked:

http://jsfiddle.net/2swEQ/2/

This is my "translation" into rCharts:

a <- rCharts::Highcharts$new()

a$xAxis(categories = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

a$tooltip(valueSuffix = " ºC", 
          enabled = F)

a$chart(list(events = list(load = "#! function()
                                    this.myTooltip = new Highcharts.Tooltip(this, this.options.tooltip) !#")))                    

a$plotOptions(events = list(click = "#! function(evt)
                                      this.chart.myTooltip.refresh(evt.point, evt) !#",
                            mouseOut = "#! function() 
                                         this.chart.myTooltip.hide() !#"))

a$series(list(
  list(name = "Tokyo",
       data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 
                25.2, 26.5, 23.3, 18.3, 13.9, 9.6))))

a

I'm pretty sure the problem is with the JS part which is not being understood by rCharts. Any ideas? Help?

Thanks,

Carlos


Solution

  • There are few mistakes in your code:

    1. a$chart(list(events = should be a$chart(events = the same structure as in a$xAxis(categories =

    2. a$plotOptions(events = needs to be a$plotOptions(series = list(events = or you could use another series type name instead of series, but series works for all series types.

    3. All three functions must use { and } for body of each function.

    Working code:

    a <- rCharts::Highcharts$new()
    
    a$xAxis(categories = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    ))
    
    a$tooltip(valueSuffix = " ºC", 
              enabled = F
    )
    
    a$chart(events = list(load = "#! function() { 
              this.myTooltip = new Highcharts.Tooltip(this, this.options.tooltip); 
           } !#"
    ))
    
    a$plotOptions(series = list(events = list(click = "#! function(evt) {
              this.chart.myTooltip.refresh(evt.point, evt);
           } !#",
           mouseOut = "#! function() {
              this.chart.myTooltip.hide();
           } !#"
    )))
    
    a$series(list(list(name = "Tokyo",
                       data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 
                               25.2, 26.5, 23.3, 18.3, 13.9, 9.6)
    )))
    
    a