Highcharter图带,带时间序列数据的图线 [英] Highcharter plotBands, plotLines with time series data

查看:99
本文介绍了Highcharter图带,带时间序列数据的图线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在绘制时间序列时,在highcharter库的plotLinesplotBands中指定值的正确方法是什么?使用以下代码,绘图线显示在图表的左端,而条带根本不显示.如果我不指定type = 'stock',则不会出现绘图线.时间序列数据似乎仅与其他类型的数据一样有效,这似乎是一个问题.因此,我相信我可能没有以正确的格式指定值.除了代码中的一种,我还尝试了时间序列格式,例如from = c(1990,1),但是它也不起作用.

What is the correct way to specify values in highcharter library's plotLines and plotBands when plotting time series? With the following code, plot line appears at the left end of chart and band does not appear at all. If I don't specify type = 'stock' even the plot line does not appear. This seems to be a problem with time series data only as with other type of data it works fine. So I believe I may not be specifying the value in correct format. Besides the one in code, I have tried the time series format e.g. from = c(1990,1) but it did not work either.

library(highcharter)    
data =ts(data = sample(c(50:100),360, replace = TRUE), start = c(1987,1), frequency = 12, names = 'index')

highchart(type = 'stock')%>%
      hc_add_series_ts(data) %>%
      hc_xAxis(type = 'datetime',
               plotLines = list(
                 list(
                   label = list(text = "This is a plotLine"),
                   color = "#FF0000",
                   width = 5,
                   value = as.Date('1990-01-01', tz = 'UTC')
                   )
                 ),
                 plotBands = list(
                   list(
                     label = list(text = "This is a plotBand"),
                     color = "rgba(100, 0, 0, 0.1)",
                     from = as.Date('1995-01-01', tz = 'UTC'),
                     to = as.Date('1996-01-01', tz = 'UTC')
                     )
                   )
               )

这是结果图

推荐答案

所有日期值都需要使用datetime_to_timestamp函数进行转换.

All your date values need to be transformed using datetime_to_timestamp function.

这是来自:

 from = as.Date('1995-01-01', tz = 'UTC'),
 to = as.Date('1996-01-01', tz = 'UTC')

收件人:

 from = datetime_to_timestamp(as.Date('1995-01-01', tz = 'UTC')),
 to = datetime_to_timestamp(as.Date('1996-01-01', tz = 'UTC'))

详细信息:

suppressPackageStartupMessages(library(highcharter))
dt <- as.Date("1995-01-01", tz = "UTC")
dt
#> [1] "1995-01-01"
datetime_to_timestamp(dt)
#> [1] 788918400000

希望这会有所帮助.

这篇关于Highcharter图带,带时间序列数据的图线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆