一种代理访问Google GraphViz Charts的方法

Google GraphViz Charts API在国内无法正常访问。本文实现了一种代理访问Google GraphViz Charts的方法。

参数列表

  • cht=gv[:<opt_engine>]  ,  可选参数  , 如果没有,这默认为gv:dot。其他可用引擎有 neato twopi circo fdp
  • chl=<DOT_string>,  必选参数。注意不支持 // 风格注释,urlencode换行符后可以支持//风格注释。不支持中文

Google官网的一些说明

  • The graph attribute size should be not be used; use the Chart API parameter chs instead.
  • The maximum number of nodes is 200, and the maximum for edges is 400.
  • Anti-aliasing, transparency, and alternate fonts are not supported.
  • The node attributes image and shapefile are not supported, and will result in an error if present.
  • The graph attributes ratio, margin, and pad are not supported, and will be ignored if present.

第三条明确说了不支持alternate fonts。

一些演示

搜集一些来自网络的dot源码用来测试。

效果如图:

Graphviz演示
Graphviz演示

更多的展示

效果图

Graphviz演示
Graphviz演示

再来个状态机

Graphviz状态机
Graphviz状态机

一个哈希表

Graphviz哈希表
Graphviz哈希表

子图

注意子图命名必须以cluster为前缀,见官方说明

效果图

Graphviz子图
Graphviz子图

实现方式

实现方式很简单,需要有一个国外的虚拟主机或者VPS,用做代理来访问Google Chart API,将结果返回即可。代码如下

代码笔记

一开始不懂设置Response Header,返回的直接是图片的乱码,放在img src里可以显示,但是新窗口打开图片就全是乱码了。设置Response Header的方法:

参考资料

4 thoughts on “一种代理访问Google GraphViz Charts的方法

  1. Pingback: 自建GraphViz API | 知行近思

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注