This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature

Create relationship diagrams with Graphviz

By Shashank Sharma on November 17, 2005 (8:00:00 AM)

Share    Print    Comments   

If you use charts to represent relationships between data or objects in presentations or project reports, try Graphviz. Licensed under the Common Public License Version 1.0, Graphviz is visualization software, designed to help you easily create structural information. You can use it to visually represent database and table relations in a project report or a simple Web site hierarchy.

Installing Graphviz is a breeze, especially if you are running a Debian-based distribution. Just run apt-get install graphviz. You can also download RPM or source files to set it up.

Graphviz comprises a collection of tools you can use to create graphics with lines or arrows between objects. Graphviz calls these undirected and directed graphics, respectively. One of these tools is dot, which accepts input in the C-like dot language. Dot is highly customizable, allowing you to control line colors, arrow heads, node shapes, and other items in your graphic.

Let's use Graphviz to print out a simple "Hello world!" The easiest way to do this is to use a text editor and write a dot-script:

graph hello
{
       Node1 [label="Hello, World!"]
}

The first word, graph, tells dot that the following lines contain a description of the graphic to be drawn. A graphic can have as many nodes as you like, each with a separate label. A label is used to write the text you want to appear in your node. Save this file as hello.dot. The command dot hello.dot -Tpng >hello.png generates the graphic. The -T switch tells dot the file format for the output. In this case, we tell dot to generate the output as .png and name it hello.png.

You can also create a directed graphic from the command line without using a text editor to create the dot file:

echo "digraph Hello {Hello->World!}" | dot -Tpng >hello.png

digraph tells dot to create a directed graphic. The above command creates two nodes with values "hello" and "world." If you want to create an undirectional graphic, simply remove the letters "di" from digraph.

A-grade graphics

Family.dot I wouldn't give an A-grade to the graphic we have made so far. For an A, a graphic must have colors and be a bit more complex. Graphviz can get you there.

Let's begin by drawing a four-member family hierarchy:

digraph family
{
	ElderSon [label="Elder Son"]
	YoungerSon [label="Younger Son"]

	PizzaHut [label="Pizza Hut"]
	McDonalds [label="McDonald's"]

  	Food -> PizzaHut
       	Food -> McDonalds
       	Father -> PizzaHut
       	Mom -> McDonalds
       	ElderSon -> McDonalds
	YoungerSon -> McDonalds
	Father -> Mom -> ElderSon -> YoungerSon

}

For dot to create our graphics, we don't need to explicitly define our nodes. Writing the node relations is enough. This graphic tells us two things: 1) the chain of command at home and their eating preferences, and 2) three people in a four family house prefer McDonald's over Pizza Hut.

When describing node relations, dot does not allow us to use blank spaces or apostrophes unless you describe the nodes first, as we have done above.

But this still still doesn't get an A because it lacks color. Let's try this instead:

digraph family1
{
       	Father [shape=Msquare, color=blue]
       	Mother [shape=box, style=filled, color=pink, height=0.7]
       	ElderSon [label="Elder Son", style=filled, color=green, width=1.5]
       	YoungerSon [label="Younger Son", shape=egg, color=Turquoise]

	PizzaHut [label="Pizza Hut"]
	McDonalds [label="McDonald's"]

	Food -> PizzaHut
       	Food -> McDonalds
       	YoungerSon -> McDonalds
       	Father -> PizzaHut
       	Mother -> McDonalds
       	ElderSon -> McDonalds

	edge [color = red]; /* denotes the family hierarchy */
       	Father -> Mother -> ElderSon -> YoungerSon
}

Here, we describe the attributes of all the nodes of the graphic before writing node relations. In addition to colors and shapes, you can also control a node's width and height. edge refers to the line connecting nodes.

We use a separate color to mark the members of the family. The edge attribute above does not apply to all node-relations. It is valid only for relations written after it in the dot script.

As you can see, dot itself rearranges the elements of our graphic to maintain clarity.

Even better graphics

Better family
Click to enlarge
Wait, there's more! Let's enter the details of some relatives and friends and see how we can make them stand out.

digraph family2
{
       	Father [shape=Msquare, color=blue]
       	Mother [shape=box, style=filled, color=pink, height=0.7]
      	ElderSon [label="Elder Son", style=filled, color=green, width=1.5]
       	YoungerSon [label="Younger Son", shape=egg, color=Turquoise]

	PizzaHut [label="Pizza Hut"]
	McDonalds [label="McDonald's"]

        subgraph cluster_relatives /*to describe entire group as single entity*/
        {
               	style = filled; /* defines the entire group */
                color = greenyellow
               	label = "Relatives"

               	UncleA [shape=plaintext]
               	AuntB [shape=plaintext]
       	}

       	Food -> PizzaHut
       	Food -> McDonalds
       	YoungerSon -> McDonalds
      	Father -> PizzaHut
       	Mother -> McDonalds
       	ElderSon -> McDonalds

       	UncleA -> PizzaHut
       	AuntB -> McDonalds

       	subgraph cluster_friends
       	{
               	style = filled; /* defines the entire group */
               	color = burlywood
               	label = "Friends"

               	FriendA [shape=plaintext]
               	NeighbourB [shape=plaintext]
       	}

       	Father -> FriendA [label="prefers pizza hut", fontcolor=darkgreen, fontsize=8]
       	Mother -> NeighbourB [label="prefers pizza hut", fontcolor=darkgreen, fontsize=8]

       	edge [color = red]; /* denotes the family hierarchy */
       	Father -> Mother -> ElderSon -> YoungerSon
}

The subgraph cluster elements are the new additions in this graphic. cluster means that the attributes apply to all elements of the subgraphic. Each node can still have its own unique attributes. Also, note the placement of the subgraphic clusters. You must always define a subgraphic cluster before describing relations of any of its elements.

Another new thing we see in this graphic is edge labels. See how "prefers pizza hut" appear next to the lines connecting Father-to-FriendA and Mother-to-NeighbourB. There are two ways to describe edge properties. You can write the properties in the same line as the relation, as we have done above, or you can describe a new edge.

edge [label="prefers pizza hut", fontcolor = darkgreen, fontsize=8]
Father -> FriendA
Mother -> NeighbourB

Read the fine dot guide (in PDF) if you have your eye on an A+ grade. Although Graphviz comes with a GUI, dotty, I find it far more efficient to use the command line to create my graphics.

Shashank Sharma is studying for a degree in computer science. He specializes in writing about free and open source software for new users.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on Create relationship diagrams with Graphviz

Note: Comments are owned by the poster. We are not responsible for their content.

Antialiasing?

Posted by: Anonymous Coward on November 17, 2005 08:47 PM
Doesnt it have antialiasing to smoothen out the edges?

Is there any GUI avaible?

#

Antialiasing & GUI?

Posted by: Anonymous Coward on November 18, 2005 12:39 AM
Re Antialiasing: The graphviz toolkit in general and dot in particular use graphical rendering libraries to generate their output. For example, several output formats including "png" and a few others use the <a href="http://www.boutell.com/gd/" title="boutell.com">GD graphics library</a boutell.com>. Since those underlying libraries provide the rendering services, that's where one would look for antialiasing. It may be that choosing a particular output format uses a library that has intrinsic antialiasing capability.

Re GUI: There are better tools for making diagrams such as kivio, Open/Star-Office draw, and several others whose names escape me at the moment. Dot was designed to generate graphs given input from some other tool. My current favorite example is <a href="http://www.stack.nl/~dimitri/doxygen/" title="stack.nl">the Doxygen code document helper/generator</a stack.nl>. So, while the article discusses using dot to generate graphs from our own input, its real value is in providing graph rendering services for other tools.

#

Re:Antialiasing?

Posted by: Anonymous Coward on November 18, 2005 12:30 PM
Antialiasing is available with the graphviz-cairo plugin, rpms currently available for Fedora Rawhide from graphviz.org.

#

Re:Antialiasing?

Posted by: Anonymous Coward on November 19, 2005 03:59 AM
You can export to ps using dot -Tps and convert
the resulting ps file to a png.

#

Re:Antialiasing?

Posted by: Administrator on November 18, 2005 01:37 AM
No particular format inheritly features anti-aliasing, but if you've got Gimp, it's not hard to anti-alias something.

First, provide the graphic at double the size you want it, or tripple, depending on what AA quality you want.

Then load Gimp. Open the file. Hit the right mouse button on it to get a menu, then go

image > scale

Scale it to exactly the size you originally wanted. So if you wanted a 500x500 file, and trippled it to 1500x1500, scale it down to 500x500. And it'll come out antialiased!

What happens is this; Each final pixel has the data of 9 pixels if you trippled, or 4 if you doubled. So it comes out looking much higher quality.

The 3Dfx Voodoo5 was the first consumer-class graphics card to do this in real time for games. They used the term FSAA, or full-scene anti-aliasing, or supersampling. This feature is standard on most modern cards, and on the Microsoft X-Box. The X-Box was the first major system to have full-scene antialiasing, though the Nintendo 64 had standard antialiasing capability all the way back in '95, which even the Playstation2 doesn't have.

#

Org charts and relationship diagrams

Posted by: Anonymous Coward on November 18, 2005 02:20 AM
If one wants to create this sort of thing without buying visio or learning to program this thing, such diagrams can be created easily with the draw component of Openoffice.org or the newer versions of Inkscape with connectors.

#

Re:Org charts and relationship diagrams

Posted by: Anonymous Coward on November 18, 2005 12:17 PM
such diagrams can be created easily with the draw component of Openoffice.org or the newer versions of Inkscape with connectors.
Yes, but Graphviz does layout for you, using any of several different algorithms.
Using Oo or Inkscape, you have to do the layout manually.
This isn't a problem with small graphs, but for more complicated (and frequently edited) graphs, automatic layout can be very helpful.
Also, Graphviz can output SVG, which can subsequently be edited with Inkscape, or imported into an Oo document.

Finally, Graphviz can produce graphs with richer graphical content than the examples given in this article.
See <a href="http://www.graphviz.org/Gallery.php" title="graphviz.org">this page</a graphviz.org> for examples of the kinds of graphs that it can generate.
I'd hate to have to do some of those by hand, even using Inkscape.

FYI, there are several Free and/or GPL diagramming programs available.
For people who want Visio-type functionality, there are <a href="http://www.gnome.org/projects/dia/" title="gnome.org">Dia</a gnome.org> for Gnome and <a href="http://www.koffice.org/kivio/" title="koffice.org">Kivio</a koffice.org> for KDE.
People who use Java can use <a href="http://www.jgraph.com/index.html" title="jgraph.com">JGraph</a jgraph.com>.
Other graphing programs that may be useful include <a href="http://www.xfig.org/" title="xfig.org">Xfig</a xfig.org> and <a href="http://freshmeat.net/projects/asy/" title="freshmeat.net">Asymptote</a freshmeat.net>.
(I have not used any of these programs extensively, so I can't recommend any one over the others.)
--
some guy I know

#

Re:Org charts and relationship diagrams

Posted by: Anonymous Coward on November 23, 2005 03:15 PM
The examples shown in this article are very small flowchart-types of graphs. Graphs can often consist of very large numbers of nodes. I frequently generate graphs with >1000 nodes, and tens of thousands of edges among them. I output the data directly from C++ programs, and use dot for visualization. I'd hate to try to do this by hand. See the GraphViz site for some very cool examples.

#

another cool trick for GraphViz:

Posted by: Administrator on November 18, 2005 03:08 PM
You can share graph sources on the Web. For example, here's the GraphViz source for the TCP state machine:

digraph G {

    subgraph P {

            label = "passive close";

        CLOSE_WAIT -> LAST_ACK;

    }

    subgraph A {

            label = "active close";

        FIN_WAIT_1 -> FIN_WAIT_2 -> TIME_WAIT;

        FIN_WAIT_1 -> CLOSING -> TIME_WAIT;

        FIN_WAIT_1 -> TIME_WAIT;

    }

    CLOSED -> LISTEN -> SYN_SENT -> ESTABLISHED -> FIN_WAIT_1;

    CLOSED -> SYN_SENT -> CLOSED;

    LISTEN -> SYN_RCVD -> LISTEN;

    SYN_SENT -> SYN_RCVD -> FIN_WAIT_1;

    SYN_RCVD -> ESTABLISHED -> CLOSE_WAIT;

    TIME_WAIT -> CLOSED;

    LAST_ACK -> CLOSED;
}

#

Stunning

Posted by: Administrator on November 18, 2005 01:24 PM
Really - wow! I was looking for something like this in Perl, and now I found that CPAN has a <a href="http://search.cpan.org/~lbrocard/GraphViz-2.02/lib/GraphViz.pm" title="cpan.org">Graphviz</a cpan.org> module as well. So now I can use Perl when I extract data from MySQL (as an example).


Cool<nobr> <wbr></nobr>:)


Thanks for his great review.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya