简体   繁体   中英

graph plot API for ArangoDB pyArango

I am using ArangoDB community edition, I can query on a created graph in AQL and get results in JSON which is graphically visualized on ArangoDB web interface tool.

AQL Query

FOR v,e,p IN 1..3 OUTBOUND 'germanCity/Hamburg' GRAPH 'routeplanner' 
OPTIONS{bfs :true} 
RETURN p

JSON output

[
  {
    "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
        ]
      }
    ]
  }
]

Equivalent Graph

图表查看器

As we can get visualized graph output in web interface I would like to display the same in Language<->ArangoDB. Language here can be driver language supported : Python, Java , C# etc.

I am using pyArango for interfacing with ArangoDB

I couldnt find an ArangoDB API for getting this graph visualization in JPG or matlibplot.

Is there any other way apart from using below two options?

  1. Use networkx.draw(networkx.graph)
  2. matplotlib.pyplot

If you need graph visualization then Graphviz library is for you. And if Python is OK then you only need a Python binding-library graphviz (that utilizes DOT language representation internally.)

It is super easy to feed your graph JSON from Arango DB to graphviz for rendering.

You can customize it to your style, add labels, colors, reshape nodes and more.

Here is a simple example of your sample 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()

Just 3 code lines for customization and 5 more to feed the graph visualization renderer. And please note that Arango Web UI does not render all of the edges between the same pair of nodes while graphviz does and you can style each differently.

You will need to install graphviz library and Python bindings

Step #1: Install library

Assuming your machine is Ubuntu:

sudo apt install graphviz

Step #2: Get Python bindings

pipenv install graphviz

If you not yet using Pipenv you can install with good old Pip :

pip install graphviz

Step #3: Run sample and enjoy

惊人的图形,以 JPEG 格式呈现

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM