2016-10-26 8 views
3

いくつかのDOTグラフを表示するためにsigma.jsを使用したいと思います。しかし、sigma.jsはjsonグラフ形式しかサポートしていないようです。ドットグラフをjsonグラフに変換する方法は?

いくつかのbashツールやjavascriptモジュールでDOTグラフをjsonグラフに変換できますか?

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

JSONグラフへの転送::DOTグラフから例えば

{ 
 
    "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" 
 
    } 
 
    ] 
 
}

答えて

2

あなたはPythonとINSTALを使用できる場合L 2つのパッケージ(networkxpygraphviz)、ここでは、JSONグラフにドットグラフを変換する短いスクリプトである:ここ

# 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 
# 
# Syntax : 
# python dot_to_json_graph.py graph.dot 

import networkx as nx 
from networkx.readwrite import json_graph 

import sys 

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

はあなたの例であり、JSONグラフに変換:

は{ "向き" : "グラフ":["ノード"、{"ラベル": "}}、"グラフ "、 {"ファイル ":" test.dot "}]、["エッジ "、{}] 、 "ラベル": "n1"}、{"id": "n2"、 "Label": "n2"、 "ラベル": "n2" {{"source":0、 "target":1、 "key":0}、 {"id": "n3"、 "ラベル": "n3"}] "source":0、 "tar" "key":0}]、 "multigraph":true}

+0

ありがとうございました! – PokerFace

関連する問題