From db4ddc69fbdc5d8cfe5b1b6b5681894f801f1285 Mon Sep 17 00:00:00 2001 From: Clemens Tolboom Date: Wed, 1 Jan 2014 16:55:10 +0100 Subject: [PATCH 1/5] Initial proposal. --- lib/Fhaculty/Graph/GraphViz.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Fhaculty/Graph/GraphViz.php b/lib/Fhaculty/Graph/GraphViz.php index a7a1b7d3..7d3d5f19 100644 --- a/lib/Fhaculty/Graph/GraphViz.php +++ b/lib/Fhaculty/Graph/GraphViz.php @@ -228,10 +228,10 @@ public function createImageSrc() public function createImageHtml() { if ($this->format === 'svg' || $this->format === 'svgz') { - return ''; + return ''; } - return ''; + return ''; } /** From 85bd17b1d2f56bea63c3a34ed3697d7e25e2264e Mon Sep 17 00:00:00 2001 From: Clemens Tolboom Date: Wed, 1 Jan 2014 17:07:26 +0100 Subject: [PATCH 2/5] Slightly better using max-width. --- lib/Fhaculty/Graph/GraphViz.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Fhaculty/Graph/GraphViz.php b/lib/Fhaculty/Graph/GraphViz.php index 7d3d5f19..db7a94ef 100644 --- a/lib/Fhaculty/Graph/GraphViz.php +++ b/lib/Fhaculty/Graph/GraphViz.php @@ -228,10 +228,10 @@ public function createImageSrc() public function createImageHtml() { if ($this->format === 'svg' || $this->format === 'svgz') { - return ''; + return ''; } - return ''; + return ''; } /** From 800fdfc3f886d41feb5ad927f38d8796abf507a1 Mon Sep 17 00:00:00 2001 From: Clemens Tolboom Date: Thu, 2 Jan 2014 09:31:27 +0100 Subject: [PATCH 3/5] Run dot to generate graph and test for their output. --- .travis.yml | 3 +++ lib/Fhaculty/Graph/GraphViz.php | 28 +++++++++++++++++++++----- tests/Fhaculty/Graph/GraphVizTest.php | 29 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2af5cf5f..339810d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,9 @@ language: php php: - 5.4 - 5.3 +before_install: + - sudo apt-get update -qq + - sudo apt-get install -qq graphviz before_script: - composer install --dev --prefer-source --no-interaction script: diff --git a/lib/Fhaculty/Graph/GraphViz.php b/lib/Fhaculty/Graph/GraphViz.php index db7a94ef..3efe0887 100644 --- a/lib/Fhaculty/Graph/GraphViz.php +++ b/lib/Fhaculty/Graph/GraphViz.php @@ -219,6 +219,27 @@ public function createImageSrc() return 'data:image/' . $format . ';base64,' . base64_encode($this->createImageData()); } + /** + * Create a DomElement for clients to enrich. + * + * $domElement = $this->createImageObject(); + * $dom->setAttribute('width', '100%'); + * return $domElement->ownerDocument->saveHtml($domElement); + * @return type + */ + public function createImageObject() { + $dom = new \DOMDocument('1.0', 'utf-8'); + if ($this->format === 'svg' || $this->format === 'svgz') { + $domElement = $dom->createElement('object'); + $domElement->setAttribute('type', 'image/svg+xml'); + $domElement->setAttribute('type', $this->createImageSrc()); + } + else { + $domElement = $dom->createElement('img'); + $domElement->setAttribute('src', $this->createImageSrc()); + } + return $domElement; + } /** * create image html code for this graph * @@ -227,11 +248,8 @@ public function createImageSrc() */ public function createImageHtml() { - if ($this->format === 'svg' || $this->format === 'svgz') { - return ''; - } - - return ''; + $domElement = $this->createImageObject(); + return $domElement->ownerDocument->saveHtml($domElement); } /** diff --git a/tests/Fhaculty/Graph/GraphVizTest.php b/tests/Fhaculty/Graph/GraphVizTest.php index 181f6241..e77ce507 100644 --- a/tests/Fhaculty/Graph/GraphVizTest.php +++ b/tests/Fhaculty/Graph/GraphVizTest.php @@ -197,4 +197,33 @@ private function getDotScriptForGraph(Graph $graph) $graphviz = new GraphViz($graph); return $graphviz->createScript(); } + + public function testImageResult() + { + $graph = new Graph(); + $graph->createVertex('a')->createEdgeTo($graph->createVertex('b')); + $graph->createVertex('c')->createEdge($graph->getVertex('b')); + + $graphviz = new GraphViz($graph); + $result = $graphviz->createImageHtml(); + $this->assertStringStartsWith('createImageObject(); + $domElement->setAttribute('width', '100%'); + $result = $domElement->ownerDocument->saveHtml($domElement); + $this->assertContains('width', $result, "Image HTML has width attribute"); + + $graphviz->setFormat('svg'); + $result = $graphviz->createImageHtml(); + $this->assertStringStartsWith('setFormat('svg'); + $domElement = $graphviz->createImageObject(); + $domElement->setAttribute('width', '100%'); + $result = $domElement->ownerDocument->saveHtml($domElement); + $this->assertContains('width', $result, "Image HTML has width attribute"); + + } + } From 51fcaf5debc7c8e13818fd491fe7ca148d098d4e Mon Sep 17 00:00:00 2001 From: Clemens Tolboom Date: Thu, 2 Jan 2014 09:42:50 +0100 Subject: [PATCH 4/5] Fixed svg generation had missing data attribute. --- lib/Fhaculty/Graph/GraphViz.php | 2 +- tests/Fhaculty/Graph/GraphVizTest.php | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Fhaculty/Graph/GraphViz.php b/lib/Fhaculty/Graph/GraphViz.php index 3efe0887..5b25b47f 100644 --- a/lib/Fhaculty/Graph/GraphViz.php +++ b/lib/Fhaculty/Graph/GraphViz.php @@ -232,7 +232,7 @@ public function createImageObject() { if ($this->format === 'svg' || $this->format === 'svgz') { $domElement = $dom->createElement('object'); $domElement->setAttribute('type', 'image/svg+xml'); - $domElement->setAttribute('type', $this->createImageSrc()); + $domElement->setAttribute('data', $this->createImageSrc()); } else { $domElement = $dom->createElement('img'); diff --git a/tests/Fhaculty/Graph/GraphVizTest.php b/tests/Fhaculty/Graph/GraphVizTest.php index e77ce507..d02f6381 100644 --- a/tests/Fhaculty/Graph/GraphVizTest.php +++ b/tests/Fhaculty/Graph/GraphVizTest.php @@ -205,21 +205,29 @@ public function testImageResult() $graph->createVertex('c')->createEdge($graph->getVertex('b')); $graphviz = new GraphViz($graph); + $graphviz->setFormat('png'); $result = $graphviz->createImageHtml(); $this->assertStringStartsWith('createImageObject(); + $this->assertNotEmpty($domElement->getAttribute('src'), "src element exists"); + $this->assertEmpty($domElement->getAttribute('AAA'), "AAA element does not exists"); + $domElement->setAttribute('width', '100%'); $result = $domElement->ownerDocument->saveHtml($domElement); $this->assertContains('width', $result, "Image HTML has width attribute"); $graphviz->setFormat('svg'); $result = $graphviz->createImageHtml(); - $this->assertStringStartsWith('assertStringStartsWith('setFormat('svg'); $domElement = $graphviz->createImageObject(); + $this->assertNotEmpty($domElement->getAttribute('type'), "type element exists"); + $this->assertNotEmpty($domElement->getAttribute('data'), "data element exists"); + $this->assertEmpty($domElement->getAttribute('AAA'), "AAA element does not exists"); + $domElement->setAttribute('width', '100%'); $result = $domElement->ownerDocument->saveHtml($domElement); $this->assertContains('width', $result, "Image HTML has width attribute"); From 271268064d39eb0b5dc8aac9c7f9d415b6b4091d Mon Sep 17 00:00:00 2001 From: Clemens Tolboom Date: Thu, 2 Jan 2014 10:02:21 +0100 Subject: [PATCH 5/5] Fixed function documentation. --- lib/Fhaculty/Graph/GraphViz.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Fhaculty/Graph/GraphViz.php b/lib/Fhaculty/Graph/GraphViz.php index 5b25b47f..1b3681e2 100644 --- a/lib/Fhaculty/Graph/GraphViz.php +++ b/lib/Fhaculty/Graph/GraphViz.php @@ -222,10 +222,13 @@ public function createImageSrc() /** * Create a DomElement for clients to enrich. * - * $domElement = $this->createImageObject(); - * $dom->setAttribute('width', '100%'); + * @code + * $domElement = $graphviz->createImageObject(); + * $domElement->setAttribute('width', '100%'); * return $domElement->ownerDocument->saveHtml($domElement); - * @return type + * @endcode + * + * @return \DomElement */ public function createImageObject() { $dom = new \DOMDocument('1.0', 'utf-8');