简体   繁体   中英

How to prevent edge labels from messing up the layout in graphviz?

I have a simple vertical graph that looks nice and symmetrical without any labels using the following code:

digraph test_alignment
{
    {rank=same; a -> b;}
    a -> c;
    b -> c;
    c -> d;
    d -> e;
    d -> f;
    {rank=same; e -> f;}
}

The initial graph

I'd like to label the edge between A and B as well as the one between E and F, using the same string for each label. I'm expecting the same output, except with longer A->B and E->F edges bearing the same label.

Unfortunately, as soon as I add a label to one of these edges, the general layout looks slightly askew (the result is similar if I add a label to the E->F edge):

digraph test_alignment
{
    {rank=same; a -> b [label="Label"];}
    a -> c;
    b -> c;
    c -> d;
    d -> e;
    d -> f;
    {rank=same; e -> f;}
}

The same graph with one label

I'm very new to graphviz and, following many questions on Stack Overflow, I have been experimenting with different combinations of rank and constraint, I tried using clusters to see if it would keep the top and bottom part properly aligned independently, and tried using a mix of dot, ccomp, gvpack and neato that produced similar results.

It always seems to boil down to the fact adding a label to the edge between nodes with the same rank affects how these nodes are positioned in a way I don't yet understand.

Am I missing something trivial, or am I attempting something I shouldn't instead of letting dot do its thing?

I'm using dot - graphviz version 2.36.0 (20140111.2315) and the linked pictures were produced with dot only (though I obtained similar results when using :

dot test_alignment.dot -Tpng -O

You may try to use the xlabel attribute, together with forcelabels if necessary. From the description:

... For edges, the label will be placed near the center of the edge. This can be useful in dot to avoid the occasional problem when the use of edge labels distorts the layout. ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM