Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow clients to add more attributes to html output #96

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 26 additions & 5 deletions lib/Fhaculty/Graph/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,30 @@ public function createImageSrc()
return 'data:image/' . $format . ';base64,' . base64_encode($this->createImageData());
}

/**
* Create a DomElement for clients to enrich.
*
* @code
* $domElement = $graphviz->createImageObject();
* $domElement->setAttribute('width', '100%');
* return $domElement->ownerDocument->saveHtml($domElement);
* @endcode
*
* @return \DomElement
*/
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('data', $this->createImageSrc());
}
else {
$domElement = $dom->createElement('img');
$domElement->setAttribute('src', $this->createImageSrc());
}
return $domElement;
}
/**
* create image html code for this graph
*
Expand All @@ -227,11 +251,8 @@ public function createImageSrc()
*/
public function createImageHtml()
{
if ($this->format === 'svg' || $this->format === 'svgz') {
return '<object type="image/svg+xml" data="' . $this->createImageSrc() . '"></object>';
}

return '<img src="' . $this->createImageSrc() . '" />';
$domElement = $this->createImageObject();
return $domElement->ownerDocument->saveHtml($domElement);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/Fhaculty/Graph/GraphVizTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,41 @@ 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);
$graphviz->setFormat('png');
$result = $graphviz->createImageHtml();
$this->assertStringStartsWith('<img src="data:image/png', $result, "Image HTML has img tag");

$graphviz = new GraphViz($graph);
$domElement = $graphviz->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('<object type="image/svg', $result, "Image HTML has object tag");

$graphviz->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");

}

}