Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active April 8, 2022 22:06
Show Gist options
  • Save helgasoft/d8f8f24198874ed5a132cd9744972683 to your computer and use it in GitHub Desktop.
Save helgasoft/d8f8f24198874ed5a132cd9744972683 to your computer and use it in GitHub Desktop.
R | ECharts | areaStyle and itemStyle, click event
library(dplyr)
library(lubridate)
# add event for clicking on blank area
jscode <- c('','',"chart.getZr().on('click', function(e) {
if (!e.target) alert('blank area: '+JSON.stringify(e.event)); });")
set.seed(421)
df1 <- tibble(date= as_date("2021-11-01") + months(0:11),
x= 12*10^6 + rnorm(12,0,2000000),
y= 12*10^6 + rnorm(12,0,1000000))
library(echarty)
p <- df1 %>% ec.init(js= jscode)
p$x$opts$yAxis <- list(axisLabel= list(formatter=ec.clmn('%@M', -1, scale=0.000001)))
p$x$opts$series <- list(
list(type='line', name='lx', encode=list(y='x'), areaStyle=list(opacity=0.2,color="blue"), itemStyle=list(color='blue')),
list(type='line', name='ly', encode=list(y='y'), areaStyle=list(opacity=0.2,color="red"), itemStyle=list(color='red')))
p$x$opts$legend <- list(show=TRUE)
p$x$on <- list( # event(s) with Javascript handler
list(event= 'legendselectchanged',
handler= htmlwidgets::JS("(evt) => alert('selected: '+evt.name);"))
)
p
@helgasoft
Copy link
Author

Thank you for trusting our code. Glad to see you are taking it further.
For documentation purposes, please copy your question in Issues.
There is a solution and will be given there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment