Skip to content

Commit 4eae228

Browse files
authored
Merge pull request #333 from Progi1984/issue269
#269 : Axis Bounds in Chart
2 parents d893d36 + 93ceb06 commit 4eae228

File tree

10 files changed

+200
-6
lines changed

10 files changed

+200
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
### Features
1515
- ODPresentation Writer : Show/Hide Value / Name / Series Name in Chart - @Progi1984 GH-272
16+
- ODPresentation Writer : Axis Bounds in Chart - @Progi1984 GH-269
1617
- PowerPoint2007 Writer : Implement character spacing - @jvanoostrom GH-301
18+
- PowerPoint2007 Writer : Axis Bounds in Chart - @Progi1984 GH-269
1719

1820
## 0.7.0 - 2016-09-12
1921

docs/shapes_chart.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ For each gridline, you can custom the width (in points), the fill type and the f
3434
$oShape->getPlotArea()->setType($oLine);
3535
$oShape->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines);
3636
37+
For Axis, you can define the min & max bounds with `setMinBounds` & `setMaxBounds` methods.
38+
For resetting them, you pass null as parameter to these methods.
39+
40+
.. code-block:: php
41+
42+
use \PhpOffice\PhpPresentation\Shape\Chart\Gridlines;
43+
44+
$oLine = new Line();
45+
46+
$oShape = $oSlide->createChartShape();
47+
$oShape->getPlotArea()->setType($oLine);
48+
$oShape->getPlotArea()->getAxisX()->setMinBounds(0);
49+
$oShape->getPlotArea()->getAxisX()->setMaxBounds(200);
50+
3751
Title
3852
^^^^^
3953

samples/Sample_05_Chart_Line.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
include_once 'Sample_Header.php';
44

55
use PhpOffice\PhpPresentation\PhpPresentation;
6-
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D;
76
use PhpOffice\PhpPresentation\Shape\Chart\Type\Line;
8-
use PhpOffice\PhpPresentation\Shape\Chart\Type\Pie3D;
9-
use PhpOffice\PhpPresentation\Shape\Chart\Type\Scatter;
107
use PhpOffice\PhpPresentation\Shape\Chart\Series;
11-
use PhpOffice\PhpPresentation\Style\Alignment;
128
use PhpOffice\PhpPresentation\Style\Border;
139
use PhpOffice\PhpPresentation\Style\Color;
1410
use PhpOffice\PhpPresentation\Style\Fill;
@@ -125,7 +121,6 @@
125121
$shape2->getPlotArea()->getAxisY()->setFormatCode('#,##0');
126122
$currentSlide->addShape($shape2);
127123

128-
129124
// Create templated slide
130125
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;
131126
$currentSlide = createTemplatedSlide($objPHPPresentation);
@@ -152,6 +147,18 @@
152147
$shape3->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines1);
153148
$shape3->getPlotArea()->getAxisY()->setMinorGridlines($oGridLines2);
154149
$currentSlide->addShape($shape3);
150+
151+
// Create templated slide
152+
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;
153+
$currentSlide = createTemplatedSlide($objPHPPresentation);
154+
155+
// Create a shape (chart)
156+
echo date('H:i:s') . ' Create a shape (chart3)' . EOL;
157+
echo date('H:i:s') . ' Feature : Gridlines' . EOL;
158+
$shape4 = clone $shape;
159+
$shape4->getPlotArea()->getAxisY()->setMinBounds(5);
160+
$shape4->getPlotArea()->getAxisY()->setMaxBounds(20);
161+
$currentSlide->addShape($shape4);
155162
// Save file
156163
echo EOL . write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
157164

src/PhpPresentation/Shape/AbstractGraphic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function getName()
117117
* Set Name
118118
*
119119
* @param string $pValue
120-
* @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
120+
* @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
121121
*/
122122
public function setName($pValue = '')
123123
{

src/PhpPresentation/Shape/Chart/Axis.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ class Axis implements ComparableInterface
5959
*/
6060
protected $minorGridlines;
6161

62+
/**
63+
* @var int
64+
*/
65+
protected $minBounds;
66+
67+
/**
68+
* @var int
69+
*/
70+
protected $maxBounds;
71+
6272
/**
6373
* Create a new \PhpOffice\PhpPresentation\Shape\Chart\Axis instance
6474
*
@@ -175,6 +185,42 @@ public function setMinorGridlines(Gridlines $minorGridlines)
175185
return $this;
176186
}
177187

188+
/**
189+
* @return int|null
190+
*/
191+
public function getMinBounds()
192+
{
193+
return $this->minBounds;
194+
}
195+
196+
/**
197+
* @param int|null $minBounds
198+
* @return Axis
199+
*/
200+
public function setMinBounds($minBounds = null)
201+
{
202+
$this->minBounds = is_null($minBounds) ? null : (int) $minBounds;
203+
return $this;
204+
}
205+
206+
/**
207+
* @return int|null
208+
*/
209+
public function getMaxBounds()
210+
{
211+
return $this->maxBounds;
212+
}
213+
214+
/**
215+
* @param int|null $maxBounds
216+
* @return Axis
217+
*/
218+
public function setMaxBounds($maxBounds = null)
219+
{
220+
$this->maxBounds = is_null($maxBounds) ? null : (int) $maxBounds;
221+
return $this;
222+
}
223+
178224
/**
179225
* Get hash code
180226
*

src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ protected function writeAxisStyle(Chart $chart)
289289
if ($chartType instanceof AbstractTypePie) {
290290
$this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
291291
}
292+
if ($chart->getPlotArea()->getAxisX()->getMinBounds() != null) {
293+
$this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisX()->getMinBounds());
294+
}
295+
if ($chart->getPlotArea()->getAxisX()->getMaxBounds() != null) {
296+
$this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisX()->getMaxBounds());
297+
}
292298
$this->xmlContent->endElement();
293299
// style:style > style:text-properties
294300
$oFont = $chart->getPlotArea()->getAxisX()->getFont();
@@ -325,6 +331,12 @@ protected function writeAxisStyle(Chart $chart)
325331
if ($chartType instanceof AbstractTypePie) {
326332
$this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
327333
}
334+
if ($chart->getPlotArea()->getAxisY()->getMinBounds() != null) {
335+
$this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisY()->getMinBounds());
336+
}
337+
if ($chart->getPlotArea()->getAxisY()->getMaxBounds() != null) {
338+
$this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisY()->getMaxBounds());
339+
}
328340
$this->xmlContent->endElement();
329341
// style:style > style:text-properties
330342
$oFont = $chart->getPlotArea()->getAxisY()->getFont();

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,18 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, $typeAxis,
19751975
$objWriter->writeAttribute('val', 'minMax');
19761976
$objWriter->endElement();
19771977

1978+
if ($oAxis->getMaxBounds() != null) {
1979+
$objWriter->startElement('c:max');
1980+
$objWriter->writeAttribute('val', $oAxis->getMaxBounds());
1981+
$objWriter->endElement();
1982+
}
1983+
1984+
if ($oAxis->getMinBounds() != null) {
1985+
$objWriter->startElement('c:min');
1986+
$objWriter->writeAttribute('val', $oAxis->getMinBounds());
1987+
$objWriter->endElement();
1988+
}
1989+
19781990
// $mainElement > ##c:scaling
19791991
$objWriter->endElement();
19801992

tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ public function testGridLines()
6868
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Gridlines', $object->getMinorGridlines());
6969
}
7070

71+
public function testBounds()
72+
{
73+
$value = rand(0, 100);
74+
$object = new Axis();
75+
76+
$this->assertNull($object->getMinBounds());
77+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMinBounds($value));
78+
$this->assertEquals($value, $object->getMinBounds());
79+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMinBounds());
80+
$this->assertNull($object->getMinBounds());
81+
82+
$this->assertNull($object->getMaxBounds());
83+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMaxBounds($value));
84+
$this->assertEquals($value, $object->getMaxBounds());
85+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setMaxBounds());
86+
$this->assertNull($object->getMaxBounds());
87+
}
88+
7189
public function testHashIndex()
7290
{
7391
$object = new Axis();

tests/PhpPresentation/Tests/Writer/ODPresentation/ObjectsChartTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,47 @@ public function testTypeArea()
128128
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'draw:fill-color', '#93A9CE');
129129
}
130130

131+
public function testTypeAxisBounds()
132+
{
133+
$value = rand(0, 100);
134+
135+
$oSeries = new Series('Downloads', array('A' => 1, 'B' => 2, 'C' => 4, 'D' => 3, 'E' => 2));
136+
$oSeries->getFill()->setStartColor(new Color('FFAABBCC'));
137+
$oLine = new Line();
138+
$oLine->addSeries($oSeries);
139+
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
140+
$oShape->getPlotArea()->setType($oLine);
141+
142+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties';
143+
144+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:minimum');
145+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:maximum');
146+
147+
$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
148+
$this->resetPresentationFile();
149+
150+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:maximum');
151+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:minimum');
152+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:minimum', $value);
153+
154+
$oShape->getPlotArea()->getAxisX()->setMinBounds(null);
155+
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
156+
$this->resetPresentationFile();
157+
158+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:minimum');
159+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:maximum');
160+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:maximum', $value);
161+
162+
$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
163+
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
164+
$this->resetPresentationFile();
165+
166+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:minimum');
167+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:minimum', $value);
168+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:maximum');
169+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:maximum', $value);
170+
}
171+
131172
public function testTypeBar()
132173
{
133174
$oSeries = new Series('Series', array('Jan' => 1, 'Feb' => 5, 'Mar' => 2));

tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,48 @@ public function testTypeArea()
144144
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
145145
}
146146

147+
public function testTypeAxisBounds()
148+
{
149+
$value = rand(0, 100);
150+
151+
$oSeries = new Series('Downloads', $this->seriesData);
152+
$oSeries->getFill()->setStartColor(new Color('FFAABBCC'));
153+
$oLine = new Line();
154+
$oLine->addSeries($oSeries);
155+
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
156+
$oShape->getPlotArea()->setType($oLine);
157+
158+
$elementMax = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:scaling/c:max';
159+
$elementMin = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:scaling/c:min';
160+
161+
$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
162+
$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);
163+
164+
$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
165+
$this->resetPresentationFile();
166+
167+
$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
168+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);
169+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin, 'val', $value);
170+
171+
$oShape->getPlotArea()->getAxisX()->setMinBounds(null);
172+
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
173+
$this->resetPresentationFile();
174+
175+
$this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);
176+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
177+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax, 'val', $value);
178+
179+
$oShape->getPlotArea()->getAxisX()->setMinBounds($value);
180+
$oShape->getPlotArea()->getAxisX()->setMaxBounds($value);
181+
$this->resetPresentationFile();
182+
183+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin);
184+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMin, 'val', $value);
185+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax);
186+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $elementMax, 'val', $value);
187+
}
188+
147189
public function testTypeBar()
148190
{
149191
$oSlide = $this->oPresentation->getActiveSlide();

0 commit comments

Comments
 (0)