From 1168789e8a55e2564cbc02236b23cf69cb0c1504 Mon Sep 17 00:00:00 2001 From: Antoine de Troostembergh Date: Sat, 6 Feb 2021 21:32:30 +0100 Subject: [PATCH] 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 9166890e..7d0ef2e6 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 102fa3ac..2b017d7f 100644 --- a/samples/Sample_41_TemplateSetChart.php +++ b/samples/Sample_41_TemplateSetChart.php @@ -17,23 +17,23 @@ $series1 = array(1, 3, 2, 5, 4); $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 f67bc1cd..2d4ab2be 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -365,12 +365,12 @@ class TemplateProcessor $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 705a5c28..168f0216 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -267,10 +267,10 @@ class Chart extends AbstractPart $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 852bf687..391daa2d 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -401,7 +401,7 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase 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);