将hcharts()条从纯色更改为渐变

问题描述 投票:0回答:1

我试图通过使颜色成为两种绿色调之间的渐变来使我的highcharter图(对于R Shiny仪表板)更漂亮。我使用pokemon中的默认highcharter数据集制作了以下基本示例。

library(shiny)
library(highcharter)

#Subset the data into the pokemon that are Grass and Poison
grass_subset <- subset(pokemon, type_1 == "grass" & type_2 == "poison")

#Define the chart parameters
grass_poison_chart <- hchart(grass_subset, type = "column", hcaes(x=pokemon, y=base_experience))

#Define theme elements
myTheme <- hc_theme(
  colors = c('#658D1B', '84BD00', 'white'),
  chart = list(
    backgroundColor = "white"    
  ))

#Apply theme to grass_poison_chart
grass_poison_chart %>% hc_add_theme(myTheme)

生产

问题是我不知道用渐变色调条纹的方法。我试图将我对myTheme的定义更改为以下内容:

 myTheme <- hc_theme(
   color = list(
     linearGradient = (0,0,0,1),
     stops = (0, '#658D1B'),(1, '#84BD00')
   )
  )

在简单地复制highcharts语法的天真尝试:

color: {
    linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 },
    stops: [
        [0, '#658D1B'],
        [1, '#84BD00']
    ]
}

把它改成括号,但我真的不知道从哪里开始。我知道我所做的可能不会起作用(但事实并非如此),但在highcharter中可能会出现这种情况吗?我错过了什么?

r highcharts shiny shinydashboard
1个回答
0
投票

很抱歉迟到的回复,我在安装R时遇到了一些问题。

我做的第一件事就是尝试用纯JS编写代码。这部分很简单:https://jsfiddle.net/BlackLabel/byht9jd6

我认为在R中实现它有点困难,但是......这部分也很简单:)这是一个代码:

library(shiny)
library(highcharter)

#Subset the data into the pokemon that are Grass and Poison
grass_subset <- subset(pokemon, type_1 == "grass" & type_2 == "poison")

#Define the chart parameters
grass_poison_chart <- hchart(grass_subset, type = "column", hcaes(x=pokemon, 
y=base_experience)) %>%
  hc_plotOptions(
    column = list(
      color = list(
        linearGradient = list(x1 = 0, x2 = 0, y1 = 0, y2 = 1),
        stops = list(
          c(0, '#658D1B'),
          c(1, '#84BD00')
        )
      )
    )
  )

#Apply theme to grass_poison_chart
grass_poison_chart %>% hc_add_theme(myTheme)
© www.soinside.com 2019 - 2024. All rights reserved.