diff --git a/README.md b/README.md index cb356e1..66e8d22 100644 --- a/README.md +++ b/README.md @@ -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'); @@ -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 diff --git a/src/GraphViz.php b/src/GraphViz.php index e86d8fa..1758d0f 100644 --- a/src/GraphViz.php +++ b/src/GraphViz.php @@ -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( diff --git a/tests/GraphVizTest.php b/tests/GraphVizTest.php index 7dbe91f..1424f96 100644 --- a/tests/GraphVizTest.php +++ b/tests/GraphVizTest.php @@ -18,7 +18,7 @@ public function testGraphEmpty() $graph = new Graph(); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } - public function testGraphExplicitDefaultName() + public function testGraphWithName() { $graph = new Graph(); $graph->setAttribute('graphviz.name', 'G'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } - public function testGraphName() + public function testGraphWithNameWithSpaces() { $graph = new Graph(); $graph->setAttribute('graphviz.name', 'My Graph Name'); @@ -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 = <<assertEquals($expected, $this->graphViz->createScript($graph)); - } - - public function testGraphNonBlankName() - { - $graph = new Graph(); - $graph->setAttribute('graphviz.name', ' '); - - $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); @@ -89,7 +61,7 @@ public function testGraphIsolatedVertices() $graph->createVertex('b'); $expected = <<setAttribute('graphviz.edge.color', 'grey'); $expected = <<setAttribute('graphviz.unknown.color', 'red'); $expected = <<createVertex('a')->createEdgeTo($graph->createVertex('b')); $expected = << "b" } @@ -180,7 +152,7 @@ public function testGraphMixed() $graph->createVertex('c')->createEdge($graph->getVertex('b')); $expected = << "b" "c" -> "b" [dir="none"] } @@ -200,7 +172,7 @@ public function testGraphUndirectedWithIsolatedVerticesFirst() $graph->getVertex('b')->createEdge($graph->getVertex('c')); $expected = <<createVertex('e')->setBalance(2)->setAttribute('graphviz.label', 'unnamed'); $expected = <<createVertex('5a')->createEdge($graph->createVertex('5b'))->getAttributeBag()->setAttributes(array('graphviz.a' => 'b', 'graphviz.c' => 'd')); $expected = <<createVertex('7a')->createEdge($graph->createVertex('7b'))->setFlow(70)->setAttribute('graphviz.label', 'prefixed'); $expected = <<