2016-10-26 14 views
3

일부 DOT 그래프를 표시하기 위해 sigma.js를 사용하고 싶습니다. 그러나 sigma.js는 json 그래프 형식 만 지원합니다.점 그래프를 json 그래프로 변환하는 방법은 무엇입니까?

자바 스크립트 모듈은 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

당신이 파이썬을 사용하여 취임시키다 경우 DOT 그래프에서 예를 들어

리터 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 ":" "}", "그래프", { "file": "test.dot"}], [ "edge", {}] , "노드": [{ "id": "n1", "레이블": "n1"}, { "id": "n2", "레이블": "n2 {{ "source": 0, "target": 1, "key": 0}, { "id": "n3", "레이블": "n3"}] "source": 0, "tar" 012, "key": 0}], "multigraph": true}

+0

대단히 감사합니다. – PokerFace