Skip to content

Commit 62d3f97

Browse files
committed
dded the ability to enable gridlines and axislabels on charts
added options 'showAxisLabels', 'showGridX', 'showGridY' to charts style element
1 parent cd2e7f7 commit 62d3f97

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

samples/Sample_32_Chart.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222
$series1 = array(1, 3, 2, 5, 4);
2323
$series2 = array(3, 1, 7, 2, 6);
2424
$series3 = array(8, 3, 2, 5, 4);
25+
$showGridLines = false;
26+
$showAxisLabels = false;
2527

2628
foreach ($chartTypes as $chartType) {
2729
$section->addTitle(ucfirst($chartType), 2);
2830
$chart = $section->addChart($chartType, $categories, $series1);
2931
$chart->getStyle()->setWidth(Converter::inchToEmu(2.5))->setHeight(Converter::inchToEmu(2));
32+
$chart->getStyle()->setShowGridX($showGridLines);
33+
$chart->getStyle()->setShowGridY($showGridLines);
34+
$chart->getStyle()->setShowAxisLabels($showAxisLabels);
3035
if (in_array($chartType, $twoSeries)) {
3136
$chart->addSeries($categories, $series2);
3237
}
@@ -43,7 +48,8 @@
4348

4449
$chartTypes = array('pie', 'bar', 'column', 'line', 'area');
4550
$multiSeries = array('bar', 'column', 'line', 'area');
46-
$style = array('width' => Converter::cmToEmu(5), 'height' => Converter::cmToEmu(4), '3d' => true);
51+
$style = array('width' => Converter::cmToEmu(5), 'height' => Converter::cmToEmu(4), '3d' => true,
52+
'showAxisLabels' => $showAxisLabels, 'showGridX' => $showGridLines, 'showGridY' => $showGridLines);
4753
foreach ($chartTypes as $chartType) {
4854
$section->addTitle(ucfirst($chartType), 2);
4955
$chart = $section->addChart($chartType, $categories, $series1, $style);

src/PhpWord/Style/Chart.php

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

49+
/**
50+
* Show labels for axis
51+
*
52+
* @var bool
53+
*/
54+
private $showAxisLabels = false;
55+
56+
/**
57+
* Show Gridlines for Y-Axis
58+
*
59+
* @var bool
60+
*/
61+
private $gridY = false;
62+
63+
/**
64+
* Show Gridlines for X-Axis
65+
*
66+
* @var bool
67+
*/
68+
private $gridX = false;
69+
4970
/**
5071
* Create a new instance
5172
*
@@ -124,4 +145,73 @@ public function set3d($value = true)
124145

125146
return $this;
126147
}
148+
149+
/**
150+
* Show labels for axis
151+
*
152+
* @return bool
153+
*/
154+
public function showAxisLabels()
155+
{
156+
return $this->showAxisLabels;
157+
}
158+
159+
/**
160+
* Set show Gridlines for Y-Axis
161+
*
162+
* @param bool $value
163+
* @return self
164+
*/
165+
public function setShowAxisLabels($value = true)
166+
{
167+
$this->showAxisLabels = $this->setBoolVal($value, $this->showAxisLabels);
168+
169+
return $this;
170+
}
171+
172+
/**
173+
* Show Gridlines for Y-Axis
174+
*
175+
* @return bool
176+
*/
177+
public function showGridY()
178+
{
179+
return $this->gridY;
180+
}
181+
182+
/**
183+
* Set show Gridlines for Y-Axis
184+
*
185+
* @param bool $value
186+
* @return self
187+
*/
188+
public function setShowGridY($value = true)
189+
{
190+
$this->gridY = $this->setBoolVal($value, $this->gridY);
191+
192+
return $this;
193+
}
194+
195+
/**
196+
* Show Gridlines for X-Axis
197+
*
198+
* @return bool
199+
*/
200+
public function showGridX()
201+
{
202+
return $this->gridX;
203+
}
204+
205+
/**
206+
* Set show Gridlines for X-Axis
207+
*
208+
* @param bool $value
209+
* @return self
210+
*/
211+
public function setShowGridX($value = true)
212+
{
213+
$this->gridX = $this->setBoolVal($value, $this->gridX);
214+
215+
return $this;
216+
}
127217
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ private function writeSeriesItem(XMLWriter $xmlWriter, $type, $values)
264264
*/
265265
private function writeAxis(XMLWriter $xmlWriter, $type)
266266
{
267+
$style = $this->element->getStyle();
267268
$types = array(
268269
'cat' => array('c:catAx', 1, 'b', 2),
269270
'val' => array('c:valAx', 2, 'l', 1),
@@ -273,18 +274,20 @@ private function writeAxis(XMLWriter $xmlWriter, $type)
273274
$xmlWriter->startElement($axisType);
274275

275276
$xmlWriter->writeElementBlock('c:axId', 'val', $axisId);
276-
$xmlWriter->writeElementBlock('c:axPos', 'val', $axisPos);
277+
$xmlWriter->writeElementBlock('c:axPos', 'val', $axisPos);
277278
$xmlWriter->writeElementBlock('c:crossAx', 'val', $axisCross);
278279
$xmlWriter->writeElementBlock('c:auto', 'val', 1);
279280

280281
if (isset($this->options['axes'])) {
281282
$xmlWriter->writeElementBlock('c:delete', 'val', 0);
282283
$xmlWriter->writeElementBlock('c:majorTickMark', 'val', 'none');
283284
$xmlWriter->writeElementBlock('c:minorTickMark', 'val', 'none');
284-
$xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'none'); // nextTo
285+
if($style->showAxisLabels())
286+
$xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'nextTo');
287+
else $xmlWriter->writeElementBlock('c:tickLblPos', 'val', 'none');
285288
$xmlWriter->writeElementBlock('c:crosses', 'val', 'autoZero');
286289
}
287-
if (isset($this->options['radar'])) {
290+
if (isset($this->options['radar']) || ($type == "cat" && $style->showGridX()) || ($type == "val" && $style->showGridY())) {
288291
$xmlWriter->writeElement('c:majorGridlines');
289292
}
290293

0 commit comments

Comments
 (0)