Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 86 additions & 3 deletions src/charts/horizontal_bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $bo
// Use inner boundings for drawning chart data
$boundings = $innerBoundings;

$yAxisNullPosition = $this->elements['xAxis']->getCoordinate( false );
$xAxisNullPosition = $this->elements['xAxis']->getCoordinate( false );

// Initialize counters
$nr = array();
Expand Down Expand Up @@ -188,6 +188,81 @@ protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $bo
// Render depending on display type of dataset
switch ( true )
{
case ( $data->displayType->default === ezcGraph::BAR ) &&
$this->options->stackBars :
// Check if a bar has already been stacked
if ( !isset( $stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] ) )
{
$start = new ezcGraphCoordinate(
$xAxisNullPosition,
$point->y
);

$stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] = $value;
}
else
{
$start = $yAxis->axisLabelRenderer->modifyChartDataPosition(
$xAxis->axisLabelRenderer->modifyChartDataPosition(
new ezcGraphCoordinate(
$xAxis->getCoordinate( $stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] ),
$yAxis->getCoordinate( $key )
)
)
);

$point = $yAxis->axisLabelRenderer->modifyChartDataPosition(
$xAxis->axisLabelRenderer->modifyChartDataPosition(
new ezcGraphCoordinate(
$xAxis->getCoordinate( $stackedValue[(int) ( $point->y * 10000 )][(int) $value > 0] += $value ),
$yAxis->getCoordinate( $key )
)
)
);
}

// Force one symbol for each stacked bar
if ( !isset( $stackedSymbol[(int) ( $point->y * 10000 )] ) )
{
$stackedSymbol[(int) ( $point->y * 10000 )] = $data->symbol[$key];
}

// Store stacked value for next iteration
$side = ( $point->x == 0 ? 1 : $point->x / abs( $point->x ) );
$stacked[(int) ( $point->y * 10000 )][$side] = $point;

$renderer->drawHorizontalStackedBar(
$boundings,
new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
$data->color->default,
$start,
$point,
$height,
$stackedSymbol[(int) ( $point->y * 10000 )],
$xAxisNullPosition
);

// Render highlight string if requested
if ( $data->highlight[$key] )
{
$renderer->drawDataHighlightText(
$boundings,
new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
$point,
$xAxisNullPosition,
$nr[$data->displayType->default],
$count[$data->displayType->default],
$this->options->highlightFont,
( $data->highlightValue[$key] ? $data->highlightValue[$key] : $value ),
$this->options->highlightSize + $this->options->highlightFont->padding * 2,
( $this->options->highlightLines ? $data->color[$key] : null ),
( $this->options->highlightXOffset ? $this->options->highlightXOffset : 0 ),
( $this->options->highlightYOffset ? $this->options->highlightYOffset : 0 ),
$height,
$data->displayType->default
);
}
break;
case $data->displayType->default === ezcGraph::BAR:
$renderer->drawHorizontalBar(
$boundings,
Expand All @@ -198,7 +273,7 @@ protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $bo
$nr[$data->displayType->default],
$count[$data->displayType->default],
$data->symbol[$key],
$yAxisNullPosition
$xAxisNullPosition
);

// Render highlight string if requested
Expand All @@ -208,7 +283,7 @@ protected function renderData( ezcGraphRenderer $renderer, ezcGraphBoundings $bo
$boundings,
new ezcGraphContext( $datasetName, $key, $data->url[$key] ),
$point,
$yAxisNullPosition,
$xAxisNullPosition,
$nr[$data->displayType->default],
$count[$data->displayType->default],
$this->options->highlightFont,
Expand Down Expand Up @@ -293,6 +368,14 @@ protected function setAxisValues()
}
}

// Also use stacked bar values as base for x axis value span
// calculation
if ( $this->options->stackBars )
{
$this->elements['xAxis']->addData( $virtualBarSumDataSet[0] );
$this->elements['xAxis']->addData( $virtualBarSumDataSet[1] );
}

// There should always be something assigned to the main x and y axis.
if ( !$this->elements['xAxis']->initialized ||
!$this->elements['yAxis']->initialized )
Expand Down
70 changes: 70 additions & 0 deletions src/renderer/horizontal_bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,76 @@ public function drawHorizontalBar(
}
}

/**
* Draw horizontal stacked bar
*
* Draws a horizontal stacked bar part as a data element in a line chart
*
* @param ezcGraphBoundings $boundings Chart boundings
* @param ezcGraphContext $context Context of call
* @param ezcGraphColor $color Color of line
* @param ezcGraphCoordinate $start
* @param ezcGraphCoordinate $position
* @param float $stepSize Space which can be used for bars
* @param int $symbol Symbol to draw for line
* @param float $axisPosition Position of axis for drawing filled lines
* @return void
*/
public function drawHorizontalStackedBar(
ezcGraphBoundings $boundings,
ezcGraphContext $context,
ezcGraphColor $color,
ezcGraphCoordinate $start,
ezcGraphCoordinate $position,
$stepSize,
$symbol = ezcGraph::NO_SYMBOL,
$axisPosition = 0. )
{
// Apply margin
$margin = $stepSize * $this->options->barMargin;
$barHeight = $stepSize - $margin;
$offset = - $stepSize / 2 + $margin / 2;

$barPointArray = array(
new ezcGraphCoordinate(
$boundings->x0 + ( $boundings->width ) * $start->x,
$boundings->y0 + ( $boundings->height ) * $position->y + $offset
),
new ezcGraphCoordinate(
$boundings->x0 + ( $boundings->width ) * $position->x,
$boundings->y0 + ( $boundings->height ) * $position->y + $offset
),
new ezcGraphCoordinate(
$boundings->x0 + ( $boundings->width ) * $position->x,
$boundings->y0 + ( $boundings->height ) * $position->y + $offset + $barHeight
),
new ezcGraphCoordinate(
$boundings->x0 + ( $boundings->width ) * $start->x,
$boundings->y0 + ( $boundings->height ) * $position->y + $offset + $barHeight
),
);

$this->addElementReference(
$context,
$this->driver->drawPolygon(
$barPointArray,
$color,
true
)
);

if ( $this->options->dataBorder > 0 )
{
$darkened = $color->darken( $this->options->dataBorder );
$this->driver->drawPolygon(
$barPointArray,
$darkened,
false,
1
);
}
}

/**
* Draw bar
*
Expand Down
135 changes: 135 additions & 0 deletions tests/renderer_horizontal_bar_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* ezcGraphHorizontalRendererTest
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package Graph
* @version //autogen//
* @subpackage Tests
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/

require_once dirname( __FILE__ ) . '/test_case.php';

/**
* Tests for ezcGraph class.
*
* @package Graph
* @subpackage Tests
*/
class ezcGraphHorizontalRendererTest extends ezcGraphTestCase
{
protected $basePath;

protected $tempDir;

protected $renderer;

protected $driver;

public static function suite()
{
return new PHPUnit_Framework_TestSuite( "ezcGraphHorizontalRendererTest" );
}

public function setUp()
{
parent::setUp();

static $i = 0;
if ( version_compare( phpversion(), '5.1.3', '<' ) )
{
$this->markTestSkipped( "This test requires PHP 5.1.3 or later." );
}

$this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/';
$this->basePath = dirname( __FILE__ ) . '/data/';

$this->renderer = new ezcGraphHorizontalRenderer();

$this->driver = $this->getMockBuilder( 'ezcGraphSvgDriver' )
->enableArgumentCloning()
->setMethods( array(
'drawPolygon',
'drawLine',
'drawTextBox',
'drawCircleSector',
'drawCircularArc',
'drawCircle',
'drawImage',
) )->getMock();
$this->renderer->setDriver( $this->driver );

$this->driver->options->width= 400;
$this->driver->options->height= 200;
}

public function tearDown()
{
$this->driver = null;
$this->renderer = null;

if ( !$this->hasFailed() )
{
$this->removeTempDir();
}
}
// /*

public function testRenderHorizontalStackedBar()
{
$this->driver
->expects( $this->at( 0 ) )
->method( 'drawPolygon' )
->with(
$this->equalTo( array(
new ezcGraphCoordinate( 200, 75. ),
new ezcGraphCoordinate( 200, 75. ),
new ezcGraphCoordinate( 200, 165. ),
new ezcGraphCoordinate( 200, 165. ),
), 1. ),
$this->equalTo( ezcGraphColor::fromHex( '#FF0000' ) ),
$this->equalTo( true )
);
$this->driver
->expects( $this->at( 1 ) )
->method( 'drawPolygon' )
->with(
$this->equalTo( array(
new ezcGraphCoordinate( 200, 75. ),
new ezcGraphCoordinate( 200, 75. ),
new ezcGraphCoordinate( 200, 165. ),
new ezcGraphCoordinate( 200, 165. ),
), 1. ),
$this->equalTo( ezcGraphColor::fromHex( '#800000' ) ),
$this->equalTo( false )
);

$this->renderer->drawHorizontalStackedBar(
new ezcGraphBoundings( 0, 0, 400, 200 ),
new ezcGraphContext(),
ezcGraphColor::fromHex( '#FF0000' ),
new ezcGraphCoordinate( .5, .2 ),
new ezcGraphCoordinate( .5, .6 ),
100,
0
);
}
}
?>