ArangoDB pyArango 的图表图 API
graph plot API for ArangoDB pyArango
我正在使用 ArangoDB 社区版,我可以查询 AQL
中创建的图形,并在 JSON 中获得结果,这些结果在 ArangoDB 网络界面工具上以图形方式可视化。
AQL
查询
FOR v,e,p IN 1..3 OUTBOUND 'germanCity/Hamburg' GRAPH 'routeplanner'
OPTIONS{bfs :true}
RETURN p
JSON输出
[
{
"edges": [
{
"_key": "6392826",
"_id": "germanHighway/6392826",
"_from": "germanCity/Hamburg",
"_to": "germanCity/Cologne",
"_rev": "_WmZ77pW--D",
"distance": 500
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Cologne",
"_id": "germanCity/Cologne",
"_rev": "_WmZ77Y6--B",
"population": 1000000,
"isCapital": false,
"loc": [
50.9364,
6.9528
]
}
]
},
{
"edges": [
{
"_key": "6392840",
"_id": "internationalHighway/6392840",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Paris",
"_rev": "_WmZ77pa--_",
"distance": 900
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Paris",
"_id": "frenchCity/Paris",
"_rev": "_WmZ77Z---D",
"population": 4000000,
"isCapital": true,
"loc": [
48.8567,
2.3508
]
}
]
},
{
"edges": [
{
"_key": "6392843",
"_id": "internationalHighway/6392843",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Lyon",
"_rev": "_WmZ77pa--B",
"distance": 1300
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Lyon",
"_id": "frenchCity/Lyon",
"_rev": "_WmZ77Z---B",
"population": 80000,
"isCapital": false,
"loc": [
45.76,
4.84
]
}
]
}
]
等效图
因为我们可以在 Web 界面中获得可视化图形输出,所以我想在 Language<->ArangoDB 中显示相同的内容。
这里的语言可以是支持的驱动语言:Python、Java、C#等
我正在使用 pyArango
与 ArangoDB 交互
我找不到 ArangoDB API 来获得 JPG 或 matlibplot 中的图形可视化。
除了使用以下两个选项,还有其他方法吗?
- 使用
networkx.draw(networkx.graph)
matplotlib.pyplot
如果您需要图形可视化,则在内部 Graphviz library is for you. And if Python is OK then you only need a Python binding-library graphviz (that utilizes DOT language 表示。)
将您的图表 JSON 从 Arango DB 提供给 graphviz 进行渲染非常容易。
您可以根据自己的风格对其进行自定义,添加标签、颜色、重塑节点等。
这是您的样本的一个简单示例 JSON:
from graphviz import Digraph
arango_graph = [
{
"edges": [
{
"_key": "6392826",
"_id": "germanHighway/6392826",
"_from": "germanCity/Hamburg",
"_to": "germanCity/Cologne",
"_rev": "_WmZ77pW--D",
"distance": 500
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Cologne",
"_id": "germanCity/Cologne",
"_rev": "_WmZ77Y6--B",
"population": 1000000,
"isCapital": False,
"loc": [
50.9364,
6.9528
]
}
]
},
{
"edges": [
{
"_key": "6392840",
"_id": "internationalHighway/6392840",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Paris",
"_rev": "_WmZ77pa--_",
"distance": 900
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Paris",
"_id": "frenchCity/Paris",
"_rev": "_WmZ77Z---D",
"population": 4000000,
"isCapital": True,
"loc": [
48.8567,
2.3508
]
}
]
},
{
"edges": [
{
"_key": "6392843",
"_id": "internationalHighway/6392843",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Lyon",
"_rev": "_WmZ77pa--B",
"distance": 1300
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Lyon",
"_id": "frenchCity/Lyon",
"_rev": "_WmZ77Z---B",
"population": 80000,
"isCapital": False,
"loc": [
45.76,
4.84
]
}
]
}
]
graph_name = 'amazing'
g = Digraph(graph_name, filename=graph_name, format='jpeg', engine='neato')
g.attr(scale='2', label='Look at my graph my graph is amazing!', fontsize='18')
g.attr('node', shape='circle', fixedsize='true', width='1')
for item in arango_graph:
for vertex in item['vertices']:
g.node(vertex['_id'], label=vertex['_key'])
for edge in item['edges']:
g.edge(edge['_from'], edge['_to'], label=str(edge['distance']))
# Render to file into some directory
g.render(directory='/tmp/', filename=graph_name)
# Or just show rendered file using system default program
g.view()
只有 3 行代码用于自定义,还有 5 行用于提供图形可视化渲染器。请注意,Arango Web UI 不会渲染同一对节点之间的所有边,而 graphviz 会渲染,您可以为每个节点设置不同的样式。
您将需要安装 graphviz
库和 Python 绑定
第 1 步:安装库
假设你的机器是Ubuntu:
sudo apt install graphviz
步骤 #2:获取 Python 绑定
pipenv install graphviz
如果您还没有使用 Pipenv you can install with good old Pip:
pip install graphviz
第 3 步:运行 品尝并享受
我正在使用 ArangoDB 社区版,我可以查询 AQL
中创建的图形,并在 JSON 中获得结果,这些结果在 ArangoDB 网络界面工具上以图形方式可视化。
AQL
查询
FOR v,e,p IN 1..3 OUTBOUND 'germanCity/Hamburg' GRAPH 'routeplanner'
OPTIONS{bfs :true}
RETURN p
JSON输出
[
{
"edges": [
{
"_key": "6392826",
"_id": "germanHighway/6392826",
"_from": "germanCity/Hamburg",
"_to": "germanCity/Cologne",
"_rev": "_WmZ77pW--D",
"distance": 500
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Cologne",
"_id": "germanCity/Cologne",
"_rev": "_WmZ77Y6--B",
"population": 1000000,
"isCapital": false,
"loc": [
50.9364,
6.9528
]
}
]
},
{
"edges": [
{
"_key": "6392840",
"_id": "internationalHighway/6392840",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Paris",
"_rev": "_WmZ77pa--_",
"distance": 900
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Paris",
"_id": "frenchCity/Paris",
"_rev": "_WmZ77Z---D",
"population": 4000000,
"isCapital": true,
"loc": [
48.8567,
2.3508
]
}
]
},
{
"edges": [
{
"_key": "6392843",
"_id": "internationalHighway/6392843",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Lyon",
"_rev": "_WmZ77pa--B",
"distance": 1300
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": false,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Lyon",
"_id": "frenchCity/Lyon",
"_rev": "_WmZ77Z---B",
"population": 80000,
"isCapital": false,
"loc": [
45.76,
4.84
]
}
]
}
]
等效图
因为我们可以在 Web 界面中获得可视化图形输出,所以我想在 Language<->ArangoDB 中显示相同的内容。 这里的语言可以是支持的驱动语言:Python、Java、C#等
我正在使用 pyArango
与 ArangoDB 交互
我找不到 ArangoDB API 来获得 JPG 或 matlibplot 中的图形可视化。
除了使用以下两个选项,还有其他方法吗?
- 使用
networkx.draw(networkx.graph)
matplotlib.pyplot
如果您需要图形可视化,则在内部 Graphviz library is for you. And if Python is OK then you only need a Python binding-library graphviz (that utilizes DOT language 表示。)
将您的图表 JSON 从 Arango DB 提供给 graphviz 进行渲染非常容易。
您可以根据自己的风格对其进行自定义,添加标签、颜色、重塑节点等。
这是您的样本的一个简单示例 JSON:
from graphviz import Digraph
arango_graph = [
{
"edges": [
{
"_key": "6392826",
"_id": "germanHighway/6392826",
"_from": "germanCity/Hamburg",
"_to": "germanCity/Cologne",
"_rev": "_WmZ77pW--D",
"distance": 500
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Cologne",
"_id": "germanCity/Cologne",
"_rev": "_WmZ77Y6--B",
"population": 1000000,
"isCapital": False,
"loc": [
50.9364,
6.9528
]
}
]
},
{
"edges": [
{
"_key": "6392840",
"_id": "internationalHighway/6392840",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Paris",
"_rev": "_WmZ77pa--_",
"distance": 900
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Paris",
"_id": "frenchCity/Paris",
"_rev": "_WmZ77Z---D",
"population": 4000000,
"isCapital": True,
"loc": [
48.8567,
2.3508
]
}
]
},
{
"edges": [
{
"_key": "6392843",
"_id": "internationalHighway/6392843",
"_from": "germanCity/Hamburg",
"_to": "frenchCity/Lyon",
"_rev": "_WmZ77pa--B",
"distance": 1300
}
],
"vertices": [
{
"_key": "Hamburg",
"_id": "germanCity/Hamburg",
"_rev": "_WmZ77Z---_",
"population": 1000000,
"isCapital": False,
"loc": [
53.5653,
10.0014
]
},
{
"_key": "Lyon",
"_id": "frenchCity/Lyon",
"_rev": "_WmZ77Z---B",
"population": 80000,
"isCapital": False,
"loc": [
45.76,
4.84
]
}
]
}
]
graph_name = 'amazing'
g = Digraph(graph_name, filename=graph_name, format='jpeg', engine='neato')
g.attr(scale='2', label='Look at my graph my graph is amazing!', fontsize='18')
g.attr('node', shape='circle', fixedsize='true', width='1')
for item in arango_graph:
for vertex in item['vertices']:
g.node(vertex['_id'], label=vertex['_key'])
for edge in item['edges']:
g.edge(edge['_from'], edge['_to'], label=str(edge['distance']))
# Render to file into some directory
g.render(directory='/tmp/', filename=graph_name)
# Or just show rendered file using system default program
g.view()
只有 3 行代码用于自定义,还有 5 行用于提供图形可视化渲染器。请注意,Arango Web UI 不会渲染同一对节点之间的所有边,而 graphviz 会渲染,您可以为每个节点设置不同的样式。
您将需要安装 graphviz
库和 Python 绑定
第 1 步:安装库
假设你的机器是Ubuntu:
sudo apt install graphviz
步骤 #2:获取 Python 绑定
pipenv install graphviz
如果您还没有使用 Pipenv you can install with good old Pip:
pip install graphviz
第 3 步:运行 品尝并享受