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

Ziq*_* Lu 6 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)
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

jbk*_*nst 0

首先,请显示您在代码中使用的软件包(dplyr、scales)。

有一个用于高图表的增强模块。遗憾的是,rCharts 默认情况下不包含该模块,因此您需要手动添加。

另一方面。Highcharts 有一个新的包装器,称为 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)
Run Code Online (Sandbox Code Playgroud)

检查速度/此处:

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