【问题标题】:Highcharter annotation on date x-axis not working - R日期x轴上的Highcharter注释不起作用 - R
【发布时间】:2018-06-04 17:32:41
【问题描述】:

我正在尝试为我的 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!')))

这是图表,

你可以看到注解在左上角为[object][object]。

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

【问题讨论】:

  • 我在 Highcharts JS API 中看到 annotations 对象应该包含 labels 数组 (api.highcharts.com/highcharts/annotations.labels.text)。我在您的代码中的任何地方都没有看到它。所以可能是配置对象构造不当的问题。
  • 我在这里使用 R 中的 hc_annotations。

标签: javascript r highcharts r-highcharter


【解决方案1】:

按照示例https://www.highcharts.com/demo/annotations 并阅读此伟大教程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)
              )
            )
          )
      )

遗憾的是 highchartsJS 需要很多嵌套列表,所以我们需要在 highcharter 中添加注释(现在)

【讨论】:

  • 没用,更新了问题截图,请查收。谢谢。
  • 但请将此注释功能作为 highcharter 的一部分简化。这让生活更轻松。 :) 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多