日期x轴上的Highcharter注释不起作用 - R.

时间:2017-12-22 02:21:06

标签: javascript r highcharts r-highcharter

我正在尝试为我的高图图表添加注释,不确定它为什么不起作用,而是显示[object] [object]。

这是我的数据,

structure(list(variable = structure(c(15522, 15553, 15584, 15614, 
15645, 15675, 15706, 15737, 15765, 15796), class = "Date"), value = c(417376, 
423563, 430290, 455643, 451542, 422419, 429472, 451694, 454900, 
456844)), row.names = c(NA, 10L), .Names = c("variable", "value"
), class = "data.frame")

代码,我试图仅为图表中的最后一个值添加注释,可能在矩形框中。

currmonth <- max(pricedata$variable)
pricedata$value <- round(pricedata$value)
highchart(type = "chart") %>% 
  hc_chart(backgroundColor = "white",zoomType = 'x') %>%
  hc_add_series_times_values(pricedata$variable, pricedata$value, name = "Price") %>%
  hc_annotations(list(xValue = currmonth, title = list(text = 'Annotated chart!')))

这是图表,

enter image description here

您可以看到注释位于左上角,作为[object] [object]。

编辑:尝试了答案,但没有成功。

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

按照示例https://www.highcharts.com/demo/annotations,阅读此greate教程https://dantonnoriega.github.io/ultinomics.org/post/2017-04-05-highcharter-explainer.html即可。

首先,最好使用hchart函数创建图表。

library(tidyverse) # for pull and last
library(highcharter)

currmonth <- max(data$variable)
lastvalue <- data %>% pull(value) %>% last()

hc <- hchart(data, "line", hcaes(variable, value)) 

然后添加复制给定链接的注释:

hc %>% 
  hc_annotations(
      list(
          labelOptions = list(y = 50, x = 0),
          labels = list(
            list(
              point = list(
                x = highcharter::datetime_to_timestamp(currmonth),
                y = lastvalue,
                xAxis = 0,
                yAxis = 0
              ),
              text = scales::dollar(lastvalue)
              )
            )
          )
      )

enter image description here

可悲的是,highchartsJS需要很多嵌套列表,所以我们需要做的就是在highcharter中添加注释(到现在为止)