Visualizing complex structures using Graphviz
|
Data Structures as Hashes
In programming, data structures often come in the form of hashes, such as associative arrays [7]. Graphviz builds these structures without much fuss if you use the correct syntax. Line 2 in Listing 4 specifies that you want a directed graph – the LR determines the direction from left to right.
Listing 4
Create a Data Structure
01 digraph G { 02 rankdir=LR; 03 node [shape=record, width=0.1, height=0.1]; 04 05 node0 [label = "<f0> | <f1> | <f2>", height=1.5]; 06 node [width=1.5]; 07 node1 [label = "{<n> n14 | 719 | <p>}"]; 08 node2 [label = "{<n> k71 | 216 | <p>}"]; 09 node3 [label = "{<n> n39 | 771 | <p>}"]; 10 node4 [label = "{<n> k56 | 250 | <p>}"]; 11 node5 [label = "{<n> a34 | 125 | <p>}"]; 12 13 node0:f0 -> node1:n; 14 node1:p -> node2:n; 15 node0:f1 -> node3:n; 16 node0:f2 -> node4:n; 17 node4:p -> node5:n; 18 }
Lines 5 through 11 define the content of the nodes as additional structures and later access the separate elements by name. The node0 in line 5 has three elements (f0 , f1 , and f2 ), whereas node1 through node5 in lines 7 through 11 each have the two elements n and p . The middle element has no identifier, only the assigned value. Linking the nodes occurs in lines 3 through 17, and Graphviz takes over from there (Figure 4).
Dependencies
The next example comes directly from the engine room of a Linux system. With the Debtree [8] program, you can explore the dependencies between packages on a Debian-based system (e.g., Ubuntu). The parameters are the names of one or more packages – for simplicity's sake, I'll use names like sqlite3 for the SQLite3 [9] database.
The program provides the result in dot format. The call in Listing 5 creates the listing on the command line and passes the results to the sqlite3.dot file (Figure 5). From this, you generate an image file with dot (Figure 6).
Listing 5
Generate an Image File
01 $ debtree sqlite3 > sqlite3.dot 02 $ dot -Tpng sqlite3.dot > sqlite3.png
« Previous 1 2 3 4 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)