diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-05-23 16:18:27 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-05-23 16:18:27 +0000 |
commit | a5476184b65f07f983d9f676d74550bcfe1925d6 (patch) | |
tree | 26986aca1e727fee06a1fe375ee4312669fc6f22 | |
parent | fd76465a079bf1a8b4b85e6f80431fcae81919f9 (diff) | |
download | zetacomponents-graph-a5476184b65f07f983d9f676d74550bcfe1925d6.zip zetacomponents-graph-a5476184b65f07f983d9f676d74550bcfe1925d6.tar.gz |
- Implemented tests for line chart legend rendering
- Implemented legend rendering
-rw-r--r-- | src/charts/line.php | 2 | ||||
-rw-r--r-- | src/element/labeled_axis.php | 2 | ||||
-rw-r--r-- | src/element/legend.php | 161 | ||||
-rw-r--r-- | src/element/numeric_axis.php | 2 | ||||
-rw-r--r-- | src/element/text.php | 2 | ||||
-rw-r--r-- | src/graph.php | 2 | ||||
-rw-r--r-- | src/graph_autoload.php | 2 | ||||
-rw-r--r-- | src/interfaces/chart.php | 8 | ||||
-rw-r--r-- | src/interfaces/element.php | 128 | ||||
-rw-r--r-- | src/interfaces/renderer.php | 16 | ||||
-rw-r--r-- | src/renderer/2d.php | 20 | ||||
-rw-r--r-- | src/structs/coordinate.php | 4 | ||||
-rw-r--r-- | tests/labeled_axis_test.php | 5 | ||||
-rw-r--r-- | tests/line_test.php | 339 | ||||
-rw-r--r-- | tests/numeric_axis_test.php | 7 |
15 files changed, 656 insertions, 44 deletions
diff --git a/src/charts/line.php b/src/charts/line.php index f639439..6f664b6 100644 --- a/src/charts/line.php +++ b/src/charts/line.php @@ -50,7 +50,7 @@ class ezcGraphLineChart extends ezcGraphChart foreach ( $this->elements as $element ) { - $boundings = $element->render( $boundings ); + $boundings = $element->render( $this->renderer, $boundings ); } // Render graph diff --git a/src/element/labeled_axis.php b/src/element/labeled_axis.php index 41fc75b..b0ce7ba 100644 --- a/src/element/labeled_axis.php +++ b/src/element/labeled_axis.php @@ -86,7 +86,7 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElement * @access public * @return void */ - public function render( ezcGraphBoundings $boundings ) + public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings ) { return $boundings; } diff --git a/src/element/legend.php b/src/element/legend.php index d88409f..3736848 100644 --- a/src/element/legend.php +++ b/src/element/legend.php @@ -21,6 +21,7 @@ class ezcGraphChartElementLegend extends ezcGraphChartElement * array( * 'label' => (string) 'Label of data element', * 'color' => (ezcGraphColor) $color, + * 'symbol' => (integer) ezcGraph::DIAMOND, * ), * ... * ) @@ -30,6 +31,44 @@ class ezcGraphChartElementLegend extends ezcGraphChartElement protected $labels; /** + * Size of a portrait style legend in percent of the size of the complete + * chart + * + * @var float + */ + protected $portraitSize = .2; + + /** + * Size of a landscape style legend in percent of the size of the complete + * chart + * + * @var float + */ + protected $landscapeSize = .1; + + /** + * Standard size of symbols and text in legends + * + * @var integer + */ + protected $symbolSize = 14; + + /** + * Padding for label elements + * + * @var integer + */ + protected $padding = 1; + + /** + * Scale symbol size up to to percent of complete legends size for very + * big legends + * + * @var float + */ + protected $minimumSymbolSize = .05; + + /** * Generate legend from several datasets with on entry per dataset * * @param array $datasets @@ -43,6 +82,9 @@ class ezcGraphChartElementLegend extends ezcGraphChartElement $this->labels[] = array( 'label' => $dataset->label->default, 'color' => $dataset->color->default, + 'symbol' => ( $dataset->symbol->default === null ? + ezcGraph::NO_SYMBOL : + $dataset->symbol->default ), ); } } @@ -61,10 +103,117 @@ class ezcGraphChartElementLegend extends ezcGraphChartElement $this->labels[] = array( 'label' => $label, 'color' => $dataset->color[$label], + 'symbol' => ( $dataset->symbol[$label] === null ? + ezcGraph::NO_SYMBOL : + $dataset->symbol[$label] ), ); } } + protected function calculateBoundings( ezcGraphBoundings $boundings ) + { + switch ( $this->position ) + { + case ezcGraph::TOP: + $this->boundings = clone $boundings; + + $this->boundings->y1 = $boundings->y0 + ($boundings->y1 - $boundings->y0) * $this->landscapeSize; + $boundings->y0 = $boundings->y0 + ($boundings->y1 - $boundings->y0) * $this->landscapeSize; + break; + case ezcGraph::LEFT: + $this->boundings = clone $boundings; + + $this->boundings->x1 = $boundings->x0 + ($boundings->x1 - $boundings->x0) * $this->portraitSize; + $boundings->x0 = $boundings->x0 + ($boundings->x1 - $boundings->x0) * $this->portraitSize; + break; + case ezcGraph::RIGHT: + $this->boundings = clone $boundings; + + $this->boundings->x0 = $boundings->x1 - ($boundings->x1 - $boundings->x0) * $this->portraitSize; + $boundings->x1 = $boundings->x1 - ($boundings->x1 - $boundings->x0) * $this->portraitSize; + break; + case ezcGraph::BOTTOM: + $this->boundings = clone $boundings; + + $this->boundings->y0 = $boundings->y1 - ($boundings->y1 - $boundings->y0) * $this->landscapeSize; + $boundings->y1 = $boundings->y1 - ($boundings->y1 - $boundings->y0) * $this->landscapeSize; + break; + } + + return $boundings; + } + + protected function renderLegend( ezcGraphRenderer $renderer ) + { + switch ( $this->position ) + { + case ezcGraph::LEFT: + case ezcGraph::RIGHT: + $symbolSize = min( + max( + $this->symbolSize, + ( $this->boundings->y1 - $this->boundings->y0 ) * $this->minimumSymbolSize + ), + ( $this->boundings->y1 - $this->boundings->y0 ) / count( $this->labels ) + ); + + foreach ( $this->labels as $labelNr => $label ) + { + $renderer->drawSymbol( + $label['color'], + new ezcGraphCoordinate( + $this->boundings->x0 + $this->padding, + $this->boundings->y0 + $labelNr * $symbolSize + $this->padding + ), + $symbolSize - 2 * $this->padding, + $symbolSize - 2 * $this->padding, + $label['symbol'] + ); + $renderer->drawTextBox( + new ezcGraphCoordinate( + $this->boundings->x0 + $symbolSize, + $this->boundings->y0 + $labelNr * $symbolSize + $this->padding + ), + $label['label'], + $this->boundings->x1 - $this->boundings->x0 - $symbolSize - $this->padding, + $symbolSize - 2 * $this->padding + ); + } + break; + case ezcGraph::TOP: + case ezcGraph::BOTTOM: + $symbolSize = min( + $this->symbolSize, + ( $this->boundings->y1 - $this->boundings->y0 ) + ); + $width = ( $this->boundings->x1 - $this->boundings->x0 ) / count( $this->labels ); + + foreach ( $this->labels as $labelNr => $label ) + { + $renderer->drawSymbol( + $label['color'], + new ezcGraphCoordinate( + $this->boundings->x0 + $labelNr * $width + $this->padding, + $this->boundings->y0 + $this->padding + ), + $symbolSize - 2 * $this->padding, + $symbolSize - 2 * $this->padding, + $label['symbol'] + ); + $renderer->drawTextBox( + new ezcGraphCoordinate( + $this->boundings->x0 + $labelNr * $width + $this->padding + $symbolSize, + $this->boundings->y0 + $this->padding + ), + $label['label'], + $width - $this->padding - $symbolSize, + $symbolSize - 2 * $this->padding + ); + } + break; + } + } + /** * Render a legend * @@ -72,8 +221,18 @@ class ezcGraphChartElementLegend extends ezcGraphChartElement * @access public * @return void */ - public function render( ezcGraphBoundings $boundings ) + public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings ) { + $boundings = $this->calculateBoundings( $boundings ); + + // Render standard elements + $this->renderBorder( $renderer ); + $this->renderBackground( $renderer ); + $this->renderTitle( $renderer ); + + // Render legend + $this->renderLegend( $renderer ); + return $boundings; } } diff --git a/src/element/numeric_axis.php b/src/element/numeric_axis.php index d84ac4d..2685c55 100644 --- a/src/element/numeric_axis.php +++ b/src/element/numeric_axis.php @@ -252,7 +252,7 @@ class ezcGraphChartElementNumericAxis extends ezcGraphChartElement * @access public * @return void */ - public function render( ezcGraphBoundings $boundings ) + public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings ) { return $boundings; } diff --git a/src/element/text.php b/src/element/text.php index 07cb229..fc2e930 100644 --- a/src/element/text.php +++ b/src/element/text.php @@ -21,7 +21,7 @@ class ezcGraphChartElementText extends ezcGraphChartElement * @access public * @return void */ - public function render( ezcGraphBoundings $boundings ) + public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings ) { return $boundings; } diff --git a/src/graph.php b/src/graph.php index 6f64078..0037e68 100644 --- a/src/graph.php +++ b/src/graph.php @@ -15,12 +15,14 @@ class ezcGraph { + const NO_SYMBOL = 0; const DIAMOND = 1; const TOP = 1; const BOTTOM = 2; const LEFT = 3; const RIGHT = 4; + const CENTER = 5; static protected $chartTypes = array( 'pie' => 'ezcGraphPieChart', diff --git a/src/graph_autoload.php b/src/graph_autoload.php index bcbcb98..b1055dc 100644 --- a/src/graph_autoload.php +++ b/src/graph_autoload.php @@ -50,7 +50,7 @@ return array( 'ezcGraphUnknownDatasetSourceException' => 'Graph/exceptions/unknown_dataset_source.php', 'ezcGraphBoundings' => 'Graph/structs/boundings.php', - 'ezcGraphCoordiinate' => 'Graph/structs/coordinate.php', + 'ezcGraphCoordinate' => 'Graph/structs/coordinate.php', ); ?> diff --git a/src/interfaces/chart.php b/src/interfaces/chart.php index b40c733..7cf1e1e 100644 --- a/src/interfaces/chart.php +++ b/src/interfaces/chart.php @@ -83,7 +83,9 @@ abstract class ezcGraphChart case 'renderer': if ( $propertyValue instanceof ezcGraphRenderer ) { - return $this->renderer = $propertyValue; + $this->renderer = $propertyValue; + $this->renderer->setDriver( $this->driver ); + return $this->renderer; } else { @@ -93,7 +95,9 @@ abstract class ezcGraphChart case 'driver': if ( $propertyValue instanceof ezcGraphDriver ) { - return $this->driver = $propertyValue; + $this->driver = $propertyValue; + $this->renderer->setDriver( $this->driver ); + return $this->driver; } else { diff --git a/src/interfaces/element.php b/src/interfaces/element.php index 73b0f52..8448739 100644 --- a/src/interfaces/element.php +++ b/src/interfaces/element.php @@ -30,6 +30,13 @@ abstract class ezcGraphChartElement extends ezcBaseOptions protected $background; /** + * Boundings of this elements + * + * @var ezcGraphBoundings + */ + protected $boundings; + + /** * Border color of chart element * * @var ezcGraphColor @@ -51,6 +58,37 @@ abstract class ezcGraphChartElement extends ezcBaseOptions protected $position; /** + * Maximum size of the title + * + * @var integer + */ + protected $maxTitleHeight = 16; + + /** + * Percentage of boundings which are used for the title with position + * left, right or center + * + * @var float + */ + protected $portraitTitleSize = .15; + + /** + * Percentage of boundings which are used for the title with position + * top otr bottom + * + * @var float + */ + protected $landscapeTitleSize = .2; + + public function __construct( array $options = array() ) + { + $this->boundings = new ezcGraphBoundings(); + $this->position = ezcGraph::LEFT; + + parent::__construct( $options ); + } + + /** * __set * * @param mixed $propertyName @@ -109,7 +147,95 @@ abstract class ezcGraphChartElement extends ezcBaseOptions * @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( ezcGraphBoundings $boundings ); + abstract public function render( ezcGraphRenderer $renderer, ezcGraphBoundings $boundings ); + + protected function renderBorder( ezcGraphRenderer $renderer ) + { + if ( $this->border instanceof ezcGraphColor ) + { + // Default bordervalue to 1 + $this->borderWidth = max( 1, $this->borderWidth ); + + // Draw border + $renderer->drawRect( + $this->border, + new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ), + $this->boundings->x1 - $this->boundings->x0, + $this->boundings->y1 - $this->boundings->y0, + $this->borderWidth + ); + + // Reduce local boundings by borderWidth + $this->boundings->x0 += $this->borderWidth; + $this->boundings->y0 += $this->borderWidth; + $this->boundings->x1 -= $this->borderWidth; + $this->boundings->y1 -= $this->borderWidth; + } + } + + protected function renderBackground( ezcGraphRenderer $renderer ) + { + if ( $this->background instanceof ezcGraphColor ) + { + $renderer->drawBackground( + $this->background, + new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ), + $this->boundings->x1 - $this->boundings->x0, + $this->boundings->y1 - $this->boundings->y0 + ); + } + } + + protected function renderTitle( ezcGraphRenderer $renderer ) + { + if ( !empty( $this->title ) ) + { + switch ( $this->position ) + { + case ezcGraph::LEFT: + case ezcGraph::RIGHT: + case ezcGraph::CENTER: + $height = min( + $this->maxTitleHeight, + ( $this->boundings->y1 - $this->boundings->y0 ) * $this->portraitTitleSize + ); + $renderer->drawTextBox( + new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ), + $this->title, + $this->boundings->x1 - $this->boundings->x0, + $height + ); + $this->boundings->y0 += $height; + break; + case ezcGraph::TOP: + $height = min( + $this->maxTitleHeight, + ( $this->boundings->y1 - $this->boundings->y0 ) * $this->landscapeTitleSize + ); + $renderer->drawTextBox( + new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y0 ), + $this->title, + $this->boundings->x1 - $this->boundings->x0, + $height + ); + $this->boundings->y0 += $height; + break; + case ezcGraph::BOTTOM: + $height = min( + $this->maxTitleHeight, + ( $this->boundings->y1 - $this->boundings->y0 ) * $this->landscapeTitleSize + ); + $renderer->drawTextBox( + new ezcGraphCoordinate( $this->boundings->x0, $this->boundings->y1 - $height ), + $this->title, + $this->boundings->x1 - $this->boundings->x0, + $height + ); + $this->boundings->y1 -= $height; + break; + } + } + } } ?> diff --git a/src/interfaces/renderer.php b/src/interfaces/renderer.php index b14ecde..f49f92a 100644 --- a/src/interfaces/renderer.php +++ b/src/interfaces/renderer.php @@ -15,6 +15,14 @@ */ abstract class ezcGraphRenderer { + + protected $driver; + + public function setDriver( ezcGraphDriver $driver ) + { + $this->driver = $driver; + } + /** * Draw a pie segment * @@ -25,7 +33,7 @@ abstract class ezcGraphRenderer * @param float $moveOut * @return void */ - abstract public function drawPieSegment( ezcGraphCoordinate $position, $radius, $startAngle = .0, $endAngle = 360., $moveOut = .0 ); + abstract public function drawPieSegment( ezcGraphColor $color, ezcGraphCoordinate $position, $radius, $startAngle = .0, $endAngle = 360., $moveOut = .0 ); /** * Draw a line @@ -38,7 +46,7 @@ abstract class ezcGraphRenderer * @param mixed $filled * @return void */ - abstract public function drawLine( ezcGraphCoordinate $position, ezcGraphCoordinate $end, $filled = true ); + abstract public function drawLine( ezcGraphColor $color, ezcGraphCoordinate $position, ezcGraphCoordinate $end, $filled = true ); /** * Draws a text box @@ -49,7 +57,7 @@ abstract class ezcGraphRenderer * @param mixed $height * @return void */ - abstract public function drawTextBox( ezcGraphCoordinate $position, $text, $width = null, $height = null ); + abstract public function drawTextBox( ezcGraphCoordinate $position, $text, $width = null, $height = null, $align = ezcGraph::LEFT ); /** * Draws a rectangle @@ -96,5 +104,5 @@ abstract class ezcGraphRenderer * @param int $symbol * @return void */ - abstract public function drawSymbol( ezcGraphCoordinate $position, $width, $height, $symbol = ezcGraph::NO_SYMBOL); + abstract public function drawSymbol( ezcGraphColor $color, ezcGraphCoordinate $position, $width, $height, $symbol = ezcGraph::NO_SYMBOL); } diff --git a/src/renderer/2d.php b/src/renderer/2d.php index 994a0ed..2711374 100644 --- a/src/renderer/2d.php +++ b/src/renderer/2d.php @@ -12,8 +12,9 @@ * * @package Graph */ -class ezcGraphRenderer2D extends ezcGraphRenderer { - +class ezcGraphRenderer2D extends ezcGraphRenderer +{ + /** * Draw a pie segment * @@ -22,10 +23,9 @@ class ezcGraphRenderer2D extends ezcGraphRenderer { * @param float $startAngle * @param float $endAngle * @param float $moveOut - * @access public * @return void */ - public function drawPieSegment( ezcGraphCoordinate $position, $radius, $startAngle = .0, $endAngle = 360., $moveOut = .0 ) + public function drawPieSegment( ezcGraphColor $color, ezcGraphCoordinate $position, $radius, $startAngle = .0, $endAngle = 360., $moveOut = .0 ) { } @@ -39,10 +39,9 @@ class ezcGraphRenderer2D extends ezcGraphRenderer { * @param ezcGraphCoordinate $position * @param ezcGraphCoordinate $end * @param mixed $filled - * @access public * @return void */ - public function drawLine( ezcGraphCoordinate $position, ezcGraphCoordinate $end, $filled = true ) + public function drawLine( ezcGraphColor $color, ezcGraphCoordinate $position, ezcGraphCoordinate $end, $filled = true ) { } @@ -54,10 +53,9 @@ class ezcGraphRenderer2D extends ezcGraphRenderer { * @param mixed $text * @param mixed $width * @param mixed $height - * @access public * @return void */ - public function drawTextBox( ezcGraphCoordinate $position, $text, $width = null, $height = null ) + public function drawTextBox( ezcGraphCoordinate $position, $text, $width = null, $height = null, $align = ezcGraph::LEFT ) { } @@ -70,7 +68,6 @@ class ezcGraphRenderer2D extends ezcGraphRenderer { * @param mixed $width * @param mixed $height * @param float $borderWidth - * @access public * @return void */ public function drawRect( ezcGraphColor $color, ezcGraphCoordinate $position = null, $width = null, $height = null, $borderWidth = 1 ) @@ -87,7 +84,6 @@ class ezcGraphRenderer2D extends ezcGraphRenderer { * @param ezcGraphCoordinate $position * @param mixed $width * @param mixed $height - * @access public * @return void */ public function drawBackground( ezcGraphColor $color, ezcGraphCoordinate $position = null, $width = null, $height = null ) @@ -102,7 +98,6 @@ class ezcGraphRenderer2D extends ezcGraphRenderer { * @param ezcGraphCoordinate $position * @param mixed $width * @param mixed $height - * @access public * @return void */ public function drawBackgroundImage( $file, ezcGraphCoordinate $position = null, $width = null, $height = null ) @@ -117,10 +112,9 @@ class ezcGraphRenderer2D extends ezcGraphRenderer { * @param float $width * @param float $height * @param int $symbol - * @access public * @return void */ - public function drawSymbol( ezcGraphCoordinate $position, $width, $height, $symbol = ezcGraph::NO_SYMBOL) + public function drawSymbol( ezcGraphColor $color, ezcGraphCoordinate $position, $width, $height, $symbol = ezcGraph::NO_SYMBOL) { } diff --git a/src/structs/coordinate.php b/src/structs/coordinate.php index 10ec1c3..d7093d4 100644 --- a/src/structs/coordinate.php +++ b/src/structs/coordinate.php @@ -9,8 +9,10 @@ class ezcGraphCoordinate /** * Empty constructor */ - public function __construct() + public function __construct( $x, $y ) { + $this->x = $x; + $this->y = $y; } /** diff --git a/tests/labeled_axis_test.php b/tests/labeled_axis_test.php index 2f9c9c2..d7c757c 100644 --- a/tests/labeled_axis_test.php +++ b/tests/labeled_axis_test.php @@ -56,6 +56,7 @@ class ezcGraphLabeledAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 20, 70, 12, 130 ); + $chart->sample->color = '#FF0000'; $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -80,7 +81,9 @@ class ezcGraphLabeledAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); + $chart->sample->color = '#FF0000'; $chart->sample2 = array( 2002 => 1270, 1170, 1610, 1370 ); + $chart->sample2->color = '#00FF00'; $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -107,7 +110,9 @@ class ezcGraphLabeledAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 2001 => 1300, 2004 => 1012, 2006 => 1450 ); + $chart->sample->color = '#FF0000'; $chart->sample2 = array( 2001 => 1270, 1170, 1610, 1370, 1559 ); + $chart->sample2->color = '#00FF00'; $chart->render( 500, 200 ); } catch ( Exception $e ) diff --git a/tests/line_test.php b/tests/line_test.php index 288247c..481f687 100644 --- a/tests/line_test.php +++ b/tests/line_test.php @@ -41,18 +41,24 @@ class ezcGraphLineChartTest extends ezcTestCase { } + protected function addSampleData( ezcGraphChart $chart ) + { + $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1); + $chart->sampleData->color = '#0000FF'; + $chart->sampleData->symbol = ezcGraph::DIAMOND; + $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'; + } + public function testElementGenerationLegend() { 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'; + $this->addSampleData( $chart ); $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -93,32 +99,331 @@ class ezcGraphLineChartTest extends ezcTestCase ); } - public function testRenderLegend() + public function testRenderLegendBackground() { 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'; + $this->addSampleData( $chart ); + $chart->legend->background = '#0000FF'; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawBackground', + 'drawTextBox', + 'drawSymbol', + ) ); + $mockedRenderer + ->expects( $this->once() ) + ->method( 'drawBackground' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#0000FF' ) ), + $this->equalTo( new ezcGraphCoordinate( 0, 0 ) ), + $this->equalTo( 100 ), + $this->equalTo( 200 ) + ); + $chart->renderer = $mockedRenderer; + $chart->render( 500, 200 ); } catch ( Exception $e ) { $this->fail( $e->getMessage() ); } - + } + + public function testRenderLegendSymbols() + { + try + { + $chart = ezcGraph::create( 'Line' ); + $this->addSampleData( $chart ); + $chart->legend->background = '#0000FF'; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawBackground', + 'drawTextBox', + 'drawSymbol', + ) ); + + $mockedRenderer + ->expects( $this->at( 1 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#0000FF' ) ), + $this->equalTo( new ezcGraphCoordinate( 1, 1 ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::DIAMOND + ); + $mockedRenderer + ->expects( $this->at( 3 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ), + $this->equalTo( new ezcGraphCoordinate( 1, 15 ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::NO_SYMBOL + ); + $mockedRenderer + ->expects( $this->at( 5 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ), + $this->equalTo( new ezcGraphCoordinate( 1, 29 ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::NO_SYMBOL + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } + } + + public function testRenderLegendText() + { + try + { + $chart = ezcGraph::create( 'Line' ); + $this->addSampleData( $chart ); + $chart->legend->background = '#0000FF'; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawBackground', + 'drawTextBox', + 'drawSymbol', + ) ); + $mockedRenderer + ->expects( $this->at( 2 ) ) + ->method( 'drawTextBox' ) + ->with( + $this->equalTo( new ezcGraphCoordinate( 14, 1 ) ), + 'sampleData', + $this->equalTo( 85 ), + $this->equalTo( 12 ) + ); + $mockedRenderer + ->expects( $this->at( 4 ) ) + ->method( 'drawTextBox' ) + ->with( + $this->equalTo( new ezcGraphCoordinate( 14, 15 ) ), + 'moreData', + $this->equalTo( 85 ), + $this->equalTo( 12 ) + ); + $mockedRenderer + ->expects( $this->at( 6 ) ) + ->method( 'drawTextBox' ) + ->with( + $this->equalTo( new ezcGraphCoordinate( 14, 29 ) ), + 'Even more data', + $this->equalTo( 85 ), + $this->equalTo( 12 ) + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } + } + + public function testRenderLegendBackgroundRight() + { + try + { + $chart = ezcGraph::create( 'Line' ); + $this->addSampleData( $chart ); + $chart->legend->background = '#0000FF'; + $chart->legend->position = ezcGraph::RIGHT; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawBackground', + 'drawTextBox', + 'drawSymbol', + ) ); + $mockedRenderer + ->expects( $this->once() ) + ->method( 'drawBackground' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#0000FF' ) ), + $this->equalTo( new ezcGraphCoordinate( 400., 0 ) ), + $this->equalTo( 100 ), + $this->equalTo( 200 ) + ); + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } + } + + public function testRenderLegendSymbolsRight() + { + try + { + $chart = ezcGraph::create( 'Line' ); + $this->addSampleData( $chart ); + $chart->legend->background = '#0000FF'; + $chart->legend->position = ezcGraph::RIGHT; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawBackground', + 'drawTextBox', + 'drawSymbol', + ) ); + + $mockedRenderer + ->expects( $this->at( 1 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#0000FF' ) ), + $this->equalTo( new ezcGraphCoordinate( 401., 1 ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::DIAMOND + ); + $mockedRenderer + ->expects( $this->at( 3 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ), + $this->equalTo( new ezcGraphCoordinate( 401., 15 ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::NO_SYMBOL + ); + $mockedRenderer + ->expects( $this->at( 5 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ), + $this->equalTo( new ezcGraphCoordinate( 401., 29 ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::NO_SYMBOL + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } + } + + + public function testRenderLegendBackgroundBottom() + { + try + { + $chart = ezcGraph::create( 'Line' ); + $this->addSampleData( $chart ); + $chart->legend->background = '#0000FF'; + $chart->legend->position = ezcGraph::BOTTOM; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawBackground', + 'drawTextBox', + 'drawSymbol', + ) ); + $mockedRenderer + ->expects( $this->once() ) + ->method( 'drawBackground' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#0000FF' ) ), + $this->equalTo( new ezcGraphCoordinate( 0, 180. ) ), + $this->equalTo( 500 ), + $this->equalTo( 20 ) + ); + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } + } + + public function testRenderLegendSymbolsBottom() + { + try + { + $chart = ezcGraph::create( 'Line' ); + $this->addSampleData( $chart ); + $chart->legend->background = '#0000FF'; + $chart->legend->position = ezcGraph::BOTTOM; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawBackground', + 'drawTextBox', + 'drawSymbol', + ) ); + + $mockedRenderer + ->expects( $this->at( 1 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#0000FF' ) ), + $this->equalTo( new ezcGraphCoordinate( 1, 181. ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::DIAMOND + ); + $mockedRenderer + ->expects( $this->at( 3 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ), + $this->equalTo( new ezcGraphCoordinate( 101, 181. ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::NO_SYMBOL + ); + $mockedRenderer + ->expects( $this->at( 5 ) ) + ->method( 'drawSymbol' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ), + $this->equalTo( new ezcGraphCoordinate( 201, 181. ) ), + $this->equalTo( 12 ), + $this->equalTo( 12 ), + ezcGraph::NO_SYMBOL + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 300, 200 ); + } + catch ( Exception $e ) + { + $this->fail( $e->getMessage() ); + } } public function testRender() { throw new PHPUnit2_Framework_IncompleteTestError( - '@TODO: Implement renderer tests.' + '@TODO: Implement renderer tests for custom padding size.' + ); + throw new PHPUnit2_Framework_IncompleteTestError( + '@TODO: Implement renderer tests checking minum symbol size' ); } } diff --git a/tests/numeric_axis_test.php b/tests/numeric_axis_test.php index 281f838..9880451 100644 --- a/tests/numeric_axis_test.php +++ b/tests/numeric_axis_test.php @@ -132,6 +132,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 20, 70, 12, 130 ); + $chart->sample->color = '#FF0000'; $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -170,6 +171,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1, 4.3, .2, 3.82 ); + $chart->sample->color = '#FF0000'; $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -208,6 +210,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => -1.8, 4.3, .2, 3.82 ); + $chart->sample->color = '#FF0000'; $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -246,6 +249,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); + $chart->sample->color = '#FF0000'; $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -284,7 +288,9 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); + $chart->sample->color = '#FF0000'; $chart->sample2 = array( 2000 => 1270, 1170, 1610, 1370 ); + $chart->sample2->color = '#00FF00'; $chart->render( 500, 200 ); } catch ( Exception $e ) @@ -323,6 +329,7 @@ class ezcGraphNumericAxisTest extends ezcTestCase { $chart = ezcGraph::create( 'Line' ); $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); + $chart->sample->color = '#FF0000'; $chart->Y_axis->majorStep = 50; $chart->render( 500, 200 ); } |