echarts4r icon indicating copy to clipboard operation
echarts4r copied to clipboard

How to use "layout = 'none'" in e_graph() properly ?

Open ziyingy opened this issue 3 years ago • 5 comments

Hi, I am currently need to plot nodes with x and y coordinates in the graph. I set layout = 'none' in the e_graph(), and in the node dataframe:

nodes <- data.frame(
                    #x = co_x,    <---- if I uncomment these two line, no result/graph will show
                    #y = co_y,    <---- if I uncomment these two line, no result/graph will show
                    name = selected_name,
                    size = selected_value,
                    grp = selected_grp,
                    stringsAsFactors = FALSE
                )

where co_x is a list like c(20,30,40) and pass it to e_graph_nodes(nodes, name, value, size, grp) , no graph is shown. If I comment layout = 'none' (that is, layout = 'force'), everything works fine without coordinates.

But I need every node sits on its given position...Did I do something wrong? Please help. Cheers

ziyingy avatar Sep 03 '21 05:09 ziyingy

Sorry, that's not currently supported. Let me see if I can add support for it.

JohnCoene avatar Sep 08 '21 14:09 JohnCoene

Hi John! I encountered exactly the same problem. I tried to make a graph based on x and y coordinates, but it obviously failed. Any news about this one? :)

rdatasculptor avatar Mar 09 '22 16:03 rdatasculptor

@ziyingy I think we can use e_list(). But haven't succeeded yet in making the edges appear.

Something like this:

nodes <- data.frame(
  name = sample(LETTERS, 10),
  value = value,
  size = value,
  grp = rep(c("grp1", "grp2"), 5),
  symbol = sample(c("circle", "rect", "triangle"), 10, replace = TRUE),
  stringsAsFactors = FALSE
)

edges <- data.frame(
  source = sample(nodes$name, 20, replace = TRUE),
  target = sample(nodes$name, 20, replace = TRUE),
  stringsAsFactors = FALSE
)

opts <- list(
  xAxis = list(
    type = "category",
    data = nodes$name,
    boundaryGap = FALSE
  ),
  yAxis = list(
    type = "value"
  ),
  series = list(
    list(
      type = 'graph',
      layout = 'none',
      coordinateSystem = 'cartesian2d',
      symbolSize = 40,
      label = list(
        show = TRUE
      ),
      edgeSymbol = c('circle', 'arrow'),
      edgeSymbolSize = c(4, 10),
      data = nodes$value,
      links = list(source = edges$source, 
                   target = edges$target),
      lineStyle = list(
        color = '#2f4554'
      )
    )
  )
)

e_charts() %>%
  e_list(opts)

rdatasculptor avatar Mar 11 '22 08:03 rdatasculptor

Well, at least I succeeded in reproducing this one:

nodes <- jsonlite::fromJSON('
    [
        {
          "name": "Node 1",
          "x": 300,
          "y": 300
        },
        {
          "name": "Node 2",
          "x": 800,
          "y": 300
        },
        {
          "name": "Node 3",
          "x": 550,
          "y": 100
        },
        {
          "name": "Node 4",
          "x": 550,
          "y": 500
        }
      
    ]',
                            simplifyVector = FALSE,
                            simplifyDataFrame = FALSE,
                            simplifyMatrix = FALSE)
 links <- jsonlite::fromJSON('     
    [
        {
          "source": 0,
          "target": 1,
          "symbolSize": [5, 20],
          "label": {
            "show": true
          },
          "lineStyle": {
            "width": 5,
            "curveness": 0.2
          }
        },
        {
          "source": "Node 2",
          "target": "Node 1",
          "label": {
            "show": true
          },
          "lineStyle": {
            "curveness": 0.2
          }
        },
        {
          "source": "Node 1",
          "target": "Node 3"
        },
        {
          "source": "Node 2",
          "target": "Node 3"
        },
        {
          "source": "Node 2",
          "target": "Node 4"
        },
        {
          "source": "Node 1",
          "target": "Node 4"
        }
      
    ]',
                             simplifyVector = FALSE,
                             simplifyDataFrame = FALSE,
                             simplifyMatrix = FALSE)
 
 opts <- list(
   
   series = list(
     list(
       type = 'graph',
       layout = 'none',
       symbolSize= 50,
       roam = TRUE,
       label = list(
         show = TRUE
       ),
       edgeSymbol = c("circle", "arrow"),
       edgeSymbolSize = c(4, 10),
       edgeLabel = list(
         fontSize = 20
       ),
       data = nodes,
       links = links,
       lineStyle = list(
         color = '#2f4554'
       )
     )
   )
 )
 
 e_charts() %>%
   e_list(opts)

rdatasculptor avatar Mar 11 '22 10:03 rdatasculptor

https://github.com/rdatasculptor/e_diagrammer/blob/main/e_charts_diagrammer.R

rdatasculptor avatar Mar 12 '22 13:03 rdatasculptor