diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-06-08 16:00:09 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-06-08 16:00:09 +0000 |
commit | f98ef3a072c9469d99d6f78c7b0fc33f74cccda5 (patch) | |
tree | 3bf725e75408adb9794c090ee86f9c9dad508a2a | |
parent | a06f707fb0f0721d42c1c603423def575161aca2 (diff) | |
download | zetacomponents-graph-f98ef3a072c9469d99d6f78c7b0fc33f74cccda5.zip zetacomponents-graph-f98ef3a072c9469d99d6f78c7b0fc33f74cccda5.tar.gz |
- Added grids to line charts (tests & implementation)
-rw-r--r-- | src/element/axis.php | 68 | ||||
-rw-r--r-- | src/interfaces/palette.php | 19 | ||||
-rw-r--r-- | src/palette/black.php | 14 | ||||
-rw-r--r-- | tests/labeled_axis_test.php | 53 | ||||
-rw-r--r-- | tests/line_test.php | 2 | ||||
-rw-r--r-- | tests/numeric_axis_test.php | 90 |
6 files changed, 233 insertions, 13 deletions
diff --git a/src/element/axis.php b/src/element/axis.php index 32d4c7d..a4c3b1e 100644 --- a/src/element/axis.php +++ b/src/element/axis.php @@ -38,18 +38,18 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement protected $labelPadding = 2; /** - * Color of the rastering + * Color of the griding * * @var ezcGraphColor */ - protected $raster = false; + protected $grid = false; /** * Raster minor steps on axis * * @var ezcGraphColor */ - protected $rasterMinor = false; + protected $minorGrid = false; /** * Labeled major steps displayed on the axis @@ -96,6 +96,8 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement $this->border = $palette->axisColor; $this->padding = $palette->padding; $this->margin = $palette->margin; + $this->grid = $palette->gridColor; + $this->minorGrid = $palette->minorGridColor; } /** @@ -122,24 +124,26 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement case 'labelPadding': $this->labelPadding = min( 0, max( 0, (float) $propertyValue ) ); break; - case 'raster': + case 'grid': if ( $propertyValue instanceof ezcGraphColor ) { - $this->raster = $propertyValue; + $this->grid = $propertyValue; } else { - throw new ezcBaseValueException( $propertyValue, 'ezcGraphColor' ); + $this->grid = ezcGraphColor::create( $propertyValue ); } - case 'rasterMinor': + break; + case 'minorGrid': if ( $propertyValue instanceof ezcGraphColor ) { - $this->rasterMinor = $propertyValue; + $this->minorGrid = $propertyValue; } else { - throw new ezcBaseValueException( $propertyValue, 'ezcGraphColor' ); + $this->minorGrid = ezcGraphColor::create( $propertyValue ); } + break; case 'majorStep': if ( $propertyValue <= 0 ) { @@ -348,8 +352,33 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement $xStepsize = ( $end->x - $start->x ) / $steps; $yStepsize = ( $end->y - $start->y ) / $steps; + // Calculate grid boundings + $negGrid = ( $boundings->x0 - $start->x ) * $direction->y + ( $boundings->y0 - $end->y ) * $direction->x; + $posGrid = ( $boundings->x1 - $end->x ) * $direction->y + ( $boundings->y1 - $start->y ) * $direction->x; + + // Draw major steps for ( $i = 0; $i <= $steps; ++$i ) { + if ( $this->grid && $this->grid->alpha < 255 ) + { + $renderer->drawLine( + $this->grid, + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + + $direction->y * $negGrid ), + (int) round( $start->y + $i * $yStepsize + + $direction->x * $negGrid ) + ), + new ezcGraphCoordinate( + (int) round( $start->x + $i * $xStepsize + + $direction->y * $posGrid ), + (int) round( $start->y + $i * $yStepsize + + $direction->x * $posGrid ) + ), + false + ); + } + $renderer->drawLine( $this->border, new ezcGraphCoordinate( @@ -372,9 +401,28 @@ abstract class ezcGraphChartElementAxis extends ezcGraphChartElement if ( $this->minorStep ) { $steps = $this->getMinorStepCount(); - for ( $i = 0; $i < $steps; ++$i ) { + if ( $this->minorGrid && $this->minorGrid->alpha < 255 ) + { + $renderer->drawLine( + $this->minorGrid, + new ezcGraphCoordinate( + (int) round($start->x + $i * ( $end->x - $start->x ) / $steps + + $direction->y * $negGrid ), + (int) round($start->y + $i * ( $end->y - $start->y ) / $steps + + -$direction->x * $negGrid ) + ), + new ezcGraphCoordinate( + (int) round($start->x + $i * ( $end->x - $start->x ) / $steps + + $direction->y * $posGrid ), + (int) round($start->y + $i * ( $end->y - $start->y ) / $steps + + -$direction->x * $posGrid ) + ), + false + ); + } + $renderer->drawLine( $this->border, new ezcGraphCoordinate( diff --git a/src/interfaces/palette.php b/src/interfaces/palette.php index 16cf92e..dcee880 100644 --- a/src/interfaces/palette.php +++ b/src/interfaces/palette.php @@ -36,13 +36,20 @@ abstract class ezcGraphPalette protected $axisColor; /** - * Color of grid + * Color of grid lines * * @var ezcGraphColor */ protected $gridColor; /** + * Color of minor grid lines + * + * @var ezcGraphColor + */ + protected $minorGridColor; + + /** * Array with colors for datasets * * @var array @@ -160,7 +167,12 @@ abstract class ezcGraphPalette { case 'axisColor': return $this->checkColor( $this->axisColor ); - + + case 'gridColor': + return $this->checkColor( $this->gridColor ); + case 'minorGridColor': + return $this->checkColor( $this->minorGridColor ); + case 'dataSetColor': $this->colorIndex = ( ( $this->colorIndex + 1 ) % count( $this->dataSetColor ) ); return $this->checkColor( $this->dataSetColor[ $this->colorIndex ] ); @@ -191,6 +203,9 @@ abstract class ezcGraphPalette return $this->padding; case 'margin': return $this->margin; + + default: + throw new ezcBasePropertyNotFoundException( $propertyName ); } } } diff --git a/src/palette/black.php b/src/palette/black.php index 862876d..b6436c5 100644 --- a/src/palette/black.php +++ b/src/palette/black.php @@ -23,6 +23,20 @@ class ezcGraphPaletteBlack extends ezcGraphPalette protected $axisColor = '#EEEEEC'; /** + * Color of grid lines + * + * @var ezcGraphColor + */ + protected $gridColor = '#888A85'; + + /** + * Color of minor grid lines + * + * @var ezcGraphColor + */ + protected $minorGridColor = '#888A8588'; + + /** * Array with colors for datasets * * @var array diff --git a/tests/labeled_axis_test.php b/tests/labeled_axis_test.php index 767cbe3..c5736a6 100644 --- a/tests/labeled_axis_test.php +++ b/tests/labeled_axis_test.php @@ -392,6 +392,59 @@ class ezcGraphLabeledAxisTest extends ezcTestCase $chart->render( 500, 200 ); } + public function testRenderNumericAxisMajorGrid() + { + $chart = ezcGraph::create( 'Line' ); + $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); + $chart->X_axis->grid = '#BBBBBB'; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawLine', + ) ); + + // X-Axis + $mockedRenderer + ->expects( $this->at( 1 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 120, 0 ) ), + $this->equalTo( new ezcGraphCoordinate( 120, 200 ) ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 3 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 240, 0 ) ), + $this->equalTo( new ezcGraphCoordinate( 240, 200 ) ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 5 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 361, 0 ) ), + $this->equalTo( new ezcGraphCoordinate( 361, 200 ) ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 7 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 481, 0 ) ), + $this->equalTo( new ezcGraphCoordinate( 481, 200 ) ), + $this->equalTo( false ) + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + public function testRenderLabeledAxisLabels() { $chart = ezcGraph::create( 'Line' ); diff --git a/tests/line_test.php b/tests/line_test.php index bf7a817..11f3214 100644 --- a/tests/line_test.php +++ b/tests/line_test.php @@ -237,7 +237,7 @@ class ezcGraphLineChartTest extends ezcTestCase ); $this->assertEquals( - '3e25cf259cb609c55f225ef1f5e3ed22', + '00ada6588d0974ecd7a73e96877d659e', md5_file( $filename ), 'Incorrect image rendered.' ); diff --git a/tests/numeric_axis_test.php b/tests/numeric_axis_test.php index 4f94bd8..d0f0229 100644 --- a/tests/numeric_axis_test.php +++ b/tests/numeric_axis_test.php @@ -623,6 +623,50 @@ class ezcGraphNumericAxisTest extends ezcTestCase $chart->render( 500, 200 ); } + public function testRenderNumericAxisMajorGrid() + { + $chart = ezcGraph::create( 'Line' ); + $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); + $chart->Y_axis->grid = ezcGraphColor::fromHex( '#BBBBBB' ); + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawLine', + ) ); + + // Y-Axis + $mockedRenderer + ->expects( $this->at( 6 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 100, 190 ) ), + $this->equalTo( new ezcGraphCoordinate( 500, 190 ) ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 8 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 100, 154 ) ), + $this->equalTo( new ezcGraphCoordinate( 500, 154 ) ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 16 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 100, 10 ) ), + $this->equalTo( new ezcGraphCoordinate( 500, 10 ) ), + $this->equalTo( false ) + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + public function testRenderNumericAxisMinor() { $chart = ezcGraph::create( 'Line' ); @@ -679,6 +723,52 @@ class ezcGraphNumericAxisTest extends ezcTestCase $chart->render( 500, 200 ); } + public function testRenderNumericAxisMinorGrid() + { + $chart = ezcGraph::create( 'Line' ); + $chart->sample = array( 2000 => 1045, 1300, 1012, 1450 ); + $chart->Y_axis->minorGrid = '#BBBBBB'; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawLine', + ) ); + + // Y-Axis + $mockedRenderer + ->expects( $this->at( 12 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 100, 190 ) ), + $this->equalTo( new ezcGraphCoordinate( 500, 190 ) ), + $this->equalTo( false ) + ); + $mockedRenderer + ->expects( $this->at( 14 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 100, 181 ) ), + $this->equalTo( new ezcGraphCoordinate( 500, 181 ) ), + $this->equalTo( false ) + ); + + // Last minor step + $mockedRenderer + ->expects( $this->at( 50 ) ) + ->method( 'drawLine' ) + ->with( + $this->equalTo( ezcGraphColor::fromHex( '#BBBBBB' ) ), + $this->equalTo( new ezcGraphCoordinate( 100, 19 ) ), + $this->equalTo( new ezcGraphCoordinate( 500, 19 ) ), + $this->equalTo( false ) + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + public function testRenderNumericAxisLabels() { $chart = ezcGraph::create( 'Line' ); |