Skip to content

Commit 31002fc

Browse files
committed
add chart in template
1 parent 4e87e72 commit 31002fc

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
use PhpOffice\PhpWord\Element\Chart;
3+
use PhpOffice\PhpWord\Shared\Converter;
4+
5+
include_once 'Sample_Header.php';
6+
7+
// Template processor instance creation
8+
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
9+
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_41_TemplateSetChart.docx');
10+
11+
$chartTypes = array('pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
12+
$twoSeries = array('bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
13+
$threeSeries = array('bar', 'line');
14+
15+
$categories = array('A', 'B', 'C', 'D', 'E');
16+
$series1 = array(1, 3, 2, 5, 4);
17+
$series2 = array(3, 1, 7, 2, 6);
18+
$series3 = array(8, 3, 2, 5, 4);
19+
20+
$i=0;
21+
foreach ($chartTypes as $chartType) {
22+
$chart = new Chart($chartType, $categories, $series1);
23+
24+
if (in_array($chartType, $twoSeries)) {
25+
$chart->addSeries($categories, $series2);
26+
}
27+
if (in_array($chartType, $threeSeries)) {
28+
$chart->addSeries($categories, $series3);
29+
}
30+
31+
$chart->getStyle()
32+
->setWidth(Converter::inchToEmu(3))
33+
->setHeight(Converter::inchToEmu(3));
34+
35+
$templateProcessor->setChart("chart{$i}", $chart);
36+
$i++;
37+
}
38+
39+
echo date('H:i:s'), ' Saving the result document...', EOL;
40+
$templateProcessor->saveAs('results/Sample_41_TemplateSetChart.docx');
41+
42+
echo getEndingNotes(array('Word2007' => 'docx'), 'results/Sample_41_TemplateSetChart.docx');
43+
if (!CLI) {
44+
include_once 'Sample_Footer.php';
45+
}
Binary file not shown.

src/PhpWord/TemplateProcessor.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,46 @@ public function setValues(array $values)
355355
}
356356
}
357357

358+
/**
359+
* @param string $search
360+
* @param \PhpOffice\PhpWord\Element\AbstractElement $complexType
361+
*/
362+
public function setChart($search, \PhpOffice\PhpWord\Element\AbstractElement $chart)
363+
{
364+
$elementName = substr(get_class($chart), strrpos(get_class($chart), '\\') + 1);
365+
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;
366+
367+
// Get the next relation id
368+
$rId= $this->getNextRelationsIndex($this->getMainPartName());
369+
$chart->setRelationId($rId);
370+
371+
// Define the chart filename
372+
$filename = "charts/chart{$rId}.xml";
373+
374+
// Get the part writer
375+
$writerPart = new \PhpOffice\PhpWord\Writer\Word2007\Part\Chart();
376+
$writerPart->setElement($chart);
377+
378+
// ContentTypes.xml
379+
$this->zipClass->addFromString("word/{$filename}", $writerPart->write());
380+
381+
// add chart to content type
382+
$xmlRelationsType = "<Override PartName=\"/word/{$filename}\" ContentType=\"application/vnd.openxmlformats-officedocument.drawingml.chart+xml\"/>";
383+
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlRelationsType, $this->tempDocumentContentTypes) . '</Types>';
384+
385+
// Add the chart to relations
386+
$xmlChartRelation = "<Relationship Id=\"rId{$rId}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart\" Target=\"charts/chart{$rId}.xml\"/>";
387+
$this->tempDocumentRelations[$this->getMainPartName()] = str_replace('</Relationships>', $xmlChartRelation, $this->tempDocumentRelations[$this->getMainPartName()]) . '</Relationships>';
388+
389+
// Write the chart
390+
$xmlWriter = new XMLWriter();
391+
$elementWriter = new $objectClass($xmlWriter, $chart, true);
392+
$elementWriter->write();
393+
394+
// Place it in the template
395+
$this->replaceXmlBlock($search, '<w:p>' . $xmlWriter->getData() . '</w:p>', 'w:p');
396+
}
397+
358398
private function getImageArgs($varNameWithArgs)
359399
{
360400
$varElements = explode(':', $varNameWithArgs);

src/PhpWord/Writer/Word2007/Part/Chart.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private function writePlotArea(XMLWriter $xmlWriter)
209209
/**
210210
* Write series.
211211
*
212-
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
212+
* @param \PhpOffice\Common\XMLWriter $xmlWriter
213213
* @param bool $scatter
214214
*/
215215
private function writeSeries(XMLWriter $xmlWriter, $scatter = false)
@@ -219,6 +219,7 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false)
219219
$colors = $style->getColors();
220220

221221
$index = 0;
222+
$colorIndex = 0;
222223
foreach ($series as $seriesItem) {
223224
$categories = $seriesItem['categories'];
224225
$values = $seriesItem['values'];
@@ -265,23 +266,21 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false)
265266
$this->writeSeriesItem($xmlWriter, 'cat', $categories);
266267
$this->writeSeriesItem($xmlWriter, 'val', $values);
267268

268-
// setting the chart colors was taken from https://github.com/PHPOffice/PHPWord/issues/494
269-
if (is_array($colors) && count($colors)) {
270-
// This is a workaround to make each series in a stack chart use a different color
271-
if ($this->options['type'] == 'bar' && stristr($this->options['grouping'], 'stacked')) {
272-
array_shift($colors);
273-
}
274-
$colorIndex = 0;
275-
foreach ($colors as $color) {
269+
// check that there are colors
270+
if (is_array($colors) && count($colors)>0) {
271+
// assign a color to each value
272+
$valueIndex=0;
273+
foreach ($values as $value) {
274+
// check that there are still enought colors
276275
$xmlWriter->startElement('c:dPt');
277-
$xmlWriter->writeElementBlock('c:idx', 'val', $colorIndex);
276+
$xmlWriter->writeElementBlock('c:idx', 'val', $valueIndex);
278277
$xmlWriter->startElement('c:spPr');
279278
$xmlWriter->startElement('a:solidFill');
280-
$xmlWriter->writeElementBlock('a:srgbClr', 'val', $color);
279+
$xmlWriter->writeElementBlock('a:srgbClr', 'val', $colors[$colorIndex++ % count($colors)]);
281280
$xmlWriter->endElement(); // a:solidFill
282281
$xmlWriter->endElement(); // c:spPr
283282
$xmlWriter->endElement(); // c:dPt
284-
$colorIndex++;
283+
$valueIndex++;
285284
}
286285
}
287286
}

0 commit comments

Comments
 (0)