Skip to content

Commit c689721

Browse files
committed
Merge pull request #186 from jrking4/develop
Add Axis Font Options & Fix Axis/Legend Tests
2 parents a93d525 + 5b51ba5 commit c689721

File tree

9 files changed

+310
-106
lines changed

9 files changed

+310
-106
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
### Features
1212
- ODPresentation Writer : Thumbnail of the presentation - @Progi1984 GH-125
1313
- PowerPoint2007 Writer : Thumbnail of the presentation - @Progi1984 GH-125
14+
- ODPresentation Writer : Add Font Support For Chart Axis - @jrking4 GH-186
15+
- PowerPoint2007 Writer : Add Font Support For Chart Axis - @jrking4 GH-186
1416

1517
## 0.6.0 - 2016-01-24
1618

samples/Sample_05_Chart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ function fnSlide_Bar(PhpPresentation $objPHPPresentation) {
108108
$shape->getTitle()->getFont()->setItalic(true);
109109
$shape->getTitle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
110110
$shape->getPlotArea()->getAxisX()->setTitle('Month');
111+
$shape->getPlotArea()->getAxisY()->getFont()->getColor()->setRGB('00FF00');
111112
$shape->getPlotArea()->getAxisY()->setTitle('Downloads');
112113
$shape->getPlotArea()->setType($barChart);
113114
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);

src/PhpPresentation/Shape/Chart/Axis.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpPresentation\Shape\Chart;
1919

2020
use PhpOffice\PhpPresentation\ComparableInterface;
21+
use PhpOffice\PhpPresentation\Style\Font;
2122

2223
/**
2324
* \PhpOffice\PhpPresentation\Shape\Chart\Axis
@@ -38,6 +39,13 @@ class Axis implements ComparableInterface
3839
*/
3940
private $formatCode = '';
4041

42+
/**
43+
* Font
44+
*
45+
* @var \PhpOffice\PhpPresentation\Style\Font
46+
*/
47+
private $font;
48+
4149
/**
4250
* Create a new \PhpOffice\PhpPresentation\Shape\Chart\Axis instance
4351
*
@@ -46,6 +54,7 @@ class Axis implements ComparableInterface
4654
public function __construct($title = 'Axis Title')
4755
{
4856
$this->title = $title;
57+
$this->font = new Font();
4958
}
5059

5160
/**
@@ -71,6 +80,29 @@ public function setTitle($value = 'Axis Title')
7180
return $this;
7281
}
7382

83+
/**
84+
* Get font
85+
*
86+
* @return \PhpOffice\PhpPresentation\Style\Font
87+
*/
88+
public function getFont()
89+
{
90+
return $this->font;
91+
}
92+
93+
/**
94+
* Set font
95+
*
96+
* @param \PhpOffice\PhpPresentation\Style\Font $pFont Font
97+
* @throws \Exception
98+
* @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph
99+
*/
100+
public function setFont(Font $pFont = null)
101+
{
102+
$this->font = $pFont;
103+
return $this;
104+
}
105+
74106
/**
75107
* Get Format Code
76108
*

src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ private function writeAxisStyle(Chart $chart)
298298
}
299299
// > style:chart-properties
300300
$this->xmlContent->endElement();
301+
// style:text-properties
302+
$this->xmlContent->startElement('style:text-properties');
303+
$this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisX()->getFont()->getColor()->getRGB());
304+
$this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisX()->getFont()->getName());
305+
$this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisX()->getFont()->getSize().'pt');
306+
$this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisX()->getFont()->isItalic() ? 'italic' : 'normal');
307+
// > style:text-properties
308+
$this->xmlContent->endElement();
301309
// style:graphic-properties
302310
$this->xmlContent->startElement('style:graphic-properties');
303311
$this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
@@ -322,6 +330,14 @@ private function writeAxisStyle(Chart $chart)
322330
}
323331
// > style:chart-properties
324332
$this->xmlContent->endElement();
333+
// style:text-properties
334+
$this->xmlContent->startElement('style:text-properties');
335+
$this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisY()->getFont()->getColor()->getRGB());
336+
$this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisY()->getFont()->getName());
337+
$this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisY()->getFont()->getSize().'pt');
338+
$this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisY()->getFont()->isItalic() ? 'italic' : 'normal');
339+
// > style:text-properties
340+
$this->xmlContent->endElement();
325341
// style:graphic-properties
326342
$this->xmlContent->startElement('style:graphic-properties');
327343
$this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');

src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,39 @@ protected function writePlotArea(XMLWriter $objWriter, PlotArea $subject, Chart
580580
$objWriter->startElement('a:pPr');
581581

582582
// a:defRPr
583-
$objWriter->writeElement('a:defRPr', null);
583+
$objWriter->startElement('a:defRPr');
584+
585+
$objWriter->writeAttribute('b', ($subject->getAxisX()->getFont()->isBold() ? 'true' : 'false'));
586+
$objWriter->writeAttribute('i', ($subject->getAxisX()->getFont()->isItalic() ? 'true' : 'false'));
587+
$objWriter->writeAttribute('strike', ($subject->getAxisX()->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike'));
588+
$objWriter->writeAttribute('sz', ($subject->getAxisX()->getFont()->getSize() * 100));
589+
$objWriter->writeAttribute('u', $subject->getAxisX()->getFont()->getUnderline());
590+
591+
if ($subject->getAxisX()->getFont()->isSuperScript() || $subject->getAxisX()->getFont()->isSubScript()) {
592+
if ($subject->getAxisX()->getFont()->isSuperScript()) {
593+
$objWriter->writeAttribute('baseline', '30000');
594+
} elseif ($subject->getAxisX()->getFont()->isSubScript()) {
595+
$objWriter->writeAttribute('baseline', '-25000');
596+
}
597+
}
598+
599+
// Font - a:solidFill
600+
$objWriter->startElement('a:solidFill');
601+
602+
// a:srgbClr
603+
$objWriter->startElement('a:srgbClr');
604+
$objWriter->writeAttribute('val', $subject->getAxisX()->getFont()->getColor()->getRGB());
605+
$objWriter->endElement();
606+
607+
$objWriter->endElement();
608+
609+
// Font - a:latin
610+
$objWriter->startElement('a:latin');
611+
$objWriter->writeAttribute('typeface', $subject->getAxisX()->getFont()->getName());
612+
$objWriter->endElement();
613+
614+
$objWriter->endElement();
615+
584616

585617
$objWriter->endElement();
586618

@@ -689,7 +721,38 @@ protected function writePlotArea(XMLWriter $objWriter, PlotArea $subject, Chart
689721
$objWriter->startElement('a:pPr');
690722

691723
// a:defRPr
692-
$objWriter->writeElement('a:defRPr', null);
724+
$objWriter->startElement('a:defRPr');
725+
726+
$objWriter->writeAttribute('b', ($subject->getAxisY()->getFont()->isBold() ? 'true' : 'false'));
727+
$objWriter->writeAttribute('i', ($subject->getAxisY()->getFont()->isItalic() ? 'true' : 'false'));
728+
$objWriter->writeAttribute('strike', ($subject->getAxisY()->getFont()->isStrikethrough() ? 'sngStrike' : 'noStrike'));
729+
$objWriter->writeAttribute('sz', ($subject->getAxisY()->getFont()->getSize() * 100));
730+
$objWriter->writeAttribute('u', $subject->getAxisY()->getFont()->getUnderline());
731+
732+
if ($subject->getAxisY()->getFont()->isSuperScript() || $subject->getAxisY()->getFont()->isSubScript()) {
733+
if ($subject->getAxisY()->getFont()->isSuperScript()) {
734+
$objWriter->writeAttribute('baseline', '30000');
735+
} elseif ($subject->getAxisY()->getFont()->isSubScript()) {
736+
$objWriter->writeAttribute('baseline', '-25000');
737+
}
738+
}
739+
740+
// Font - a:solidFill
741+
$objWriter->startElement('a:solidFill');
742+
743+
// a:srgbClr
744+
$objWriter->startElement('a:srgbClr');
745+
$objWriter->writeAttribute('val', $subject->getAxisY()->getFont()->getColor()->getRGB());
746+
$objWriter->endElement();
747+
748+
$objWriter->endElement();
749+
750+
// Font - a:latin
751+
$objWriter->startElement('a:latin');
752+
$objWriter->writeAttribute('typeface', $subject->getAxisY()->getFont()->getName());
753+
$objWriter->endElement();
754+
755+
$objWriter->endElement();
693756

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

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

Lines changed: 24 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -17,120 +17,58 @@
1717

1818
namespace PhpOffice\PhpPresentation\Tests\Shape\Chart;
1919

20-
use PhpOffice\PhpPresentation\Shape\Chart\Legend;
21-
use PhpOffice\PhpPresentation\Style\Alignment;
22-
use PhpOffice\PhpPresentation\Style\Border;
23-
use PhpOffice\PhpPresentation\Style\Fill;
20+
use PhpOffice\PhpPresentation\Shape\Chart\Axis;
2421
use PhpOffice\PhpPresentation\Style\Font;
2522

2623
/**
27-
* Test class for Legend element
24+
* Test class for Axis element
2825
*
29-
* @coversDefaultClass PhpOffice\PhpPresentation\Shape\Chart\Legend
26+
* @coversDefaultClass PhpOffice\PhpPresentation\Shape\Chart\Axis
3027
*/
3128
class AxisTest extends \PHPUnit_Framework_TestCase
3229
{
3330
public function testConstruct()
3431
{
35-
$object = new Legend();
32+
$object = new Axis();
3633

34+
$this->assertEquals('Axis Title', $object->getTitle());
3735
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont());
38-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Border', $object->getBorder());
39-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Fill', $object->getFill());
40-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->getAlignment());
4136
}
4237

43-
public function testAlignment()
38+
public function testFormatCode()
4439
{
45-
$object = new Legend();
46-
47-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setAlignment(new Alignment()));
48-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->getAlignment());
49-
}
50-
51-
public function testFont()
52-
{
53-
$object = new Legend();
54-
55-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setFont());
56-
$this->assertNull($object->getFont());
57-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setFont(new Font()));
58-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont());
40+
$object = new Axis();
41+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFormatCode());
42+
$this->assertEquals('', $object->getFormatCode());
43+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFormatCode('AAAA'));
44+
$this->assertEquals('AAAA', $object->getFormatCode());
5945
}
6046

6147
public function testHashIndex()
6248
{
63-
$object = new Legend();
49+
$object = new Axis();
6450
$value = rand(1, 100);
6551

6652
$this->assertEmpty($object->getHashIndex());
67-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setHashIndex($value));
53+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setHashIndex($value));
6854
$this->assertEquals($value, $object->getHashIndex());
6955
}
7056

71-
public function testHeight()
72-
{
73-
$object = new Legend();
74-
$value = rand(0, 100);
75-
76-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setHeight());
77-
$this->assertEquals(0, $object->getHeight());
78-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setHeight($value));
79-
$this->assertEquals($value, $object->getHeight());
80-
}
81-
82-
public function testOffsetX()
83-
{
84-
$object = new Legend();
85-
$value = rand(0, 100);
86-
87-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setOffsetX());
88-
$this->assertEquals(0, $object->getOffsetX());
89-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setOffsetX($value));
90-
$this->assertEquals($value, $object->getOffsetX());
91-
}
92-
93-
public function testOffsetY()
94-
{
95-
$object = new Legend();
96-
$value = rand(0, 100);
97-
98-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setOffsetY());
99-
$this->assertEquals(0, $object->getOffsetY());
100-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setOffsetY($value));
101-
$this->assertEquals($value, $object->getOffsetY());
102-
}
103-
104-
public function testPosition()
105-
{
106-
$object = new Legend();
107-
108-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setPosition());
109-
$this->assertEquals(Legend::POSITION_RIGHT, $object->getPosition());
110-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setPosition(Legend::POSITION_BOTTOM));
111-
$this->assertEquals(Legend::POSITION_BOTTOM, $object->getPosition());
112-
}
113-
114-
public function testVisible()
57+
public function testTitle()
11558
{
116-
$object = new Legend();
117-
118-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setVisible());
119-
$this->assertTrue($object->isVisible());
120-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setVisible(true));
121-
$this->assertTrue($object->isVisible());
122-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setVisible(false));
123-
$this->assertFalse($object->isVisible());
59+
$object = new Axis();
60+
$this->assertEquals('Axis Title', $object->getTitle());
61+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setTitle('AAAA'));
62+
$this->assertEquals('AAAA', $object->getTitle());
12463
}
12564

126-
public function testWidth()
65+
public function testFont()
12766
{
128-
$object = new Legend();
129-
$value = rand(0, 100);
67+
$object = new Axis();
13068

131-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setWidth());
132-
$this->assertEquals(0, $object->getWidth());
133-
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Legend', $object->setWidth($value));
134-
$this->assertEquals($value, $object->getWidth());
69+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFont());
70+
$this->assertNull($object->getFont());
71+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->setFont(new Font()));
72+
$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont());
13573
}
13674
}

0 commit comments

Comments
 (0)