首先,加载recharts
:
library(recharts)
时间轴默认方位为6点钟位。
建议用chart %>% setTimeline(...)
形式调用setTimeline
。
所有函数名都有小驼峰法和小写连划线(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 |
|
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 |
列表,时间轴线条样式,默认为 |
label |
列表,时间轴标签样式,默认为 |
checkpointStyle |
列表,时间轴控制点样式,默认为 |
controlStyle |
列表,时间轴控制器样式,默认为 |
symbol |
文本,时间轴图标。默认为 ‘emptyDiamond’. 可以用 |
symbolSize |
图标尺寸,默认为4. |
currentIndex |
当前时间轴切片索引值,对应于z的某个水平,用于初始选中某个特定切片。默认为0. |
data |
时间轴数据,也可用作时间轴标签。默认为NULL. |
… |
省略号 |
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)