Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize boxes horizontally and then vertically in graphviz

Tags:

graph

graphviz

Is there a way to have boxes show up horizontally in some cases, and vertically in others? (see related question).

Here is the code and output which I am getting:

code:

/**
** Diagram representing the Simulator Engine
**/
digraph G {
        graph [
            rankdir = "TB"
        ];

        /**
        ** The simulator engine rectangle
        **/
    subgraph cluster_simulator_engine {
        style=filled;
        color=lightgrey;
        node [style=filled,color=white];
        label = "Simulator Engine";

                /**
                ** The first topology
                **/
                subgraph cluster_T1 {
                        color=white;
                        node [style=filled];

                        /**
                        ** The n^th neuron
                        **/
                        subgraph cluster_T1_N3 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron n";

                                /**
                                ** The n^th synapse 
                                **/
                                "T1_N3_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T1_N3_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T1_N3_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                /*"T1_N1_S3" -> "T1_N1_S2" [style=invis];*/
                        }

                        /**
                        ** The second neuron
                        **/
                        subgraph cluster_T1_N2 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 2";

                                /**
                                ** The n^th synapse 
                                **/
                                "T1_N2_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T1_N2_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T1_N2_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                /*"T1_N1_S3" -> "T1_N1_S2" [style=invis];*/
                        }


                        /**
                        ** The third neuron
                        **/
                        subgraph cluster_T1_N1 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 1";

                                /**
                                ** The n^th synapse 
                                **/
                                "T1_N1_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T1_N1_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T1_N1_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                /*"T1_N1_S3" -> "T1_N1_S2" [style=invis];*/
                        }


                        label = "Topology 1";
                }

                /**
                ** The second topology
                **/
                subgraph cluster_T2 {
                        color=white;
                        node [style=filled];

                        /**
                        ** The n^th neuron
                        **/
                        subgraph cluster_T2_N3 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron n";

                                /**
                                ** The n^th synapse 
                                **/
                                "T2_N3_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T2_N3_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T2_N3_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                /*"T1_N1_S3" -> "T1_N1_S2" [style=invis];*/
                        }

                        /**
                        ** The second neuron
                        **/
                        subgraph cluster_T2_N2 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 2";

                                /**
                                ** The n^th synapse 
                                **/
                                "T2_N2_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T2_N2_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T2_N2_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                /*"T1_N1_S3" -> "T1_N1_S2" [style=invis];*/
                        }


                        /**
                        ** The third neuron
                        **/
                        subgraph cluster_T2_N1 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 1";

                                /**
                                ** The n^th synapse 
                                **/
                                "T2_N1_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T2_N1_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T2_N1_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                /*"T1_N1_S3" -> "T1_N1_S2" [style=invis];*/
                        }


                        label = "Topology 2";
                }

    }

}

Output:

First Diagram

Obviously this is far too long. What I want is to move each synapse into its own line (I think it's called a 'rank' in Graphviz-jargon). Apparently, there is no way to do this, but there is a trick. Therefore, I take the same code above and introduce invisible edges like so

code:

/**
** Diagram representing the Simulator Engine
**/
digraph G {
        graph [
            rankdir = "TB"
        ];

        /**
        ** The simulator engine rectangle
        **/
    subgraph cluster_simulator_engine {
        style=filled;
        color=lightgrey;
        node [style=filled,color=white];
        label = "Simulator Engine";

                /**
                ** The first topology
                **/
                subgraph cluster_T1 {
                        color=white;
                        node [style=filled];

                        /**
                        ** The n^th neuron
                        **/
                        subgraph cluster_T1_N3 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron n";

                                /**
                                ** The n^th synapse 
                                **/
                                "T1_N3_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T1_N3_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T1_N3_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                "T1_N3_S1" -> "T1_N3_S2" [style=invis];
                                "T1_N3_S2" -> "T1_N3_S3" [style=invis];
                        }

                        /**
                        ** The second neuron
                        **/
                        subgraph cluster_T1_N2 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 2";

                                /**
                                ** The n^th synapse 
                                **/
                                "T1_N2_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T1_N2_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T1_N2_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                "T1_N2_S2" -> "T1_N2_S3" [style=invis];
                                "T1_N2_S1" -> "T1_N2_S2" [style=invis];
                        }


                        /**
                        ** The third neuron
                        **/
                        subgraph cluster_T1_N1 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 1";

                                /**
                                ** The n^th synapse 
                                **/
                                "T1_N1_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T1_N1_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T1_N1_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                "T1_N1_S1" -> "T1_N1_S2" [style=invis];
                                "T1_N1_S2" -> "T1_N1_S3" [style=invis];
                        }


                        label = "Topology 1";
                }

                /**
                ** The second topology
                **/
                subgraph cluster_T2 {
                        color=white;
                        node [style=filled];

                        /**
                        ** The n^th neuron
                        **/
                        subgraph cluster_T2_N3 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron n";

                                /**
                                ** The n^th synapse 
                                **/
                                "T2_N3_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T2_N3_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T2_N3_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                "T2_N3_S1" -> "T2_N3_S2" [style=invis];
                                "T2_N3_S2" -> "T2_N3_S3" [style=invis];
                        }

                        /**
                        ** The second neuron
                        **/
                        subgraph cluster_T2_N2 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 2";

                                /**
                                ** The n^th synapse 
                                **/
                                "T2_N2_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T2_N2_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T2_N2_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                "T2_N2_S1" -> "T2_N2_S2" [style=invis];
                                "T2_N2_S2" -> "T2_N2_S3" [style=invis];
                        }


                        /**
                        ** The third neuron
                        **/
                        subgraph cluster_T2_N1 {
                                color=lightgrey;
                                node [style=filled];
                                label = "Neuron 1";

                                /**
                                ** The n^th synapse 
                                **/
                                "T2_N1_S3" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse n"
                                ];

                                /**
                                ** The second synapse 
                                **/
                                "T2_N1_S2" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 2"
                                ];

                                /**
                                ** The first synapse 
                                **/
                                "T2_N1_S1" [
                                    style=filled
                                    shape=box
                                    color=white
                                    label="Synapse 1"
                                ];

                                "T2_N1_S1" -> "T2_N1_S2" [style=invis];
                                "T2_N1_S2" -> "T2_N1_S3" [style=invis];
                        }


                        label = "Topology 2";
                }

    }

}

and the output now looks more appealing.

output: Second smaller diagram

But now there is a huge gap between the synapse boxes. Setting nodesep=0.1 or len=0.1 has no effect. Can anyone tell me how to fix this, or how to redesign this.

NOTE: If anyone is curious why I go from 1 to 2 to n, it is because I plan to put an ellipses in there, but I have no clue how to do that...cross that bridge when I get to it.

like image 864
puk Avatar asked Feb 01 '12 08:02

puk


1 Answers

It's ranksep you're looking for - add this line to the attributes for the graph:

ranksep = 0.1

In dot, this gives the desired rank separation, in inches. This is the minimum vertical distance between the bottom of the nodes in one rank and the tops of nodes in the next.

like image 190
marapet Avatar answered Sep 30 '22 18:09

marapet