首先,加载recharts:

library(recharts)

1 介绍Introduction

时间轴默认方位为6点钟位。

建议用chart %>% setTimeline(...)形式调用setTimeline

2 用法Function Call

所有函数名都有小驼峰法和小写连划线(Hadly Wickham推荐此法)两种形式。为了便于偷懒,还有缩写版(如timeline缩写成tl)。

setTimeline(chart, show = TRUE, type = c("time", "number"),
  realtime = TRUE, x = 80, y = NULL, x2 = 80, y2 = 0, width = NULL,
  height = 50, bgColor = "rgba(0,0,0,0)", borderColor = "#ccc",
  borderWidth = 0, controlPosition = c("left", "right", "none"),
  autoPlay = FALSE, loop = TRUE, playInterval = 2000, lineStyle = NULL,
  label = NULL, checkpointStyle = NULL, controlStyle = NULL,
  symbol = "emptyDiamond", symbolSize = 4, currentIndex = 0,
  data = NULL, ...)

setTL(chart, show = TRUE, type = c("time", "number"), realtime = TRUE,
  x = 80, y = NULL, x2 = 80, y2 = 0, width = NULL, height = 50,
  bgColor = "rgba(0,0,0,0)", borderColor = "#ccc", borderWidth = 0,
  controlPosition = c("left", "right", "none"), autoPlay = FALSE,
  loop = TRUE, playInterval = 2000, lineStyle = NULL, label = NULL,
  checkpointStyle = NULL, controlStyle = NULL, symbol = "emptyDiamond",
  symbolSize = 4, currentIndex = 0, data = NULL, ...)

set_timeline(chart, show = TRUE, type = c("time", "number"),
  realtime = TRUE, x = 80, y = NULL, x2 = 80, y2 = 0, width = NULL,
  height = 50, bgColor = "rgba(0,0,0,0)", borderColor = "#ccc",
  borderWidth = 0, controlPosition = c("left", "right", "none"),
  autoPlay = FALSE, loop = TRUE, playInterval = 2000, lineStyle = NULL,
  label = NULL, checkpointStyle = NULL, controlStyle = NULL,
  symbol = "emptyDiamond", symbolSize = 4, currentIndex = 0,
  data = NULL, ...)

set_tl(chart, show = TRUE, type = c("time", "number"), realtime = TRUE,
  x = 80, y = NULL, x2 = 80, y2 = 0, width = NULL, height = 50,
  bgColor = "rgba(0,0,0,0)", borderColor = "#ccc", borderWidth = 0,
  controlPosition = c("left", "right", "none"), autoPlay = FALSE,
  loop = TRUE, playInterval = 2000, lineStyle = NULL, label = NULL,
  checkpointStyle = NULL, controlStyle = NULL, symbol = "emptyDiamond",
  symbolSize = 4, currentIndex = 0, data = NULL, ...)
参数 要求

chart

echartRechart创建的Echarts对象.

show

逻辑型,是否显示时间轴。默认为TRUE。如要把时间轴部件整体从Echarts对象中移除,设为NULL.

type

时间轴格式,‘time’或’number’。默认为’time’.

realtime

逻辑轴,是否实时生效。默认为TRUE.

x

时间轴左上角x坐标,默认为80.

y

时间轴左上角y坐标,默认为NULL (自动配置).

x2

时间轴右下角x坐标,默认为80.

y2

时间轴右下角y坐标,默认为0.

width

时间轴宽度,可以是数值或百分比格式的文本,默认为NULL.

height

时间轴高度,默认为50 (px).

bgColor

时间轴背景色,默认为“rgba(0,0,0,0)” (透明).

borderColor

时间轴边框色,默认为“#ccc”.

borderWidth

时间轴边框宽度,默认为0px (不显示).

controlPosition

时间轴控制钮的方位,可以是’left’, ‘right’ 或 ‘none’. 默认为’left’.

autoPlay

逻辑型,时间轴动画是否自动播放,默认为FALSE.

loop

逻辑型,时间轴是否循环播放,默认为TRUE.

playInterval

播放各时间轴切片的事件间隔,默认为2000 (ms).

lineStyle

列表,时间轴线条样式,默认为 list(color="#666", width=1, type="dashed"). 参见lineStyle函数.

label

列表,时间轴标签样式,默认为 list(show=TRUE, interval="auto", rotate=0, formatter=NULL, textStyle=list(color="#333")). 参见labelStyle函数.

checkpointStyle

列表,时间轴控制点样式,默认为 list(symbol="auto", symbolSize="auto", color="auto", borderColor="auto", borderWidth="auto", label=list(show=FALSE, textStyle=list(color="auto"))). 支持属性包括’symbol’, ‘symbolSize’, ‘color’, ‘borderColor’, ‘borderWidth’, ‘label’.

controlStyle

列表,时间轴控制器样式,默认为 list(itemSize=15, itemGap=5, normal=list(color="#333"), emphasis=list(color="#1e90ff")). Supports features of ‘itemSize’, ‘itemGap’, ‘normal’, ‘emphasis’.

symbol

文本,时间轴图标。默认为 ‘emptyDiamond’. 可以用setSymbols函数支持的图标.

symbolSize

图标尺寸,默认为4.

currentIndex

当前时间轴切片索引值,对应于z的某个水平,用于初始选中某个特定切片。默认为0.

data

时间轴数据,也可用作时间轴标签。默认为NULL.

省略号

3 举例Showcase

g <- echartr(iris, Sepal.Width, Petal.Width, t=Species)
g

初始时显示时间轴,可以关闭。

g %>% setTimeline(show=FALSE)

移动时间轴。注意,这里不能用pos参数。

g %>% setTimeline(y=40)

启用autoPlay并修改playInterval。

g %>% setTimeline(autoPlay=TRUE, playInterval=500)

修改标签样式,并重命名。

g %>% setTimeline(label=labelStyle(textStyle=textStyle(
    color='red', fontFamily='Courier New')),
    data=c('iris 1', 'iris 2', 'iris 3'))

其他样式。

g %>% setTimeline(bgColor='yellow', borderWidth = 1,
                  borderColor='gray') %>%
    setGrid(y2=90)

修改图标。

g %>% setTimeline(symbol='emptyRectangle')

初始时显示第二个时间轴切片,注意,currentIndex要设为1。

g %>% setTimeline(currentIndex=1)