diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2007-03-08 12:01:24 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2007-03-08 12:01:24 +0000 |
commit | b08ae56b53938333b051b91b0a1f912428864950 (patch) | |
tree | 68f089c05feab87cbaf0c3cfcd36407b3cce755f /src/tools.php | |
parent | eff7a30681180181dc22b9500e6db45a7a916183 (diff) | |
download | zetacomponents-graph-b08ae56b53938333b051b91b0a1f912428864950.zip zetacomponents-graph-b08ae56b53938333b051b91b0a1f912428864950.tar.gz |
- Added feature #9511: Added helper functions to create image maps an link SVG
images in ezcGraphTools
Diffstat (limited to 'src/tools.php')
-rw-r--r-- | src/tools.php | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/tools.php b/src/tools.php new file mode 100644 index 0000000..690a47f --- /dev/null +++ b/src/tools.php @@ -0,0 +1,176 @@ +<?php +/** + * File containing the abstract ezcGraph class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Toolkit for several operation with graphs + * + * @package Graph + * @mainclass + */ +class ezcGraphTools +{ + /** + * Create an XHtml image map from a chart with gd driver. The name of the + * image map can be specified and will be ezcGraphImageMap otherwise. + * + * @param ezcGraphChart $chart + * @param string $name + * @return string + */ + public static function createImageMap( ezcGraphChart $chart, $name = null ) + { + if ( $name === null ) + { + $name = 'ezcGraphImageMap'; + } + + if ( ! ( $chart->driver instanceof ezcGraphGdDriver ) ) + { + throw new ezcGraphToolsIncompatibleDriverException( $chart->driver, 'ezcGraphGdDriver' ); + } + + $elements = $chart->renderer->getElementReferences(); + + if ( !count( $elements ) ) + { + throw new ezcGraphToolsNotRenderedException( $chart ); + } + + $imageMap = sprintf( "<map name=\"%s\">\n", $name ); + + // Iterate over legends elements + foreach ( $elements['legend'] as $objectName => $polygones ) + { + $url = $elements['legend_url'][$objectName]; + + if ( empty( $url ) ) + { + continue; + } + + foreach ( $polygones as $shape => $polygone ) + { + $coordinateString = ''; + foreach ( $polygone as $coordinate ) + { + $coordinateString .= sprintf( '%d,%d,', $coordinate->x, $coordinate->y ); + } + + $imageMap .= sprintf( "\t<area shape=\"poly\" coords=\"%s\" href=\"%s\" alt=\"%s\" />\n", + substr( $coordinateString, 0, -1 ), + $url, + $objectName + ); + } + } + + // Iterate over data + foreach ( $elements['data'] as $dataset => $datapoints ) + { + foreach ( $datapoints as $datapoint => $polygones ) + { + $url = $chart->data[$dataset]->url[$datapoint]; + + if ( empty( $url ) ) + { + continue; + } + + foreach ( $polygones as $polygon ) + { + $coordinateString = ''; + foreach ( $polygon as $coordinate ) + { + $coordinateString .= sprintf( '%d,%d,', $coordinate->x, $coordinate->y ); + } + + $imageMap .= sprintf( "\t<area shape=\"poly\" coords=\"%s\" href=\"%s\" alt=\"%s\" />\n", + substr( $coordinateString, 0, -1 ), + $url, + $datapoint + ); + } + } + } + + return $imageMap . "</map>\n"; + } + + /** + * Add links to clickable SVG elements in a chart with SVG driver. + * + * @param ezcGraphChart $chart + * @return void + */ + public static function linkSvgElements( ezcGraphChart $chart ) + { + if ( ! ( $chart->driver instanceof ezcGraphSvgDriver ) ) + { + throw new ezcGraphToolsIncompatibleDriverException( $chart->driver, 'ezcGraphSvgDriver' ); + } + + $fileName = $chart->getRenderedFile(); + + if ( !$fileName ) + { + throw new ezcGraphToolsNotRenderedException( $chart ); + } + + $dom = new DOMDocument(); + $dom->load( $fileName ); + $xpath = new DomXPath( $dom ); + + $elements = $chart->renderer->getElementReferences(); + + // Link chart elements + foreach ( $elements['data'] as $dataset => $datapoints ) + { + foreach( $datapoints as $datapoint => $ids ) + { + $url = $chart->data[$dataset]->url[$datapoint]; + + if ( empty( $url ) ) + { + continue; + } + + foreach ( $ids as $id ) + { + $element = $xpath->query( '//*[@id = \'' . $id . '\']' )->item( 0 ); + + $element->setAttribute( 'style', $element->getAttribute( 'style' ) . ' cursor: pointer;' ); + $element->setAttribute( 'onclick', "top.location = '{$url}'" ); + } + } + } + + // Link legend elements + foreach ( $elements['legend'] as $objectName => $ids ) + { + $url = $elements['legend_url'][$objectName]; + + if ( empty( $url ) ) + { + continue; + } + + foreach ( $ids as $id ) + { + $element = $xpath->query( '//*[@id = \'' . $id . '\']' )->item( 0 ); + + $element->setAttribute( 'style', $element->getAttribute( 'style' ) . ' cursor: pointer;' ); + $element->setAttribute( 'onclick', "top.location = '{$url}'" ); + } + } + + $dom->save( $fileName ); + } +} + +?> |