有条件地显示/隐藏Highcharter的工具提示

问题描述:

我正在绘制一个简单的Highchart Area图,我想根据底层系列的某些值有条件地显示/隐藏工具提示(在以下情况下,它基于z有条件地显示/隐藏Highcharter的工具提示

下面是我的R代码里面:

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://*.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'))))) 

基本上,我想要的是:当z = 1值则显示工具提示,否则不显示。但是上面的代码失败了,因为它根本没有显示工具提示。

有关如何实现上述条件显示Tooltip的任何想法?

感谢任何指针。

我改变了formatter参数后面的代码以满足您的需求。

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://*.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'))))) 
+0

谢谢。这完全符合我的意图:) – Bogaso