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 new file mode 100644 index 0000000000..2b017d7f7c --- /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 0000000000..c958b335c8 Binary files /dev/null and b/samples/resources/Sample_41_TemplateSetChart.docx differ diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index b5fba6140e..2d4ab2be79 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -355,6 +355,46 @@ public function setValues(array $values) } } + /** + * @param string $search + * @param \PhpOffice\PhpWord\Element\AbstractElement $complexType + */ + public function setChart($search, \PhpOffice\PhpWord\Element\AbstractElement $chart) + { + $elementName = substr(get_class($chart), strrpos(get_class($chart), '\\') + 1); + $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName; + + // Get the next relation id + $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); + + // 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..e0d1752b31 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -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; + for ($i = 0; $i < count($values); $i++) { + // 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++; } } } 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); diff --git a/tests/PhpWord/Writer/Word2007/Element/ChartTest.php b/tests/PhpWord/Writer/Word2007/Element/ChartTest.php index 432d8db86a..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], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx"); + self::assertEquals($colorArray[$idx - 1], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx"); } }