Search code examples
graphviz

Graphviz: Items not in alphabetic order


Good morning, can anyone tell why the items on the graph are displayed in the order 1,3,2 rather than 1,2,3 please? I guess this is a bug, how to workaround it? Thanks in advance!

  1. Open http://viz-js.com/

  2. Paste this and focus on 3rd level (the bottom)

digraph G { 
"NA";
"I";
"II";
"III";
"1";
"2";
"3";
{rank = same 1; 2 ; 3}
{rank = same I; II ; III}
"NA"->"I" ;
"NA"->"II" ;
"NA"->"III" ;
"I"->"1" ;
"I"->"2" ;
"I"->"3" ;
"II"->"1" ;
"II"->"2" ;
"II"->"3" ;
"III"->"1" ;
"III"->"2" ;
"III"->"3" ;
"NA"->"1" ;
"NA"->"2" ;
"NA"->"3" ;
}

Solution

  • A common issue, we want a graph to look "just so" - whatever our personal aesthetic is. But the dot algorithms are programmed to a different set of goals - sometimes producing very confusing results.
    Luckily your problem is easily (and explicitly) resolved with the ordering attribute.

    digraph G { 
    "NA";
    "I"  [ordering=out];  // explicit ordering of out edges
    "II";
    "III";
    "1";
    "2";
    "3";
    {rank = same 1; 2 ; 3}
    {rank = same I; II ; III}
    "NA"->"I" ;
    "NA"->"II" ;
    "NA"->"III" ;
    "I"->"1" ;
    "I"->"2" ;
    "I"->"3" ;
    "II"->"1" ;
    "II"->"2" ;
    "II"->"3" ;
    "III"->"1" ;
    "III"->"2" ;
    "III"->"3" ;
    "NA"->"1" ;
    "NA"->"2" ;
    "NA"->"3" ;
    }
    

    enter image description here

    Sometimes these ordering-type problems can be hard to get "right", but not this time. The only trick was to find the "right" attribute and a way to use it.