现在的位置: 首页 > 综合 > 正文

使用graphviz画程序流程图

2016年05月06日 ⁄ 综合 ⁄ 共 1417字 ⁄ 字号 评论关闭

graphviz是一个画程序流程图的基于dot语言的工具,非常的好用。

Hello World

digraph g {
    Hello -> World
}

使用dot -Tpng -o hello.png ./hello.dot生成png格式的图片如下:

中文支持

要在生成的图片中显示中文,需要使用支持中文的字体,这里我们使用微软雅黑:

mkdir ~/.fonts/
cp msyh.ttf ~/.fonts/
fc-cache

执行上面的几个命令后,我们就可以使用微软雅黑字体了,代码如下:

graph g {
    node [
        fontname="Microsoft YaHei"
    ]
    你好 -- Hello
}

生成的图片如下:

执行dot命令的时候,加上-v参数,可以查看详细的信息,这个有助于我们调试,比如执行dot -v -Tpng -o chinese.png chinese.dot后,我们可以在输出中看到下面的信息:

从这里我们可以发现字体Microsoft YaHei被解析成了msyh.ttf这个文件,这是正确的。

更多示例

话不多说了,直接上代码和生成的图片:

digraph g {
    node [
        shape=circle,
        style=filled,
        color="#000000",
        fixedsize=true
        width=0.30
        height=0.30
    ];
    
    start [ label="" ];
   
    node [
        shape=doublecircle,
        style=filled,
        color="#000000"
        fixedsize=true
        width=0.3
        height=0.3
    ];
    end   [ label="" ];
    
    node [
        shape=rectangle
        style="solid,rounded"
        fontname="Microsoft YaHei"
        fixedsize=false
        margin="0.20,0.10"
    ];
    step1 [ label="1) 初始化所有事件模块的ctx_index序号" ];
    step2 [ label="2) 分配指针数组,存储所有事件模块生成的配置项结构体指针" ];
    step3 [ label="3) 调用所有事件模块的create_conf方法" ];
    step4 [ label="4) 为所有事件模块解析nginx.conf配置文件" ];
    step5 [ label="5) 调用所有事件模块的init_conf方法" ];
    start -> step1;
    step1 -> step2;
    step2 -> step3;
    step3 -> step4;
    step4 -> step5;
    step5 -> end;
}

digraph g {
    rankdir=BT
    graph [
        splines=ortho
    ]
    node [
        fontname="verdana"
        shape=circle
        fixedsize=true
        width=0.00
        height=0.00
    ]
   
    nothing [label=""]
    
    node [
        fontname="verdana"
        shape="box"
        style="filled"
        fillcolor="#B2D4EB"
        fixedsize=true
        width=1.4
        height=0.3
    ]

    nothing -> Investment [arrowhead="onormal" arrowsize=1.5]
    Stock -> nothing [arrowhead="none"]
    Bond -> nothing [arrowhead="none"]
    RealEstate -> nothing [arrowhead="none"]
}

抱歉!评论已关闭.