Skip to content

Commit

Permalink
PHPOffice#269 : Axis Bounds in Chart
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Mar 3, 2017
1 parent d893d36 commit 9a68458
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

### Features
- ODPresentation Writer : Show/Hide Value / Name / Series Name in Chart - @Progi1984 GH-272
- ODPresentation Writer : Axis Bounds in Chart - @Progi1984 GH-269
- PowerPoint2007 Writer : Implement character spacing - @jvanoostrom GH-301
- PowerPoint2007 Writer : Axis Bounds in Chart - @Progi1984 GH-269

## 0.7.0 - 2016-09-12

Expand Down
14 changes: 14 additions & 0 deletions docs/shapes_chart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ For each gridline, you can custom the width (in points), the fill type and the f
$oShape->getPlotArea()->setType($oLine);
$oShape->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines);
For Axis, you can define the min & max bounds with `setMinBounds` & `setMaxBounds` methods.
For resetting them, you pass null as parameter to these methods.

.. code-block:: php
use \PhpOffice\PhpPresentation\Shape\Chart\Gridlines;
$oLine = new Line();
$oShape = $oSlide->createChartShape();
$oShape->getPlotArea()->setType($oLine);
$oShape->getPlotArea()->getAxisX()->setMinBounds(0);
$oShape->getPlotArea()->getAxisX()->setMaxBounds(200);
Title
^^^^^

Expand Down
17 changes: 12 additions & 5 deletions samples/Sample_05_Chart_Line.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
include_once 'Sample_Header.php';

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Line;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Pie3D;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Scatter;
use PhpOffice\PhpPresentation\Shape\Chart\Series;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Border;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Fill;
Expand Down Expand Up @@ -125,7 +121,6 @@
$shape2->getPlotArea()->getAxisY()->setFormatCode('#,##0');
$currentSlide->addShape($shape2);


// Create templated slide
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;
$currentSlide = createTemplatedSlide($objPHPPresentation);
Expand All @@ -152,6 +147,18 @@
$shape3->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines1);
$shape3->getPlotArea()->getAxisY()->setMinorGridlines($oGridLines2);
$currentSlide->addShape($shape3);

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

// Create a shape (chart)
echo date('H:i:s') . ' Create a shape (chart3)' . EOL;
echo date('H:i:s') . ' Feature : Gridlines' . EOL;
$shape4 = clone $shape;
$shape4->getPlotArea()->getAxisY()->setMinBounds(5);
$shape4->getPlotArea()->getAxisY()->setMaxBounds(20);
$currentSlide->addShape($shape4);
// Save file
echo EOL . write($objPHPPresentation, basename(__FILE__, '.php'), $writers);

Expand Down
2 changes: 1 addition & 1 deletion src/PhpPresentation/Shape/AbstractGraphic.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getName()
* Set Name
*
* @param string $pValue
* @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
* @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
*/
public function setName($pValue = '')
{
Expand Down
46 changes: 46 additions & 0 deletions src/PhpPresentation/Shape/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class Axis implements ComparableInterface
*/
protected $minorGridlines;

/**
* @var int
*/
protected $minBounds;

/**
* @var int
*/
protected $maxBounds;

/**
* Create a new \PhpOffice\PhpPresentation\Shape\Chart\Axis instance
*
Expand Down Expand Up @@ -175,6 +185,42 @@ public function setMinorGridlines(Gridlines $minorGridlines)
return $this;
}

/**
* @return int|null
*/
public function getMinBounds()
{
return $this->minBounds;
}

/**
* @param int|null $minBounds
* @return Axis
*/
public function setMinBounds($minBounds = null)
{
$this->minBounds = is_null($minBounds) ? null : (int) $minBounds;
return $this;
}

/**
* @return int|null
*/
public function getMaxBounds()
{
return $this->maxBounds;
}

/**
* @param int|null $maxBounds
* @return Axis
*/
public function setMaxBounds($maxBounds = null)
{
$this->maxBounds = is_null($maxBounds) ? null : (int) $maxBounds;
return $this;
}

/**
* Get hash code
*
Expand Down
12 changes: 12 additions & 0 deletions src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ protected function writeAxisStyle(Chart $chart)
if ($chartType instanceof AbstractTypePie) {
$this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
}
if ($chart->getPlotArea()->getAxisX()->getMinBounds() != null) {
$this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisX()->getMinBounds());
}
if ($chart->getPlotArea()->getAxisX()->getMaxBounds() != null) {
$this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisX()->getMaxBounds());
}
$this->xmlContent->endElement();
// style:style > style:text-properties
$oFont = $chart->getPlotArea()->getAxisX()->getFont();
Expand Down Expand Up @@ -325,6 +331,12 @@ protected function writeAxisStyle(Chart $chart)
if ($chartType instanceof AbstractTypePie) {
$this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
}
if ($chart->getPlotArea()->getAxisY()->getMinBounds() != null) {
$this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisY()->getMinBounds());
}
if ($chart->getPlotArea()->getAxisY()->getMaxBounds() != null) {
$this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisY()->getMaxBounds());
}
$this->xmlContent->endElement();
// style:style > style:text-properties
$oFont = $chart->getPlotArea()->getAxisY()->getFont();
Expand Down
12 changes: 12 additions & 0 deletions src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,18 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, $typeAxis,
$objWriter->writeAttribute('val', 'minMax');
$objWriter->endElement();

if($oAxis->getMaxBounds() != null) {
$objWriter->startElement('c:max');
$objWriter->writeAttribute('val', $oAxis->getMaxBounds());
$objWriter->endElement();
}

if($oAxis->getMinBounds() != null) {
$objWriter->startElement('c:min');
$objWriter->writeAttribute('val', $oAxis->getMinBounds());
$objWriter->endElement();
}

// $mainElement > ##c:scaling
$objWriter->endElement();

Expand Down
18 changes: 18 additions & 0 deletions tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ public function testGridLines()
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Gridlines', $object->getMinorGridlines());
}

public function testBounds()
{
$value = rand(0, 100);
$object = new Axis();

$this->assertNull($object->getMinBounds());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMinBounds($value));
$this->assertEquals($value, $object->getMinBounds());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMinBounds());
$this->assertNull($object->getMinBounds());

$this->assertNull($object->getMaxBounds());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMaxBounds($value));
$this->assertEquals($value, $object->getMaxBounds());
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMaxBounds());
$this->assertNull($object->getMaxBounds());
}

public function testHashIndex()
{
$object = new Axis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,47 @@ public function testTypeArea()
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'draw:fill-color', '#93A9CE');
}

public function testTypeAxisBounds()
{
$value = rand(0, 100);

$oSeries = new Series('Downloads', array('A' => 1, 'B' => 2, 'C' => 4, 'D' => 3, 'E' => 2));
$oSeries->getFill()->setStartColor(new Color('FFAABBCC'));
$oLine = new Line();
$oLine->addSeries($oSeries);
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
$oShape->getPlotArea()->setType($oLine);

$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties';

$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:minimum');
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:maximum');

$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
$this->resetPresentationFile();

$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:maximum');
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:minimum');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:minimum', $value);

$oShape->getPlotArea()->getAxisX()->setMinBounds(null);
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
$this->resetPresentationFile();

$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:minimum');
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:maximum');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:maximum', $value);

$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
$this->resetPresentationFile();

$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:minimum');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:minimum', $value);
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:maximum');
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:maximum', $value);
}

public function testTypeBar()
{
$oSeries = new Series('Series', array('Jan' => 1, 'Feb' => 5, 'Mar' => 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,48 @@ public function testTypeArea()
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
}

public function testTypeAxisBounds()
{
$value = rand(0, 100);

$oSeries = new Series('Downloads', $this->seriesData);
$oSeries->getFill()->setStartColor(new Color('FFAABBCC'));
$oLine = new Line();
$oLine->addSeries($oSeries);
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
$oShape->getPlotArea()->setType($oLine);

$elementMax = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:scaling/c:max';
$elementMin = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:scaling/c:min';

$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);

$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
$this->resetPresentationFile();

$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin, 'val', $value);

$oShape->getPlotArea()->getAxisX()->setMinBounds(null);
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
$this->resetPresentationFile();

$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax, 'val', $value);

$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
$this->resetPresentationFile();

$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin, 'val', $value);
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax, 'val', $value);
}

public function testTypeBar()
{
$oSlide = $this->oPresentation->getActiveSlide();
Expand Down

0 comments on commit 9a68458

Please sign in to comment.