1

I'm working on a dashboard, that provides sales achievement. I'm using echarts4r, and deployed in the cloud of shinyapps.io

datafpytd[datafpytd$NIK==input$NIK,] %>% 
  e_charts(Month) %>% 
  e_bar(serie = GAP_Revenue, name = "Gap Revenue YTD") %>%
  e_color(ifelse(datafpytd[datafpytd$NIK==input$NIK,]$GAP_Revenue>0,"green","red"))

How do I make the color of the bar chart different depending on its value, if greater than 0, the color should be green, otherwise should be red?

This is the sample data:

 structure(list(Index = c(1L, 1L, 1L, 1L, 1L, 1L), Group = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "Major Accounts", class = "factor"), 
    Div = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Financial Institution", class = "factor"), 
    Dept = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "-", class = "factor"), 
    Name = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Jamil W.", class = "factor"), 
    Status = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "PE", class = "factor"), 
    NIK = c(75994631L, 75994631L, 75994631L, 75994631L, 75994631L, 
    75994631L), Data_Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = "Monthly", class = "factor"), Month = structure(c(3L, 
    2L, 5L, 1L, 6L, 4L), .Label = c("April", "February", "January", 
    "June", "March", "May"), class = "factor"), Name_FP = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "Financial Institution - Jamil W.", class = "factor"), 
    FAB_In = c(681272250L, 549260036L, 247058501L, 492335484L, 
    1624053867L, 636687867L), Target_FAB_In = c(562081784L, 655762082L, 
    708223048L, 929889299L, 1033210332L, 797047970L), Revenue = c(32594192621, 
    31953999491, 31259721093, 31809774195, 31725823061, 29965576806
    ), Target_Revenue = c(31609626147, 32896422576, 34077352579, 
    31709041552, 32174547049, 33854985985)), class = "data.frame", row.names = c(NA, 
-6L)) 

Thanks

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Can you put a sample data by `dput()` ? – maydin Aug 21 '19 at 07:26
  • hi, i dont know how to attach the data here, but i'm using csv data, GAP revenue contains a number, Month is a month period. – Lufthy Oreckel Aug 21 '19 at 09:10
  • Just run `dput(head(data,10))` on your pc, copy the output and put in here. – maydin Aug 21 '19 at 12:36
  • this is the data, posted in the questions. thaanks btw :) – Lufthy Oreckel Aug 22 '19 at 05:59
  • I have found an information in [here](https://github.com/JohnCoene/echarts4r/issues/66). It tells that, if you have a column named color (not colors, not something else but color..), you can pass the color info into graph by `e_add("itemStyle", color )` . I know it sounds very weird but, it is all I can found for this...By the way, yes I tried and it worked! – maydin Aug 22 '19 at 07:48
  • 1
    whoa it worked @maydin ! thaaanks a looot! :) – Lufthy Oreckel Aug 22 '19 at 08:12

2 Answers2

5

I'm the author of echarts4r, sorry I see your question so late. You can use e_visual_map and set it to piecewise.

df <- tibble::tibble(
  Month = as.character(1:10),
  Revenue = runif(10, -10, 10)
)

library(echarts4r)

df %>% 
  e_charts(Month) %>% 
  e_bar(Revenue) %>% 
  e_visual_map(
    type = "piecewise",
    pieces = list(
      list(
        gt = 0,
        color = "green"
      ),
      list(
        lte = 0,
        color = "red"
      )
    )
  )

echarts4r plot

JohnCoene
  • 2,107
  • 1
  • 14
  • 31
2

In order to display other options and make available potential alternatives echarts4r, using @JohnCoene 's example, we have:

df <- tibble::tibble(
  Month = as.character(1:10),
  Revenue = runif(10, -10, 10)
)

library(echarts4r)
library(dplyr)

df %>% 
  dplyr::mutate(cond = ifelse(Revenue > 0, "green", "red")) %>% 
  dplyr::group_by(cond) %>% 
  e_charts(Month) %>% 
  e_bar(Revenue) %>% 
  e_color(color = c("green", "red")) %>% 
  e_legend(FALSE)

enter image description here

One more option.

df %>% 
  e_charts(Month) %>% 
  e_bar(Revenue,
        itemStyle = list(color = htmlwidgets::JS("
          function(params) {
                                var colorList = ['blue', 'blue', 'pink', 'blue', 'pink', 'blue', 'pink', 'blue', 'blue', 'pink'];
                                return colorList[params.dataIndex]
                                }
                                
    "))) %>% 
  e_legend(FALSE)

enter image description here

bbiasi
  • 1,549
  • 2
  • 15
  • 31