graphviz的基础使用

2024-05-25 06:36
文章标签 基础 使用 graphviz

本文主要是介绍graphviz的基础使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Graphviz官网文档
在线编辑器
d3-graphviz文档

  • 字符串使用双引号,可使用 \n 进行换行,b [label = “two \n lines”]
  • 有向图digraph,节点关系指向使用 ->
    无向图graph,节点关系指向使用
  • node[label = “内容”],'内容’则为该node的显示内容
    node1 -> node2[label = “内容”],'内容’则为node1指向node2的边的内容
    在这里插入图片描述
  • rankdir可以设置边的走向,包括:LR,RL,TB,BT
    rankdir=LR在这里插入图片描述
  • 构造边
    一对一:a -> b
    一对多:a -> {b, c, d}
    多对一:{b, c, d} -> a
    多对多:{e, f, g} -> {h, i, j}
  • node[shape=box],通过shape来设定node的形状
    节点形状
    shape=polygon,多边形
    sides=5,五条边
    peripheries=3,三层边框
    style=filled,样式为颜色填充
    fillcolor=gray,填充颜色为gray
    color=lightblue || color = “.7 .3 1.0”,边框颜色为lightblue,若没有设定fillcolor,则填充颜色也为lightblue
    skew=.4,节点倾斜角度,正值使多边形顶部向右倾斜,负向左。
    distortion=.7,正值会导致顶部大于底部;负值的作用正好相反。
    regular = true,可以使你的多边形是一个规则的多边形
    orientation = * ,可以让你的多边形旋转*角度
    在这里插入图片描述
    在这里插入图片描述

dir设置箭头:forward,back,both,none
A->B [dir=forward]
在这里插入图片描述
设置箭头样式:arrowhead=curve
箭头样式
在这里插入图片描述
只有当dir为back或both时,arrowtail才生效

digraph b {edge[dir=both]a->b[arrowtail=box, arrowhead=curve]
}

在这里插入图片描述

设置箭头箭尾lable
headlabel=“哈哈”, taillabel=“嚯嚯”
在这里插入图片描述

  • 设置指定的元素在同一层:{rank = same a, d}
    rank的选项:same,min,source,max
    图最开始的地方为最小,终点处为最大,如果,rankdir=LR,那么{rank=min e, f}则ef位于最左侧,其他同理
    rank属性
    在这里插入图片描述
    在这里插入图片描述
    rank = same处于同层
digraph G{rankdir = LR{node[shape = plaintext]1995 -> 1996 -> 1997 -> 1998 -> 1999 -> 2000 -> 2001}{node[shape = box, style = filled]WAR3 -> Xhero -> Footman -> DOTAWAR3 -> Battleship}{{rank = same 1996 WAR3}{rank = same 1998 Xhero Battleship}{rank = same 1999 Footman}{rank = same 2001 DOTA}}
}

在这里插入图片描述

  • 设置 边 的label是否有连线:decorate = true
    在这里插入图片描述

  • label对齐方式:labelloc = “t”
    如果labelloc=t、labelloc=c、labelloc=b,则标签将分别与节点的顶部对齐、居中对齐或与底部对齐。
    默认情况下,标签垂直居中。根图标签位于底部,clusters标签位于顶部。
    对于graphs 和 clusters,只允许使用labelloc=t和labelloc=b,分别对应于顶部和底部的位置。
    labelloc
    在这里插入图片描述

  • labeljust = l,图标签的位置在左边,也可以设置为r到右边
    在这里插入图片描述

  • weight: 边的权重,该权重在布局图时会被考虑。较重的边会更短。
    A -> B [weight=2]

  • record 是由横竖的矩形组成的图形
    < f0 >< f1 >< f2 >是port点,可以在节点连接时作为连接点,
    比如:struct1:f0 -> struct2:f1,则是从struct1的f0的位置连接到struct2的f1位置

digraph G{node[shape = record]struct1[label = "<f0> left | <f1> middle | <f2> right"]struct2[label = "<f0> one | <f1> two"]struct3[label = "hello \nworld | {b | {c | <here> d | e} | f} | g | h"]struct1 -> struct2struct1 -> struct3
}

在这里插入图片描述

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>"]A: f0: sw -> B: f1A: f2: se -> C: f1B: f0: sw -> D: f1B: f2: se -> E: f1
}

在这里插入图片描述

  • portPos属性 portPos
    节点的 port 属性是指节点连接另一个节点的线条端点位置,端口的位置有8种,分别为节点的东、南、西、北、东南、东北、西南、西北,属性的值分别为e, s, w, n, se, ne, sw, nw
    有两种类型的 port 属性:
    1. 一种使指定源节点的端点位置,使用 tailport 属性,如下脚本指定 a节点的端点位置为东:

    digraph G {a -> b [tailport = e];
    }
    

    在这里插入图片描述

    1. 一种指定目的节点的端点位置,使用:pos语法,如下脚本指定b节点的端点位置为西:
    digraph G {a -> b:w;
    }
    

    在这里插入图片描述
    也可以通过上述语法指定 record 形状的域字段(如f1)的端点位置:

    digraph G {a -> b:f1:w;
    }
    

    其他示例:

    digraph G{node[shape = box]c:n -> d[label = n]c1:ne -> d1[label = ne]c2:e -> d2[label = e]c3:se -> d3[label = se]c4:s -> d4[label = s]c5:sw -> d5[label = sw]c6:w -> d6[label = w]c7:nw -> d7[label = nw]
    }
    

在这里插入图片描述
tailport和headport与"n",“ne”,“e”,“se”,“s”,“sw”,“w"和"nw”的作用相同

digraph G{node[shape = box]a->b[tailport = n, label = n]a1->b1[tailport = ne, label = ne]a2->b2[tailport = e, label = e]a3->b3[tailport = se, label = se]a4->b4[tailport = s, label = s]a5->b5[tailport = sw, label = sw]a6->b6[tailport = w, label = w]a7->b7[tailport = nw, label = nw]
}

在这里插入图片描述

digraph G{node[shape = box]a->b[tailport = e, headport=s]
}

在这里插入图片描述

  • 设置headlabel、taillabel以及label样式
    labelfontcolor设置headlabel和taillabel
    fontcolor设置label样式
    在这里插入图片描述
  • 先使用 compound=true,然后就能用lhead和ltail设置连接的子图,将一条边连到一个子图的边界上
    使用bgcolor修改背景色
    子图的名称必须以cluster开头,否则graphviz无法设别
digraph {compound=true;bgcolor="lightblue";subgraph cluster_a {bgcolor="lightgreen"label="Cluster A";node1; node3; node5; node7;}subgraph cluster_b {bgcolor="lightyellow"label="Cluster B";node2; node4; node6; node8;}node1 -> node2 [label="1"];node3 -> node4 [label="2" ltail="cluster_a"];node5 -> node6 [label="3" lhead="cluster_b"];node7 -> node8 [label="4" ltail="cluster_a" lhead="cluster_b"];
}

在这里插入图片描述

  • 插入图片
digraph {c[shape = none, image = "./pic.png"]a -> b -> c;c -> d;
}

在这里插入图片描述

  • 时序图
digraph time {rankdir = "LR";node[shape = "point", width = 0, height = 0]edge[arrowhead = "none", style = "dashed"]{rank = "same"edge[style = "solided"];APP[shape = "plaintext"]; // 将 APP 这个节点以 plaintext 的形式展现出来,其他的依旧是 pointAPP -> step00 -> step01 -> step02 -> step03 -> step04 -> step05;}{rank="same";edge[style="solided"];SDK[shape="plaintext"];SDK -> step10 -> step11 -> step12 -> step13 -> step14 -> step15;}{rank="same";edge[style="solided"];AliPay[shape="plaintext"];AliPay -> step20 -> step21 -> step22 -> step23 -> step24 -> step25;}{rank="same";edge[style="solided"];Server[shape="plaintext"];Server -> step30 -> step31 -> step32 -> step33 -> step34 -> step35;}step00 -> step10 [label="sends order info", arrowhead="normal"];step11 -> step21 [label="open AliPay", arrowhead="normal"];step22 -> step12 [label="pay success", arrowhead="normal"];step13 -> step03 [label="pay success", arrowhead="normal"];step24 -> step34 [label="pay success", arrowhead="normal"];
}

在这里插入图片描述

  • 使用HTML实现一个表格节点实体(注意:html不区分大小写)
    更多html-like语法:graphviz官网中对record节点的介绍
digraph html_label_Example
{label = "html_like_label_example";bgcolor="transparent";//背景透明html_ex_node[shape=record, margin=0, label=<<table border="0" cellborder="1" cellspacing="0" cellpadding="4"><tr><td rowspan="2">test</td><td>a</td>                <td rowspan="2">HTML-Like<br/>label</td></tr><tr><td>b</td></tr></table>>];
}

在这里插入图片描述

  • style属性
    style 属性用于修改节点的外观,当前,支持8种类型的 style:filled, invisible, diagonals, rounded. dashed, dotted, solid, bold

    1. filled : 此值指示应填充节点的内部。使用的颜色是 fillcolor 定义的,若 fillcolor 属性未定义,则使用 color 属性的颜色。对于未填充的节点,节点内部对当前图形或簇背景色的任何颜色都是透明的。请注意,点形状始终是填充的。
    digraph G {rankdir=LRnode [shape=box, color=blue]node1 [style=filled] node2 [style=filled, fillcolor=red] node0 -> node1 -> node2
    }
    

    在这里插入图片描述

    1. invisible : 不可见。设置此样式会导致节点根本不显示。请注意,节点仍用于布局图形。
    digraph G {rankdir=LRnode [shape=box, color=blue]node0 [style=invisible, color=red] node0 -> node1
    }
    

    在这里插入图片描述
    3. diagonals: 斜线 。“斜线”样式会导致在节点多边形的顶点附近绘制小斜线。

    digraph G {rankdir=LRnode [shape=box, color=blue]node0 [style=diagonals] 
    }
    

    在这里插入图片描述

    1. rounded :圆形的,使节点的边变得圆滑,可以作用在 record 形状上。
    digraph R {rankdir=LRnode [style=rounded]node1 [shape=box]node2 [fillcolor=yellow, style="rounded,filled", shape=diamond]node3 [shape=record, label="{ a | b | c }"]node1 -> node2 -> node3
    }
    

    在这里插入图片描述

    1. dashed : 使节点的边变为虚线
    2. dotted : 使节点的边变为点线
    3. solid : 使节点的边变为直线,默认属性
    4. bold : 使节点的边线加粗
  • 直线曲线
    splines=false的情况下,线条都为直线

    digraph g{splines=false;a->b;b->c;a->c;
    }
    

    在这里插入图片描述

  • 线条的链接点
    headport 和 tailport取值决定链接点
    headclip 和 tailclip取值决定出发点
    headclip:如果为true,则将边的头剪裁到头节点的边界。否则,边的末端将剪裁到节点的中心或端口的中心(如果适用)。
    tailclip:如果为true,则将边的尾部剪裁到尾部节点的边界。否则,边的末端将指向节点的中心或端口的中心(如果适用)。

    在这里插入图片描述

  • 是否约束,只划线,不影响节点布局计算
    如果为false,则在对节点进行排名时不使用边

    digraph G {a -> c;a -> b;b -> c [constraint=false];a1 -> c1;a1 -> b1;b1 -> c1 [constraint=true];
    }
    

    在这里插入图片描述

参考文章:
graphviz的基本语法及使用
DOT + graphviz 轻松画图
一小时实践入门 Graphviz
十分钟了解绘图神器——Graphviz学习笔记
转+更新 Graphviz 教程,例子+ 高级应用 写代码,编程绘制架构图(分层拓扑图) 转自官网
Graphviz的使用指南
markdown 绘图利器之graphviz
GraphViz DOT有向图 (六)edge线条控制

这篇关于graphviz的基础使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1000747

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v