【问题标题】:annotation in highchart doesn't work as expectedhighchart 中的注释无法按预期工作
【发布时间】:2018-08-06 04:43:51
【问题描述】:

我正在使用 Highchart 绘制一些时间序列,并想在图中添加一些注释以突出显示一些关键点。我知道将光标放在图形上可以弹出上下文,但是,需要一些自动图形生成,因此注释是最好的方法。

我做到了,下面代码的最后一行。然而,效果并不是我所期望的。文本位于左下角,不在右侧水平位置但垂直位置在右侧。时间序列是使用 xts 库创建的,这意味着横轴只是 date 数据结构,没什么花哨的。 xValue 指定为总长度为 1018 的所有时间点的第 900 个元素,因此第 900 个时间点必须在图形的后半部分。

任何人都知道如何将注释放在正确的位置?非常感谢。

hc <- highchart(type = "stock") %>% 
  hc_title(text = "Some time series") %>% 
  hc_add_series(x, color='green', name="x", showInLegend = TRUE) %>%
  hc_add_series(y, color='red', name="y", showInLegend = TRUE) %>% 
  hc_add_series(z, color='blue', name="z", showInLegend = TRUE) %>% 
  hc_navigator(enabled=FALSE) %>% 
  hc_scrollbar(enabled=FALSE) %>%
  hc_legend(enabled=TRUE, layout="horizontal") %>%
  hc_annotations(list(enabledButtons=FALSE, xValue = index(x)[900], yValue = -5, title =list(text = "Hello world! How can I make this work!")))

hc

数据可以大致使用如下脚本生成:

dt <- seq(as.Date("2014/1/30"), as.Date("2018/2/6"), "days")
dt <- dt[!weekdays(dt) %in% c("Saturday", "Sunday")]
n <- length(dt)
x <- xts(rnorm(n), order.by=dt)
y <- xts(rnorm(n), order.by=dt)
z <- xts(rnorm(n), order.by=dt)

【问题讨论】:

  • 请提供一个可重现的最小示例,以帮助其他人帮助您
  • @jbkunst 谢谢,添加了一段代码来生成数据。
  • 我在 Highstock(纯 JS)中准备了一个最小的注释示例,一切似乎都运行良好:jsfiddle.net/BlackLabel/xr50ftw1index(x)[900] 在您的代码中的值是什么?
  • @KamilKulig 我确信 Highchart 库在 JS 中比在 R 中运行得更好。任何线索为什么它在 R 中不起作用?
  • 不幸的是,我对 R 了解不多。Highcharter 不是官方包装器 - 一些在 Highcharts 中不会出现的问题可能会出现在其中。也许包装器的创建者会有所帮助。 @jbkunst?

标签: r date highcharts xts


【解决方案1】:

让我们以 @kamil-kulig 示例为明星,这将有点超出 R 世界,但如果您不介意,我想给出一些理由。

如果我们看到注解选项不是一个对象而是一个对象列表,那么在 highcharter 中实现了hc_add_annotation 函数。

现在,您使用的是旧版本的 highcharter。 Highcharter 开发版本使用的是 highchartsJS 的 v6,它进行了一些更改:在 annotations.js 之前是一个插件之前,现在作为一个模块包含在参数名称中。

示例一:简单

Kamil Kulig 的例子被复制做:

highchart(type = "stock") %>% 
  hc_add_annotation(
    labelOptions = list(
      backgroundColor = 'rgba(255,255,255,0.5)',
      verticalAlign = 'top',
      y = 15
    ),
    labels = list(
      list(
        point = list(
          xAxis = 0,
          yAxis = 0,
          x = datetime_to_timestamp(as.Date("2017/01/02")),
          y = 1.5
        ),
        text = "Some annotation"
      )
    )
  ) %>% 
  hc_xAxis(
    minRange = 1
  ) %>% 
  hc_add_series(
    pointStart = start,
    pointInterval = day,
    data = c(3, 4, 1)
  )

示例二:使用您的数据

在添加x 位置时要小心。 Highcharter 包含一个 datetime_to_timestamp 函数,用于将日期转换为 highcharts 所需的纪元/时间戳。

library(xts)

dt <- seq(as.Date("2014/1/30"), as.Date("2018/2/6"), "days")
dt <- dt[!weekdays(dt) %in% c("Saturday", "Sunday")]
n <- length(dt)
x <- xts(rnorm(n), order.by=dt)
y <- xts(rnorm(n), order.by=dt)
z <- xts(rnorm(n), order.by=dt)


highchart(type = "stock") %>% 
  hc_title(text = "Some time series") %>% 
  hc_add_series(x, color='green', name="x", showInLegend = TRUE) %>%
  hc_add_series(y, color='red', name="y", showInLegend = TRUE) %>% 
  hc_add_series(z, color='blue', name="z", showInLegend = TRUE) %>% 
  hc_navigator(enabled=FALSE) %>% 
  hc_scrollbar(enabled=FALSE) %>%
  hc_legend(enabled=TRUE, layout="horizontal") %>%
  hc_add_annotation(
    labels = list(
      list(
        point = list(
          xAxis = 0,
          yAxis = 0,
          x = datetime_to_timestamp(as.Date(index(x)[900])),
          y = 1
        ),
        text = "Hello world! How can I make this work!"
      )
    )
  )

【讨论】:

  • 感谢您提出这个问题。我尝试了您的脚本,但得到了一个没有注释的图表。我还查看了datetime_to_timestamp 的输出,这是一个像1.499818e+12 这样的大型科学数字,不确定是它起作用还是不起作用的原因,只是想知道是否有任何其他设置或选项,或者可能是您未在代码中提供的操作系统。我在 Windows 10 上使用 RStudio。谢谢!
  • 你使用的是 development 版本的 highcharter 吗?
  • 我使用的是 Highchart R 库,而不是它的任何其他版本。
  • development 指的是最新版本的包(可能还没有在 CRAN 上),包含新版本的 higchartsJS、修复的 bug、新的功能等. 请看github.com/jbkunst/highcharter#installation
猜你喜欢
  • 2014-03-11
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
相关资源
最近更新 更多