Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active July 22, 2020 19:34
Show Gist options
  • Save helgasoft/a5668a78eb27dea1f6fd3eb9243e2c0c to your computer and use it in GitHub Desktop.
Save helgasoft/a5668a78eb27dea1f6fd3eb9243e2c0c to your computer and use it in GitHub Desktop.
echarts4r demo
# simple echart with one symbol and line,area,point
library(quantmod); library(echarts4r); library(dplyr)
getSymbols("IBM", from='2019-01-01')
IBM <- as.data.frame(IBM)
names(IBM) <- c("open", "high", "low", "close", "volume", "adjusted")
df <- round(IBM, 2) %>% mutate(sym="IBM", date=row.names(IBM))
ivl <- c("2019-10-03", "2020-04-03") # mark area interval
sset <- df %>% filter(between(as.Date(date), as.Date(ivl[1]),as.Date(ivl[2])))
fit <- lm(Cl(sset) ~ c(1:nrow(sset))) # linear regression btw two dates
df %>%
e_charts(date) %>%
e_y_axis(scale = TRUE) %>%
e_candle(open, close, low, high, legend=FALSE) %>%
e_tooltip(trigger = "axis") %>%
e_datazoom(start = 20) %>%
e_mark_area(data = list(
list(xAxis = ivl[1]), list(xAxis = ivl[2]))
) %>%
e_mark_line(lineStyle = list(type='solid', color='#ff33b8', width=2),
data = list(list(xAxis=ivl[1], yAxis=first(fitted(fit))),
list(xAxis=ivl[2], yAxis=last(fitted(fit)))
), symbol=c('arrow','diamond') #'none'
) %>%
e_mark_point(data = list(xAxis=df$date[df$close==max(df$close)],
yAxis=max(df$close),
value=paste('max\n',round(max(df$close)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment