Skip to content

Commit 985c508

Browse files
committed
Merge pull request #191 from Progi1984/issue129
#129 : PR
2 parents cc2b16c + 362528c commit 985c508

File tree

12 files changed

+607
-301
lines changed

12 files changed

+607
-301
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
### Features
1515
- ODPresentation & PowerPoint2007 Writer : Add support for Comment - @Progi1984 GH-116
1616
- ODPresentation & PowerPoint2007 Writer : Thumbnail of the presentation - @Progi1984 GH-125
17+
- ODPresentation & PowerPoint2007 Writer : Add support for Gridlines in Chart - @Progi1984 GH-129
1718
- ODPresentation & PowerPoint2007 Writer : Marker of Series in Line & Scatter chart is customizable - @Progi1984 GH-169
1819
- ODPresentation & PowerPoint2007 Writer : Outline of Series in Line & Scatter chart is customizable - @Progi1984 GH-169
1920
- PowerPoint2007 & Serialized Writer : Support for Zip Adapter - @Progi1984 GH-176

docs/shapes_chart.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ Example:
1414
Parts
1515
-----
1616

17+
Axis
18+
^^^^
19+
20+
You can define gridlines (minor and major) for each axis (X & Y).
21+
For each gridline, you can custom the width (in points), the fill type and the fill color.
22+
23+
.. code-block:: php
24+
25+
use \PhpOffice\PhpPresentation\Shape\Chart\Gridlines;
26+
27+
$oLine = new Line();
28+
29+
$oGridLines = new Gridlines();
30+
$oGridLines->getOutline()->setWidth(10);
31+
$oGridLines->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE));
32+
33+
$oShape = $oSlide->createChartShape();
34+
$oShape->getPlotArea()->setType($oLine);
35+
$oShape->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines);
36+
1737
Title
1838
^^^^^
1939

@@ -22,8 +42,8 @@ For hiding it, you define its visibility to false.
2242

2343
.. code-block:: php
2444
25-
$chartShape = $slide->createChartShape();
2645
$oLine = new Line();
46+
$oShape = $slide->createChartShape();
2747
$oShape->getPlotArea()->setType($oLine);
2848
// Hide the title
2949
$oShape->getTitle()->setVisible(false);

samples/Sample_05_Chart_Line.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,33 @@
125125
$shape2->getPlotArea()->getAxisY()->setFormatCode('#,##0');
126126
$currentSlide->addShape($shape2);
127127

128+
129+
// Create templated slide
130+
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;
131+
$currentSlide = createTemplatedSlide($objPHPPresentation);
132+
133+
// Create a line chart (that should be inserted in a shape)
134+
echo date('H:i:s') . ' Create a line chart (that should be inserted in a chart shape)' . EOL;
135+
$lineChart3 = clone $lineChart;
136+
137+
$oGridLines1 = new \PhpOffice\PhpPresentation\Shape\Chart\Gridlines();
138+
$oGridLines1->getOutline()->setWidth(10);
139+
$oGridLines1->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_BLUE));
140+
141+
$oGridLines2 = new \PhpOffice\PhpPresentation\Shape\Chart\Gridlines();
142+
$oGridLines2->getOutline()->setWidth(1);
143+
$oGridLines2->getOutline()->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color(Color::COLOR_DARKGREEN));
144+
145+
// Create a shape (chart)
146+
echo date('H:i:s') . ' Create a shape (chart3)' . EOL;
147+
echo date('H:i:s') . ' Feature : Gridlines' . EOL;
148+
$shape3 = clone $shape;
149+
$shape3->setName('Shape 3');
150+
$shape3->getTitle()->setText('Chart with Gridlines');
151+
$shape3->getPlotArea()->setType($lineChart3);
152+
$shape3->getPlotArea()->getAxisX()->setMajorGridlines($oGridLines1);
153+
$shape3->getPlotArea()->getAxisY()->setMinorGridlines($oGridLines2);
154+
$currentSlide->addShape($shape3);
128155
// Save file
129156
echo EOL . write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
130157

samples/results/.gitkeep

Whitespace-only changes.

src/PhpPresentation/Shape/Chart/Axis.php

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

2020
use PhpOffice\PhpPresentation\ComparableInterface;
21+
use PhpOffice\PhpPresentation\Shape\Chart\Gridlines;
2122
use PhpOffice\PhpPresentation\Style\Font;
2223

2324
/**
2425
* \PhpOffice\PhpPresentation\Shape\Chart\Axis
2526
*/
2627
class Axis implements ComparableInterface
2728
{
29+
const AXIS_X = 'x';
30+
const AXIS_Y = 'y';
31+
2832
/**
2933
* Title
3034
*
@@ -46,6 +50,16 @@ class Axis implements ComparableInterface
4650
*/
4751
private $font;
4852

53+
/**
54+
* @var Gridlines
55+
*/
56+
protected $majorGridlines;
57+
58+
/**
59+
* @var Gridlines
60+
*/
61+
protected $minorGridlines;
62+
4963
/**
5064
* Create a new \PhpOffice\PhpPresentation\Shape\Chart\Axis instance
5165
*
@@ -126,6 +140,42 @@ public function setFormatCode($value = '')
126140
return $this;
127141
}
128142

143+
/**
144+
* @return Gridlines
145+
*/
146+
public function getMajorGridlines()
147+
{
148+
return $this->majorGridlines;
149+
}
150+
151+
/**
152+
* @param Gridlines $majorGridlines
153+
* @return Axis
154+
*/
155+
public function setMajorGridlines(Gridlines $majorGridlines)
156+
{
157+
$this->majorGridlines = $majorGridlines;
158+
return $this;
159+
}
160+
161+
/**
162+
* @return Gridlines
163+
*/
164+
public function getMinorGridlines()
165+
{
166+
return $this->minorGridlines;
167+
}
168+
169+
/**
170+
* @param Gridlines $minorGridlines
171+
* @return Axis
172+
*/
173+
public function setMinorGridlines(Gridlines $minorGridlines)
174+
{
175+
$this->minorGridlines = $minorGridlines;
176+
return $this;
177+
}
178+
129179
/**
130180
* Get hash code
131181
*
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpPresentation\Shape\Chart;
4+
5+
use PhpOffice\PhpPresentation\Style\Outline;
6+
7+
class Gridlines
8+
{
9+
/**
10+
* @var Outline
11+
*/
12+
protected $outline;
13+
14+
public function __construct()
15+
{
16+
$this->outline = new Outline();
17+
}
18+
19+
/**
20+
* @return Outline
21+
*/
22+
public function getOutline()
23+
{
24+
return $this->outline;
25+
}
26+
27+
/**
28+
* @param Outline $outline
29+
* @return Gridlines
30+
*/
31+
public function setOutline(Outline $outline)
32+
{
33+
$this->outline = $outline;
34+
return $this;
35+
}
36+
}

src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,20 +252,29 @@ private function writeAxis(Chart $chart)
252252
$this->xmlContent->writeAttribute('chart:name', 'primary-x');
253253
$this->xmlContent->writeAttribute('chartooo:axis-type', 'text');
254254
$this->xmlContent->writeAttribute('chart:style-name', 'styleAxisX');
255-
// chart:categories
255+
// chart:axis > chart:categories
256256
$this->xmlContent->startElement('chart:categories');
257257
$this->xmlContent->writeAttribute('table:cell-range-address', 'table-local.$A$2:.$A$'.($this->numData+1));
258-
// > chart:categories
259258
$this->xmlContent->endElement();
260-
// > chart:axis
259+
// chart:axis > chart:grid
260+
$this->writeGridline($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor', 'major');
261+
// chart:axis > chart:grid
262+
$this->writeGridline($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor', 'minor');
263+
// ##chart:axis
261264
$this->xmlContent->endElement();
265+
262266
// chart:axis
263267
$this->xmlContent->startElement('chart:axis');
264268
$this->xmlContent->writeAttribute('chart:dimension', 'y');
265269
$this->xmlContent->writeAttribute('chart:name', 'primary-y');
266270
$this->xmlContent->writeAttribute('chart:style-name', 'styleAxisY');
267-
// > chart:axis
271+
// chart:axis > chart:grid
272+
$this->writeGridline($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor', 'major');
273+
// chart:axis > chart:grid
274+
$this->writeGridline($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor', 'minor');
275+
// ##chart:axis
268276
$this->xmlContent->endElement();
277+
269278
if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) {
270279
// chart:axis
271280
$this->xmlContent->startElement('chart:axis');
@@ -275,77 +284,117 @@ private function writeAxis(Chart $chart)
275284
$this->xmlContent->endElement();
276285
}
277286
}
287+
288+
protected function writeGridline($oGridlines, $styleName, $chartClass)
289+
{
290+
if (!($oGridlines instanceof Chart\Gridlines)) {
291+
return ;
292+
}
293+
294+
$this->xmlContent->startElement('chart:grid');
295+
$this->xmlContent->writeAttribute('chart:style-name', $styleName);
296+
$this->xmlContent->writeAttribute('chart:class', $chartClass);
297+
$this->xmlContent->endElement();
298+
}
278299

279300
/**
280301
* @param Chart $chart
281302
* @todo Set function in \PhpPresentation\Shape\Chart\Axis for defining width and color of the axis
282303
*/
283-
private function writeAxisStyle(Chart $chart)
304+
protected function writeAxisStyle(Chart $chart)
284305
{
285306
$chartType = $chart->getPlotArea()->getType();
286-
307+
287308
// AxisX
288309
// style:style
289310
$this->xmlContent->startElement('style:style');
290311
$this->xmlContent->writeAttribute('style:name', 'styleAxisX');
291312
$this->xmlContent->writeAttribute('style:family', 'chart');
292-
// style:chart-properties
313+
// style:style > style:chart-properties
293314
$this->xmlContent->startElement('style:chart-properties');
294315
$this->xmlContent->writeAttribute('chart:display-label', 'true');
295316
$this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false');
296317
$this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
297318
if ($chartType instanceof AbstractTypePie) {
298319
$this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
299320
}
300-
// > style:chart-properties
301321
$this->xmlContent->endElement();
302-
// style:text-properties
322+
// style:style > style:text-properties
303323
$this->xmlContent->startElement('style:text-properties');
304324
$this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisX()->getFont()->getColor()->getRGB());
305325
$this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisX()->getFont()->getName());
306326
$this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisX()->getFont()->getSize().'pt');
307327
$this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisX()->getFont()->isItalic() ? 'italic' : 'normal');
308-
// > style:text-properties
309328
$this->xmlContent->endElement();
310-
// style:graphic-properties
329+
// style:style > style:graphic-properties
311330
$this->xmlContent->startElement('style:graphic-properties');
312331
$this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
313332
$this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
314-
// > style:graphic-properties
315333
$this->xmlContent->endElement();
316-
// > style:style
334+
// ##style:style
317335
$this->xmlContent->endElement();
336+
337+
// AxisX GridLines Major
338+
$this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor');
339+
340+
// AxisX GridLines Minor
341+
$this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor');
318342

319343
// AxisY
320344
// style:style
321345
$this->xmlContent->startElement('style:style');
322346
$this->xmlContent->writeAttribute('style:name', 'styleAxisY');
323347
$this->xmlContent->writeAttribute('style:family', 'chart');
324-
// style:chart-properties
348+
// style:style > style:chart-properties
325349
$this->xmlContent->startElement('style:chart-properties');
326350
$this->xmlContent->writeAttribute('chart:display-label', 'true');
327351
$this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false');
328352
$this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
329353
if ($chartType instanceof AbstractTypePie) {
330354
$this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
331355
}
332-
// > style:chart-properties
333356
$this->xmlContent->endElement();
334-
// style:text-properties
357+
// style:style > style:text-properties
335358
$this->xmlContent->startElement('style:text-properties');
336359
$this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisY()->getFont()->getColor()->getRGB());
337360
$this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisY()->getFont()->getName());
338361
$this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisY()->getFont()->getSize().'pt');
339362
$this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisY()->getFont()->isItalic() ? 'italic' : 'normal');
340-
// > style:text-properties
341363
$this->xmlContent->endElement();
342364
// style:graphic-properties
343365
$this->xmlContent->startElement('style:graphic-properties');
344366
$this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
345367
$this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
346-
// > style:graphic-properties
347368
$this->xmlContent->endElement();
348-
// > style:style
369+
// ## style:style
370+
$this->xmlContent->endElement();
371+
372+
// AxisY GridLines Major
373+
$this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor');
374+
375+
// AxisY GridLines Minor
376+
$this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor');
377+
}
378+
379+
/**
380+
* @param Chart\Gridlines $oGridlines
381+
* @param string $styleName
382+
*/
383+
protected function writeGridlineStyle($oGridlines, $styleName)
384+
{
385+
if (!($oGridlines instanceof Chart\Gridlines)) {
386+
return;
387+
}
388+
// style:style
389+
$this->xmlContent->startElement('style:style');
390+
$this->xmlContent->writeAttribute('style:name', $styleName);
391+
$this->xmlContent->writeAttribute('style:family', 'chart');
392+
// style:style > style:graphic-properties
393+
$this->xmlContent->startElement('style:graphic-properties');
394+
$this->xmlContent->writeAttribute('svg:stroke-width', number_format(CommonDrawing::pointsToCentimeters($oGridlines->getOutline()->getWidth()), 2, '.', '').'cm');
395+
$this->xmlContent->writeAttribute('svg:stroke-color', '#'.$oGridlines->getOutline()->getFill()->getStartColor()->getRGB());
396+
$this->xmlContent->endElement();
397+
// ##style:style
349398
$this->xmlContent->endElement();
350399
}
351400

0 commit comments

Comments
 (0)