利用Graphviz绘制逻辑关系依赖图

Posted 技术即艺术

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用Graphviz绘制逻辑关系依赖图相关的知识,希望对你有一定的参考价值。

说明:在很多情况下,需要将复杂且有些规律的代码整理成逻辑片段,这个时候就需要画图,很多时候图比代码更加直观

Graphviz是一个比较好的绘图工具,可以通过简单的代码绘制出复杂的逻辑图,且其代码就像平时说话的语言一样直观

案例中是我在开发中整理的一个复杂的sql中的表依赖关系:

代码:

digraph sbjob{
graph [fontname=Arial, nodesep=0.125, ranksep=0.25];
node [fontcolor=black, fontname=Arial, height=0, shape=box, style=filled, width=0];
edge [fontname=Arial, len=3.5];

INT_STG_OCS_TB_OCS_ACCOUNTEXT_SUBSCRIBER_SD [label="INT_STG_OCS_\nTB_OCS_ACCOUNTEXT_\nSUBSCRIBER_SD"]

STG_OCS_TB_OCS_ACCOUNTEXT_SUBSCRIBER [label="STG_OCS_TB_\nOCS_ACCOUNTEXT\n_SUBSCRIBER"]

LOAD_DATE_PARAMETERS_TBL [label="LOAD_DATE_\nPARAMETERS_TBL"]

SUBSCRIBER_AUDIT_TB [label="SUBSCRIBER_\nAUDIT_TB"]

STG_INF_CONTACT_PERSON [label="STG_INF_CONTACT\n_PERSON"]

INT_STG_INF_CONTACT_PERSON [label="INT_STG_INF_\nCONTACT_PERSON"]

STG_INF_CUSTOMER_ALL [label="STG_INF_\nCUSTOMER_ALL"]

INT_STG_INF_CUSTOMER_ALL [label="INT_STG_INF_\nCUSTOMER_ALL"]

STG_OCS_ACCOUNTS_SUBCRIBER [label="STG_OCS_ACCOUNTS\n_SUBCRIBER"]

INT_STG_OCS_ACCOUNTS_SUBCRIBER [label="INT_STG_OCS_\nACCOUNTS_SUBCRIBER"]

STG_CRM_PERSON_SUBSCRIBER [label="STG_CRM_PERSON_\nSUBSCRIBER"]

INT_STG_CRM_PERSON_SUBSCRIBER [label="INT_STG_CRM_PERSON_\nSUBSCRIBER"]

STG_T_S_OCS_TSNATIVENUMHEAD [label="STG_T_S_OCS_\nTSNATIVENUMHEAD"]

INT_STG_T_S_OCS_TSNATIVENUMHEAD [label="INT_STG_T_S_OCS_\nTSNATIVENUMHEAD"]

SUBSCRIBER_DIMENSION_STG [label="SUBSCRIBER_\nDIMENSION_STG"]

INT_STG_CRM_SUBSCRIBER_SD [label="INT_STG_CRM_\nSUBSCRIBER_SD"]

INT_STG_CBE_SUBSCRIBER_SD [label="INT_STG_CBE_\nSUBSCRIBER_SD"]

IMEI_MOBILEBRAND [label="IMEI_MOBILEBRAND"]

SUBSCRIBER_TEMP_TB [label="SUBSCRIBER_\nTEMP_TB"]

SUBSCRIBER_DIMENSION [label="SUBSCRIBER_\nDIMENSION"]

SUBSCRIBER_REGION_TB [label="SUBSCRIBER_\nREGION_TB"]

SUBSCRIBER_TEMP_TB [label="SUBSCRIBER_TEMP_TB"]

AUDIT_TB [label="AUDIT_TB"]

//////////////////////////////////////////////////////

INT_STG_OCS_TB_OCS_ACCOUNTEXT_SUBSCRIBER_SD ->
{STG_OCS_TB_OCS_ACCOUNTEXT_SUBSCRIBER,
LOAD_DATE_PARAMETERS_TBL}[color="#0d1976", fontcolor="#0d1976", label="1"]
//---

SUBSCRIBER_AUDIT_TB ->
AUDIT_TB[color="#BBFFFF", fontcolor="#BBFFFF", label="2"]
//---
STG_INF_CONTACT_PERSON ->
{AUDIT_TB,
LOAD_DATE_PARAMETERS_TBL,
INT_STG_INF_CONTACT_PERSON}[color="#FFFF00", fontcolor="#FFFF00", label="3"]
//---
STG_INF_CUSTOMER_ALL ->
{AUDIT_TB,
LOAD_DATE_PARAMETERS_TBL,
INT_STG_INF_CUSTOMER_ALL}[color="#EE0000", fontcolor="#EE0000", label="4"]
//---
STG_OCS_ACCOUNTS_SUBCRIBER ->
{SUBSCRIBER_AUDIT_TB,
LOAD_DATE_PARAMETERS_TBL,
INT_STG_OCS_ACCOUNTS_SUBCRIBER}[color="#9ACD32", fontcolor="#9ACD32", label="5"]

STG_CRM_PERSON_SUBSCRIBER ->
{SUBSCRIBER_AUDIT_TB,
LOAD_DATE_PARAMETERS_TBL,
INT_STG_CRM_PERSON_SUBSCRIBER}[color="#98F5FF", fontcolor="#98F5FF", label="6"]
//---
STG_T_S_OCS_TSNATIVENUMHEAD ->
{AUDIT_TB,
LOAD_DATE_PARAMETERS_TBL,
INT_STG_T_S_OCS_TSNATIVENUMHEAD}[color="#7D26CD", fontcolor="#7D26CD", label="7"]
//---
SUBSCRIBER_DIMENSION_STG ->
{INT_STG_CRM_SUBSCRIBER_SD,
INT_STG_CRM_PERSON_SUBSCRIBER,
INT_STG_CBE_SUBSCRIBER_SD,
INT_STG_OCS_TB_OCS_ACCOUNTEXT_SUBSCRIBER_SD,
INT_STG_OCS_ACCOUNTS_SUBCRIBER,
IMEI_MOBILEBRAND,
SUBSCRIBER_AUDIT_TB,
INT_STG_INF_CONTACT_PERSON,
INT_STG_INF_CUSTOMER_ALL}[color="#838B83", fontcolor="#838B83", label="8"]
//---
SUBSCRIBER_TEMP_TB ->
{AUDIT_TB,
SUBSCRIBER_DIMENSION,
SUBSCRIBER_DIMENSION_STG}[color="#B8860B", fontcolor="#B8860B", label="9"]

SUBSCRIBER_REGION_TB ->
{INT_STG_T_S_OCS_TSNATIVENUMHEAD,
SUBSCRIBER_DIMENSION_STG,
SUBSCRIBER_TEMP_TB,
SUBSCRIBER_DIMENSION_STG,
SUBSCRIBER_TEMP_TB} [color="#B3EE3A", fontcolor="#B3EE3A", label="10"]

// update
SUBSCRIBER_AUDIT_TB ->
{SUBSCRIBER_TEMP_TB,
SUBSCRIBER_DIMENSION_STG,
SUBSCRIBER_AUDIT_TB}[color="#B22222", fontcolor="#B22222", label="11"]

// update
SUBSCRIBER_DIMENSION ->
SUBSCRIBER_TEMP_TB[color="#030303", fontcolor="#030303", label="12"]

SUBSCRIBER_DIMENSION ->
{SUBSCRIBER_DIMENSION_STG,
SUBSCRIBER_TEMP_TB,
SUBSCRIBER_REGION_TB}[color="#32CD32", fontcolor="#32CD32", label="13"]

// update
SUBSCRIBER_DIMENSION ->
{SUBSCRIBER_DIMENSION_STG,
SUBSCRIBER_TEMP_TB}[color="#8B0000", fontcolor="#8B0000", label="14"]

}

保存为.dot文件

在cmd下执行命令(前提是你安装了Graphviz并配置了环境变量,这个请自行百度):

neato C:\Users\meij\Desktop\graph3.gv -Tpng -o sb_job_tab.png

解释:neato是生成某一种图形样式,Graphviz提供了很多样式

        也可以生成.img文件

大家安装客户端后可以按照上面的代码和步骤就可以实现生成图片,结果因为图片太大不在这里展示。

技术分享

以上是关于利用Graphviz绘制逻辑关系依赖图的主要内容,如果未能解决你的问题,请参考以下文章

doxygen+graphviz轻松绘制函数调用图(call graph)

使用 Graphviz 绘制 NetworkX 图

graphviz绘制图和树,python

社会关系网络网怎么绘制

算法图绘制工具Graphviz

用Graphviz绘制好看网络图