How to transform a dot graph to json graph?
Asked Answered
L

3

9

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"
    }
  ]
}
Liability answered 26/10, 2016 at 12:33 Comment(0)
A
9

dot -Txdot_json -ogrpaph.json graph.dot

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

Arak answered 20/7, 2018 at 13:22 Comment(2)
sudo apt-get install graphviz -- To get dot tools on your Ubuntu machine, maybe it helps someone.Mott
the output does not seem networkx-compatible... (KeyError: 'nodes')Cockroach
S
6

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
# https://mcmap.net/q/1137302/-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}
Sniperscope answered 27/10, 2016 at 8:53 Comment(0)
E
5

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.

Expiratory answered 26/4, 2018 at 10:35 Comment(1)
Thanks! Tried converting using graphviz, but turns out not all output formats are always supported. dot_to_json worked perfectly though.Gide

© 2022 - 2024 — McMap. All rights reserved.