From 31002fc6e867fef24f4dd82827e8f527fd9d479e Mon Sep 17 00:00:00 2001 From: dbarzin Date: Sat, 6 Feb 2021 10:40:52 +0100 Subject: [PATCH 1/6] add chart in template --- samples/Sample_41_TemplateSetChart.php | 45 ++++++++++++++++++ .../resources/Sample_41_TemplateSetChart.docx | Bin 0 -> 9576 bytes src/PhpWord/TemplateProcessor.php | 40 ++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Chart.php | 23 +++++---- 4 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 samples/Sample_41_TemplateSetChart.php create mode 100644 samples/resources/Sample_41_TemplateSetChart.docx diff --git a/samples/Sample_41_TemplateSetChart.php b/samples/Sample_41_TemplateSetChart.php new file mode 100644 index 0000000000..102fa3acea --- /dev/null +++ b/samples/Sample_41_TemplateSetChart.php @@ -0,0 +1,45 @@ +addSeries($categories, $series2); + } + if (in_array($chartType, $threeSeries)) { + $chart->addSeries($categories, $series3); + } + + $chart->getStyle() + ->setWidth(Converter::inchToEmu(3)) + ->setHeight(Converter::inchToEmu(3)); + + $templateProcessor->setChart("chart{$i}", $chart); + $i++; +} + +echo date('H:i:s'), ' Saving the result document...', EOL; +$templateProcessor->saveAs('results/Sample_41_TemplateSetChart.docx'); + +echo getEndingNotes(array('Word2007' => 'docx'), 'results/Sample_41_TemplateSetChart.docx'); +if (!CLI) { + include_once 'Sample_Footer.php'; +} diff --git a/samples/resources/Sample_41_TemplateSetChart.docx b/samples/resources/Sample_41_TemplateSetChart.docx new file mode 100644 index 0000000000000000000000000000000000000000..c958b335c8ffe06c109ef3ccea5fb34bc81e1f8c GIT binary patch literal 9576 zcma)i1y~)+vMwIn-Q6JscZWb=A;CS!f}jg`2`<6i-JRfW0fM^+cPBW(9^{;TlC$r- z`@LG<%r~oh{_2_P>gt~UOF;$_3KI+#78Wc>yFdZ#4?%do*0Tj#*u7?cIhRdzzJq*& z3bX2#VUXnJRHq_tRNWdhmLlcxDRSr-CPh{%Uvp(y0lrB=wUe0bc4vn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wG7y7Fp!KUf@4FyMG7&Id{D*2Y!p> zvswQWF8{qa_&fT~!22@4{g!H?KhVEtyT2>=GY_1H=NRPgU;{df4EuKC~XZgetNextRelationsIndex($this->getMainPartName()); + $chart->setRelationId($rId); + + // Define the chart filename + $filename = "charts/chart{$rId}.xml"; + + // Get the part writer + $writerPart = new \PhpOffice\PhpWord\Writer\Word2007\Part\Chart(); + $writerPart->setElement($chart); + + // ContentTypes.xml + $this->zipClass->addFromString("word/{$filename}", $writerPart->write()); + + // add chart to content type + $xmlRelationsType = ""; + $this->tempDocumentContentTypes = str_replace('', $xmlRelationsType, $this->tempDocumentContentTypes) . ''; + + // Add the chart to relations + $xmlChartRelation = ""; + $this->tempDocumentRelations[$this->getMainPartName()] = str_replace('', $xmlChartRelation, $this->tempDocumentRelations[$this->getMainPartName()]) . ''; + + // Write the chart + $xmlWriter = new XMLWriter(); + $elementWriter = new $objectClass($xmlWriter, $chart, true); + $elementWriter->write(); + + // Place it in the template + $this->replaceXmlBlock($search, '' . $xmlWriter->getData() . '', 'w:p'); + } + private function getImageArgs($varNameWithArgs) { $varElements = explode(':', $varNameWithArgs); diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 8907160b22..58fa55d687 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -209,7 +209,7 @@ private function writePlotArea(XMLWriter $xmlWriter) /** * Write series. * - * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter + * @param \PhpOffice\Common\XMLWriter $xmlWriter * @param bool $scatter */ private function writeSeries(XMLWriter $xmlWriter, $scatter = false) @@ -219,6 +219,7 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false) $colors = $style->getColors(); $index = 0; + $colorIndex = 0; foreach ($series as $seriesItem) { $categories = $seriesItem['categories']; $values = $seriesItem['values']; @@ -265,23 +266,21 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false) $this->writeSeriesItem($xmlWriter, 'cat', $categories); $this->writeSeriesItem($xmlWriter, 'val', $values); - // setting the chart colors was taken from https://github.com/PHPOffice/PHPWord/issues/494 - if (is_array($colors) && count($colors)) { - // This is a workaround to make each series in a stack chart use a different color - if ($this->options['type'] == 'bar' && stristr($this->options['grouping'], 'stacked')) { - array_shift($colors); - } - $colorIndex = 0; - foreach ($colors as $color) { + // check that there are colors + if (is_array($colors) && count($colors)>0) { + // assign a color to each value + $valueIndex=0; + foreach ($values as $value) { + // check that there are still enought colors $xmlWriter->startElement('c:dPt'); - $xmlWriter->writeElementBlock('c:idx', 'val', $colorIndex); + $xmlWriter->writeElementBlock('c:idx', 'val', $valueIndex); $xmlWriter->startElement('c:spPr'); $xmlWriter->startElement('a:solidFill'); - $xmlWriter->writeElementBlock('a:srgbClr', 'val', $color); + $xmlWriter->writeElementBlock('a:srgbClr', 'val', $colors[$colorIndex++ % count($colors)]); $xmlWriter->endElement(); // a:solidFill $xmlWriter->endElement(); // c:spPr $xmlWriter->endElement(); // c:dPt - $colorIndex++; + $valueIndex++; } } } From 48ee2eb78a5d74bd9845ea86fb277b156759ae34 Mon Sep 17 00:00:00 2001 From: dbarzin Date: Sat, 6 Feb 2021 10:50:21 +0100 Subject: [PATCH 2/6] fix param error --- src/PhpWord/Writer/Word2007/Part/Chart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 58fa55d687..705a5c2856 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -209,7 +209,7 @@ private function writePlotArea(XMLWriter $xmlWriter) /** * Write series. * - * @param \PhpOffice\Common\XMLWriter $xmlWriter + * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param bool $scatter */ private function writeSeries(XMLWriter $xmlWriter, $scatter = false) From 214df1a1cbede6a99b643e14a0be2a0dbd31a898 Mon Sep 17 00:00:00 2001 From: dbarzin Date: Sat, 6 Feb 2021 12:57:16 +0100 Subject: [PATCH 3/6] fix unit test --- tests/PhpWord/Writer/Word2007/Element/ChartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpWord/Writer/Word2007/Element/ChartTest.php b/tests/PhpWord/Writer/Word2007/Element/ChartTest.php index 432d8db86a..a493191989 100644 --- a/tests/PhpWord/Writer/Word2007/Element/ChartTest.php +++ b/tests/PhpWord/Writer/Word2007/Element/ChartTest.php @@ -113,7 +113,7 @@ public function testChartElements() for ($idxp1 = 1; $idxp1 < $numColor; ++$idxp1) { $idx = $idxp1; // stacked bar chart is shifted $element = $path . "/c:ser/c:dPt[$idxp1]/c:spPr/a:solidFill/a:srgbClr"; - self::assertEquals($colorArray[$idx], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx"); + self::assertEquals($colorArray[$idx-1], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx"); } } From 1168789e8a55e2564cbc02236b23cf69cb0c1504 Mon Sep 17 00:00:00 2001 From: Antoine de Troostembergh Date: Sat, 6 Feb 2021 21:32:30 +0100 Subject: [PATCH 4/6] fix formatting --- docs/templates-processing.rst | 17 +++++++++++--- samples/Sample_41_TemplateSetChart.php | 26 +++++++++++----------- src/PhpWord/TemplateProcessor.php | 4 ++-- src/PhpWord/Writer/Word2007/Part/Chart.php | 6 ++--- tests/PhpWord/TemplateProcessorTest.php | 2 +- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/docs/templates-processing.rst b/docs/templates-processing.rst index 9166890ebb..7d0ef2e654 100644 --- a/docs/templates-processing.rst +++ b/docs/templates-processing.rst @@ -249,9 +249,20 @@ See ``Sample_40_TemplateSetComplexValue.php`` for examples. $table->addCell(150)->addText('Cell B2'); $table->addCell(150)->addText('Cell B3'); $templateProcessor->setComplexBlock('table', $table); - + +setChartValue +""""""""""""" +Replace a variable by a chart. + +.. code-block:: php + + $categories = array('A', 'B', 'C', 'D', 'E'); + $series1 = array(1, 3, 2, 5, 4); + $chart = new Chart('doughnut', $categories, $series1); + $templateProcessor->setChartValue('myChart', $chart); + save -""""""""" +"""" Saves the loaded template within the current directory. Returns the file path. .. code-block:: php @@ -259,7 +270,7 @@ Saves the loaded template within the current directory. Returns the file path. $filepath = $templateProcessor->save(); saveAs -""""""""" +"""""" Saves a copy of the loaded template in the indicated path. .. code-block:: php diff --git a/samples/Sample_41_TemplateSetChart.php b/samples/Sample_41_TemplateSetChart.php index 102fa3acea..2b017d7f7c 100644 --- a/samples/Sample_41_TemplateSetChart.php +++ b/samples/Sample_41_TemplateSetChart.php @@ -17,23 +17,23 @@ $series2 = array(3, 1, 7, 2, 6); $series3 = array(8, 3, 2, 5, 4); -$i=0; +$i = 0; foreach ($chartTypes as $chartType) { - $chart = new Chart($chartType, $categories, $series1); + $chart = new Chart($chartType, $categories, $series1); - if (in_array($chartType, $twoSeries)) { - $chart->addSeries($categories, $series2); - } - if (in_array($chartType, $threeSeries)) { - $chart->addSeries($categories, $series3); - } + if (in_array($chartType, $twoSeries)) { + $chart->addSeries($categories, $series2); + } + if (in_array($chartType, $threeSeries)) { + $chart->addSeries($categories, $series3); + } - $chart->getStyle() - ->setWidth(Converter::inchToEmu(3)) - ->setHeight(Converter::inchToEmu(3)); + $chart->getStyle() + ->setWidth(Converter::inchToEmu(3)) + ->setHeight(Converter::inchToEmu(3)); - $templateProcessor->setChart("chart{$i}", $chart); - $i++; + $templateProcessor->setChart("chart{$i}", $chart); + $i++; } echo date('H:i:s'), ' Saving the result document...', EOL; diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index f67bc1cd16..2d4ab2be79 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -365,12 +365,12 @@ public function setChart($search, \PhpOffice\PhpWord\Element\AbstractElement $ch $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName; // Get the next relation id - $rId= $this->getNextRelationsIndex($this->getMainPartName()); + $rId = $this->getNextRelationsIndex($this->getMainPartName()); $chart->setRelationId($rId); // Define the chart filename $filename = "charts/chart{$rId}.xml"; - + // Get the part writer $writerPart = new \PhpOffice\PhpWord\Writer\Word2007\Part\Chart(); $writerPart->setElement($chart); diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 705a5c2856..168f02168d 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -267,10 +267,10 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false) $this->writeSeriesItem($xmlWriter, 'val', $values); // check that there are colors - if (is_array($colors) && count($colors)>0) { + if (is_array($colors) && count($colors) > 0) { // assign a color to each value - $valueIndex=0; - foreach ($values as $value) { + $valueIndex = 0; + for ($i = 1; $i < count($values); $i++) { // check that there are still enought colors $xmlWriter->startElement('c:dPt'); $xmlWriter->writeElementBlock('c:idx', 'val', $valueIndex); diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 852bf687d3..391daa2de8 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -401,7 +401,7 @@ public function testSetImageValue() return $imagePath; }, 'documentContent' => array('path' => $imagePath, 'width' => 500, 'height' => 500), - 'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false), + 'footerValue' => array('path' => $imagePath, 'width' => 100, 'height' => 50, 'ratio' => false), ); $templateProcessor->setImageValue(array_keys($variablesReplace), $variablesReplace); From 31259f6448c943e126654ca0ebf86900735b35c7 Mon Sep 17 00:00:00 2001 From: Antoine de Troostembergh Date: Sat, 6 Feb 2021 22:12:19 +0100 Subject: [PATCH 5/6] fix --- src/PhpWord/Writer/Word2007/Part/Chart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 168f02168d..e0d1752b31 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -270,7 +270,7 @@ private function writeSeries(XMLWriter $xmlWriter, $scatter = false) if (is_array($colors) && count($colors) > 0) { // assign a color to each value $valueIndex = 0; - for ($i = 1; $i < count($values); $i++) { + for ($i = 0; $i < count($values); $i++) { // check that there are still enought colors $xmlWriter->startElement('c:dPt'); $xmlWriter->writeElementBlock('c:idx', 'val', $valueIndex); From dc9b1edb5b39ff308f4ede2e729fbc57e96122fd Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 6 Feb 2021 22:50:11 +0100 Subject: [PATCH 6/6] fix formatting --- tests/PhpWord/Writer/Word2007/Element/ChartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpWord/Writer/Word2007/Element/ChartTest.php b/tests/PhpWord/Writer/Word2007/Element/ChartTest.php index a493191989..a69838a9a3 100644 --- a/tests/PhpWord/Writer/Word2007/Element/ChartTest.php +++ b/tests/PhpWord/Writer/Word2007/Element/ChartTest.php @@ -113,7 +113,7 @@ public function testChartElements() for ($idxp1 = 1; $idxp1 < $numColor; ++$idxp1) { $idx = $idxp1; // stacked bar chart is shifted $element = $path . "/c:ser/c:dPt[$idxp1]/c:spPr/a:solidFill/a:srgbClr"; - self::assertEquals($colorArray[$idx-1], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx"); + self::assertEquals($colorArray[$idx - 1], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx"); } }