Graphviz calculates flexible graphs

Slashdot it! Delicious Share on Facebook Tweet! Digg!
© jaroas - freeimages.com

© jaroas - freeimages.com

Arrangements

Instead of toiling over a graphics layout, you can use Graphviz to concentrate on the essential content and let the software do the design.

The more elements a graph contains, the more complicated the placement of nodes. Instead of working on your project into the night, why not pass the task on to the computer – equipped with Graphviz [1] – to do the perfect job? Graphviz is based on an open format, called DOT, which uses simple text files.

Most distributions already have Graphviz in their repositories. With Ubuntu, you install the software by using the apt-get install graphviz command.

Basics

Listing 1 shows the basis for a simple diagram. The first line defines the diagram by name (G ). All components enclosed in curly brackets are related to this diagram – in this case, the connections for a tree diagram. End each line with a semicolon.

Listing 1

Simple Diagram

digraph G {
   DOT -> "Organizational Diagram";
   DOT -> "Arrow Diagram";
   DOT -> "Mind Maps";
   DOT -> "Network Plans";
}

Graphviz includes a renderer that creates the diagrams. It calculates the best order the individual elements should take. The standard renderers are dot , neato , fdp , circo , and twopi . The result depends on which renderer you choose.

The software supports a number of output formats. The most common are EPS, JPG, PNG, SVG, and PDF. You can get the complete list with dot -T? . If you select svg , you can edit the result again in a drawing program.

The actual conversion is achieved with a simple command. Figure 1 shows the result of the following command:

Figure 1: A simple command converts the entries in Listing 1 to a diagram.
$ dot -T png -o fig01-diagram.png fig01-diagram.dot

The description language essentially knows three objects with which you can create a diagram (Table 1). Properties that you set for these objects apply to all objects – provided you give the property a new value in each case.

Table 1

Objects

graph A directed graph is called a digraph ; an undirected graph is designated a graph . Every graph can contain further graphs.
node Nodes that are connected in a certain way. You don't need to define a node in advance; it gets defined during its first use.
edge Link between two nodes. A directed link occurs when two nodes connect with a -> .

Labeling

By default, Graphviz uses the name of the node as its label. Node names can't have just any characters, and they especially cannot contain special characters. If you want to use space or special characters or breaks, then use label to define the label and enclose the content in quotes. Specify a break with the \n escape sequence. The code in Listing 2 shows an overview of the sequence of schools in the United States (Figure 2).

Figure 2: Chart of educational flow in the US (more or less).

Listing 2

Educational Flow in the United States

digraph G {
    PK  [label="Pre-school"];
    P   [label="Primary"];
    S   [label="Secondary"];
    VT  [label="Vocational / Technical"];
    CC  [label="Community College"];
    UP  [label="Undergraduate Programs"];
    MD  [label="Master's Degree"];
    PS  [label="Professional Schools"];
    PHD [label="Doctoral Studies"];
    PDS [label="Postdoctoral Studies / Research"];
    PK->P;
    P->S;
    S->VT;S->CC;S->UP;
    CC->UP;
    UP->MD;UP->PS;
    MD->PHD;
    PS->PHD;PS->PDS;
    PHD->PDS;
}

The result isn't necessarily optimal; it would have been better to flow from left to right rather than top to bottom. The rankdir attribute of the graph object sets the chart's orientation. The values for it are TB (top to bottom), LR (left to right), RL (right to left), and BT (bottom to top).

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content