I have a print_dot()
function that outputs dot on stdout.
That way I can do:
$ ./myprogram < input | dot -T x11
It works great when I try to print one graph.
Now when I print several graphs, nothing shows up. The dot window is blank, X11 and dot take all the CPU. Nothing is printed on stderr.
$ echo -e "graph { a -- b }" | dot -T x11 # work
$ echo -e "graph { a -- b } \n graph { c --d }" | dot -T x11 # doesn't work
# it seems to be interpreted nonetheless
$ echo -e "graph { a -- b } \n graph { c -- d } " | dot -T xdot
graph {
...
}
graph {
...
}
Also, when I remove the \n
between the 2 graphs, only the first graph is interpreted (what a nice feature...):
$ echo -e "graph { a -- b } graph { c -- d } " | dot -T xdo开发者_运维技巧t
graph {
...
}
Piping the xdot output to dot again doesn't fix the problem.
So, how does one render multiple graphs with graphviz?
One calls dot
multiple times. Or one puts everything into a single graph, taking care to avoid duplication of names.
Use gvpack
$ echo -e "graph { a -- b }\ngraph { c -- d }" | gvpack -u | dot -Tpng > graphs.png
Result
Simple script that reads graphs on stdin and opens multiple dot instance.
#!/usr/bin/perl
my $o;
my @l;
while(<>) {
if(/^\s*(di)?graph/) {
push @l, $o;
$o = '';
}
$o .= $_;
}
if($o =~ /graph/) {
push @l, $o;
}
for(@l) {
if(fork() == 0) {
open my $p, '| dot -T x11' or die $!;
print $p $_;
close $p;
exit 0;
}
}
精彩评论