graphviz php,Graphviz:一个图形库

Graphviz

931113acc1b1895305b993429c531cab.png68747470733a2f2f706f7365722e707567782e6f72672f616c6f6d2f677261706876697a2f762f737461626c6568747470733a2f2f706f7365722e707567782e6f72672f616c6f6d2f677261706876697a2f646f776e6c6f61647368747470733a2f2f706f7365722e707567782e6f72672f616c6f6d2f677261706876697a2f6c6963656e736568747470733a2f2f706f7365722e707567782e6f72672f616c6f6d2f677261706876697a2f642f6d6f6e74686c7968747470733a2f2f706f7365722e707567782e6f72672f616c6f6d2f677261706876697a2f642f6461696c79

Graphviz generation for PHP

d51a79c37c675e94af4766fa7c06a708.png

Installation

Install the latest version with:

composer require alom/graphviz

Usage

This library allow you to create Dot Graph with a PHP fluid interface:

$graph = new Alom\Graphviz\Digraph('G');

$graph

->subgraph('cluster_1')

->attr('node', array('style' => 'filled', 'fillcolor' => 'blue'))

->node('A')

->node('B')

->edge(array('b0', 'b1', 'b2', 'b3'))

->end()

->edge(array('A', 'B', 'C'))

;

echo $graph->render();

Escaping of labels

By default, labels will be escaped, so that your PHP string is represented "as it is" in the graph. If you don't want the label to be escaped, add set the special _escaped attribute to false:

$graph = new Alom\Graphviz\Digraph('G');

$graph

->node('my_table', array(

'label' => '<

>',

'_escaped' => false

))

Browsing the graph

When you have created lot of subgraphs and nodes, it might be useful to be able to browse it using identifiers. For example, if you have the following graph:

$graph = new Alom\Graphviz\Digraph('G');

$graph

->subgraph('cluster_1')

->node('A')

->node('B')

->end()

->subgraph('cluster_2')

->node('C')

->node('D')

->end()

->edge(array('C', 'D'))

;

You can do the following to access the nodes in the existing graph:

$cluster = $graph->get('cluster_1');

$node = $graph->get('cluster_2')->get('D');

When you have a node or an edge, you can manipulate its attributes:

# read a value

echo $node->getAttribute('label', 'no label'); # second argument is default value

# write a value

$node->attribute('label', 'new label');

On a graph, you can access or verify edge existence:

$graph->hasEdge(array('A', 'B'));

$graph->getEdge(array('C', 'D'));

Using cluster and record IDs

If you create an edge from/to an ID inside a record, use an array instead of a string:

$graph = new Alom\Graphviz\Digraph('G');

$graph

->node('A', array('shape' => 'record', 'label' => '{ <1> Part 1 | <2> Part 2}'))

->node('B')

->edge(array('B', array('A', '1')))

;

As you can see in the example above, the edge is composed of two parts:

'B': a regular node

array('A', '1'): targets the cell "1" inside the A node

This method also work for getEdge, hasEdge and every edge-related method.

Samples

Take a look at examples located in samples folder:

00-readme.php: Example from graphviz README

01-basic.php: Basic styling of nodes

02-table.php: An example for HTML table escaping

You can generate any of those graph by using the following commands:

php samples/00-readme.php | dot -Tpdf -oout.pdf

xdg-open out.pdf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值