How to draw multiple graphs with dot?
Asked Answered
E

3

14

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 xdot
graph {
    ...
}

Piping the xdot output to dot again doesn't fix the problem.

So, how does one render multiple graphs with graphviz?

Emunctory answered 17/3, 2011 at 17:12 Comment(0)
E
4

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;        
    }
}
Emunctory answered 24/3, 2011 at 18:47 Comment(0)
L
8

One calls dot multiple times. Or one puts everything into a single graph, taking care to avoid duplication of names.

Luanneluanni answered 17/3, 2011 at 17:21 Comment(0)
S
6

Use gvpack

$ echo -e "graph { a -- b }\ngraph { c -- d }" | gvpack -u | dot -Tpng > graphs.png

Result

enter image description here

Scandal answered 23/2, 2018 at 12:20 Comment(0)
E
4

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;        
    }
}
Emunctory answered 24/3, 2011 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.