XQuery/Graph Visualization with Graphviz

Graphviz developed by AT&T provides a package of code for generating graph images from a text definition.

The input file in 'dot' format can be generated by an XQuery script with text output.

Motivation
You want to create a graph to visualize complex structures such as taxonomies, object hierarchies or organizational hierarchies.

Database visualization
A graphical representation of the relationships between employee and manager in the empdept example.

This script generates the dot format file, with employees as (implicit ) nodes and arcs from employee to manager to show managed by relationships.

The output is serialised as text. Serialisation strips out all XML tags so XML can be used to structure the output and there is no need to serialise each item. The Graphviz dot format uses { } curly brackets as delimiters so these need to be escaped (doubled) in XQuery.

[/visualization/hierarchy-dot.xq   Generate dot file]

To process dot format files with SVG, we can use a module such as this on GitHub. Because this module uses the process:module, the script must run with db access rights.

[/visualization/hierarchy-svg.xq SVG image]

This would look more like a typical organisational chart if the graph was reversed. Graphviz provides a wide range of controls over the content and appearance of the graph.

[/visualization/hierarchy-inverted-svg.xq SVG graph]

Since Enames are not necessarily unique, it would be better to use the EmpNo as the node identifier and label the node with the name:

[/visualization/hierarchy2.xq SVG graph]

Similarly, the Department/Employee Hierarchy can be graphed:

[/visualization/empdept-svg.xq SVG graph]