Skip to content

Commit 3fef190

Browse files
committed
PHPOffice#278: Basic chart
1 parent 9a5f91a commit 3fef190

File tree

12 files changed

+149
-122
lines changed

12 files changed

+149
-122
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This release added drawing shapes (arc, curve, line, polyline, rect, oval) eleme
1414
- RTF Writer: Support for sections, margins, and borders - @ivanlanin GH-249
1515
- Section: Ability to set paper size, e.g. A4, A3, and Legal - @ivanlanin GH-249
1616
- General: New `PhpWord::save()` method to encapsulate `IOFactory` - @ivanlanin
17+
- Element: Basic 2D charts (pie, doughnut, bar, line, area, scatter, radar) - @ivanlanin GH-278
1718

1819
### Bugfixes
1920

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ With PHPWord, you can create DOCX, ODT, or RTF documents dynamically using your
3030
- Insert list items as bulleted, numbered, or multilevel
3131
- Insert hyperlinks
3232
- Insert footnotes and endnotes
33+
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
34+
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
3335
- Create document from templates
3436
- Use XSL 1.0 style sheets to transform main document part of OOXML template
3537
- ... and many more features on progress

docs/elements.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ column shows the containers while the rows lists the elements.
4949
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
5050
| 20 | Shape | v | v | v | v | v | v |
5151
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
52+
| 21 | Chart | v | - | - | - | - | - |
53+
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
5254

5355
Legend:
5456

@@ -399,3 +401,8 @@ Shapes
399401
------
400402

401403
To be completed.
404+
405+
Charts
406+
------
407+
408+
To be completed.

docs/intro.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Features
4545
- Insert list items as bulleted, numbered, or multilevel
4646
- Insert hyperlinks
4747
- Insert footnotes and endnotes
48+
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
49+
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
4850
- Create document from templates
4951
- Use XSL 1.0 style sheets to transform main document part of OOXML
5052
template

docs/src/documentation.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Don't forget to change `code::` directive to `code-block::` in the resulting rst
3737
- [Fields](#fields)
3838
- [Lines](#lines)
3939
- [Shapes](#shapes)
40+
- [Charts](#charts)
4041
- [Styles](#styles)
4142
- [Section](#section)
4243
- [Font](#font)
@@ -76,6 +77,8 @@ PHPWord is an open source project licensed under the terms of [LGPL version 3](h
7677
- Insert list items as bulleted, numbered, or multilevel
7778
- Insert hyperlinks
7879
- Insert footnotes and endnotes
80+
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
81+
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
7982
- Create document from templates
8083
- Use XSL 1.0 style sheets to transform main document part of OOXML template
8184
- ... and many more features on progress
@@ -440,6 +443,7 @@ Below are the matrix of element availability in each container. The column shows
440443
| 18 | Field | v | v | v | v | v | v |
441444
| 19 | Line | v | v | v | v | v | v |
442445
| 20 | Shape | v | v | v | v | v | v |
446+
| 21 | Chart | v | - | - | - | - | - |
443447

444448
Legend:
445449

@@ -737,6 +741,10 @@ To be completed.
737741

738742
To be completed.
739743

744+
## Charts
745+
746+
To be completed.
747+
740748
# Styles
741749

742750
## Section

samples/Sample_32_Chart.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@
99
$section = $phpWord->addSection(array('colsNum' => 2));
1010
$phpWord->addTitleStyle(1, array('size' => 14, 'bold' => true), array('keepNext' => true, 'spaceBefore' => 240));
1111

12-
$charts = array('pie', 'doughnut', 'line', 'area', 'scatter', 'bar', 'radar');
13-
$labels = array('A', 'B', 'C', 'D', 'E');
14-
$data = array(1, 3, 2, 5, 4);
12+
$chartTypes = array('pie', 'doughnut', 'bar', 'line', 'area', 'scatter', 'radar');
13+
$twoSeries = array('bar', 'line', 'area', 'scatter', 'radar');
14+
$threeSeries = array('bar', 'line');
15+
$categories = array('A', 'B', 'C', 'D', 'E');
16+
$series1 = array(1, 3, 2, 5, 4);
17+
$series2 = array(3, 1, 7, 2, 6);
18+
$series3 = array(8, 3, 2, 5, 4);
1519

16-
foreach ($charts as $chart) {
17-
$section->addTitle(ucfirst($chart), 1);
18-
$section->addChart($chart, $labels, $data);
20+
foreach ($chartTypes as $chartType) {
21+
$section->addTitle(ucfirst($chartType), 1);
22+
$chart = $section->addChart($chartType, $categories, $series1);
23+
if (in_array($chartType, $twoSeries)) {
24+
$chart->addSeries($categories, $series2);
25+
}
26+
if (in_array($chartType, $threeSeries)) {
27+
$chart->addSeries($categories, $series3);
28+
}
1929
$section->addTextBreak();
2030
}
2131

src/PhpWord/Element/Chart.php

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,23 @@ class Chart extends AbstractElement
3939
private $type = 'pie';
4040

4141
/**
42-
* Labels
42+
* Series
4343
*
4444
* @var array
4545
*/
46-
private $labels = array();
47-
48-
/**
49-
* Data
50-
*
51-
* @var array
52-
*/
53-
private $data = array();
46+
private $series = array();
5447

5548
/**
5649
* Create new instance
5750
*
5851
* @param string $type
59-
* @param array $labels
60-
* @param array $data
52+
* @param array $categories
53+
* @param array $values
6154
*/
62-
public function __construct($type, $labels, $data)
55+
public function __construct($type, $categories, $values)
6356
{
6457
$this->setType($type);
65-
$this->setLabels($labels);
66-
$this->setData($data);
58+
$this->addSeries($categories, $values);
6759
}
6860

6961
/**
@@ -88,42 +80,23 @@ public function setType($value)
8880
}
8981

9082
/**
91-
* Get labels
92-
*
93-
* @return array
94-
*/
95-
public function getLabels()
96-
{
97-
return $this->labels;
98-
}
99-
100-
/**
101-
* Set labels
83+
* Add series
10284
*
103-
* @param array $value
85+
* @param array $categories
86+
* @param array $values
10487
*/
105-
public function setLabels($value)
88+
public function addSeries($categories, $values)
10689
{
107-
$this->labels = $value;
90+
$this->series[] = array('categories' => $categories, 'values' => $values);
10891
}
10992

11093
/**
111-
* Get data
94+
* Get series
11295
*
11396
* @return array
11497
*/
115-
public function getData()
116-
{
117-
return $this->data;
118-
}
119-
120-
/**
121-
* Set data
122-
*
123-
* @param array $value
124-
*/
125-
public function setData($value)
98+
public function getSeries()
12699
{
127-
$this->data = $value;
100+
return $this->series;
128101
}
129102
}

src/PhpWord/Shared/XMLWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function getData()
150150
* @param string|array $attributes
151151
* @param string $value
152152
*/
153-
public function writeBlock($element, $attributes, $value = null)
153+
public function writeElementBlock($element, $attributes, $value = null)
154154
{
155155
$this->xmlWriter->startElement($element);
156156
if (!is_array($attributes)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function write()
4848
$xmlWriter->startElement('wp:inline');
4949

5050
// EMU
51-
$xmlWriter->writeBlock('wp:extent', array('cx' => '2000000', 'cy' => '2000000'));
52-
$xmlWriter->writeBlock('wp:docPr', array('id' => $rId, 'name' => "Chart{$rId}"));
51+
$xmlWriter->writeElementBlock('wp:extent', array('cx' => '2000000', 'cy' => '2000000'));
52+
$xmlWriter->writeElementBlock('wp:docPr', array('id' => $rId, 'name' => "Chart{$rId}"));
5353

5454
$xmlWriter->startElement('a:graphic');
5555
$xmlWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');

0 commit comments

Comments
 (0)