From 4c71c431a983f0c1216981e2011e823d303848d7 Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Mon, 12 Jun 2006 16:29:14 +0000 Subject: - Added tests and implementation for background images in charts - Fixed bug with image sizes in gd driver --- src/charts/line.php | 2 + src/charts/pie.php | 3 +- src/driver/gd.php | 6 +-- src/element/background.php | 128 +++++++++++++++++++++++++++++++++++++++++++++ src/graph_autoload.php | 1 + src/options/chart.php | 24 ++------- 6 files changed, 140 insertions(+), 24 deletions(-) create mode 100644 src/element/background.php (limited to 'src') 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 @@ +.' ); + } + + $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 ); -- cgit v1.1