summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/axis/labeled.php43
-rw-r--r--tests/labeled_axis_test.php51
2 files changed, 90 insertions, 4 deletions
diff --git a/src/axis/labeled.php b/src/axis/labeled.php
index b6e0960..5a4f742 100644
--- a/src/axis/labeled.php
+++ b/src/axis/labeled.php
@@ -22,6 +22,19 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis
*/
protected $labels = array();
+ /**
+ * Reduced amount of labels which will be displayed in the chart
+ *
+ * @var array
+ */
+ protected $displayedLabels = array();
+
+ /**
+ * Maximum count of labels which can be displayed on one axis
+ * @TODO: Perhaps base this on the chart size
+ */
+ const MAX_LABEL_COUNT = 10;
+
protected function increaseKeys( $array, $startKey )
{
foreach ( $array as $key => $value )
@@ -85,7 +98,29 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis
*/
public function calculateAxisBoundings()
{
- return true;
+ $labelCount = count( $this->labels ) - 1;
+ if ( $labelCount <= self::MAX_LABEL_COUNT )
+ {
+ $this->displayedLabels = $this->labels;
+ return true;
+ }
+
+ for ( $div = self::MAX_LABEL_COUNT; $div > 0; --$div )
+ {
+ if ( ( $labelCount % $div ) === 0 )
+ {
+ $step = $labelCount / $div;
+ foreach ( $this->labels as $nr => $label )
+ {
+ if ( ( $nr % $step ) === 0 )
+ {
+ $this->displayedLabels[] = $label;
+ }
+ }
+
+ return true;
+ }
+ }
}
/**
@@ -158,7 +193,7 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis
*/
protected function getMajorStepCount()
{
- return count( $this->labels ) - 1;
+ return count( $this->displayedLabels ) - 1;
}
/**
@@ -301,9 +336,9 @@ class ezcGraphChartElementLabeledAxis extends ezcGraphChartElementAxis
*/
protected function getLabel( $step )
{
- if ( isset( $this->labels[$step] ) )
+ if ( isset( $this->displayedLabels[$step] ) )
{
- return $this->labels[$step];
+ return $this->displayedLabels[$step];
}
else
{
diff --git a/tests/labeled_axis_test.php b/tests/labeled_axis_test.php
index 009c3e7..767cbe3 100644
--- a/tests/labeled_axis_test.php
+++ b/tests/labeled_axis_test.php
@@ -448,6 +448,57 @@ class ezcGraphLabeledAxisTest extends ezcTestCase
$chart->render( 500, 200 );
}
+
+ public function testRenderLabeledAxisWithManyPoints()
+ {
+ $data = array();
+ for ( $i = -100; $i <= 500; ++$i )
+ {
+ $data[$i] = 25 * sin( $i / 50 );
+ }
+ $chart = ezcGraph::create( 'Line' );
+ $chart->sinus = $data;
+
+ $mockedRenderer = $this->getMock( 'ezcGraphRenderer2D', array(
+ 'drawTextBox',
+ ) );
+
+ // X-Axis
+ $mockedRenderer
+ ->expects( $this->at( 1 ) )
+ ->method( 'drawTextBox' )
+ ->with(
+ $this->equalTo( new ezcGraphCoordinate( 122, 102 ) ),
+ $this->equalTo( '-100' ),
+ $this->equalTo( 31 ),
+ $this->equalTo( 8 ),
+ $this->equalTo( ezcGraph::LEFT | ezcGraph::TOP )
+ );
+ $mockedRenderer
+ ->expects( $this->at( 2 ) )
+ ->method( 'drawTextBox' )
+ ->with(
+ $this->equalTo( new ezcGraphCoordinate( 155, 102 ) ),
+ $this->equalTo( '-40' ),
+ $this->equalTo( 31 ),
+ $this->equalTo( 8 ),
+ $this->equalTo( ezcGraph::CENTER | ezcGraph::TOP )
+ );
+ $mockedRenderer
+ ->expects( $this->at( 11 ) )
+ ->method( 'drawTextBox' )
+ ->with(
+ $this->equalTo( new ezcGraphCoordinate( 450, 102 ) ),
+ $this->equalTo( '500' ),
+ $this->equalTo( 31 ),
+ $this->equalTo( 8 ),
+ $this->equalTo( ezcGraph::RIGHT | ezcGraph::TOP )
+ );
+
+ $chart->renderer = $mockedRenderer;
+
+ $chart->render( 500, 200 );
+ }
}
?>
OpenPOWER on IntegriCloud