如何在highcharter中配置填充渐变的color stops颜色断点?
解决方案
核心问题是你之前的fillColor配置仅定义了渐变方向,缺少stops参数来设置色阶,同时linearGradient的终点参数配置错误,以下是可直接运行的修改后代码:
library(quantmod) library(highcharter) df = getSymbols("SPY", from = "2021-05-01", auto.assign = F) highchart() %>% hc_chart(zoomType = "x") %>% hc_plotOptions( area = list( fillColor = list( linearGradient = list( x1 = 0, y1 = 0, # 渐变起点:图表顶部 x2 = 0, y2 = 1 # 渐变终点:图表底部 ), # 色阶配置,格式为c(渐变位置, 颜色值),渐变位置取值范围为0-1 stops = list( c(0, "rgba(68, 170, 213, 0.8)"), # 渐变起点颜色 c(1, "rgba(68, 170, 213, 0)") # 渐变终点透明色,和官方demo效果对齐 ) ), 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, color = "#44aad5") %>% hc_legend(enabled = FALSE) %>% hc_xAxis(type = "datetime")
关键修改说明
- 移除了
hc_chart中错误的type = "container配置,该参数无需手动设置,会自动识别系列类型 - 调整
linearGradient的y2 = 1,实现垂直方向的渐变效果 - 新增
stops参数定义色阶,支持十六进制、rgb/rgba多种颜色格式,可根据需求自行调整色值和渐变位置 - 在
hc_add_series中指定折线主色,和填充渐变的顶部色保持一致,视觉效果更统一
内容的提问来源于stack exchange,提问作者niceguy
相关产品推荐
相关产品推荐

