Skip to content

Commit

Permalink
Omit root graph name unless explicitly assigned
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Oct 9, 2018
1 parent 9895d3a commit 4d14455
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 50 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ 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 Expand Up @@ -101,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
9 changes: 3 additions & 6 deletions src/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,11 @@ public function createScript(Graph $graph)
* 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);
if ($name !== null) {
$name = $this->escapeId($name) . ' ';
}

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

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

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

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

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

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

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

public function testGraphName()
public function testGraphWithNameWithSpaces()
{
$graph = new Graph();
$graph->setAttribute('graphviz.name', 'My Graph Name');
Expand All @@ -49,34 +49,6 @@ public function testGraphName()
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 All @@ -89,7 +61,7 @@ public function testGraphIsolatedVertices()
$graph->createVertex('b');

$expected = <<<VIZ
graph G {
graph {
"a"
"b"
}
Expand All @@ -107,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 @@ -125,7 +97,7 @@ public function testUnknownGraphAttributesWillBeDiscarded()
$graph->setAttribute('graphviz.unknown.color', 'red');

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


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

$expected = <<<VIZ
digraph G {
digraph {
"a" -> "b"
}
Expand All @@ -180,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 @@ -200,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 @@ -221,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 @@ -244,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 @@ -269,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 4d14455

Please sign in to comment.