@@ -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