用于ArangoDB pyArango图形绘制API

fcwjkofz  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(160)

我正在使用ArangoDB社区版,我可以在AQL中查询创建的图,并在JSON中获得结果,这在ArangoDB Web接口工具上以图形方式可视化。
查询

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界面中获得可视化的图形输出,我想在语言ArangoDB中显示相同的<->图形输出。这里的语言可以是支持的驱动程序语言:Python、Java、C#等
我正在使用pyArango与ArangoDB连接
我找不到一个ArangoDB API来获得JPG或matlibplot中的图形可视化。
除了使用以下两个选项外,是否还有其他方法?
1.使用networkx.draw(networkx.graph)

  1. matplotlib.pyplot
5n0oy7gb

5n0oy7gb1#

如果你需要图形可视化,那么Graphviz库就很适合你,如果Python可以,那么你只需要一个Python绑定库graphviz(它在内部使用DOT language表示)。
将Arango DB中的图表JSON输入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,可以使用良好的旧Pip进行安装:*
pip install graphviz

第3步:运行示例并享受

相关问题