JohnCoene / echarts4r

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

timeline in combination with calendar #630

Closed rdatasculptor closed 1 month ago

rdatasculptor commented 1 month ago

I know that the calendar chart does not officially support timeline, but isn';t there a smart way to only show the month of the timeline instead of all the months in the range with only the content of the month shown that is picked by the timeline?

dates <- seq.Date(as.Date("2017-01-01"), as.Date("2018-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)

year <- data.frame(date = dates, values = values) %>%
  mutate(month = as.character(date),
         month = substr(month, 6, 7))

year |> 
  group_by(month) |>
  e_charts(date, timeline = TRUE) |> 
  e_calendar(range = c("2018")) |> 
  e_heatmap(values, coord_system = "calendar") |> 
  e_visual_map(max = 30) |> 
  e_title("Calendar", "Heatmap")
JohnCoene commented 1 month ago

It looks like you can be more specific with the range, e.g.: range = c('2017-01-02', '2017-02-23') Does that work?

rdatasculptor commented 1 month ago

untortenately not. As you can see in the screenshot timeline picks january, but february is still being shown (though empty). image

rdatasculptor commented 1 month ago

the range can't be determined by the timeline so it seems. But maybe there is a workaround somehow?

rdatasculptor commented 1 month ago

Okay, the moment I asked that question, I thought of a solution:

dates <- seq.Date(as.Date("2017-01-01"), as.Date("2018-12-31"), by = "day")
values <- rnorm(length(dates), 20, 6)

year <- data.frame(date = dates, values = values) %>%
  mutate(month = as.character(date),
         month = substr(month, 6, 7))

get_days_in_month <- function(year, month) {
  require(lubridate)
  date <- ymd(paste(year, month, "01", sep = "-"))

  last_day <- ceiling_date(date, "month") - days(1)

  return(day(last_day))
}

calendarList <- list()
months <- unique(year$month)
months <- months[order(months)]
for (i in 1:length(months)){
  calendarList[[i]] <- list(range = c(paste0("2017-", months[i], "-01"), 
                                      paste0("2017-", months[i], "-", get_days_in_month(2017, as.numeric(months[i])))))
  }

year |> 
  group_by(month) |>
  e_charts(date, timeline = TRUE) |> 
  e_timeline_serie(calendar = calendarList) %>%
  e_heatmap(values, coord_system = "calendar") |> 
  e_visual_map(max = 30) |> 
  e_title("Calendar", "Heatmap")