在echarts4r中使用代理来改变特定条的颜色

camsedfj  于 11个月前  发布在  Echarts
关注(0)|答案(1)|浏览(131)

我正在阅读使用代理来添加新系列到现有图表。对于一个特定的图表,我不想添加全新的系列,而是想在现有的图表上强调一点,即通过改变颜色,比如说红色。我不知道该怎么做。如何以这种方式更改图表的选项?
框架代码:

library(shiny)
library(echarts4r)

df <- data.frame(
  x = 1:5,
  y = runif(n = 1:5, min = 50, max = 200)
)

ui <- fluidPage(
  echarts4rOutput("chart"),
  actionButton("changecolor", "Change color of third bar"),
  actionButton("removecolor", "Remove color of third bar")
)

server <- function(input, output){
  
  output$chart <- renderEcharts4r({
    e_charts(df, x) |> 
      e_bar(y)
  })
  
  observeEvent(input$changecolor, {
    echarts4rProxy("chart") # What now?
      
  })
  
}

shinyApp(ui, server)
vs91vp4v

vs91vp4v1#

下面是一个工作示例,它可以实现您想要的功能:

library(shiny)
library(echarts4r)

df <- data.frame(
  x = 1:5,
  y = runif(n = 1:5, min = 50, max = 200)
)

ui <- fluidPage(
  echarts4rOutput("chart"),
  actionButton("changecolor", "Change color of third bar"),
  actionButton("removecolor", "Remove color of third bar")
  
)

server <- function(input, output){
  
  color <- reactiveVal("'blue'")
  observeEvent(input$changecolor, {
    color("'red'")
  }) 
  observeEvent(input$removecolor, {
    color("'blue'")
  }) 
  
  output$chart <- renderEcharts4r({
    e_charts(df, x) |> 
      e_bar(y,
            itemStyle = list(color = htmlwidgets::JS(paste0("
          function(params) {
                                var colorList = ['blue', 'blue',", 
                                color(), ", 'blue', 'blue']; return colorList[params.dataIndex]
                                }")))) |>
      e_legend(FALSE)
  })
  
}

shinyApp(ui, server)

相关问题