Source for file GraphViz.php
Documentation is available at GraphViz.php
// +---------------------------------------------------------------------------+
// | PEAR :: Image :: GraphViz |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
// +---------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License, |
// | that is available at http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +---------------------------------------------------------------------------+
// $Id: GraphViz.php,v 1.23 2004/12/22 08:06:11 sebastian Exp $
require_once 'System.php';
* Interface to AT&T's GraphViz tools.
* The GraphViz class allows for the creation of and the work with directed
* and undirected graphs and their visualization with AT&T's GraphViz tools.
* require_once 'Image/GraphViz.php';
* $graph = new Image_GraphViz();
* 'URL' => 'http://link1',
* 'label' => 'This is a label',
* 'URL' => 'http://link2',
* 'URL' => 'http://link3',
* 'label' => 'Edge Label'
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @author Karsten Dambekalns <k.dambekalns@fishfarm.de>
* @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
* @package Image_GraphViz
* Path to GraphViz/dot command
* Path to GraphViz/neato command
* Representation of the graph
* Setting the name of the Graph is useful for including multiple image maps on
* one page. If not set, the graph will be named 'G'.
* @param boolean $directed Directed (TRUE) or undirected (FALSE) graph.
* @param array $attributes Attributes of the graph
* @param string $name Name of the Graph
function Image_GraphViz($directed = TRUE , $attributes = array (), $name = NULL ) {
$this->graph['name'] = $name;
* Output image of the graph in a given format.
* @param string Format of the output image.
* This may be one of the formats supported by GraphViz.
function image($format = 'svg') {
if ($data = $this->fetch($format)) {
$sendContentLengthHeader = TRUE;
header('Content-Type: image/' . $format);
header('Content-Type: image/jpeg');
header('Content-Type: application/pdf');
header('Content-Type: image/svg+xml');
$sendContentLengthHeader = FALSE;
if ($sendContentLengthHeader) {
* Return image (data) of the graph in a given format.
* @param string Format of the output image.
* This may be one of the formats supported by GraphViz.
* @return string The image (data) created by GraphViz.
function fetch($format = 'svg') {
$outputfile = $file . '.' . $format;
$command .= " -T$format -o$outputfile $file";
$fp = fopen($outputfile, 'rb');
* Add a cluster to the graph.
* @param array Attributes of the cluster.
function addCluster($id, $title, $attributes = array ()) {
$this->graph['clusters'][$id]['title'] = $title;
$this->graph['clusters'][$id]['attributes'] = $attributes;
* Add a note to the graph.
* @param string Name of the node.
* @param array Attributes of the node.
* @param string Group of the node.
function addNode($name, $attributes = array (), $group = 'default') {
$this->graph['nodes'][$group][$name] = $attributes;
* Remove a node from the graph.
* @param Name of the node to be removed.
if (isset ($this->graph['nodes'][$group][$name])) {
unset ($this->graph['nodes'][$group][$name]);
* Add an edge to the graph.
* Caveat! This cannot handle multiple identical edges. If you use non-numeric
* IDs for the nodes, this will not do (too much) harm. For numeric IDs the
* array_merge() that is used will change the keys when merging arrays, leading
* to new nodes appearing in the graph.
* @param array Start and End node of the edge.
* @param array Attributes of the edge.
function addEdge($edge, $attributes = array ()) {
if (!isset ($this->graph['edges'][$id])) {
$this->graph['edges'][$id] = $edge;
$this->graph['edges'][$id],
if (!isset ($this->graph['edgeAttributes'][$id])) {
$this->graph['edgeAttributes'][$id] = $attributes;
$this->graph['edgeAttributes'][$id],
* Remove an edge from the graph.
* @param array Start and End node of the edge to be removed.
if (isset ($this->graph['edges'][$id])) {
unset ($this->graph['edges'][$id]);
if (isset ($this->graph['edgeAttributes'][$id])) {
unset ($this->graph['edgeAttributes'][$id]);
* Add attributes to the graph.
* @param array Attributes to be added to the graph.
$this->graph['attributes'],
* Set attributes of the graph.
* @param array Attributes to be set for the graph.
$this->graph['attributes'] = $attributes;
* Set directed/undirected flag for the graph.
* @param boolean Directed (TRUE) or undirected (FALSE) graph.
$this->graph['directed'] = $directed;
* @param string File to load graph from.
* @param string File to save the graph to.
* @return mixed File the graph was saved to, FALSE on failure.
function save($file = '') {
$file = System ::mktemp ('graph_');
if ($fp = @fopen($file, 'w')) {
@fputs($fp, $serialized_graph);
* Parse the graph into GraphViz markup.
* @return string GraphViz markup
$parsedGraph = "digraph " . $this->graph['name'] . " {\n";
$parsedGraph = "digraph G {\n";
if (isset ($this->graph['attributes'])) {
foreach ($this->graph['attributes'] as $key => $value) {
$attributeList[] = $key . '="' . $value . '"';
if (!empty ($attributeList)) {
$parsedGraph .= 'graph [ '. implode(',', $attributeList) . " ];\n";
if (isset ($this->graph['nodes'])) {
foreach($this->graph['nodes'] as $group => $nodes) {
if ($group != 'default') {
"subgraph \"cluster_%s\" {\nlabel=\"%s\";\n",
isset ($this->graph['clusters'][$group]) ? $this->graph['clusters'][$group]['title'] : ''
if (isset ($this->graph['clusters'][$group]['attributes'])) {
foreach ($this->graph['clusters'][$group]['attributes'] as $key => $value) {
$attributeList[] = $key . '="' . $value . '"';
if (!empty ($attributeList)) {
$parsedGraph .= implode(',', $attributeList) . ";\n";
foreach($nodes as $node => $attributes) {
foreach($attributes as $key => $value) {
$attributeList[] = $key . '="' . $value . '"';
if (!empty ($attributeList)) {
if ($group != 'default') {
if (isset ($this->graph['edges'])) {
foreach($this->graph['edges'] as $label => $node) {
foreach($this->graph['edgeAttributes'][$label] as $key => $value) {
$attributeList[] = $key . '="' . $value . '"';
if (!empty ($attributeList)) {
return $parsedGraph . "}\n";
* Save GraphViz markup to file.
* @param string File to write the GraphViz markup to.
* @return mixed File to which the GraphViz markup was
* written, FALSE on failure.
$parsedGraph = $this->parse();
if (!empty ($parsedGraph)) {
$file = System ::mktemp ('graph_');
if ($fp = @fopen($file, 'w')) {
@fputs($fp, $parsedGraph);
Documentation generated on Mon, 11 Mar 2019 14:14:17 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|