Skip to content

Commit

Permalink
Merge pull request #774 from Progi1984/pr713
Browse files Browse the repository at this point in the history
PowerPoint 2007 Writer : Added support to Font for Axis tick label & chinese font support
  • Loading branch information
Progi1984 authored Dec 5, 2023
2 parents 0016a93 + b72f395 commit a8db2bf
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- PowerPoint2007 Reader
- PowerPoint2007 Writer
- Added support for PHP 8.2 & 8.3 - [@Progi1984](https://github.com/Progi1984) in [#769](https://github.com/PHPOffice/PHPPresentation/pull/769)
- PowerPoint 2007 Writer : Added support to Font for Axis tick label & chinese font support - [@zhengwhizz](https://github.com/zhengwhizz) in [#774](https://github.com/PHPOffice/PHPPresentation/pull/774)

## Bugfixes

Expand Down
25 changes: 24 additions & 1 deletion docs/usage/shapes/chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ $shape->getPlotArea()->setType($line);
$shape->getPlotArea()->getAxisX()->getOutline()->setWidth(10);
$shape->getPlotArea()->getAxisX()->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE));
```
#### Tick Label Position
#### Tick Label
##### Position

You can define the tick label position with the `setTickLabelPosition` method.
For resetting it, you pass `Axis::TICK_LABEL_POSITION_NEXT_TO` as parameter to this method.
Expand All @@ -187,6 +188,28 @@ $shape = $slide->createChartShape();
$shape->getPlotArea()->setType($line);
$shape->getPlotArea()->getAxisY()->setTickLabelPosition(Axis::TICK_LABEL_POSITION_LOW);
```

##### Font

You can define the tick label font with the `setTickLabelFont` method.
For resetting it, you pass `null` as parameter to this method.

``` php
<?php

use PhpOffice\PhpPresentation\Shape\Chart\Axis;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Font;

$line = new Line();
$font = new Font();
$font->setColor(new Color('C00000'))

$shape = $slide->createChartShape();
$shape->getPlotArea()->setType($line);
$shape->getPlotArea()->getAxisY()->setTickLabelFont($font);
```

#### Tick Marks

For Axis Y, you can define tick mark with `setMinorTickMark` & `setMajorTickMark` methods.
Expand Down
26 changes: 26 additions & 0 deletions src/PhpPresentation/Shape/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class Axis implements ComparableInterface
*/
private $font;

/**
* Tick lable font.
*
* @var Font
*/
protected $tickLabelFont;

/**
* @var null|Gridlines
*/
Expand Down Expand Up @@ -144,6 +151,7 @@ public function __construct(string $title = 'Axis Title')
$this->title = $title;
$this->outline = new Outline();
$this->font = new Font();
$this->tickLabelFont = new Font();
}

/**
Expand Down Expand Up @@ -172,6 +180,24 @@ public function getFont(): ?Font
return $this->font;
}

/**
* Set tick label font.
*/
public function setTickLabelFont(?Font $font = null): self
{
$this->tickLabelFont = $font;

return $this;
}

/**
* Get tick label font.
*/
public function getTickLabelFont(): ?Font
{
return $this->tickLabelFont;
}

/**
* Set font.
*/
Expand Down
107 changes: 99 additions & 8 deletions src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,6 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ

// a:defRPr
$objWriter->startElement('a:defRPr');

$objWriter->writeAttribute('b', ($series->getFont()->isBold() ? 'true' : 'false'));
$objWriter->writeAttribute('i', ($series->getFont()->isItalic() ? 'true' : 'false'));
$objWriter->writeAttribute('strike', ($series->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike'));
Expand All @@ -895,12 +894,16 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ
// a:solidFill
$objWriter->startElement('a:solidFill');
$this->writeColor($objWriter, $series->getFont()->getColor());
// >a:solidFill
$objWriter->endElement();

// a:latin
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
// >a:latin
$objWriter->endElement();

// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

// >a:defRPr
Expand Down Expand Up @@ -1106,6 +1109,10 @@ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $in
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

$objWriter->endElement();

Expand Down Expand Up @@ -1309,6 +1316,10 @@ protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bo
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

// c:dLbls\c:txPr\a:p\a:pPr\a:defRPr\
$objWriter->endElement();
Expand Down Expand Up @@ -1450,6 +1461,10 @@ protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includ
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

$objWriter->endElement();

Expand Down Expand Up @@ -1610,6 +1625,10 @@ protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $in
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

$objWriter->endElement();

Expand Down Expand Up @@ -1759,6 +1778,10 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

$objWriter->endElement();

Expand Down Expand Up @@ -1931,6 +1954,10 @@ protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $in
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

$objWriter->endElement();

Expand Down Expand Up @@ -2093,6 +2120,10 @@ protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $series->getFont()->getName());
$objWriter->endElement();

$objWriter->endElement();

Expand Down Expand Up @@ -2366,6 +2397,10 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $oAxis->getFont()->getName());
$objWriter->endElement();
// a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $oAxis->getFont()->getName());
$objWriter->endElement();

$objWriter->endElement();

Expand Down Expand Up @@ -2429,9 +2464,65 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty

// c:spPr
$objWriter->startElement('c:spPr');
// Outline
$this->writeOutline($objWriter, $oAxis->getOutline());
// ##c:spPr
$objWriter->endElement();

// c:txPr
$objWriter->startElement('c:txPr');

// a:bodyPr
$objWriter->writeElement('a:bodyPr', null);

// a:lstStyle
$objWriter->writeElement('a:lstStyle', null);

// a:p
$objWriter->startElement('a:p');

// a:pPr
$objWriter->startElement('a:pPr');

// a:defRPr
$objWriter->startElement('a:defRPr');
$objWriter->writeAttribute('b', ($oAxis->getTickLabelFont()->isBold() ? 'true' : 'false'));
$objWriter->writeAttribute('i', ($oAxis->getTickLabelFont()->isItalic() ? 'true' : 'false'));
$objWriter->writeAttribute('strike', ($oAxis->getTickLabelFont()->isStrikethrough() ? 'sngStrike' : 'noStrike'));
$objWriter->writeAttribute('sz', ($oAxis->getTickLabelFont()->getSize() * 100));
$objWriter->writeAttribute('u', $oAxis->getTickLabelFont()->getUnderline());
$objWriter->writeAttributeIf($oAxis->getTickLabelFont()->isSuperScript(), 'baseline', '300000');
$objWriter->writeAttributeIf($oAxis->getTickLabelFont()->isSubScript(), 'baseline', '-250000');

// Font - a:solidFill
$objWriter->startElement('a:solidFill');
$this->writeColor($objWriter, $oAxis->getTickLabelFont()->getColor());
$objWriter->endElement();

// Font - a:latin
$objWriter->startElement('a:latin');
$objWriter->writeAttribute('typeface', $oAxis->getTickLabelFont()->getName());
$objWriter->endElement();

// Font - a:ea
$objWriter->startElement('a:ea');
$objWriter->writeAttribute('typeface', $oAxis->getTickLabelFont()->getName());
$objWriter->endElement();

//## a:defRPr
$objWriter->endElement();

//## a:pPr
$objWriter->endElement();

// a:endParaRPr
$objWriter->startElement('a:endParaRPr');
$objWriter->writeAttribute('lang', 'en-US');
$objWriter->writeAttribute('dirty', '0');
$objWriter->endElement();

// ## a:p
$objWriter->endElement();

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

// c:crossAx
Expand Down Expand Up @@ -2462,7 +2553,7 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
$objWriter->endElement();

// c:majorUnit
if ($oAxis->getMajorUnit() != null) {
if ($oAxis->getMajorUnit() !== null) {
$objWriter->startElement('c:tickLblSkip');
$objWriter->writeAttribute('val', $oAxis->getMajorUnit());
$objWriter->endElement();
Expand All @@ -2482,14 +2573,14 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty
$objWriter->endElement();

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

// c:minorUnit
if (null != $oAxis->getMinorUnit()) {
if ($oAxis->getMinorUnit() !== null) {
$objWriter->startElement('c:minorUnit');
$objWriter->writeAttribute('val', $oAxis->getMinorUnit());
$objWriter->endElement();
Expand Down
11 changes: 11 additions & 0 deletions tests/PhpPresentation/Tests/Shape/Chart/AxisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public function testOutline(): void
self::assertInstanceOf(Outline::class, $object->getOutline());
}

public function testTickLabelFont(): void
{
$object = new Axis();

self::assertInstanceOf(Font::class, $object->getTickLabelFont());
self::assertInstanceOf(Axis::class, $object->setTickLabelFont());
self::assertNull($object->getTickLabelFont());
self::assertInstanceOf(Axis::class, $object->setTickLabelFont(new Font()));
self::assertInstanceOf(Font::class, $object->getTickLabelFont());
}

public function testTickLabelPosition(): void
{
$object = new Axis();
Expand Down
Loading

0 comments on commit a8db2bf

Please sign in to comment.