Skip to content

#355 : Doughnut Chart #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ php:

matrix:
allow_failures:
- php: 7.1
- php: hhvm

env:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
- PowerPoint2007 : Text is subscripted when set superscript to false - @qmachard GH-360

### Changes
- PHP 7.1 is now supported - @Progi1984 GH-355

### Features
- ODPresentation Writer : Support for the position of Legend - @Progi1984 GH-355
- ODPresentation Writer : Support for DoughnutChart - @Progi1984 GH-355
- PowerPoint2007 Writer : Support for DoughnutChart - @Progi1984 GH-355

## 0.9.0 - 2017-07-05

Expand Down
113 changes: 85 additions & 28 deletions samples/Sample_05_Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,23 @@ function fnSlide_BarStacked(PhpPresentation $objPHPPresentation) {
echo date( 'H:i:s' ) . ' Create a stacked bar chart (that should be inserted in a chart shape)' . EOL;
$StackedBarChart = new Bar();
$series1 = new Series( '2009', $series1Data );
$series1->setShowSeriesName( false );
$series1->setShowSeriesName(false);
$series1->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF4F81BD' ) );
$series1->getFont()->getColor()->setRGB( '00FF00' );
$series1->setShowValue( true );
$series1->setShowPercentage( false );
$series1->setShowValue(true);
$series1->setShowPercentage(false);
$series2 = new Series( '2010', $series2Data );
$series2->setShowSeriesName( false );
$series2->setShowSeriesName(false);
$series2->getFont()->getColor()->setRGB( 'FF0000' );
$series2->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FFC0504D' ) );
$series2->setShowValue( true );
$series2->setShowPercentage( false );
$series2->setShowValue(true);
$series2->setShowPercentage(false);
$series3 = new Series( '2011', $series3Data );
$series3->setShowSeriesName( false );
$series3->setShowSeriesName(false);
$series3->getFont()->getColor()->setRGB( 'FF0000' );
$series3->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF804DC0' ) );
$series3->setShowValue( true );
$series3->setShowPercentage( false );
$series3->setShowValue(true);
$series3->setShowPercentage(false);
$StackedBarChart->addSeries( $series1 );
$StackedBarChart->addSeries( $series2 );
$StackedBarChart->addSeries( $series3 );
Expand All @@ -197,7 +197,7 @@ function fnSlide_BarStacked(PhpPresentation $objPHPPresentation) {
echo date( 'H:i:s' ) . ' Create a shape (chart)' . EOL;
$shape = $currentSlide->createChartShape();
$shape->setName( 'PHPPresentation Monthly Downloads' )
->setResizeProportional( false )
->setResizeProportional(false)
->setHeight( 550 )
->setWidth( 700 )
->setOffsetX( 120 )
Expand All @@ -206,13 +206,13 @@ function fnSlide_BarStacked(PhpPresentation $objPHPPresentation) {
$shape->setFill( $oFill );
$shape->getBorder()->setLineStyle( Border::LINE_SINGLE );
$shape->getTitle()->setText( 'PHPPresentation Monthly Downloads' );
$shape->getTitle()->getFont()->setItalic( true );
$shape->getTitle()->getFont()->setItalic(true);
$shape->getTitle()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_RIGHT );
$shape->getPlotArea()->getAxisX()->setTitle( 'Month' );
$shape->getPlotArea()->getAxisY()->setTitle( 'Downloads' );
$shape->getPlotArea()->setType( $StackedBarChart );
$shape->getLegend()->getBorder()->setLineStyle( Border::LINE_SINGLE );
$shape->getLegend()->getFont()->setItalic( true );
$shape->getLegend()->getFont()->setItalic(true);
}

function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
Expand Down Expand Up @@ -245,26 +245,26 @@ function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
echo date( 'H:i:s' ) . ' Create a percent stacked horizontal bar chart (that should be inserted in a chart shape)' . EOL;
$PercentStackedBarChartHoriz = new Bar();
$series1 = new Series( '2009', $series1Data );
$series1->setShowSeriesName( false );
$series1->setShowSeriesName(false);
$series1->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF4F81BD' ) );
$series1->getFont()->getColor()->setRGB( '00FF00' );
$series1->setShowValue( true );
$series1->setShowPercentage( false );
$series1->setShowValue(true);
$series1->setShowPercentage(false);
// Set Data Label Format For Chart To Display Percent
$series1->setDlblNumFormat( '#%' );
$series2 = new Series( '2010', $series2Data );
$series2->setShowSeriesName( false );
$series2->setShowSeriesName(false);
$series2->getFont()->getColor()->setRGB( 'FF0000' );
$series2->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FFC0504D' ) );
$series2->setShowValue( true );
$series2->setShowPercentage( false );
$series2->setShowValue(true);
$series2->setShowPercentage(false);
$series2->setDlblNumFormat( '#%' );
$series3 = new Series( '2011', $series3Data );
$series3->setShowSeriesName( false );
$series3->setShowSeriesName(false);
$series3->getFont()->getColor()->setRGB( 'FF0000' );
$series3->getFill()->setFillType( Fill::FILL_SOLID )->setStartColor( new Color( 'FF804DC0' ) );
$series3->setShowValue( true );
$series3->setShowPercentage( false );
$series3->setShowValue(true);
$series3->setShowPercentage(false);
$series3->setDlblNumFormat( '#%' );
$PercentStackedBarChartHoriz->addSeries( $series1 );
$PercentStackedBarChartHoriz->addSeries( $series2 );
Expand All @@ -275,7 +275,7 @@ function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
echo date( 'H:i:s' ) . ' Create a shape (chart)' . EOL;
$shape = $currentSlide->createChartShape();
$shape->setName( 'PHPPresentation Monthly Downloads' )
->setResizeProportional( false )
->setResizeProportional(false)
->setHeight( 550 )
->setWidth( 700 )
->setOffsetX( 120 )
Expand All @@ -284,13 +284,13 @@ function fnSlide_BarPercentStacked(PhpPresentation $objPHPPresentation) {
$shape->setFill( $oFill );
$shape->getBorder()->setLineStyle( Border::LINE_SINGLE );
$shape->getTitle()->setText( 'PHPPresentation Monthly Downloads' );
$shape->getTitle()->getFont()->setItalic( true );
$shape->getTitle()->getFont()->setItalic(true);
$shape->getTitle()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_RIGHT );
$shape->getPlotArea()->getAxisX()->setTitle( 'Month' );
$shape->getPlotArea()->getAxisY()->setTitle( 'Downloads' );
$shape->getPlotArea()->setType( $PercentStackedBarChartHoriz );
$shape->getLegend()->getBorder()->setLineStyle( Border::LINE_SINGLE );
$shape->getLegend()->getFont()->setItalic( true );
$shape->getLegend()->getFont()->setItalic(true);
}

function fnSlide_Bar3D(PhpPresentation $objPHPPresentation) {
Expand Down Expand Up @@ -384,6 +384,61 @@ function fnSlide_Bar3DHorizontal(PhpPresentation $objPHPPresentation) {
$shape->getLegend()->getFont()->setItalic(true);
}

function fnSlide_Doughnut(PhpPresentation $objPHPPresentation)
{
global $oFill;
global $oShadow;

// Create templated slide
echo EOL . date('H:i:s') . ' Create templated slide' . EOL;
$currentSlide = createTemplatedSlide($objPHPPresentation);

// Generate sample data for second chart
echo date('H:i:s') . ' Generate sample data for chart' . EOL;
$seriesData = array('Monday' => 18, 'Tuesday' => 23, 'Wednesday' => 14, 'Thursday' => 12, 'Friday' => 20, 'Saturday' => 8, 'Sunday' => 10);

// Create a doughnut chart (that should be inserted in a shape)
echo date('H:i:s') . ' Create a non-3D Doughnut chart (that should be inserted in a chart shape)' . EOL;
$doughnutChart = new \PhpOffice\PhpPresentation\Shape\Chart\Type\Doughnut();
$doughnutChart->setHoleSize(43);
$series = new Series('Downloads', $seriesData);
$series->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF7CB5EC'));
$series->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF434348'));
$series->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF90ED7D'));
$series->getDataPointFill(3)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFF7A35C'));
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8085E9'));
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFF15C80'));
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFE4D354'));
$series->setShowPercentage(true);
$series->setShowValue(false);
$series->setShowSeriesName(false);
$series->setShowCategoryName(true);
$series->setDlblNumFormat('%d');
$series->setSeparator(' > ');
$series->getFont()->getColor()->setRGB('FFFF00');
$series->getFont()->setBold(true);
$doughnutChart->addSeries($series);

// Create a shape (chart)
echo date('H:i:s') . ' Create a shape (chart)' . EOL;
$shape = $currentSlide->createChartShape();
$shape->setName('PHPPresentation Daily Downloads')
->setResizeProportional(false)
->setHeight(550)
->setWidth(700)
->setOffsetX(120)
->setOffsetY(80);
$shape->setShadow($oShadow);
$shape->setFill($oFill);
$shape->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getTitle()->setText('PHPPresentation Daily Downloads');
$shape->getTitle()->getFont()->setItalic(true);
$shape->getPlotArea()->setType($doughnutChart);
$shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
$shape->getLegend()->getFont()->setItalic(true);
$shape->getLegend()->setPosition(\PhpOffice\PhpPresentation\Shape\Chart\Legend::POSITION_LEFT);
}

function fnSlide_Pie3D(PhpPresentation $objPHPPresentation) {
global $oFill;
global $oShadow;
Expand Down Expand Up @@ -456,10 +511,10 @@ function fnSlide_Pie(PhpPresentation $objPHPPresentation) {
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8085E9'));
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFF15C80'));
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFE4D354'));
$series->setShowPercentage( true );
$series->setShowValue( false );
$series->setShowSeriesName( false );
$series->setShowCategoryName( true );
$series->setShowPercentage(true);
$series->setShowValue(false);
$series->setShowSeriesName(false);
$series->setShowCategoryName(true);
$series->setDlblNumFormat('%d');
$pieChart->addSeries($series);

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

fnSlide_Bar3DHorizontal($objPHPPresentation);

fnSlide_Doughnut($objPHPPresentation);

fnSlide_Pie3D($objPHPPresentation);

fnSlide_Pie($objPHPPresentation);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPresentation/Shape/AbstractGraphic.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function isResizeProportional()
* Set ResizeProportional
*
* @param boolean $pValue
* @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
* @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
*/
public function setResizeProportional($pValue = true)
{
Expand Down
67 changes: 67 additions & 0 deletions src/PhpPresentation/Shape/Chart/Type/Doughnut.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* This file is part of PHPPresentation - A pure PHP library for reading and writing
* presentations documents.
*
* PHPPresentation is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
*
* @link https://github.com/PHPOffice/PHPPresentation
* @copyright 2009-2015 PHPPresentation contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpPresentation\Shape\Chart\Type;

use PhpOffice\PhpPresentation\ComparableInterface;

/**
* self
*/
class Doughnut extends AbstractTypePie implements ComparableInterface
{
/**
* Hole Size
* @var int
*/
protected $holeSize = 50;

/**
* @return int
*/
public function getHoleSize()
{
return $this->holeSize;
}

/**
* @param int $holeSize
* @return Doughnut
* @link https://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.charts.holesize(v=office.14).aspx
*/
public function setHoleSize($holeSize = 50)
{
if ($holeSize < 10) {
$holeSize = 10;
}
if ($holeSize > 90) {
$holeSize = 90;
}
$this->holeSize = $holeSize;
return $this;
}

/**
* Get hash code
*
* @return string Hash code
*/
public function getHashCode()
{
return md5(parent::getHashCode() . __CLASS__);
}
}
44 changes: 42 additions & 2 deletions src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpOffice\PhpPresentation\Shape\Chart\Type\Area;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Doughnut;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Line;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Pie3D;
use PhpOffice\PhpPresentation\Shape\Chart\Type\Scatter;
Expand Down Expand Up @@ -182,8 +183,11 @@ protected function writeContentPart(Chart $chart)
$this->xmlContent->writeAttribute('chart:style-name', 'styleChart');
$this->xmlContent->writeAttributeIf($chartType instanceof Area, 'chart:class', 'chart:area');
$this->xmlContent->writeAttributeIf($chartType instanceof AbstractTypeBar, 'chart:class', 'chart:bar');
if (!($chartType instanceof Doughnut)) {
$this->xmlContent->writeAttributeIf($chartType instanceof AbstractTypePie, 'chart:class', 'chart:circle');
}
$this->xmlContent->writeAttributeIf($chartType instanceof Doughnut, 'chart:class', 'chart:ring');
$this->xmlContent->writeAttributeIf($chartType instanceof Line, 'chart:class', 'chart:line');
$this->xmlContent->writeAttributeIf($chartType instanceof AbstractTypePie, 'chart:class', 'chart:circle');
$this->xmlContent->writeAttributeIf($chartType instanceof Scatter, 'chart:class', 'chart:scatter');

//**** Title ****
Expand Down Expand Up @@ -437,7 +441,25 @@ private function writeLegend(Chart $chart)
{
// chart:legend
$this->xmlContent->startElement('chart:legend');
$this->xmlContent->writeAttribute('chart:legend-position', 'end');
switch ($chart->getLegend()->getPosition()) {
case Chart\Legend::POSITION_BOTTOM:
$position = 'bottom';
break;
case Chart\Legend::POSITION_LEFT:
$position = 'start';
break;
case Chart\Legend::POSITION_TOP:
$position = 'top';
break;
case Chart\Legend::POSITION_TOPRIGHT:
$position = 'top-end';
break;
case Chart\Legend::POSITION_RIGHT:
default:
$position = 'end';
break;
}
$this->xmlContent->writeAttribute('chart:legend-position', $position);
$this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetX()), 3) . 'cm');
$this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetY()), 3) . 'cm');
$this->xmlContent->writeAttribute('style:legend-expansion', 'high');
Expand All @@ -455,6 +477,11 @@ private function writeLegendStyle(Chart $chart)
$this->xmlContent->startElement('style:style');
$this->xmlContent->writeAttribute('style:name', 'styleLegend');
$this->xmlContent->writeAttribute('style:family', 'chart');
// style:chart-properties
$this->xmlContent->startElement('style:chart-properties');
$this->xmlContent->writeAttribute('chart:auto-position', 'true');
// > style:chart-properties
$this->xmlContent->endElement();
// style:text-properties
$this->xmlContent->startElement('style:text-properties');
$this->xmlContent->writeAttribute('fo:color', '#'.$chart->getLegend()->getFont()->getColor()->getRGB());
Expand Down Expand Up @@ -712,6 +739,19 @@ private function writeSeriesStyle(Chart $chart, Chart\Series $series)
$this->xmlContent->writeAttribute('chart:symbol-height', $symbolSize.'cm');
}
}

$separator = $series->getSeparator();
if (!empty($separator)) {
// style:chart-properties/chart:label-separator
$this->xmlContent->startElement('chart:label-separator');
if ($separator == PHP_EOL) {
$this->xmlContent->writeRaw('<text:p><text:line-break /></text:p>');
} else {
$this->xmlContent->writeElement('text:p', $separator);
}
$this->xmlContent->endElement();
}

// > style:chart-properties
$this->xmlContent->endElement();
// style:graphic-properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract class AbstractLayoutPack
*
* @var array
*/
protected $themes = '';
protected $themes = array();

/**
* Theme relations
Expand Down
Loading