@@ -346,6 +346,48 @@ public function setValues(array $values)
346346 }
347347 }
348348
349+ /**
350+ * @param string $search
351+ * @param \PhpOffice\PhpWord\Element\AbstractElement $chart
352+ */
353+ public function setChart ($ search , \PhpOffice \PhpWord \Element \AbstractElement $ chart )
354+ {
355+ $ elementName = substr (get_class ($ chart ), strrpos (get_class ($ chart ), '\\' ) + 1 );
356+ $ objectClass = 'PhpOffice \\PhpWord \\Writer \\Word2007 \\Element \\' . $ elementName ;
357+
358+ // Get the next relation id
359+ $ rId = $ this ->getNextRelationsIndex ($ this ->getMainPartName ());
360+ $ chart ->setRelationId ($ rId );
361+
362+ // Define the chart filename
363+ $ filename = "charts/chart {$ rId }.xml " ;
364+
365+ // Get the part writer
366+ $ writerPart = new \PhpOffice \PhpWord \Writer \Word2007 \Part \Chart ();
367+ $ writerPart ->setElement ($ chart );
368+
369+ // ContentTypes.xml
370+ $ this ->zipClass ->addFromString ("word/ {$ filename }" , $ writerPart ->write ());
371+
372+ // Add the chart to relations
373+ $ relationTpl = '<Relationship Id="rId{RID}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="charts/chart{RID}.xml"/> ' ;
374+ $ typeTpl = '<Override PartName="/word/{CHART}" ContentType="application/vnd.openxmlformats-officedocument.drawingml.chart+xml"/> ' ;
375+
376+ $ xmlChartRelation = str_replace (array ('{RID} ' ), array ($ rId ), $ relationTpl );
377+ $ xmlChartType = str_replace (array ('{CHART} ' ), array ($ filename ), $ typeTpl );
378+
379+ $ this ->tempDocumentContentTypes = str_replace ('</Types> ' , $ xmlChartType , $ this ->tempDocumentContentTypes ) . '</Types> ' ;
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