Highcharts(rCharts)onclick工具提示

时间:2016-06-10 10:29:06

标签: r highcharts rcharts

我正在尝试重现这个jsfiddle,只有在点击该点时才会出现工具提示:

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

这是我对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

我很确定问题出在JS部分,rCharts不理解。有任何想法吗?救命?

谢谢,

卡洛斯

1 个答案:

答案 0 :(得分:1)

您的代码中几乎没有错误:

  1. a$chart(list(events =应与a$chart(events =

  2. 中的a$xAxis(categories =结构相同
  3. a$plotOptions(events =需要a$plotOptions(series = list(events =或者您可以使用其他系列类型名称而不是系列,但系列适用于所有系列类型。

  4. 这三个函数必须使用{}作为每个函数的正文。

  5. 工作代码:

    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