英文:
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'm reading up on using proxies to add new series to existing charts. For a specific graph, instead of adding brand new series, I'd like to emphasize a certain point on existing one, namely by changing color, let's say to red.
I didn't manage to figure out how to do it. How can I change a chart'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 <- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论