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

时间:2017-09-02 22:34:46

标签: r highcharts tooltip

我正在绘制一个简单的Highchart Area图,我想根据底层系列的某些值有条件地显示/隐藏Tooltip(在下面的例子中,它基于z的值)

以下是我的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的值显示工具提示时,否则不显示。但是上面的代码失败了,因为它根本没有显示工具提示。

有关如何实现上述条件显示工具提示的想法吗?

感谢任何指针。

1 个答案:

答案 0 :(得分: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')))))