From 9e572ecd9b44dd5dc1bcd0f1497fdaa72bdeb00b Mon Sep 17 00:00:00 2001 From: Stephan212 <48237559+Stephan212@users.noreply.github.com> Date: Wed, 21 Aug 2019 10:18:33 +0100 Subject: [PATCH 1/7] 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 --- src/PhpWord/Style/Chart.php | 35 ++++++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Chart.php | 3 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Style/Chart.php b/src/PhpWord/Style/Chart.php index 06b4829c..7b271586 100644 --- a/src/PhpWord/Style/Chart.php +++ b/src/PhpWord/Style/Chart.php @@ -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 * @@ -285,6 +292,34 @@ class Chart extends AbstractStyle 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 + * + * default: right + * + * @param bool $value + */ + public function setLegendPosition($value = 'r') + { + $this->legendPosition = $value; + + return $this; + } + /* * Show labels for axis * diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 812d3bf1..dd738d1e 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -131,6 +131,7 @@ class Chart extends AbstractPart $title = $style->getTitle(); $showLegend = $style->isShowLegend(); + $legendPosition = $style->getLegendPosition(); //Chart title if ($title) { @@ -154,7 +155,7 @@ class Chart extends AbstractPart //Chart legend if ($showLegend) { - $xmlWriter->writeRaw(''); + $xmlWriter->writeRaw(''); } $xmlWriter->startElement('c:plotArea'); From 90a8900208c9a4238a85993cac241dcd89d85e5a Mon Sep 17 00:00:00 2001 From: Stephan212 <48237559+Stephan212@users.noreply.github.com> Date: Wed, 21 Aug 2019 10:54:43 +0100 Subject: [PATCH 2/7] Stephan212 chart dynamic legend position (#2) * 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 Syle/Chart.php Add in the public functions to getStyle() to get and set the legend position --- src/PhpWord/Style/Chart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PhpWord/Style/Chart.php b/src/PhpWord/Style/Chart.php index 7b271586..f6de7c22 100644 --- a/src/PhpWord/Style/Chart.php +++ b/src/PhpWord/Style/Chart.php @@ -308,6 +308,7 @@ class Chart extends AbstractStyle * "b" - bottom of chart * "t" - top of chart * "l" - left of chart + * "tr" - top right of chart * * default: right * From 6f1b18937c89df8b27956f788c731050341dbbfb Mon Sep 17 00:00:00 2001 From: csk83 Date: Wed, 26 Aug 2020 12:50:08 +0800 Subject: [PATCH 3/7] code format --- src/PhpWord/Writer/Word2007/Part/Chart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index dd738d1e..b661a102 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -155,7 +155,7 @@ class Chart extends AbstractPart //Chart legend if ($showLegend) { - $xmlWriter->writeRaw(''); + $xmlWriter->writeRaw(''); } $xmlWriter->startElement('c:plotArea'); From d42854ddb6c0083622c7905cb607f453b01aa0c3 Mon Sep 17 00:00:00 2001 From: csk83 Date: Sat, 29 Aug 2020 20:41:25 +0800 Subject: [PATCH 4/7] chart doc/sample/code comments --- docs/styles.rst | 1 + samples/Sample_32_Chart.php | 5 +++++ src/PhpWord/Style/Chart.php | 19 ++++++++++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/docs/styles.rst b/docs/styles.rst index 18a9c2ec..06d983e5 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -201,6 +201,7 @@ Available Chart style options: - ``colors``. A list of colors to use in the chart. - ``title``. The title for the chart. - ``showLegend``. Show legend, *true* or *false*. +- ``LegendPosition``. Legend position, *r* (default), *b*, *t*, *l* or *tr*. - ``categoryLabelPosition``. Label position for categories, *nextTo* (default), *low* or *high*. - ``valueLabelPosition``. Label position for values, *nextTo* (default), *low* or *high*. - ``categoryAxisTitle``. The title for the category axis. diff --git a/samples/Sample_32_Chart.php b/samples/Sample_32_Chart.php index c24a6f8e..bfb9ffe8 100644 --- a/samples/Sample_32_Chart.php +++ b/samples/Sample_32_Chart.php @@ -25,6 +25,9 @@ $series2 = array(3, 1, 7, 2, 6); $series3 = array(8, 3, 2, 5, 4); $showGridLines = false; $showAxisLabels = false; +$showLegend = true; +$legendPosition = 't'; +// r = right, l = left, t = top, b = bottom, tr = top right foreach ($chartTypes as $chartType) { $section->addTitle(ucfirst($chartType), 2); @@ -33,6 +36,8 @@ foreach ($chartTypes as $chartType) { $chart->getStyle()->setShowGridX($showGridLines); $chart->getStyle()->setShowGridY($showGridLines); $chart->getStyle()->setShowAxisLabels($showAxisLabels); + $chart->getStyle()->setShowLegend($showLegend); + $chart->getStyle()->setLegendPosition($legendPosition); if (in_array($chartType, $twoSeries)) { $chart->addSeries($categories, $series2); } diff --git a/src/PhpWord/Style/Chart.php b/src/PhpWord/Style/Chart.php index f6de7c22..c02c0af4 100644 --- a/src/PhpWord/Style/Chart.php +++ b/src/PhpWord/Style/Chart.php @@ -67,7 +67,8 @@ class Chart extends AbstractStyle private $showLegend = false; /** - * Chart legend Position. + * Chart legend Position. + * Possible values are 'r', 't', 'b', 'l', 'tr' * * @var string */ @@ -240,6 +241,7 @@ class Chart extends AbstractStyle * Set the colors to use in a chart. * * @param array $value a list of colors to use in the chart + * @return self */ public function setColors($value = array()) { @@ -262,6 +264,7 @@ class Chart extends AbstractStyle * Set the chart title * * @param string $value + * @return self */ public function setTitle($value = null) { @@ -284,6 +287,7 @@ class Chart extends AbstractStyle * Set chart legend visibility * * @param bool $value + * @return self */ public function setShowLegend($value = false) { @@ -312,11 +316,13 @@ class Chart extends AbstractStyle * * default: right * - * @param bool $value + * @param string $legendPosition + * @return self */ - public function setLegendPosition($value = 'r') + public function setLegendPosition($legendPosition = 'r') { - $this->legendPosition = $value; + $enum = array('r', 'b', 't', 'l', 'tr'); + $this->legendPosition = $this->setEnumVal($legendPosition, $enum, $this->legendPosition); return $this; } @@ -364,7 +370,10 @@ class Chart extends AbstractStyle { foreach (array_keys($this->dataLabelOptions) as $option) { if (isset($values[$option])) { - $this->dataLabelOptions[$option] = $this->setBoolVal($values[$option], $this->dataLabelOptions[$option]); + $this->dataLabelOptions[$option] = $this->setBoolVal( + $values[$option], + $this->dataLabelOptions[$option] + ); } } } From 9a59cf0590e25b1d018712fd4e6f55fcd1ffe97b Mon Sep 17 00:00:00 2001 From: csk83 Date: Mon, 31 Aug 2020 11:56:21 +0800 Subject: [PATCH 5/7] format check --- src/PhpWord/Element/AbstractContainer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 0c773dbe..f9451c8d 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -108,7 +108,8 @@ abstract class AbstractContainer extends AbstractElement } } else { // All other elements - array_unshift($args, $element); // Prepend element name to the beginning of args array + array_unshift($args, $element); + // Prepend element name to the beginning of args array return call_user_func_array(array($this, 'addElement'), $args); } } From e1bbc85cc4b8f5c078a3dba23b7d1332e46dc9d4 Mon Sep 17 00:00:00 2001 From: troosan Date: Mon, 8 Feb 2021 22:40:46 +0100 Subject: [PATCH 6/7] Make syntax php 5.3 compliant --- tests/PhpWord/Shared/HtmlTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 7a806c26..05e2398a 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -639,9 +639,9 @@ class HtmlTest extends AbstractWebServerEmbeddedTest public function testParseTableAndCellWidth() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); - $section = $phpWord->addSection([ + $section = $phpWord->addSection(array( 'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE, - ]); + )); // borders & backgrounds are here just for better visual comparison $html = << Date: Mon, 8 Feb 2021 22:58:14 +0100 Subject: [PATCH 7/7] fix code formatting --- src/PhpWord/Shared/Html.php | 48 +++++++++++++++---------------- tests/PhpWord/Shared/HtmlTest.php | 32 ++++++++++----------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 6fa2a64d..ac90b5f0 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -115,7 +115,7 @@ class Html // tables, cells if (false !== strpos($val, '%')) { // e.g. or
- $styles['width'] = intval($val) * 50; + $styles['width'] = (int) $val * 50; $styles['unit'] = \PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT; } else { // e.g. , where "2" = 2px (always pixels) - $val = intval($val).'px'; + $val = (int) $val . 'px'; $styles['cellSpacing'] = Converter::cssToTwip($val); break; case 'bgcolor': @@ -693,7 +693,7 @@ class Html // This may be adjusted, if better ratio or formula found. // BC change: up to ver. 0.17.0 was $size converted to points - Converter::cssToPoint($size) $size = Converter::cssToTwip($matches[1]); - $size = intval($size / 2); + $size = (int) ($size / 2); // valid variants may be e.g. borderSize, borderTopSize, borderLeftColor, etc .. $styles["border{$which}Size"] = $size; // twips $styles["border{$which}Color"] = trim($matches[2], '#'); @@ -871,9 +871,9 @@ class Html } /** - * Transforms a HTML/CSS alignment into a \PhpOffice\PhpWord\SimpleType\Jc + * Transforms a HTML/CSS vertical alignment * - * @param string $cssAlignment + * @param string $alignment * @return string|null */ protected static function mapAlignVertical($alignment) @@ -901,10 +901,10 @@ class Html } /** - * Map list style for ordered list - * - * @param string $cssListType - */ + * Map list style for ordered list + * + * @param string $cssListType + */ protected static function mapListType($cssListType) { switch ($cssListType) { @@ -959,12 +959,12 @@ class Html } /** - * Render horizontal rule - * Note: Word rule is not the same as HTML's
since it does not support width and thus neither alignment - * - * @param \DOMNode $node - * @param \PhpOffice\PhpWord\Element\AbstractContainer $element - */ + * Render horizontal rule + * Note: Word rule is not the same as HTML's
since it does not support width and thus neither alignment + * + * @param \DOMNode $node + * @param \PhpOffice\PhpWord\Element\AbstractContainer $element + */ protected static function parseHorizRule($node, $element) { $styles = self::parseInlineStyle($node); @@ -972,19 +972,19 @@ class Html //
is implemented as an empty paragraph - extending 100% inside the section // Some properties may be controlled, e.g.
- $fontStyle = $styles + ['size' => 3]; + $fontStyle = $styles + array('size' => 3); - $paragraphStyle = $styles + [ - 'lineHeight' => 0.25, // multiply default line height - e.g. 1, 1.5 etc - 'spacing' => 0, // twip - 'spaceBefore' => 120, // twip, 240/2 (default line height) - 'spaceAfter' => 120, // twip - 'borderBottomSize' => empty($styles['line-height']) ? 1 : $styles['line-height'], + $paragraphStyle = $styles + array( + 'lineHeight' => 0.25, // multiply default line height - e.g. 1, 1.5 etc + 'spacing' => 0, // twip + 'spaceBefore' => 120, // twip, 240/2 (default line height) + 'spaceAfter' => 120, // twip + 'borderBottomSize' => empty($styles['line-height']) ? 1 : $styles['line-height'], 'borderBottomColor' => empty($styles['color']) ? '000000' : $styles['color'], 'borderBottomStyle' => 'single', // same as "solid" - ]; + ); - $element->addText("", $fontStyle, $paragraphStyle); + $element->addText('', $fontStyle, $paragraphStyle); // Notes:
cannot be: // - table - throws error "cannot be inside textruns", e.g. lists diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 05e2398a..9f355b67 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -634,8 +634,8 @@ class HtmlTest extends AbstractWebServerEmbeddedTest } /** - * Parse widths in tables and cells, which also allows for controlling column width - */ + * Parse widths in tables and cells, which also allows for controlling column width + */ public function testParseTableAndCellWidth() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); @@ -703,14 +703,14 @@ HTML; } /** - * Test parsing background color for table rows and table cellspacing - */ + * Test parsing background color for table rows and table cellspacing + */ public function testParseCellspacingRowBgColor() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); - $section = $phpWord->addSection([ + $section = $phpWord->addSection(array( 'orientation' => \PhpOffice\PhpWord\Style\Section::ORIENTATION_LANDSCAPE, - ]); + )); // borders & backgrounds are here just for better visual comparison $html = <<assertTrue($doc->elementExists($xpath)); $this->assertEquals('single', $doc->getElement($xpath)->getAttribute('w:val')); - $this->assertEquals(intval(5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz')); + $this->assertEquals((int) (5 * 15 / 2), $doc->getElement($xpath)->getAttribute('w:sz')); $this->assertEquals('lightblue', $doc->getElement($xpath)->getAttribute('w:color')); $xpath = '/w:document/w:body/w:p[4]/w:pPr/w:spacing'; @@ -785,8 +785,8 @@ HTML; } /** - * Parse ordered list start & numbering style - */ + * Parse ordered list start & numbering style + */ public function testParseOrderedList() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); @@ -846,8 +846,8 @@ HTML; } /** - * Parse ordered list start & numbering style - */ + * Parse ordered list start & numbering style + */ public function testParseVerticalAlign() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); @@ -886,8 +886,8 @@ HTML; } /** - * Fix bug - don't decode double quotes inside double quoted string - */ + * Fix bug - don't decode double quotes inside double quoted string + */ public function testDontDecodeAlreadyEncodedDoubleQuotes() { $phpWord = new \PhpOffice\PhpWord\PhpWord(); @@ -900,6 +900,6 @@ HTML; Html::addHtml($section, $html); $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); - $this->assertTrue(is_object($doc)); + $this->assertInternalType('object', $doc); } }