diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-06-12 16:29:14 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-06-12 16:29:14 +0000 |
commit | 4c71c431a983f0c1216981e2011e823d303848d7 (patch) | |
tree | 8fb31999ca1fb711e54c2eb8519f59116d9f05b6 /src | |
parent | 3f1b5624719e557f8c4f7cb49f57d92fe3b37b4d (diff) | |
download | zetacomponents-graph-4c71c431a983f0c1216981e2011e823d303848d7.zip zetacomponents-graph-4c71c431a983f0c1216981e2011e823d303848d7.tar.gz |
- Added tests and implementation for background images in charts
- Fixed bug with image sizes in gd driver
Diffstat (limited to 'src')
-rw-r--r-- | src/charts/line.php | 2 | ||||
-rw-r--r-- | src/charts/pie.php | 3 | ||||
-rw-r--r-- | src/driver/gd.php | 6 | ||||
-rw-r--r-- | src/element/background.php | 128 | ||||
-rw-r--r-- | src/graph_autoload.php | 1 | ||||
-rw-r--r-- | src/options/chart.php | 24 |
6 files changed, 140 insertions, 24 deletions
diff --git a/src/charts/line.php b/src/charts/line.php index a9509a9..c86ff29 100644 --- a/src/charts/line.php +++ b/src/charts/line.php @@ -162,8 +162,10 @@ class ezcGraphLineChart extends ezcGraphChart // Render border and background $boundings = $this->renderBorder( $boundings ); + $boundings = $this->options->backgroundImage->render( $this->renderer, $boundings ); $boundings = $this->renderBackground( $boundings ); + // Render subelements foreach ( $this->elements as $name => $element ) { // Special settings for special elements diff --git a/src/charts/pie.php b/src/charts/pie.php index e907b03..8645c08 100644 --- a/src/charts/pie.php +++ b/src/charts/pie.php @@ -200,15 +200,16 @@ class ezcGraphPieChart extends ezcGraphChart $this->options->width = $width; $this->options->height = $height; - // Render subelements $boundings = new ezcGraphBoundings(); $boundings->x1 = $this->options->width; $boundings->y1 = $this->options->height; // Render border and background $boundings = $this->renderBorder( $boundings ); + $boundings = $this->options->backgroundImage->render( $this->renderer, $boundings ); $boundings = $this->renderBackground( $boundings ); + // Render subelements foreach ( $this->elements as $name => $element ) { $this->driver->options->font = $element->font; diff --git a/src/driver/gd.php b/src/driver/gd.php index 19064c2..6aa43f3 100644 --- a/src/driver/gd.php +++ b/src/driver/gd.php @@ -108,7 +108,7 @@ class ezcGraphGdDriver extends ezcGraphDriver 'image' => imagecreatefrompng( $file ) ); default: - throw new ezcGraphGdDriverUnsupportedImageFormatException( $data[2] ); + throw new ezcGraphGdDriverUnsupportedImageTypeException( $data[2] ); } } @@ -601,8 +601,8 @@ class ezcGraphGdDriver extends ezcGraphDriver $this->supersample( $position->y ), 0, 0, - $this->supersample( $position->x + $width ), - $this->supersample( $position->y + $height ), + $this->supersample( $width ), + $this->supersample( $height ), $imageFile['width'], $imageFile['height'] ); } diff --git a/src/element/background.php b/src/element/background.php new file mode 100644 index 0000000..a1a4f19 --- /dev/null +++ b/src/element/background.php @@ -0,0 +1,128 @@ +<?php +/** + * File containing the abstract ezcGraphChartElementText class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ +/** + * Class to represent a legend as a chart element + * + * @package Graph + */ +class ezcGraphChartElementBackgroundImage extends ezcGraphChartElement +{ + + /** + * Filename of the file to use for background + * + * @var string + */ + protected $source = ''; + + /** + * __set + * + * @param mixed $propertyName + * @param mixed $propertyValue + * @throws ezcBaseValueException + * If a submitted parameter was out of range or type. + * @throws ezcBasePropertyNotFoundException + * If a the value for the property options is not an instance of + * @return void + */ + public function __set( $propertyName, $propertyValue ) + { + switch ( $propertyName ) + { + case 'source': + // Check for existance of file + if ( !is_file( $propertyValue ) || !is_readable( $propertyValue ) ) + { + throw new ezcBaseFileNotFoundException( $propertyValue ); + } + + // Check for beeing an image file + $data = getImageSize( $propertyValue ); + if ( $data === false ) + { + throw new ezcGraphInvalidImageFileException( $propertyValue ); + } + + // SWF files are useless.. + if ( $data[2] === 4 ) + { + throw new ezcGraphInvalidImageFileException( 'We cant use SWF files like <' . $propertyValue . '>.' ); + } + + $this->source = $propertyValue; + break; + case 'position': + $this->position = (int) $propertyValue; + break; + default: + return parent::__set( $propertyName, $propertyValue ); + } + } + + /** + * Render a legend + * + * @param ezcGraphRenderer $renderer + * @access public + * @return void + */ + public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings ) + { + if ( empty( $this->source ) ) + { + return $boundings; + } + + // Get background image boundings + $data = getimagesize( $this->source ); + + // Determine x position + switch ( true ) + { + case ( $this->position & ezcGraph::LEFT ): + $xPosition = 0; + break; + case ( $this->position & ezcGraph::RIGHT ): + $xPosition = $boundings->x1 - $data[0]; + break; + case ( $this->position & ezcGraph::CENTER ): + default: + $xPosition = (int) round( ( $boundings->x1 - $data[0] ) / 2 ); + break; + } + + // Determine y position + switch ( true ) + { + case ( $this->position & ezcGraph::TOP ): + $yPosition = 0; + break; + case ( $this->position & ezcGraph::BOTTOM ): + $yPosition = $boundings->y1 - $data[1]; + break; + case ( $this->position & ezcGraph::MIDDLE ): + default: + $yPosition = (int) round( ( $boundings->y1 - $data[1] ) / 2 ); + break; + } + + $renderer->drawBackgroundImage( + $this->source, + new ezcGraphCoordinate( $xPosition, $yPosition ), + $data[0], + $data[1] + ); + + return $boundings; + } +} + +?> diff --git a/src/graph_autoload.php b/src/graph_autoload.php index 658bcec..525f7d9 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -47,6 +47,7 @@ return array( 'ezcGraphFontOptions' => 'Graph/options/font.php', 'ezcGraphChartElementText' => 'Graph/element/text.php', 'ezcGraphChartElementLegend' => 'Graph/element/legend.php', + 'ezcGraphChartElementBackgroundImage' => 'Graph/element/background.php', 'ezcGraphChartElementAxis' => 'Graph/element/axis.php', 'ezcGraphChartElementNumericAxis' => 'Graph/axis/numeric.php', 'ezcGraphChartElementLabeledAxis' => 'Graph/axis/labeled.php', diff --git a/src/options/chart.php b/src/options/chart.php index ada1f77..ccbb6c0 100644 --- a/src/options/chart.php +++ b/src/options/chart.php @@ -75,6 +75,9 @@ class ezcGraphChartOptions extends ezcBaseOptions { $this->font = new ezcGraphFontOptions(); + $this->backgroundImage = new ezcGraphChartElementBackgroundImage(); + $this->backgroundImage->position = ezcGraph::CENTER | ezcGraph::MIDDLE; + parent::__construct( $options ); } @@ -101,26 +104,7 @@ class ezcGraphChartOptions extends ezcBaseOptions $this->padding = max( 0, (int) $propertyValue ); break; case 'backgroundImage': - // Check for existance of file - if ( !is_file( $propertyValue ) || !is_readable( $propertyValue ) ) - { - throw new ezcBaseFileNotFoundException( $propertyValue ); - } - - // Check for beeing an image file - $data = getImageSize( $propertyValue ); - if ( $data === false ) - { - throw new ezcGraphInvalidImageFileException( $propertyValue ); - } - - // SWF files are useless.. - if ( $data[2] === 4 ) - { - throw new ezcGraphInvalidImageFileException( 'We cant use SWF files like <' . $propertyValue . '>.' ); - } - - $this->backgroundImage = $propertyValue; + $this->backgroundImage->source = $propertyValue; break; case 'background': $this->background = ezcGraphColor::create( $propertyValue ); |