Debian Codegraph @

doc/libguide/basic.tex

raw
1 \section{Basic graph drawing}
2 Figure~\ref{fig:basic} gives a template for the basic library use of \gviz,
3 in this instance using the \dot\ hierarchical layout.
4 (Appendix~\ref{sec:simple} provides the listing of the complete program.)
5 Basically, the program creates a graph using the \graph\
6 library, setting node and edge
7 attributes to affect how the graph is
8 to be drawn; calls the layout code; and then uses the position information
9 attached to the nodes and edges to render the graph. The remainder of
10 this section explores these steps in more detail.
11
12 \begin{figure}[hbt]
13 \begin{verbatim}
14 Agraph_t* G;
15 GVC_t* gvc;
16
17 gvc = gvContext(); /* library function */
18 G = createGraph ();
19 gvLayout (gvc, G, "dot"); /* library function */
20 drawGraph (G);
21 gvFreeLayout(gvc, g); /* library function */
22 agclose (G); /* library function */
23 gvFreeContext(gvc);
24 \end{verbatim}
25 \caption{Basic use}
26 \label{fig:basic}
27 \end{figure}
28
29 Here, we just note the {\tt gvc} parameter. This is a handle
30 to a {\em Graphviz context}, which contains drawing and rendering
31 information independent of the properties pertaining to a particular
32 graph as well as various state information.
33 For the present, we view this an abstract parameter required
34 for various \gviz\ functions. We will discuss it further in
35 Section~\ref{sec:gvc}.
36
37 \subsection{Creating the graph}
38 The first step in drawing a graph is to create it. To use the \gviz\
39 layout software, the graph must be created using the \graph\ library.
40
41 We can create a graph in one of two main ways, using {\tt agread} or {\tt agopen}.
42 The former function takes a {\tt FILE*} pointer to a file open for reading.
43 \begin{verbatim}
44 FILE* fp;
45 Agraph_t* G = agread(fp, 0);
46 \end{verbatim}
47 It is assumed the file contains the description of graphs using the
48 \DOT\ language. The {\tt agread} function parses one graph at a time,
49 returning a pointer to an attributed graph generated from the input,
50 or {\tt NULL} if there are no more graphs or an error occurred.
51
52 The \gviz\ library provides several specialized variations of {\tt agread}. If
53 the \DOT\ representation of the graph is stored in memory at {\tt char* cp}, then
54 \begin{verbatim}
55 Agraph_t* G = agmemread(cp);
56 \end{verbatim}
57 can be used to parse the representation. By default, the {\tt agread} function relies on the
58 standard {\tt FILE} structure and {\tt fgets} function of the stdio library. You
59 can supply your own data source {\tt dp} coupled with your own discipline {\tt disc}
60 for reading the data to read a graph using
61 \begin{verbatim}
62 Agraph_t* G = agread(dp, &disc);
63 \end{verbatim}
64 Further details on using {\tt agread} and disciplines can be found in the \graph\ library manual.
65
66 The alternative technique is to call {\tt agopen}.
67 \begin{verbatim}
68 Agraph_t* G = agopen(name, type, &disc);
69 \end{verbatim}
70 The first argument is a {\tt char*} giving the name of the graph;
71 the second argument is an {\tt Agdesc\_t} value describing the type of graph
72 to be created. A graph can be directed or undirected. In
73 addition, a graph can be strict, i.e., have at most one edge between
74 any pair of nodes, or non-strict, allowing an arbitrary number of edges
75 between two nodes. If the graph is directed, the pair of nodes is ordered,
76 so the graph can have edges from node {\tt A} to node {\tt B} as well
77 as edges from {\tt B} to {\tt A}. These four combinations are specified
78 by the values in Table~\ref{table:types}.
79 The return value is a new graph, with no nodes or edges.
80 \begin{table*}[h]
81 \centering
82 \begin{tabular}{|l|l|} \hline
83 Graph Type & Graph \\ \hline
84 Agundirected & Non-strict, undirected graph \\
85 Agstrictundirected & Strict, undirected graph \\
86 Agdirected & Non-strict, directed graph \\
87 Agstrictdirected & Strict, directed graph \\ \hline
88 \end{tabular}
89 \caption{Graph types}
90 \label{table:types}
91 \end{table*}
92 So, to open a graph named {\tt "network"} that is directed but not strict, one would use
93 \begin{verbatim}
94 Agraph_t* G = agopen("network", Agdirected, 0);
95 \end{verbatim}
96 The third argument is a pointer to a discipline of functions used for reading, memory, etc.
97 If the value of 0 or NULL is used, the library uses a default discipline.
98
99 Nodes and edges are created by the functions {\tt agnode} and
100 {\tt agedge}, respectively.
101 \begin{verbatim}
102 Agnode_t *agnode(Agraph_t*, char*, int);
103 Agedge_t *agedge(Agraph_t*, Agnode_t*, Agnode_t*, char*, int);
104 \end{verbatim}
105 The first argument is the graph containing the node or edge. Note
106 that if this is a subgraph, the node or edge will also belong to
107 all containing graphs. The second argument to {\tt agnode} is the
108 node's name. This is a key for the node within the graph. If
109 {\tt agnode} is called twice with the same name, the second invocation
110 will not create a new node but simply return a pointer to the previously
111 created node with the given name. The third argument specifies whether or not
112 a node of the given name should be created if it does not already exist.
113
114 Edges are created using {\tt agedge} by passing in the edge's two nodes.
115 If the graph is not strict, additional calls to {\tt agedge} with
116 the same arguments will create additional edges between the two nodes.
117 The string argument allows you to supply a further name to distinguish between
118 edges with the same head and tail.
119 If the graph is strict, extra calls will simply return the already existing
120 edge.
121 For directed graphs, the first and
122 second node arguments are taken to be the tail and head
123 nodes, respectively. For undirected graph, they still play this role
124 for the functions {\tt agfstout} and {\tt agfstin}, but when checking
125 if an edge exists with {\tt agedge} or {\tt agfindedge}, the order is
126 irrelevant. As with {\tt agnode}, the final argument specifies whether or not
127 the edge should be created if it does not yet exist.
128
129 As suggested above, a graph can also contain subgraphs. These are
130 created using {\tt agsubg}:
131 \begin{verbatim}
132 Agraph_t *agsubg(Agraph_t*, char*, int);
133 \end{verbatim}
134 The first argument is the immediate parent graph; the second argument
135 is the name of the subgraph; the final argument indicates if the subgraph should
136 be created.
137
138 Subgraphs play three roles in \gviz.
139 First, a subgraph can be used to represent graph structure, indicating that
140 certain nodes and edges should be grouped together. This is the usual
141 role for subgraphs and typically specifies semantic information about
142 the graph components. In this generality, the drawing software makes
143 no use of subgraphs, but maintains the structure for use elsewhere
144 within an application.
145
146 In the second role, a subgraph can provide a context for setting
147 attributes. In \gviz, these are often attributes used by the layout
148 and rendering functions.
149 For example, the application could specify that {\tt blue}
150 is the default color for nodes. Then, every node within the subgraph will
151 have color blue. In the context of graph drawing, a more interesting
152 example is:
153 \begin{verbatim}
154 subgraph {
155 rank = same; A; B; C;
156 }
157 \end{verbatim}
158 This (anonymous) subgraph specifies that the nodes {\tt A}, {\tt B} and {\tt C}
159 should all be placed on the same rank if drawn using \dot.
160
161 The third role for subgraphs combines the previous two. If the name of
162 the subgraph begins with {\tt "cluster"}, \gviz\ identifies the subgraph
163 as a special {\em cluster} subgraph. The drawing software\footnote{if
164 supported} will do the layout of the graph so that the nodes belonging
165 to the cluster are drawn together, with the entire drawing of the cluster
166 contained within a bounding rectangle.
167
168 We note here some important fields used in nodes, edges and graphs.
169 If {\tt np}, {\tt ep} and {\tt gp} are pointers to a node, edge and
170 graph, respectively, {\tt agnameof(np)} and {\tt agraphof(np)} give the
171 name of the node and the root graph containing it,
172 {\tt agtail(ep)} and {\tt aghead(ep)} give the tail and head nodes of the
173 edge, and {\tt agroot(gp)} gives the root graph containing the subgraph.
174 For the root graph, this field will point to itself.
175
176 \subsubsection{Attributes}
177 \label{sec:attributes}
178 In addition to the abstract graph structure provided by nodes, edges and
179 subgraphs, the \gviz\ libraries also support graph attributes. These
180 are simply string-valued name/value pairs. Attributes are used to specify
181 any additional information which cannot be encoded in the abstract graph.
182 In particular, the attributes are heavily used by the drawing software to
183 tailor the various geometric and visual aspects of the drawing.
184
185 Reading attributes is easily done. The function {\tt agget} takes a pointer
186 to a graph component (node, edge or graph) and an attribute name, and
187 returns the value of the attribute for the given component. Note that the
188 function may return either {\tt NULL} or a pointer to the empty string.
189 The first value indicates that the given attribute has not been defined for
190 any component in the graph of the given kind. Thus, if {\tt abc} is a
191 pointer to a node and {\tt agget(abc,"color")} returns {\tt NULL}, then
192 no node in the root graph has a color attribute. If the function returns
193 the empty string, this usually indicates that the attribute has been
194 defined but the attribute value associated with the specified object is the
195 default for the application. So, if {\tt agget(abc,"color")}
196 now returns {\tt ""}, the node is taken to have the default color. In
197 practical terms, these two cases are very similar. Using our example,
198 whether the attribute value is {\tt NULL} or {\tt ""}, the drawing code
199 will still need to pick a color for drawing and will probably use the
200 default in both cases.
201
202 Setting attributes is a bit more complex. Before attaching an attribute
203 to a graph component, the code must first set up the default case. This
204 is accomplished by a call to {\tt agattr}.
205 It takes a graph, an object type ({\tt AGRAPH, AGNODE, AGEDGE}), and
206 two strings as arguments, and return a representation of the attribute.
207 The first string gives the name of the attribute; the second supplies
208 the default value.
209 The graph must be the root graph.
210
211 Once the attribute has been initialized, the attribute can be set for
212 a specific component by calling
213 \begin{verbatim}
214 agset (void*, char*, char*)
215 \end{verbatim}
216 with a pointer to the component,
217 the name of the attribute and the value to which it should be set.
218 For example, the call
219 \begin{verbatim}
220 agset (np, "color", "blue");
221 \end{verbatim}
222 sets the color of node {\tt np} to {\tt "blue"}.
223 The attribute value must not be {\tt NULL}.
224
225 For simplicity, the \graph\ library provides the function
226 \begin{verbatim}
227 agsafeset(void*, char*, char*, char*)
228 \end{verbatim}
229 the first three arguments
230 being the same as those of {\tt agset}. This function first checks that
231 the named attribute has been declared for the given graph component.
232 If it has not, it declares the attribute, using its last argument as
233 the required default value. It then sets the attribute value for the
234 specific component.
235
236 Note that some attributes are replicated in the graph, appearing once
237 as the usual string-valued attribute, and also in an internal machine
238 format such an {\tt int}, {\tt double} or some more structured type.
239 An application should only set attributes using strings and {\tt agset}.
240 The implementation of the layout algorithm
241 may change the machine-level representation at any time.
242 Hence, the low-level
243 interface cannot be relied on by the application to supply the desired
244 input values. Also note that there
245 is not a one-to-one correspondence between string-valued
246 attributes and internal attributes. A given string attribute might be
247 encoded as part of some data structure, might be represented via
248 multiple fields, or may have no internal representation at all.
249
250 In order to expedite the reading and writing of attributes for large
251 graphs, \gviz\ provides a lower-level mechanism for manipulating attributes
252 which can avoid hashing a string.
253 Attributes have a representation of type \verb+Agsym_t+. This is basically the
254 value returned by the initialization function {\tt agattr}. (Passing {\tt NULL}
255 as the default value will cause {\tt agattr} to return the \verb+Agsym_t+ if
256 it exists, and {\tt NULL} otherwise.)
257 An attribute can also be obtained by a call to {\tt agattrsym}, which takes
258 a graph component and an attribute name. If the attribute has been defined,
259 the function returns a pointer to the corresponding \verb+Agsym_t+ value.
260 This can be used to directly access the corresponding attribute value,
261 using the functions {\tt agxget} and {\tt agxset}. These are identical to
262 {\tt agget} and {\tt agset}, respectively, except that instead of
263 taking the attribute name as the second argument, they use
264 the \verb+Agsym_t+ value to access the attribute
265 value from an array.
266
267 Due to the nature of the implementation of attributes in \gviz, an application
268 should, if possible, attempt to define and initialize all
269 attributes before creating nodes and edges.
270
271 The drawing algorithms in \gviz\ use a large collection of attributes,
272 giving the application a great deal of control over the appearance of the
273 drawing. For more detailed and complete information on what the attributes mean, the
274 reader should consult the page \url{https://www.graphviz.org/doc/info/attrs.html}.
275
276 Here, we consider some of the more commonly used attributes.
277 We can divide the attributes into those that affect the placement
278 of nodes, edges and clusters in the layout and those, such as color,
279 which do not.
280 Table~\ref{tab:nattr_geom} gives the node attributes which have the potential
281 to change the layout. This is followed by Tables~\ref{tab:eattr_geom},
282 \ref{tab:gattr_geom} and \ref{tab:cattr_geom}, which do the same for
283 edges, graphs, and clusters.
284 \begin{table}[htbp]\footnotesize
285 \centering
286 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
287 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
288 {\tt distortion} & {\tt 0.0} & node distortion for {\tt shape=polygon} \\
289 {\tt fixedsize} & false & label text has no affect on node size \\
290 {\tt fontname} & {\tt Times-Roman} & font family \\
291 {\tt fontsize} & {\tt 14} & point size of label \\
292 {\tt group} & & name of node's group \\
293 {\tt height} & {\tt .5} & height in inches \\
294 {\tt label} & node name & any string \\
295 {\tt margin} & 0.11,0.055 & space between node label and boundary \\
296 {\tt orientation} & {\tt 0.0} & node rotation angle \\
297 {\tt peripheries} & {\em shape dependent} & number of node boundaries \\
298 {\tt pin} & false & fix node at its {\tt pos} attribute \\
299 {\tt regular} & false & force polygon to be regular \\
300 {\tt root} & & indicates node should be used as root of a layout \\
301 {\tt shape} & {\tt ellipse} & node shape \\
302 {\tt shapefile} & & $\dagger$ external EPSF or SVG custom shape file\\
303 {\tt sides} & {\tt 4} & number of sides for {\tt shape=polygon} \\
304 {\tt skew} & {\tt 0.0} & skewing of node for {\tt shape=polygon} \\
305 {\tt width} & {\tt .75} & width in inches \\
306 {\tt z} & {\tt 0.0} & $\dagger$ z coordinate for VRML output \\
307 \hline
308 \end{tabular}
309 \caption{Geometric node attributes}
310 \label{tab:nattr_geom}
311 \end{table}
312 \begin{table}[htbp]\footnotesize
313 \centering
314 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
315 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
316 {\tt constraint} & true & use edge to affect node ranking \\
317 {\tt fontname} & {\tt Times-Roman} & font family \\
318 {\tt fontsize} & {\tt 14} & point size of label \\
319 {\tt headclip} & true & clip head end to node boundary \\
320 {\tt headport} & center & position where edge attaches to head node \\
321 {\tt label} & & edge label \\
322 {\tt len} & 1.0/0.3 & preferred edge length \\
323 {\tt lhead} & & name of cluster to use as head of edge \\
324 {\tt ltail} & & name of cluster to use as tail of edge \\
325 {\tt minlen} & {\tt 1} & minimum rank distance between head and tail \\
326 {\tt samehead} & & tag for head node; edge heads with the same tag are merged
327 onto the same port \\
328 {\tt sametail} & & tag for tail node; edge tails with the same tag are merged
329 onto the same port \\
330 {\tt tailclip} & true & clip tail end to node boundary \\
331 {\tt tailport} & center & position where edge attaches to tail node \\
332 {\tt weight} & {\tt 1} & importance of edge \\
333 \hline
334 \end{tabular}
335 \caption{Geometric edge attributes}
336 \label{tab:eattr_geom}
337 \end{table}
338 \begin{table}[htbp]\footnotesize
339 \centering
340 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
341 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
342 {\tt center} & false & $\dagger$ center drawing on {\tt page} \\
343 {\tt clusterrank} & {\tt local} & may be {\tt global} or {\tt none} \\
344 {\tt compound} & false & allow edges between clusters \\
345 {\tt concentrate} & false & enables edge concentrators \\
346 {\tt defaultdist} & $1+(\sum_{e \in E} len)/|E|\sqrt{|V|}$ & separation between nodes in different components \\
347 {\tt dim} & $2$ & dimension of layout \\
348 {\tt dpi} & $96/0$ & dimension of layout \\
349 {\tt epsilon} & $.0001 |V|$ or $.0001$ & termination condition \\
350 {\tt fontname} & {\tt Times-Roman} & font family \\
351 {\tt fontpath} & & list of directories to such for fonts \\
352 {\tt fontsize} & $14$ & point size of label \\
353 {\tt label} & & $\dagger$ any string \\
354 {\tt margin} & & $\dagger$ space placed around drawing \\
355 {\tt maxiter} & {\em layout dependent} & bound on iterations in layout \\
356 {\tt mclimit} & $1.0$ & scale factor for mincross iterations \\
357 {\tt mindist} & $1.0$ & minimum distance between nodes \\
358 {\tt mode} & {\tt major} & variation of layout \\
359 {\tt model} & {\tt shortpath} & model used for distance matrix \\
360 {\tt nodesep} & $.25$ & separation between nodes, in inches \\
361 {\tt nslimit} & & if set to {\it f}, bounds network simplex iterations by {\it (f)(number of nodes)} when setting x-coordinates \\
362 {\tt ordering} & & specify out or in edge ordering \\
363 {\tt orientation} & {\tt portrait} & $\dagger$ use landscape orientation if {\tt rotate} is not used and the value is {\tt landscape} \\
364 {\tt overlap} & true & specify if and how to remove node overlaps \\
365 {\tt pack} & & do components separately, then pack \\
366 {\tt packmode} & {\tt node} & granularity of packing \\
367 {\tt page} & & $\dagger$ unit of pagination, {\it e.g.} {\tt "8.5,11"} \\
368 {\tt quantum} & & if ${\tt quantum} > 0.0$, node label dimensions will be rounded to integral multiples of {\tt quantum} \\
369 {\tt rank} & & {\tt same}, {\tt min}, {\tt max}, {\tt source} or {\tt sink} \\
370 {\tt rankdir} & {\tt TB} & sense of layout, i.e, top to bottom, left to right, etc. \\
371 {\tt ranksep} & {\tt .75} & separation between ranks, in inches. \\
372 {\tt ratio} & & approximate aspect ratio desired, {\tt fill} or {\tt auto} \\
373 {\tt remincross} & & If true and there are multiple clusters, re-run crossing minimization \\
374 {\tt resolution} & & synonym for {\tt dpi} \\
375 {\tt root} & & specifies node to be used as root of a layout \\
376 {\tt rotate} & & $\dagger$ If 90, set orientation to landscape \\
377 {\tt searchsize} & {\tt 30} & maximum edges with negative cut values to
378 check when looking for a minimum one during network simplex \\
379 {\tt sep} & $0.1$ & factor to increase nodes when removing overlap \\
380 {\tt size} & & maximum drawing size, in inches \\
381 {\tt splines} & & render edges using splines \\
382 {\tt start} & {\tt random} & manner of initial node placement \\
383 {\tt voro\_margin} & $0.05$ & factor to increase bounding box when more space is necessary during Voronoi adjustment \\
384 {\tt viewport} & & $\dagger$Clipping window \\
385 \hline
386 \end{tabular}
387 \caption{Geometric graph attributes}
388 \label{tab:gattr_geom}
389 \end{table}
390 \begin{table}[htbp]\footnotesize
391 \centering
392 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
393 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
394 {\tt fontname} & {\tt Times-Roman} & font family \\
395 {\tt fontsize} & {\tt 14} & point size of label \\
396 {\tt label} & & edge label \\
397 {\tt peripheries} & $1$ & number of cluster boundaries \\
398 \hline
399 \end{tabular}
400 \caption{Geometric cluster attributes}
401 \label{tab:cattr_geom}
402 \end{table}
403 Note that in some cases, the effect is indirect. An example of this is the
404 {\tt nslimit} attribute, which potentially reduces the effort spent on
405 network simplex algorithms to position nodes, thereby changing the layout.
406 Some of these attributes affect the initial layout of the graph in universal
407 coordinates. Others only play a role if the application uses the \gviz\
408 renderers (cf. Section~\ref{sec:layout_info}),
409 which map the drawing into device-specific coordinates related to a
410 concrete output format.
411 For example, \gviz\ only uses the {\tt center} attribute, which specifies
412 that the graph drawing should be centered within its page, when the library
413 generates a concrete representation.
414 The tables distinguish these device-specific attributes by
415 a $\dagger$ symbol at the start of the Use column.
416
417 Tables~\ref{tab:nattr_dec}, \ref{tab:eattr_dec}, \ref{tab:gattr_dec} and
418 \ref{tab:cattr_dec} list the node, edge, graph and cluster
419 attributes, respectively,
420 that do not effect the placement of components.
421 Obviously, the values of these attributes are not reflected in the
422 position information of the graph after layout. If the application
423 handles the actual drawing of the graph, it must decide if it wishes
424 to use these attributes or not.
425 \begin{table}[htbp]\footnotesize
426 \centering
427 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
428 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
429 {\tt color} & {\tt black} & node shape color \\
430 {\tt fillcolor} & {\tt lightgrey} & node fill color \\
431 {\tt fontcolor} & {\tt black} & text color \\
432 {\tt layer} & overlay range & {\tt all}, {\it id} or {\it id:id} \\
433 {\tt nojustify} & false & context for justifying multiple lines of text \\
434 {\tt style} & & style options, e.g. {\tt bold, dotted, filled} \\
435 \hline
436 \end{tabular}
437 \caption{Decorative node attributes}
438 \label{tab:nattr_dec}
439 \end{table}
440 \begin{table}[htbp]\footnotesize
441 \centering
442 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
443 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
444 {\tt arrowhead} & normal & style of arrowhead at head end \\
445 {\tt arrowsize} & {\tt 1.0} & scaling factor for arrowheads \\
446 {\tt arrowtail} & normal & style of arrowhead at tail end \\
447 {\tt color} & {\tt black} & edge stroke color \\
448 {\tt decorate} & & if set, draws a line connecting labels with their edges \\
449 {\tt dir} & {\tt forward/none} & {\tt forward}, {\tt back}, {\tt both}, or {\tt none} \\
450 {\tt fontcolor} & {\tt black} & type face color \\
451 {\tt headlabel} & & label placed near head of edge \\
452 {\tt labelangle} & {\tt -25.0} & angle in degrees which head or tail label
453 is rotated off edge \\
454 {\tt labeldistance} & {\tt 1.0} & scaling factor for distance of head or tail label from node \\
455 {\tt labelfloat} & false & lessen constraints on edge label placement \\
456 {\tt labelfontcolor} & {\tt black} & type face color for head and tail labels\\
457 {\tt labelfontname} & {\tt Times-Roman} & font family for head and tail labels\\
458 {\tt labelfontsize} & {\tt 14} & point size for head and tail labels \\
459 {\tt layer} & overlay range & {\tt all}, {\it id} or {\it id:id} \\
460 {\tt nojustify} & false & context for justifying multiple lines of text \\
461 {\tt style} & & drawing attributes such as {\tt bold}, {\tt dotted}, or {\tt filled} \\
462 {\tt taillabel} & & label placed near tail of edge \\
463 \hline
464 \end{tabular}
465 \caption{Decorative edge attributes}
466 \label{tab:eattr_dec}
467 \end{table}
468 \begin{table}[htbp]\footnotesize
469 \centering
470 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
471 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
472 {\tt bgcolor} & & background color for drawing, plus initial fill color \\
473 {\tt charset} & {\tt UTF-8} & character encoding for text \\
474 {\tt fontcolor} & {\tt black} & type face color \\
475 {\tt labeljust} & centered & left, right or center alignment for graph labels \\
476 {\tt labelloc} & bottom & top or bottom location for graph labels \\
477 {\tt layers} & & names for output layers \\
478 {\tt layersep} & {\tt "\t :" } & separator characters used in layer specification \\
479 {\tt nojustify} & false & context for justifying multiple lines of text \\
480 {\tt outputorder} & {\tt breadthfirst} & order in which to emit nodes and edges \\
481 {\tt pagedir} & {\tt BL} & traversal order of pages \\
482 {\tt samplepoints} & {\tt 8} & number of points used to represent ellipses
483 and circles on output \\
484 {\tt stylesheet} & & XML stylesheet \\
485 {\tt truecolor} & & determines truecolor or color map model for bitmap output \\
486 \hline
487 \end{tabular}
488 \caption{Decorative graph attributes}
489 \label{tab:gattr_dec}
490 \end{table}
491 \begin{table}[htbp]\footnotesize
492 \centering
493 \begin{tabular}[t]{|l|l|p{2.5in}|} \hline
494 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Default} & \multicolumn{1}{c|}{Use} \\ \hline
495 {\tt bgcolor} & & background color for cluster \\
496 {\tt color} & {\tt black} & cluster boundary color \\
497 {\tt fillcolor} & {\tt black} & cluster fill color \\
498 {\tt fontcolor} & {\tt black} & text color \\
499 {\tt labeljust} & centered & left, right or center alignment for cluster labels \\
500 {\tt labelloc} & top & top or bottom location for cluster labels \\
501 {\tt nojustify} & false & context for justifying multiple lines of text \\
502 {\tt pencolor} & {\tt black} & cluster boundary color \\
503 {\tt style} & & style options, e.g. {\tt bold, dotted, filled}; \\
504 \hline
505 \end{tabular}
506 \caption{Decorative cluster attributes}
507 \label{tab:cattr_dec}
508 \end{table}
509
510 Among these attributes, some are used more frequently than others.
511 A graph drawing typically needs to encode various application-dependent
512 properties in the representations of the nodes. This can be done
513 with text, using the {\tt label}, {\tt fontname} and {\tt fontsize}
514 attributes; with color, using the {\tt color}, {\tt fontcolor},
515 {\tt fillcolor} and {\tt bgcolor} attributes; or with
516 shapes, the most common attributes being {\tt shape}, {\tt height},
517 {\tt width}, {\tt style}, {\tt fixedsize}, {\tt peripheries} and {\tt regular},
518
519 Edges often display additional semantic information with the
520 {\tt color} and {\tt style} attributes. If the edge is directed,
521 the {\tt arrowhead}, {\tt arrowsize}, {\tt arrowtail} and {\tt dir}
522 attributes can play a role. Using splines rather than line segments
523 for edges, as determined by the {\tt splines} attribute, is done
524 for aesthetics or clarity rather than to convey more information.
525
526 There are also a number of frequently used attributes which affect
527 the layout geometry of the nodes and edges. These include
528 {\tt compound}, {\tt len}, {\tt lhead}, {\tt ltail}, {\tt minlen},
529 {\tt nodesep}, {\tt pin}, {\tt pos}, {\tt rank}, {\tt rankdir}, {\tt ranksep}
530 and {\tt weight}. Within this category, we should also mention the
531 {\tt pack} and {\tt overlap} attributes, though they have a somewhat
532 different flavor.
533
534 The attributes described thus far are used as input to the layout algorithms.
535 There is a collection of attributes, displayed in Table~\ref{tab:write},
536 which, by convention, \gviz\ uses to specify the geometry of a layout.
537 \begin{table}[htbp]\footnotesize
538 \centering
539 \begin{tabular}[t]{|l|p{3.5in}|} \hline
540 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Use} \\ \hline
541 {\tt bb} & bounding box of drawing or cluster \\
542 {\tt lp} & position of graph, cluster or edge label \\
543 {\tt pos} & position of node or edge control points \\
544 {\tt rects} & rectangles used in records \\
545 {\tt vertices} & points defining node's boundary, if requested \\
546 \hline
547 \end{tabular}
548 \caption{Output position attributes}
549 \label{tab:write}
550 \end{table}
551 After an application has used \gviz\ to determine position information,
552 if it wants to write out the graph in \DOT\ with this information, it should
553 use the same attributes.
554
555 In addition to the attributes described above which have visual effect,
556 there is a collection of attributes used to supply identification
557 information or web actions. Table~\ref{tab:web} lists these.
558 \begin{table}[htbp]\footnotesize
559 \centering
560 \begin{tabular}[t]{|l|p{3.5in}|} \hline
561 \multicolumn{1}{|c|}{Name} & \multicolumn{1}{c|}{Use} \\ \hline
562 {\tt URL} & hyperlink associated with node, edge, graph or cluster \\
563 {\tt comment} & comments inserted into output \\
564 {\tt headURL} & URL attached to head label \\
565 {\tt headhref} & synonym for {\tt headURL} \\
566 {\tt headtarget} & browser window associated with {\tt headURL} \\
567 {\tt headtooltip} & tooltip associated with {\tt headURL} \\
568 {\tt href} & synonym for {\tt URL} \\
569 {\tt tailURL} & URL attached to tail label \\
570 {\tt tailhref} & synonym for {\tt tailURL} \\
571 {\tt tailtarget} & browser window associated with {\tt tailURL} \\
572 {\tt tailtooltip} & tooltip associated with {\tt tailURL} \\
573 {\tt target} & browser window associated with {\tt URL} \\
574 {\tt tooltip} & tooltip associated with {\tt URL} \\
575 \hline
576 \end{tabular}
577 \caption{Miscellaneous attributes}
578 \label{tab:web}
579 \end{table}
580
581 \subsubsection{Attribute and HTML Strings}
582 \label{sec:attributes_strings}
583 When an attribute is assigned a value, the graph library replicates the
584 string. This means the application can use a temporary string as the
585 argument; it does not have to keep the string throughout the application.
586 Each node, edge, and graph maintains its own attribute
587 values. Obviously, many of these are the same strings, so to save
588 memory, the graph library uses a reference counting mechanism to
589 share strings. An application can employ this mechanism by using
590 the {\tt agstrdup()} function. If it does, it must also use the
591 {\tt agstrfree()} function if it wishes to release the string.
592
593 When using strings as labels, one can have some formatting control via
594 the various inline escape sequences such as \verb+"\n"+, \verb+"\l"+,
595 \verb+"\N"+, etc., and attributes such as {\tt fontname} and {\tt fontcolor}.
596 To get a great deal more flexibility, one can use HTML-like labels.
597 In the \DOT\ language, these strings are delimited by angle brackets
598 {\tt <...>} rather than double quotes in order to be work seamlessly with
599 ordinary strings. Even at the library level, these strings are semantically
600 identical to ordinary strings except when used as labels.
601 To create one of these, one uses {\tt agstrdup\_html()}
602 rather than {\tt agstrdup()}. The {\tt agstrfree()} is still used to
603 release the string. For example, one might use the following code to attach
604 an HTML string to a node:
605 \begin{verbatim}
606 Agnode_t* n;
607 char* l = agstrdup_html(agroot(n), "<B>some bold text</B>");
608 agset (n,"label",l);
609 agstrfree (l);
610 \end{verbatim}
611 In addition, the function {\tt aghtmlstr()}
612 can be used query if an attribute string is an HTML string.
613
614 \subsection{Laying out the graph}
615
616 Once the graph exists and the attributes are set, the application can
617 pass the graph to one of the \gviz\ layout functions by a call to
618 {\tt gvLayout}. As arguments, this function takes a pointer to a \gvc, a
619 pointer to the graph to be laid out, and the name of the desired layout
620 algorithm. The algorithm names are the same as those of the layout
621 programs listed in Section~\ref{sec:intro}. Thus, {\tt "dot"} is used
622 to invoke {\tt dot}, etc.\footnote{Usually, all of these algorithms
623 are available. It is possible, however, that an application can arrange
624 to have only a subset made available.}
625
626 The layout algorithm will do everything that the corresponding
627 program would do, given the graph and its attributes. This includes
628 assigning node positions,
629 representing edges as splines\footnote{
630 Line segments are represented as degenerate splines.
631 }, handling the special case of an unconnected
632 graph, plus dealing with various technical features such as preventing
633 node overlaps.
634
635 There are two special layout engines available in the library:
636 {\tt "nop"} and {\tt "nop2"}. These correspond to running the
637 \neato\ command with the flags {\tt -n} and {\tt -n2}, respectively.
638 That is, they assume the input graph already has position information stored
639 for nodes and, in the latter case, some edges. They can be used to route
640 edges in the graph or perform other adjustments. Note that they expect
641 the position information to be stored as {\tt pos} attributes in the
642 nodes and edges. The application can do this itself, or use the
643 {\tt dot} renderer.
644
645 For example, if one wants to position the nodes of a graph using
646 a \dot\ layout, but wants edges drawn as line segments, one could
647 use the following code shown in Figure~\ref{fig:nop}.
648 \begin{figure}[hbt]
649 \begin{verbatim}
650 Agraph_t* G;
651 GVC_t* gvc;
652
653 /*
654 * Create gvc and graph
655 */
656
657 gvLayout (gvc, G, "dot");
658 gvRender (gvc, G, "dot", NULL);
659 gvFreeLayout(gvc, G);
660 gvLayout (gvc, G, "nop");
661 gvRender (gvc, G, "png", stdout);
662 gvFreeLayout(gvc, G);
663 agclose (G);
664 \end{verbatim}
665 \caption{Basic use}
666 \label{fig:nop}
667 \end{figure}
668 The first call to {\tt gvLayout} lays out the graph using dot;
669 the first call to {\tt gvRender} attaches the computed position
670 information to the nodes and edges.
671 The second call to {\tt gvLayout} adds straight-line edges to the
672 already positioned nodes; the second call to {\tt gvRender} outputs
673 the graph in {\tt png} for on {\tt stdout}.
674
675 \subsection{Rendering the graph}
676 \label{sec:layout_info}
677 Once the layout is done, the graph data structures contain
678 the position information for drawing the graph.
679 The application needs to decide how to use this information.
680
681 To use the renderers supplied with the \gviz\ software, the application
682 can call one of the library functions
683 \begin{verbatim}
684 gvRender (GVC_t *gvc, Agraph_t* g, char *format, FILE *out);
685 gvRenderFilename (GVC_t *gvc, Agraph_t* g, char *format, char *filename);
686 \end{verbatim}
687 The first and second arguments are a graphviz context handle and a pointer
688 to the graph to be rendered. The final argument gives, respecitively,
689 a file stream open for writing or the name of a file to which the
690 graph should be written. The third argument names the renderer to
691 be used, such as {\tt "ps"}, {\tt "png"} or {\tt "dot"}.
692 The allowed strings are the same ones used with the {\tt -T} flag
693 when the layout program is invoked from a command shell.
694
695 After a graph has been laid out using {\tt gvLayout}, an application
696 can perform multiple calls to the rendering functions. A typical instance
697 might be
698 \begin{verbatim}
699 gvLayout (gvc, g, "dot");
700 gvRenderFilename (gvc, g, "png", "out.png");
701 gvRenderFilename (gvc, g, "cmap", "out.map");
702 \end{verbatim}
703 in which the graph is laid out using the \dot\ algorithm, followed by
704 PNG bitmap output and a corresponding map file which can be used in a web
705 browser.
706
707 As with reading, \gviz\ provides some specialized functions for rendering.
708 Of note is
709 \begin{verbatim}
710 gvRenderData (GVC_t *gvc, Agraph_t* g, char *format, char **result,
711 unsigned int *length)
712 \end{verbatim}
713 which writes the output of the rendering onto an allocated character buffer. A pointer
714 to this buffer is returned in {\tt *result} and the number of bytes written is stored in
715 {\tt length}. After using the buffer, the memory should be freed by the application.
716 As the calling program may rely on a different run-time system than that used by
717 \gviz, the library provides the function
718 \begin{verbatim}
719 gvFreeRenderData (char *data);
720 \end{verbatim}
721 which can be used to free the memory pointed to by {\tt *result}.
722
723 Sometimes, an application will decide to do its own rendering.
724 An application-supplied
725 drawing routine, such as {\tt drawGraph} in Figure~\ref{fig:basic}
726 can then read this information,
727 map it to display coordinates, and call routines to render the drawing.
728
729 One simple way to do this is to use the position and drawing information as
730 supplied by the {\tt dot} or {\tt xdot} format (see
731 Sections~\ref{sect:dot} and \ref{sect:xdot}). To get this, the application
732 can call the appropriate renderer, passing a NULL stream
733 pointer to {\tt gvRender}\footnote{
734 This convention only works, and only makes sense, with the {\tt dot}
735 and {\tt xdot} renderers. For other renders, a NULL stream will cause
736 output to be written on {\tt stdout}.} as in Figure~\ref{fig:nop}.
737 This will attach the information as string attributes. The application
738 can then use {\tt agget} to read the attributes.
739
740 On the other hand, an application may desire to read the primitive
741 data structures used by the algorithms to record the layout information.
742 In the remainder of this section,
743 we describe in reasonable detail these data structures.
744 An application can
745 use these values directly to guide its drawing. In some cases, for example,
746 with arrowheads attached to {\tt bezier} values or HTML-like labels, it
747 would be onerous for an application to fully interpret the data.
748 For this reason,
749 if an application wishes to provide all of the
750 graphics features while avoiding the low-level details of the data
751 structures, we suggest either using {\tt xdot} approach, described above,
752 or supplying its own renderer plug-in as described in
753 Section~\ref{sec:renderers}.
754
755 The \gviz\ layout algorithms rely on a specific set of fields
756 to record position and drawing information.
757 Thus, the definitions
758 of the information fields are fixed by the layout library and
759 cannot be altered by the application.\footnote{This is a limitation
760 of the \graph\ library. We plan to remove this restriction by moving to
761 a mechanism which allows arbitrary dynamic extensions to the
762 node, edge and graph structures. Meanwhile, if the application requires
763 the addition of extra fields, it can define its own structures, which
764 should be extensions of the components of the information types, with
765 the additional fields attached at the end. Then, instead of calling
766 {\tt aginit()}, it can use the more general {\tt aginitlib()}, and
767 supply the sizes of its nodes, edges and graphs. This will ensure
768 that these components will have the correct sizes and alignments.
769 The application can then cast the generic \graph\ types to the
770 types it defined, and access the additional fields.}
771
772 The fields should only be accessed using macro expressions provided for
773 this purpose.
774 Thus, if {\tt np} is a node pointer, the width field should
775 be read using \verb+ND_width(np)+.
776 Edge and graph attributes follow the same convention, with
777 prefixes \verb+ED_+ and \verb+GD_+, respectively.
778 A complete list of these macros is given in {\tt types.h},
779 along with various auxiliary types such as {\tt pointf} or
780 {\tt bezier}
781 \footnote{We strongly deprecate accessing the fields directly, for the usual reason
782 of good programming style. By using the macros, source code will not be
783 affected by any changes to the how the value is provided}.
784
785 We now consider the principal fields providing position information.
786
787 Each node has {\tt ND\_coord}, {\tt ND\_width} and {\tt ND\_height}
788 attributes. The value
789 of {\tt ND\_coord} gives the position of the center of the node,
790 in points.\footnote{
791 The \neato\ and \fdp\ layouts allow the graph to specify fixed positions
792 for nodes. Unfortunately, some post-processing done in \gviz\ translates
793 the layout so that its lower-left corner is at the origin. To recover
794 the original coordinates, the application will need to translate all positions
795 by the vector $p_0 - p$, where $p_0$ and $p$ are the input position and
796 the final position of some node whose position was fixed.
797 }
798 The {\tt ND\_width} and {\tt ND\_height} attributes specify the size of the
799 bounding box of the node, in inches.
800 Note that the {\tt width} and {\tt height} attributes provide in the input
801 graph are minimum values, so that the values stored in {\tt ND\_width}
802 and {\tt ND\_height} may be larger.
803
804 Edges, even if a line segment, are represented as cubic B-splines or
805 piecewise Bezier curves.
806 The {\tt ED\_spl} attribute of the edge stores this spline information.
807 It has a pointer to an array of 1 or more {\tt bezier} structures. Each
808 of these describes a single piecewise Bezier curve as well as associated
809 arrowhead information. Normally, a single {\tt bezier} structure is
810 sufficient to represent an edge. In some cases, however,
811 the edge may need multiple {\tt bezier} parts, as when the {\tt concentrate}
812 attribute is set, whereby mostly parallel edges are represented by a
813 shared spline.
814 Of course, the application always has the possibility of drawing a line
815 segment connecting the centers of the edge's nodes.
816
817 If a subgraph is specified as a cluster, the nodes of the cluster
818 will be drawn together and the entire subgraph is contained within
819 a rectangle containing no other nodes. The rectangle is specified by
820 the {\tt GD\_bb} attribute of the subgraph, the coordinates in points in
821 the global coordinate system.
822
823 \subsubsection{Drawing nodes and edges}
824 \label{sec:nodes}
825
826 With the position and size information described above, an application
827 can draw the nodes and edges of a graph. It could just use rectangles
828 or circles for nodes, and represent edges as line segments or splines.
829 However, nodes and edges typically have a variety of other attributes,
830 such as color or line style, which an application can read from the
831 appropriate fields and use in its rendering.
832
833 Additional drawing information about the node depends mostly on the shape
834 of the node.
835 For record-type nodes, where \verb+ND_shape(n)->name+ is {\tt "record"} or
836 {\tt "Mrecord"}, the node consists of a packed collection
837 of rectangles. In this case, \verb+ND_shape_info(n)+ can be cast to
838 \verb+field_t*+, which describes the recursive partition of the node
839 into rectangles. The value {\tt b} of \verb+field_t+
840 gives the bounding rectangle of the field, in points in the coordinate
841 system of the node, i.e., where the center of the node is at the
842 origin.
843
844 If \verb+ND_shape(n)->usershape+ is true, the shape
845 is specified by the user. Typically, this is format dependent, e.g.,
846 the node might be specified by a GIF image, and we ignore this case
847 for the present.
848
849 The final node class consists of those with polygonal shape\footnote{This
850 is not quite true but close enough for now.}, which includes the limiting
851 cases of circles, ellipses, and none. In this case,
852 \verb+ND_shape_info(n)+ can be cast to \verb+polygon_t*+, which specifies
853 the many parameters (number of sides, skew and distortions, etc.)
854 used to describe polygons, as well as the points used as vertices.
855 Note that the vertices are in inches, and are in the coordinate system
856 of the node, with the origin at the center of the node.
857
858 To handle a node's shape, an application has two basic choices. It
859 can implement the geometry for each of the different shapes.
860 Thus, it could see that \verb+ND_shape(n)->name+ is {\tt "box"}, and
861 use the {\tt ND\_coord}, {\tt ND\_width} and {\tt ND\_height} attributes to draw
862 rectangle at the given position with the given width and height.
863 A second approach would be
864 to use the specification of the shape as stored internally in the
865 {\tt shape\_info} field of the node. For example, given a polygonal node,
866 its \verb+ND_shape_info(n)+ field contains a {\tt vertices} field,
867 mentioned above, which is an ordered list of all the vertices used
868 to draw the appropriate polygon, taking into account multiple peripheries.
869 Again, if an application desires to be fully faithful in the rendering,
870 it may be preferable to use the {\tt xdot} information or to supply its
871 own renderer plugin.
872
873 For edges, each {\tt bezier} structure has a \verb+list+ field pointing to
874 an array containing the control points and a \verb+size+ field giving the
875 number of points in \verb+list+, which will always have the form $(3*n+1)$.
876 In addition, there are
877 fields for specifying arrowheads. If {\tt bp} points to
878 a {\tt bezier} structure and the {\tt bp->sflag} field is
879 true, there should be an arrowhead attached to the beginning of the bezier.
880 The field {\tt bp->sp} gives the point where the nominal tip of the arrowhead
881 would touch the tail node. (If there is no arrowhead, {\tt bp->list[0]} will
882 touch the node.) Thus, the length and direction of the arrowhead
883 is determined by the vector going from {\tt bp->list[0]} to {\tt bp->sp}.
884 The actual
885 shape and width of the arrowhead is determined by the {\tt arrowtail} and
886 {\tt arrowsize} attributes. Analogously, an arrowhead at the head node is
887 specified by {\tt bp->eflag} and the vector
888 from {\tt bp->list[bp->size-1]} to {\tt bp->ep}.
889
890 The label field (\verb+ND_label(n)+, \verb+ED_label(e)+,
891 \verb+GD_label(g)+) encodes any text label associated with a graph object.
892 Edges, graphs and clusters will occasionally have labels; nodes
893 almost always have a label, since the default label is the node's name.
894 The basic label string is stored in the {\tt text} field, while the
895 {\tt fontname}, {\tt fontcolor} and {\tt fontsize} fields describe
896 the basic font characteristics.
897 In many cases, the basic label string is further parsed, either into
898 multiple, justified text lines, or as a nested box structure for
899 HTML-like labels or nodes of record shape.
900 This information is available in other fields.
901
902 \subsection{Cleaning up a graph}
903 \label{sec:clean}
904
905 Once all layout information is obtained from the graph, the resources
906 should be reclaimed. To do this, the application should call
907 the cleanup routine associated with the layout algorithm used to
908 draw the graph. This is done by a call to {\tt gvFreeLayout}.
909
910 The example of Figure~\ref{fig:basic}
911 demonstrates the case where the application is drawing a single graph.
912 The example given in Appendix~\ref{sec:dot}
913 shows how cleanup might be done when processing multiple graphs.
914
915 The application can best determine when it should clean up. The example
916 in the appendix
917 performs this just before a new graph is drawn, but the application could
918 have done this much earlier, for example, immediately after the graph
919 is drawn using {\tt gvRender}. Note, though, that layout information
920 is destroyed during cleanup. If the application needs to reuse this
921 data, for example, to refresh the display, it should delay calling the
922 cleanup function, or arrange to copy the layout data elsewhere.
923 Also, in the simplest case where the application just draws one graph
924 and exits, there is no need to do cleanup at all, though this is sometimes
925 considered poor programming style.
926
927 A given graph can be laid out multiple times.
928 The application, however, must clean up the earlier layout's
929 information with a call to {\tt gvFreeLayout}
930 before invoking a new layout function.
931 An example of this was given in Figure~\ref{fig:nop}.
932
933 Note that if you render a graph into the {\tt dot} or {\tt xdot}
934 format, this attaches attributes onto the graph. Some of these attributes
935 are used during layout. For example, the {\tt neato} layout will use
936 the {\tt pos} attribute of the nodes for an initial layout, while the
937 {\tt twopi} layout may set the {\tt root} attribute, which will lock in
938 this attribute for any future layouts using {\tt twopi}.
939 To avoid having these attributes affecting another
940 layout of the graph, the user should should set these attributes to the
941 empty string before calling {\tt gvLayout} again.
942
943 Once the application is totally done with a graph, it should call
944 {\tt agclose} to close the graph and reclaim the remaining resources associated
945 with it.
946