结合Dot语言和工具graphviz来画图

来源:互联网 发布:二级ms office软件 编辑:程序博客网 时间:2024/05/28 23:21

按照经验,画图可以分两种形式: 所见即所得, 所想即所得。

所见即所得, 是我们平常用的最多的一种方法,利用画图工具,进行元件的拖拉和组合,很容易就得到我们想要的图。

而所想即所得,可能有点抽象, 我们只是计划图里应该有什么元素,每个元素之间是什么关系;在定义了元素和关系后,  利用画图工具画出我们想要的图。

一般来说,用第一种方法就足够了。但是如果这个图比较复杂,而且可能需要根据构想来随时更改, 第一种方法总显得有点费劲吧。可能只是我没见识到真正的画图神器?

无论怎么说,我发觉现在用graphviz+dot更顺手了, 因为可看作是可编程和可配置,图的修改和优化的方便性不是一般的爽。


至于如何使用Dot语言以及如何使用graphviz生成图片, 可以参考下官方文档或网上的例子。

这里展示一个系统结构图的构建例子, 直接上脚本:

digraph G {rankdir=TB;fontname = "Microsoft YaHei";fontsize = 12;bgcolor=grey;node [ fontname = "Microsoft YaHei", fontsize = 11, shape = "oval", color=white, fontcolor=white];edge [ fontname = "Microsoft YaHei", fontsize = 10 , color=white, fontcolor=gray];User [label="用户", shape="egg", style=filled, color=goldenrod];      subgraph cluster1 {          fontname = "Microsoft YaHei";          fontsize = 12;          label="#APPs#";          bgcolor="mintcream";          node [shape = "ellipse", style=filled,color=skyblue, fontcolor=black];          android_app [label="android/ios app"];      };      subgraph cluster2 {          fontname = "Microsoft YaHei";          fontsize = 12;          label="#API云端#";          bgcolor="mintcream";          node [shape = "ellipse", style=filled,color=skyblue, fontcolor=black];          api_server [label="API Server"];      };      subgraph cluster3 {          fontname = "Microsoft YaHei";          fontsize = 12;          label="#IoT云端#";          bgcolor="mintcream";          node [shape = "ellipse", style=filled,color=skyblue, fontcolor=black];          iot_server [label="IoT Server"];      };      subgraph cluster4 {          subgraph cluster4_s1 {              fontname = "Microsoft YaHei";              fontsize = 9;              label="#外围设备#";              bgcolor="dimgrey";              node [shape = "box", style=filled,color=skyblue, fontcolor=black];              cluster4_worker [label="各功能工作部件"];          };          subgraph cluster4_s2 {              fontname = "Microsoft YaHei";              fontsize = 9;              label="#外围设备#";              bgcolor="dimgrey";              node [shape = "box", style=filled,color=skyblue, fontcolor=black];              cluster4_other_2 [label="..."];              cluster4_voicecard [label="声卡设备"];              cluster4_control_panel [label="遥控器"];              cluster4_sensor [label="感应器"];          };          subgraph cluster4_s3 {              fontname = "Microsoft YaHei";              fontsize = 9;              label="#外围设备#";              bgcolor="dimgrey";              node [shape = "box", style=filled,color=skyblue, fontcolor=black];              cluster4_wifi [label="WiFi(Gateway)"];          };        fontname = "Microsoft YaHei";        fontsize = 12;        fontcolor=white;        label="#智能设备系统#";        bgcolor="#202020";        node [ fontname = "Microsoft YaHei", fontsize = 11, shape = "oval", color=white, fontcolor=white];        edge [ fontname = "Microsoft YaHei", fontsize = 10 , color=white, fontcolor=gray];        Start [label="启动", shape="doublecircle",style=filled, color=burlywood];        MainInitiator [label="系统初始化", shape="oval",style=filled, color=burlywood];        SignalMonitor [label="信号检测", shape="oval",style=filled, color=burlywood];        ControlCenter [label="控制中心", shape="oval",style=filled, color=burlywood];        NetworkManager [label="网络管理", shape="oval",style=filled, color=burlywood];        VoiceManager [label="语音管理", shape="oval",style=filled, color=burlywood];        WarningManager [label="warnning管理", shape="oval",style=filled, color=burlywood];        DeviceIot [label="IoT服务入口", shape="oval",style=filled, color=burlywood];        DeviceAPICli [label="API服务入口", shape="oval",style=filled, color=burlywood];        CMD_MQ [label="指令队列", shape=box, style=filled, height=.1, width=.5, color=yellowgreen];        WARNING_MQ [label="报警队列", shape=box, style=filled, height=.1, width=.5, color=yellowgreen];        Dev_Status [label="设备状态缓存", shape=box, style=filled, height=.2, width=.5, color=steelblue];        ControlCenter->Dev_Status->ControlCenter [color=white, style=dashed];        ControlCenter->cluster4_worker[label="控制", color=grey, style=solid];        ControlCenter->ControlCenter [label="安全机制", color=grey, style=solid];        SignalMonitor->VoiceManager[label="启动/关闭", color=green, style=dashed];        SignalMonitor->ControlCenter[label="启动/关闭", color=green, style=dashed];        SignalMonitor->WARNING_MQ [label="写入串口报警", color=red, style=solid];        SignalMonitor->CMD_MQ [label="写入串口指令", color=red, style=solid];        Start->MainInitiator [color=green, style=dashed];        MainInitiator->MainInitiator [label="系统自检", color=green, style=solid];        MainInitiator->SignalMonitor[label="启动Step1", color=green, style=dashed];        MainInitiator->ControlCenter[label="启动Step2", color=green, style=dashed];        MainInitiator->NetworkManager[label="启动Step3", color=green, style=dashed];        MainInitiator->VoiceManager[label="启动Step4", color=green, style=dashed];        NetworkManager->DeviceIot[label="启动Step1", color=green, style=dashed];        NetworkManager->DeviceAPICli[label="启动Step2", color=green, style=dashed];        cluster4_voicecard->VoiceManager [color=grey, style=solid];        VoiceManager->CMD_MQ [label="写入语音指令", color=red, style=solid];        DeviceIot->CMD_MQ [label="写入远程指令", color=red, style=solid];        DeviceAPICli->CMD_MQ [label="写入远程指令", color=red, style=dashed];        DeviceIot->cluster4_wifi [label="发布/订阅", color=grey, style=dashed];        DeviceAPICli->cluster4_wifi [label="账号/设置/日志", color=grey, style=dashed];        CMD_MQ->ControlCenter [label="读取MQ队列", color=red, style=dashed];        WARNING_MQ->WarningManager->cluster4_voicecard [label="报警", color=red, style=dashed];        //WarningManager->cluster4_voicecard [color=grey, style=dashed];        cluster4_control_panel->SignalMonitor [label="信号", color=grey, style=solid];        cluster4_sensor->SignalMonitor [label="信号", color=grey, style=solid];    };User->cluster4_voicecard [color=sienna, style=solid];User->cluster4_control_panel [color=sienna, style=solid];User->android_app [color=sienna, style=solid];api_server->cluster4_wifi [label="账号/设置", fontcolor=black, color=black, style=dashed];iot_server->cluster4_wifi [label="发布/订阅", fontcolor=black, color=black, style=dashed];android_app->cluster4_wifi [label="wifi设置/账号绑定", fontcolor=black, color=black, style=dashed];android_app->api_server [label="账号/设置", fontsize = 10, fontcolor=black, color=black, style=dashed];api_server->android_app [fontsize = 11, fontcolor=black, color=black, style=dashed];api_server->iot_server [label="发布/订阅", fontsize = 10, fontcolor=black, color=black, style=dashed];iot_server->api_server [fontsize = 11, fontcolor=black, color=black, style=dashed];}
该脚本在graphviz里运行后,得到如下效果图:


各种元素楚河汉界泾渭分明,而且可以随时修改脚本。至于如何排版布局,交由graphviz去完成就好。

1 0