highcharter 股票中的其他工具提示信息

时间:2021-03-10 13:09:00

标签: r highcharts r-highcharter

我目前正在构建一个应用程序,我想在 highcharter 股票中添加工具提示以及其他信息。

#Data
df <- data.frame(time = seq(as.Date("2021-03-10"), length = 10, by = "days"),
                 values = 1:10,
                 additionalInfo1 = LETTERS[1:10],
                 additionalInfo2 = letters[1:10])

#Packages
library(highcharter) #plots
library(xts) #conversion for stock-highchart
library(dplyr) #piping-operator

我知道如何在使用 type="line" 时向工具提示添加附加信息。这可以通过

完成
highchart_line <- hchart(df, "line", hcaes(x = time, y = values), 
                         tooltip = list(headerFormat = "<b> Some Tooltipheader </b> <br/>",
                                        pointFormat = paste0("index: {point.index} <br/>",
                                                             "time: {point.time} <br/>", 
                                                             "additional1: {point.additionalInfo1} <br/>",
                                                             "additional2: {point.additionalInfo2}")))

并显示一切正常。好处是可以提供整个数据,因此可以访问列名。

创建 type="stock" 时,我需要将数据转换为 xts,同时仅使用时间和值:

stockdata_xts <- xts(x = df$values, order.by = df$time)
highchart_stock <- highchart(type="stock") %>% 
  hc_add_series(stockdata_xts, name = "someData",
                tooltip = list(pointFormat = paste0(
                  "point.x: {point.x} <br/>",
                  "point.y: {point.y} <br/>",
                  "point.index: {point.index} <br/>"
                ))) %>% 
  hc_rangeSelector(enabled = FALSE)

有没有办法将工具提示的附加数据放在股票高点图表中,使工具提示看起来像上面折线图中的那个?也许 xts 对象的某些功能包括更多我不知道的属性。

我想使用 stock-highchart 的原因是导航栏。如果有一种方法可以在 line-highchart 中包含导航栏,我也会很感激。还有一个函数 highcharter::hc_navigator,但是文档说它只适用于高库存。 (参见 https://cloud.r-project.org/web/packages/highcharter/highcharter.pdf ,第 50 页)

1 个答案:

答案 0 :(得分:0)

是的,导航器在 stockChart 中工作。对于这种工具提示自定义,我认为最好使用专用的格式化程序 API 函数:https://api.highcharts.com/highstock/tooltip.formatter

在这里你可以找到一篇可以帮助你在 R 中使用 JS 代码的文章: https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/?fbclid=IwAR1Em2yNUsIJunTRS4IEbUwGksb5PC7LfZATLcyvb7uLS7ZvV7v4-e0L0