Skip to content

Commit 9e572ec

Browse files
chart - Add dynamic Legend positions (#1)
* Writer/Part/chart.php - Add dynamic Legend positions The position of the legend of charts was always fixed to the right. Adding in the option to set it dynamically via a new option under styles/chart * Update Styles/Chart.php Add in the public functions to getStyle() to get and set the legend position
1 parent 85e9144 commit 9e572ec

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/PhpWord/Style/Chart.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ class Chart extends AbstractStyle
6666
*/
6767
private $showLegend = false;
6868

69+
/**
70+
* Chart legend Position.
71+
*
72+
* @var string
73+
*/
74+
private $legendPosition = 'r';
75+
6976
/**
7077
* A list of display options for data labels
7178
*
@@ -285,6 +292,34 @@ public function setShowLegend($value = false)
285292
return $this;
286293
}
287294

295+
/**
296+
* Get chart legend position
297+
*
298+
* @return string
299+
*/
300+
public function getLegendPosition()
301+
{
302+
return $this->legendPosition;
303+
}
304+
305+
/**
306+
* Set chart legend position. choices:
307+
* "r" - right of chart
308+
* "b" - bottom of chart
309+
* "t" - top of chart
310+
* "l" - left of chart
311+
*
312+
* default: right
313+
*
314+
* @param bool $value
315+
*/
316+
public function setLegendPosition($value = 'r')
317+
{
318+
$this->legendPosition = $value;
319+
320+
return $this;
321+
}
322+
288323
/*
289324
* Show labels for axis
290325
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ private function writePlotArea(XMLWriter $xmlWriter)
131131

132132
$title = $style->getTitle();
133133
$showLegend = $style->isShowLegend();
134+
$legendPosition = $style->getLegendPosition();
134135

135136
//Chart title
136137
if ($title) {
@@ -154,7 +155,7 @@ private function writePlotArea(XMLWriter $xmlWriter)
154155

155156
//Chart legend
156157
if ($showLegend) {
157-
$xmlWriter->writeRaw('<c:legend><c:legendPos val="r"/></c:legend>');
158+
$xmlWriter->writeRaw('<c:legend><c:legendPos val="'.$legendPosition.'"/></c:legend>');
158159
}
159160

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

0 commit comments

Comments
 (0)