Skip to content

Commit

Permalink
Merge pull request #28 from clue-labs/omit-name
Browse files Browse the repository at this point in the history
Omit root graph name unless explicitly assigned via graphviz.name attribute
  • Loading branch information
clue authored Oct 9, 2018
2 parents 1a9b92a + 4d14455 commit 80e5b26
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ $hello->createEdgeTo($world);

See also the [examples](examples/).

Additionally, this library accepts an optional `graphviz.name` attribute that
will be used as the name (or ID) for the root graph object in the DOT output if
given. Unless explicitly assigned, this will be omitted by default. It is common
to assign a `G` here, but usually there should be no need to assign this. Among
others, this may be used as the title or tooltip in SVG output.

```php
$graph = new Fhaculty\Graph\Graph();
$graph->setAttribute('graphviz.name', 'G');

$graph->createVertex('first');
```

### Vertex attributes

GraphViz supports a number of attributes on each vertex instance (GraphViz calls
Expand Down
12 changes: 11 additions & 1 deletion src/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,17 @@ 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 = $this->escapeId($name) . ' ';
}

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

// add global attributes
$globals = array(
Expand Down
50 changes: 39 additions & 11 deletions tests/GraphVizTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,35 @@ public function testGraphEmpty()
$graph = new Graph();

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

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

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

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

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

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

$expected = <<<VIZ
graph "My Graph Name" {
}
VIZ;
Expand All @@ -33,7 +61,7 @@ public function testGraphIsolatedVertices()
$graph->createVertex('b');

$expected = <<<VIZ
graph G {
graph {
"a"
"b"
}
Expand All @@ -51,7 +79,7 @@ public function testGraphDefaultAttributes()
$graph->setAttribute('graphviz.edge.color', 'grey');

$expected = <<<VIZ
graph G {
graph {
graph [bgcolor="transparent"]
node [color="blue"]
edge [color="grey"]
Expand All @@ -69,7 +97,7 @@ public function testUnknownGraphAttributesWillBeDiscarded()
$graph->setAttribute('graphviz.unknown.color', 'red');

$expected = <<<VIZ
graph G {
graph {
}
VIZ;
Expand All @@ -88,7 +116,7 @@ public function testEscaping()


$expected = <<<VIZ
graph G {
graph {
"a"
"b¹²³ is; ok\\\\ay, &quot;right&quot;?"
3
Expand All @@ -107,7 +135,7 @@ public function testGraphDirected()
$graph->createVertex('a')->createEdgeTo($graph->createVertex('b'));

$expected = <<<VIZ
digraph G {
digraph {
"a" -> "b"
}
Expand All @@ -124,7 +152,7 @@ public function testGraphMixed()
$graph->createVertex('c')->createEdge($graph->getVertex('b'));

$expected = <<<VIZ
digraph G {
digraph {
"a" -> "b"
"c" -> "b" [dir="none"]
}
Expand All @@ -144,7 +172,7 @@ public function testGraphUndirectedWithIsolatedVerticesFirst()
$graph->getVertex('b')->createEdge($graph->getVertex('c'));

$expected = <<<VIZ
graph G {
graph {
"d"
"a" -- "b"
"b" -- "c"
Expand All @@ -165,7 +193,7 @@ public function testVertexLabels()
$graph->createVertex('e')->setBalance(2)->setAttribute('graphviz.label', 'unnamed');

$expected = <<<VIZ
graph G {
graph {
"a" [label="a (+1)"]
"b" [label="b (0)"]
"c" [label="c (-1)"]
Expand All @@ -188,7 +216,7 @@ public function testEdgeLayoutAtributes()
$graph->createVertex('5a')->createEdge($graph->createVertex('5b'))->getAttributeBag()->setAttributes(array('graphviz.a' => 'b', 'graphviz.c' => 'd'));

$expected = <<<VIZ
graph G {
graph {
"1a" -- "1b"
"2a" -- "2b" [numeric=20]
"3a" -- "3b" [textual="forty"]
Expand All @@ -213,7 +241,7 @@ public function testEdgeLabels()
$graph->createVertex('7a')->createEdge($graph->createVertex('7b'))->setFlow(70)->setAttribute('graphviz.label', 'prefixed');

$expected = <<<VIZ
graph G {
graph {
"1a" -- "1b"
"2a" -- "2b" [label=20]
"3a" -- "3b" [label="0/30"]
Expand Down

0 comments on commit 80e5b26

Please sign in to comment.