绘制4000条时,Highcharts太慢(rCharts)

时间:2015-06-17 14:45:30

标签: r highcharts rcharts

我们正在尝试使用Highcharts将来自a1列的大约4000个数据点绘制为条形图。 a1条的颜色基于另一列a3的值。如果a3在一行上是负数,则关于a1的相应条应为红色,而正a3应为绿色。

问题是生成图表大约需要25秒,打印图表需要20秒。有人可以帮我们修复代码并加快速度吗?我们试图禁用动画和阴影,但这并没有太大帮助。这是代码:

fun <- function(){

      ## Generate a random data set with roughly 4,000 lines
      df <- as.data.frame(cbind(x = seq(1:3900),
                                a1 = rnorm(3900, 1000000, 2000000),
                                a2 = abs(rnorm(3900, 1000000, 2000000)),
                                a3 = rnorm(3900, 20000, 30000),
                                a4 = rnorm(3900, 1000, 500),
                                a5 = rnorm(3900, 0.01, 0.02)))

      ## Modify the data set to assign colors to each bar based on the values
      ## of a3. Green bars signify positive a3's and red bars signify
      ## negative a3's
      df <- df %>% 
            mutate(a6 = cumsum(a3)) %>%
            mutate(color = ifelse(a3 > 0, 
                                  "rgba(50,205,50,0.6)", 
                                  "rgba(223,83,83,0.6)")) %>%
            mutate(y = a1,
                   a1 = comma_format()(round(a1, 0)),
                   a3 = comma_format()(round(a3, 0)),
                   a4 = comma_format()(round(a4, 4)),
                   a5 = comma_format()(round(a5, 0)),
                   a6 = comma_format()(round(a6, 0))
            )

      ## Store the data in a list so that it is readable by Highcharts
      input <- list()
      input <- lapply(unname(split(df, seq(nrow(df)))), as.list)

      ## Draw the graph with Highcharts
      a <- rCharts::Highcharts$new()
      a$series(data = input, 
               name = "a1 values", 
               type = "column")
      a$plotOptions(series = list(turboThreshold = 4000))
      a$chart(zoomType = "xy", animation = FALSE)
      a$addParams(width = 1000, height = 400, title = list(text = "The Slow Chart"))
      a$tooltip(formatter = "#! function() 
            {return 'Date:<b> ' + this.point.x + 
                        '</b> <br/>a1 values:<b> ' + this.point.a1 +
                '</b> <br/>a3 values:<b> ' + this.point.a2 +  
                '</b> <br/>a4 values:<b> ' + this.point.a3 + 
                '</b> <br/>a5 values:<b> ' + this.point.a5 + 
                '</b> <br/>a6 values:<b> ' + this.point.a6} !#")
      print(a)
}

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

首先,请显示您在代码中使用的包(dplyr,scale)。

对于highcharts有一个boost module。遗憾的是,rCharts默认不包含该模块,因此您需要手动添加。

另一方面。有一个名为highcharter的高级图表的新包装器,它具有此模块的实现。使用它不需要超过1秒钟来绘制3900列。

highchart2() %>% 
  hc_title(text = "Not so slow chart ;)") %>% 
  hc_subtitle(text = "Thanks boost module") %>% 
  hc_chart(zoomType = "x", animation = FALSE, type = "column") %>% 
  hc_plotOptions(series = list(turboThreshold = 4000)) %>% 
  hc_add_serie(data = input)

检查速度/此处:

http://rpubs.com/jbkunst/highcharts-too-slow-when-plotting-4000-bars-rcharts