Skip to content

Commit

Permalink
Allow setting of graph name via graphviz.name attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
rhelms authored and clue committed Oct 9, 2018
1 parent 1a9b92a commit 9895d3a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Once [installed](#install), let's build and display a sample graph:

````php
$graph = new Fhaculty\Graph\Graph();
// Set graph name to blank to hide default G tooltip for SVG output
$graph->setAttribute('graphviz.name', '');

$blue = $graph->createVertex('blue');
$blue->setAttribute('graphviz.color', 'blue');
Expand Down
15 changes: 14 additions & 1 deletion src/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,20 @@ public function createScript(Graph $graph)
$alg = new Directed($graph);
$directed = $alg->hasDirected();

$script = ($directed ? 'di':'') . 'graph G {' . self::EOL;
/*
* The website [http://www.graphviz.org/content/dot-language] uses the term `ID` when displaying
* the abstract grammar for the DOT language.
* But the man pages for dot use the term `name` when describing the graph file language.
*/
$name = $graph->getAttribute('graphviz.name');
if ($name === null || $name === 'G') {
// don't escape a name of G to maintain default behavior
$name = 'G';
} else {
$name = $this->escapeId($name);
}

$script = ($directed ? 'di':'') . 'graph ' . $name . ' {' . self::EOL;

// add global attributes
$globals = array(
Expand Down
56 changes: 56 additions & 0 deletions tests/GraphVizTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,62 @@ public function testGraphEmpty()
graph G {
}
VIZ;

$this->assertEquals($expected, $this->graphViz->createScript($graph));
}

public function testGraphExplicitDefaultName()
{
$graph = new Graph();
$graph->setAttribute('graphviz.name', 'G');

$expected = <<<VIZ
graph G {
}
VIZ;

$this->assertEquals($expected, $this->graphViz->createScript($graph));
}

public function testGraphName()
{
$graph = new Graph();
$graph->setAttribute('graphviz.name', 'My Graph Name');

$expected = <<<VIZ
graph "My Graph Name" {
}
VIZ;

$this->assertEquals($expected, $this->graphViz->createScript($graph));
}

public function testGraphBlankName()
{
$graph = new Graph();
$graph->setAttribute('graphviz.name', '');

$expected = <<<VIZ
graph "" {
}
VIZ;

$this->assertEquals($expected, $this->graphViz->createScript($graph));
}

public function testGraphNonBlankName()
{
$graph = new Graph();
$graph->setAttribute('graphviz.name', ' ');

$expected = <<<VIZ
graph " " {
}
VIZ;

$this->assertEquals($expected, $this->graphViz->createScript($graph));
Expand Down

0 comments on commit 9895d3a

Please sign in to comment.