JavaScript#

编写JSON格式图数据并使用D3 JavaScript库生成HTML/JavaScript绘图的示例。

您需要下载以下目录:

import json

import flask
import networkx as nx

G = nx.barbell_graph(6, 3)
# 这个d3示例使用name属性作为鼠标悬停时的值,
# so add a name to each node
for n in G:
    G.nodes[n]["name"] = n
# 写入JSON格式数据
d = nx.json_graph.node_link_data(G)  # 节点-链接格式以序列化
# write json
json.dump(d, open("force/force.json", "w"))
print("Wrote node-link JSON data to force/force.json")

# 通过HTTP提供文件以允许跨源请求
app = flask.Flask(__name__, static_folder="force")


@app.route("/")
def static_proxy():
    return app.send_static_file("force.html")


print("\nGo to http://localhost:8000 to see the example\n")
app.run(port=8000)

Gallery generated by Sphinx-Gallery