Completed
Pull Request — develop (#216)
by Franck
07:08
created

ObjectsChart::writeContentPart()   C

Complexity

Conditions 7
Paths 22

Size

Total Lines 142
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 90
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 142
ccs 90
cts 90
cp 1
rs 6.4589
cc 7
eloc 90
nc 22
nop 1
crap 7

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