4

我正在使用 ArangoDB 社区版,我可以查询创建的图形AQL并以 JSON 格式获取结果,该结果在 ArangoDB Web 界面工具上以图形方式可视化。

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 交互

我找不到用于在 JPG 或 matlibplot 中获取此图形可视化的 ArangoDB API。

除了使用以下两个选项之外,还有其他方法吗?

  1. 采用networkx.draw(networkx.graph)
  2. matplotlib.pyplot
4

1 回答 1

4

如果您需要图形可视化,那么Graphviz库适合您。如果 Python 没问题,那么您只需要一个 Python 绑定库graphviz(它在内部使用DOT 语言表示。)

将您的图形 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,您可以使用旧的Pip进行安装:

pip install graphviz

第 3 步:运行示例并享受

惊人的图形,以 JPEG 呈现

于 2018-05-03T10:59:46.703 回答