Highcharts - 深入研究R中的多个系列

时间:2018-05-09 07:08:02

标签: r highcharts

我知道这已经在JS中得到了回答,但我希望在R中使用highcharter解决方案。Highcharts - drill down to multiple series

我是JS的新手,也不熟悉R中的highcharter库,所以任何帮助都会非常感激。下面的代码编译但由于代码比任何代码都更具实验性,因此它不允许我按照希望深入到多系列图表。

DATABrowser = list(list(y= 55.11
                    ,drilldown = list(
                      name = 'MSIE versions',
                      categories = list('MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 
'MSIE 9.0'),
                      series = list(list(
                        type = 'spline',
                        name = 'MSIE versions 2000',
                        data = list(10.85, 7.35, 33.06, 2.81)

                      ),list(
                        type = 'spline',
                        name = 'MSIE versions 2010',
                        data  = list (1, 5, 10, 15)
                      ))

                    )),list(y = 21.6),list(y = 11.6),list(y = 7.3),list(y = 
2.6)

)

categories = list('MSIE', 'Firefox', 'Chrome', 'Safari', 'Opera')
name = 'Browser brands'



fn <-"function () {

var drilldown = this.drilldown;
var len = chart.series.length;
var name = null, catergories = drilldown.categories, data = drilldown, type 
=drilldown.type;
chart.xAxis[0].setCategories(categories);
for(var i = 0; i < len; i++){
chart.series[0].remove();
}

if(data.series){
for( i = 0; i < data.series.length; i ++ ){
chart.addSeries({
name: data.series[i].name,
data: data.series[i].data,
type: data.series[i].type,
});
}
} else {
chart.addSeries({
name: name,
data: data,
type: type,

});
}
} 
"

hc = highchart() %>%
hc_chart(type = "column") %>%

hc_title(text = "Basic Drilldown Big Bossing") %>%
hc_xAxis(categories = categories) %>%
hc_add_series(
name = name
,data = DATABrowser
) %>%   hc_plotOptions(
column = list(
  # allowPointSelect = TRUE,
  cursor = "pointer",
  point = list(
    events = list(
         click = JS(fn)
    )
  )
)
)
hc

1 个答案:

答案 0 :(得分:1)

JS函数返回错误

  

图表未定义

事实上,我们无法访问click事件中的图表,但我们可以通过以下方式对其进行检索:

var chart = Highcharts.charts[0]

所以把它放在JS中,再加上一些拼写错误的解决方案给了我们:

fn <-"function () {
  var chart = Highcharts.charts[0];
  var drilldown = this.drilldown;
  var len = chart.series.length;
  var name = null, 
      categories = drilldown.categories, 
      data = drilldown, 
      type = drilldown.type;
  chart.xAxis[0].setCategories(categories);
  for(var i = 0; i < len; i++){
    chart.series[0].remove();
  }

  if(data.series){
    for( i = 0; i < data.series.length; i ++ ){
      chart.addSeries({
        name: data.series[i].name,
        data: data.series[i].data,
        type: data.series[i].type,
        });
    }
  } else {
    chart.addSeries({
    name: name,
    data: data,
    type: type,

    });
  }
} 
"

给我们:

library(highcharter)

highchart() %>%
  hc_chart(type = "column") %>%

  hc_title(text = "Basic Drilldown Big Bossing") %>%
  hc_xAxis(categories = categories) %>%
  hc_add_series(
    name = name
    ,data = DATABrowser
  ) %>%   hc_plotOptions(
    column = list(
      # allowPointSelect = TRUE,
      cursor = "pointer",
      point = list(
        events = list(
          click = JS(fn)
        )
      )
    )
  )

First screen Second screen