Skip to content

Commit b647374

Browse files
committed
#33 : Add support of charts for ODPresentation
1 parent 72b2318 commit b647374

File tree

7 files changed

+983
-9
lines changed

7 files changed

+983
-9
lines changed

samples/Sample_07_Chart.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@
5353
$bar3DChart = new Bar3D();
5454
$series1 = new Series('2009', $series1Data);
5555
$series1->setShowSeriesName(true);
56+
$series1->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4F81BD'));
5657
$series1->getFont()->getColor()->setRGB('00FF00');
5758
$series1->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFE06B20'));
5859
$series2 = new Series('2010', $series2Data);
5960
$series2->setShowSeriesName(true);
6061
$series2->getFont()->getColor()->setRGB('FF0000');
62+
$series2->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFC0504D'));
6163
$bar3DChart->addSeries($series1);
6264
$bar3DChart->addSeries($series2);
6365

@@ -98,6 +100,13 @@
98100
$pie3DChart = new Pie3D();
99101
$series = new Series('Downloads', $seriesData);
100102
$series->setShowSeriesName(true);
103+
$series->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4672A8'));
104+
$series->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFAB4744'));
105+
$series->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8AA64F'));
106+
$series->getDataPointFill(3)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF725990'));
107+
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4299B0'));
108+
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFDC853E'));
109+
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF93A9CE'));
101110
$pie3DChart->addSeries($series);
102111

103112
// Create a shape (chart)

src/PhpPowerpoint/Writer/ODPresentation.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Manifest;
2727
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Meta;
2828
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Mimetype;
29+
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\ObjectsChart;
2930
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Styles;
3031
use PhpOffice\PhpPowerpoint\Shape\AbstractDrawing;
3132

@@ -55,6 +56,11 @@ class ODPresentation implements WriterInterface
5556
*/
5657
private $drawingHashTable;
5758

59+
/**
60+
* @var \PhpOffice\PhpPowerpoint\Shape\Chart[]
61+
*/
62+
public $chartArray = array();
63+
5864
/**
5965
* Use disk caching where possible?
6066
*
@@ -88,6 +94,7 @@ public function __construct(PHPPowerPoint $pPHPPowerPoint = null)
8894
$this->writerParts['meta'] = new Meta();
8995
$this->writerParts['mimetype'] = new Mimetype();
9096
$this->writerParts['styles'] = new Styles();
97+
$this->writerParts['charts'] = new ObjectsChart();
9198

9299
$this->writerParts['drawing'] = new Drawing();
93100

@@ -155,6 +162,16 @@ public function save($pFilename)
155162
// Add META-INF/manifest.xml
156163
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writePart($this->presentation));
157164

165+
// Add charts
166+
foreach ($this->chartArray as $keyChart => $shapeChart) {
167+
$arrayFile = $this->getWriterPart('charts')->writePart($this->presentation, $shapeChart);
168+
foreach ($arrayFile as $file => $content) {
169+
if(!empty($content)){
170+
$objZip->addFromString('Object '.$keyChart.'/' . $file, $content);
171+
}
172+
}
173+
}
174+
158175
// Add media
159176
$arrMedia = array();
160177
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
@@ -208,7 +225,6 @@ public function save($pFilename)
208225
}
209226
@unlink($pFilename);
210227
}
211-
212228
} else {
213229
throw new \Exception("PHPPowerPoint object unassigned.");
214230
}

src/PhpPowerpoint/Writer/ODPresentation/AbstractPart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
abstract class AbstractPart
2929
{
30-
abstract public function writePart(PhpPowerpoint $pPHPPowerPoint);
30+
// abstract public function writePart(PhpPowerpoint $pPHPPowerPoint);
3131

3232
/**
3333
* Parent WriterInterface object

src/PhpPowerpoint/Writer/ODPresentation/Content.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,15 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)
452452

453453
// Check type
454454
if ($shape instanceof RichText) {
455-
$this->writeTxt($objWriter, $shape, $shapeId);
455+
$this->writeShapeTxt($objWriter, $shape, $shapeId);
456456
} elseif ($shape instanceof Table) {
457457
$this->writeShapeTable($objWriter, $shape, $shapeId);
458458
} elseif ($shape instanceof Line) {
459459
$this->writeShapeLine($objWriter, $shape, $shapeId);
460-
/*} elseif ($shape instanceof Chart) {
461-
$this->_writeChart($objWriter, $shape, $shapeId);
462-
}*/
460+
} elseif ($shape instanceof Chart) {
461+
$this->writeShapeChart($objWriter, $shape, $shapeId);
463462
} elseif ($shape instanceof AbstractDrawing) {
464-
$this->writePic($objWriter, $shape, $shapeId);
463+
$this->writeShapePic($objWriter, $shape, $shapeId);
465464
}
466465
}
467466
$objWriter->endElement();
@@ -481,7 +480,7 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)
481480
* @param \PhpOffice\PhpPowerpoint\Shape\AbstractDrawing $shape
482481
* @param int $shapeId
483482
*/
484-
public function writePic(XMLWriter $objWriter, AbstractDrawing $shape, $shapeId)
483+
public function writeShapePic(XMLWriter $objWriter, AbstractDrawing $shape, $shapeId)
485484
{
486485
// draw:frame
487486
$objWriter->startElement('draw:frame');
@@ -515,7 +514,7 @@ public function writePic(XMLWriter $objWriter, AbstractDrawing $shape, $shapeId)
515514
* @param \PhpOffice\PhpPowerpoint\Shape\RichText $shape
516515
* @param int $shapeId
517516
*/
518-
public function writeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
517+
public function writeShapeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
519518
{
520519
// draw:custom-shape
521520
$objWriter->startElement('draw:custom-shape');
@@ -754,4 +753,34 @@ public function writeShapeTable(XMLWriter $objWriter, Table $shape, $shapeId)
754753
// > draw:frame
755754
$objWriter->endElement();
756755
}
756+
757+
/**
758+
* Write table Chart
759+
* @param XMLWriter $objWriter
760+
* @param Chart $shape
761+
* @param integer $shapeId
762+
*/
763+
public function writeShapeChart(XMLWriter $objWriter, Chart $shape, $shapeId)
764+
{
765+
$this->getParentWriter()->chartArray[$shapeId] = $shape;
766+
767+
// draw:frame
768+
$objWriter->startElement('draw:frame');
769+
$objWriter->writeAttribute('draw:name', $shape->getTitle()->getText());
770+
$objWriter->writeAttribute('svg:x', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm');
771+
$objWriter->writeAttribute('svg:y', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm');
772+
$objWriter->writeAttribute('svg:height', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm');
773+
$objWriter->writeAttribute('svg:width', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getWidth()), 3) . 'cm');
774+
775+
// draw:object
776+
$objWriter->startElement('draw:object');
777+
$objWriter->writeAttribute('xlink:href', './Object '.$shapeId);
778+
$objWriter->writeAttribute('xlink:type', 'simple');
779+
$objWriter->writeAttribute('xlink:show', 'embed');
780+
781+
// > draw:object
782+
$objWriter->endElement();
783+
// > draw:frame
784+
$objWriter->endElement();
785+
}
757786
}

src/PhpPowerpoint/Writer/ODPresentation/Manifest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)
7373
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
7474
$objWriter->writeAttribute('manifest:full-path', 'styles.xml');
7575
$objWriter->endElement();
76+
77+
$arrChart = $this->getParentWriter()->chartArray;
78+
foreach ($arrChart as $key => $shape) {
79+
$objWriter->startElement('manifest:file-entry');
80+
$objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/');
81+
$objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.chart');
82+
$objWriter->endElement();
83+
$objWriter->startElement('manifest:file-entry');
84+
$objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/content.xml');
85+
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
86+
$objWriter->endElement();
87+
}
7688

7789
$arrMedia = array();
7890
for ($i = 0; $i < $parentWriter->getDrawingHashTable()->count(); ++$i) {

0 commit comments

Comments
 (0)