Skip to content

Commit 90fef2c

Browse files
committed
Add TemplateProcessor.setChart
1 parent 3b8d1cf commit 90fef2c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

docs/templates-processing.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ You can also set multiple values by passing all of them in an array.
3535
3636
$templateProcessor->setValues(array('firstname' => 'John', 'lastname' => 'Doe'));
3737
38+
setChartValue
39+
"""""""""""""
40+
Replace a variable by a chart.
41+
42+
.. code-block:: php
43+
$categories = array('A', 'B', 'C', 'D', 'E');
44+
$series1 = array(1, 3, 2, 5, 4);
45+
$chart = new Chart('doughnut', $categories, $series1);
46+
$templateProcessor->setChartValue('myChart', $chart);
47+
3848
setImageValue
3949
"""""""""""""
4050
The search-pattern model for images can be like:

src/PhpWord/TemplateProcessor.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,48 @@ public function setValues(array $values)
346346
}
347347
}
348348

349+
350+
/**
351+
* @param string $search
352+
* @param \PhpOffice\PhpWord\Element\AbstractElement $complexType
353+
*/
354+
public function setChart($search, \PhpOffice\PhpWord\Element\AbstractElement $chart)
355+
{
356+
$elementName = substr(get_class($chart), strrpos(get_class($chart), '\\') + 1);
357+
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;
358+
359+
// Get the next relation id
360+
$rId= $this->getNextRelationsIndex($this->getMainPartName());
361+
$chart->setRelationId($rId);
362+
363+
// Define the chart filename
364+
$filename = "charts/chart{$rId}.xml";
365+
366+
// Get the part writer
367+
$writerPart = new \PhpOffice\PhpWord\Writer\Word2007\Part\Chart();
368+
$writerPart->setElement($chart);
369+
370+
// ContentTypes.xml
371+
$this->contentTypes['override']["/word/{$filename}"] = 'chart';
372+
373+
$this->relationships[] = array('target' => $filename, 'type' => 'chart', 'rID' => $rId);
374+
375+
$this->zipClass->addFromString("word/{$filename}", $writerPart->write());
376+
377+
// Add the chart to relations
378+
$xmlChartRelation = "<Relationship Id=\"rId{$rId}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart\" Target=\"charts/chart{$rId}.xml\"/>";
379+
380+
$this->tempDocumentRelations[$this->getMainPartName()] = str_replace('</Relationships>', $xmlChartRelation, $this->tempDocumentRelations[$this->getMainPartName()]) . '</Relationships>';
381+
382+
// Write the chart
383+
$xmlWriter = new XMLWriter();
384+
$elementWriter = new $objectClass($xmlWriter, $chart, true);
385+
$elementWriter->write();
386+
387+
// Place it in the template
388+
$this->replaceXmlBlock($search, "<w:p>" . $xmlWriter->getData() . "</w:p>", 'w:p');
389+
}
390+
349391
private function getImageArgs($varNameWithArgs)
350392
{
351393
$varElements = explode(':', $varNameWithArgs);

0 commit comments

Comments
 (0)