Completed
Pull Request — develop (#186)
by
unknown
07:55
created

ObjectsChart::writeAxisStyle()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 68
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 68
ccs 45
cts 45
cp 1
rs 8.5748
cc 5
eloc 44
nc 4
nop 1
crap 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Writer\ODPresentation;
19
20
use PhpOffice\Common\Drawing as CommonDrawing;
21
use PhpOffice\Common\Text;
22
use PhpOffice\Common\XMLWriter;
23
use PhpOffice\PhpPresentation\PhpPresentation;
24
use PhpOffice\PhpPresentation\Shape\Chart;
25
use PhpOffice\PhpPresentation\Shape\Chart\Title;
26
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypeBar;
27
use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractTypePie;
28
use PhpOffice\PhpPresentation\Shape\Chart\Type\Area;
29
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar;
30
use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D;
31
use PhpOffice\PhpPresentation\Shape\Chart\Type\Line;
32
use PhpOffice\PhpPresentation\Shape\Chart\Type\Pie;
33
use PhpOffice\PhpPresentation\Shape\Chart\Type\Pie3D;
34
use PhpOffice\PhpPresentation\Shape\Chart\Type\Scatter;
35
use PhpOffice\PhpPresentation\Style\Fill;
36
37
/**
38
 * \PhpOffice\PhpPresentation\Writer\ODPresentation\Objects
39
 */
40
class ObjectsChart extends AbstractPart
41
{
42
    /**
43
     * @var XMLWriter
44
     */
45
    private $xmlContent;
46
    /**
47
     * @var XMLWriter
48
     */
49
    private $xmlMeta;
50
    /**
51
     * @var XMLWriter
52
     */
53
    private $xmlStyles;
54
    /**
55
     * @var mixed
56
     */
57
    private $arrayData;
58
    /**
59
     * @var mixed
60
     */
61
    private $arrayTitle;
62
    /**
63
     * @var integer
64
     */
65
    private $numData;
66
    /**
67
     * @var integer
68
     */
69
    private $numSeries;
70
    /**
71
     * @var string
72
     */
73
    private $rangeCol;
74
    
75 15
    public function writePart(Chart $chart)
76
    {
77 15
        $this->xmlContent = $this->getXMLWriter();
78 15
        $this->xmlMeta = $this->getXMLWriter();
79 15
        $this->xmlStyles = $this->getXMLWriter();
80
        
81 15
        $this->writeContentPart($chart);
82
        
83
        return array(
84 14
            'content.xml' => $this->xmlContent->getData(),
85 14
            'meta.xml' => $this->xmlMeta->getData(),
86 14
            'styles.xml' => $this->xmlStyles->getData(),
87
        );
88
    }
89
    
90
    /**
91
     * @param Chart $chart
92
     */
93 15
    private function writeContentPart(Chart $chart)
94
    {
95 15
        $chartType = $chart->getPlotArea()->getType();
96 15
        if (!($chartType instanceof Area || $chartType instanceof AbstractTypeBar || $chartType instanceof Line || $chartType instanceof AbstractTypePie || $chartType instanceof Scatter)) {
97 1
            throw new \Exception('The chart type provided could not be rendered.');
98
        }
99
        
100
        // Data
101 14
        $this->arrayData = array();
102 14
        $this->arrayTitle = array();
103 14
        $this->numData = 0;
104 14
        foreach ($chart->getPlotArea()->getType()->getData() as $series) {
105 12
            $inc = 0;
106 12
            $this->arrayTitle[] = $series->getTitle();
107 12
            foreach ($series->getValues() as $key => $value) {
108 12
                if (!isset($this->arrayData[$inc])) {
109 12
                    $this->arrayData[$inc] = array();
110
                }
111 12
                if (empty($this->arrayData[$inc])) {
112 12
                    $this->arrayData[$inc][] = $key;
113
                }
114 12
                $this->arrayData[$inc][] = $value;
115 12
                $inc++;
116
            }
117 12
            if ($inc > $this->numData) {
118 12
                $this->numData = $inc;
119
            }
120
        }
121
        
122
        // office:document-content
123 14
        $this->xmlContent->startElement('office:document-content');
124 14
        $this->xmlContent->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
125 14
        $this->xmlContent->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0');
126 14
        $this->xmlContent->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
127 14
        $this->xmlContent->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0');
128 14
        $this->xmlContent->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0');
129 14
        $this->xmlContent->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
130 14
        $this->xmlContent->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
131 14
        $this->xmlContent->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
132 14
        $this->xmlContent->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
133 14
        $this->xmlContent->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0');
134 14
        $this->xmlContent->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0');
135 14
        $this->xmlContent->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0');
136 14
        $this->xmlContent->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0');
137 14
        $this->xmlContent->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML');
138 14
        $this->xmlContent->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0');
139 14
        $this->xmlContent->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0');
140 14
        $this->xmlContent->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
141 14
        $this->xmlContent->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer');
142 14
        $this->xmlContent->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc');
143 14
        $this->xmlContent->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events');
144 14
        $this->xmlContent->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms');
145 14
        $this->xmlContent->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
146 14
        $this->xmlContent->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
147 14
        $this->xmlContent->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report');
148 14
        $this->xmlContent->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2');
149 14
        $this->xmlContent->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
150 14
        $this->xmlContent->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
151 14
        $this->xmlContent->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table');
152 14
        $this->xmlContent->writeAttribute('xmlns:chartooo', 'http://openoffice.org/2010/chart');
153 14
        $this->xmlContent->writeAttribute('xmlns:drawooo', 'http://openoffice.org/2010/draw');
154 14
        $this->xmlContent->writeAttribute('xmlns:calcext', 'urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0');
155 14
        $this->xmlContent->writeAttribute('xmlns:loext', 'urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0');
156 14
        $this->xmlContent->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0');
157 14
        $this->xmlContent->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0');
158 14
        $this->xmlContent->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/');
159 14
        $this->xmlContent->writeAttribute('office:version', '1.2');
160
        
161
        // office:automatic-styles
162 14
        $this->xmlContent->startElement('office:automatic-styles');
163
        
164
        // Chart
165 14
        $this->writeChartStyle($chart);
166
        
167
        // Axis
168 14
        $this->writeAxisStyle($chart);
169
        
170
        // Series
171 14
        $this->numSeries = 0;
172 14
        foreach ($chart->getPlotArea()->getType()->getData() as $series) {
173 12
            $this->writeSeriesStyle($chart, $series);
174
            
175 12
            $this->numSeries++;
176
        }
177
        
178
        // Floor
179 14
        $this->writeFloorStyle();
180
        
181
        // Legend
182 14
        $this->writeLegendStyle($chart);
183
        
184
        // PlotArea
185 14
        $this->writePlotAreaStyle($chart);
186
        
187
        // Title
188 14
        $this->writeTitleStyle($chart->getTitle());
189
        
190
        // Wall
191 14
        $this->writeWallStyle($chart);
192
        
193
        // > office:automatic-styles
194 14
        $this->xmlContent->endElement();
195
        
196
        // office:body
197 14
        $this->xmlContent->startElement('office:body');
198
        // office:chart
199 14
        $this->xmlContent->startElement('office:chart');
200
        // office:chart
201 14
        $this->xmlContent->startElement('chart:chart');
202 14
        $this->xmlContent->writeAttribute('svg:width', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getWidth()), 3) . 'cm');
203 14
        $this->xmlContent->writeAttribute('svg:height', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getHeight()), 3) . 'cm');
204 14
        $this->xmlContent->writeAttribute('xlink:href', '.');
205 14
        $this->xmlContent->writeAttribute('xlink:type', 'simple');
206 14
        $this->xmlContent->writeAttribute('chart:style-name', 'styleChart');
207 14
        if ($chartType instanceof Area) {
208 1
            $this->xmlContent->writeAttribute('chart:class', 'chart:area');
209
        } elseif ($chartType instanceof AbstractTypeBar) {
210 6
            $this->xmlContent->writeAttribute('chart:class', 'chart:bar');
211
        } elseif ($chartType instanceof Line) {
212 3
            $this->xmlContent->writeAttribute('chart:class', 'chart:line');
213
        } elseif ($chartType instanceof AbstractTypePie) {
214 3
            $this->xmlContent->writeAttribute('chart:class', 'chart:circle');
215
        } elseif ($chartType instanceof Scatter) {
216 1
            $this->xmlContent->writeAttribute('chart:class', 'chart:scatter');
217
        }
218
        
219
        //**** Title ****
220 14
        $this->writeTitle($chart->getTitle());
221
222
        //**** Legend ****
223 14
        $this->writeLegend($chart);
224
        
225
        //**** Plotarea ****
226 14
        $this->writePlotArea($chart);
227
228
        //**** Table ****
229 14
        $this->writeTable();
230
        
231
        // > chart:chart
232 14
        $this->xmlContent->endElement();
233
        // > office:chart
234 14
        $this->xmlContent->endElement();
235
        // > office:body
236 14
        $this->xmlContent->endElement();
237
        // > office:document-content
238 14
        $this->xmlContent->endElement();
239 14
    }
240
    
241
    /**
242
     * @param Chart $chart
243
     */
244 14
    private function writeAxis(Chart $chart)
245
    {
246 14
        $chartType = $chart->getPlotArea()->getType();
247
        
248
        // chart:axis
249 14
        $this->xmlContent->startElement('chart:axis');
250 14
        $this->xmlContent->writeAttribute('chart:dimension', 'x');
251 14
        $this->xmlContent->writeAttribute('chart:name', 'primary-x');
252 14
        $this->xmlContent->writeAttribute('chartooo:axis-type', 'text');
253 14
        $this->xmlContent->writeAttribute('chart:style-name', 'styleAxisX');
254
        // chart:categories
255 14
        $this->xmlContent->startElement('chart:categories');
256 14
        $this->xmlContent->writeAttribute('table:cell-range-address', 'table-local.$A$2:.$A$'.($this->numData+1));
257
        // > chart:categories
258 14
        $this->xmlContent->endElement();
259
        // > chart:axis
260 14
        $this->xmlContent->endElement();
261
        // chart:axis
262 14
        $this->xmlContent->startElement('chart:axis');
263 14
        $this->xmlContent->writeAttribute('chart:dimension', 'y');
264 14
        $this->xmlContent->writeAttribute('chart:name', 'primary-y');
265 14
        $this->xmlContent->writeAttribute('chart:style-name', 'styleAxisY');
266
        // > chart:axis
267 14
        $this->xmlContent->endElement();
268 14
        if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) {
269
            // chart:axis
270 5
            $this->xmlContent->startElement('chart:axis');
271 5
            $this->xmlContent->writeAttribute('chart:dimension', 'z');
272 5
            $this->xmlContent->writeAttribute('chart:name', 'primary-z');
273
            // > chart:axis
274 5
            $this->xmlContent->endElement();
275
        }
276 14
    }
277
    
278
    /**
279
     * @param Chart $chart
280
     * @todo Set function in \PhpPresentation\Shape\Chart\Axis for defining width and color of the axis
281
     */
282 14
    private function writeAxisStyle(Chart $chart)
283
    {
284 14
        $chartType = $chart->getPlotArea()->getType();
285
        
286
        // AxisX
287
        // style:style
288 14
        $this->xmlContent->startElement('style:style');
289 14
        $this->xmlContent->writeAttribute('style:name', 'styleAxisX');
290 14
        $this->xmlContent->writeAttribute('style:family', 'chart');
291
        // style:chart-properties
292 14
        $this->xmlContent->startElement('style:chart-properties');
293 14
        $this->xmlContent->writeAttribute('chart:display-label', 'true');
294 14
        $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false');
295 14
        $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
296 14
        if ($chartType instanceof AbstractTypePie) {
297 3
            $this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
298
        }
299
        // > style:chart-properties
300 14
        $this->xmlContent->endElement();
301
        // style:text-properties
302 14
        $this->xmlContent->startElement('style:text-properties');
303 14
        $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisX()->getFont()->getColor()->getRGB());
304 14
        $this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisX()->getFont()->getName());
305 14
        $this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisX()->getFont()->getSize().'pt');
306 14
        $this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisX()->getFont()->isItalic() ? 'italic' : 'normal');
307
        // > style:text-properties
308 14
        $this->xmlContent->endElement();
309
        // style:graphic-properties
310 14
        $this->xmlContent->startElement('style:graphic-properties');
311 14
        $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
312 14
        $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
313
        // > style:graphic-properties
314 14
        $this->xmlContent->endElement();
315
        // > style:style
316 14
        $this->xmlContent->endElement();
317
        
318
        // AxisY
319
        // style:style
320 14
        $this->xmlContent->startElement('style:style');
321 14
        $this->xmlContent->writeAttribute('style:name', 'styleAxisY');
322 14
        $this->xmlContent->writeAttribute('style:family', 'chart');
323
        // style:chart-properties
324 14
        $this->xmlContent->startElement('style:chart-properties');
325 14
        $this->xmlContent->writeAttribute('chart:display-label', 'true');
326 14
        $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false');
327 14
        $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
328 14
        if ($chartType instanceof AbstractTypePie) {
329 3
            $this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
330
        }
331
        // > style:chart-properties
332 14
        $this->xmlContent->endElement();
333
        // style:text-properties
334 14
        $this->xmlContent->startElement('style:text-properties');
335 14
        $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisY()->getFont()->getColor()->getRGB());
336 14
        $this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisY()->getFont()->getName());
337 14
        $this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisY()->getFont()->getSize().'pt');
338 14
        $this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisY()->getFont()->isItalic() ? 'italic' : 'normal');
339
        // > style:text-properties
340 14
        $this->xmlContent->endElement();
341
        // style:graphic-properties
342 14
        $this->xmlContent->startElement('style:graphic-properties');
343 14
        $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
344 14
        $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
345
        // > style:graphic-properties
346 14
        $this->xmlContent->endElement();
347
        // > style:style
348 14
        $this->xmlContent->endElement();
349 14
    }
350
    
351
    /**
352
     * @param Chart $chart
353
     */
354 14
    private function writeChartStyle(Chart $chart)
355
    {
356
        // style:style
357 14
        $this->xmlContent->startElement('style:style');
358 14
        $this->xmlContent->writeAttribute('style:name', 'styleChart');
359 14
        $this->xmlContent->writeAttribute('style:family', 'chart');
360
        // style:graphic-properties
361 14
        $this->xmlContent->startElement('style:graphic-properties');
362 14
        $this->xmlContent->writeAttribute('draw:stroke', $chart->getFill()->getFillType());
363 14
        $this->xmlContent->writeAttribute('draw:fill-color', '#'.$chart->getFill()->getStartColor()->getRGB());
364
        // > style:graphic-properties
365 14
        $this->xmlContent->endElement();
366
        // > style:style
367 14
        $this->xmlContent->endElement();
368 14
    }
369
    
370 14
    private function writeFloor()
371
    {
372
        // chart:floor
373 14
        $this->xmlContent->startElement('chart:floor');
374 14
        $this->xmlContent->writeAttribute('chart:style-name', 'styleFloor');
375
        // > chart:floor
376 14
        $this->xmlContent->endElement();
377 14
    }
378
    
379 14
    private function writeFloorStyle()
380
    {
381
        // style:style
382 14
        $this->xmlContent->startElement('style:style');
383 14
        $this->xmlContent->writeAttribute('style:name', 'styleFloor');
384 14
        $this->xmlContent->writeAttribute('style:family', 'chart');
385
        // style:chart-properties
386 14
        $this->xmlContent->startElement('style:graphic-properties');
387 14
        $this->xmlContent->writeAttribute('draw:fill', 'none');
388
        //@todo : Permit edit color and size border of floor
389 14
        $this->xmlContent->writeAttribute('draw:stroke', 'solid');
390 14
        $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
391 14
        $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
392
        // > style:chart-properties
393 14
        $this->xmlContent->endElement();
394
        // > style:style
395 14
        $this->xmlContent->endElement();
396 14
    }
397
    
398
    /**
399
     * @param Chart $chart
400
     */
401 14
    private function writeLegend(Chart $chart)
402
    {
403
        // chart:legend
404 14
        $this->xmlContent->startElement('chart:legend');
405 14
        $this->xmlContent->writeAttribute('chart:legend-position', 'end');
406 14
        $this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetX()), 3) . 'cm');
407 14
        $this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetY()), 3) . 'cm');
408 14
        $this->xmlContent->writeAttribute('style:legend-expansion', 'high');
409 14
        $this->xmlContent->writeAttribute('chart:style-name', 'styleLegend');
410
        // > chart:legend
411 14
        $this->xmlContent->endElement();
412 14
    }
413
    
414
    /**
415
     * @param Chart $chart
416
     */
417 14
    private function writeLegendStyle(Chart $chart)
418
    {
419
        // style:style
420 14
        $this->xmlContent->startElement('style:style');
421 14
        $this->xmlContent->writeAttribute('style:name', 'styleLegend');
422 14
        $this->xmlContent->writeAttribute('style:family', 'chart');
423
        // style:text-properties
424 14
        $this->xmlContent->startElement('style:text-properties');
425 14
        $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getLegend()->getFont()->getColor()->getRGB());
426 14
        $this->xmlContent->writeAttribute('fo:font-family', $chart->getLegend()->getFont()->getName());
427 14
        $this->xmlContent->writeAttribute('fo:font-size', $chart->getLegend()->getFont()->getSize().'pt');
428 14
        $this->xmlContent->writeAttribute('fo:font-style', $chart->getLegend()->getFont()->isItalic() ? 'italic' : 'normal');
429
        // > style:text-properties
430 14
        $this->xmlContent->endElement();
431
        // > style:style
432 14
        $this->xmlContent->endElement();
433 14
    }
434
    
435
    /**
436
     * @param Chart $chart
437
     */
438 14
    private function writePlotArea(Chart $chart)
439
    {
440 14
        $chartType = $chart->getPlotArea()->getType();
441
        
442
        // chart:plot-area
443 14
        $this->xmlContent->startElement('chart:plot-area');
444 14
        $this->xmlContent->writeAttribute('chart:style-name', 'stylePlotArea');
445 14
        if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) {
446 5
            $this->xmlContent->writeAttribute('dr3d:ambient-color', '#cccccc');
447 5
            $this->xmlContent->writeAttribute('dr3d:lighting-mode', 'true');
448
        }
449 14
        if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) {
450
            // dr3d:light
451
            $arrayLight = array(
452 5
                array('#808080', '(0 0 1)', 'false', 'true'),
453
                array('#666666', '(0.2 0.4 1)', 'true', 'false'),
454
                array('#808080', '(0 0 1)', 'false', 'false'),
455
                array('#808080', '(0 0 1)', 'false', 'false'),
456
                array('#808080', '(0 0 1)', 'false', 'false'),
457
                array('#808080', '(0 0 1)', 'false', 'false'),
458
                array('#808080', '(0 0 1)', 'false', 'false'),
459
            );
460 5
            foreach ($arrayLight as $light) {
461 5
                $this->xmlContent->startElement('dr3d:light');
462 5
                $this->xmlContent->writeAttribute('dr3d:diffuse-color', $light[0]);
463 5
                $this->xmlContent->writeAttribute('dr3d:direction', $light[1]);
464 5
                $this->xmlContent->writeAttribute('dr3d:enabled', $light[2]);
465 5
                $this->xmlContent->writeAttribute('dr3d:specular', $light[3]);
466 5
                $this->xmlContent->endElement();
467
            }
468
        }
469
        
470
        //**** Axis ****
471 14
        $this->writeAxis($chart);
472
        
473
        //**** Series ****
474 14
        $this->rangeCol = 'B';
475 14
        $this->numSeries = 0;
476 14
        foreach ($chart->getPlotArea()->getType()->getData() as $series) {
477 12
            $this->writeSeries($chart, $series);
478 12
            $this->rangeCol++;
479 12
            $this->numSeries++;
480
        }
481
        
482
        //**** Wall ****
483 14
        $this->writeWall();
484
        //**** Floor ****
485 14
        $this->writeFloor();
486
        // > chart:plot-area
487 14
        $this->xmlContent->endElement();
488 14
    }
489
    
490
    /**
491
     * @param Chart $chart
492
     * @link : http://books.evc-cit.info/odbook/ch08.html#chart-plot-area-section
493
     */
494 14
    private function writePlotAreaStyle(Chart $chart)
495
    {
496 14
        $chartType = $chart->getPlotArea()->getType();
497
        
498
        // style:style
499 14
        $this->xmlContent->startElement('style:style');
500 14
        $this->xmlContent->writeAttribute('style:name', 'stylePlotArea');
501 14
        $this->xmlContent->writeAttribute('style:family', 'chart');
502
        // style:text-properties
503 14
        $this->xmlContent->startElement('style:chart-properties');
504 14
        if ($chartType instanceof Bar3D) {
505 3
            $this->xmlContent->writeAttribute('chart:three-dimensional', 'true');
506 3
            $this->xmlContent->writeAttribute('chart:right-angled-axes', 'true');
507
        } elseif ($chartType instanceof Pie3D) {
508 2
            $this->xmlContent->writeAttribute('chart:three-dimensional', 'true');
509 2
            $this->xmlContent->writeAttribute('chart:right-angled-axes', 'true');
510
        }
511 14
        if ($chartType instanceof AbstractTypeBar) {
512 6
            if ($chartType->getBarDirection() == AbstractTypeBar::DIRECTION_HORIZONTAL) {
513 2
                $this->xmlContent->writeAttribute('chart:vertical', 'true');
514
            } else {
515 4
                $this->xmlContent->writeAttribute('chart:vertical', 'false');
516
            }
517 6
            if ($chartType->getBarGrouping() == Bar::GROUPING_CLUSTERED) {
518 6
                $this->xmlContent->writeAttribute('chart:stacked', 'false');
519 6
                $this->xmlContent->writeAttribute('chart:overlap', '0');
520
            } elseif ($chartType->getBarGrouping() == Bar::GROUPING_STACKED) {
521
                $this->xmlContent->writeAttribute('chart:stacked', 'true');
522
                $this->xmlContent->writeAttribute('chart:overlap', '100');
523
            } elseif ($chartType->getBarGrouping() == Bar::GROUPING_PERCENTSTACKED) {
524
                $this->xmlContent->writeAttribute('chart:stacked', 'true');
525
                $this->xmlContent->writeAttribute('chart:overlap', '100');
526
                $this->xmlContent->writeAttribute('chart:percentage', 'true');
527
            }
528
        }
529 14
        $labelFormat = 'value';
530 14
        if ($chartType instanceof AbstractTypeBar) {
531 6
            if ($chartType->getBarGrouping() == Bar::GROUPING_PERCENTSTACKED) {
532
                $labelFormat = 'percentage';
533
            }
534
        }
535 14
        $this->xmlContent->writeAttribute('chart:data-label-number', $labelFormat);
536
537
        // > style:text-properties
538 14
        $this->xmlContent->endElement();
539
        // > style:style
540 14
        $this->xmlContent->endElement();
541 14
    }
542
    
543
    /**
544
     * @param Chart $chart
545
     * @param Chart\Series $series
546
     * @throws \Exception
547
     */
548 12
    private function writeSeries(Chart $chart, Chart\Series $series)
549
    {
550 12
        $chartType = $chart->getPlotArea()->getType();
551
        
552 12
        $numRange = count($series->getValues());
553
        // chart:series
554 12
        $this->xmlContent->startElement('chart:series');
555 12
        $this->xmlContent->writeAttribute('chart:values-cell-range-address', 'table-local.$'.$this->rangeCol.'$2:.$'.$this->rangeCol.'$'.($numRange+1));
556 12
        $this->xmlContent->writeAttribute('chart:label-cell-address', 'table-local.$'.$this->rangeCol.'$1');
557 12
        if ($chartType instanceof Area) {
558 1
            $this->xmlContent->writeAttribute('chart:class', 'chart:area');
559
        } elseif ($chartType instanceof AbstractTypeBar) {
560 5
            $this->xmlContent->writeAttribute('chart:class', 'chart:bar');
561
        } elseif ($chartType instanceof Line) {
562 2
            $this->xmlContent->writeAttribute('chart:class', 'chart:line');
563
        } elseif ($chartType instanceof AbstractTypePie) {
564 3
            $this->xmlContent->writeAttribute('chart:class', 'chart:circle');
565
        } elseif ($chartType instanceof Scatter) {
566 1
            $this->xmlContent->writeAttribute('chart:class', 'chart:scatter');
567
        }
568 12
        $this->xmlContent->writeAttribute('chart:style-name', 'styleSeries'.$this->numSeries);
569 12
        if ($chartType instanceof Area || $chartType instanceof AbstractTypeBar || $chartType instanceof Line || $chartType instanceof Scatter) {
570 9
            $dataPointFills = $series->getDataPointFills();
571 9
            if (empty($dataPointFills)) {
572 5
                $incRepeat = $numRange;
573
            } else {
574 4
                $inc = 0;
575 4
                $incRepeat = 0;
576 4
                $newFill = new Fill();
577
                do {
578 4
                    if ($series->getDataPointFill($inc)->getHashCode() != $newFill->getHashCode()) {
579
                        // chart:data-point
580 4
                        $this->xmlContent->startElement('chart:data-point');
581 4
                        $this->xmlContent->writeAttribute('chart:repeated', $incRepeat);
582
                        // > chart:data-point
583 4
                        $this->xmlContent->endElement();
584 4
                        $incRepeat = 0;
585
        
586
                        // chart:data-point
587 4
                        $this->xmlContent->startElement('chart:data-point');
588 4
                        $this->xmlContent->writeAttribute('chart:style-name', 'styleSeries'.$this->numSeries.'_'.$inc);
589
                        // > chart:data-point
590 4
                        $this->xmlContent->endElement();
591
                    }
592 4
                    $inc++;
593 4
                    $incRepeat++;
594 4
                } while ($inc < $numRange);
595 4
                $incRepeat--;
596
            }
597
            // chart:data-point
598 9
            $this->xmlContent->startElement('chart:data-point');
599 9
            $this->xmlContent->writeAttribute('chart:repeated', $incRepeat);
600
            // > chart:data-point
601 9
            $this->xmlContent->endElement();
602
        } elseif ($chartType instanceof AbstractTypePie) {
603 3
            $count = count($series->getDataPointFills());
604 3
            for ($inc = 0; $inc < $count; $inc++) {
605
                // chart:data-point
606 2
                $this->xmlContent->startElement('chart:data-point');
607 2
                $this->xmlContent->writeAttribute('chart:style-name', 'styleSeries'.$this->numSeries.'_'.$inc);
608
                // > chart:data-point
609 2
                $this->xmlContent->endElement();
610
            }
611
        }
612
        
613
        // > chart:series
614 12
        $this->xmlContent->endElement();
615 12
    }
616
    
617
    /**
618
     * @param Chart $chart
619
     * @param Chart\Series $series
620
     */
621 12
    private function writeSeriesStyle(Chart $chart, Chart\Series $series)
622
    {
623 12
        $chartType = $chart->getPlotArea()->getType();
624
        
625
        // style:style
626 12
        $this->xmlContent->startElement('style:style');
627 12
        $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries);
628 12
        $this->xmlContent->writeAttribute('style:family', 'chart');
629
        // style:chart-properties
630 12
        $this->xmlContent->startElement('style:chart-properties');
631 12
        $this->xmlContent->writeAttribute('chart:data-label-number', 'value');
632 12
        $this->xmlContent->writeAttribute('chart:label-position', 'center');
633 12
        if ($chartType instanceof AbstractTypePie) {
634 3
            $this->xmlContent->writeAttribute('chart:pie-offset', $chartType->getExplosion());
635
        }
636 12
        if ($chartType instanceof Line) {
637
            //@todo : Permit edit the symbol of a line
638 2
            $this->xmlContent->writeAttribute('chart:symbol-type', 'automatic');
639
        }
640
        // > style:chart-properties
641 12
        $this->xmlContent->endElement();
642
        // style:graphic-properties
643 12
        $this->xmlContent->startElement('style:graphic-properties');
644 12
        if ($chartType instanceof Line || $chartType instanceof Scatter) {
645
            //@todo : Permit edit the color and width of a line
646 3
            $this->xmlContent->writeAttribute('svg:stroke-width', '0.079cm');
647 3
            $this->xmlContent->writeAttribute('svg:stroke-color', '#4a7ebb');
648
        } else {
649 9
            $this->xmlContent->writeAttribute('draw:stroke', 'none');
650 9
            if (!($chartType instanceof Area)) {
651 8
                $this->xmlContent->writeAttribute('draw:fill', $series->getFill()->getFillType());
652
            }
653
        }
654 12
        $this->xmlContent->writeAttribute('draw:fill-color', '#'.$series->getFill()->getStartColor()->getRGB());
655
        // > style:graphic-properties
656 12
        $this->xmlContent->endElement();
657
        // style:text-properties
658 12
        $this->xmlContent->startElement('style:text-properties');
659 12
        $this->xmlContent->writeAttribute('fo:color', '#'.$series->getFont()->getColor()->getRGB());
660 12
        $this->xmlContent->writeAttribute('fo:font-family', $series->getFont()->getName());
661 12
        $this->xmlContent->writeAttribute('fo:font-size', $series->getFont()->getSize().'pt');
662
        // > style:text-properties
663 12
        $this->xmlContent->endElement();
664
        
665
        // > style:style
666 12
        $this->xmlContent->endElement();
667
        
668 12
        foreach ($series->getDataPointFills() as $idx => $oFill) {
669
            // style:style
670 6
            $this->xmlContent->startElement('style:style');
671 6
            $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries.'_'.$idx);
672 6
            $this->xmlContent->writeAttribute('style:family', 'chart');
673
            // style:graphic-properties
674 6
            $this->xmlContent->startElement('style:graphic-properties');
675 6
            $this->xmlContent->writeAttribute('draw:fill', $oFill->getFillType());
676 6
            $this->xmlContent->writeAttribute('draw:fill-color', '#'.$oFill->getStartColor()->getRGB());
677
            // > style:graphic-properties
678 6
            $this->xmlContent->endElement();
679
            // > style:style
680 6
            $this->xmlContent->endElement();
681
        }
682 12
    }
683
    
684
    /**
685
     */
686 14
    private function writeTable()
687
    {
688
        // table:table
689 14
        $this->xmlContent->startElement('table:table');
690 14
        $this->xmlContent->writeAttribute('table:name', 'table-local');
691
    
692
        // table:table-header-columns
693 14
        $this->xmlContent->startElement('table:table-header-columns');
694
        // table:table-column
695 14
        $this->xmlContent->startElement('table:table-column');
696
        // > table:table-column
697 14
        $this->xmlContent->endElement();
698
        // > table:table-header-columns
699 14
        $this->xmlContent->endElement();
700
        
701
        // table:table-columns
702 14
        $this->xmlContent->startElement('table:table-columns');
703
        // table:table-column
704 14
        $this->xmlContent->startElement('table:table-column');
705 14
        if (!empty($this->arrayData)) {
706 12
            $rowFirst = reset($this->arrayData);
707 12
            $this->xmlContent->writeAttribute('table:number-columns-repeated', count($rowFirst) - 1);
708
        }
709
        // > table:table-column
710 14
        $this->xmlContent->endElement();
711
        // > table:table-columns
712 14
        $this->xmlContent->endElement();
713
    
714
        // table:table-header-rows
715 14
        $this->xmlContent->startElement('table:table-header-rows');
716
        // table:table-row
717 14
        $this->xmlContent->startElement('table:table-row');
718 14
        if (!empty($this->arrayData)) {
719 12
            $rowFirst = reset($this->arrayData);
720 12
            foreach ($rowFirst as $key => $cell) {
721
                // table:table-cell
722 12
                $this->xmlContent->startElement('table:table-cell');
723 12
                if (isset($this->arrayTitle[$key - 1])) {
724 12
                    $this->xmlContent->writeAttribute('office:value-type', 'string');
725
                }
726
                // text:p
727 12
                $this->xmlContent->startElement('text:p');
728 12
                if (isset($this->arrayTitle[$key - 1])) {
729 12
                    $this->xmlContent->text($this->arrayTitle[$key - 1]);
730
                }
731
                // > text:p
732 12
                $this->xmlContent->endElement();
733
                // > table:table-cell
734 12
                $this->xmlContent->endElement();
735
            }
736
        }
737
        // > table:table-row
738 14
        $this->xmlContent->endElement();
739
        // > table:table-header-rows
740 14
        $this->xmlContent->endElement();
741
    
742
        // table:table-rows
743 14
        $this->xmlContent->startElement('table:table-rows');
744
    
745 14
        foreach ($this->arrayData as $row) {
746
            // table:table-row
747 12
            $this->xmlContent->startElement('table:table-row');
748 12
            foreach ($row as $cell) {
749
                // table:table-cell
750 12
                $this->xmlContent->startElement('table:table-cell');
751 12
                if (is_numeric($cell)) {
752 12
                    $this->xmlContent->writeAttribute('office:value-type', 'float');
753 12
                    $this->xmlContent->writeAttribute('office:value', $cell);
754
                } else {
755 12
                    $this->xmlContent->writeAttribute('office:value-type', 'string');
756
                }
757
                // text:p
758 12
                $this->xmlContent->startElement('text:p');
759 12
                $this->xmlContent->text($cell);
760
                // > text:p
761 12
                $this->xmlContent->endElement();
762
                // > table:table-cell
763 12
                $this->xmlContent->endElement();
764
            }
765
            // > table:table-row
766 12
            $this->xmlContent->endElement();
767
        }
768
    
769
        // > table:table-rows
770 14
        $this->xmlContent->endElement();
771
        // > table:table
772 14
        $this->xmlContent->endElement();
773 14
    }
774
    
775
    /**
776
     * @param Title $oTitle
777
     */
778 14
    private function writeTitle(Title $oTitle)
779
    {
780 14
        if ($oTitle->isVisible()) {
781
            // chart:title
782 14
            $this->xmlContent->startElement('chart:title');
783 14
            $this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetX()), 3) . 'cm');
784 14
            $this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetY()), 3) . 'cm');
785 14
            $this->xmlContent->writeAttribute('chart:style-name', 'styleTitle');
786
            // > text:p
787 14
            $this->xmlContent->startElement('text:p');
788 14
            $this->xmlContent->text($oTitle->getText());
789
            // > text:p
790 14
            $this->xmlContent->endElement();
791
            // > chart:title
792 14
            $this->xmlContent->endElement();
793
        }
794 14
    }
795
    
796
    /**
797
     * @param Title $oTitle
798
     */
799 14
    private function writeTitleStyle(Title $oTitle)
800
    {
801 14
        if ($oTitle->isVisible()) {
802
            // style:style
803 14
            $this->xmlContent->startElement('style:style');
804 14
            $this->xmlContent->writeAttribute('style:name', 'styleTitle');
805 14
            $this->xmlContent->writeAttribute('style:family', 'chart');
806
            // style:text-properties
807 14
            $this->xmlContent->startElement('style:text-properties');
808 14
            $this->xmlContent->writeAttribute('fo:color', '#'.$oTitle->getFont()->getColor()->getRGB());
809 14
            $this->xmlContent->writeAttribute('fo:font-family', $oTitle->getFont()->getName());
810 14
            $this->xmlContent->writeAttribute('fo:font-size', $oTitle->getFont()->getSize().'pt');
811 14
            $this->xmlContent->writeAttribute('fo:font-style', $oTitle->getFont()->isItalic() ? 'italic' : 'normal');
812
            // > style:text-properties
813 14
            $this->xmlContent->endElement();
814
            // > style:style
815 14
            $this->xmlContent->endElement();
816
        }
817 14
    }
818
    
819 14
    private function writeWall()
820
    {
821
        // chart:wall
822 14
        $this->xmlContent->startElement('chart:wall');
823 14
        $this->xmlContent->writeAttribute('chart:style-name', 'styleWall');
824
        // > chart:wall
825 14
        $this->xmlContent->endElement();
826 14
    }
827
828
    /**
829
     * @param Chart $chart
830
     */
831 14
    private function writeWallStyle(Chart $chart)
832
    {
833 14
        $chartType = $chart->getPlotArea()->getType();
834
        
835
        // style:style
836 14
        $this->xmlContent->startElement('style:style');
837 14
        $this->xmlContent->writeAttribute('style:name', 'styleWall');
838 14
        $this->xmlContent->writeAttribute('style:family', 'chart');
839
        // style:chart-properties
840 14
        $this->xmlContent->startElement('style:graphic-properties');
841
        //@todo : Permit edit color and size border of wall
842 14
        if ($chartType instanceof Line || $chartType instanceof Scatter) {
843 4
            $this->xmlContent->writeAttribute('draw:fill', 'solid');
844 4
            $this->xmlContent->writeAttribute('draw:fill-color', '#FFFFFF');
845
        } else {
846 10
            $this->xmlContent->writeAttribute('draw:fill', 'none');
847 10
            $this->xmlContent->writeAttribute('draw:stroke', 'solid');
848 10
            $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
849 10
            $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
850
        }
851
        // > style:chart-properties
852 14
        $this->xmlContent->endElement();
853
        // > style:style
854 14
        $this->xmlContent->endElement();
855 14
    }
856
}
857