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

相关文章

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求