highcharts 为highcharter柱形图设置条件颜色和图案

vd8tlhqk  于 2023-08-05  发布在  Highcharts
关注(0)|答案(1)|浏览(197)

我想创建一个柱形图,其中只有最后一列具有模式(在下面的示例中为2020年),其余列具有实心填充。我知道可以用hc_add_dependency("modules/pattern-fill.js")添加的模式填充模块,但我很难让它为我的图表工作。事实上,我甚至不能将默认(纯色)颜色更改为自定义颜色。我真的很感激你的帮助!非常感谢!
下面是一个最小的例子:

library(tidyverse)
library(highcharter)

df <- data.frame(cbind(seq(2001, 2020, by=1),
                       sample(1:15, size=20, replace=T),
                       sample(1:15, size=20, replace=T))) 
colnames(df) <- c("year", "newA", "newB")

  highchart() %>%
    hc_chart(type = "column") %>%
    hc_add_series(
      name = "New A",
      data = df$newA,
      stack = "new",
      showInLegend = F) %>%
    hc_add_series(
      name = "New B",
      data = df$newB,
      stack = "new",
      showInLegend = F) %>%
    hc_plotOptions(column = list(stacking = "normal")) %>%
    hc_xAxis(categories = df$year, title = list(text = ""))

字符串

cyej8jka

cyej8jka1#

在此示例中,使用单个数据系列创建柱形图。只有最后一列(“第3天”)用模式填充,如hc_add_series()函数中所指定的。对于这个列,我们提供了一个包含值('y')和颜色规范的列表,而不是只提供一个值:

library(highcharter)

Column_with_stripe <- highchart() %>%
  hc_chart(type = "column") %>%
  hc_add_dependency("modules/pattern-fill.js") %>%
  hc_xAxis(categories = c('Day 1', 'Day 2', 'Day 3'), tickmarkPlacement = "on") %>%
  hc_yAxis(title = list(text = "Value")) %>%
  hc_plotOptions(line = list (datalabels = list(enabled = TRUE), enableMouseTracking = FALSE)) %>%
  hc_add_series(
    name = "Var1", 
    data = list(
      5, # Day 1
      8, # Day 2
      list(y = 10, color = list(pattern = list(path = 'M 0 0 L 10 10 M 9 -1 L 11 1 M -1 9 L 1 11', width = 5, height = 5, color = '#f00'))) # Day 3
    ),
    color = "black"
  )

print(Column_with_stripe)

字符串

相关问题