Skip to content

Commit d4f37c4

Browse files
Feat: adds reversed order to axis component
1 parent b4a8f15 commit d4f37c4

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

docs/usage/shapes/chart.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ $shape->getPlotArea()->getAxisY()->setCrossesAt('3');
121121
| `AXIS::CROSSES_MIN` | Axis crosses at minimum. |
122122
| `AXIS::CROSSES_MAX` | Axis crosses at maximum. |
123123

124+
#### Reversed Order
125+
126+
You can reverse the order of a categorial or value axis trought `setIsReversedOrder`.
127+
128+
Notice: If you reverse the order of a axis, this automatically changes the position of the other axis.
129+
To reset this effect, the axis intersection point on the other axis must be set to `Axis::CROSSES_MAX` using `setCrossesAt`.
130+
131+
``` php
132+
use PhpOffice\PhpPresentation\Shape\Axis;
133+
134+
$bar = new Bar();
135+
136+
$shape = $slide->createChartShape();
137+
$shape->getPlotArea()->setType($bar);
138+
139+
// default value, will return false
140+
$shape->getPlotArea()->getAxisY()->isReversedOrder()
141+
142+
// reverse order
143+
$shape->getPlotArea()->getAxisY()->setIsReversedOrder(true);
144+
145+
// revert the automatic intersection switch on x axis
146+
$shape->getPlotArea()->getAxisX()->setCrossesAt(Axis::CROSSES_MAX);
147+
```
148+
124149
#### Outline
125150

126151
You can define outline for each axis (X & Y).

src/PhpPresentation/Shape/Chart/Axis.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class Axis implements ComparableInterface
9393
*/
9494
protected $crossesAt = self::CROSSES_AUTO;
9595

96+
/**
97+
* @var bool
98+
*/
99+
protected $isReversedOrder = false;
100+
96101
/**
97102
* @var string
98103
*/
@@ -272,6 +277,26 @@ public function setCrossesAt(string $value = self::CROSSES_AUTO): self
272277
return $this;
273278
}
274279

280+
/**
281+
* @return bool
282+
*/
283+
public function isReversedOrder(): bool
284+
{
285+
return $this->isReversedOrder;
286+
}
287+
288+
/**
289+
* @param bool $value
290+
*
291+
* @return self
292+
*/
293+
public function setIsReversedOrder(bool $value = false): self
294+
{
295+
$this->isReversedOrder = $value;
296+
297+
return $this;
298+
}
299+
275300
public function getMajorGridlines(): ?Gridlines
276301
{
277302
return $this->majorGridlines;

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,7 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
22982298
}
22992299

23002300
$crossesAt = $oAxis->getCrossesAt();
2301+
$orientation = $oAxis->isReversedOrder() ? 'maxMin' : 'minMax';
23012302

23022303
if (Chart\Axis::AXIS_X == $typeAxis) {
23032304
$mainElement = 'c:catAx';
@@ -2324,7 +2325,7 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
23242325

23252326
// $mainElement > c:scaling > c:orientation
23262327
$objWriter->startElement('c:orientation');
2327-
$objWriter->writeAttribute('val', 'minMax');
2328+
$objWriter->writeAttribute('val', $orientation);
23282329
$objWriter->endElement();
23292330

23302331
if (null != $oAxis->getMaxBounds()) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public function testCrossesAt(): void
7070
$this->assertEquals(Axis::CROSSES_MAX, $object->getCrossesAt());
7171
}
7272

73+
public function testIsReversedOrder(): void
74+
{
75+
$object = new Axis();
76+
$this->assertFalse($object->isReversedOrder());
77+
$this->assertInstanceOf(Axis::class, $object->setIsReversedOrder(true));
78+
$this->assertTrue($object->isReversedOrder());
79+
$this->assertInstanceOf(Axis::class, $object->setIsReversedOrder(false));
80+
$this->assertFalse($object->isReversedOrder());
81+
}
82+
7383
public function testFont(): void
7484
{
7585
$object = new Axis();

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,35 @@ public function testAxisCrosses(): void
324324
$this->assertIsSchemaECMA376Valid();
325325
}
326326

327+
public function testIsReversedOrder(): void
328+
{
329+
$element = '/c:chartSpace/c:chart/c:plotArea/c:catAx/c:scaling/c:orientation';
330+
331+
$oSlide = $this->oPresentation->getActiveSlide();
332+
$oShape = $oSlide->createChartShape();
333+
$oLine = new Line();
334+
$oShape->getPlotArea()->setType($oLine);
335+
336+
// default
337+
$this->assertFalse($oShape->getPlotArea()->getAxisX()->isReversedOrder());
338+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', 'minMax');
339+
$this->assertIsSchemaECMA376Valid();
340+
341+
// reversed order
342+
$this->assertInstanceOf('PhpOffice\PhpPresentation\Shape\Chart\Axis', $oShape->getPlotArea()->getAxisX()->setIsReversedOrder(true));
343+
$this->resetPresentationFile();
344+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
345+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', 'maxMin');
346+
$this->assertIsSchemaECMA376Valid();
347+
348+
// reset reversed order
349+
$this->assertInstanceOf('PhpOffice\PhpPresentation\Shape\Chart\Axis', $oShape->getPlotArea()->getAxisX()->setIsReversedOrder(false));
350+
$this->resetPresentationFile();
351+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
352+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', 'minMax');
353+
$this->assertIsSchemaECMA376Valid();
354+
}
355+
327356
public function testAxisFont(): void
328357
{
329358
$oSlide = $this->oPresentation->getActiveSlide();

0 commit comments

Comments
 (0)