Skip to content

Commit

Permalink
#33 : Add support of charts for ODPresentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jul 21, 2014
1 parent 72b2318 commit b647374
Show file tree
Hide file tree
Showing 7 changed files with 983 additions and 9 deletions.
9 changes: 9 additions & 0 deletions samples/Sample_07_Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@
$bar3DChart = new Bar3D();
$series1 = new Series('2009', $series1Data);
$series1->setShowSeriesName(true);
$series1->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4F81BD'));
$series1->getFont()->getColor()->setRGB('00FF00');
$series1->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFE06B20'));
$series2 = new Series('2010', $series2Data);
$series2->setShowSeriesName(true);
$series2->getFont()->getColor()->setRGB('FF0000');
$series2->getFill()->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFC0504D'));
$bar3DChart->addSeries($series1);
$bar3DChart->addSeries($series2);

Expand Down Expand Up @@ -98,6 +100,13 @@
$pie3DChart = new Pie3D();
$series = new Series('Downloads', $seriesData);
$series->setShowSeriesName(true);
$series->getDataPointFill(0)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4672A8'));
$series->getDataPointFill(1)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFAB4744'));
$series->getDataPointFill(2)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF8AA64F'));
$series->getDataPointFill(3)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF725990'));
$series->getDataPointFill(4)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF4299B0'));
$series->getDataPointFill(5)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FFDC853E'));
$series->getDataPointFill(6)->setFillType(Fill::FILL_SOLID)->setStartColor(new Color('FF93A9CE'));
$pie3DChart->addSeries($series);

// Create a shape (chart)
Expand Down
18 changes: 17 additions & 1 deletion src/PhpPowerpoint/Writer/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Manifest;
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Meta;
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Mimetype;
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\ObjectsChart;
use PhpOffice\PhpPowerpoint\Writer\ODPresentation\Styles;
use PhpOffice\PhpPowerpoint\Shape\AbstractDrawing;

Expand Down Expand Up @@ -55,6 +56,11 @@ class ODPresentation implements WriterInterface
*/
private $drawingHashTable;

/**
* @var \PhpOffice\PhpPowerpoint\Shape\Chart[]
*/
public $chartArray = array();

/**
* Use disk caching where possible?
*
Expand Down Expand Up @@ -88,6 +94,7 @@ public function __construct(PHPPowerPoint $pPHPPowerPoint = null)
$this->writerParts['meta'] = new Meta();
$this->writerParts['mimetype'] = new Mimetype();
$this->writerParts['styles'] = new Styles();
$this->writerParts['charts'] = new ObjectsChart();

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

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

// Add charts
foreach ($this->chartArray as $keyChart => $shapeChart) {
$arrayFile = $this->getWriterPart('charts')->writePart($this->presentation, $shapeChart);
foreach ($arrayFile as $file => $content) {
if(!empty($content)){
$objZip->addFromString('Object '.$keyChart.'/' . $file, $content);
}
}
}

// Add media
$arrMedia = array();
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
Expand Down Expand Up @@ -208,7 +225,6 @@ public function save($pFilename)
}
@unlink($pFilename);
}

} else {
throw new \Exception("PHPPowerPoint object unassigned.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPowerpoint/Writer/ODPresentation/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
abstract class AbstractPart
{
abstract public function writePart(PhpPowerpoint $pPHPPowerPoint);
// abstract public function writePart(PhpPowerpoint $pPHPPowerPoint);

/**
* Parent WriterInterface object
Expand Down
43 changes: 36 additions & 7 deletions src/PhpPowerpoint/Writer/ODPresentation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,15 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)

// Check type
if ($shape instanceof RichText) {
$this->writeTxt($objWriter, $shape, $shapeId);
$this->writeShapeTxt($objWriter, $shape, $shapeId);
} elseif ($shape instanceof Table) {
$this->writeShapeTable($objWriter, $shape, $shapeId);
} elseif ($shape instanceof Line) {
$this->writeShapeLine($objWriter, $shape, $shapeId);
/*} elseif ($shape instanceof Chart) {
$this->_writeChart($objWriter, $shape, $shapeId);
}*/
} elseif ($shape instanceof Chart) {
$this->writeShapeChart($objWriter, $shape, $shapeId);
} elseif ($shape instanceof AbstractDrawing) {
$this->writePic($objWriter, $shape, $shapeId);
$this->writeShapePic($objWriter, $shape, $shapeId);
}
}
$objWriter->endElement();
Expand All @@ -481,7 +480,7 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)
* @param \PhpOffice\PhpPowerpoint\Shape\AbstractDrawing $shape
* @param int $shapeId
*/
public function writePic(XMLWriter $objWriter, AbstractDrawing $shape, $shapeId)
public function writeShapePic(XMLWriter $objWriter, AbstractDrawing $shape, $shapeId)
{
// draw:frame
$objWriter->startElement('draw:frame');
Expand Down Expand Up @@ -515,7 +514,7 @@ public function writePic(XMLWriter $objWriter, AbstractDrawing $shape, $shapeId)
* @param \PhpOffice\PhpPowerpoint\Shape\RichText $shape
* @param int $shapeId
*/
public function writeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
public function writeShapeTxt(XMLWriter $objWriter, RichText $shape, $shapeId)
{
// draw:custom-shape
$objWriter->startElement('draw:custom-shape');
Expand Down Expand Up @@ -754,4 +753,34 @@ public function writeShapeTable(XMLWriter $objWriter, Table $shape, $shapeId)
// > draw:frame
$objWriter->endElement();
}

/**
* Write table Chart
* @param XMLWriter $objWriter
* @param Chart $shape
* @param integer $shapeId
*/
public function writeShapeChart(XMLWriter $objWriter, Chart $shape, $shapeId)
{
$this->getParentWriter()->chartArray[$shapeId] = $shape;

// draw:frame
$objWriter->startElement('draw:frame');
$objWriter->writeAttribute('draw:name', $shape->getTitle()->getText());
$objWriter->writeAttribute('svg:x', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm');
$objWriter->writeAttribute('svg:y', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm');
$objWriter->writeAttribute('svg:height', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm');
$objWriter->writeAttribute('svg:width', String::numberFormat(SharedDrawing::pixelsToCentimeters($shape->getWidth()), 3) . 'cm');

// draw:object
$objWriter->startElement('draw:object');
$objWriter->writeAttribute('xlink:href', './Object '.$shapeId);
$objWriter->writeAttribute('xlink:type', 'simple');
$objWriter->writeAttribute('xlink:show', 'embed');

// > draw:object
$objWriter->endElement();
// > draw:frame
$objWriter->endElement();
}
}
12 changes: 12 additions & 0 deletions src/PhpPowerpoint/Writer/ODPresentation/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public function writePart(PhpPowerpoint $pPHPPowerPoint)
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
$objWriter->writeAttribute('manifest:full-path', 'styles.xml');
$objWriter->endElement();

$arrChart = $this->getParentWriter()->chartArray;
foreach ($arrChart as $key => $shape) {
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/');
$objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.chart');
$objWriter->endElement();
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', 'Object '.$key.'/content.xml');
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
$objWriter->endElement();
}

$arrMedia = array();
for ($i = 0; $i < $parentWriter->getDrawingHashTable()->count(); ++$i) {
Expand Down
Loading

0 comments on commit b647374

Please sign in to comment.