程序员的绘图利器 — Graphviz

概述

 

官网:http://www.graphviz.org/

Graphviz (Graph Visualization Software) 是一个由AT&T实验室启动的开源工具包。DOT是一种图形描述语言,非常简单的,

Graphviz就是用来处理这种语言的工具。只需要简单了解一下DOT语言,就可以用Graphviz绘图了,它对程序员特别有用。

So in short, if you are a programmer, it is born for you。

 

无向图

graph graphname {
    a -- b -- c;
    b -- d;
}

 

有向图

digraph graphname {
    a -> b -> c;
    b -> d;
}

 

属性

//DOT语言中,可以对节点和边添加不同的属性。

digraph graphname {
    //节点的属性,节点的名称
    a [lable = "Foo"];

    //节点的属性,节点的形状
    b [shape = box];

    //边的属性,边的颜色
    a -> b -> c [color = blue];

    //边的属性,边的线状
    b -> d [style = dotted];
}

 

基本图形

digraph G {
//把图片的尺寸设为4inch * 4inch
size = "4,4";
main [shape = box];

//边的重要程度,默认是1
main->parse [weight = 8];
parse->execute;

//点状线
main->init[style = dotted];
main->cleanup;

//连接了两条线
execute->{make_string;printf}
init->make_string;

//把边的默认颜色设为red
edge [color = red];
main->printf [sytle=bold, label = "100times"];

//节点的名称
make_string [label = "make a\nstring"];

//设置节点的默认属性
node [shape=box,style =filled,color=lightgrey];
execute->compare;
}

 

 

多边形

digraph G{
a -> b -> c;
b -> d;

/* 形状为多边形,边数为5,外框为3条,颜色为淡蓝,样式为填充 */
a [shape = polygon, sides = 5, peripheries = 3, color = lightblue, style = filled];

/* 形状为多边形,边数为4,角的倾斜度为0.4,内容为hellow world*/
c [shape = polygon, sides = 4, skew = 0.4, label = "hello world"];

/* 形状为倒三角,整体旋转30度 */
d [shape = invtriangle,orientation = 30];

/* 形状为多边形,边数为4,扭曲度为0.7 */
e [shape = polygon, sides = 4, distortion = 0.7];
}

 

 

数据结构

 

(1)复杂的标签

digraph structs {
/* 把节点的默认形状设为矩形record,默认的是圆角矩形Mrecord */
node [shape = record];

struct1 [label = "left|middle|right"];
struct2 [label = "one|two"];
struct3 [label = "hello\nworld|{b|{c|d|e}|f}|g|h"];

struct1 -> struct2;
struct1 -> struct3;
}


 

graph picture {
//这幅图的名字
label = "I love you";

//图名字的位置在bottom,也可以是t
labelloc = b;

//图名字的位置在left,也可以是r
labeljust = l;

edge[decorate = true];

C -- D [label = "s1"];
C -- E [label = "s2"];
C -- F [label = "s3"];
D -- E [label = "s4"];
D -- F [label = "s5"];

edge[decorate = false, labelfontcolor = blue, fontcolor = red];
C1 -- D1 [headlabel = "c1",taillabel = "d1",label = "c1 - d1"];
}


 

 

(2)行列对齐

digraph html {
rankdir = LR;
{
node[shape = plaintext];
1995 -> 1996 -> 1997 -> 1998 -> 1999 -> 2000 -> 2001;
}
{
node[shape = box, style = filled];
WAR3 -> Xhero -> Footman -> DOTA:
WAR3 -> Battleship;
}
{rank = same; 1996; WAR3;}
{rank = same; 1998; Xhero; Battleship;}
{rank = same; 1999; Footman;}
{rank = same; 2001; DOTA;}
}

 

 

(3)二叉树

digraph G {
label = "Binary search tree";
node [shape = record];

A [label = "<f0>|<f1>A|<f2>"];
B [label = "<f0>|<f1>B|<f2>"];
C [label = "<f0>|<f1>C|<f2>"];
D [label = "<f0>|<f1>D|<f2>"];
E [label = "<f0>|<f1>E|<f2>"];
F [label = "<f0>|<f1>F|<f2>"];
G [label = "<f0>|<f1>G|<f2>"];

A:f0 -> B:f1;
A:f2 -> C:f1;
B:f0 -> D:f1;
B:f2 -> E:f1;
C:f0 -> F:f1;
C:f2 -> G:f1;
}

 

 

(4)哈希表

digraph G{
nodesep = .05;
rankdir = LR;

node [shape = record,width = .1,height = .1];
node0 [label = "<f0>|<f1>|<f2>|<f3>|<f4>|<f5>|<f6>|",height = 2.5];

node [width = 1.5];
node1 [label = "{<n>n14|719|<p>}"];
node2 [label = "{<n>a1|805|<p>}"];
node3 [label = "{<n>i9|718|<p>}"];
node4 [label = "{<n>e5|989|<p>}"];
node5 [label = "{<n>t20|959|<p>}"];
node6 [label = "{<n>o15|794|<p>}"];
node7 [label = "{<n>s19|659|<p>}"];

node0:f0 -> node1:n;
node0:f1 -> node2:n;
node0:f2 -> node3:n;
node0:f5 -> node4:n;
node0:f6 -> node5:n;
node2:p -> node6:n;
node4:p -> node7:n;
}


 

 

流程图

digraph G{
subgraph cluster0 {
node [style = filled,color = white];
style = filled;
color = lightgrey;
a0 -> a1 -> a2 -> a3;
label = "process #1";
}

subgraph cluster1 {
node [style = filled];
b0 -> b1 -> b2 -> b3;
label = "process #2";
color = blue;
}

start -> a0;
start -> b0;
a1 -> b3;
b2 -> a3;
a3 -> a0;
a3 -> end;
b3 -> end;
start [shape = Mdiamond];
end [shape = Msquare];
}


 

Reference

[1] http://zh.wikipedia.org/zh-cn/DOT语言,DOT语言简明介绍。

[2] http://zh.wikipedia.org/zh/Graphviz,简单背景知识。

[3] Graphviz中文指南。

 

  • 15
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Graphviz是一个开源的图形可视化工具,可以用于绘制各种类型的图形,如流程图、组织结构图、网络拓扑图等。要配置Graphviz,可以按照以下步骤进行操作: 1. 下载Graphviz:首先,你需要从Graphviz官方网站(https://graphviz.org/)下载适合你操作系统的安装包。Graphviz支持多个操作系统,包括Windows、Mac和Linux。 2. 安装Graphviz:下载完成后,运行安装程序并按照提示进行安装。在安装过程中,你可以选择自定义安装路径和组件。 3. 配置环境变量(仅适用于Windows):如果你使用的是Windows操作系统,需要将Graphviz的安装路径添加到系统的环境变量中。具体步骤如下: - 在Windows搜索栏中输入“环境变量”,并打开“编辑系统环境变量”。 - 在弹出的窗口中,点击“环境变量”按钮。 - 在“系统变量”部分,找到名为“Path”的变量,并点击“编辑”。 - 在弹出的窗口中,点击“新建”并添加Graphviz的安装路径(例如:C:\Program Files\Graphviz\bin)。 - 确认所有窗口并保存更改。 4. 验证安装:完成上述步骤后,可以通过在命令行中输入以下命令来验证Graphviz是否成功安装: ``` dot -V ``` 如果成功安装,将显示Graphviz的版本信息。 配置Graphviz后,你可以使用其提供的命令行工具或者集成到你的开发环境中来绘制图形。如果你使用的是Python,可以使用Graphviz的Python包(pygraphvizgraphviz)来生成图形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值