helgasoft / echarty

Minimal R/Shiny Interface to ECharts.js
https://helgasoft.github.io/echarty/
86 stars 3 forks source link

Combinations of echarts4r and charty #18

Closed rdatasculptor closed 2 years ago

rdatasculptor commented 2 years ago

Hi @helgasoft,

I have just a question/thought. For me the interface of echarts4r is a little bit easier for me to understand than echarty. On the other hand echarty seems much more capable of unleash all the features that echarts has. Do you think there is somehow a way to combine the two packages, e.g. by building and lay-outing a chart with a echarts4r and adding features, charts, morphs, etc. with the help of charty?

You have this nice function that translates a js echarts object into an (echarty-) R object. I was thinking of a similar translator function that tranlates an echarts4r-object into a echarty-object.

I know this will probably a much too complicated way to go, but I am curious about your opinion on this.

Thanks in advance!

helgasoft commented 2 years ago

Combine the two packages is, as you say, a "much too complicated way to go", got to choose one. echarts4r is user-friendly because it encapsulates a lot of settings internally. Advanced users however need more control, echarty was born of this necessity. The idea behind echarty's API is "why encapsulate when we can use the ECharts API directly?". It is like C# vs C++, different ways of getting it done. A question of preference or necessity.

As a side note: both packages have functions to inspect their final option sent to ECharts.js

library(echarts4r)
cars |> e_charts(speed) |> e_scatter(dist) |> e_inspect(json=TRUE, pretty=TRUE)
library(echarty)
cars |> ec.init() |> ec.inspect()  # first 2 columns taken by default as x,y
rdatasculptor commented 2 years ago

Yes, I understand. And thanks for putting me in the direction of the inspect-functions!

And last but not least, thank you for all the effort you put in echarty! I can clearly see its power and its additional value compared to echarts4r

rdatasculptor commented 2 years ago

I think I made a first "integration" of echarts4r and echarty... kind of... :) The chart is made by using echarts4r, but is displayed by using echarty.

library(echarts4r)
library(echarty)

df <- data.frame(
  year = c(
    rep(2016, 25),
    rep(2017, 25),
    rep(2018, 25),
    rep(2019, 25)
  ),
  x = rnorm(100),
  y = rnorm(100),
  grp = c(
    rep("A", 50),
    rep("B", 50)
  )
) 

## echarts4r
txt <- df |> 
  group_by(year) |> 
  e_charts(x, timeline = TRUE) |> 
  e_scatter(y) |> 
  e_loess(y ~ x) |> 
  e_inspect(json=TRUE, pretty=TRUE)

## echarty
ec.fromJson(txt)

And now I am wondering.. Could this potentially be a way for echarts4r users to bring crosstalk features to echarts4r... (Since echarty has crosstalk integration capabilities) somehow... (@JohnCoene).

This obviously doesn't work, but I was thinking of something like this:

library(crosstalk)
sdf <- mtcars %>% rownames_to_column(var='name') %>% relocate(mpg,wt)
sdf <- SharedData$new(sdf)

p <- sdf %>% ec.init(height=500, title=list(text="echarty 2D"))
p$x$opts$xAxis$scale <- TRUE
p$x$opts$yAxis$scale <- TRUE
p$x$opts <- append(p$x$opts, list(
  toolbox = list(feature=list(brush=list(ey=''))),
  brush = list(brushLink='all', throttleType='debounce'),
  dataZoom = list(type='inside'),
  tooltip = list(formatter = htmlwidgets::JS("function(params){return params.value[2];}"))
))
p$x$opts$series[[1]] = append(p$x$opts$series[[1]], list(
  itemStyle = list(color = htmlwidgets::JS("function(params){
    let cyl=params.value[3]; return (cyl==4 ? 'RoyalBlue' : cyl==6 ? 'OrangeRed':'green');}") ),
  selectedMode = 'multiple',
  select = list(itemStyle=list(color='magenta')), 
  emphasis = list(focus='self', blurScope='series'), # for brush rcv
  blur = list(opacity = 0.2)
))

psjon <- p |> ec.inspect() |> jsonlite::fromJSON()

p2 <- e_charts() |>
  e_list(psjon, append = FALSE)

bscols(
  list(
    filter_slider("hp", "Horsepower", sdf, ~hp, width = "100%")
  ),  p2
)

I guess still something has to be done on the side of echarts4r, but maybe someone can pick this up or gives this inspiration about how to make this work. (perhaps a R and htmlwidgets hero-wizard like @timelyportfolio :))

helgasoft commented 2 years ago

Aah, trailblazing, love it 🥇 , and going straight to crosstalk - ambitious! However... crosstalk is communication between htmlwidgets and is not 'recorded' in the exported JSON. Also, echarty implements crosstalk thru ECharts dataset which is not supported by echarts4r. So I do not see a way of adding somehow crosstalk capabilities to echarts4r via echarty.

But your idea opens other possibilities of graphical integration. Here is a playful one - adding lotties to echarts4r charts:

# df <- ...
txt <- df |> 
#   group_by(year) |> 
    e_charts(x) |> #, timeline = TRUE) |>   # lotties can't be used in timeline (yet)
    e_scatter(y) |> 
    e_loess(y ~ x) |> 
    e_inspect(json=TRUE, pretty=TRUE)
p4 <- ec.fromJson(txt)

json <- 'https://raw.githubusercontent.com/airbnb/lottie-ios/edf0afcfaf7e0bd37f9311a3502b2981aa8a37f2/Tests/Samples/PinJump.json'
cont <- jsonlite::fromJSON(json, simplifyDataFrame=FALSE)
# set only graphic option in echarty
py <- ec.init(preset=FALSE,
    load='https://helgasoft.github.io/echarty/js/lottie-parser.js',
    graphic= list(elements= list(
         list( type= "group", 
            # lottie params = info + optional (scale, loop, etc.)
            info= cont, scale= 250, # loop= FALSE,
            left= 'center', bottom= '20%'  #,rotation= -20
         )
    )) )
# merge p4 & py options
py$x$opts <- append(py$x$opts, p4$x$opts)
py

image

rdatasculptor commented 2 years ago

Yesssssss! It opens a whole new world to explore (at least for me it does).

Thanks again @helgasoft!! This is very very inspiring. (And thank you for the technical explanation of crosstalk)