我们正在尝试使用Highcharts将大约4000个数据点从a1列绘制为条形图。 a1条的颜色基于另一列名为a3的值。如果某一行的a3为负,则a1对应的条应该是红色的,而正数a3应该是绿色的。
问题在于,生成图表需要大约25秒的时间,而仅打印图表需要20秒钟。请有人帮助我们修复代码并使其更快。我们尝试禁用动画和阴影,但效果不太明显。以下是代码:
问题在于,生成图表需要大约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)
}
非常感谢您的帮助!
dataGrouping
绘制大量点(没有限制,但大约 100,000 个应该很平滑)。或者使用异步数据加载来加载更多点,就像在 170 万个点的示例中一样。 - Paweł Fus