如何在echarts4r中控制日期格式?我可以看到货币,百分比和小数的格式器,但没有日期格式。
这是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)
答案 0 :(得分:1)
您可以将自定义JavaScript函数传递到formatter
内的axisLabel
参数,就像这样:
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)
您将获得: