GraphViz DOT有向图 - 复杂案例分析(3)

469 阅读2分钟

文章目录

案例

案例来源

stackoverflow.com/questions/7…

案例代码

digraph start_up {
    { 
/* fake levels (level0 -> level1) and support nodes
 *
 * graphviz to charts is what latex is to documents, 
 * sometimes you'll have to fight it.
 * This is typically done by defining levels and connection points that
 * don't really have anything to do with your graph, but are used to 
 * force the graph to appear in a certain way.
 */
        node [shape=none, /*label="."*/]; l1a; l2a; l3a; l4a; l5a; l6a;
        node [shape=square label="no"]; l20a; 
    }

    {   /* connectiong point for the no arrow above "arrived" */
        node [width=0 shape=point label=""];
        d1; no;
    }

    node [style = rounded]; 
    node [shape = rect] start end;
    node [style = ""];

    node [shape = diamond]; {
        node [label="USB\nCommand\nArrived"]; arrived; 
        node [label="Has USB 3.0\nInterface Been\nSelected"]; selected;
        node [label="Initialize\nCode"]; init;
    }

    start -> init; 
    /*init -> arrived; */
    init -> d1 [arrowhead=none]; 
            d1 -> arrived;

/* 
 * tricky part:
 * since nodes in a digrap go either from top to bottom or left to right, we 
 * can usually not connect (->) two nodes and have them appear on the same 
 * level unless the connection is specified within a block that has the 
 * parameter `rank' set to `same'
 */
            l20a->no [arrowhead=none];

    {   rank=same; no -> arrived [dir=back arrowtail=none]; }
    {   rank=same; l20a -> d1; }

    /*arrived       -> arrived;*/ /*  [label="No" tailport=w headport=n]; */
    arrived     -> selected [label = "Yes"];
    selected    -> end


    /* just to demonstrate */
    l1a-> l2a-> l3a-> l4a-> l5a-> l6a;
}

在这里插入图片描述

案例精简保留重点,分析实现

digraph start_up {
    // 定义两个隐藏不可见节点d1, d2
    {
        node [width=0 shape=point label=""];
        d1; d2;
    }
    // 默认节点样式为diamond, 定义3个节点
    node [shape = diamond]; 
    {
        node [label="USB"]; usb; 
        node [label="USB 3.0\nInterface Been"]; usb3;
        node [label="do .."]; nodo;
    }

    // 开始节点指向隐藏节点d1, d1指向usb
    init -> d1 [arrowhead=none]; 
            d1 -> usb;

    // no节点指向隐藏节点d2
    nodo->d2 [arrowhead=none];

    // no节点和d1节点设置为同等级,并且指向
    {   rank=same; nodo -> d1; }
    // d2节点和usb节点设置为同等级, 并且提向
    {   rank=same; d2 -> usb [dir=back arrowtail=none label="no"]; }

    // yes 流程
    usb-> usb3[label = "Yes"];
}

在这里插入图片描述

分析实现点

  • 流程线条借助于两个隐藏节点
  • no节点指向d2确定上下关系,d1和usb节点确定上下关系
  • 隐藏节点和相应的节点rank对齐

案例中左侧进度实现原理是: 节点深度一样,它们就会处在同样的行中