diff --git a/CHANGELOG.md b/CHANGELOG.md index d1db7a5e..79ae2511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). v0.16.0 (xx xxx 2018) ---------------------- ### Added +- Add setting Chart Title and Legend visibility @Tom-Magill #1433 ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269 diff --git a/docs/styles.rst b/docs/styles.rst index 8c5de7cb..f8d26a9b 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -192,6 +192,14 @@ Available Chart style options: - ``width``. Width (in EMU). - ``height``. Height (in EMU). - ``3d``. Is 3D; applies to pie, bar, line, area, *true* or *false*. +- ``colors``. A list of colors to use in the chart. +- ``title``. The title for the chart. +- ``showLegend``. Show legend, *true* or *false*. +- ``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. +- ``valueAxisTitle``. The title for the values axis. +- ``majorTickMarkPos``. The position for major tick marks, *in*, *out*, *cross*, *none* (default). - ``showAxisLabels``. Show labels for axis, *true* or *false*. - ``gridX``. Show Gridlines for X-Axis, *true* or *false*. - ``gridY``. Show Gridlines for Y-Axis, *true* or *false*. diff --git a/src/PhpWord/Style/Chart.php b/src/PhpWord/Style/Chart.php index 5b02e636..06b4829c 100644 --- a/src/PhpWord/Style/Chart.php +++ b/src/PhpWord/Style/Chart.php @@ -52,6 +52,20 @@ class Chart extends AbstractStyle */ private $colors = array(); + /** + * Chart title + * + * @var string + */ + private $title = null; + + /** + * Chart legend visibility + * + * @var bool + */ + private $showLegend = false; + /** * A list of display options for data labels * @@ -97,9 +111,15 @@ class Chart extends AbstractStyle */ private $valueAxisTitle; + /** + * The position for major tick marks + * Possible values are 'in', 'out', 'cross', 'none' + * + * @var string + */ private $majorTickMarkPos = 'none'; - /* + /** * Show labels for axis * * @var bool @@ -221,6 +241,50 @@ class Chart extends AbstractStyle return $this; } + /** + * Get the chart title + * + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * Set the chart title + * + * @param string $value + */ + public function setTitle($value = null) + { + $this->title = $value; + + return $this; + } + + /** + * Get chart legend visibility + * + * @return bool + */ + public function isShowLegend() + { + return $this->showLegend; + } + + /** + * Set chart legend visibility + * + * @param bool $value + */ + public function setShowLegend($value = false) + { + $this->showLegend = $value; + + return $this; + } + /* * Show labels for axis * @@ -394,8 +458,8 @@ class Chart extends AbstractStyle } /** - * set the position for major tick marks - * @param string $position [description] + * Set the position for major tick marks + * @param string $position */ public function setMajorTickPosition($position) { @@ -403,7 +467,7 @@ class Chart extends AbstractStyle $this->majorTickMarkPos = $this->setEnumVal($position, $enum, $this->majorTickMarkPos); } - /* + /** * Show Gridlines for X-Axis * * @return bool diff --git a/src/PhpWord/Writer/Word2007/Part/Chart.php b/src/PhpWord/Writer/Word2007/Part/Chart.php index 5a3ef276..812d3bf1 100644 --- a/src/PhpWord/Writer/Word2007/Part/Chart.php +++ b/src/PhpWord/Writer/Word2007/Part/Chart.php @@ -105,8 +105,6 @@ class Chart extends AbstractPart { $xmlWriter->startElement('c:chart'); - $xmlWriter->writeElementBlock('c:autoTitleDeleted', 'val', 1); - $this->writePlotArea($xmlWriter); $xmlWriter->endElement(); // c:chart @@ -131,6 +129,34 @@ class Chart extends AbstractPart $style = $this->element->getStyle(); $this->options = $this->types[$type]; + $title = $style->getTitle(); + $showLegend = $style->isShowLegend(); + + //Chart title + if ($title) { + $xmlWriter->startElement('c:title'); + $xmlWriter->startElement('c:tx'); + $xmlWriter->startElement('c:rich'); + $xmlWriter->writeRaw(' + + + + + ' . $title . ' + + '); + $xmlWriter->endElement(); // c:rich + $xmlWriter->endElement(); // c:tx + $xmlWriter->endElement(); // c:title + } else { + $xmlWriter->writeElementBlock('c:autoTitleDeleted', 'val', 1); + } + + //Chart legend + if ($showLegend) { + $xmlWriter->writeRaw(''); + } + $xmlWriter->startElement('c:plotArea'); $xmlWriter->writeElement('c:layout');