Skip to content

Commit c177c55

Browse files
authored
Merge pull request #1433 from Tom-Magill/develop
Add feature to set Chart Title and Legend visibility
2 parents fb60865 + 9f684c7 commit c177c55

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
v0.16.0 (xx xxx 2018)
77
----------------------
88
### Added
9+
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
910

1011
### Fixed
1112
- Fix regex in `cloneBlock` function @nicoder #1269

docs/styles.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ Available Chart style options:
192192
- ``width``. Width (in EMU).
193193
- ``height``. Height (in EMU).
194194
- ``3d``. Is 3D; applies to pie, bar, line, area, *true* or *false*.
195+
- ``colors``. A list of colors to use in the chart.
196+
- ``title``. The title for the chart.
197+
- ``showLegend``. Show legend, *true* or *false*.
198+
- ``categoryLabelPosition``. Label position for categories, *nextTo* (default), *low* or *high*.
199+
- ``valueLabelPosition``. Label position for values, *nextTo* (default), *low* or *high*.
200+
- ``categoryAxisTitle``. The title for the category axis.
201+
- ``valueAxisTitle``. The title for the values axis.
202+
- ``majorTickMarkPos``. The position for major tick marks, *in*, *out*, *cross*, *none* (default).
195203
- ``showAxisLabels``. Show labels for axis, *true* or *false*.
196204
- ``gridX``. Show Gridlines for X-Axis, *true* or *false*.
197205
- ``gridY``. Show Gridlines for Y-Axis, *true* or *false*.

src/PhpWord/Style/Chart.php

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ class Chart extends AbstractStyle
5252
*/
5353
private $colors = array();
5454

55+
/**
56+
* Chart title
57+
*
58+
* @var string
59+
*/
60+
private $title = null;
61+
62+
/**
63+
* Chart legend visibility
64+
*
65+
* @var bool
66+
*/
67+
private $showLegend = false;
68+
5569
/**
5670
* A list of display options for data labels
5771
*
@@ -97,9 +111,15 @@ class Chart extends AbstractStyle
97111
*/
98112
private $valueAxisTitle;
99113

114+
/**
115+
* The position for major tick marks
116+
* Possible values are 'in', 'out', 'cross', 'none'
117+
*
118+
* @var string
119+
*/
100120
private $majorTickMarkPos = 'none';
101121

102-
/*
122+
/**
103123
* Show labels for axis
104124
*
105125
* @var bool
@@ -221,6 +241,50 @@ public function setColors($value = array())
221241
return $this;
222242
}
223243

244+
/**
245+
* Get the chart title
246+
*
247+
* @return string
248+
*/
249+
public function getTitle()
250+
{
251+
return $this->title;
252+
}
253+
254+
/**
255+
* Set the chart title
256+
*
257+
* @param string $value
258+
*/
259+
public function setTitle($value = null)
260+
{
261+
$this->title = $value;
262+
263+
return $this;
264+
}
265+
266+
/**
267+
* Get chart legend visibility
268+
*
269+
* @return bool
270+
*/
271+
public function isShowLegend()
272+
{
273+
return $this->showLegend;
274+
}
275+
276+
/**
277+
* Set chart legend visibility
278+
*
279+
* @param bool $value
280+
*/
281+
public function setShowLegend($value = false)
282+
{
283+
$this->showLegend = $value;
284+
285+
return $this;
286+
}
287+
224288
/*
225289
* Show labels for axis
226290
*
@@ -394,16 +458,16 @@ public function getMajorTickPosition()
394458
}
395459

396460
/**
397-
* set the position for major tick marks
398-
* @param string $position [description]
461+
* Set the position for major tick marks
462+
* @param string $position
399463
*/
400464
public function setMajorTickPosition($position)
401465
{
402466
$enum = array('in', 'out', 'cross', 'none');
403467
$this->majorTickMarkPos = $this->setEnumVal($position, $enum, $this->majorTickMarkPos);
404468
}
405469

406-
/*
470+
/**
407471
* Show Gridlines for X-Axis
408472
*
409473
* @return bool

src/PhpWord/Writer/Word2007/Part/Chart.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ private function writeChart(XMLWriter $xmlWriter)
105105
{
106106
$xmlWriter->startElement('c:chart');
107107

108-
$xmlWriter->writeElementBlock('c:autoTitleDeleted', 'val', 1);
109-
110108
$this->writePlotArea($xmlWriter);
111109

112110
$xmlWriter->endElement(); // c:chart
@@ -131,6 +129,34 @@ private function writePlotArea(XMLWriter $xmlWriter)
131129
$style = $this->element->getStyle();
132130
$this->options = $this->types[$type];
133131

132+
$title = $style->getTitle();
133+
$showLegend = $style->isShowLegend();
134+
135+
//Chart title
136+
if ($title) {
137+
$xmlWriter->startElement('c:title');
138+
$xmlWriter->startElement('c:tx');
139+
$xmlWriter->startElement('c:rich');
140+
$xmlWriter->writeRaw('
141+
<a:bodyPr/>
142+
<a:lstStyle/>
143+
<a:p>
144+
<a:pPr>
145+
<a:defRPr/></a:pPr><a:r><a:rPr/><a:t>' . $title . '</a:t></a:r>
146+
<a:endParaRPr/>
147+
</a:p>');
148+
$xmlWriter->endElement(); // c:rich
149+
$xmlWriter->endElement(); // c:tx
150+
$xmlWriter->endElement(); // c:title
151+
} else {
152+
$xmlWriter->writeElementBlock('c:autoTitleDeleted', 'val', 1);
153+
}
154+
155+
//Chart legend
156+
if ($showLegend) {
157+
$xmlWriter->writeRaw('<c:legend><c:legendPos val="r"/></c:legend>');
158+
}
159+
134160
$xmlWriter->startElement('c:plotArea');
135161
$xmlWriter->writeElement('c:layout');
136162

0 commit comments

Comments
 (0)