using a graphml file for d3.js force-directed layout using a graphml file for d3.js force-directed layout json json

using a graphml file for d3.js force-directed layout


I will recommend that you convert your graphml file in a json file. I ran into the same issue and here's how I convert a graphml file in json using python and the networkx library:

import networkx as nximport jsonfrom networkx.readwrite import json_graphimport reG=nx.read_graphml('YourFile.graphml', unicode)data = json_graph.node_link_data(G)for node in data['nodes']:  str=node['id']  node['id']=[int(s) for s in str.split("n") if s.isdigit()][0]with open('YourFile.json', 'w') as f:  json.dump(data, f, indent=4)

The for loop in the code converts your node ids into numbers. This is very important since the sources and target in the json final file are already numbers (not characters).


Following on from @fccoelho, Anders Eriksen (anderser) has put together an example of converting GraphML XML to d3 JSON format with Python, here (GitHub gist)

convert.py <- GraphML-to-d3_JSON

The Python script takes advantage of the Python-louvain and Networkx.github.io libraries.