diff --git a/.travis.yml b/.travis.yml index 30be35b..14eeac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,11 @@ matrix: sudo: false +addons: + apt: + packages: + - graphviz + install: - composer install --no-interaction diff --git a/README.md b/README.md index 91f25dc..47499ac 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,10 @@ GraPHP attributes as documented below. For the full list of all GraphViz attributes, please refer to the [GraphViz documentation](https://graphviz.gitlab.io/_pages/doc/info/attrs.html). -Note that all [attributes](#attributes) will be quoted and escaped by default, -so a `>` will appear as-is and will not be interpreted as HTML. See also -[HTML-like labels](#html-like-labels) below for more details. +Note that all attributes use UTF-8 encoding (Unicode) and will be quoted and +escaped by default, so a `ö` and `>` will appear as-is and will not be +interpreted as HTML. See also [HTML-like labels](#html-like-labels) below for +more details. ### Graph attributes @@ -82,6 +83,22 @@ $graph->setAttribute('graphviz.graph.bgcolor', 'transparent'); is done for consistency reasons with respect to default vertex and edge attributes as documented below. +For example, the `rankdir` attribute can be used to change the orientation to +horizontal mode (left to right) like this: + +```php +$graph = new Fhaculty\Graph\Graph(); +$graph->setAttribute('graphviz.graph.rankdir', 'LR'); + +$hello = $graph->createVertex('hello'); +$world = $graph->createVertex('wörld'); +$hello->createEdgeTo($world); +``` + +![html graph example](examples/02-html.png) + +See also the [examples](examples/). + ### Vertex attributes GraphViz supports a number of attributes on each vertex instance (GraphViz calls diff --git a/examples/02-html.php b/examples/02-html.php new file mode 100644 index 0000000..0207520 --- /dev/null +++ b/examples/02-html.php @@ -0,0 +1,25 @@ +setAttribute('graphviz.graph.rankdir', 'LR'); + +$hello = $graph->createVertex('hello'); +$world = $graph->createVertex('wörld'); +$hello->createEdgeTo($world); + +$graphviz = new Graphp\GraphViz\GraphViz(); +$graphviz->setFormat('svg'); + +echo ' + + +hello wörld + +' . $graphviz->createImageHtml($graph) . ' + + +'; diff --git a/examples/02-html.png b/examples/02-html.png new file mode 100644 index 0000000..847cb2a Binary files /dev/null and b/examples/02-html.png differ diff --git a/src/GraphViz.php b/src/GraphViz.php index 152d473..a31eb22 100644 --- a/src/GraphViz.php +++ b/src/GraphViz.php @@ -6,7 +6,6 @@ use Graphp\Algorithms\Groups; use Graphp\Algorithms\Degree; use Fhaculty\Graph\Exception\UnexpectedValueException; -use Fhaculty\Graph\Exception\InvalidArgumentException; use Fhaculty\Graph\Edge\Base as Edge; use \stdClass; use Fhaculty\Graph\Attribute\AttributeBagNamespaced; @@ -155,7 +154,10 @@ public function createImageData(Graph $graph) */ public function createImageSrc(Graph $graph) { - $format = ($this->format === 'svg' || $this->format === 'svgz') ? 'svg+xml' : $this->format; + $format = $this->format; + if ($this->format === 'svg' || $this->format === 'svgz') { + $format = 'svg+xml;charset=' . $graph->getAttribute('graphviz.graph.charset', 'UTF-8'); + } return 'data:image/' . $format . ';base64,' . base64_encode($this->createImageData($graph)); } diff --git a/tests/GraphVizTest.php b/tests/GraphVizTest.php index a73300a..bf76a41 100644 --- a/tests/GraphVizTest.php +++ b/tests/GraphVizTest.php @@ -227,4 +227,34 @@ public function testEdgeLabels() $this->assertEquals($expected, $this->graphViz->createScript($graph)); } + + public function testCreateImageSrcWillExportPngDefaultFormat() + { + $graph = new Graph(); + + $src = $this->graphViz->createImageSrc($graph); + + $this->assertStringStartsWith('data:image/png;base64,', $src); + } + + public function testCreateImageSrcAsSvgWithUtf8DefaultCharset() + { + $graph = new Graph(); + + $this->graphViz->setFormat('svg'); + $src = $this->graphViz->createImageSrc($graph); + + $this->assertStringStartsWith('data:image/svg+xml;charset=UTF-8;base64,', $src); + } + + public function testCreateImageSrcAsSvgzWithExplicitIsoCharsetLatin1() + { + $graph = new Graph(); + $graph->setAttribute('graphviz.graph.charset', 'iso-8859-1'); + + $this->graphViz->setFormat('svgz'); + $src = $this->graphViz->createImageSrc($graph); + + $this->assertStringStartsWith('data:image/svg+xml;charset=iso-8859-1;base64,', $src); + } }