Graphviz

Graph Visualization Software

Autor: rubens
Data: 06/04/2009

Para mais informações sobre o Graphviz, veja http://www.graphviz.org/

Código graphviz

O código que cria o grafo é executado diretamente pelo Sandbox. Usamos, para tanto, a diretiva code indicando a linguagem graphviz. O conteúdo da diretiva é o texto correspondente ao arquivo de entrada '.dot'.

01. digraph test123 {
02.     fontname = "Comic Sans MS"
03.     fontsize = 10
04.     node [
05.         fontname = "Comic Sans MS"
06.         fontsize = 10
07.     ]
08.     edge [
09.         fontname = "Comic Sans MS"
10.         fontsize = 10
11.     ]
12.     a -> b -> c;
13.     a -> {x y};
14.     b [shape=box];
15.     c [label="Exemplo", color=blue, fontsize=24, fontname="Palatino-Italic",
16.                         fontcolor=red, style=filled];
17.     a -> z [label=" hi", weight=100];
18.     x -> z [label="multi-line\nlabel"];
19.     edge [style=dashed,color=red];
20.     b -> x;
21.     {rank=same; a y}
22. }
/media/_xsb/Ajuda/graphviz/GRVIZ91996_001.png

Usando o Python

Criando a visualização com a função mmgraphviz

Passamos o texto que cria o grafo para esta função, que retorna uma imagem.

01. dot_string = """
02.     digraph G {
03.         subgraph cluster1 {
04.             label="Parents";
05.             edge2 [label="Bob"];
06.             edge3 [label="Alice"];
07.         }
08.         subgraph cluster2 {
09.             label="Parents";
10.             edge21 [label="Bob"];
11.             edge31 [label="Alice"];
12.         }
13.         subgraph cluster4 {
14.             label="Children";
15.             edge5 [label="Carol", shape="box"];
16.             edge6 [label="Eve"];
17.             edge7 [label="Isaac"];
18.             edge6 -> edge5;
19.         }
20.         edge2 -> edge7;
21.         edge3 -> edge6;
22.         edge31 -> edge7;
23.         edge6 -> "???";
24.     }
25. """
26. 
27. mmgraphviz(dot_string, title='A Sample Graphic')
/media/_xsb/Ajuda/graphviz/GRVIZ91996_002.png

A Sample Graphic

Node como record:

01. dot_string = """
02.     digraph structs {
03.         node [shape=record];
04.         row0 [label="<0> 0|<f1> 1|<f2> 2"];
05.         row1 [label="<f0> 3|<f1> 4|<f2> 5"];
06.         row2 [label="<f0> 6|<f1> 7|<f2> 8"];
07.         row0:0 -> row1:f0;
08.         row1:f2 -> row2:f2;
09.         row2:f1 -> row2:f1;
10.         row1:f1 -> row1:f2;
11.     }
12.         """
13. 
14. mmgraphviz(dot_string, title='A Sample Graphic')
/media/_xsb/Ajuda/graphviz/GRVIZ91996_003.png

A Sample Graphic

Criando o grafo programaticamente

Criamos o programa do graphviz através da extensão Python gvgen.

01. import gvgen
02. 
03. graph = gvgen.GvGen()
04. graph.smart_mode = 1
05. 
06. graph.styleDefaultAppend("color", "blue")
07. 
08. parents = graph.newItem("Parents")
09. father = graph.newItem("Bob", parents)
10. mother = graph.newItem("Alice", parents)
11. children = graph.newItem("Children")
12. child1 = graph.newItem("Carol", children)
13. child2 = graph.newItem("Eve", children)
14. child3 = graph.newItem("Isaac", children)
15. postman = graph.newItem("Postman")
16. graph.newLink(father,child1)
17. graph.newLink(child1, father)
18. graph.newLink(father, child1)
19. graph.newLink(father,child2)
20. graph.newLink(mother,child2)
21. myl = graph.newLink(mother,child1)
22. graph.newLink(mother,child3)
23. graph.newLink(postman,child3,"Email is safer")
24. graph.newLink(parents, postman)    # Cluster link
25. 
26. graph.propertyForeachLinksAppend(parents, "color", "blue")
27. 
28. graph.propertyForeachLinksAppend(father, "label", "Red link")
29. graph.propertyForeachLinksAppend(father, "color", "red")
30. 
31. graph.propertyAppend(postman, "color", "red")
32. graph.propertyAppend(postman, "fontcolor", "white")
33. 
34. graph.styleAppend("link", "label", "mylink")
35. graph.styleAppend("link", "color", "green")
36. graph.styleApply("link", myl)
37. graph.propertyAppend(myl, "arrowhead", "empty")
38. 
39. graph.styleAppend("Post", "color", "blue")
40. graph.styleAppend("Post", "style", "filled")
41. graph.styleAppend("Post", "shape", "rectangle")
42. graph.styleApply("Post", postman)
43. 
44. import StringIO
45. fd = StringIO.StringIO()
46. graph.dot(fd)
47. dottext = fd.getvalue()
48. print dottext
49. mmgraphviz(dottext, title='A Sample Graphic')
/* Generated by GvGen v.0.9.1 (http://www.picviz.com/sections/opensource/gvgen.html) */

digraph G {
compound=true;
   subgraph cluster1 {
   color="blue";
label="Parents";
      node3 [color="blue",label="Alice"];
      node2 [color="blue",label="Bob"];
   }
   subgraph cluster4 {
   color="blue";
label="Children";
      node7 [color="blue",label="Isaac"];
      node6 [color="blue",label="Eve"];
      node5 [color="blue",label="Carol"];
   }
   node8 [style="filled",color="red",fontcolor="white",shape="rectangle",label="Postman"];
node3->node8 [color="blue",arrowsize="0.8",penwidth="1.2",ltail=cluster1];
node3->node6 [arrowsize="0.8",penwidth="1.2"];
node3->node5 [color="green",arrowsize="0.8",penwidth="1.2",arrowhead="empty",label="mylink"];
node3->node7 [arrowsize="0.8",penwidth="1.2"];
node2->node5 [color="red",arrowsize="0.8",penwidth="1.2",dir="both",label="Red link"];
node2->node6 [color="red",arrowsize="0.8",penwidth="1.2",label="Red link"];
node8->node7 [arrowsize="0.8",label="Email is safer",penwidth="1.2"];
}
/media/_xsb/Ajuda/graphviz/GRVIZ91996_004.png

A Sample Graphic