diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-05-19 15:26:29 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-05-19 15:26:29 +0000 |
commit | 8231d5da5ceadd3f04cc52d526a838da09e034c2 (patch) | |
tree | 3fd0318d204889da496aa7265192318786ea0c70 | |
parent | 559f33a133a5b7d3f19afcd618fbc2e8e9ec3ade (diff) | |
download | zetacomponents-graph-8231d5da5ceadd3f04cc52d526a838da09e034c2.zip zetacomponents-graph-8231d5da5ceadd3f04cc52d526a838da09e034c2.tar.gz |
- Refactored rendering methods
- Refactored chart title to be a chartelement
-rw-r--r-- | design/design.txt | 2 | ||||
-rw-r--r-- | src/charts/line.php | 18 | ||||
-rw-r--r-- | src/charts/pie.php | 2 | ||||
-rw-r--r-- | src/element/labeled_axis.php | 4 | ||||
-rw-r--r-- | src/element/legend.php | 4 | ||||
-rw-r--r-- | src/element/numeric_axis.php | 4 | ||||
-rw-r--r-- | src/element/text.php | 30 | ||||
-rw-r--r-- | src/graph_autoload.php | 4 | ||||
-rw-r--r-- | src/interfaces/chart.php | 19 | ||||
-rw-r--r-- | src/interfaces/element.php | 13 | ||||
-rw-r--r-- | src/structs/boundings.php | 37 | ||||
-rw-r--r-- | src/structs/coordinate.php | 2 | ||||
-rw-r--r-- | tests/chart_test.php | 6 | ||||
-rw-r--r-- | tests/labeled_axis_test.php | 6 | ||||
-rw-r--r-- | tests/line_test.php | 24 | ||||
-rw-r--r-- | tests/numeric_axis_test.php | 12 | ||||
-rw-r--r-- | tests/pie_test.php | 2 |
17 files changed, 148 insertions, 41 deletions
diff --git a/design/design.txt b/design/design.txt index 83e5c4e..89ef258 100644 --- a/design/design.txt +++ b/design/design.txt @@ -123,7 +123,7 @@ The following example shows how to use the class: :: $line->renderer = new ezcGraphRenderer2D(); $line->driver = new ezcGraphGDDriver(); - $line->render('file.png'); + $line->render( 500, 200, 'file.png' ); ?> diff --git a/src/charts/line.php b/src/charts/line.php index 256c21e..f639439 100644 --- a/src/charts/line.php +++ b/src/charts/line.php @@ -30,7 +30,7 @@ class ezcGraphLineChart extends ezcGraphChart * @access public * @return void */ - public function render() + public function render( $width, $height, $file = null ) { // Calculate axis scaling and labeling $this->elements['X_axis']->calculateFromDataset( $this->data ); @@ -38,6 +38,22 @@ class ezcGraphLineChart extends ezcGraphChart // Generate legend $this->elements['legend']->generateFromDatasets( $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; + + foreach ( $this->elements as $element ) + { + $boundings = $element->render( $boundings ); + } + + // Render graph } } ?> diff --git a/src/charts/pie.php b/src/charts/pie.php index a803104..49044b0 100644 --- a/src/charts/pie.php +++ b/src/charts/pie.php @@ -43,7 +43,7 @@ class ezcGraphPieChart extends ezcGraphChart * @access public * @return void */ - public function render() + public function render( $width, $height, $file = null ) { // Generate legend $this->elements['legend']->generateFromDataset( reset( $this->data ) ); diff --git a/src/element/labeled_axis.php b/src/element/labeled_axis.php index 3dec5eb..41fc75b 100644 --- a/src/element/labeled_axis.php +++ b/src/element/labeled_axis.php @@ -86,9 +86,9 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElement * @access public * @return void */ - public function render() + public function render( ezcGraphBoundings $boundings ) { - + return $boundings; } } diff --git a/src/element/legend.php b/src/element/legend.php index 60a8a33..d88409f 100644 --- a/src/element/legend.php +++ b/src/element/legend.php @@ -72,9 +72,9 @@ class ezcGraphChartElementLegend extends ezcGraphChartElement * @access public * @return void */ - public function render() + public function render( ezcGraphBoundings $boundings ) { - + return $boundings; } } diff --git a/src/element/numeric_axis.php b/src/element/numeric_axis.php index 647a72b..d84ac4d 100644 --- a/src/element/numeric_axis.php +++ b/src/element/numeric_axis.php @@ -252,9 +252,9 @@ class ezcGraphChartElementNumericAxis extends ezcGraphChartElement * @access public * @return void */ - public function render() + public function render( ezcGraphBoundings $boundings ) { - + return $boundings; } } diff --git a/src/element/text.php b/src/element/text.php new file mode 100644 index 0000000..07cb229 --- /dev/null +++ b/src/element/text.php @@ -0,0 +1,30 @@ +<?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 ezcGraphChartElementText extends ezcGraphChartElement +{ + /** + * Render a legend + * + * @param ezcGraphRenderer $renderer + * @access public + * @return void + */ + public function render( ezcGraphBoundings $boundings ) + { + return $boundings; + } +} + +?> diff --git a/src/graph_autoload.php b/src/graph_autoload.php index c7d5bc4..bcbcb98 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -33,6 +33,7 @@ return array( 'ezcGraphInvalidDriverException' => 'Graph/exceptions/invalid_driver.php', 'ezcGraphChartElement' => 'Graph/interfaces/element.php', + 'ezcGraphChartElementText' => 'Graph/element/text.php', 'ezcGraphChartElementLegend' => 'Graph/element/legend.php', 'ezcGraphChartElementNumericAxis' => 'Graph/element/numeric_axis.php', 'ezcGraphChartElementLabeledAxis' => 'Graph/element/labeled_axis.php', @@ -47,6 +48,9 @@ return array( 'ezcGraphNoSuchDatasetException' => 'Graph/exceptions/no_such_dataset.php', 'ezcGraphTooManyDatasetsExceptions' => 'Graph/exceptions/too_many_datasets.php', 'ezcGraphUnknownDatasetSourceException' => 'Graph/exceptions/unknown_dataset_source.php', + + 'ezcGraphBoundings' => 'Graph/structs/boundings.php', + 'ezcGraphCoordiinate' => 'Graph/structs/coordinate.php', ); ?> diff --git a/src/interfaces/chart.php b/src/interfaces/chart.php index f05485e..b40c733 100644 --- a/src/interfaces/chart.php +++ b/src/interfaces/chart.php @@ -50,19 +50,17 @@ abstract class ezcGraphChart */ protected $driver; - /** - * Title of the chart - * - * @var string - */ - protected $title; - public function __construct( array $options = array() ) { $this->options = new ezcGraphChartOptions( $options ); - // Add standard element legend + // Add standard elements + $this->elements['title'] = new ezcGraphChartElementText(); $this->elements['legend'] = new ezcGraphChartElementLegend(); + + // Define standard renderer and driver + $this->renderer = new ezcGraphRenderer2D(); + $this->driver = new ezcGraphSVGDriver(); } /** @@ -80,7 +78,7 @@ abstract class ezcGraphChart { switch ( $propertyName ) { case 'title': - return $this->title = (string) $propertyValue; + $this->elements['title']->title = $propertyValue; break; case 'renderer': if ( $propertyValue instanceof ezcGraphRenderer ) @@ -201,10 +199,9 @@ abstract class ezcGraphChart * Creates basic visual chart elements from the chart to be processed by * the renderer. * - * @abstract * @return void */ - abstract public function render(); + abstract public function render( $widht, $height, $file = null ); } ?> diff --git a/src/interfaces/element.php b/src/interfaces/element.php index d914b14..73b0f52 100644 --- a/src/interfaces/element.php +++ b/src/interfaces/element.php @@ -20,7 +20,7 @@ abstract class ezcGraphChartElement extends ezcBaseOptions * * @var string */ - protected $title = 'Legend'; + protected $title; /** * Background color of chart element @@ -103,14 +103,13 @@ abstract class ezcGraphChartElement extends ezcBaseOptions /** * Renders this chart element * - * Creates basic visual chart elements from this chart element to be - * processed by the renderer. + * This method receives and returns a part of the canvas where it can be + * rendered on. * - * @param ezcGraphRenderer $renderer - * @access public - * @return void + * @param ezcGraphBoundings $boundings Part of canvase to render element on + * @return ezcGraphBoundings Part of canvas, which is still free to draw on */ - abstract public function render(); + abstract public function render( ezcGraphBoundings $boundings ); } ?> diff --git a/src/structs/boundings.php b/src/structs/boundings.php new file mode 100644 index 0000000..003c72e --- /dev/null +++ b/src/structs/boundings.php @@ -0,0 +1,37 @@ +<?php + +class ezcGraphBoundings +{ + public $x0 = 0; + + public $y0 = 0; + + public $x1 = false; + + public $y1 = false; + + /** + * Empty constructor + */ + public function __construct() + { + } + + /** + * Throws a BasePropertyNotFound exception. + */ + public function __set( $name, $value ) + { + throw new ezcBasePropertyNotFoundException( $name ); + } + + /** + * Throws a BasePropertyNotFound exception. + */ + public function __get( $name ) + { + throw new ezcBasePropertyNotFoundException( $name ); + } +} + +?> diff --git a/src/structs/coordinate.php b/src/structs/coordinate.php index 11193a1..10ec1c3 100644 --- a/src/structs/coordinate.php +++ b/src/structs/coordinate.php @@ -30,6 +30,4 @@ class ezcGraphCoordinate } } -? - ?> diff --git a/tests/chart_test.php b/tests/chart_test.php index b59a739..e19be7e 100644 --- a/tests/chart_test.php +++ b/tests/chart_test.php @@ -63,7 +63,11 @@ class ezcGraphChartTest extends ezcTestCase $this->assertSame( 'Test title', - $this->getNonPublicProperty( $pieChart, 'title' ) + $pieChart->title->title + ); + + $this->assertTrue( + $pieChart->title instanceof ezcGraphChartElementText ); } diff --git a/tests/labeled_axis_test.php b/tests/labeled_axis_test.php index 0f8c274..2f9c9c2 100644 --- a/tests/labeled_axis_test.php +++ b/tests/labeled_axis_test.php @@ -56,7 +56,7 @@ class ezcGraphLabeledAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 20, 70, 12, 130 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -81,7 +81,7 @@ class ezcGraphLabeledAxisTest extends ezcTestCase $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); $chart->sample2 = array( 2002 => 1270, 1170, 1610, 1370 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -108,7 +108,7 @@ class ezcGraphLabeledAxisTest extends ezcTestCase $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 2001 => 1300, 2004 => 1012, 2006 => 1450 ); $chart->sample2 = array( 2001 => 1270, 1170, 1610, 1370, 1559 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { diff --git a/tests/line_test.php b/tests/line_test.php index 27ae65a..288247c 100644 --- a/tests/line_test.php +++ b/tests/line_test.php @@ -53,7 +53,7 @@ class ezcGraphLineChartTest extends ezcTestCase $chart->evenMoreData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1); $chart->evenMoreData->color = '#FF0000'; $chart->evenMoreData->label = 'Even more data'; - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -93,6 +93,28 @@ class ezcGraphLineChartTest extends ezcTestCase ); } + public function testRenderLegend() + { + try + { + $chart = ezcGraph::create( 'Line' ); + $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1); + $chart->sampleData->color = '#0000FF'; + $chart->moreData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1); + $chart->moreData->color = '#FF0000'; + $chart->evenMoreData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1); + $chart->evenMoreData->color = '#FF0000'; + $chart->evenMoreData->label = 'Even more data'; + $chart->render( 500, 200 ); + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } + + + } + public function testRender() { throw new PHPUnit2_Framework_IncompleteTestError( diff --git a/tests/numeric_axis_test.php b/tests/numeric_axis_test.php index ca1046a..281f838 100644 --- a/tests/numeric_axis_test.php +++ b/tests/numeric_axis_test.php @@ -132,7 +132,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 20, 70, 12, 130 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -170,7 +170,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1, 4.3, .2, 3.82 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -208,7 +208,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => -1.8, 4.3, .2, 3.82 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -246,7 +246,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -285,7 +285,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); $chart->sample2 = array( 2000 => 1270, 1170, 1610, 1370 ); - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { @@ -324,7 +324,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); $chart->Y_axis->majorStep = 50; - $chart->render(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { diff --git a/tests/pie_test.php b/tests/pie_test.php index 15a3163..b4c6d22 100644 --- a/tests/pie_test.php +++ b/tests/pie_test.php @@ -49,7 +49,7 @@ class ezcGraphPieChartTest extends ezcTestCase $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(); + $chart->render( 500, 200 ); } catch ( Exception $e ) { |