Reputation: 3764
How can the date format be controlled in echarts4r? There is a formatter for currency, percentage and decimal but not for dates that I can see.
Here's an example from https://rpubs.com/paul_simmering/echarts
library(echarts4r)
library(nycflights13) # data
library(tidyverse)
flights_ts <- flights %>%
transmute(week = as.Date(cut(time_hour, "week")), dep_delay, origin) %>%
group_by(origin, week) %>% # works with echarts
summarise(dep_delay = sum(dep_delay, na.rm = TRUE))
ts_base <- flights_ts %>%
e_charts(x = week) %>%
e_datazoom(
type = "slider",
toolbox = FALSE,
bottom = -5
) %>%
e_tooltip() %>%
e_title("Departure delays by airport") %>%
e_x_axis(week, axisPointer = list(show = TRUE))
ts_base %>% e_line(dep_delay)
The chart dates are in format month-date-year which I'd like to change to year-month-date:
Upvotes: 3
Views: 1221
Reputation: 371
You can have a custom JavaScript function passed to the formatter
argument inside the axisLabel
, like so:
library(echarts4r)
library(nycflights13) # data
library(tidyverse)
yearMonthDate <- htmlwidgets::JS('function (value) {
var d = new Date(value);
var datestring = d.getFullYear() + "-" + ("0"+(d.getMonth()+1)).slice(-2) + "-" + ("0" + d.getDate()).slice(-2)
return datestring
}')
ts_base <- flights_ts %>%
e_charts(x = week) %>%
e_datazoom(
type = "slider",
toolbox = FALSE,
bottom = -5
) %>%
e_tooltip() %>%
e_title("Departure delays by airport") %>%
e_x_axis(
week,
axisPointer = list(show = TRUE),
axisLabel = list(
formatter = yearMonthDate
))
ts_base %>% e_line(dep_delay)
You'll get:
Upvotes: 2