Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform a dot graph to json graph?

Tags:

graphviz

dot

I want to use sigma.js to show some DOT graph. But it seems that sigma.js only support json graph format.

Is there some bash tools or javascript module can transform a DOT graph to json graph?

For example from DOT graph:

graph {
 n1 [Label = "n1"];
 n2 [Label = "n2"];
 n3 [Label = "n3"];
 n1 -- n2;
 n1 -- n3;
 n2 -- n2;
}

Transfer to JSON graph:

{
  "nodes": [
    {
      "id": "n0",
      "label": "A node",
      "x": 0,
      "y": 0,
      "size": 3
    },
    {
      "id": "n1",
      "label": "Another node",
      "x": 3,
      "y": 1,
      "size": 2
    },
    {
      "id": "n2",
      "label": "And a last one",
      "x": 1,
      "y": 3,
      "size": 1
    }
  ],
  "edges": [
    {
      "id": "e0",
      "source": "n0",
      "target": "n1"
    },
    {
      "id": "e1",
      "source": "n1",
      "target": "n2"
    },
    {
      "id": "e2",
      "source": "n2",
      "target": "n0"
    }
  ]
}
like image 534
PokerFace Avatar asked Oct 26 '16 12:10

PokerFace


3 Answers

dot -Txdot_json -ogrpaph.json graph.dot

See https://www.graphviz.org/doc/info/output.html#a:xdot_json

like image 78
Ilya Klyuchnikov Avatar answered Sep 29 '22 15:09

Ilya Klyuchnikov


If you can use python and install 3 packages (networkx and pygraphviz and pydot), here is a short script to convert a dot graph to json graph:

# dot_to_json_graph.py
# http://stackoverflow.com/questions/40262441/how-to-transform-a-dot-graph-to-json-graph

# Packages needed  :
# sudo aptitude install python-networkx python-pygraphviz python-pydot
#
# Syntax :
# python dot_to_json_graph.py graph.dot

import networkx as nx
from networkx.readwrite import json_graph

import sys
import json

if len(sys.argv)==1:
  sys.stderr.write("Syntax : python %s dot_file\n" % sys.argv[0])
else:
  dot_graph = nx.nx_pydot.read_dot(sys.argv[1])
  print(json.dumps(json_graph.node_link_data(dot_graph)))

Here is your example, converted to json graph:

{"directed": false, "graph": [["node", {"Label": ""}], ["graph",
{"file": "test.dot"}], ["edge", {}], ["name", ""]], "nodes": [{"id":
"n1", "Label": "n1"}, {"id": "n2", "Label": "n2"}, {"id": "n3",
"Label": "n3"}], "links": [{"source": 0, "target": 1, "key": 0},
{"source": 0, "target": 2, "key": 0}, {"source": 1, "target": 1,
"key": 0}], "multigraph": true}
like image 21
Eric Duminil Avatar answered Sep 29 '22 16:09

Eric Duminil


I could not install pygraphviz both in Windows and Linux, and I've ran in the same problem recently. networkx now have support to dumping into json more easily, including d3.js formats (info in this page)

I've come with two workarounds:

1- Use PyDot

Likely to be outdated soon! PyDot may not be used so frequently now that we have graphviz interface in python.

def dot_to_json(file_in):
    import networkx
    from networkx.readwrite import json_graph
    import pydot
    graph_netx = networkx.drawing.nx_pydot.read_dot(file_in)
    graph_json = json_graph.node_link_data( graph_netx )
    return json_graph.node_link_data(graph_netx)


2- Use graphviz Source

As shown in this part of the graphviz interface documentation, you can create a graph from a .dot source file.

like image 38
Victor Silva Avatar answered Sep 29 '22 16:09

Victor Silva