【问题标题】:Plotbands in Highcharter - When x axis is dateHighcharter中的Plotbands - 当x轴是日期时
【发布时间】:2020-12-02 15:59:42
【问题描述】:

我有以下数据集:

xdata <- seq(as.Date("2020-11-01"),as.Date("2020-11-10"), "days")
ydata <- c(1:10)
datamipo <- data.frame(xdata,ydata)

我想使用 highcharter 库制作图表。我想用情节带突出显示 2020-11-01 到 2020-11-05 的日子。这是我尝试过的:

datamipo %>% 
  hchart(type = "line",
         hcaes(x = xdata, y = ydata),
         color = "#25af7b") %>%
  hc_xAxis(plotBands = list(
    list(
      from = 1,
      to = 5,
      label = list(text = "This is a plotBand"),
      color = hex_to_rgba("red", 0.1),
      zIndex = 1
    )
  ))

但是 plotband 是不可见的。请问,你知道我该怎么做吗?

【问题讨论】:

  • fromto 需要提供日期,而不是 1 和 5,并使用 datetime_to_timestamp 进行转换,如 this answer 中所述
  • 嗨@Ben,感谢您的指导。当轴引用日期时,似乎 highcharter 不采用 xAxis 中的相对数字。
  • 你觉得在这个要求下你仍然可以获得你想要的图表吗?您的情况是否需要更多的数据处理才能完成这项工作?
  • 您好@Ben,这是我对代码所做的更改并且工作正常:from = datamipo$xdata[1] %&gt;% datetime_to_timestamp(), to = datamipo$xdata[5] %&gt;% datetime_to_timestamp() 但问题是数据必须格式化为“yyyy-mm-dd”。谢谢!

标签: r highcharts r-highcharter


【解决方案1】:

也许您可以尝试在 R 中使用 Highchart JavaScript API 语法? https://api.highcharts.com/highcharts/xAxis.plotBands https://api.highcharts.com/highcharts/chart.events.load

您可以在此处找到有关如何执行此操作的文章: https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/

代码示例:

library('highcharter')
highchart() %>%
  hc_add_series(
    type = "line",
    data = list(5, 4, 3, 5)
  ) %>%
  hc_chart(events = list(load = JS("function() {
        const chart = this;
        chart.update({
          xAxis: {
            plotBands: [{
              color: '#FCFFC5',
              from: 0.5,
              to: 2.5
            }]
          }
        });
    }
    ")
  ))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多