【问题标题】:Highcharter plotBands, plotLines with time series dataHighcharter plotBands,带有时间序列数据的 plotLines
【发布时间】:2017-05-04 16:16:58
【问题描述】:

在绘制时间序列时,在 highcharter 库的 plotLinesplotBands 中指定值的正确方法是什么?使用以下代码,绘图线出现在图表的左端,而波段根本不出现。如果我不指定type = 'stock',即使情节线也不会出现。这似乎是时间序列数据的问题,只有其他类型的数据才能正常工作。所以我相信我可能没有以正确的格式指定值。除了代码中的那个之外,我还尝试了时间序列格式,例如from = c(1990,1) 但它也不起作用。

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')
                     )
                   )
               )

这是结果图表

【问题讨论】:

    标签: r highcharts


    【解决方案1】:

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

    这是,来自:

     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
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-20
      • 2016-04-05
      • 2018-11-02
      • 1970-01-01
      • 2013-06-09
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多