From 62d3f97e30a5b7262c6655c9a87686254daf5793 Mon Sep 17 00:00:00 2001 From: Frank Meyer Date: Tue, 21 Jul 2015 14:07:23 +0200 Subject: [PATCH 1/3] dded the ability to enable gridlines and axislabels on charts added options 'showAxisLabels', 'showGridX', 'showGridY' to charts style element --- samples/Sample_32_Chart.php | 8 +- src/PhpWord/Style/Chart.php | 90 ++++++++++++++++++++++ src/PhpWord/Writer/Word2007/Part/Chart.php | 9 ++- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/samples/Sample_32_Chart.php b/samples/Sample_32_Chart.php index bc18392e..b6830fa0 100644 --- a/samples/Sample_32_Chart.php +++ b/samples/Sample_32_Chart.php @@ -22,11 +22,16 @@ $categories = array('A', 'B', 'C', 'D', 'E'); $series1 = array(1, 3, 2, 5, 4); $series2 = array(3, 1, 7, 2, 6); $series3 = array(8, 3, 2, 5, 4); +$showGridLines = false; +$showAxisLabels = false; foreach ($chartTypes as $chartType) { $section->addTitle(ucfirst($chartType), 2); $chart = $section->addChart($chartType, $categories, $series1); $chart->getStyle()->setWidth(Converter::inchToEmu(2.5))->setHeight(Converter::inchToEmu(2)); + $chart->getStyle()->setShowGridX($showGridLines); + $chart->getStyle()->setShowGridY($showGridLines); + $chart->getStyle()->setShowAxisLabels($showAxisLabels); if (in_array($chartType, $twoSeries)) { $chart->addSeries($categories, $series2); } @@ -43,7 +48,8 @@ $section = $phpWord->addSection(array('colsNum' => 2, 'breakType' => 'continuous $chartTypes = array('pie', 'bar', 'column', 'line', 'area'); $multiSeries = array('bar', 'column', 'line', 'area'); -$style = array('width' => Converter::cmToEmu(5), 'height' => Converter::cmToEmu(4), '3d' => true); +$style = array('width' => Converter::cmToEmu(5), 'height' => Converter::cmToEmu(4), '3d' => true, + 'showAxisLabels' => $showAxisLabels, 'showGridX' => $showGridLines, 'showGridY' => $showGridLines); foreach ($chartTypes as $chartType) { $section->addTitle(ucfirst($chartType), 2); $chart = $section->addChart($chartType, $categories, $series1, $style); diff --git a/src/PhpWord/Style/Chart.php b/src/PhpWord/Style/Chart.php index 13b72a33..112a0a4a 100644 --- a/src/PhpWord/Style/Chart.php +++ b/src/PhpWord/Style/Chart.php @@ -46,6 +46,27 @@ class Chart extends AbstractStyle */ private $is3d = false; + /** + * Show labels for axis + * + * @var bool + */ + private $showAxisLabels = false; + + /** + * Show Gridlines for Y-Axis + * + * @var bool + */ + private $gridY = false; + + /** + * Show Gridlines for X-Axis + * + * @var bool + */ + private $gridX = false; + /** * Create a new instance * @@ -124,4 +145,73 @@ class Chart extends AbstractStyle return $this; } + + /** + * Show labels for axis + * + * @return bool + */ + public function showAxisLabels() + { + return $this->showAxisLabels; + } + + /** + * Set show Gridlines for Y-Axis + * + * @param bool $value + * @return self + */ + public function setShowAxisLabels($value = true) + { + $this->showAxisLabels = $this->setBoolVal($value, $this->showAxisLabels); + + return $this; + } + + /** + * Show Gridlines for Y-Axis + * + * @return bool + */ + public function showGridY() + { + return $this->gridY; + } + + /** + * Set show Gridlines for Y-Axis + * + * @param bool $value + * @return self + */ + public function setShowGridY($value = true) + { + $this->gridY = $this->setBoolVal($value, $this->gridY); + + return $this; + } + + /** + * Show Gridlines for X-Axis + * + * @return bool + */ + public function showGridX() + { + return $this->gridX; + } + + /** + * Set show Gridlines for X-Axis + * + * @param bool $value + * @return self + */ + public function setShowGridX($value = true) + { + $this->gridX = $this->setBoolVal($value, $this->gridX); + + return $this; + } } diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 8423762c..abc2962a 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -264,6 +264,7 @@ class Chart extends AbstractPart */ private function writeAxis(XMLWriter $xmlWriter, $type) { + $style = $this->element->getStyle(); $types = array( 'cat' => array('c:catAx', 1, 'b', 2), 'val' => array('c:valAx', 2, 'l', 1), @@ -273,7 +274,7 @@ class Chart extends AbstractPart $xmlWriter->startElement($axisType); $xmlWriter->writeElementBlock('c:axId', 'val', $axisId); - $xmlWriter->writeElementBlock('c:axPos', 'val', $axisPos); + $xmlWriter->writeElementBlock('c:axPos', 'val', $axisPos); $xmlWriter->writeElementBlock('c:crossAx', 'val', $axisCross); $xmlWriter->writeElementBlock('c:auto', 'val', 1); @@ -281,10 +282,12 @@ class Chart extends AbstractPart $xmlWriter->writeElementBlock('c:delete', 'val', 0); $xmlWriter->writeElementBlock('c:majorTickMark', 'val', 'none'); $xmlWriter->writeElementBlock('c:minorTickMark', 'val', 'none'); - $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'none'); // nextTo + if($style->showAxisLabels()) + $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'nextTo'); + else $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'none'); $xmlWriter->writeElementBlock('c:crosses', 'val', 'autoZero'); } - if (isset($this->options['radar'])) { + if (isset($this->options['radar']) || ($type == "cat" && $style->showGridX()) || ($type == "val" && $style->showGridY())) { $xmlWriter->writeElement('c:majorGridlines'); } From 3a251770e89d0052b50c6b41551291692d46d559 Mon Sep 17 00:00:00 2001 From: Frank Meyer Date: Tue, 21 Jul 2015 14:56:29 +0200 Subject: [PATCH 2/3] Removed complains of php-codesniffer removed whitespace changed inline if statement --- src/PhpWord/Writer/Word2007/Part/Chart.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index abc2962a..56df1027 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -274,7 +274,7 @@ class Chart extends AbstractPart $xmlWriter->startElement($axisType); $xmlWriter->writeElementBlock('c:axId', 'val', $axisId); - $xmlWriter->writeElementBlock('c:axPos', 'val', $axisPos); + $xmlWriter->writeElementBlock('c:axPos', 'val', $axisPos); $xmlWriter->writeElementBlock('c:crossAx', 'val', $axisCross); $xmlWriter->writeElementBlock('c:auto', 'val', 1); @@ -282,9 +282,12 @@ class Chart extends AbstractPart $xmlWriter->writeElementBlock('c:delete', 'val', 0); $xmlWriter->writeElementBlock('c:majorTickMark', 'val', 'none'); $xmlWriter->writeElementBlock('c:minorTickMark', 'val', 'none'); - if($style->showAxisLabels()) + if($style->showAxisLabels()) { $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'nextTo'); - else $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'none'); + } + else { + $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'none'); + } $xmlWriter->writeElementBlock('c:crosses', 'val', 'autoZero'); } if (isset($this->options['radar']) || ($type == "cat" && $style->showGridX()) || ($type == "val" && $style->showGridY())) { From a6528471c26f280cb74cd1add9e8ae1c5676956a Mon Sep 17 00:00:00 2001 From: Frank Meyer Date: Tue, 21 Jul 2015 15:11:34 +0200 Subject: [PATCH 3/3] changed if statement due to error in codesniffer --- src/PhpWord/Writer/Word2007/Part/Chart.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 56df1027..92d61e6e 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -282,10 +282,9 @@ class Chart extends AbstractPart $xmlWriter->writeElementBlock('c:delete', 'val', 0); $xmlWriter->writeElementBlock('c:majorTickMark', 'val', 'none'); $xmlWriter->writeElementBlock('c:minorTickMark', 'val', 'none'); - if($style->showAxisLabels()) { + if ($style->showAxisLabels()) { $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'nextTo'); - } - else { + } else { $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'none'); } $xmlWriter->writeElementBlock('c:crosses', 'val', 'autoZero');