Skip to content

Commit 65b0f06

Browse files
jaek-stroosan
authored andcommitted
New features when creating charts (#1332)
* add stacked bar and column charts * add chart colors feature * adding preliminary chart axis title functionality to XMLwriter * added percent_stacked to available types array * Make tick mark and tick label positions configurable * scrutinizer fixes * update changelog
1 parent 94be56b commit 65b0f06

File tree

6 files changed

+531
-21
lines changed

6 files changed

+531
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ v0.15.0 (?? ??? 2018)
2222
- Added the ability to enable gridlines and axislabels on charts @FrankMeyer #576
2323
- Add support for table indent (tblInd) @Trainmaster #1343
2424
- Added parsing of internal links in HTML reader @lalop #1336
25+
- Several improvements to charts @JAEK-S #1332
2526

2627
### Fixed
2728
- Fix reading of docx default style - @troosan #1238

samples/Sample_32_Chart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
$section->addTitle('2D charts', 1);
1717
$section = $phpWord->addSection(array('colsNum' => 2, 'breakType' => 'continuous'));
1818

19-
$chartTypes = array('pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar');
20-
$twoSeries = array('bar', 'column', 'line', 'area', 'scatter', 'radar');
19+
$chartTypes = array('pie', 'doughnut', 'bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
20+
$twoSeries = array('bar', 'column', 'line', 'area', 'scatter', 'radar', 'stacked_bar', 'percent_stacked_bar', 'stacked_column', 'percent_stacked_column');
2121
$threeSeries = array('bar', 'line');
2222
$categories = array('A', 'B', 'C', 'D', 'E');
2323
$series1 = array(1, 3, 2, 5, 4);

src/PhpWord/Element/Chart.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ class Chart extends AbstractElement
6161
* @param array $categories
6262
* @param array $values
6363
* @param array $style
64+
* @param null|mixed $seriesName
6465
*/
65-
public function __construct($type, $categories, $values, $style = null)
66+
public function __construct($type, $categories, $values, $style = null, $seriesName = null)
6667
{
6768
$this->setType($type);
68-
$this->addSeries($categories, $values);
69+
$this->addSeries($categories, $values, $seriesName);
6970
$this->style = $this->setNewStyle(new ChartStyle(), $style, true);
7071
}
7172

@@ -86,7 +87,7 @@ public function getType()
8687
*/
8788
public function setType($value)
8889
{
89-
$enum = array('pie', 'doughnut', 'line', 'bar', 'column', 'area', 'radar', 'scatter');
90+
$enum = array('pie', 'doughnut', 'line', 'bar', 'stacked_bar', 'percent_stacked_bar', 'column', 'stacked_column', 'percent_stacked_column', 'area', 'radar', 'scatter');
9091
$this->type = $this->setEnumVal($value, $enum, 'pie');
9192
}
9293

@@ -95,10 +96,15 @@ public function setType($value)
9596
*
9697
* @param array $categories
9798
* @param array $values
99+
* @param null|mixed $name
98100
*/
99-
public function addSeries($categories, $values)
101+
public function addSeries($categories, $values, $name = null)
100102
{
101-
$this->series[] = array('categories' => $categories, 'values' => $values);
103+
$this->series[] = array(
104+
'categories' => $categories,
105+
'values' => $values,
106+
'name' => $name,
107+
);
102108
}
103109

104110
/**

src/PhpWord/Style/Chart.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,60 @@ class Chart extends AbstractStyle
4646
private $is3d = false;
4747

4848
/**
49+
* A list of colors to use in the chart
50+
*
51+
* @var array
52+
*/
53+
private $colors = array();
54+
55+
/**
56+
* A list of display options for data labels
57+
*
58+
* @var array
59+
*/
60+
private $dataLabelOptions = array(
61+
'showVal' => true, // value
62+
'showCatName' => true, // category name
63+
'showLegendKey' => false, //show the cart legend
64+
'showSerName' => false, // series name
65+
'showPercent' => false,
66+
'showLeaderLines' => false,
67+
'showBubbleSize' => false,
68+
);
69+
70+
/**
71+
* A string that tells the writer where to write chart labels or to skip
72+
* "nextTo" - sets labels next to the axis (bar graphs on the left) (default)
73+
* "low" - labels on the left side of the graph
74+
* "high" - labels on the right side of the graph
75+
*
76+
* @var string
77+
*/
78+
private $categoryLabelPosition = 'nextTo';
79+
80+
/**
81+
* A string that tells the writer where to write chart labels or to skip
82+
* "nextTo" - sets labels next to the axis (bar graphs on the bottom) (default)
83+
* "low" - labels are below the graph
84+
* "high" - labels above the graph
85+
*
86+
* @var string
87+
*/
88+
private $valueLabelPosition = 'nextTo';
89+
90+
/**
91+
* @var string
92+
*/
93+
private $categoryAxisTitle;
94+
95+
/**
96+
* @var string
97+
*/
98+
private $valueAxisTitle;
99+
100+
private $majorTickMarkPos = 'none';
101+
102+
/*
49103
* Show labels for axis
50104
*
51105
* @var bool
@@ -146,6 +200,28 @@ public function set3d($value = true)
146200
}
147201

148202
/**
203+
* Get the list of colors to use in a chart.
204+
*
205+
* @return array
206+
*/
207+
public function getColors()
208+
{
209+
return $this->colors;
210+
}
211+
212+
/**
213+
* Set the colors to use in a chart.
214+
*
215+
* @param array $value a list of colors to use in the chart
216+
*/
217+
public function setColors($value = array())
218+
{
219+
$this->colors = $value;
220+
221+
return $this;
222+
}
223+
224+
/*
149225
* Show labels for axis
150226
*
151227
* @return bool
@@ -169,6 +245,31 @@ public function setShowAxisLabels($value = true)
169245
}
170246

171247
/**
248+
* get the list of options for data labels
249+
*
250+
* @return array
251+
*/
252+
public function getDataLabelOptions()
253+
{
254+
return $this->dataLabelOptions;
255+
}
256+
257+
/**
258+
* Set values for data label options.
259+
* This will only change values for options defined in $this->dataLabelOptions, and cannot create new ones.
260+
*
261+
* @param array $values [description]
262+
*/
263+
public function setDataLabelOptions($values = array())
264+
{
265+
foreach (array_keys($this->dataLabelOptions) as $option) {
266+
if (isset($values[$option])) {
267+
$this->dataLabelOptions[$option] = $this->setBoolVal($values[$option], $this->dataLabelOptions[$option]);
268+
}
269+
}
270+
}
271+
272+
/*
172273
* Show Gridlines for Y-Axis
173274
*
174275
* @return bool
@@ -192,6 +293,117 @@ public function setShowGridY($value = true)
192293
}
193294

194295
/**
296+
* Get the categoryLabelPosition setting
297+
*
298+
* @return string
299+
*/
300+
public function getCategoryLabelPosition()
301+
{
302+
return $this->categoryLabelPosition;
303+
}
304+
305+
/**
306+
* Set the categoryLabelPosition setting
307+
* "none" - skips writing labels
308+
* "nextTo" - sets labels next to the (bar graphs on the left)
309+
* "low" - labels on the left side of the graph
310+
* "high" - labels on the right side of the graph
311+
*
312+
* @param mixed $labelPosition
313+
* @return self
314+
*/
315+
public function setCategoryLabelPosition($labelPosition)
316+
{
317+
$enum = array('nextTo', 'low', 'high');
318+
$this->categoryLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->categoryLabelPosition);
319+
320+
return $this;
321+
}
322+
323+
/**
324+
* Get the valueAxisLabelPosition setting
325+
*
326+
* @return string
327+
*/
328+
public function getValueLabelPosition()
329+
{
330+
return $this->valueLabelPosition;
331+
}
332+
333+
/**
334+
* Set the valueLabelPosition setting
335+
* "none" - skips writing labels
336+
* "nextTo" - sets labels next to the value
337+
* "low" - sets labels are below the graph
338+
* "high" - sets labels above the graph
339+
*
340+
* @param string
341+
* @param mixed $labelPosition
342+
*/
343+
public function setValueLabelPosition($labelPosition)
344+
{
345+
$enum = array('nextTo', 'low', 'high');
346+
$this->valueLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->valueLabelPosition);
347+
348+
return $this;
349+
}
350+
351+
/**
352+
* Get the categoryAxisTitle
353+
* @return string
354+
*/
355+
public function getCategoryAxisTitle()
356+
{
357+
return $this->categoryAxisTitle;
358+
}
359+
360+
/**
361+
* Set the title that appears on the category side of the chart
362+
* @param string $axisTitle
363+
*/
364+
public function setCategoryAxisTitle($axisTitle)
365+
{
366+
$this->categoryAxisTitle = $axisTitle;
367+
368+
return $this;
369+
}
370+
371+
/**
372+
* Get the valueAxisTitle
373+
* @return string
374+
*/
375+
public function getValueAxisTitle()
376+
{
377+
return $this->valueAxisTitle;
378+
}
379+
380+
/**
381+
* Set the title that appears on the value side of the chart
382+
* @param string $axisTitle
383+
*/
384+
public function setValueAxisTitle($axisTitle)
385+
{
386+
$this->valueAxisTitle = $axisTitle;
387+
388+
return $this;
389+
}
390+
391+
public function getMajorTickPosition()
392+
{
393+
return $this->majorTickMarkPos;
394+
}
395+
396+
/**
397+
* set the position for major tick marks
398+
* @param string $position [description]
399+
*/
400+
public function setMajorTickPosition($position)
401+
{
402+
$enum = array('in', 'out', 'cross', 'none');
403+
$this->majorTickMarkPos = $this->setEnumVal($position, $enum, $this->majorTickMarkPos);
404+
}
405+
406+
/*
195407
* Show Gridlines for X-Axis
196408
*
197409
* @return bool

0 commit comments

Comments
 (0)