2013-07-05

php graphviz

But First ...
There are always lots of things on the to-do list.

Like learn a new language ... maybe even a spoken one, not just a slightly different way to talk to the machine, in pretty much the same old (or new(ish)) way.

Or try out something new. But first ... there's always a catch.

Its time to paint the garage floor. But first, the car has to be moved. Oh, the couch is in the way as well, will have to take care of that first. But the vinyl collection has to go somewhere, the couch moves much more easily (well, that's relative, can "easy" be mentioned in the same sentence with "moving" ... ) without two hundred pounds of not yet recycled plastic ... and the floor has to be really clean so the epoxy paint can stick, for a period of at least a fortnight ...

The But First dependency diagram, or graph, was drawn with just a few lines of php code. Really. And a couple of php packages too, but first ...

 
<?php

require_once 'Image/GraphViz.php';
$gv = new Image_GraphViz();
$gv->addEdge(array('Paint garage floor' => 'but first ...'));
$gv->addEdge(array('move car' => 'sweep floor'));
$gv->addEdge(array('store LPs' => 'move couch'));
$gv->addEdge(array('move couch' => 'sweep floor'));
$gv->addEdge(array('sweep floor' => 'mop floor'));
$gv->addEdge(array('mop floor' =>'paint floor'));
$gv->image();

?>


Got the first ink-ling for that diagram in an O'Reilly book, one that has been sitting on the shelf for ... a really long time, Mastering Algorithms with Perl. Its probably out in at least a second, if not third, edition. See Chapter 8, Graphs. Maybe its time to go see if those later editions do exist, or not, but first ...

The graphviz package has lots of other stuff. With an addNode(... call attributes can be set, i.e. 'URL' => '...' turns the node into a clickable link. Different shapes to represent the node, the font, font size, fill color, quite a few more items are in there. See graphviz shape docs for the details.

With php, just the graphviz-php package is not quite enough, there is a php-pear-Image-GraphViz "Interface to AT&Ts GraphViz Tools" package which includes the Image/GraphViz.php file. And not to mention setting up httpd for php, but will leave that one for another day. 





No comments:

Post a Comment