---+!! Overview of directed graph editing
%TOC%
---++ Introduction
Support for creating advanced directed graphs has been installed on this TWiki installation. Directed graphs are rendered by [[http://www.graphviz.org][Graphviz]]. Only the most important basics are covered here, you should consult the dotguide.pdf from Graphviz's homepage for a more thourough guide on creating directed graphs.
---++ What are directed graphs?
|
*The graph:*
digraph G {
a [style=filled, color=green];
b [style=filled, color=red];
a -> b [style=dotted, color=blue];
}
|
*The code:*
digraph G {
a [style=filled, color=green];
b [style=filled, color=red];
a -> b [style=dotted, color=blue];
}
|
*Explanation:*
This example is very similar to the above example, with the difference that we have defined some attributes to both the nodes and the edge. As you can see the nodes are filled with green and red, respective. The edge has been changed to have a dotted line instead of a solid line.
If you want to assign attributes to nodes, the nodes have to be defined first, which means they appear on a line on their own. The order of node and edge definitions is not important, the whole file is parsed before any layout is begun.
The first line in the graph a [style=filled, color=green]; defines the node =a= and assigns the attributes style=filled and color=green. These two attributes make the node filled with the color green.
|
As you can see to assign attributes to a node you just type its name (with the optional quotation marks) followed by the attribute definitions separated by commas, as it appears in the example above.
To add attributes to edges (i.e. arrows) just add the attributes to the line of the edge definition as can be seen on the last line of the graph definition.
---+++ Adding labels to edges
|
*The graph:*
digraph G {
size="2.5,2";
{
node [shape=plaintext, fontsize=16];
1975 -> 1980 -> 1985;
}
{ rank=same; 1975; a; b; c; }
{ rank=same; 1980; d; e; }
{ rank=same; 1985; f; g; h; i; }
a -> d;
b -> e;
c -> e;
d -> f;
e -> g;
e -> h;
e -> i;
}
|
*The code:*
digraph G {
{
node [shape=plaintext, fontsize=16];
1975 -> 1980 -> 1985;
}
{ rank=same; 1975; a; b; c; }
{ rank=same; 1980; d; e; }
{ rank=same; 1985; f; g; h; i; }
a -> d;
b -> e;
c -> e;
d -> f;
e -> g;
e -> h;
e -> i;
}
|
*Explanation:*
In this graph three groups are defined, by putting them inside a block, defined by ={ }=.
The first group defines the nodes =1975=, =1980= and =1985=, by connecting them together; these nodes are of the type ="plaintext"= with font size equal to 16. We do this because the years are to be placed to the left as a timeline and therefore the font size is choosen to be a little larger.
The next three groups for example { rank=same; 1975; a; b; } ensures that the nodes =1975=, =a= and =b= are on the same level, of course, because =a= and =b= occured in 1975 (_It is left as an exercise to the reader to figure out what a and b actually is_).
Following the last group definition we define all edges.
|
---+++ Node types
Choose the shape of a node by adding the attribute =shape= to the node definition.
|
digraph G {
" " [shape=box];
}
box
|
digraph G {
" " [shape=polygon];
}
polygon
|
digraph G {
" " [shape=ellipse];
}
ellipse
|
digraph G {
" " [shape=circle];
}
circle
|
|
digraph G {
" " [shape=point];
}
point
|
digraph G {
" " [shape=egg];
}
egg
|
digraph G {
" " [shape=triangle];
}
triangle
|
digraph G {
plaintext [shape=plaintext];
}
plaintext
|
|
digraph G {
" " [shape=diamond];
}
diamond
|
digraph G {
" " [shape=trapezium];
}
trapezium
|
digraph G {
" " [shape=parallelogram];
}
parallelogram
|
digraph G {
" " [shape=house];
}
house
|
|
digraph G {
" " [shape=hexagon];
}
hexagon
|
digraph G {
" " [shape=octagon];
}
octagon
|
digraph G {
" " [shape=doublecircle];
}
doublecircle
|
digraph G {
" " [shape=doubleoctagon];
}
doubleoctagon
|
|
digraph G {
" " [shape=tripleoctagon];
}
tripleoctagon
|
digraph G {
" " [shape=invtriangle];
}
invtriangle
|
digraph G {
" " [shape=invtrapezium];
}
invtrapezium
|
digraph G {
" " [shape=invhouse];
}
invhouse
|
|
digraph G {
" " [shape=Mdiamond];
}
Mdiamond
|
digraph G {
" " [shape=Msquare];
}
Msquare
|
digraph G {
" " [shape=Mcircle];
}
Mcircle
|
---+++ Arrow types
The form of the edge can be set by using the =arrowtail= and =arrowhead= attributes with edges. The attribute =arrowtail= sets the shape of the arrow at the source node, while =arrowhead= sets the shape of the arrow at the destination node.