diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-06-14 15:34:12 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-06-14 15:34:12 +0000 |
commit | ccd7c75ecbaa8f13af316b14d7a4162b6a54819d (patch) | |
tree | 3617484fa52c3552aad97845f787c5ffbc43f487 | |
parent | 67864e582b01d121626bb3f9b60b45b7181bb1b7 (diff) | |
download | zetacomponents-graph-ccd7c75ecbaa8f13af316b14d7a4162b6a54819d.zip zetacomponents-graph-ccd7c75ecbaa8f13af316b14d7a4162b6a54819d.tar.gz |
- Add possibility to use filled line charts
-rw-r--r-- | src/charts/line.php | 75 | ||||
-rw-r--r-- | tests/line_test.php | 80 |
2 files changed, 154 insertions, 1 deletions
diff --git a/src/charts/line.php b/src/charts/line.php index 9d3d499..60935de 100644 --- a/src/charts/line.php +++ b/src/charts/line.php @@ -73,7 +73,15 @@ class ezcGraphLineChart extends ezcGraphChart { foreach ( $this->data as $data ) { + if ( $this->options->fillLines !== false ) + { + $fillColor = clone $data->color->default; + $fillColor->alpha = (int) round( ( 255 - $fillColor->alpha ) * ( $this->options->fillLines / 255 ) ); + } + $lastPoint = false; + $lastKey = false; + $lastValue = false; foreach ( $data as $key => $value ) { $point = new ezcGraphCoordinate( @@ -81,6 +89,71 @@ class ezcGraphLineChart extends ezcGraphChart (int) round( $this->elements['Y_axis']->getCoordinate( $boundings, $value ) ) ); + // Fill the line + if ( $lastPoint !== false && $this->options->fillLines !== false ) + { + $axisPosition = (int) round( $this->elements['Y_axis']->getCoordinate( $boundings, false ) ); + + $lastAxisPoint = new ezcGraphCoordinate( + (int) round( $this->elements['X_axis']->getCoordinate( $boundings, $lastKey ) ), + $axisPosition + ); + $axisPoint = new ezcGraphCoordinate( + (int) round( $this->elements['X_axis']->getCoordinate( $boundings, $key ) ), + $axisPosition + ); + + if ( $value / abs( $value ) == $lastValue / abs( $lastValue ) ) + { + // Values have the same sign, so that the line do not cross any axes + $renderer->drawPolygon( + array( + $lastPoint, + $point, + $axisPoint, + $lastAxisPoint, + ), + $fillColor, + true + ); + } + else + { + // Draw two polygones to consider cutting point with axis + $diffOne = abs( $axisPosition - $lastPoint->y ); + $diffTwo = abs( $axisPosition - $point->y ); + + // Switch values, if first is greater then second + $cuttingPosition = $diffOne / ( $diffTwo + $diffOne ); + + // Calculate cutting point + $cuttingPoint = new ezcGraphCoordinate( + (int) round( $lastAxisPoint->x + ( $axisPoint->x - $lastAxisPoint->x ) * $cuttingPosition ), + $axisPosition + ); + + // Finally draw polygons + $renderer->drawPolygon( + array( + $lastPoint, + $cuttingPoint, + $lastAxisPoint, + ), + $fillColor, + true + ); + $renderer->drawPolygon( + array( + $point, + $cuttingPoint, + $axisPoint, + ), + $fillColor, + true + ); + } + } + // Draw line if ( $lastPoint !== false ) { @@ -113,6 +186,8 @@ class ezcGraphLineChart extends ezcGraphChart } $lastPoint = $point; + $lastValue = $value; + $lastKey = $key; } } } diff --git a/tests/line_test.php b/tests/line_test.php index 7f32ff0..28fae16 100644 --- a/tests/line_test.php +++ b/tests/line_test.php @@ -99,7 +99,7 @@ class ezcGraphLineChartTest extends ezcTestCase public function testRenderChartLines() { $chart = ezcGraph::create( 'Line' ); - $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1); + $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => 324, 'sample 4' => 120, 'sample 5' => 1 ); $chart->sampleData->color = '#CC0000'; $chart->sampleData->symbol = ezcGraph::DIAMOND; @@ -149,6 +149,84 @@ class ezcGraphLineChartTest extends ezcTestCase $chart->render( 500, 200 ); } + public function testRenderChartFilledLines() + { + $chart = ezcGraph::create( 'Line' ); + $chart->sampleData = array( 'sample 1' => 234, 'sample 2' => 21, 'sample 3' => -46, 'sample 4' => 120 ); + $chart->palette = 'Black'; + $chart->options->fillLines = 100; + + $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array( + 'drawPolygon', + ) ); + + $mockedRenderer + ->expects( $this->at( 2 ) ) + ->method( 'drawPolygon' ) + ->with( + $this->equalTo( array( + new ezcGraphCoordinate( 120, 40 ), + new ezcGraphCoordinate( 240, 136 ), + new ezcGraphCoordinate( 240, 145 ), + new ezcGraphCoordinate( 120, 145 ), + ) ), + $this->equalTo( ezcGraphColor::fromHex( '#3465A464' ) ), + $this->equalTo( true ) + ); + $mockedRenderer + ->expects( $this->at( 3 ) ) + ->method( 'drawPolygon' ) + ->with( + $this->equalTo( array( + new ezcGraphCoordinate( 240, 136 ), + new ezcGraphCoordinate( 276, 145 ), + new ezcGraphCoordinate( 240, 145 ), + ) ), + $this->equalTo( ezcGraphColor::fromHex( '#3465A464' ) ), + $this->equalTo( true ) + ); + $mockedRenderer + ->expects( $this->at( 4 ) ) + ->method( 'drawPolygon' ) + ->with( + $this->equalTo( array( + new ezcGraphCoordinate( 360, 166 ), + new ezcGraphCoordinate( 276, 145 ), + new ezcGraphCoordinate( 360, 145 ), + ) ), + $this->equalTo( ezcGraphColor::fromHex( '#3465A464' ) ), + $this->equalTo( true ) + ); + $mockedRenderer + ->expects( $this->at( 5 ) ) + ->method( 'drawPolygon' ) + ->with( + $this->equalTo( array( + new ezcGraphCoordinate( 360, 166 ), + new ezcGraphCoordinate( 394, 145 ), + new ezcGraphCoordinate( 360, 145 ), + ) ), + $this->equalTo( ezcGraphColor::fromHex( '#3465A464' ) ), + $this->equalTo( true ) + ); + $mockedRenderer + ->expects( $this->at( 6 ) ) + ->method( 'drawPolygon' ) + ->with( + $this->equalTo( array( + new ezcGraphCoordinate( 480, 91 ), + new ezcGraphCoordinate( 394, 145 ), + new ezcGraphCoordinate( 480, 145 ), + ) ), + $this->equalTo( ezcGraphColor::fromHex( '#3465A464' ) ), + $this->equalTo( true ) + ); + + $chart->renderer = $mockedRenderer; + + $chart->render( 500, 200 ); + } + public function testRenderChartLinesModifiedThickness() { $chart = ezcGraph::create( 'Line' ); |