Author: Kore Nordmann
Date: 2006-06-12 13:09:21 +0200 (Mon, 12 Jun 2006)
New Revision: 3118

Log:
- Added tests and basic implementation for pie charts

Modified:
   trunk/Graph/src/charts/line.php
   trunk/Graph/src/charts/pie.php
   trunk/Graph/src/driver/gd.php
   trunk/Graph/src/driver/svg.php
   trunk/Graph/src/interfaces/chart.php
   trunk/Graph/src/interfaces/driver.php
   trunk/Graph/src/renderer/2d.php
   trunk/Graph/src/structs/color.php
   trunk/Graph/tests/driver_gd_test.php
   trunk/Graph/tests/line_test.php
   trunk/Graph/tests/palette_test.php
   trunk/Graph/tests/pie_test.php
   trunk/Graph/tests/renderer_2d_test.php

Modified: trunk/Graph/src/charts/line.php
===================================================================
--- trunk/Graph/src/charts/line.php     2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/src/charts/line.php     2006-06-12 11:09:21 UTC (rev 3118)
@@ -15,8 +15,10 @@
 class ezcGraphLineChart extends ezcGraphChart
 {
  
-    public function __construct()
+    public function __construct( array $options = array() )
     {
+        $this->options = new ezcGraphChartOptions( $options );
+
         parent::__construct();
 
         $this->addElement( 'X_axis', new ezcGraphChartElementLabeledAxis() );

Modified: trunk/Graph/src/charts/pie.php
===================================================================
--- trunk/Graph/src/charts/pie.php      2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/src/charts/pie.php      2006-06-12 11:09:21 UTC (rev 3118)
@@ -14,6 +14,18 @@
  */
 class ezcGraphPieChart extends ezcGraphChart
 {
+    
+    protected $maxLabelHeight = .15;
+
+    protected $label = '%1$s: %2$d (%3$.1f%%)';
+
+    public function __construct( array $options = array() )
+    {
+        $this->options = new ezcGraphChartOptions( $options );
+
+        parent::__construct( $options );
+    }
+
     /**
      * Adds a dataset to the charts data
      * 
@@ -33,9 +45,145 @@
         else
         {
             parent::addDataSet( $name, $values );
+
+            // Colorize each data element
+            foreach ( $this->data[$name] as $label => $value )
+            {
+                $this->data[$name]->color[$label] = 
$this->palette->dataSetColor;
+            }
         }
     }
 
+    protected function renderData( $renderer, $boundings )
+    {
+        // Only draw the first (and only) dataset
+        $dataset = reset( $this->data );
+
+        $this->driver->options->font = $this->options->font;
+
+        // Calculate sum of all values to be able to calculate percentage
+        $sum = 0;
+        foreach ( $dataset as $value )
+        {
+            $sum += $value;
+        }
+    
+        // Calculate position and size of pie
+        $center = new ezcGraphCoordinate(
+            (int) round( $boundings->x0 + ( $boundings->x1 - $boundings->x0 ) 
/ 2 ),
+            (int) round( $boundings->y0 + ( $boundings->y1 - $boundings->y0 ) 
/ 2 )
+        );
+        $radius = min(
+            $boundings->x1 - $boundings->x0,
+            $boundings->y1 - $boundings->y0
+        ) / 2;
+
+        // Draw all data
+        $angle = 0.;
+        $labels = array();
+        foreach ( $dataset as $label => $value )
+        {
+            $renderer->drawPieSegment(
+                $dataset->color[$label],
+                $center,
+                $radius,
+                $angle,
+                $endAngle = $angle + $value / $sum * 360,
+                0
+            );
+
+            // Determine position of label
+            $middle = $angle + ( $endAngle - $angle ) / 2;
+            $pieSegmentCenter = new ezcGraphCoordinate(
+                (int) round( cos( deg2rad( $middle ) ) * $radius + $center->x 
),
+                (int) round( sin( deg2rad( $middle ) ) * $radius + $center->y )
+            );
+
+            // Split labels up into left an right size and index them on their
+            // y position
+            $labels[(int) ($pieSegmentCenter->x > 
$center->x)][$pieSegmentCenter->y] = array(
+                new ezcGraphCoordinate(
+                    (int) round( cos( deg2rad( $middle ) ) * $radius * 2 / 3 + 
$center->x ),
+                    (int) round( sin( deg2rad( $middle ) ) * $radius * 2 / 3 + 
$center->y )
+                ),
+                sprintf( $this->label, $label, $value, $value * 100 / $sum )
+            );
+            $angle = $endAngle;
+        }
+
+        $labelHeight = (int) round( min(
+            ( $boundings->y1 - $boundings->y0 ) / count( $labels[0] ),
+            ( $boundings->y1 - $boundings->y0 ) / count( $labels[1] ),
+            ( $boundings->y1 - $boundings->y0 ) * $this->maxLabelHeight
+        ) );
+        
+        $symbolSize = 6;
+
+        // Finally draw labels
+        foreach ( $labels as $side => $labelPart )
+        {
+            $minHeight = $boundings->y0;
+            $toShare = ( $boundings->y1 - $boundings->y0 ) - count( $labelPart 
) * $labelHeight;
+
+            // Sort to draw topmost label first
+            ksort( $labelPart );
+            $sign = ( $side ? -1 : 1 );
+
+            foreach ( $labelPart as $height => $label )
+            {
+                // Determine position of label
+                $minHeight += round( max( 0, $height - $minHeight ) / ( 
$boundings->y1 - $boundings->y0 ) * $toShare );
+                $labelPosition = new ezcGraphCoordinate(
+                    (int) round( $center->x - $sign * ( cos ( asin ( ( 
$center->y - $minHeight - $labelHeight / 2 ) / $radius ) ) * $radius + 
$symbolSize ) ),
+                    (int) round( $minHeight + $labelHeight / 2 )
+                );
+
+                // Draw label
+                $renderer->drawLine(
+                    $this->options->font->color,
+                    $label[0],
+                    $labelPosition,
+                    false
+                );
+
+                $renderer->drawSymbol(
+                    $this->options->font->color,
+                    new ezcGraphCoordinate(
+                        $label[0]->x - $symbolSize / 2,
+                        $label[0]->y - $symbolSize / 2
+                    ),
+                    $symbolSize,
+                    $symbolSize,
+                    ezcGraph::BULLET
+                );
+                $renderer->drawSymbol(
+                    $this->options->font->color,
+                    new ezcGraphCoordinate(
+                        $labelPosition->x - $symbolSize / 2,
+                        $labelPosition->y - $symbolSize / 2
+                    ),
+                    $symbolSize,
+                    $symbolSize,
+                    ezcGraph::BULLET
+                );
+
+                $renderer->drawTextBox(
+                    new ezcGraphCoordinate(
+                        ( !$side ? $boundings->x0 : $labelPosition->x + 
$symbolSize ),
+                        $minHeight
+                    ),
+                    $label[1],
+                    (int) round( !$side ? $labelPosition->x - $boundings->x0 - 
$symbolSize : $boundings->x1 - $labelPosition->x - $symbolSize ),
+                    $labelHeight,
+                    ( !$side ? ezcGraph::RIGHT : ezcGraph::LEFT ) | 
ezcGraph::MIDDLE
+                );
+
+                // Add used space to minHeight
+                $minHeight += $labelHeight;
+            }
+        }
+    }
+
     /**
      * Render a pie chart
      * 
@@ -45,8 +193,39 @@
      */
     public function render( $width, $height, $file = null )
     {
+        // Set image properties in driver
+        $this->driver->options->width = $width;
+        $this->driver->options->height = $height;
+
         // Generate legend
         $this->elements['legend']->generateFromDataset( reset( $this->data ) );
+
+        // Get boundings from parameters
+        $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->renderBackground( $boundings );
+
+        foreach ( $this->elements as $name => $element )
+        {
+            $this->driver->options->font = $element->font;
+            $boundings = $element->render( $this->renderer, $boundings );
+        }
+
+        // Render graph
+        $this->renderData( $this->renderer, $boundings );
+
+        if ( !empty( $file ) )
+        {
+            $this->renderer->render( $file );
+        }
     }
 }
 

Modified: trunk/Graph/src/driver/gd.php
===================================================================
--- trunk/Graph/src/driver/gd.php       2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/src/driver/gd.php       2006-06-12 11:09:21 UTC (rev 3118)
@@ -43,8 +43,8 @@
         if ( !isset( $this->image ) )
         {
             $this->image = imagecreatetruecolor( 
-                $this->options->width * $this->options->supersampling, 
-                $this->options->height * $this->options->supersampling
+                $this->supersample( $this->options->width ), 
+                $this->supersample( $this->options->height )
             );
             $bgColor = imagecolorallocate( $this->image, 255, 255, 255 );
             // Default to a white background
@@ -108,7 +108,7 @@
                     'image' => imagecreatefrompng( $file )
                 );
             default:
-                throw new ezcGraphGdDriverUnsupportedImageTypeException( 
$data[2] );
+                throw new ezcGraphGdDriverUnsupportedImageFormatException( 
$data[2] );
         }
     }
 
@@ -126,7 +126,7 @@
      * @param mixed $filled 
      * @return void
      */
-    public function drawPolygon( array $points, ezcGraphColor $color, $filled 
= true )
+    public function drawPolygon( array $points, ezcGraphColor $color, $filled 
= true, $thickness = 1 )
     {
         $image = $this->getImage();
 
@@ -229,6 +229,12 @@
             }
         }
 
+        // Check width of last line
+        $boundings = imagettfbbox( $size, 0, $this->options->font->font, 
implode( ' ', $lines[$line] ) );
+        if ( $boundings[2] > $width ) {
+            return false;
+        }
+
         // It seems to fit - return line array
         return $lines;
     }
@@ -373,7 +379,7 @@
      * @param ezcGraphColor $color 
      * @return void
      */
-    public function drawCircleSector( ezcGraphCoordinate $center, $width, 
$height, $startAngle, $endAngle, ezcGraphColor $color )
+    public function drawCircleSector( ezcGraphCoordinate $center, $width, 
$height, $startAngle, $endAngle, ezcGraphColor $color, $filled = true )
     {
         $image = $this->getImage();
         $drawColor = $this->allocate( $color );
@@ -386,17 +392,34 @@
             $endAngle = $tmp;
         }
 
-        imagefilledarc( 
-            $image, 
-            $this->supersample( $center->x ), 
-            $this->supersample( $center->y ), 
-            $this->supersample( $width ), 
-            $this->supersample( $height ), 
-            $startAngle, 
-            $endAngle, 
-            $drawColor, 
-            IMG_ARC_PIE 
-        );
+        if ( $filled )
+        {
+            imagefilledarc( 
+                $image, 
+                $this->supersample( $center->x ), 
+                $this->supersample( $center->y ), 
+                $this->supersample( $width ), 
+                $this->supersample( $height ), 
+                $startAngle, 
+                $endAngle, 
+                $drawColor, 
+                IMG_ARC_PIE 
+            );
+        }
+        else
+        {
+            imagefilledarc( 
+                $image, 
+                $this->supersample( $center->x ), 
+                $this->supersample( $center->y ), 
+                $this->supersample( $width ), 
+                $this->supersample( $height ), 
+                $startAngle, 
+                $endAngle, 
+                $drawColor, 
+                IMG_ARC_PIE | IMG_ARC_NOFILL | IMG_ARC_EDGED
+            );
+        }
     }
 
     /**

Modified: trunk/Graph/src/driver/svg.php
===================================================================
--- trunk/Graph/src/driver/svg.php      2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/src/driver/svg.php      2006-06-12 11:09:21 UTC (rev 3118)
@@ -29,7 +29,7 @@
      * @param mixed $filled 
      * @return void
      */
-    public function drawPolygon( array $points, ezcGraphColor $color, $filled 
= true )
+    public function drawPolygon( array $points, ezcGraphColor $color, $filled 
= true, $thickness = 1 )
     {
         
     }
@@ -42,7 +42,7 @@
      * @param ezcGraphColor $color 
      * @return void
      */
-    public function drawLine( ezcGraphCoordinate $start, ezcGraphCoordinate 
$end, ezcGraphColor $color )
+    public function drawLine( ezcGraphCoordinate $start, ezcGraphCoordinate 
$end, ezcGraphColor $color, $thickness = 1 )
     {
         
     }
@@ -73,7 +73,7 @@
      * @param ezcGraphColor $color 
      * @return void
      */
-    public function drawCircleSector( ezcGraphCoordinate $center, $width, 
$height, $startAngle, $endAngle, ezcGraphColor $color )
+    public function drawCircleSector( ezcGraphCoordinate $center, $width, 
$height, $startAngle, $endAngle, ezcGraphColor $color, $filled = true )
     {
         
     }

Modified: trunk/Graph/src/interfaces/chart.php
===================================================================
--- trunk/Graph/src/interfaces/chart.php        2006-06-12 11:00:39 UTC (rev 
3117)
+++ trunk/Graph/src/interfaces/chart.php        2006-06-12 11:09:21 UTC (rev 
3118)
@@ -59,8 +59,6 @@
 
     public function __construct( array $options = array() )
     {
-        $this->options = new ezcGraphChartOptions( $options );
-
         $this->__set( 'palette', 'Tango' );
 
         // Add standard elements

Modified: trunk/Graph/src/interfaces/driver.php
===================================================================
--- trunk/Graph/src/interfaces/driver.php       2006-06-12 11:00:39 UTC (rev 
3117)
+++ trunk/Graph/src/interfaces/driver.php       2006-06-12 11:09:21 UTC (rev 
3118)
@@ -92,7 +92,7 @@
      * @param mixed $filled 
      * @return void
      */
-    abstract public function drawPolygon( array $points, ezcGraphColor $color, 
$filled = true );
+    abstract public function drawPolygon( array $points, ezcGraphColor $color, 
$filled = true, $thickness = 1 );
     
     /**
      * Draws a single line
@@ -102,7 +102,7 @@
      * @param ezcGraphColor $color 
      * @return void
      */
-    abstract public function drawLine( ezcGraphCoordinate $start, 
ezcGraphCoordinate $end, ezcGraphColor $color );
+    abstract public function drawLine( ezcGraphCoordinate $start, 
ezcGraphCoordinate $end, ezcGraphColor $color, $thickness = 1 );
     
     /**
      * Wrties text in a box of desired size
@@ -127,7 +127,7 @@
      * @param ezcGraphColor $color 
      * @return void
      */
-    abstract public function drawCircleSector( ezcGraphCoordinate $center, 
$width, $height, $startAngle, $endAngle, ezcGraphColor $color );
+    abstract public function drawCircleSector( ezcGraphCoordinate $center, 
$width, $height, $startAngle, $endAngle, ezcGraphColor $color, $filled = true );
     
     /**
      * Draws a circular arc

Modified: trunk/Graph/src/renderer/2d.php
===================================================================
--- trunk/Graph/src/renderer/2d.php     2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/src/renderer/2d.php     2006-06-12 11:09:21 UTC (rev 3118)
@@ -37,6 +37,8 @@
             );
 
         }
+        
+        $darkenedColor = $color->darken( .5 );
 
         $this->driver->drawCircleSector(
             $position,
@@ -44,8 +46,19 @@
             $radius * 2,
             $startAngle,
             $endAngle,
-            $color
+            $color,
+            true
         );
+
+        $this->driver->drawCircleSector(
+            $position,
+            $radius * 2,
+            $radius * 2,
+            $startAngle,
+            $endAngle,
+            $darkenedColor,
+            false
+        );
     }
     
     /**

Modified: trunk/Graph/src/structs/color.php
===================================================================
--- trunk/Graph/src/structs/color.php   2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/src/structs/color.php   2006-06-12 11:09:21 UTC (rev 3118)
@@ -213,9 +213,9 @@
         $color = clone $this;
 
         $value = 1 - $value;
-        $color->red *= $value;
-        $color->green *= $value;
-        $color->blue *= $value;
+        $color->red = (int) round( $this->red * $value );
+        $color->green = (int) round( $this->green * $value );
+        $color->blue = (int) round( $this->blue * $value );
 
         return $color;
     }

Modified: trunk/Graph/tests/driver_gd_test.php
===================================================================
--- trunk/Graph/tests/driver_gd_test.php        2006-06-12 11:00:39 UTC (rev 
3117)
+++ trunk/Graph/tests/driver_gd_test.php        2006-06-12 11:09:21 UTC (rev 
3118)
@@ -27,7 +27,6 @@
     protected $testFiles = array(
         'jpeg'          => 'jpeg.jpg',
         'png'           => 'png.png',
-        'text'          => 'text.txt',
     );
 
        public static function suite()
@@ -201,6 +200,34 @@
         );
     }
 
+    public function testDrawCircleSectorAcuteNonFilled()
+    {
+        $filename = $this->tempDir . __FUNCTION__ . '.png';
+
+        $this->driver->drawCircleSector(
+            new ezcGraphCoordinate( 100, 50 ),
+            80,
+            40,
+            12.5,
+            45,
+            ezcGraphColor::fromHex( '#3465A4' ),
+            false
+        );
+
+        $this->driver->render( $filename );
+
+        $this->assertTrue(
+            file_exists( $filename ),
+            'No image was generated.'
+        );
+
+        $this->assertEquals(
+            '34d665371c527ea77ff0489121feb12b',
+            md5_file( $filename ),
+            'Incorrect image rendered.'
+        );
+    }
+
     public function testDrawCircleSectorAcuteReverse()
     {
         $filename = $this->tempDir . __FUNCTION__ . '.png';
@@ -277,7 +304,7 @@
         );
 
         $this->assertEquals(
-            'b553423de4a14f54bc86b34656585169',
+            '5cfb7d9db3b242734460a20f67ff48fd',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
@@ -305,7 +332,7 @@
         );
 
         $this->assertEquals(
-            'b553423de4a14f54bc86b34656585169',
+            '5cfb7d9db3b242734460a20f67ff48fd',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
@@ -333,7 +360,7 @@
         );
 
         $this->assertEquals(
-            'dc388c561ab72cb7113ed650455550a9',
+            '13bdd4f4fd40c65b0ff428ba3491a30c',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
@@ -415,26 +442,6 @@
         );
     }
 
-    public function testDrawImageInvalideFileType()
-    {
-        $filename = $this->tempDir . __FUNCTION__ . '.png';
-
-        try {
-            $this->driver->drawImage(
-                $this->basePath . $this->testFiles['text'],
-                new ezcGraphCoordinate( 10, 10 ),
-                100,
-                50
-            );
-        }
-        catch ( ezcGraphGdDriverUnsupportedImageTypeException $e )
-        {
-            return true;
-        }
-
-        $this->fail( 'Expected ezcGraphGdDriverUnsupportedImageTypeException.' 
);
-    }
-
     public function testDrawImagePng()
     {
         $filename = $this->tempDir . __FUNCTION__ . '.png';
@@ -967,7 +974,7 @@
         );
 
         $this->assertEquals(
-            '129f5f8516b3ba58489837930a0cc599',
+            '290939514fee7a5572ef786f54dae827',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
@@ -1078,6 +1085,43 @@
             'Incorrect image rendered.'
         );
     }
+
+    public function testDrawStringWithSpecialChars()
+    {
+        $filename = $this->tempDir . __FUNCTION__ . '.png';
+        $this->driver->options->supersampling = 2;
+
+        $this->driver->drawPolygon(
+            array(
+                new ezcGraphCoordinate( 47, 54 ),
+                new ezcGraphCoordinate( 47, 84 ),
+                new ezcGraphCoordinate( 99, 84 ),
+                new ezcGraphCoordinate( 99, 54 ),
+            ),
+            ezcGraphColor::fromHex( '#DDDDDD' ),
+            true
+        );
+        $this->driver->drawTextBox(
+            'Safari (13.8%)',
+            new ezcGraphCoordinate( 47, 54 ),
+            52,
+            30,
+            ezcGraph::LEFT
+        );
+
+        $this->driver->render( $filename );
+
+        $this->assertTrue(
+            file_exists( $filename ),
+            'No image was generated.'
+        );
+
+        $this->assertEquals(
+            'ef4deac70fa8c0ca3d4149fd65e31131',
+            md5_file( $filename ),
+            'Incorrect image rendered.'
+        );
+    }
 }
 
 ?>

Modified: trunk/Graph/tests/line_test.php
===================================================================
--- trunk/Graph/tests/line_test.php     2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/tests/line_test.php     2006-06-12 11:09:21 UTC (rev 3118)
@@ -46,7 +46,7 @@
      */
     public function tearDown()
     {
-        //$this->removeTempDir();
+        $this->removeTempDir();
     }
 
     protected function addSampleData( ezcGraphChart $chart )
@@ -237,7 +237,7 @@
         );
 
         $this->assertEquals(
-            '8cc7463f5fe568a3116824137251222f',
+            'ef4deac70fa8c0ca3d4149fd65e31131',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );

Modified: trunk/Graph/tests/palette_test.php
===================================================================
--- trunk/Graph/tests/palette_test.php  2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/tests/palette_test.php  2006-06-12 11:09:21 UTC (rev 3118)
@@ -214,7 +214,7 @@
         $chart = ezcGraph::create( 'Line' );
 
         $this->assertEquals(
-            ezcGraphColor::fromHex( '#555753' ),
+            ezcGraphColor::fromHex( '#2E3436' ),
             $chart->palette->fontColor,
             'Font color not properly set.'
         );

Modified: trunk/Graph/tests/pie_test.php
===================================================================
--- trunk/Graph/tests/pie_test.php      2006-06-12 11:00:39 UTC (rev 3117)
+++ trunk/Graph/tests/pie_test.php      2006-06-12 11:09:21 UTC (rev 3118)
@@ -18,6 +18,10 @@
 class ezcGraphPieChartTest extends ezcTestCase
 {
 
+    protected $basePath;
+
+    protected $tempDir;
+
        public static function suite()
        {
                return new ezcTestSuite( "ezcGraphPieChartTest" );
@@ -30,6 +34,9 @@
      */
     public function setUp()
     {
+        static $i = 0;
+        $this->tempDir = $this->createTempDir( 'ezcGraphGdDriverTest' . 
sprintf( '_%03d_', ++$i ) ) . '/';
+        $this->basePath = dirname( __FILE__ ) . '/data/';
     }
 
     /**
@@ -39,14 +46,13 @@
      */
     public function tearDown()
     {
+        $this->removeTempDir();
     }
 
     public function testElementGenerationLegend()
     {
         $chart = ezcGraph::create( 'Pie' );
         $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 
'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1);
-        $chart->sampleData->color = '#0000FF';
-        $chart->sampleData->color['sample 3'] = '#FF0000';
         $chart->render( 500, 200 );
         
         $legend = $this->getNonPublicProperty( $chart->legend, 'labels' );
@@ -64,23 +70,209 @@
         );
 
         $this->assertEquals(
-            ezcGraphColor::fromHex( '#0000FF' ),
+            ezcGraphColor::fromHex( '#CC0000' ),
             $legend[1]['color'],
             'Default color for single label is wrong.'
         );
 
         $this->assertEquals(
-            ezcGraphColor::fromHex( '#FF0000' ),
+            ezcGraphColor::fromHex( '#EDD400' ),
             $legend[2]['color'],
             'Special color for single label is wrong.'
         );
     }
 
-    public function testRender()
+    public function testPieRenderPieSegments()
     {
-        throw new PHPUnit2_Framework_IncompleteTestError(
-            '@TODO: Implement renderer tests.'
+        $chart = ezcGraph::create( 'Pie' );
+        $chart->sample = array(
+            'Mozilla' => 4375,
+            'IE' => 345,
+            'Opera' => 1204,
+            'wget' => 231,
+            'Safari' => 987,
         );
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawPieSegment',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 0 ) )
+            ->method( 'drawPieSegment' )
+            ->with(
+                $this->equalTo( ezcGraphColor::fromHex( '#4E9A06' ) ),
+                $this->equalTo( new ezcGraphCoordinate( 240, 100 ) ),
+                $this->equalTo( 100 ),
+                $this->equalTo( 0 ),
+                $this->equalTo( 220.52646317558 ),
+                $this->equalTo( 0 )
+            );
+
+        $mockedRenderer
+            ->expects( $this->at( 1 ) )
+            ->method( 'drawPieSegment' )
+            ->with(
+                $this->equalTo( ezcGraphColor::fromHex( '#4E9A06' ) ),
+                $this->equalTo( new ezcGraphCoordinate( 240, 100 ) ),
+                $this->equalTo( 100 ),
+                $this->equalTo( 220.52646317558 ),
+                $this->equalTo( 0 ),
+                $this->equalTo( 0 )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 400, 200 );
     }
+
+    public function testPieRenderPieLables()
+    {
+        $chart = ezcGraph::create( 'Pie' );
+        $chart->sample = array(
+            'Mozilla' => 4375,
+            'IE' => 345,
+            'Opera' => 1204,
+            'wget' => 231,
+            'Safari' => 987,
+        );
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawTextBox',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 5 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 80, 0 ) ),
+                $this->equalTo( 'Opera: 1204 (16.9%)' ),
+                $this->equalTo( 95 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 6 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 80, 30 ) ),
+                $this->equalTo( 'IE: 345 (4.8%)' ),
+                $this->equalTo( 64 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 7 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 80, 134 ) ),
+                $this->equalTo( 'Mozilla: 4375 (61.3%)' ),
+                $this->equalTo( 61 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 8 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 321, 13 ) ),
+                $this->equalTo( 'wget: 231 (3.2%)' ),
+                $this->equalTo( 79 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::LEFT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 9 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 347, 54 ) ),
+                $this->equalTo( 'Safari: 987 (13.8%)' ),
+                $this->equalTo( 53 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::LEFT | ezcGraph::MIDDLE )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 400, 200 );
+    }
+
+    public function testPieRenderPieLableIdentifiers()
+    {
+        $chart = ezcGraph::create( 'Pie' );
+        $chart->sample = array(
+            'Mozilla' => 4375,
+            'IE' => 345,
+            'Opera' => 1204,
+            'wget' => 231,
+            'Safari' => 987,
+        );
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawLine',
+            'drawSymbol',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 5 ) )
+            ->method( 'drawLine' )
+            ->with(
+                $this->equalTo( ezcGraphColor::fromHex( '#2E3436' ) ),
+                $this->equalTo( new ezcGraphCoordinate( 238, 33 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 181, 15 ) ),
+                $this->equalTo( false )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 6 ) )
+            ->method( 'drawSymbol' )
+            ->with(
+                $this->equalTo( ezcGraphColor::fromHex( '#2E3436' ) ),
+                $this->equalTo( new ezcGraphCoordinate( 235, 30 ) ),
+                $this->equalTo( 6 ),
+                $this->equalTo( 6 ),
+                $this->equalTo( ezcGraph::BULLET )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 7 ) )
+            ->method( 'drawSymbol' )
+            ->with(
+                $this->equalTo( ezcGraphColor::fromHex( '#2E3436' ) ),
+                $this->equalTo( new ezcGraphCoordinate( 178, 12 ) ),
+                $this->equalTo( 6 ),
+                $this->equalTo( 6 ),
+                $this->equalTo( ezcGraph::BULLET )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 400, 200 );
+    }
+
+    public function testCompleteRendering()
+    {
+        $filename = $this->tempDir . __FUNCTION__ . '.png';
+
+        $chart = ezcGraph::create( 'Pie' );
+
+        $chart->sample = array(
+            'Mozilla' => 4375,
+            'IE' => 345,
+            'Opera' => 1204,
+            'wget' => 231,
+            'Safari' => 987,
+        );
+
+        $chart->driver = new ezcGraphGdDriver();
+        $chart->options->font = $this->basePath . 'font.ttf';
+        $chart->render( 400, 200, $filename );
+
+        $this->assertTrue(
+            file_exists( $filename ),
+            'No image was generated.'
+        );
+
+        $this->assertEquals(
+            'b1de7ecb51784885c11d86091489d12d',
+            md5_file( $filename ),
+            'Incorrect image rendered.'
+        );
+    }
 }
 ?>

Modified: trunk/Graph/tests/renderer_2d_test.php
===================================================================
--- trunk/Graph/tests/renderer_2d_test.php      2006-06-12 11:00:39 UTC (rev 
3117)
+++ trunk/Graph/tests/renderer_2d_test.php      2006-06-12 11:09:21 UTC (rev 
3118)
@@ -244,7 +244,7 @@
     public function testRenderPieSegment()
     {
         $this->driver
-            ->expects( $this->once() )
+            ->expects( $this->at( 0 ) )
             ->method( 'drawCircleSector' )
             ->with(
                 $this->equalTo( new ezcGraphCoordinate( 100, 100 ) ),
@@ -252,8 +252,21 @@
                 $this->equalTo( 200 ),
                 $this->equalTo( 20 ),
                 $this->equalTo( 124 ),
-                $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) )
+                $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ),
+                $this->equalTo( true )
             );
+        $this->driver
+            ->expects( $this->at( 1 ) )
+            ->method( 'drawCircleSector' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 100, 100 ) ),
+                $this->equalTo( 200 ),
+                $this->equalTo( 200 ),
+                $this->equalTo( 20 ),
+                $this->equalTo( 124 ),
+                $this->equalTo( ezcGraphColor::fromHex( '#800000' ) ),
+                $this->equalTo( false )
+            );
 
         $this->renderer->drawPieSegment(
             ezcGraphColor::fromHex( '#FF0000' ),
@@ -267,7 +280,7 @@
     public function testRenderPieSegmentMoveOut()
     {
         $this->driver
-            ->expects( $this->once() )
+            ->expects( $this->at( 0 ) )
             ->method( 'drawCircleSector' )
             ->with(
                 $this->equalTo( new ezcGraphCoordinate( 95, 100 ) ),
@@ -277,6 +290,18 @@
                 $this->equalTo( 270 ),
                 $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) )
             );
+        $this->driver
+            ->expects( $this->at( 1 ) )
+            ->method( 'drawCircleSector' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 95, 100 ) ),
+                $this->equalTo( 200 ),
+                $this->equalTo( 200 ),
+                $this->equalTo( 90 ),
+                $this->equalTo( 270 ),
+                $this->equalTo( ezcGraphColor::fromHex( '#800000' ) ),
+                $this->equalTo( false )
+            );
 
         $this->renderer->drawPieSegment(
             ezcGraphColor::fromHex( '#FF0000' ),

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

Reply via email to