Author: Kore Nordmann
Date: 2006-06-20 13:16:48 +0200 (Tue, 20 Jun 2006)
New Revision: 3149

Log:
- Added option to remove symbol lines from pie charts
- Decrease size of pie charts for images with too less width

Modified:
   trunk/Graph/src/charts/pie.php
   trunk/Graph/src/options/pie_chart.php
   trunk/Graph/tests/pie_test.php

Modified: trunk/Graph/src/charts/pie.php
===================================================================
--- trunk/Graph/src/charts/pie.php      2006-06-19 19:34:53 UTC (rev 3148)
+++ trunk/Graph/src/charts/pie.php      2006-06-20 11:16:48 UTC (rev 3149)
@@ -69,10 +69,12 @@
             (int) round( $boundings->x0 + ( $boundings->x1 - $boundings->x0 ) 
/ 2 ),
             (int) round( $boundings->y0 + ( $boundings->y1 - $boundings->y0 ) 
/ 2 )
         );
+
+        // Limit radius to fourth of width and half of height at maximum
         $radius = min(
-            $boundings->x1 - $boundings->x0,
-            $boundings->y1 - $boundings->y0
-        ) / 2;
+            ( $boundings->x1 - $boundings->x0 ) / 4,
+            ( $boundings->y1 - $boundings->y0 ) / 2
+        );
 
         // Draw all data
         $angle = 0.;
@@ -130,38 +132,47 @@
                 // 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( 
+                        $center->x - 
+                        $sign * ( 
+                            cos ( asin ( ( $center->y - $minHeight - 
$labelHeight / 2 ) / $radius ) ) * $radius + 
+                            $symbolSize * (int) $this->options->showSymbol 
+                        ) 
+                    ),
                     (int) round( $minHeight + $labelHeight / 2 )
                 );
 
-                // Draw label
-                $renderer->drawLine(
-                    $this->options->font->color,
-                    $label[0],
-                    $labelPosition,
-                    false
-                );
+                if ( $this->options->showSymbol )
+                {
+                    // 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->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(

Modified: trunk/Graph/src/options/pie_chart.php
===================================================================
--- trunk/Graph/src/options/pie_chart.php       2006-06-19 19:34:53 UTC (rev 
3148)
+++ trunk/Graph/src/options/pie_chart.php       2006-06-20 11:16:48 UTC (rev 
3149)
@@ -34,6 +34,13 @@
     protected $label = '%1$s: %2$d (%3$.1f%%)';
 
     /**
+     * Indicates wheather to show the line between pie elements and labels
+     * 
+     * @var bool
+     */
+    protected $showSymbol = true;
+
+    /**
      * Size of symbols used concat a label with a pie
      * 
      * @var float
@@ -74,6 +81,9 @@
             case 'moveOut':
                 $this->moveOut = min( 1, max( 0, (float) $propertyValue ) );
                 break;
+            case 'showSymbol':
+                $this->showSymbol = (bool) $propertyValue;
+                break;
             default:
                 return parent::__set( $propertyName, $propertyValue );
         }

Modified: trunk/Graph/tests/pie_test.php
===================================================================
--- trunk/Graph/tests/pie_test.php      2006-06-19 19:34:53 UTC (rev 3148)
+++ trunk/Graph/tests/pie_test.php      2006-06-20 11:16:48 UTC (rev 3149)
@@ -103,7 +103,7 @@
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#4E9A06' ) ),
                 $this->equalTo( new ezcGraphCoordinate( 240, 100 ) ),
-                $this->equalTo( 90 ),
+                $this->equalTo( 72 ),
                 $this->equalTo( 0 ),
                 $this->equalTo( 220.52646317558, .1 ),
                 $this->equalTo( 0 )
@@ -114,7 +114,7 @@
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#CC0000' ) ),
                 $this->equalTo( new ezcGraphCoordinate( 240, 100 ) ),
-                $this->equalTo( 90 ),
+                $this->equalTo( 72 ),
                 $this->equalTo( 220.52646317558, .1 ),
                 $this->equalTo( 237.916549986, .1 ),
                 $this->equalTo( 0 )
@@ -125,7 +125,7 @@
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#EDD400' ) ),
                 $this->equalTo( new ezcGraphCoordinate( 240, 100 ) ),
-                $this->equalTo( 90 ),
+                $this->equalTo( 72 ),
                 $this->equalTo( 237.916549986, .1 ),
                 $this->equalTo( 298.60543265192, .1 ),
                 $this->equalTo( 0 )
@@ -136,7 +136,7 @@
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#75505B' ) ),
                 $this->equalTo( new ezcGraphCoordinate( 240, 100 ) ),
-                $this->equalTo( 90 ),
+                $this->equalTo( 72 ),
                 $this->equalTo( 298.60543265192, .1 ),
                 $this->equalTo( 310.24922990759, .1 ),
                 $this->equalTo( 0 )
@@ -147,7 +147,7 @@
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#F57900' ) ),
                 $this->equalTo( new ezcGraphCoordinate( 240, 100 ) ),
-                $this->equalTo( 90 ),
+                $this->equalTo( 72 ),
                 $this->equalTo( 310.24922990759, .1 ),
                 $this->equalTo( 360., .1 ),
                 $this->equalTo( 0 )
@@ -176,9 +176,9 @@
             ->expects( $this->at( 5 ) )
             ->method( 'drawTextBox' )
             ->with(
-                $this->equalTo( new ezcGraphCoordinate( 80, 0 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 80, 11 ) ),
                 $this->equalTo( 'Opera: 1204 (16.9%)' ),
-                $this->equalTo( 95 ),
+                $this->equalTo( 118 ),
                 $this->equalTo( 30 ),
                 $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
             );
@@ -186,9 +186,9 @@
             ->expects( $this->at( 6 ) )
             ->method( 'drawTextBox' )
             ->with(
-                $this->equalTo( new ezcGraphCoordinate( 80, 30 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 80, 41 ) ),
                 $this->equalTo( 'IE: 345 (4.8%)' ),
-                $this->equalTo( 64 ),
+                $this->equalTo( 81 ),
                 $this->equalTo( 30 ),
                 $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
             );
@@ -196,9 +196,9 @@
             ->expects( $this->at( 7 ) )
             ->method( 'drawTextBox' )
             ->with(
-                $this->equalTo( new ezcGraphCoordinate( 80, 134 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 80, 128 ) ),
                 $this->equalTo( 'Mozilla: 4375 (61.3%)' ),
-                $this->equalTo( 61 ),
+                $this->equalTo( 81 ),
                 $this->equalTo( 30 ),
                 $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
             );
@@ -206,9 +206,9 @@
             ->expects( $this->at( 8 ) )
             ->method( 'drawTextBox' )
             ->with(
-                $this->equalTo( new ezcGraphCoordinate( 321, 13 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 304, 24 ) ),
                 $this->equalTo( 'wget: 231 (3.2%)' ),
-                $this->equalTo( 79 ),
+                $this->equalTo( 96 ),
                 $this->equalTo( 30 ),
                 $this->equalTo( ezcGraph::LEFT | ezcGraph::MIDDLE )
             );
@@ -216,9 +216,9 @@
             ->expects( $this->at( 9 ) )
             ->method( 'drawTextBox' )
             ->with(
-                $this->equalTo( new ezcGraphCoordinate( 347, 54 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 329, 62 ) ),
                 $this->equalTo( 'Safari: 987 (13.8%)' ),
-                $this->equalTo( 53 ),
+                $this->equalTo( 71 ),
                 $this->equalTo( 30 ),
                 $this->equalTo( ezcGraph::LEFT | ezcGraph::MIDDLE )
             );
@@ -227,6 +227,77 @@
         $chart->render( 400, 200 );
     }
 
+    public function testPieRenderPieLablesWithoutSymbols()
+    {
+        $chart = ezcGraph::create( 'Pie' );
+        $chart->sample = array(
+            'Mozilla' => 4375,
+            'IE' => 345,
+            'Opera' => 1204,
+            'wget' => 231,
+            'Safari' => 987,
+        );
+        $chart->options->showSymbol = false;
+
+        $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+            'drawTextBox',
+        ) );
+
+        $mockedRenderer
+            ->expects( $this->at( 5 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 80, 11 ) ),
+                $this->equalTo( 'Opera: 1204 (16.9%)' ),
+                $this->equalTo( 124 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 6 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 80, 41 ) ),
+                $this->equalTo( 'IE: 345 (4.8%)' ),
+                $this->equalTo( 87 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 7 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 80, 128 ) ),
+                $this->equalTo( 'Mozilla: 4375 (61.3%)' ),
+                $this->equalTo( 87 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::RIGHT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 8 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 298, 24 ) ),
+                $this->equalTo( 'wget: 231 (3.2%)' ),
+                $this->equalTo( 102 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::LEFT | ezcGraph::MIDDLE )
+            );
+        $mockedRenderer
+            ->expects( $this->at( 9 ) )
+            ->method( 'drawTextBox' )
+            ->with(
+                $this->equalTo( new ezcGraphCoordinate( 323, 62 ) ),
+                $this->equalTo( 'Safari: 987 (13.8%)' ),
+                $this->equalTo( 77 ),
+                $this->equalTo( 30 ),
+                $this->equalTo( ezcGraph::LEFT | ezcGraph::MIDDLE )
+            );
+
+        $chart->renderer = $mockedRenderer;
+        $chart->render( 400, 200 );
+    }
+
     public function testPieRenderPieLableIdentifiers()
     {
         $chart = ezcGraph::create( 'Pie' );
@@ -248,8 +319,8 @@
             ->method( 'drawLine' )
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#2E3436' ) ),
-                $this->equalTo( new ezcGraphCoordinate( 238, 33 ) ),
-                $this->equalTo( new ezcGraphCoordinate( 181, 15 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 238, 47 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 204, 26 ) ),
                 $this->equalTo( false )
             );
         $mockedRenderer
@@ -257,7 +328,7 @@
             ->method( 'drawSymbol' )
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#2E3436' ) ),
-                $this->equalTo( new ezcGraphCoordinate( 235, 30 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 235, 44 ) ),
                 $this->equalTo( 6 ),
                 $this->equalTo( 6 ),
                 $this->equalTo( ezcGraph::BULLET )
@@ -267,7 +338,7 @@
             ->method( 'drawSymbol' )
             ->with(
                 $this->equalTo( ezcGraphColor::fromHex( '#2E3436' ) ),
-                $this->equalTo( new ezcGraphCoordinate( 178, 12 ) ),
+                $this->equalTo( new ezcGraphCoordinate( 201, 23 ) ),
                 $this->equalTo( 6 ),
                 $this->equalTo( 6 ),
                 $this->equalTo( ezcGraph::BULLET )
@@ -301,7 +372,7 @@
         );
 
         $this->assertEquals(
-            'c12df5fe4632d7a4b6bd3406bf395514',
+            'dfc7af85be7a48e452c55ac1e1a84abe',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
@@ -363,10 +434,42 @@
         );
 
         $this->assertEquals(
-            '016c82f3a51c8b35c36323360ab4f40b',
+            'ea963eb0fa1549ac2f62a478e2a6225d',
             md5_file( $filename ),
             'Incorrect image rendered.'
         );
     }
+
+    public function testCompleteRenderingWithHilightWithoutSymbols()
+    {
+        $filename = $this->tempDir . __FUNCTION__ . '.png';
+
+        $chart = ezcGraph::create( 'Pie' );
+        $chart->options->showSymbol = false;
+
+        $chart->sample = array(
+            'Mozilla' => 4375,
+            'IE' => 345,
+            'Opera' => 1204,
+            'wget' => 231,
+            'Safari' => 987,
+        );
+        $chart->sample->highlight['Safari'] = true;
+
+        $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(
+            '6ced9d94eb716fb5dfe8c5b1b945d42c',
+            md5_file( $filename ),
+            'Incorrect image rendered.'
+        );
+    }
 }
 ?>

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

Reply via email to