Skip to content

Commit 78a3eb7

Browse files
committed
Add fixed setChart for templating PHPOffice#1058
Added legend position Added overlay to 0 so the graph scales correctly
1 parent 733f845 commit 78a3eb7

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/PhpWord/Style/Chart.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ class Chart extends AbstractStyle
101101
*/
102102
private $valueLabelPosition = 'nextTo';
103103

104+
/**
105+
* A string that tells the write where to write the legend on the chart
106+
* "b" - Bottom of graph
107+
* "l" - Left of graph
108+
* "r" - Right of graph
109+
* "t" - top of graph
110+
* "tr" - top right of graph
111+
* @var string
112+
*/
113+
private $legendPosition = 'r';
114+
104115
/**
105116
* @var string
106117
*/
@@ -285,6 +296,35 @@ public function setShowLegend($value = false)
285296
return $this;
286297
}
287298

299+
/**
300+
* Get the legendPosition setting
301+
*
302+
* @return string
303+
*/
304+
public function getLegendPosition()
305+
{
306+
return $this->legendPosition;
307+
}
308+
309+
/**
310+
* Set the legendPosition setting
311+
* "b" - Bottom of graph
312+
* "l" - Left of graph
313+
* "r" - Right of graph
314+
* "t" - top of graph
315+
* "tr" - top right of graph
316+
*
317+
* @param string
318+
* @param mixed $legendPosition
319+
*/
320+
public function setLegendPosition($legendPosition)
321+
{
322+
$enum = array('b', 'l', 'r', 't', 'tr');
323+
$this->legendPosition = $this->setEnumVal($legendPosition, $enum, $this->legendPosition);
324+
325+
return $this;
326+
}
327+
288328
/*
289329
* Show labels for axis
290330
*

src/PhpWord/TemplateProcessor.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/PhpWord/Writer/Word2007/Part/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function writePlotArea(XMLWriter $xmlWriter)
154154

155155
//Chart legend
156156
if ($showLegend) {
157-
$xmlWriter->writeRaw('<c:legend><c:legendPos val="r"/></c:legend>');
157+
$xmlWriter->writeRaw('<c:legend><c:legendPos val="' . $style->getLegendPosition() . '"/><c:overlay val="0" /></c:legend>');
158158
}
159159

160160
$xmlWriter->startElement('c:plotArea');

0 commit comments

Comments
 (0)