diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/driver/svg.php | 107 | ||||
-rw-r--r-- | src/exceptions/invalid_id.php | 26 | ||||
-rw-r--r-- | src/graph_autoload.php | 1 | ||||
-rw-r--r-- | src/options/chart.php | 2 | ||||
-rw-r--r-- | src/options/svg_driver.php | 61 |
5 files changed, 155 insertions, 42 deletions
diff --git a/src/driver/svg.php b/src/driver/svg.php index 768f091..53bb73c 100644 --- a/src/driver/svg.php +++ b/src/driver/svg.php @@ -57,24 +57,49 @@ class ezcGraphSvgDriver extends ezcGraphDriver { if ( $this->dom === null ) { - $this->dom = new DOMDocument(); - $svg = $this->dom->createElementNS( 'http://www.w3.org/2000/svg', 'svg' ); - $this->dom->appendChild( $svg ); - - $svg->setAttribute( 'width', $this->options->width ); - $svg->setAttribute( 'height', $this->options->height ); - $svg->setAttribute( 'version', '1.0' ); - $svg->setAttribute( 'id', 'ezcGraph' ); - - $this->defs = $this->dom->createElement( 'defs' ); - $this->defs = $svg->appendChild( $this->defs ); - - $this->elements = $this->dom->createElement( 'g' ); - $this->elements->setAttribute( 'id', 'chart' ); - $this->elements->setAttribute( 'color-rendering', $this->options->colorRendering ); - $this->elements->setAttribute( 'shape-rendering', $this->options->shapeRendering ); - $this->elements->setAttribute( 'text-rendering', $this->options->textRendering ); - $this->elements = $svg->appendChild( $this->elements ); + if ( $this->options->templateDocument !== false ) + { + $this->dom = new DOMDocument(); + $this->dom->load( $this->options->templateDocument ); + + $this->defs = $this->dom->getElementsByTagName( 'defs' )->item( 0 ); + $svg = $this->dom->getElementsByTagName( 'svg' )->item( 0 ); + } + else + { + $this->dom = new DOMDocument(); + $svg = $this->dom->createElementNS( 'http://www.w3.org/2000/svg', 'svg' ); + $this->dom->appendChild( $svg ); + + $svg->setAttribute( 'width', $this->options->width ); + $svg->setAttribute( 'height', $this->options->height ); + $svg->setAttribute( 'version', '1.0' ); + $svg->setAttribute( 'id', 'ezcGraph' ); + + $this->defs = $this->dom->createElement( 'defs' ); + $this->defs = $svg->appendChild( $this->defs ); + } + + if ( $this->options->insertIntoGroup !== false ) + { + // getElementById only works for Documents validated against a certain + // schema, so that the use of XPath should be faster in most cases. + $xpath = new DomXPath( $this->dom ); + $this->elements = $xpath->query( '//*[@id = \'' . $this->options->insertIntoGroup . '\']' )->item( 0 ); + if ( !$this->elements ) + { + throw new ezcGraphSvgDriverInvalidIdException( $this->options->insertIntoGroup ); + } + } + else + { + $this->elements = $this->dom->createElement( 'g' ); + $this->elements->setAttribute( 'id', 'chart' ); + $this->elements->setAttribute( 'color-rendering', $this->options->colorRendering ); + $this->elements->setAttribute( 'shape-rendering', $this->options->shapeRendering ); + $this->elements->setAttribute( 'text-rendering', $this->options->textRendering ); + $this->elements = $svg->appendChild( $this->elements ); + } } } @@ -92,15 +117,15 @@ class ezcGraphSvgDriver extends ezcGraphDriver $lastPoint = end( $points ); $pointString = sprintf( ' M %.4f,%.4f', - $lastPoint->x, - $lastPoint->y + $lastPoint->x + $this->options->graphOffset->x, + $lastPoint->y + $this->options->graphOffset->y ); foreach ( $points as $point ) { $pointString .= sprintf( ' L %.4f,%.4f', - $point->x, - $point->y + $point->x + $this->options->graphOffset->x, + $point->y + $this->options->graphOffset->y ); } $pointString .= ' z '; @@ -151,10 +176,10 @@ class ezcGraphSvgDriver extends ezcGraphDriver $this->createDocument(); $pointString = sprintf( ' M %.4f,%.4f L %.4f,%.4f', - $start->x, - $start->y, - $end->x, - $end->y + $start->x + $this->options->graphOffset->x, + $start->y + $this->options->graphOffset->y, + $end->x + $this->options->graphOffset->x, + $end->y + $this->options->graphOffset->y ); $path = $this->dom->createElement( 'path' ); @@ -320,9 +345,9 @@ class ezcGraphSvgDriver extends ezcGraphDriver } $textNode = $this->dom->createElement( 'text', $string ); - $textNode->setAttribute( 'x', $position->x ); + $textNode->setAttribute( 'x', $position->x + $this->options->graphOffset->x ); $textNode->setAttribute( 'text-length', ( $size * strlen( $string ) * $this->options->assumedCharacterWidth ) . 'px' ); - $textNode->setAttribute( 'y', $position->y ); + $textNode->setAttribute( 'y', $position->y + $this->options->graphOffset->y ); $textNode->setAttribute( 'style', sprintf( @@ -369,15 +394,15 @@ class ezcGraphSvgDriver extends ezcGraphDriver $width /= 2; $height /= 2; - $Xstart = $center->x + $width * cos( ( -$startAngle / 180 ) * M_PI ); - $Ystart = $center->y + $height * sin( ( $startAngle / 180 ) * M_PI ); - $Xend = $center->x + $width * cos( ( -( $endAngle ) / 180 ) * M_PI ); - $Yend = $center->y + $height * sin( ( ( $endAngle ) / 180 ) * M_PI ); + $Xstart = $center->x + $this->options->graphOffset->x + $width * cos( ( -$startAngle / 180 ) * M_PI ); + $Ystart = $center->y + $this->options->graphOffset->y + $height * sin( ( $startAngle / 180 ) * M_PI ); + $Xend = $center->x + $this->options->graphOffset->x + $width * cos( ( -( $endAngle ) / 180 ) * M_PI ); + $Yend = $center->y + $this->options->graphOffset->y + $height * sin( ( ( $endAngle ) / 180 ) * M_PI ); $arc = $this->dom->createElement( 'path' ); $arc->setAttribute('d', sprintf('M %.2f,%.2f L %.2f,%.2f A %.2f,%2f 0 %d,1 %.2f,%.2f z', // Middle - $center->x, $center->y, + $center->x + $this->options->graphOffset->x, $center->y + $this->options->graphOffset->y, // Startpoint $Xstart, $Ystart, // Radius @@ -447,10 +472,10 @@ class ezcGraphSvgDriver extends ezcGraphDriver $width /= 2; $height /= 2; - $Xstart = $center->x + $width * cos( ( -$startAngle / 180 ) * M_PI ); - $Ystart = $center->y + $height * sin( ( $startAngle / 180 ) * M_PI ); - $Xend = $center->x + $width * cos( ( -( $endAngle ) / 180 ) * M_PI ); - $Yend = $center->y + $height * sin( ( ( $endAngle ) / 180 ) * M_PI ); + $Xstart = $center->x + $this->options->graphOffset->x + $width * cos( ( -$startAngle / 180 ) * M_PI ); + $Ystart = $center->y + $this->options->graphOffset->y + $height * sin( ( $startAngle / 180 ) * M_PI ); + $Xend = $center->x + $this->options->graphOffset->x + $width * cos( ( -( $endAngle ) / 180 ) * M_PI ); + $Yend = $center->y + $this->options->graphOffset->y + $height * sin( ( ( $endAngle ) / 180 ) * M_PI ); $arc = $this->dom->createElement( 'path' ); $arc->setAttribute('d', sprintf(' M %.2f,%.2f @@ -505,8 +530,8 @@ class ezcGraphSvgDriver extends ezcGraphDriver $this->createDocument(); $ellipse = $this->dom->createElement('ellipse'); - $ellipse->setAttribute( 'cx', $center->x ); - $ellipse->setAttribute( 'cy', $center->y ); + $ellipse->setAttribute( 'cx', $center->x + $this->options->graphOffset->x ); + $ellipse->setAttribute( 'cy', $center->y + $this->options->graphOffset->y ); $ellipse->setAttribute( 'rx', $width / 2 ); $ellipse->setAttribute( 'ry', $height / 2 ); @@ -556,8 +581,8 @@ class ezcGraphSvgDriver extends ezcGraphDriver $data = getimagesize( $file ); $image = $this->dom->createElement( 'image' ); - $image->setAttribute( 'x', $position->x ); - $image->setAttribute( 'y', $position->y ); + $image->setAttribute( 'x', $position->x + $this->options->graphOffset->x ); + $image->setAttribute( 'y', $position->y + $this->options->graphOffset->y ); $image->setAttribute( 'width', $width . 'px' ); $image->setAttribute( 'height', $height . 'px' ); $image->setAttributeNS( diff --git a/src/exceptions/invalid_id.php b/src/exceptions/invalid_id.php new file mode 100644 index 0000000..d849f13 --- /dev/null +++ b/src/exceptions/invalid_id.php @@ -0,0 +1,26 @@ +<?php +/** + * File containing the ezcGraphSvgDriverInvalidIdException class + * + * @package Graph + * @version //autogen// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Exception thrown when a id could not be found in a SVG document to insert + * elements in. + * + * @package Graph + * @version //autogen// + */ +class ezcGraphSvgDriverInvalidIdException extends ezcGraphException +{ + public function __construct( $id ) + { + parent::__construct( "Could not find element with id <{$id}> in SVG document." ); + } +} + +?> + diff --git a/src/graph_autoload.php b/src/graph_autoload.php index 5e50522..15703ea 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -45,6 +45,7 @@ return array( 'ezcGraphGdDriverUnsupportedImageTypeException' => 'Graph/exceptions/unsupported_image_type.php', 'ezcGraphSvgDriver' => 'Graph/driver/svg.php', 'ezcGraphSvgDriverOptions' => 'Graph/options/svg_driver.php', + 'ezcGraphSvgDriverInvalidIdException' => 'Graph/exceptions/invalid_id.php', 'ezcGraphInvalidDriverException' => 'Graph/exceptions/invalid_driver.php', 'ezcGraphVerboseDriver' => 'Graph/driver/verbose.php', diff --git a/src/options/chart.php b/src/options/chart.php index 788a03b..edfe012 100644 --- a/src/options/chart.php +++ b/src/options/chart.php @@ -36,7 +36,7 @@ class ezcGraphChartOptions extends ezcBaseOptions */ protected $font; - public function __construct( array $options=array() ) + public function __construct( array $options = array() ) { $this->font = new ezcGraphFontOptions(); diff --git a/src/options/svg_driver.php b/src/options/svg_driver.php index 6bd4ff8..f372b8b 100644 --- a/src/options/svg_driver.php +++ b/src/options/svg_driver.php @@ -60,6 +60,37 @@ class ezcGraphSvgDriverOptions extends ezcGraphDriverOptions protected $textRendering = 'optimizeLegibility'; /** + * Use existing SVG document as template to insert graph into. If + * insertIntoGroup is not set, a new group will be inserted in the svg + * root node. + * + * @var string + */ + protected $templateDocument = false; + + /** + * ID of a SVG group node to insert the graph. Only works with a custom + * template document. + * + * @var mixed + * @access protected + */ + protected $insertIntoGroup = false; + + /** + * Offset of the graph in the svg + * + * @var ezcGraphCoordinate + */ + protected $graphOffset; + + public function __construct( array $options = array() ) + { + $this->graphOffset = new ezcGraphCoordinate( 0, 0 ); + parent::__construct( $options ); + } + + /** * Set an option value * * @param string $propertyName @@ -145,6 +176,36 @@ class ezcGraphSvgDriverOptions extends ezcGraphDriverOptions throw new ezcBaseValueException( $propertyName, $propertyValue, implode( $values, ', ' ) ); } break; + case 'templateDocument': + if ( !is_file( $propertyValue ) || !is_readable( $propertyValue ) ) + { + throw new ezcBaseFileNotFoundException( $propertyValue ); + } + else + { + $this->templateDocument = realpath( $propertyValue ); + } + break; + case 'insertIntoGroup': + if ( !is_string( $propertyValue ) ) + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'string' ); + } + else + { + $this->insertIntoGroup = $propertyValue; + } + break; + case 'graphOffset': + if ( $propertyValue instanceof ezcGraphCoordinate ) + { + $this->graphOffset = $propertyValue; + } + else + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'ezcGraphCoordinate' ); + } + break; default: parent::__set( $propertyName, $propertyValue ); break; |