JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
585 stars 82 forks source link

How is it possible to adapt the line color to the data input? #565

Closed Joselinah closed 10 months ago

Joselinah commented 10 months ago

Hi,

I would like to customize the line color in the chart so that when I select a value, it takes on a specific color. Currently I only managed to get the points in the desired color with e_add_nested("itemStyle", color).


data <- PMK_gesamtDE_Jahrdf %>%
  dplyr::filter(Phänomen %in% input$phäno1 &
                  Jahr %in% input$jahr1) %>%
  group_by(Phänomen) %>%
  dplyr::mutate(
    color = case_when(
      Phänomen == "rechts" ~ "#c97654",
      Phänomen == "ausländisch" ~ "#C93312",
      Phänomen == "links" ~ "#446455"))

data %>%
  e_charts(Jahr)%>%
  e_line(Anzahl, smooth=FALSE)%>%
  e_axis_labels(x="Jahr")%>%
  e_x_axis(show=TRUE, axisTick=FALSE, axisLine=list(show=FALSE)) %>%
  e_title("Gesamtanzahl Deutschland", "politisch motivierte Kriminalität")%>%
  e_tooltip(trigger = "axis", order="valueDesc")%>%
  e_mark_point(input$phäno, data=list(type="max"))%>%
  e_toolbox(bottom=0)%>%
  e_toolbox_feature(feature = "dataZoom")%>%
  e_toolbox_feature(feature = "dataView")%>%
  e_add_nested("itemStyle", color)

 })}

Is this possible and could someone please help me with this?

Thanks for help!

Joselinah commented 10 months ago

I was able to solve my problem with this #195 If anyone has the same question, here is my solution:

I defined colors and added e_color(colors)


output$linechart <- renderEcharts4r({

  colors <- c(
    "blue","#C93312","#446455","blue","brown","green")

data <- PMK_gesamtDE_Jahrdf %>%
  dplyr::filter(Phänomen %in% input$phäno1 &
                  Jahr %in% input$jahr1)

data %>%
  group_by(Phänomen)%>%
  e_charts(Jahr)%>%
  e_line(serie=Anzahl, smooth=FALSE)%>%
  e_axis_labels(x="Jahr")%>%
  e_x_axis(show=TRUE, axisTick=FALSE, axisLine=list(show=FALSE)) %>%
  e_title("Gesamtanzahl Deutschland", "politisch motivierte Kriminalität")%>%
  e_tooltip(trigger = "axis", order="valueDesc")%>%
  e_mark_point(input$phäno1, data=list(type="max"))%>%
  e_toolbox(bottom=0)%>%
  e_toolbox_feature(feature = "dataZoom")%>%
  e_toolbox_feature(feature = "dataView")%>%
  e_color(colors)
})} 

Regards :)

rdatasculptor commented 10 months ago

@Joselinah I guess you also could have added one more line to your first echarts attempt:

e_add_nested("lineStyle", color)