Skip to content

Commit 4840faa

Browse files
authored
Merge pull request #1922 from csk83/develop
Add Option for Dynamic Chart Legend Position #1699
2 parents 1c3fdd2 + 1b7b43a commit 4840faa

File tree

6 files changed

+96
-44
lines changed

6 files changed

+96
-44
lines changed

docs/styles.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Available Chart style options:
201201
- ``colors``. A list of colors to use in the chart.
202202
- ``title``. The title for the chart.
203203
- ``showLegend``. Show legend, *true* or *false*.
204+
- ``LegendPosition``. Legend position, *r* (default), *b*, *t*, *l* or *tr*.
204205
- ``categoryLabelPosition``. Label position for categories, *nextTo* (default), *low* or *high*.
205206
- ``valueLabelPosition``. Label position for values, *nextTo* (default), *low* or *high*.
206207
- ``categoryAxisTitle``. The title for the category axis.

samples/Sample_32_Chart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
$series3 = array(8, 3, 2, 5, 4);
2626
$showGridLines = false;
2727
$showAxisLabels = false;
28+
$showLegend = true;
29+
$legendPosition = 't';
30+
// r = right, l = left, t = top, b = bottom, tr = top right
2831

2932
foreach ($chartTypes as $chartType) {
3033
$section->addTitle(ucfirst($chartType), 2);
@@ -33,6 +36,8 @@
3336
$chart->getStyle()->setShowGridX($showGridLines);
3437
$chart->getStyle()->setShowGridY($showGridLines);
3538
$chart->getStyle()->setShowAxisLabels($showAxisLabels);
39+
$chart->getStyle()->setShowLegend($showLegend);
40+
$chart->getStyle()->setLegendPosition($legendPosition);
3641
if (in_array($chartType, $twoSeries)) {
3742
$chart->addSeries($categories, $series2);
3843
}

src/PhpWord/Shared/Html.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected static function parseInlineStyle($node, $styles = array())
115115
// tables, cells
116116
if (false !== strpos($val, '%')) {
117117
// e.g. <table width="100%"> or <td width="50%">
118-
$styles['width'] = intval($val) * 50;
118+
$styles['width'] = (int) $val * 50;
119119
$styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT;
120120
} else {
121121
// e.g. <table width="250> where "250" = 250px (always pixels)
@@ -125,7 +125,7 @@ protected static function parseInlineStyle($node, $styles = array())
125125
break;
126126
case 'cellspacing':
127127
// tables e.g. <table cellspacing="2">, where "2" = 2px (always pixels)
128-
$val = intval($val).'px';
128+
$val = (int) $val . 'px';
129129
$styles['cellSpacing'] = Converter::cssToTwip($val);
130130
break;
131131
case 'bgcolor':
@@ -729,7 +729,7 @@ protected static function parseStyle($attribute, $styles)
729729
// This may be adjusted, if better ratio or formula found.
730730
// BC change: up to ver. 0.17.0 was $size converted to points - Converter::cssToPoint($size)
731731
$size = Converter::cssToTwip($matches[1]);
732-
$size = intval($size / 2);
732+
$size = (int) ($size / 2);
733733
// valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc ..
734734
$styles["border{$which}Size"] = $size; // twips
735735
$styles["border{$which}Color"] = trim($matches[2], '#');
@@ -907,9 +907,9 @@ protected static function mapAlign($cssAlignment)
907907
}
908908

909909
/**
910-
* Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc
910+
* Transforms a HTML/CSS vertical alignment
911911
*
912-
* @param string $cssAlignment
912+
* @param string $alignment
913913
* @return string|null
914914
*/
915915
protected static function mapAlignVertical($alignment)
@@ -937,10 +937,10 @@ protected static function mapAlignVertical($alignment)
937937
}
938938

939939
/**
940-
* Map list style for ordered list
941-
*
942-
* @param string $cssListType
943-
*/
940+
* Map list style for ordered list
941+
*
942+
* @param string $cssListType
943+
*/
944944
protected static function mapListType($cssListType)
945945
{
946946
switch ($cssListType) {
@@ -995,32 +995,32 @@ protected static function parseLink($node, $element, &$styles)
995995
}
996996

997997
/**
998-
* Render horizontal rule
999-
* Note: Word rule is not the same as HTML's <hr> since it does not support width and thus neither alignment
1000-
*
1001-
* @param \DOMNode $node
1002-
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
1003-
*/
998+
* Render horizontal rule
999+
* Note: Word rule is not the same as HTML's <hr> since it does not support width and thus neither alignment
1000+
*
1001+
* @param \DOMNode $node
1002+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
1003+
*/
10041004
protected static function parseHorizRule($node, $element)
10051005
{
10061006
$styles = self::parseInlineStyle($node);
10071007

10081008
// <hr> is implemented as an empty paragraph - extending 100% inside the section
10091009
// Some properties may be controlled, e.g. <hr style="border-bottom: 3px #DDDDDD solid; margin-bottom: 0;">
10101010

1011-
$fontStyle = $styles + ['size' => 3];
1011+
$fontStyle = $styles + array('size' => 3);
10121012

1013-
$paragraphStyle = $styles + [
1014-
'lineHeight' => 0.25, // multiply default line height - e.g. 1, 1.5 etc
1015-
'spacing' => 0, // twip
1016-
'spaceBefore' => 120, // twip, 240/2 (default line height)
1017-
'spaceAfter' => 120, // twip
1018-
'borderBottomSize' => empty($styles['line-height']) ? 1 : $styles['line-height'],
1013+
$paragraphStyle = $styles + array(
1014+
'lineHeight' => 0.25, // multiply default line height - e.g. 1, 1.5 etc
1015+
'spacing' => 0, // twip
1016+
'spaceBefore' => 120, // twip, 240/2 (default line height)
1017+
'spaceAfter' => 120, // twip
1018+
'borderBottomSize' => empty($styles['line-height']) ? 1 : $styles['line-height'],
10191019
'borderBottomColor' => empty($styles['color']) ? '000000' : $styles['color'],
10201020
'borderBottomStyle' => 'single', // same as "solid"
1021-
];
1021+
);
10221022

1023-
$element->addText("", $fontStyle, $paragraphStyle);
1023+
$element->addText('', $fontStyle, $paragraphStyle);
10241024

10251025
// Notes: <hr/> cannot be:
10261026
// - table - throws error "cannot be inside textruns", e.g. lists

src/PhpWord/Style/Chart.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class Chart extends AbstractStyle
6666
*/
6767
private $showLegend = false;
6868

69+
/**
70+
* Chart legend Position.
71+
* Possible values are 'r', 't', 'b', 'l', 'tr'
72+
*
73+
* @var string
74+
*/
75+
private $legendPosition = 'r';
76+
6977
/**
7078
* A list of display options for data labels
7179
*
@@ -233,6 +241,7 @@ public function getColors()
233241
* Set the colors to use in a chart.
234242
*
235243
* @param array $value a list of colors to use in the chart
244+
* @return self
236245
*/
237246
public function setColors($value = array())
238247
{
@@ -255,6 +264,7 @@ public function getTitle()
255264
* Set the chart title
256265
*
257266
* @param string $value
267+
* @return self
258268
*/
259269
public function setTitle($value = null)
260270
{
@@ -277,6 +287,7 @@ public function isShowLegend()
277287
* Set chart legend visibility
278288
*
279289
* @param bool $value
290+
* @return self
280291
*/
281292
public function setShowLegend($value = false)
282293
{
@@ -285,6 +296,37 @@ public function setShowLegend($value = false)
285296
return $this;
286297
}
287298

299+
/**
300+
* Get chart legend position
301+
*
302+
* @return string
303+
*/
304+
public function getLegendPosition()
305+
{
306+
return $this->legendPosition;
307+
}
308+
309+
/**
310+
* Set chart legend position. choices:
311+
* "r" - right of chart
312+
* "b" - bottom of chart
313+
* "t" - top of chart
314+
* "l" - left of chart
315+
* "tr" - top right of chart
316+
*
317+
* default: right
318+
*
319+
* @param string $legendPosition
320+
* @return self
321+
*/
322+
public function setLegendPosition($legendPosition = 'r')
323+
{
324+
$enum = array('r', 'b', 't', 'l', 'tr');
325+
$this->legendPosition = $this->setEnumVal($legendPosition, $enum, $this->legendPosition);
326+
327+
return $this;
328+
}
329+
288330
/*
289331
* Show labels for axis
290332
*
@@ -328,7 +370,10 @@ public function setDataLabelOptions($values = array())
328370
{
329371
foreach (array_keys($this->dataLabelOptions) as $option) {
330372
if (isset($values[$option])) {
331-
$this->dataLabelOptions[$option] = $this->setBoolVal($values[$option], $this->dataLabelOptions[$option]);
373+
$this->dataLabelOptions[$option] = $this->setBoolVal(
374+
$values[$option],
375+
$this->dataLabelOptions[$option]
376+
);
332377
}
333378
}
334379
}

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');

tests/PhpWord/Shared/HtmlTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -640,14 +640,14 @@ public function testParseLetterSpacing()
640640
}
641641

642642
/**
643-
* Parse widths in tables and cells, which also allows for controlling column width
644-
*/
643+
* Parse widths in tables and cells, which also allows for controlling column width
644+
*/
645645
public function testParseTableAndCellWidth()
646646
{
647647
$phpWord = new \PhpOffice\PhpWord\PhpWord();
648-
$section = $phpWord->addSection([
648+
$section = $phpWord->addSection(array(
649649
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
650-
]);
650+
));
651651

652652
// borders & backgrounds are here just for better visual comparison
653653
$html = <<<HTML
@@ -709,14 +709,14 @@ public function testParseTableAndCellWidth()
709709
}
710710

711711
/**
712-
* Test parsing background color for table rows and table cellspacing
713-
*/
712+
* Test parsing background color for table rows and table cellspacing
713+
*/
714714
public function testParseCellspacingRowBgColor()
715715
{
716716
$phpWord = new \PhpOffice\PhpWord\PhpWord();
717-
$section = $phpWord->addSection([
717+
$section = $phpWord->addSection(array(
718718
'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE,
719-
]);
719+
));
720720

721721
// borders & backgrounds are here just for better visual comparison
722722
$html = <<<HTML
@@ -750,8 +750,8 @@ public function testParseCellspacingRowBgColor()
750750
}
751751

752752
/**
753-
* Parse horizontal rule
754-
*/
753+
* Parse horizontal rule
754+
*/
755755
public function testParseHorizRule()
756756
{
757757
$phpWord = new \PhpOffice\PhpWord\PhpWord();
@@ -780,7 +780,7 @@ public function testParseHorizRule()
780780
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:pBdr/w:bottom';
781781
$this->assertTrue($doc->elementExists($xpath));
782782
$this->assertEquals('single', $doc->getElement($xpath)->getAttribute('w:val'));
783-
$this->assertEquals(intval(5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz'));
783+
$this->assertEquals((int) (5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz'));
784784
$this->assertEquals('lightblue', $doc->getElement($xpath)->getAttribute('w:color'));
785785

786786
$xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing';
@@ -791,8 +791,8 @@ public function testParseHorizRule()
791791
}
792792

793793
/**
794-
* Parse ordered list start & numbering style
795-
*/
794+
* Parse ordered list start & numbering style
795+
*/
796796
public function testParseOrderedList()
797797
{
798798
$phpWord = new \PhpOffice\PhpWord\PhpWord();
@@ -852,8 +852,8 @@ public function testParseOrderedList()
852852
}
853853

854854
/**
855-
* Parse ordered list start & numbering style
856-
*/
855+
* Parse ordered list start & numbering style
856+
*/
857857
public function testParseVerticalAlign()
858858
{
859859
$phpWord = new \PhpOffice\PhpWord\PhpWord();
@@ -892,8 +892,8 @@ public function testParseVerticalAlign()
892892
}
893893

894894
/**
895-
* Fix bug - don't decode double quotes inside double quoted string
896-
*/
895+
* Fix bug - don't decode double quotes inside double quoted string
896+
*/
897897
public function testDontDecodeAlreadyEncodedDoubleQuotes()
898898
{
899899
$phpWord = new \PhpOffice\PhpWord\PhpWord();
@@ -906,6 +906,6 @@ public function testDontDecodeAlreadyEncodedDoubleQuotes()
906906

907907
Html::addHtml($section, $html);
908908
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
909-
$this->assertTrue(is_object($doc));
909+
$this->assertInternalType('object', $doc);
910910
}
911911
}

0 commit comments

Comments
 (0)