Skip to content

Commit 4f47e18

Browse files
committed
Support for defining ticks label position for Axis in Chart
1 parent 7f626d9 commit 4f47e18

File tree

8 files changed

+167
-1
lines changed

8 files changed

+167
-1
lines changed

docs/changes/1.0.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
- Support for managing missing values in Chart - @TonisOrmisson GH-581 & @Progi1986 GH-659
4444
- ODPresentation Writer
4545
- PowerPoint2007 Writer
46+
- Support for defining ticks label position for Axis in Chart - @Web-Mobiledev GH-591 & @Progi1986 GH-660
47+
- ODPresentation Writer
48+
- PowerPoint2007 Writer
4649

4750
## Project Management
4851
- Migrated from Travis CI to Github Actions - @Progi1984 GH-635

docs/usage/shapes/chart.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,28 @@ $shape->getPlotArea()->setType($line);
109109
$shape->getPlotArea()->getAxisX()->getOutline()->setWidth(10);
110110
$shape->getPlotArea()->getAxisX()->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE));
111111
```
112+
#### Tick Label Position
113+
114+
You can define the tick label position with the `setTickLabelPosition` method.
115+
For resetting it, you pass `Axis::TICK_LABEL_POSITION_NEXT_TO` as parameter to this method.
116+
117+
Differents types are available:
118+
119+
* `Axis::TICK_LABEL_POSITION_HIGH`: **Labels are at the high end of the perpendicular axis**
120+
* `Axis::TICK_LABEL_POSITION_LOW`: **Labels are at the low end of the perpendicular axis**
121+
* `Axis::TICK_LABEL_POSITION_NEXT_TO`: **Labels are next to the axis** (default)
122+
123+
``` php
124+
<?php
125+
126+
use PhpOffice\PhpPresentation\Shape\Chart\Axis;
127+
128+
$line = new Line();
129+
130+
$shape = $slide->createChartShape();
131+
$shape->getPlotArea()->setType($line);
132+
$shape->getPlotArea()->getAxisY()->setTickLabelPosition(Axis::TICK_LABEL_POSITION_LOW);
133+
```
112134
#### Tick Marks
113135

114136
For Axis Y, you can define tick mark with `setMinorTickMark` & `setMajorTickMark` methods.

src/PhpPresentation/Shape/Chart/Axis.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Axis implements ComparableInterface
3232
public const TICK_MARK_INSIDE = 'in';
3333
public const TICK_MARK_OUTSIDE = 'out';
3434

35+
public const TICK_LABEL_POSITION_NEXT_TO = 'nextTo';
36+
public const TICK_LABEL_POSITION_HIGH = 'high';
37+
public const TICK_LABEL_POSITION_LOW = 'low';
38+
3539
/**
3640
* Title.
3741
*
@@ -88,6 +92,11 @@ class Axis implements ComparableInterface
8892
*/
8993
protected $majorTickMark = self::TICK_MARK_NONE;
9094

95+
/**
96+
* @var string
97+
*/
98+
protected $tickLabelPosition = self::TICK_LABEL_POSITION_NEXT_TO;
99+
91100
/**
92101
* @var float
93102
*/
@@ -452,4 +461,30 @@ public function setIsVisible(bool $value): self
452461

453462
return $this;
454463
}
464+
465+
/**
466+
* @return string
467+
*/
468+
public function getTickLabelPosition(): string
469+
{
470+
return $this->tickLabelPosition;
471+
}
472+
473+
/**
474+
* @param string $value
475+
*
476+
* @return self
477+
*/
478+
public function setTickLabelPosition(string $value = self::TICK_LABEL_POSITION_NEXT_TO): self
479+
{
480+
if (in_array($value, [
481+
self::TICK_LABEL_POSITION_HIGH,
482+
self::TICK_LABEL_POSITION_LOW,
483+
self::TICK_LABEL_POSITION_NEXT_TO,
484+
])) {
485+
$this->tickLabelPosition = $value;
486+
}
487+
488+
return $this;
489+
}
455490
}

src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpOffice\Common\Text;
88
use PhpOffice\Common\XMLWriter;
99
use PhpOffice\PhpPresentation\Shape\Chart;
10+
use PhpOffice\PhpPresentation\Shape\Chart\Axis;
1011
use PhpOffice\PhpPresentation\Shape\Chart\Title;
1112
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypeBar;
1213
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypePie;
@@ -309,6 +310,20 @@ private function writeAxisMainStyle(Chart\Axis $axis, string $styleName, bool $i
309310
$this->xmlContent->writeAttributeIf($isPieChart, 'chart:reverse-direction', 'true');
310311
$this->xmlContent->writeAttributeIf(null !== $axis->getMinBounds(), 'chart:minimum', $axis->getMinBounds());
311312
$this->xmlContent->writeAttributeIf(null !== $axis->getMaxBounds(), 'chart:maximum', $axis->getMaxBounds());
313+
switch ($axis->getTickLabelPosition()) {
314+
case Axis::TICK_LABEL_POSITION_NEXT_TO:
315+
$this->xmlContent->writeAttribute('chart:axis-label-position', 'near-axis');
316+
break;
317+
case Axis::TICK_LABEL_POSITION_HIGH:
318+
$this->xmlContent->writeAttribute('chart:axis-position', '0');
319+
$this->xmlContent->writeAttribute('chart:axis-label-position', 'outside-end');
320+
break;
321+
case Axis::TICK_LABEL_POSITION_LOW:
322+
$this->xmlContent->writeAttribute('chart:axis-position', '0');
323+
$this->xmlContent->writeAttribute('chart:axis-label-position', 'outside-start');
324+
$this->xmlContent->writeAttribute('chart:tick-mark-position', 'at-axis');
325+
break;
326+
}
312327
$this->xmlContent->endElement();
313328
// style:graphic-properties
314329
$this->xmlContent->startElement('style:graphic-properties');

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
22842284

22852285
// c:tickLblPos
22862286
$objWriter->startElement('c:tickLblPos');
2287-
$objWriter->writeAttribute('val', 'nextTo');
2287+
$objWriter->writeAttribute('val', $oAxis->getTickLabelPosition());
22882288
$objWriter->endElement();
22892289

22902290
// c:spPr

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ public function testOutline(): void
135135
$this->assertInstanceOf(Outline::class, $object->getOutline());
136136
}
137137

138+
public function testTickLabelPosition(): void
139+
{
140+
$object = new Axis();
141+
142+
$this->assertEquals(Axis::TICK_LABEL_POSITION_NEXT_TO, $object->getTickLabelPosition());
143+
$this->assertInstanceOf(Axis::class, $object->setTickLabelPosition(Axis::TICK_LABEL_POSITION_HIGH));
144+
$this->assertEquals(Axis::TICK_LABEL_POSITION_HIGH, $object->getTickLabelPosition());
145+
$this->assertInstanceOf(Axis::class, $object->setTickLabelPosition(Axis::TICK_LABEL_POSITION_NEXT_TO));
146+
$this->assertEquals(Axis::TICK_LABEL_POSITION_NEXT_TO, $object->getTickLabelPosition());
147+
$this->assertInstanceOf(Axis::class, $object->setTickLabelPosition(Axis::TICK_LABEL_POSITION_LOW));
148+
$this->assertEquals(Axis::TICK_LABEL_POSITION_LOW, $object->getTickLabelPosition());
149+
$this->assertInstanceOf(Axis::class, $object->setTickLabelPosition('Unauthorized'));
150+
$this->assertEquals(Axis::TICK_LABEL_POSITION_LOW, $object->getTickLabelPosition());
151+
}
152+
138153
public function testTickMark(): void
139154
{
140155
$value = Axis::TICK_MARK_INSIDE;

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpOffice\Common\Drawing as CommonDrawing;
66
use PhpOffice\PhpPresentation\Shape\Chart;
7+
use PhpOffice\PhpPresentation\Shape\Chart\Axis;
78
use PhpOffice\PhpPresentation\Shape\Chart\Gridlines;
89
use PhpOffice\PhpPresentation\Shape\Chart\Legend;
910
use PhpOffice\PhpPresentation\Shape\Chart\Marker;
@@ -511,6 +512,50 @@ public function testTypeAxisBounds(): void
511512
$this->assertIsSchemaOpenDocumentNotValid('1.2');
512513
}
513514

515+
public function testTypeAxisTickLabelPosition(): void
516+
{
517+
$element = '/office:document-content/office:automatic-styles/style:style[@style:name=\'styleAxisX\']/style:chart-properties';
518+
519+
$oSeries = new Series('Series', $this->seriesData);
520+
$oLine = new Line();
521+
$oLine->addSeries($oSeries);
522+
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
523+
$oShape->getPlotArea()->setType($oLine);
524+
525+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:axis-label-position');
526+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:axis-label-position', 'near-axis');
527+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:axis-position');
528+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:tick-mark-position');
529+
530+
// chart:title : Element chart failed to validate attributes
531+
$this->assertIsSchemaOpenDocumentNotValid('1.2');
532+
533+
$this->resetPresentationFile();
534+
$oShape->getPlotArea()->getAxisX()->setTickLabelPosition(Axis::TICK_LABEL_POSITION_HIGH);
535+
536+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:axis-label-position');
537+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:axis-label-position', 'outside-end');
538+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:axis-position');
539+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:axis-position', '0');
540+
$this->assertZipXmlAttributeNotExists('Object 1/content.xml', $element, 'chart:tick-mark-position');
541+
542+
// chart:title : Element chart failed to validate attributes
543+
$this->assertIsSchemaOpenDocumentNotValid('1.2');
544+
545+
$this->resetPresentationFile();
546+
$oShape->getPlotArea()->getAxisX()->setTickLabelPosition(Axis::TICK_LABEL_POSITION_LOW);
547+
548+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:axis-label-position');
549+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:axis-label-position', 'outside-start');
550+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:axis-position');
551+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:axis-position', '0');
552+
$this->assertZipXmlAttributeExists('Object 1/content.xml', $element, 'chart:tick-mark-position');
553+
$this->assertZipXmlAttributeEquals('Object 1/content.xml', $element, 'chart:tick-mark-position', 'at-axis');
554+
555+
// chart:title : Element chart failed to validate attributes
556+
$this->assertIsSchemaOpenDocumentNotValid('1.2');
557+
}
558+
514559
public function testTypeBar(): void
515560
{
516561
$oSeries = new Series('Series', ['Jan' => '1', 'Feb' => '5', 'Mar' => '2']);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,37 @@ public function testTypeAxisBounds(): void
414414
$this->assertIsSchemaECMA376Valid();
415415
}
416416

417+
public function testTypeAxisTickLabelPosition(): void
418+
{
419+
$element = '/c:chartSpace/c:chart/c:plotArea/c:valAx/c:tickLblPos';
420+
421+
$oSeries = new Series('Downloads', $this->seriesData);
422+
$oLine = new Line();
423+
$oLine->addSeries($oSeries);
424+
$oShape = $this->oPresentation->getActiveSlide()->createChartShape();
425+
$oShape->getPlotArea()->setType($oLine);
426+
427+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
428+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', Axis::TICK_LABEL_POSITION_NEXT_TO);
429+
430+
$this->assertIsSchemaECMA376Valid();
431+
432+
$oShape->getPlotArea()->getAxisY()->setTickLabelPosition(Axis::TICK_LABEL_POSITION_HIGH);
433+
$this->resetPresentationFile();
434+
435+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
436+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', Axis::TICK_LABEL_POSITION_HIGH);
437+
$this->assertIsSchemaECMA376Valid();
438+
439+
$oShape->getPlotArea()->getAxisY()->setTickLabelPosition(Axis::TICK_LABEL_POSITION_LOW);
440+
$this->resetPresentationFile();
441+
442+
$this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element);
443+
$this->assertZipXmlAttributeEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'val', Axis::TICK_LABEL_POSITION_LOW);
444+
445+
$this->assertIsSchemaECMA376Valid();
446+
}
447+
417448
public function testTypeAxisTickMark(): void
418449
{
419450
$value = Axis::TICK_MARK_CROSS;

0 commit comments

Comments
 (0)