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

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

Use proxies in echarts4r to change color of specific bar

问题

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

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)

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. 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?
  5. 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)

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 这是一个可以实现你想要的功能的示例代码:
  5. ```r
  6. library(shiny)
  7. library(echarts4r)
  8. df <- data.frame(
  9. x = 1:5,
  10. y = runif(n = 1:5, min = 50, max = 200)
  11. )
  12. ui <- fluidPage(
  13. echarts4rOutput("chart"),
  14. actionButton("changecolor", "Change color of third bar"),
  15. actionButton("removecolor", "Remove color of third bar")
  16. )
  17. server <- function(input, output){
  18. color <- reactiveVal("'blue'")
  19. observeEvent(input$changecolor, {
  20. color("'red'")
  21. })
  22. observeEvent(input$removecolor, {
  23. color("'blue'")
  24. })
  25. output$chart <- renderEcharts4r({
  26. e_charts(df, x) |>
  27. e_bar(y,
  28. itemStyle = list(color = htmlwidgets::JS(paste0("
  29. function(params) {
  30. var colorList = ['blue', 'blue',",
  31. color(), ", 'blue', 'blue']; return colorList[params.dataIndex]
  32. }")))) |
  33. e_legend(FALSE)
  34. })
  35. }
  36. shinyApp(ui, server)

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

英文:

Here is a working example that does what you want:

  1. library(shiny)
  2. library(echarts4r)
  3. df &lt;- data.frame(
  4. x = 1:5,
  5. y = runif(n = 1:5, min = 50, max = 200)
  6. )
  7. ui &lt;- fluidPage(
  8. echarts4rOutput(&quot;chart&quot;),
  9. actionButton(&quot;changecolor&quot;, &quot;Change color of third bar&quot;),
  10. actionButton(&quot;removecolor&quot;, &quot;Remove color of third bar&quot;)
  11. )
  12. server &lt;- function(input, output){
  13. color &lt;- reactiveVal(&quot;&#39;blue&#39;&quot;)
  14. observeEvent(input$changecolor, {
  15. color(&quot;&#39;red&#39;&quot;)
  16. })
  17. observeEvent(input$removecolor, {
  18. color(&quot;&#39;blue&#39;&quot;)
  19. })
  20. output$chart &lt;- renderEcharts4r({
  21. e_charts(df, x) |&gt;
  22. e_bar(y,
  23. itemStyle = list(color = htmlwidgets::JS(paste0(&quot;
  24. function(params) {
  25. var colorList = [&#39;blue&#39;, &#39;blue&#39;,&quot;,
  26. color(), &quot;, &#39;blue&#39;, &#39;blue&#39;]; return colorList[params.dataIndex]
  27. }&quot;)))) |&gt;
  28. e_legend(FALSE)
  29. })
  30. }
  31. 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:

确定