2016-10-26 54 views
3

我想用sigma.js來顯示一些DOT圖。但似乎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圖:

{ 
 
    "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和安設升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圖:

{ 「定向」 :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}, { 「來源」:0,「tar get「:2,」key「:0},{」source「:1,」target「:1, 」key「:0}],」multigraph「:true}

+0

非常感謝! – PokerFace