2017-09-02 95 views
1

我正在繪製一個簡單的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://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'))))) 

基本上,我想要的是:當z = 1值則顯示工具提示,否則不顯示。但是上面的代碼失敗了,因爲它根本沒有顯示工具提示。

有關如何實現上述條件顯示Tooltip的任何想法?

感謝任何指針。

回答

1

我改變了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://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'))))) 
+0

謝謝。這完全符合我的意圖:) – Bogaso