How can I create named edge “types” in Graphviz/dot/neato?

后端 未结 2 546
执念已碎
执念已碎 2020-12-11 09:32

I need to draw a diagram with graphviz/dot where there are common edge types between nodes and am trying to find a way to define a label for each type of edge and then use t

2条回答
  •  臣服心动
    2020-12-11 10:17

    I think I got your solution, using m4 (thanks to Simon). Using and adapting your sample, I created a file called gv.m4:

    digraph {
        define(`edge_x',`[label="type x", color=red, style=solid]')
        define(`edge_y',`[label="type y", color=green, style=dashed]')
        define(`edge_z',`[label="type z", color=blue, style=dotted]')
    
        subgraph cluster_1 {
            a -> b edge_x;
            b -> a edge_y;
    
            b -> c edge_x;
            c -> b edge_y;
    
            c -> d edge_z;
        }
    
        subgraph cluster_2 {
            d -> e edge_x;
            e -> d edge_y;
    
            e -> f edge_x;
            f -> e edge_y;
    
            f -> c edge_z;
        }
    }
    

    and converted it with the simple command

    m4 gv.m4 > gv.dot
    

    which now contains your defined edges

    digraph {
    
        subgraph cluster_1 {
            a -> b [label="type x", color=red, style=solid];
            b -> a [label="type y", color=green, style=dashed];
    
            b -> c [label="type x", color=red, style=solid];
            c -> b [label="type y", color=green, style=dashed];
    
            c -> d [label="type z", color=blue, style=dotted];
        }
    
        subgraph cluster_2 {
            d -> e [label="type x", color=red, style=solid];
            e -> d [label="type y", color=green, style=dashed];
    
            e -> f [label="type x", color=red, style=solid];
            f -> e [label="type y", color=green, style=dashed];
    
            f -> c [label="type z", color=blue, style=dotted];
        }
    }
    

    and yields the expected graph:

    You can do much more with m4 - stuff that is missing in graphViz, like maintaining and (even conditionally) including subfiles. For example, if you put your two subgraphs into two separate files gv1.txt and gv2.txt, this would work nicely:

    digraph incl
    {
        define(`edge_x',`[label="type x", color=red, style=solid]')
        define(`edge_y',`[label="type y", color=green, style=dashed]')
        define(`edge_z',`[label="type z", color=blue, style=dotted]')
        include(gv1.txt)
        include(gv2.txt)
         e -> d[ color = yellow, label = "this is new!"];
    }
    

提交回复
热议问题