Skip to content

Commit 7bd91a4

Browse files
authored
Merge pull request #402 from Progi1984/issue355
#355 : Doughnut Chart
2 parents 097d50a + a35c00d commit 7bd91a4

File tree

11 files changed

+645
-102
lines changed

11 files changed

+645
-102
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ php:
1111

1212
matrix:
1313
allow_failures:
14-
- php: 7.1
1514
- php: hhvm
1615

1716
env:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
- PowerPoint2007 : Text is subscripted when set superscript to false - @qmachard GH-360
77

88
### Changes
9+
- PHP 7.1 is now supported - @Progi1984 GH-355
910

1011
### Features
12+
- ODPresentation Writer : Support for the position of Legend - @Progi1984 GH-355
13+
- ODPresentation Writer : Support for DoughnutChart - @Progi1984 GH-355
14+
- PowerPoint2007 Writer : Support for DoughnutChart - @Progi1984 GH-355
1115

1216
## 0.9.0 - 2017-07-05
1317

samples/Sample_05_Chart.php

Lines changed: 85 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,23 @@ function fnSlide_BarStacked(PhpPresentation $objPHPPresentation) {
172172
echo date( 'H:i:s' ) . ' Create a stacked bar chart (that should be inserted in a chart shape)' . EOL;
173173
$StackedBarChart = new Bar();
174174
$series1 = new Series( '2009', $series1Data );
175-
$series1->setShowSeriesName( false );
175+
$series1->setShowSeriesName(false);
176176
$series1->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF4F81BD' ) );
177177
$series1->getFont()->getColor()->setRGB( '00FF00' );
178-
$series1->setShowValue( true );
179-
$series1->setShowPercentage( false );
178+
$series1->setShowValue(true);
179+
$series1->setShowPercentage(false);
180180
$series2 = new Series( '2010', $series2Data );
181-
$series2->setShowSeriesName( false );
181+
$series2->setShowSeriesName(false);
182182
$series2->getFont()->getColor()->setRGB( 'FF0000' );
183183
$series2->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FFC0504D' ) );
184-
$series2->setShowValue( true );
185-
$series2->setShowPercentage( false );
184+
$series2->setShowValue(true);
185+
$series2->setShowPercentage(false);
186186
$series3 = new Series( '2011', $series3Data );
187-
$series3->setShowSeriesName( false );
187+
$series3->setShowSeriesName(false);
188188
$series3->getFont()->getColor()->setRGB( 'FF0000' );
189189
$series3->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF804DC0' ) );
190-
$series3->setShowValue( true );
191-
$series3->setShowPercentage( false );
190+
$series3->setShowValue(true);
191+
$series3->setShowPercentage(false);
192192
$StackedBarChart->addSeries( $series1 );
193193
$StackedBarChart->addSeries( $series2 );
194194
$StackedBarChart->addSeries( $series3 );
@@ -197,7 +197,7 @@ function fnSlide_BarStacked(PhpPresentation $objPHPPresentation) {
197197
echo date( 'H:i:s' ) . ' Create a shape (chart)' . EOL;
198198
$shape = $currentSlide->createChartShape();
199199
$shape->setName( 'PHPPresentation Monthly Downloads' )
200-
->setResizeProportional( false )
200+
->setResizeProportional(false)
201201
->setHeight( 550 )
202202
->setWidth( 700 )
203203
->setOffsetX( 120 )
@@ -206,13 +206,13 @@ function fnSlide_BarStacked(PhpPresentation $objPHPPresentation) {
206206
$shape->setFill( $oFill );
207207
$shape->getBorder()->setLineStyle( Border::LINE_SINGLE );
208208
$shape->getTitle()->setText( 'PHPPresentation Monthly Downloads' );
209-
$shape->getTitle()->getFont()->setItalic( true );
209+
$shape->getTitle()->getFont()->setItalic(true);
210210
$shape->getTitle()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_RIGHT );
211211
$shape->getPlotArea()->getAxisX()->setTitle( 'Month' );
212212
$shape->getPlotArea()->getAxisY()->setTitle( 'Downloads' );
213213
$shape->getPlotArea()->setType( $StackedBarChart );
214214
$shape->getLegend()->getBorder()->setLineStyle( Border::LINE_SINGLE );
215-
$shape->getLegend()->getFont()->setItalic( true );
215+
$shape->getLegend()->getFont()->setItalic(true);
216216
}
217217

218218
function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
@@ -245,26 +245,26 @@ function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
245245
echo date( 'H:i:s' ) . ' Create a percent stacked horizontal bar chart (that should be inserted in a chart shape)' . EOL;
246246
$PercentStackedBarChartHoriz = new Bar();
247247
$series1 = new Series( '2009', $series1Data );
248-
$series1->setShowSeriesName( false );
248+
$series1->setShowSeriesName(false);
249249
$series1->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF4F81BD' ) );
250250
$series1->getFont()->getColor()->setRGB( '00FF00' );
251-
$series1->setShowValue( true );
252-
$series1->setShowPercentage( false );
251+
$series1->setShowValue(true);
252+
$series1->setShowPercentage(false);
253253
// Set Data Label Format For Chart To Display Percent
254254
$series1->setDlblNumFormat( '#%' );
255255
$series2 = new Series( '2010', $series2Data );
256-
$series2->setShowSeriesName( false );
256+
$series2->setShowSeriesName(false);
257257
$series2->getFont()->getColor()->setRGB( 'FF0000' );
258258
$series2->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FFC0504D' ) );
259-
$series2->setShowValue( true );
260-
$series2->setShowPercentage( false );
259+
$series2->setShowValue(true);
260+
$series2->setShowPercentage(false);
261261
$series2->setDlblNumFormat( '#%' );
262262
$series3 = new Series( '2011', $series3Data );
263-
$series3->setShowSeriesName( false );
263+
$series3->setShowSeriesName(false);
264264
$series3->getFont()->getColor()->setRGB( 'FF0000' );
265265
$series3->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF804DC0' ) );
266-
$series3->setShowValue( true );
267-
$series3->setShowPercentage( false );
266+
$series3->setShowValue(true);
267+
$series3->setShowPercentage(false);
268268
$series3->setDlblNumFormat( '#%' );
269269
$PercentStackedBarChartHoriz->addSeries( $series1 );
270270
$PercentStackedBarChartHoriz->addSeries( $series2 );
@@ -275,7 +275,7 @@ function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
275275
echo date( 'H:i:s' ) . ' Create a shape (chart)' . EOL;
276276
$shape = $currentSlide->createChartShape();
277277
$shape->setName( 'PHPPresentation Monthly Downloads' )
278-
->setResizeProportional( false )
278+
->setResizeProportional(false)
279279
->setHeight( 550 )
280280
->setWidth( 700 )
281281
->setOffsetX( 120 )
@@ -284,13 +284,13 @@ function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
284284
$shape->setFill( $oFill );
285285
$shape->getBorder()->setLineStyle( Border::LINE_SINGLE );
286286
$shape->getTitle()->setText( 'PHPPresentation Monthly Downloads' );
287-
$shape->getTitle()->getFont()->setItalic( true );
287+
$shape->getTitle()->getFont()->setItalic(true);
288288
$shape->getTitle()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_RIGHT );
289289
$shape->getPlotArea()->getAxisX()->setTitle( 'Month' );
290290
$shape->getPlotArea()->getAxisY()->setTitle( 'Downloads' );
291291
$shape->getPlotArea()->setType( $PercentStackedBarChartHoriz );
292292
$shape->getLegend()->getBorder()->setLineStyle( Border::LINE_SINGLE );
293-
$shape->getLegend()->getFont()->setItalic( true );
293+
$shape->getLegend()->getFont()->setItalic(true);
294294
}
295295

296296
function fnSlide_Bar3D(PhpPresentation $objPHPPresentation) {
@@ -384,6 +384,61 @@ function fnSlide_Bar3DHorizontal(PhpPresentation $objPHPPresentation) {
384384
$shape->getLegend()->getFont()->setItalic(true);
385385
}
386386

387+
function fnSlide_Doughnut(PhpPresentation $objPHPPresentation)
388+
{
389+
global $oFill;
390+
global $oShadow;
391+
392+
// Create templated slide
393+
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;
394+
$currentSlide = createTemplatedSlide($objPHPPresentation);
395+
396+
// Generate sample data for second chart
397+
echo date('H:i:s') . ' Generate sample data for chart' . EOL;
398+
$seriesData = array('Monday' => 18, 'Tuesday' => 23, 'Wednesday' => 14, 'Thursday' => 12, 'Friday' => 20, 'Saturday' => 8, 'Sunday' => 10);
399+
400+
// Create a doughnut chart (that should be inserted in a shape)
401+
echo date('H:i:s') . ' Create a non-3D Doughnut chart (that should be inserted in a chart shape)' . EOL;
402+
$doughnutChart = new \PhpOffice\PhpPresentation\Shape\Chart\Type\Doughnut();
403+
$doughnutChart->setHoleSize(43);
404+
$series = new Series('Downloads', $seriesData);
405+
$series->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF7CB5EC'));
406+
$series->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF434348'));
407+
$series->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF90ED7D'));
408+
$series->getDataPointFill(3)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFF7A35C'));
409+
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8085E9'));
410+
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFF15C80'));
411+
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFE4D354'));
412+
$series->setShowPercentage(true);
413+
$series->setShowValue(false);
414+
$series->setShowSeriesName(false);
415+
$series->setShowCategoryName(true);
416+
$series->setDlblNumFormat('%d');
417+
$series->setSeparator(' > ');
418+
$series->getFont()->getColor()->setRGB('FFFF00');
419+
$series->getFont()->setBold(true);
420+
$doughnutChart->addSeries($series);
421+
422+
// Create a shape (chart)
423+
echo date('H:i:s') . ' Create a shape (chart)' . EOL;
424+
$shape = $currentSlide->createChartShape();
425+
$shape->setName('PHPPresentation Daily Downloads')
426+
->setResizeProportional(false)
427+
->setHeight(550)
428+
->setWidth(700)
429+
->setOffsetX(120)
430+
->setOffsetY(80);
431+
$shape->setShadow($oShadow);
432+
$shape->setFill($oFill);
433+
$shape->getBorder()->setLineStyle(Border::LINE_SINGLE);
434+
$shape->getTitle()->setText('PHPPresentation Daily Downloads');
435+
$shape->getTitle()->getFont()->setItalic(true);
436+
$shape->getPlotArea()->setType($doughnutChart);
437+
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
438+
$shape->getLegend()->getFont()->setItalic(true);
439+
$shape->getLegend()->setPosition(\PhpOffice\PhpPresentation\Shape\Chart\Legend::POSITION_LEFT);
440+
}
441+
387442
function fnSlide_Pie3D(PhpPresentation $objPHPPresentation) {
388443
global $oFill;
389444
global $oShadow;
@@ -456,10 +511,10 @@ function fnSlide_Pie(PhpPresentation $objPHPPresentation) {
456511
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8085E9'));
457512
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFF15C80'));
458513
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFE4D354'));
459-
$series->setShowPercentage( true );
460-
$series->setShowValue( false );
461-
$series->setShowSeriesName( false );
462-
$series->setShowCategoryName( true );
514+
$series->setShowPercentage(true);
515+
$series->setShowValue(false);
516+
$series->setShowSeriesName(false);
517+
$series->setShowCategoryName(true);
463518
$series->setDlblNumFormat('%d');
464519
$pieChart->addSeries($series);
465520

@@ -563,6 +618,8 @@ function fnSlide_Scatter(PhpPresentation $objPHPPresentation) {
563618

564619
fnSlide_Bar3DHorizontal($objPHPPresentation);
565620

621+
fnSlide_Doughnut($objPHPPresentation);
622+
566623
fnSlide_Pie3D($objPHPPresentation);
567624

568625
fnSlide_Pie($objPHPPresentation);

src/PhpPresentation/Shape/AbstractGraphic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function isResizeProportional()
226226
* Set ResizeProportional
227227
*
228228
* @param boolean $pValue
229-
* @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
229+
* @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
230230
*/
231231
public function setResizeProportional($pValue = true)
232232
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* This file is part of PHPPresentation - A pure PHP library for reading and writing
4+
* presentations documents.
5+
*
6+
* PHPPresentation is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPPresentation
14+
* @copyright 2009-2015 PHPPresentation contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpPresentation\Shape\Chart\Type;
19+
20+
use PhpOffice\PhpPresentation\ComparableInterface;
21+
22+
/**
23+
* self
24+
*/
25+
class Doughnut extends AbstractTypePie implements ComparableInterface
26+
{
27+
/**
28+
* Hole Size
29+
* @var int
30+
*/
31+
protected $holeSize = 50;
32+
33+
/**
34+
* @return int
35+
*/
36+
public function getHoleSize()
37+
{
38+
return $this->holeSize;
39+
}
40+
41+
/**
42+
* @param int $holeSize
43+
* @return Doughnut
44+
* @link https://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.charts.holesize(v=office.14).aspx
45+
*/
46+
public function setHoleSize($holeSize = 50)
47+
{
48+
if ($holeSize < 10) {
49+
$holeSize = 10;
50+
}
51+
if ($holeSize > 90) {
52+
$holeSize = 90;
53+
}
54+
$this->holeSize = $holeSize;
55+
return $this;
56+
}
57+
58+
/**
59+
* Get hash code
60+
*
61+
* @return string Hash code
62+
*/
63+
public function getHashCode()
64+
{
65+
return md5(parent::getHashCode() . __CLASS__);
66+
}
67+
}

src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpOffice\PhpPresentation\Shape\Chart\Type\Area;
1414
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar;
1515
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D;
16+
use PhpOffice\PhpPresentation\Shape\Chart\Type\Doughnut;
1617
use PhpOffice\PhpPresentation\Shape\Chart\Type\Line;
1718
use PhpOffice\PhpPresentation\Shape\Chart\Type\Pie3D;
1819
use PhpOffice\PhpPresentation\Shape\Chart\Type\Scatter;
@@ -182,8 +183,11 @@ protected function writeContentPart(Chart $chart)
182183
$this->xmlContent->writeAttribute('chart:style-name', 'styleChart');
183184
$this->xmlContent->writeAttributeIf($chartType instanceof Area, 'chart:class', 'chart:area');
184185
$this->xmlContent->writeAttributeIf($chartType instanceof AbstractTypeBar, 'chart:class', 'chart:bar');
186+
if (!($chartType instanceof Doughnut)) {
187+
$this->xmlContent->writeAttributeIf($chartType instanceof AbstractTypePie, 'chart:class', 'chart:circle');
188+
}
189+
$this->xmlContent->writeAttributeIf($chartType instanceof Doughnut, 'chart:class', 'chart:ring');
185190
$this->xmlContent->writeAttributeIf($chartType instanceof Line, 'chart:class', 'chart:line');
186-
$this->xmlContent->writeAttributeIf($chartType instanceof AbstractTypePie, 'chart:class', 'chart:circle');
187191
$this->xmlContent->writeAttributeIf($chartType instanceof Scatter, 'chart:class', 'chart:scatter');
188192

189193
//**** Title ****
@@ -437,7 +441,25 @@ private function writeLegend(Chart $chart)
437441
{
438442
// chart:legend
439443
$this->xmlContent->startElement('chart:legend');
440-
$this->xmlContent->writeAttribute('chart:legend-position', 'end');
444+
switch ($chart->getLegend()->getPosition()) {
445+
case Chart\Legend::POSITION_BOTTOM:
446+
$position = 'bottom';
447+
break;
448+
case Chart\Legend::POSITION_LEFT:
449+
$position = 'start';
450+
break;
451+
case Chart\Legend::POSITION_TOP:
452+
$position = 'top';
453+
break;
454+
case Chart\Legend::POSITION_TOPRIGHT:
455+
$position = 'top-end';
456+
break;
457+
case Chart\Legend::POSITION_RIGHT:
458+
default:
459+
$position = 'end';
460+
break;
461+
}
462+
$this->xmlContent->writeAttribute('chart:legend-position', $position);
441463
$this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetX()), 3) . 'cm');
442464
$this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetY()), 3) . 'cm');
443465
$this->xmlContent->writeAttribute('style:legend-expansion', 'high');
@@ -455,6 +477,11 @@ private function writeLegendStyle(Chart $chart)
455477
$this->xmlContent->startElement('style:style');
456478
$this->xmlContent->writeAttribute('style:name', 'styleLegend');
457479
$this->xmlContent->writeAttribute('style:family', 'chart');
480+
// style:chart-properties
481+
$this->xmlContent->startElement('style:chart-properties');
482+
$this->xmlContent->writeAttribute('chart:auto-position', 'true');
483+
// > style:chart-properties
484+
$this->xmlContent->endElement();
458485
// style:text-properties
459486
$this->xmlContent->startElement('style:text-properties');
460487
$this->xmlContent->writeAttribute('fo:color', '#'.$chart->getLegend()->getFont()->getColor()->getRGB());
@@ -712,6 +739,19 @@ private function writeSeriesStyle(Chart $chart, Chart\Series $series)
712739
$this->xmlContent->writeAttribute('chart:symbol-height', $symbolSize.'cm');
713740
}
714741
}
742+
743+
$separator = $series->getSeparator();
744+
if (!empty($separator)) {
745+
// style:chart-properties/chart:label-separator
746+
$this->xmlContent->startElement('chart:label-separator');
747+
if ($separator == PHP_EOL) {
748+
$this->xmlContent->writeRaw('<text:p><text:line-break /></text:p>');
749+
} else {
750+
$this->xmlContent->writeElement('text:p', $separator);
751+
}
752+
$this->xmlContent->endElement();
753+
}
754+
715755
// > style:chart-properties
716756
$this->xmlContent->endElement();
717757
// style:graphic-properties

src/PhpPresentation/Writer/PowerPoint2007/LayoutPack/AbstractLayoutPack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ abstract class AbstractLayoutPack
5858
*
5959
* @var array
6060
*/
61-
protected $themes = '';
61+
protected $themes = array();
6262

6363
/**
6464
* Theme relations

0 commit comments

Comments
 (0)