使用echarts4r中的代理来更改特定柱形的颜色。

huangapple go评论73阅读模式
英文:

Use proxies in echarts4r to change color of specific bar

问题

我正在研究如何使用代理将新系列添加到现有图表中。对于特定的图表,我想强调现有系列中的某一点,比如通过改变颜色,比如改成红色。
我没有找到如何做到这一点的方法。我该如何以这样的方式更改图表的选项?

骨架代码:

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") # 然后呢?

})

}

shinyApp(ui, server)


<details>
<summary>英文:</summary>

I&#39;m reading up on using proxies to add new series to existing charts. For a specific graph, instead of adding brand new series, I&#39;d like to emphasize a certain point on existing one, namely by changing color, let&#39;s say to red.
I didn&#39;t manage to figure out how to do it. How can I change a chart&#39;s options in such a way?

Skeleton code:

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)


</details>


# 答案1
**得分**: 1

这是一个可以实现你想要的功能的示例代码:

```r
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)

请注意,代码中的中文字符已保留在原文中。

英文:

Here is a working example that does what you want:

library(shiny)
library(echarts4r)


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

ui &lt;- fluidPage(
  echarts4rOutput(&quot;chart&quot;),
  actionButton(&quot;changecolor&quot;, &quot;Change color of third bar&quot;),
  actionButton(&quot;removecolor&quot;, &quot;Remove color of third bar&quot;)
  
)

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

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年6月1日 20:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定