1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
\section{A sample program: {\tt simple.c}}
\label{sec:simple}
This following code illustrates an application which uses \gviz\ to
position a graph using the \dot\ layout and then writes the output
using the {\tt plain} format.
An application can replace the call to {\tt gvRender} with its own
function for rendering the graph, using the layout information
encoded in the graph structure (cf. Section~\ref{sec:layout_info}).
\begin{verbatim}
#include <gvc.h>
int main(int argc, char **argv)
{
GVC_t *gvc;
Agraph_t *g;
FILE *fp;
gvc = gvContext();
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = stdin;
g = agread(fp, 0);
gvLayout(gvc, g, "dot");
gvRender(gvc, g, "plain", stdout);
gvFreeLayout(gvc, g);
agclose(g);
return (gvFreeContext(gvc));
}
\end{verbatim}
\newpage
\section{A sample program: {\tt dot.c}}
\label{sec:dot}
This example shows how an application might read a stream of input graphs,
lay out each, and then use the \gviz\ renderers to write the drawings
to an output file. Indeed, this
is precisely how the \dot\ program is written, ignoring some signal
handling, its specific declaration of
the {\tt Info} data (cf. Section~\ref{sec:info}), and a few other
minor details.
\begin{verbatim}
#include <gvc.h>
int main(int argc, char **argv)
{
Agraph_t *g, *prev = NULL;
GVC_t *gvc;
gvc = gvContext();
gvParseArgs(gvc, argc, argv);
while ((g = gvNextInputGraph(gvc))) {
if (prev) {
gvFreeLayout(gvc, prev);
agclose(prev);
}
gvLayoutJobs(gvc, g);
gvRenderJobs(gvc, g);
prev = g;
}
return (gvFreeContext(gvc));
}
\end{verbatim}
\newpage
\section{A sample program: {\tt demo.c}}
\label{sec:demo}
This example provides a modification of the previous example. Again it
relies on the \gviz\ renderers, but now it creates the graph dynamically
rather than reading the graph from a file.
Note that either the graph or the {\tt argv[]} values have to specify which
layout algorithm is used, as explained in Section~\ref{sec:gvc}. Specifically,
the input graph must have the {\tt layout} attribute set, or the command line
arguments must contain a valid {\tt "-K"} flag.
If not, {\tt gvParseArgs} will look at the base name part of {\tt argv[0]} and
use that as the name of desired layout program. For this to work, the executable
program needs to be renamed as one of the \gviz\ layout programs (cf. Section~\ref{sec:intro}).
\begin{verbatim}
#include <gvc.h>
int main(int argc, char **argv)
{
Agraph_t *g;
Agnode_t *n, *m;
Agedge_t *e;
Agsym_t *a;
GVC_t *gvc;
/* set up a graphviz context */
gvc = gvContext();
/* parse command line args - minimally argv[0] sets layout engine */
gvParseArgs(gvc, argc, argv);
/* Create a simple digraph */
g = agopen("g", Agdirected);
n = agnode(g, "n", 1);
m = agnode(g, "m", 1);
e = agedge(g, n, m, 0, 1);
/* Set an attribute - in this case one that affects the visible rendering */
agsafeset(n, "color", "red", "");
/* Compute a layout using layout engine from command line args */
gvLayoutJobs(gvc, g);
/* Write the graph according to -T and -o options */
gvRenderJobs(gvc, g);
/* Free layout data */
gvFreeLayout(gvc, g);
/* Free graph structures */
agclose(g);
/* close output file, free context, and return number of errors */
return (gvFreeContext(gvc));
}
\end{verbatim}
|