Skip to content

Commit

Permalink
FIXED : #58 : Added support for Horizontal bar chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Dec 22, 2014
1 parent 871c1c0 commit fa24cb3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Features
- Added support for grouping shapes together in a Group - @Pr0phet GH-68
- Added support for calculating the offset and extent on a Slide. - @Pr0phet GH-68
- Added support for Horizontal bar chart - @rdoepke @Progi1984 GH-58

### Bugfix
- PSR-0 via composer broken - @Progi1984 GH-51
Expand Down
34 changes: 34 additions & 0 deletions samples/Sample_07_Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getLegend()->getFont()->setItalic(true);


// Create templated slide
echo date('H:i:s') . ' Create templated slide'.EOL;
$currentSlide = createTemplatedSlide($objPHPPowerPoint);

// Create a bar chart (that should be inserted in a shape)
echo date('H:i:s') . ' Create a horizontal bar chart (that should be inserted in a chart shape) '.EOL;
$bar3DChartHorz = clone $bar3DChart;
$bar3DChartHorz->setBarDirection(Bar3D::DIRECTION_HORIZONTAL);

// Create a shape (chart)
echo date('H:i:s') . ' Create a shape (chart)'.EOL;
$shape = $currentSlide->createChartShape();
$shape->setName('PHPPowerPoint Monthly Downloads')
->setResizeProportional(false)
->setHeight(550)
->setWidth(700)
->setOffsetX(120)
->setOffsetY(80);
$shape->setShadow($oShadow);
$shape->setFill($oFill);
$shape->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getTitle()->setText('PHPPowerPoint Monthly Downloads');
$shape->getTitle()->getFont()->setItalic(true);
$shape->getTitle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
$shape->getPlotArea()->getAxisX()->setTitle('Month');
$shape->getPlotArea()->getAxisY()->setTitle('Downloads');
$shape->getPlotArea()->setType($bar3DChartHorz);
$shape->getView3D()->setRightAngleAxes(true);
$shape->getView3D()->setRotationX(20);
$shape->getView3D()->setRotationY(20);
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getLegend()->getFont()->setItalic(true);

// Create templated slide
echo date('H:i:s') . ' Create templated slide'.EOL;
$currentSlide = createTemplatedSlide($objPHPPowerPoint);
Expand Down
3 changes: 1 addition & 2 deletions src/PhpPowerpoint/Shape/Chart/PlotArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace PhpOffice\PhpPowerpoint\Shape\Chart;

use PhpOffice\PhpPowerpoint\ComparableInterface;
use PhpOffice\PhpPowerpoint\Shape\Chart\Type\AbstractType;

/**
* \PhpOffice\PhpPowerpoint\Shape\Chart\PlotArea
Expand Down Expand Up @@ -105,7 +104,7 @@ public function getType()
* @param \PhpOffice\PhpPowerpoint\Shape\Chart\Type\AbstractType $value
* @return \PhpOffice\PhpPowerpoint\Shape\Chart\PlotArea
*/
public function setType(AbstractType $value)
public function setType(Type\AbstractType $value)
{
$this->type = $value;

Expand Down
8 changes: 8 additions & 0 deletions src/PhpPowerpoint/Writer/ODPresentation/ObjectsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ private function writePlotArea(Chart $chart)

/**
* @param Chart $chart
* @link : http://books.evc-cit.info/odbook/ch08.html#chart-plot-area-section
*/
private function writePlotAreaStyle(Chart $chart)
{
Expand All @@ -452,6 +453,13 @@ private function writePlotAreaStyle(Chart $chart)
$this->xmlContent->writeAttribute('chart:three-dimensional', 'true');
$this->xmlContent->writeAttribute('chart:right-angled-axes', 'true');
}
if ($chartType instanceof Bar3D) {
if ($chartType->getBarDirection() == Bar3D::DIRECTION_HORIZONTAL) {
$this->xmlContent->writeAttribute('chart:vertical', 'true');
} else {
$this->xmlContent->writeAttribute('chart:vertical', 'false');
}
}
$this->xmlContent->writeAttribute('chart:data-label-number', 'value');
// > style:text-properties
$this->xmlContent->endElement();
Expand Down
12 changes: 11 additions & 1 deletion tests/PhpPowerpoint/Tests/Shape/Chart/Type/Bar3DTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,23 @@ public function testData()
$this->assertCount(count($array), $object->getData());
}

public function testSerties()
public function testSeries()
{
$object = new Bar3D();

$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\Chart\\Type\\Bar3D', $object->addSeries(new Series()));
$this->assertCount(1, $object->getData());
}

public function testBarDirection()
{
$object = new Bar3D();
$this->assertEquals(Bar3D::DIRECTION_VERTICAL, $object->getBarDirection());
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\Chart\\Type\\Bar3D', $object->setBarDirection(Bar3D::DIRECTION_HORIZONTAL));
$this->assertEquals(Bar3D::DIRECTION_HORIZONTAL, $object->getBarDirection());
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\Chart\\Type\\Bar3D', $object->setBarDirection(Bar3D::DIRECTION_VERTICAL));
$this->assertEquals(Bar3D::DIRECTION_VERTICAL, $object->getBarDirection());
}

public function testHashCode()
{
Expand Down
2 changes: 0 additions & 2 deletions tests/PhpPowerpoint/Tests/SlideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public function testSlideMasterId()

public function testGroup()
{
$value = rand(1, 100);

$object = new Slide();
$this->assertInstanceOf('PhpOffice\\PhpPowerpoint\\Shape\\Group', $object->createGroup());
}
Expand Down
32 changes: 32 additions & 0 deletions tests/PhpPowerpoint/Tests/Writer/ODPresentation/ChartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ public function testChartBar3D()

$element = '/office:document-content/office:body/office:chart/chart:chart/chart:plot-area/chart:series/chart:data-point';
$this->assertTrue($pres->elementExists($element, 'Object 1/content.xml'));

$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'stylePlotArea\']/style:chart-properties';
$this->assertTrue($pres->elementExists($element, 'Object 1/content.xml'));
$this->assertEquals('false', $pres->getElementAttribute($element, 'chart:vertical', 'Object 1/content.xml'));
}

public function testChartBar3DHorizontal()
{
$oSeries = new Series('Series', array('Jan' => 1, 'Feb' => 5, 'Mar' => 2));
$oSeries->setShowSeriesName(true);
$oSeries->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4672A8'));
$oSeries->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFAB4744'));
$oSeries->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8AA64F'));

$oBar3D = new Bar3D();
$oBar3D->setBarDirection(Bar3D::DIRECTION_HORIZONTAL);
$oBar3D->addSeries($oSeries);

$phpPowerPoint = new PhpPowerpoint();
$oSlide = $phpPowerPoint->getActiveSlide();
$oChart = $oSlide->createChartShape();
$oChart->getPlotArea()->setType($oBar3D);

$pres = TestHelperDOCX::getDocument($phpPowerPoint, 'ODPresentation');

$element = '/office:document-content/office:body/office:chart/chart:chart';
$this->assertTrue($pres->elementExists($element, 'Object 1/content.xml'));
$this->assertEquals('chart:bar', $pres->getElementAttribute($element, 'chart:class', 'Object 1/content.xml'));

$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'stylePlotArea\']/style:chart-properties';
$this->assertTrue($pres->elementExists($element, 'Object 1/content.xml'));
$this->assertEquals('true', $pres->getElementAttribute($element, 'chart:vertical', 'Object 1/content.xml'));
}

public function testChartLine()
Expand Down
27 changes: 27 additions & 0 deletions tests/PhpPowerpoint/Tests/Writer/PowerPoint2007/ChartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ public function testTypeBar3DSuperScript()
$this->assertEquals('30000', $oXMLDoc->getElementAttribute($element, 'baseline', 'ppt/charts/'.$oShape->getIndexedFilename()));
}

public function testTypeBar3DBarDirection()
{
$seriesData = array(
'A' => 1,
'B' => 2,
'C' => 4,
'D' => 3,
'E' => 2,
);

$oPHPPowerPoint = new PhpPowerpoint();
$oSlide = $oPHPPowerPoint->getActiveSlide();
$oShape = $oSlide->createChartShape();
$oShape->setResizeProportional(false)->setHeight(550)->setWidth(700)->setOffsetX(120)->setOffsetY(80);
$oBar3D = new Bar3D();
$oBar3D->setBarDirection(Bar3D::DIRECTION_HORIZONTAL);
$oSeries = new Series('Downloads', $seriesData);
$oBar3D->addSeries($oSeries);
$oShape->getPlotArea()->setType($oBar3D);

$oXMLDoc = TestHelperDOCX::getDocument($oPHPPowerPoint, 'PowerPoint2007');

$element = '/c:chartSpace/c:chart/c:plotArea/c:bar3DChart/c:barDir';
$this->assertTrue($oXMLDoc->elementExists($element, 'ppt/charts/'.$oShape->getIndexedFilename()));
$this->assertEquals(Bar3D::DIRECTION_HORIZONTAL, $oXMLDoc->getElementAttribute($element, 'val', 'ppt/charts/'.$oShape->getIndexedFilename()));
}

public function testTypeLine()
{
$seriesData = array(
Expand Down

0 comments on commit fa24cb3

Please sign in to comment.