Recharts4r-沿轴格式化日期

时间:2020-09-10 23:29:36

标签: r echarts echarts4r

如何在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)

图表日期的格式为month-date-year,我想更改为year-month-date: ts

1 个答案:

答案 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)

您将获得:

enter image description here