graphviz:如何防止集群覆盖rank = source语句

以下代码生成上图:

digraph G {


//---graph config

    fontname=Helvetica

    rankdir = RL
    splines = polyline
    compound = true
    //concentrate = true

    labeljust = c
    labelloc = t

    ranksep=0.5
    nodesep=0.5

    //size="10,10"
    ratio=compress

    edge [
        minlen=1
        arrowsize=0.75
        labeldistance=5     

        fontname=Helvetica
        fontsize=12
        fontcolor=black     

        labelfontsize=12
        labelfontcolor=red
        labelfontname=Helvetica


        ]


    node [
        fontname=Helvetica
        fontsize=12
        fontcolor=black

        regular=true
        shape=diamond
        // width=0.25
        // height=0.25
        ]



// --- # nodes  

{// records
node [shape=record, width=1]


b10 [label="  { R-7 | 5 } | B/10  "]
b20 [label="  { R-6 | 10 } | B/20  "]
b30 [label="  { R-5 | 10 } | B/30  "]
d10 [label="  { R-10 | 15 } | D/10  "]
d20 [label="  { R-9 | 10 } | D/20  "]
d30 [label="  { R-8 | 10 } | D/30  "]
a20 [label="  { R-2 | 5 } | A/20  "]
a30 [label="  { R-1 | 10 } | A/30  "]

}

{// circles
node [shape=circle]
e [label="E"]
c [label="C"]
}

{// box
node [shape=box]
a [label="A"]
}


//--- # edges

{
edge [weight = 1000] 

//straight
c -> b10 -> b20 -> b30
e -> d10 -> d20 -> d30
a20 -> a30 -> a

//combination
{b30 d30} -> a20
}


//--- # Clusters 

// subgraph cluster_1{
// label="a "
// e d10 d20
// }

// subgraph cluster_2{
// label="b "
// c b10 b20 b30
// }

// subgraph cluster_3{
// label="c "
// a30 a20
// }



// --- # bugfixes

{// c before e
edge [style=invis] 
c -> e


{rank=source e c} // force same rank before other nodes
}



}

这和我想要的一样干净整洁.
但是,我希望能够标记和评论结构的某些部分,我认为集群应该是正确的方法.

如果取消注释代码的CLUSTERS部分,则会得到以下代码和相应的图形:

digraph G {


//---graph config

    fontname=Helvetica

    rankdir = RL
    splines = polyline
    compound = true
    //concentrate = true

    labeljust = c
    labelloc = t

    ranksep=0.5
    nodesep=0.5

    //size="10,10"
    ratio=compress

    edge [
        minlen=1
        arrowsize=0.75
        labeldistance=5     

        fontname=Helvetica
        fontsize=12
        fontcolor=black     

        labelfontsize=12
        labelfontcolor=red
        labelfontname=Helvetica


        ]


    node [
        fontname=Helvetica
        fontsize=12
        fontcolor=black

        regular=true
        shape=diamond
        // width=0.25
        // height=0.25
        ]



// --- # nodes  

{// records
node [shape=record, width=1]


b10 [label="  { R-7 | 5 } | B/10  "]
b20 [label="  { R-6 | 10 } | B/20  "]
b30 [label="  { R-5 | 10 } | B/30  "]
d10 [label="  { R-10 | 15 } | D/10  "]
d20 [label="  { R-9 | 10 } | D/20  "]
d30 [label="  { R-8 | 10 } | D/30  "]
a20 [label="  { R-2 | 5 } | A/20  "]
a30 [label="  { R-1 | 10 } | A/30  "]

}

{// circles
node [shape=circle]
e [label="E"]
c [label="C"]
}

{// box
node [shape=box]
a [label="A"]
}


//--- # edges

{
edge [weight = 1000] 

//straight
c -> b10 -> b20 -> b30
e -> d10 -> d20 -> d30
a20 -> a30 -> a

//combination
{b30 d30} -> a20
}


//--- # Clusters 

subgraph cluster_1{
label="a "
e d10 d20
}

subgraph cluster_2{
label="b "
c b10 b20 b30
}

subgraph cluster_3{
label="c "
a30 a20
}



// --- # bugfixes

{// c before e
edge [style=invis] 
c -> e


{rank=source e c} // force same rank before other nodes
}



}

从代码末尾的错误修正部分可以看出,我希望节点C和E绝对出现在所有其他节点上方的“相同”级别.

此外,我希望记录的上下序列与第一个例子中的漂亮直线相连.我介绍的边缘的重量没有帮助.

有没有人知道如何解决这个问题,以及如何使graphviz产生一个漂亮的干净图形,如例#1只有3个拥抱框和相应的标签添加?

最佳答案 我试图只修改所需的内容:

>添加了一个没有label和style = invis的附加集群(对于d30)
>更改节点的顺序,使群集b高于群集a
>边缘重量被移除
>删除了错误修正部分
>删除了一些换行符

这是我最近的graphviz版本(2.29)得到的:

不完美,但更接近.

digraph G {
//---graph config

    fontname=Helvetica

    rankdir = RL
    splines = polyline
    compound = true
    //concentrate = true

    labeljust = c
    labelloc = t

    ranksep=0.5
    nodesep=0.5

    //size="10,10"
    ratio=compress

    edge [
        minlen=1
        arrowsize=0.75
        labeldistance=5     

        fontname=Helvetica
        fontsize=12
        fontcolor=black     

        labelfontsize=12
        labelfontcolor=red
        labelfontname=Helvetica
        ]

    node [
        fontname=Helvetica
        fontsize=12
        fontcolor=black

        regular=true
        shape=diamond
        // width=0.25
        // height=0.25
        ]

// --- # nodes  

{// records
node [shape=record, width=1]

d10 [label="  { R-10 | 15 } | D/10  "]
d20 [label="  { R-9 | 10 } | D/20  "]
d30 [label="  { R-8 | 10 } | D/30  "]
b10 [label="  { R-7 | 5 } | B/10  "]
b20 [label="  { R-6 | 10 } | B/20  "]
b30 [label="  { R-5 | 10 } | B/30  "]
a20 [label="  { R-2 | 5 } | A/20  "]
a30 [label="  { R-1 | 10 } | A/30  "]
}

{// circles
node [shape=circle]
e [label="E"]
c [label="C"]
}

{// box
node [shape=box]
a [label="A"]
}

//--- # edges

{

//straight
c -> b10 -> b20 -> b30
e -> d10 -> d20 -> d30
a20 -> a30 -> a

//combination
{b30 d30} -> a20}

//--- # Clusters 


 subgraph cluster_1{
 label="a "
 e d10 d20
 }

 subgraph cluster_2{
 label="b "
 c b10 b20 b30
 }

 subgraph cluster_3{
 label="c "
 a30 a20
 }

 subgraph cluster_4{
 label=""
 style=invis
 d30
 }

}
点赞