Skip to content

Add Option for Dynamic Chart Legend Position #1699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/PhpWord/Style/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class Chart extends AbstractStyle
*/
private $showLegend = false;

/**
* Chart legend Position.
*
* @var string
*/
private $legendPosition = 'r';

/**
* A list of display options for data labels
*
Expand Down Expand Up @@ -285,6 +292,35 @@ public function setShowLegend($value = false)
return $this;
}

/**
* Get chart legend position
*
* @return string
*/
public function getLegendPosition()
{
return $this->legendPosition;
}

/**
* Set chart legend position. choices:
* "r" - right of chart
* "b" - bottom of chart
* "t" - top of chart
* "l" - left of chart
* "tr" - top right of chart
*
* default: right
*
* @param bool $value
*/
public function setLegendPosition($value = 'r')
{
$this->legendPosition = $value;

return $this;
}

/*
* Show labels for axis
*
Expand Down
3 changes: 2 additions & 1 deletion src/PhpWord/Writer/Word2007/Part/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private function writePlotArea(XMLWriter $xmlWriter)

$title = $style->getTitle();
$showLegend = $style->isShowLegend();
$legendPosition = $style->getLegendPosition();

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

//Chart legend
if ($showLegend) {
$xmlWriter->writeRaw('<c:legend><c:legendPos val="r"/></c:legend>');
$xmlWriter->writeRaw('<c:legend><c:legendPos val="'.$legendPosition.'"/></c:legend>');
}

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