如何在 highcharter 中应用颜色停止?

How to apply color stops in highcharter?

我正在尝试将此 highchart 演示应用到 R 中:https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/line-time-series

我完成了大部分工作,我的问题是我不确定如何为填充渐变应用颜色参数。

这是我的代码:

library(quantmod)
library(highcharter)

df = getSymbols("SPY", from = "2021-05-01", auto.assign = F)

highchart() %>%
  hc_chart(
    type = "container", zoomType = "x") %>%
  hc_plotOptions(
    area = list(
      fillColor = list(
        linearGradient = list(
          x1 = 0,
          y1 = 0,
          x2 = 0,
          y2 = 0
        )
      )
    ),
    marker = list(
      radius = 2
    ),
    lineWidth = 1,
    states = list(
      hover = list(
        lineWidth = 1
      )
    ),
    threshold = NULL
  ) %>%
  hc_add_series(type = "area", name = "test", data = df$SPY.Close) %>%
  hc_legend(enabled = FALSE) %>%
  hc_xAxis(type = "datetime") 

这是我的图表:

感谢任何帮助!

使用 color_stops 助手,您可以像这样设置填充颜色渐变的停止点:

注意:要获得渐变,您必须在 linearGradient 中设置 y2=1

library(quantmod)
library(highcharter)

df = getSymbols("SPY", auto.assign = F)

highchart() %>%
  hc_chart(
    type = "container", zoomType = "x") %>%
  hc_plotOptions(
    area = list(
      fillColor = list(
        linearGradient = list(
          x1 = 0,
          y1 = 0,
          x2 = 0,
          y2 = 1
        ),
        stops = color_stops(n = 2, colors = c("#2f7ed8","white"))
      ),
      marker = list(
        radius = 2
      ),
      lineWidth = 1,
      states = list(
        hover = list(
          lineWidth = 1
        )
      ),
      threshold = NULL
    )
  ) %>%
  hc_add_series(type = "area", name = "test", data = df$SPY.Close) %>%
  hc_legend(enabled = FALSE) %>%
  hc_xAxis(type = "datetime")