Author: Kore Nordmann
Date: 2006-06-14 17:34:12 +0200 (Wed, 14 Jun 2006)
New Revision: 3133
Log:
- Add possibility to use filled line charts
Modified:
trunk/Graph/src/charts/line.php
trunk/Graph/tests/line_test.php
Modified: trunk/Graph/src/charts/line.php
===================================================================
--- trunk/Graph/src/charts/line.php 2006-06-14 13:48:41 UTC (rev 3132)
+++ trunk/Graph/src/charts/line.php 2006-06-14 15:34:12 UTC (rev 3133)
@@ -73,7 +73,15 @@
{
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 @@
(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 @@
}
$lastPoint = $point;
+ $lastValue = $value;
+ $lastKey = $key;
}
}
}
Modified: trunk/Graph/tests/line_test.php
===================================================================
--- trunk/Graph/tests/line_test.php 2006-06-14 13:48:41 UTC (rev 3132)
+++ trunk/Graph/tests/line_test.php 2006-06-14 15:34:12 UTC (rev 3133)
@@ -99,7 +99,7 @@
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 @@
$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' );
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components