Author: Kore Nordmann
Date: 2006-06-12 18:29:14 +0200 (Mon, 12 Jun 2006)
New Revision: 3125

Log:
- Added tests and implementation for background images in charts
- Fixed bug with image sizes in gd driver

Added:
   trunk/Graph/src/element/background.php
   trunk/Graph/tests/background_image_test.php
Modified:
   trunk/Graph/src/charts/line.php
   trunk/Graph/src/charts/pie.php
   trunk/Graph/src/driver/gd.php
   trunk/Graph/src/graph_autoload.php
   trunk/Graph/src/options/chart.php
   trunk/Graph/tests/chart_test.php
   trunk/Graph/tests/driver_gd_test.php
   trunk/Graph/tests/suite.php

Modified: trunk/Graph/src/charts/line.php
===================================================================
--- trunk/Graph/src/charts/line.php     2006-06-12 15:52:13 UTC (rev 3124)
+++ trunk/Graph/src/charts/line.php     2006-06-12 16:29:14 UTC (rev 3125)
@@ -162,8 +162,10 @@
 
         // 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

Modified: trunk/Graph/src/charts/pie.php
===================================================================
--- trunk/Graph/src/charts/pie.php      2006-06-12 15:52:13 UTC (rev 3124)
+++ trunk/Graph/src/charts/pie.php      2006-06-12 16:29:14 UTC (rev 3125)
@@ -200,15 +200,16 @@
         $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;

Modified: trunk/Graph/src/driver/gd.php
===================================================================
--- trunk/Graph/src/driver/gd.php       2006-06-12 15:52:13 UTC (rev 3124)
+++ trunk/Graph/src/driver/gd.php       2006-06-12 16:29:14 UTC (rev 3125)
@@ -108,7 +108,7 @@
                     'image' => imagecreatefrompng( $file )
                 );
             default:
-                throw new ezcGraphGdDriverUnsupportedImageFormatException( 
$data[2] );
+                throw new ezcGraphGdDriverUnsupportedImageTypeException( 
$data[2] );
         }
     }
 
@@ -601,8 +601,8 @@
             $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']
         );
     }

Added: trunk/Graph/src/element/background.php
===================================================================
--- trunk/Graph/src/element/background.php      2006-06-12 15:52:13 UTC (rev 
3124)
+++ trunk/Graph/src/element/background.php      2006-06-12 16:29:14 UTC (rev 
3125)
@@ -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;
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/element/background.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Graph/src/graph_autoload.php
===================================================================
--- trunk/Graph/src/graph_autoload.php  2006-06-12 15:52:13 UTC (rev 3124)
+++ trunk/Graph/src/graph_autoload.php  2006-06-12 16:29:14 UTC (rev 3125)
@@ -47,6 +47,7 @@
     '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',

Modified: trunk/Graph/src/options/chart.php
===================================================================
--- trunk/Graph/src/options/chart.php   2006-06-12 15:52:13 UTC (rev 3124)
+++ trunk/Graph/src/options/chart.php   2006-06-12 16:29:14 UTC (rev 3125)
@@ -75,6 +75,9 @@
     {
         $this->font = new ezcGraphFontOptions();
 
+        $this->backgroundImage = new ezcGraphChartElementBackgroundImage();
+        $this->backgroundImage->position = ezcGraph::CENTER | ezcGraph::MIDDLE;
+
         parent::__construct( $options );
     }
 
@@ -101,26 +104,7 @@
                 $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 );

Added: trunk/Graph/tests/background_image_test.php
===================================================================
--- trunk/Graph/tests/background_image_test.php 2006-06-12 15:52:13 UTC (rev 
3124)
+++ trunk/Graph/tests/background_image_test.php 2006-06-12 16:29:14 UTC (rev 
3125)
@@ -0,0 +1,185 @@
+<?php
+/**
+ * ezcGraphBackgroundImageTest 
+ * 
+ * @package Graph
+ * @version //autogen//
+ * @subpackage Tests
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Tests for ezcGraph class.
+ * 
+ * @package ImageAnalysis
+ * @subpackage Tests
+ */
+class ezcGraphBackgroundImageTest extends ezcTestCase
+{
+
+    protected $testFiles = array(
+        'png'          => 'png.png',
+    );
+
+    protected $basePath;
+
+    protected $tempDir;
+
+       public static function suite()
+       {
+               return new ezcTestSuite( "ezcGraphBackgroundImageTest" );
+       }
+
+    /**
+     * setUp 
+     * 
+     * @access public
+     */
+    public function setUp()
+    {
+        static $i = 0;
+        $this->tempDir = $this->createTempDir( 'ezcGraphGdDriverTest' . 
sprintf( '_%03d_', ++$i ) ) . '/';
+        $this->basePath = dirname( __FILE__ ) . '/data/';
+    }
+
+    /**
+     * tearDown 
+     * 
+     * @access public
+     */
+    public function tearDown()
+    {
+        $this->removeTempDir();
+    }
+
+    public function testRenderStandard()
+    {
+        $chart = ezcGraph::create( 'line' );
+        $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 
'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+        $chart->options->backgroundImage = $this->basePath . 
$this->testFiles['png'];
+        $chart->options->background = '#000000FF';
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawBackgroundImage',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 0 ) )
+            ->method( 'drawBackgroundImage' )
+            ->with(
+                $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+                $this->equalTo( new ezcGraphCoordinate( 162, 50 ) ),
+                $this->equalTo( 177 ),
+                $this->equalTo( 100 )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 500, 200 );
+    }
+
+    public function testRenderPieBottomRight()
+    {
+        $chart = ezcGraph::create( 'pie' );
+        $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 
'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+        $chart->options->backgroundImage = $this->basePath . 
$this->testFiles['png'];
+        $chart->options->backgroundImage->position = ezcGraph::BOTTOM | 
ezcGraph::RIGHT;
+        $chart->options->background = '#000000FF';
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawBackgroundImage',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 0 ) )
+            ->method( 'drawBackgroundImage' )
+            ->with(
+                $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+                $this->equalTo( new ezcGraphCoordinate( 323, 100 ) ),
+                $this->equalTo( 177 ),
+                $this->equalTo( 100 )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 500, 200 );
+    }
+
+    public function testRenderTop()
+    {
+        $chart = ezcGraph::create( 'line' );
+        $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 
'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+        $chart->options->backgroundImage = $this->basePath . 
$this->testFiles['png'];
+        $chart->options->backgroundImage->position = ezcGraph::TOP;
+        $chart->options->background = '#000000FF';
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawBackgroundImage',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 0 ) )
+            ->method( 'drawBackgroundImage' )
+            ->with(
+                $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+                $this->equalTo( new ezcGraphCoordinate( 162, 0 ) ),
+                $this->equalTo( 177 ),
+                $this->equalTo( 100 )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 500, 200 );
+    }
+
+    public function testRenderLeft()
+    {
+        $chart = ezcGraph::create( 'line' );
+        $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 
'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+        $chart->options->backgroundImage = $this->basePath . 
$this->testFiles['png'];
+        $chart->options->backgroundImage->position = ezcGraph::LEFT;
+        $chart->options->background = '#000000FF';
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawBackgroundImage',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 0 ) )
+            ->method( 'drawBackgroundImage' )
+            ->with(
+                $this->equalTo( $this->basePath . $this->testFiles['png'] ),
+                $this->equalTo( new ezcGraphCoordinate( 0, 50 ) ),
+                $this->equalTo( 177 ),
+                $this->equalTo( 100 )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 500, 200 );
+    }
+
+    public function testRenderWithTransparentBackground()
+    {
+        $filename = $this->tempDir . __FUNCTION__ . '.png';
+
+        $chart = ezcGraph::create( 'line' );
+        $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 
'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
+        $chart->palette = 'Black';
+        $chart->options->backgroundImage = $this->basePath . 
$this->testFiles['png'];
+        $chart->options->background = '#2E343655';
+
+        $chart->driver = new ezcGraphGdDriver();
+        $chart->options->font = $this->basePath . 'font.ttf';
+        $chart->render( 500, 200, $filename );
+
+        $this->assertTrue(
+            file_exists( $filename ),
+            'No image was generated.'
+        );
+
+        $this->assertEquals(
+            '17c3aac43de88a0a8ce620d5e72db40d',
+            md5_file( $filename ),
+            'Incorrect image rendered.'
+        );
+    }
+}
+?>


Property changes on: trunk/Graph/tests/background_image_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Graph/tests/chart_test.php
===================================================================
--- trunk/Graph/tests/chart_test.php    2006-06-12 15:52:13 UTC (rev 3124)
+++ trunk/Graph/tests/chart_test.php    2006-06-12 16:29:14 UTC (rev 3125)
@@ -69,7 +69,13 @@
         $pieChart = ezcGraph::create( 'Pie' );
         $pieChart->options->backgroundImage = $this->basePath . 
$this->testFiles['jpeg'];
 
-        $this->assertProtectedPropertySame( $pieChart->options, 
'backgroundImage', $this->basePath . $this->testFiles['jpeg'] );
+        $background = $this->getNonPublicProperty( $pieChart->options, 
'backgroundImage' );
+        $this->assertTrue(
+            $background instanceof ezcGraphChartElementBackgroundImage,
+            'Background is not an ezcGraphChartElementBackgroundImage.'
+        );
+
+        $this->assertProtectedPropertySame( $background, 'source', 
$this->basePath . $this->testFiles['jpeg'] );
     }
 
     public function testSetOptionsInvalidBackgroundImage()

Modified: trunk/Graph/tests/driver_gd_test.php
===================================================================
--- trunk/Graph/tests/driver_gd_test.php        2006-06-12 15:52:13 UTC (rev 
3124)
+++ trunk/Graph/tests/driver_gd_test.php        2006-06-12 16:29:14 UTC (rev 
3125)
@@ -436,7 +436,7 @@
         );
 
         $this->assertEquals(
-            '2c727bfabf917c5afa5144e63a6bf3c2',
+            '1f2603bbfd75fdc9cd325afd12398505',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
@@ -461,7 +461,7 @@
         );
 
         $this->assertEquals(
-            'a384e438b0853ccaaacac94fb7977f2a',
+            'dc436ebc6ceccaaac09562c7287938ae',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
@@ -1053,7 +1053,7 @@
         );
 
         $this->assertEquals(
-            'ca9fec349aab02d6518406fa02a656c9',
+            '7de0f967001b35b6726b4f8f9dd2e7ae',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );

Modified: trunk/Graph/tests/suite.php
===================================================================
--- trunk/Graph/tests/suite.php 2006-06-12 15:52:13 UTC (rev 3124)
+++ trunk/Graph/tests/suite.php 2006-06-12 16:29:14 UTC (rev 3125)
@@ -30,6 +30,7 @@
 require_once 'driver_gd_test.php';
 require_once 'font_test.php';
 require_once 'palette_test.php';
+require_once 'background_image_test.php';
 
 /**
 * Test suite for ImageAnalysis package.
@@ -58,6 +59,7 @@
         $this->addTest( ezcGraphFontTest::suite() );
         $this->addTest( ezcGraphTextTest::suite() );
         $this->addTest( ezcGraphPaletteTest::suite() );
+        $this->addTest( ezcGraphBackgroundImageTest::suite() );
     }
 
     public static function suite()

-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to