graphviz

Overview

Graphviz

Build status Latest Stable Version Total Downloads License Monthly Downloads Daily Downloads

Graphviz generation for PHP

Build Status

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' => '<<table>...</table>>',
        '_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:

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
Comments
  • WIP Support for HTML labels

    WIP Support for HTML labels

    There currently is no support for HTML table labels http://graphviz.org/content/node-shapes#html

    I was curious for this support so wrote these small failing tests.

    • [ ] Fix failing test for normal title (trivial to fix)
    • [ ] Fix failing test for record title (is all escaping properly esp. ports: http://graphviz.org/content/node-shapes#record)
    • [ ] Fix failing test for HTML title (this is tricky due to the current escaper)

    Note I come from https://github.com/clue/graph-uml and https://github.com/clue/graph of which the latter has a graphviz rendering. The latter is about to split off graphviz into it's own project so maybe we can team-up ... not sure.

    Feel free to close or fix for the tests :-)

    opened by clemens-tolboom 5
  • Extremely simple work around to allow unescaped values.

    Extremely simple work around to allow unescaped values.

    This was a 5 mins 'hack'. Really tight on schedule, cannot provide tests etc. Merge it or ignore it, your call, just pulling for other people who might be looking for a super quick no-escaping solution

    Extremely simple to use

                $graph->node($state['value'], [
                    'label' => chr(1) . $label,
    
    opened by ptheofan 3
  • GraphViz with html

    GraphViz with html

    How to do that with your lib

            $graph
                    ->set('rankdir', 'LR')
                    ->subgraph('cluster_0')
                    ->set('style', 'filled')
                    ->set('color', 'lightgrey')
                    ->attr('node', array('style' => 'filled', 'color' => 'white'))
                    ->edge(array('a0', 'a1', 'a2', 'a3'))
                    ->set('label', 'process #1')
                    ->end()
                    ->subgraph('cluster_1')
                    ->attr('node', array('style' => 'filled'))
                    ->edge(array('b0', 'b1', 'b2', 'b3'))
                    ->set('color', 'blue')
                    ->end()
                    ->edge(array('start', 'a0'))
                    ->edge(array('start', 'b0'))
                    ->edge(array('a1', 'b3'))
                    ->edge(array('b2', 'a3'))
                    ->edge(array('a3', 'a0'))
                    ->edge(array('a3', 'end'))
                    ->edge(array('b3', 'end'))
                    ->node('AAA', array('label'=> "<<table><tr><td>xfghxhfg</td></tr></table>>"))
            ;
            $dot = $graph->render();
    

    It display html instead take in consideration so it add a " between = and <.

    opened by Esysteme 3
  • Add support for html in Label

    Add support for html in Label

    this is work now :

            $graph
                    ->set('rankdir', 'LR')
                    ->subgraph('cluster_0')
                    ->set('style', 'filled')
                    ->set('color', 'lightgrey')
                    ->attr('node', array('style' => 'filled', 'color' => 'white'))
                    ->edge(array('a0', 'a1', 'a2', 'a3'))
                    ->set('label', 'process #1')
                    ->end()
                    ->subgraph('cluster_1')
                    ->attr('node', array('style' => 'filled'))
                    ->edge(array('b0', 'b1', 'b2', 'b3'))
                    ->set('color', 'blue')
                    ->end()
                    ->edge(array('start', 'a0'))
                    ->edge(array('start', 'b0'))
                    ->edge(array('a1', 'b3'))
                    ->edge(array('b2', 'a3'))
                    ->edge(array('a3', 'a0'))
                    ->edge(array('a3', 'end'))
                    ->edge(array('b3', 'end'))
                    ->node('AAA', array('label'=> "<<table><tr><td>xfghxhfg</td></tr></table>>"))
            ;
            $dot = $graph->render();
    
    opened by Esysteme 2
  • Render should not add colon

    Render should not add colon

    I get warning about the last line from dot due to this semi colon

    syntax error in line 129 near ';'

    The documentation on http://graphviz.org/content/dot-language has no ; in

    graph   :   [ strict ] (graph | digraph) [ ID ] '{' stmt_list '}'
    
    opened by clemens-tolboom 2
  • Infinite loop caused by nested sub-graphs

    Infinite loop caused by nested sub-graphs

    Creating a graph with nesting of more than two levels causes an infinite loop.

    screen shot 2017-04-29 at 2 09 27 pm

    Steps to reproduce

    Create nested graph structure like so.

    $graph = new Digraph('G');
    $graph->node('A')->edge(['A', 'B']);
    
    $sub1 = $graph
        ->subgraph('cluster_1')
        ->node('B')
        ->edge(['B', 'C']);
    
    $sub2 = $sub1->subgraph('cluster_2')
        ->node('C')
        ->edge(['C', 'D']);
    
    $sub3 = $sub2->subgraph('cluster_3')
        ->node('D');
    

    The final call to Subgraph::createEdge() will create an infinite loop as it is unable to resolve the top level graph.

    opened by badams 1
  • Escaping reserved keywords is not case-sensitive

    Escaping reserved keywords is not case-sensitive

    When creating a node which uses a reserved keyword for its name (eg. graph, node, edge)

    The name should be escaped with quotation marks, the current implementation works if your node names are all lower case, however the test does not consider names like Node etc..

    opened by badams 1
  • Tag a stable version

    Tag a stable version

    Hi,

    Could you tag a stable version 1.0.0? It would let Composer users to use your lib without lowering the minimum-stability of their project. You only have to add a tag in your github repository, see http://getcomposer.org/doc/02-libraries.md#tags

    opened by AurelC2G 1
  • Enable escaping for hyphens

    Enable escaping for hyphens

    When an attribute contains an hyphen (" - "), the string needs to be escaped. If not, graphviz will throw a syntax error. This diff enables the existing escaping methods whenever an hyphen is present in the attribute. It also adds a test to make sure this functionality is present.

    opened by AurelC2G 1
  • Some code care.

    Some code care.

    Replaced tabs with spaces throughout the code. Introduced type hinting in function signatures. Marked SubgraphTest.php as incomplete. Fixed PHPDoc comments everywhere.

    opened by petsagouris 1
  • No semicolor to end of dot output.

    No semicolor to end of dot output.

    Fixes #5

    I get warning about the last line from dot due to this semi colon

    syntax error in line 129 near ';'

    The documentation on http://graphviz.org/content/dot-language has no ; in

    graph   :   [ strict ] (graph | digraph) [ ID ] '{' stmt_list '}'
    
    opened by clemens-tolboom 0
  • Add parsing function for dot files

    Add parsing function for dot files

    Nice work!

    But I have a requirement to read the generated .dot, modify the nodes/edges of the .dot file, and then generate a new .dot

    It seems like a new parsing function needs to be added.

    Looking forward to your reply.

    Best wishes, Porlockzzz

    enhancement 
    opened by Porlockzzz 3
Releases(v2.0.0)
Owner
Alexandre Salomé
Software engineer
Alexandre Salomé