Graph Easy安装和使用

序言

graph easy简要介绍

如何快速使用Graph Easy画出一个 Geek 的 Ascii 图

graph-easy 读取图形的描述(节点和边的连接网络,而不是饼图:-),然后将其转换为所需的输出格式。

graph-easy支持下面格式输入:

graph-easy支持下面格式输出:

// 简单的转成dot,再通过dot命令转成png文件
graph-easy graph.txt --dot | dot -Tpng -o graph.png
graph-easy graph.txt --png

Graph Easy Mac 安装和简单使用

  1. 安装graph : brew install graphviz
  2. 配置CPAN的镜像,Comprehensive Perl Archive Network (CPAN),类似于 PEAR,是 Perl 的一个网络库
  3. 安装Graph:Easy: sudo cpan Graph:Easy
  • 阿里云的 CPAN 镜像:https://developer.aliyun.com/mirror/CPAN
  • CPAN 镜像官网:https://mirrors.aliyun.com/CPAN/?spm=a2c6h.13651104.0.0.6954cbf2CcWgpL

通过cpan安装 Graph:Easy模块

因为Graph:Easy是perl模块,所以需要通过cpan命令行工具,从CPAN(CPAN - query, download and build perl modules from CPAN sites) 下载Graph:Easy模块。

具体CPAN modules安装可以参考官网文档: https://mirrors.aliyun.com/CPAN/modules/INSTALL.html

首次可以配置下ALIYUN的镜像

$ cpan       

... 输入yes
Would you like to configure as much as possible automatically? [yes]

... 输入sudo
What approach do you want?  (Choose 'local::lib', 'sudo' or 'manual')
 [local::lib] sudo

... 镜像配置,输入no
Would you like me to automatically choose some CPAN mirror
sites for you? (This means connecting to the Internet) [yes] no

... 输入Aliyun的镜像配置,https://mirrors.aliyun.com/CPAN/
Please enter your CPAN site: [] https://mirrors.aliyun.com/CPAN/
Enter another URL or ENTER to quit: []
New urllist
  https://mirrors.aliyun.com/CPAN/

Autoconfiguration complete.

commit: wrote '/Users/lupingguo/.cpan/CPAN/MyConfig.pm'

// 代理用户名
o conf proxy_user yourname
o conf proxy_pass yourpass

// cpan安装graph easy
sudo cpan Graph:Easy

graph-easy 基本使用

基本语法: graph-easy –help

$ graph-easy --help查看
graph-easy [options] [inputfile [outputfile]]

echo "[ Bonn ] - car -> [ Berlin ]" | graph-easy
graph-easy --input=graph.dot --as_ascii
graph-easy --html --output=mygraph.html graph.txt
graph-easy graph.txt graph.svg
graph-easy graph.txt --as_dot | dot -Tpng -o graph.png
graph-easy graph.txt --png
graph-easy graph.vcg --dot
graph-easy graph.dot --gdl
graph-easy graph.dot --graphml

DOT 语言支持

参考: https://graphviz.org/doc/info/lang.html

简单示例

// The graph name and the semicolons are optional
graph g1 {
    a -- b -- c;
    b -- d;
}

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

示例 1: 简单的东西/南北画图,以及 label 标签

// 简单指向
$ graph-easy <<< '[a]->[b]'
+---+     +---+
| a | --> | b |
+---+     +---+

// 增加标签
$ echo "[Hello] - Coding -> [world]"|graph-easy
+-------+  Coding   +-------+
| Hello | --------> | world |
+-------+           +-------+

// 流程指向,并在箭头上备注文字
$ graph-easy <<< '[a]->[b]->{label:"true";}[c]->[d]->{label:"FeedBack";}[a]'
      FeedBack
  +---------------------------------+
  v                                 |
+---+     +---+  true   +---+     +---+
| a | --> | b | ------> | c | --> | d |
+---+     +---+         +---+     +---+

// 从上往下画
$ graph-easy <<< 'graph{flow:south;}[a]->[b]'
+---+
| a |
+---+
  |
  |
  v
+---+
| b |
+---+

// 线的类型,->与..>
$ graph-easy <<< '[a]..>[b]'

复杂流程,可以通过将 graph-easy 语法写入.dot 后缀文件再生成

// 输入 graph.dot
graph{flow:south;}
[a]->{label:"true";}[b]->[c]
[a]->{label:"false";}[c]
[c]..>{label:"callback";}[a]


$ graph-easy graph.dot
$ graph-easy --input=graph.dot --as_ascii
             +-------+
  .........> |   a   | -+
  :          +-------+  |
  :            |        |
  :            | true   |
  :            v        |
  :          +-------+  |
  : callback |   b   |  | false
  :          +-------+  |
  :            |        |
  :            |        |
  :            v        |
  :          +-------+  |
  .......... |   c   | <+
             +-------+

谈下画图工具

Graphviz(Graph Visualization) - 图像可视化

图可视化是一种将结构信息表示为抽象图和网络图的方法。自动图形绘制在软件工程、数据库和网页设计、网络以及许多其他领域的可视化界面中具有许多重要的应用。

Graphviz 是开源图形可视化软件。它有几个主要的图形布局程序(参考: https://graphviz.org/gallery/ )。有关一些示例布局,请参阅图库。它还具有 Web 和交互式图形界面,以及辅助工具、库和语言绑定

ACSII画图一般通过 DOT语言描述后,借用 graph-easy 渲染,UML 可以借用 PlanUML 和 DotUML 画图,语法有一点点差异。

推荐PlanUML,原因:

  • 无 Logo
  • 支持 VScode、Jetbrain 插件
  • 文档清晰,语法也比较简单

画图软件

  • DOT: graphviz, graph-easy
  • UML:
    • DotUML
    • PlanUML

Dot 语法支持(graphviz)

DOT 是一种图像描述语言,为人类和计算机理解: https://en.wikipedia.org/wiki/DOT_(graph_description_language),唯一的问题是用用户必须依赖自动布局

$ cat main.dot
 digraph G {
   main -> parse -> execute;
   main -> init;
   main -> cleanup;
   execute -> make_string;
   execute -> printf
   init -> make_string;
   main -> printf;
   execute -> compare;
 }

// 通过DOT语言,生成png文件
$ cat main.dot|dot -Tpng -o main.png

// 也可以通过graph-easy渲染成ASCII图片
$ cat main.dot|graph-easy
                +-------------+
                |   printf    | <-------------+
                +-------------+               |
                  ^                           |
                  |                           |
                  |                           |
+---------+     +-------------+     +------+  |
| cleanup | <-- |    main     | --> | init |  |
+---------+     +-------------+     +------+  |
                  |                   |       |
                  |                   |       |
                  v                   |       |
                +-------------+       |       |
                |    parse    |  +----+-------+
                +-------------+  |    |
                  |              |    |
                  |              |    |
                  v              |    |
+---------+     +-------------+  |    |
| compare | <-- |   execute   | -+    |
+---------+     +-------------+       |
                  |                   |
                  |                   |
                  v                   |
                +-------------+       |
                | make_string | <-----+
                +-------------+

参考文档

  1. Graphviz: https://graphviz.org/
  2. Grahp-Easy: https://metacpan.org/dist/Graph-Easy/view/bin/graph-easy
  3. DotUML: https://dotuml.com/documentation.html#secAttribute
  4. PlanUML: https://plantuml.com/zh/
  5. Dot 语言: https://en.wikipedia.org/wiki/DOT_(graph_description_language)