Completed
Pull Request — develop (#195)
by Franck
07:42
created

ObjectsChart::writePart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 1
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 55
    public function render()
53
    {
54 55
        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 55
        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
        if ($chartType instanceof Area) {
184 1
            $this->xmlContent->writeAttribute('chart:class', 'chart:area');
185
        } elseif ($chartType instanceof AbstractTypeBar) {
186 8
            $this->xmlContent->writeAttribute('chart:class', 'chart:bar');
187
        } elseif ($chartType instanceof Line) {
188 6
            $this->xmlContent->writeAttribute('chart:class', 'chart:line');
189
        } elseif ($chartType instanceof AbstractTypePie) {
190 3
            $this->xmlContent->writeAttribute('chart:class', 'chart:circle');
191
        } elseif ($chartType instanceof Scatter) {
192 3
            $this->xmlContent->writeAttribute('chart:class', 'chart:scatter');
193
        }
194
195
        //**** Title ****
196 21
        $this->writeTitle($chart->getTitle());
197
198
        //**** Legend ****
199 21
        $this->writeLegend($chart);
200
201
        //**** Plotarea ****
202 21
        $this->writePlotArea($chart);
203
204
        //**** Table ****
205 21
        $this->writeTable();
206
207
        // > chart:chart
208 21
        $this->xmlContent->endElement();
209
        // > office:chart
210 21
        $this->xmlContent->endElement();
211
        // > office:body
212 21
        $this->xmlContent->endElement();
213
        // > office:document-content
214 21
        $this->xmlContent->endElement();
215
216 21
        return $this->xmlContent->getData();
217
    }
218
219
    /**
220
     * @param Chart $chart
221
     */
222 21
    private function writeAxis(Chart $chart)
223
    {
224 21
        $chartType = $chart->getPlotArea()->getType();
225
226
        // chart:axis
227 21
        $this->xmlContent->startElement('chart:axis');
228 21
        $this->xmlContent->writeAttribute('chart:dimension', 'x');
229 21
        $this->xmlContent->writeAttribute('chart:name', 'primary-x');
230 21
        $this->xmlContent->writeAttribute('chartooo:axis-type', 'text');
231 21
        $this->xmlContent->writeAttribute('chart:style-name', 'styleAxisX');
232
        // chart:axis > chart:categories
233 21
        $this->xmlContent->startElement('chart:categories');
234 21
        $this->xmlContent->writeAttribute('table:cell-range-address', 'table-local.$A$2:.$A$'.($this->numData+1));
235 21
        $this->xmlContent->endElement();
236
        // chart:axis > chart:grid
237 21
        $this->writeGridline($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor', 'major');
238
        // chart:axis > chart:grid
239 21
        $this->writeGridline($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor', 'minor');
240
        // ##chart:axis
241 21
        $this->xmlContent->endElement();
242
243
        // chart:axis
244 21
        $this->xmlContent->startElement('chart:axis');
245 21
        $this->xmlContent->writeAttribute('chart:dimension', 'y');
246 21
        $this->xmlContent->writeAttribute('chart:name', 'primary-y');
247 21
        $this->xmlContent->writeAttribute('chart:style-name', 'styleAxisY');
248
        // chart:axis > chart:grid
249 21
        $this->writeGridline($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor', 'major');
250
        // chart:axis > chart:grid
251 21
        $this->writeGridline($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor', 'minor');
252
        // ##chart:axis
253 21
        $this->xmlContent->endElement();
254
255 21
        if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) {
256
            // chart:axis
257 5
            $this->xmlContent->startElement('chart:axis');
258 5
            $this->xmlContent->writeAttribute('chart:dimension', 'z');
259 5
            $this->xmlContent->writeAttribute('chart:name', 'primary-z');
260
            // > chart:axis
261 5
            $this->xmlContent->endElement();
262
        }
263 21
    }
264
265 21
    protected function writeGridline($oGridlines, $styleName, $chartClass)
266
    {
267 21
        if (!($oGridlines instanceof Chart\Gridlines)) {
268 21
            return ;
269
        }
270
271 1
        $this->xmlContent->startElement('chart:grid');
272 1
        $this->xmlContent->writeAttribute('chart:style-name', $styleName);
273 1
        $this->xmlContent->writeAttribute('chart:class', $chartClass);
274 1
        $this->xmlContent->endElement();
275 1
    }
276
277
    /**
278
     * @param Chart $chart
279
     * @todo Set function in \PhpPresentation\Shape\Chart\Axis for defining width and color of the axis
280
     */
281 21
    protected function writeAxisStyle(Chart $chart)
282
    {
283 21
        $chartType = $chart->getPlotArea()->getType();
284
285
        // AxisX
286
        // style:style
287 21
        $this->xmlContent->startElement('style:style');
288 21
        $this->xmlContent->writeAttribute('style:name', 'styleAxisX');
289 21
        $this->xmlContent->writeAttribute('style:family', 'chart');
290
        // style:style > style:chart-properties
291 21
        $this->xmlContent->startElement('style:chart-properties');
292 21
        $this->xmlContent->writeAttribute('chart:display-label', 'true');
293 21
        $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false');
294 21
        $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
295 21
        if ($chartType instanceof AbstractTypePie) {
296 3
            $this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
297
        }
298 21
        $this->xmlContent->endElement();
299
        // style:style > style:text-properties
300 21
        $this->xmlContent->startElement('style:text-properties');
301 21
        $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisX()->getFont()->getColor()->getRGB());
302 21
        $this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisX()->getFont()->getName());
303 21
        $this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisX()->getFont()->getSize().'pt');
304 21
        $this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisX()->getFont()->isItalic() ? 'italic' : 'normal');
305 21
        $this->xmlContent->endElement();
306
        // style:style > style:graphic-properties
307 21
        $this->xmlContent->startElement('style:graphic-properties');
308 21
        $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
309 21
        $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
310 21
        $this->xmlContent->endElement();
311
        // ##style:style
312 21
        $this->xmlContent->endElement();
313
314
        // AxisX GridLines Major
315 21
        $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor');
316
317
        // AxisX GridLines Minor
318 21
        $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor');
319
320
        // AxisY
321
        // style:style
322 21
        $this->xmlContent->startElement('style:style');
323 21
        $this->xmlContent->writeAttribute('style:name', 'styleAxisY');
324 21
        $this->xmlContent->writeAttribute('style:family', 'chart');
325
        // style:style > style:chart-properties
326 21
        $this->xmlContent->startElement('style:chart-properties');
327 21
        $this->xmlContent->writeAttribute('chart:display-label', 'true');
328 21
        $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false');
329 21
        $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false');
330 21
        if ($chartType instanceof AbstractTypePie) {
331 3
            $this->xmlContent->writeAttribute('chart:reverse-direction', 'true');
332
        }
333 21
        $this->xmlContent->endElement();
334
        // style:style > style:text-properties
335 21
        $this->xmlContent->startElement('style:text-properties');
336 21
        $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getPlotArea()->getAxisY()->getFont()->getColor()->getRGB());
337 21
        $this->xmlContent->writeAttribute('fo:font-family', $chart->getPlotArea()->getAxisY()->getFont()->getName());
338 21
        $this->xmlContent->writeAttribute('fo:font-size', $chart->getPlotArea()->getAxisY()->getFont()->getSize().'pt');
339 21
        $this->xmlContent->writeAttribute('fo:font-style', $chart->getPlotArea()->getAxisY()->getFont()->isItalic() ? 'italic' : 'normal');
340 21
        $this->xmlContent->endElement();
341
        // style:graphic-properties
342 21
        $this->xmlContent->startElement('style:graphic-properties');
343 21
        $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
344 21
        $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
345 21
        $this->xmlContent->endElement();
346
        // ## style:style
347 21
        $this->xmlContent->endElement();
348
349
        // AxisY GridLines Major
350 21
        $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor');
351
352
        // AxisY GridLines Minor
353 21
        $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor');
354 21
    }
355
356
    /**
357
     * @param Chart\Gridlines $oGridlines
358
     * @param string $styleName
359
     */
360 21
    protected function writeGridlineStyle($oGridlines, $styleName)
361
    {
362 21
        if (!($oGridlines instanceof Chart\Gridlines)) {
363 21
            return;
364
        }
365
        // style:style
366 1
        $this->xmlContent->startElement('style:style');
367 1
        $this->xmlContent->writeAttribute('style:name', $styleName);
368 1
        $this->xmlContent->writeAttribute('style:family', 'chart');
369
        // style:style > style:graphic-properties
370 1
        $this->xmlContent->startElement('style:graphic-properties');
371 1
        $this->xmlContent->writeAttribute('svg:stroke-width', number_format(CommonDrawing::pointsToCentimeters($oGridlines->getOutline()->getWidth()), 2, '.', '').'cm');
372 1
        $this->xmlContent->writeAttribute('svg:stroke-color', '#'.$oGridlines->getOutline()->getFill()->getStartColor()->getRGB());
373 1
        $this->xmlContent->endElement();
374
        // ##style:style
375 1
        $this->xmlContent->endElement();
376 1
    }
377
378
    /**
379
     * @param Chart $chart
380
     */
381 21
    private function writeChartStyle(Chart $chart)
382
    {
383
        // style:style
384 21
        $this->xmlContent->startElement('style:style');
385 21
        $this->xmlContent->writeAttribute('style:name', 'styleChart');
386 21
        $this->xmlContent->writeAttribute('style:family', 'chart');
387
        // style:graphic-properties
388 21
        $this->xmlContent->startElement('style:graphic-properties');
389 21
        $this->xmlContent->writeAttribute('draw:stroke', $chart->getFill()->getFillType());
390 21
        $this->xmlContent->writeAttribute('draw:fill-color', '#'.$chart->getFill()->getStartColor()->getRGB());
391
        // > style:graphic-properties
392 21
        $this->xmlContent->endElement();
393
        // > style:style
394 21
        $this->xmlContent->endElement();
395 21
    }
396
397 21
    private function writeFloor()
398
    {
399
        // chart:floor
400 21
        $this->xmlContent->startElement('chart:floor');
401 21
        $this->xmlContent->writeAttribute('chart:style-name', 'styleFloor');
402
        // > chart:floor
403 21
        $this->xmlContent->endElement();
404 21
    }
405
406 21
    private function writeFloorStyle()
407
    {
408
        // style:style
409 21
        $this->xmlContent->startElement('style:style');
410 21
        $this->xmlContent->writeAttribute('style:name', 'styleFloor');
411 21
        $this->xmlContent->writeAttribute('style:family', 'chart');
412
        // style:chart-properties
413 21
        $this->xmlContent->startElement('style:graphic-properties');
414 21
        $this->xmlContent->writeAttribute('draw:fill', 'none');
415
        //@todo : Permit edit color and size border of floor
416 21
        $this->xmlContent->writeAttribute('draw:stroke', 'solid');
417 21
        $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
418 21
        $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
419
        // > style:chart-properties
420 21
        $this->xmlContent->endElement();
421
        // > style:style
422 21
        $this->xmlContent->endElement();
423 21
    }
424
425
    /**
426
     * @param Chart $chart
427
     */
428 21
    private function writeLegend(Chart $chart)
429
    {
430
        // chart:legend
431 21
        $this->xmlContent->startElement('chart:legend');
432 21
        $this->xmlContent->writeAttribute('chart:legend-position', 'end');
433 21
        $this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetX()), 3) . 'cm');
434 21
        $this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetY()), 3) . 'cm');
435 21
        $this->xmlContent->writeAttribute('style:legend-expansion', 'high');
436 21
        $this->xmlContent->writeAttribute('chart:style-name', 'styleLegend');
437
        // > chart:legend
438 21
        $this->xmlContent->endElement();
439 21
    }
440
441
    /**
442
     * @param Chart $chart
443
     */
444 21
    private function writeLegendStyle(Chart $chart)
445
    {
446
        // style:style
447 21
        $this->xmlContent->startElement('style:style');
448 21
        $this->xmlContent->writeAttribute('style:name', 'styleLegend');
449 21
        $this->xmlContent->writeAttribute('style:family', 'chart');
450
        // style:text-properties
451 21
        $this->xmlContent->startElement('style:text-properties');
452 21
        $this->xmlContent->writeAttribute('fo:color', '#'.$chart->getLegend()->getFont()->getColor()->getRGB());
453 21
        $this->xmlContent->writeAttribute('fo:font-family', $chart->getLegend()->getFont()->getName());
454 21
        $this->xmlContent->writeAttribute('fo:font-size', $chart->getLegend()->getFont()->getSize().'pt');
455 21
        $this->xmlContent->writeAttribute('fo:font-style', $chart->getLegend()->getFont()->isItalic() ? 'italic' : 'normal');
456
        // > style:text-properties
457 21
        $this->xmlContent->endElement();
458
        // > style:style
459 21
        $this->xmlContent->endElement();
460 21
    }
461
462
    /**
463
     * @param Chart $chart
464
     */
465 21
    private function writePlotArea(Chart $chart)
466
    {
467 21
        $chartType = $chart->getPlotArea()->getType();
468
469
        // chart:plot-area
470 21
        $this->xmlContent->startElement('chart:plot-area');
471 21
        $this->xmlContent->writeAttribute('chart:style-name', 'stylePlotArea');
472 21
        if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) {
473 5
            $this->xmlContent->writeAttribute('dr3d:ambient-color', '#cccccc');
474 5
            $this->xmlContent->writeAttribute('dr3d:lighting-mode', 'true');
475
        }
476 21
        if ($chartType instanceof Bar3D || $chartType instanceof Pie3D) {
477
            // dr3d:light
478
            $arrayLight = array(
479 5
                array('#808080', '(0 0 1)', 'false', 'true'),
480
                array('#666666', '(0.2 0.4 1)', 'true', 'false'),
481
                array('#808080', '(0 0 1)', 'false', 'false'),
482
                array('#808080', '(0 0 1)', 'false', 'false'),
483
                array('#808080', '(0 0 1)', 'false', 'false'),
484
                array('#808080', '(0 0 1)', 'false', 'false'),
485
                array('#808080', '(0 0 1)', 'false', 'false'),
486
            );
487 5
            foreach ($arrayLight as $light) {
488 5
                $this->xmlContent->startElement('dr3d:light');
489 5
                $this->xmlContent->writeAttribute('dr3d:diffuse-color', $light[0]);
490 5
                $this->xmlContent->writeAttribute('dr3d:direction', $light[1]);
491 5
                $this->xmlContent->writeAttribute('dr3d:enabled', $light[2]);
492 5
                $this->xmlContent->writeAttribute('dr3d:specular', $light[3]);
493 5
                $this->xmlContent->endElement();
494
            }
495
        }
496
497
        //**** Axis ****
498 21
        $this->writeAxis($chart);
499
500
        //**** Series ****
501 21
        $this->rangeCol = 'B';
502 21
        $this->numSeries = 0;
503 21
        foreach ($chartType->getSeries() as $series) {
504 19
            $this->writeSeries($chart, $series);
505 19
            $this->rangeCol++;
506 19
            $this->numSeries++;
507
        }
508
509
        //**** Wall ****
510 21
        $this->writeWall();
511
        //**** Floor ****
512 21
        $this->writeFloor();
513
        // > chart:plot-area
514 21
        $this->xmlContent->endElement();
515 21
    }
516
517
    /**
518
     * @param Chart $chart
519
     * @link : http://books.evc-cit.info/odbook/ch08.html#chart-plot-area-section
520
     */
521 21
    private function writePlotAreaStyle(Chart $chart)
522
    {
523 21
        $chartType = $chart->getPlotArea()->getType();
524
525
        // style:style
526 21
        $this->xmlContent->startElement('style:style');
527 21
        $this->xmlContent->writeAttribute('style:name', 'stylePlotArea');
528 21
        $this->xmlContent->writeAttribute('style:family', 'chart');
529
        // style:text-properties
530 21
        $this->xmlContent->startElement('style:chart-properties');
531 21
        if ($chartType instanceof Bar3D) {
532 3
            $this->xmlContent->writeAttribute('chart:three-dimensional', 'true');
533 3
            $this->xmlContent->writeAttribute('chart:right-angled-axes', 'true');
534
        } elseif ($chartType instanceof Pie3D) {
535 2
            $this->xmlContent->writeAttribute('chart:three-dimensional', 'true');
536 2
            $this->xmlContent->writeAttribute('chart:right-angled-axes', 'true');
537
        }
538 21
        if ($chartType instanceof AbstractTypeBar) {
539 8
            if ($chartType->getBarDirection() == AbstractTypeBar::DIRECTION_HORIZONTAL) {
540 2
                $this->xmlContent->writeAttribute('chart:vertical', 'true');
541
            } else {
542 6
                $this->xmlContent->writeAttribute('chart:vertical', 'false');
543
            }
544 8
            if ($chartType->getBarGrouping() == Bar::GROUPING_CLUSTERED) {
545 6
                $this->xmlContent->writeAttribute('chart:stacked', 'false');
546 6
                $this->xmlContent->writeAttribute('chart:overlap', '0');
547 2
            } elseif ($chartType->getBarGrouping() == Bar::GROUPING_STACKED) {
548 1
                $this->xmlContent->writeAttribute('chart:stacked', 'true');
549 1
                $this->xmlContent->writeAttribute('chart:overlap', '100');
550 1
            } elseif ($chartType->getBarGrouping() == Bar::GROUPING_PERCENTSTACKED) {
551 1
                $this->xmlContent->writeAttribute('chart:stacked', 'true');
552 1
                $this->xmlContent->writeAttribute('chart:overlap', '100');
553 1
                $this->xmlContent->writeAttribute('chart:percentage', 'true');
554
            }
555
        }
556 21
        $labelFormat = 'value';
557 21
        if ($chartType instanceof AbstractTypeBar) {
558 8
            if ($chartType->getBarGrouping() == Bar::GROUPING_PERCENTSTACKED) {
559 1
                $labelFormat = 'percentage';
560
            }
561
        }
562 21
        $this->xmlContent->writeAttribute('chart:data-label-number', $labelFormat);
563
564
        // > style:text-properties
565 21
        $this->xmlContent->endElement();
566
        // > style:style
567 21
        $this->xmlContent->endElement();
568 21
    }
569
570
    /**
571
     * @param Chart $chart
572
     * @param Chart\Series $series
573
     * @throws \Exception
574
     */
575 19
    private function writeSeries(Chart $chart, Chart\Series $series)
576
    {
577 19
        $chartType = $chart->getPlotArea()->getType();
578
579 19
        $numRange = count($series->getValues());
580
        // chart:series
581 19
        $this->xmlContent->startElement('chart:series');
582 19
        $this->xmlContent->writeAttribute('chart:values-cell-range-address', 'table-local.$'.$this->rangeCol.'$2:.$'.$this->rangeCol.'$'.($numRange+1));
583 19
        $this->xmlContent->writeAttribute('chart:label-cell-address', 'table-local.$'.$this->rangeCol.'$1');
584 19
        if ($chartType instanceof Area) {
585 1
            $this->xmlContent->writeAttribute('chart:class', 'chart:area');
586
        } elseif ($chartType instanceof AbstractTypeBar) {
587 7
            $this->xmlContent->writeAttribute('chart:class', 'chart:bar');
588
        } elseif ($chartType instanceof Line) {
589 5
            $this->xmlContent->writeAttribute('chart:class', 'chart:line');
590
        } elseif ($chartType instanceof AbstractTypePie) {
591 3
            $this->xmlContent->writeAttribute('chart:class', 'chart:circle');
592
        } elseif ($chartType instanceof Scatter) {
593 3
            $this->xmlContent->writeAttribute('chart:class', 'chart:scatter');
594
        }
595 19
        $this->xmlContent->writeAttribute('chart:style-name', 'styleSeries'.$this->numSeries);
596 19
        if ($chartType instanceof Area || $chartType instanceof AbstractTypeBar || $chartType instanceof Line || $chartType instanceof Scatter) {
597 16
            $dataPointFills = $series->getDataPointFills();
598 16
            if (empty($dataPointFills)) {
599 12
                $incRepeat = $numRange;
600
            } else {
601 4
                $inc = 0;
602 4
                $incRepeat = 0;
603 4
                $newFill = new Fill();
604
                do {
605 4
                    if ($series->getDataPointFill($inc)->getHashCode() != $newFill->getHashCode()) {
606
                        // chart:data-point
607 4
                        $this->xmlContent->startElement('chart:data-point');
608 4
                        $this->xmlContent->writeAttribute('chart:repeated', $incRepeat);
609
                        // > chart:data-point
610 4
                        $this->xmlContent->endElement();
611 4
                        $incRepeat = 0;
612
613
                        // chart:data-point
614 4
                        $this->xmlContent->startElement('chart:data-point');
615 4
                        $this->xmlContent->writeAttribute('chart:style-name', 'styleSeries'.$this->numSeries.'_'.$inc);
616
                        // > chart:data-point
617 4
                        $this->xmlContent->endElement();
618
                    }
619 4
                    $inc++;
620 4
                    $incRepeat++;
621 4
                } while ($inc < $numRange);
622 4
                $incRepeat--;
623
            }
624
            // chart:data-point
625 16
            $this->xmlContent->startElement('chart:data-point');
626 16
            $this->xmlContent->writeAttribute('chart:repeated', $incRepeat);
627
            // > chart:data-point
628 16
            $this->xmlContent->endElement();
629
        } elseif ($chartType instanceof AbstractTypePie) {
630 3
            $count = count($series->getDataPointFills());
631 3
            for ($inc = 0; $inc < $count; $inc++) {
632
                // chart:data-point
633 2
                $this->xmlContent->startElement('chart:data-point');
634 2
                $this->xmlContent->writeAttribute('chart:style-name', 'styleSeries'.$this->numSeries.'_'.$inc);
635
                // > chart:data-point
636 2
                $this->xmlContent->endElement();
637
            }
638
        }
639
640
        // > chart:series
641 19
        $this->xmlContent->endElement();
642 19
    }
643
644
    /**
645
     * @param Chart $chart
646
     * @param Chart\Series $series
647
     */
648 19
    private function writeSeriesStyle(Chart $chart, Chart\Series $series)
649
    {
650 19
        $chartType = $chart->getPlotArea()->getType();
651
652
        // style:style
653 19
        $this->xmlContent->startElement('style:style');
654 19
        $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries);
655 19
        $this->xmlContent->writeAttribute('style:family', 'chart');
656
        // style:chart-properties
657 19
        $this->xmlContent->startElement('style:chart-properties');
658 19
        $this->xmlContent->writeAttribute('chart:data-label-number', 'value');
659 19
        $this->xmlContent->writeAttribute('chart:label-position', 'center');
660 19
        if ($chartType instanceof AbstractTypePie) {
661 3
            $this->xmlContent->writeAttribute('chart:pie-offset', $chartType->getExplosion());
662
        }
663 19
        if ($chartType instanceof Line || $chartType instanceof Scatter) {
664 8
            $oMarker = $series->getMarker();
665 8
            if ($oMarker->getSymbol() == Chart\Marker::SYMBOL_NONE) {
666
                /**
667
                 * @link : http://www.datypic.com/sc/odf/a-chart_symbol-type.html
668
                 */
669 8
                $this->xmlContent->writeAttribute('chart:symbol-type', 'none');
670
            } else {
671
                /**
672
                 * @link : http://www.datypic.com/sc/odf/a-chart_symbol-name.html
673
                 */
674 2
                $this->xmlContent->writeAttribute('chart:symbol-type', 'named-symbol');
675 2
                switch ($oMarker->getSymbol()) {
676 2
                    case Chart\Marker::SYMBOL_DASH:
677 2
                        $symbolName = 'horizontal-bar';
678 2
                        break;
679 2
                    case Chart\Marker::SYMBOL_DOT:
680 2
                        $symbolName = 'circle';
681 2
                        break;
682 2
                    case Chart\Marker::SYMBOL_TRIANGLE:
683 2
                        $symbolName = 'arrow-up';
684 2
                        break;
685
                    default:
686 2
                        $symbolName = $oMarker->getSymbol();
687 2
                        break;
688
                }
689 2
                $this->xmlContent->writeAttribute('chart:symbol-name', $symbolName);
690 2
                $symbolSize = number_format(CommonDrawing::pointsToCentimeters($oMarker->getSize()), 2, '.', '');
691 2
                $this->xmlContent->writeAttribute('chart:symbol-width', $symbolSize.'cm');
692 2
                $this->xmlContent->writeAttribute('chart:symbol-height', $symbolSize.'cm');
693
            }
694
        }
695
        // > style:chart-properties
696 19
        $this->xmlContent->endElement();
697
        // style:graphic-properties
698 19
        $this->xmlContent->startElement('style:graphic-properties');
699 19
        if ($chartType instanceof Line || $chartType instanceof Scatter) {
700 8
            $outlineWidth = '';
701 8
            $outlineColor = '';
702
703 8
            $oOutline = $series->getOutline();
704 8
            if ($oOutline instanceof Outline) {
705 2
                $outlineWidth = $oOutline->getWidth();
706 2
                if (!empty($outlineWidth)) {
707 2
                    $outlineWidth = number_format(CommonDrawing::pointsToCentimeters($outlineWidth), 3, '.', '');
708
                }
709 2
                $outlineColor = $oOutline->getFill()->getStartColor()->getRGB();
710
            }
711 8
            if (empty($outlineWidth)) {
712 8
                $outlineWidth = '0.079';
713
            }
714 8
            if (empty($outlineColor)) {
715 8
                $outlineColor = '4a7ebb';
716
            }
717 8
            $this->xmlContent->writeAttribute('svg:stroke-width', $outlineWidth.'cm');
718 8
            $this->xmlContent->writeAttribute('svg:stroke-color', '#'.$outlineColor);
719
        } else {
720 11
            $this->xmlContent->writeAttribute('draw:stroke', 'none');
721 11
            if (!($chartType instanceof Area)) {
722 10
                $this->xmlContent->writeAttribute('draw:fill', $series->getFill()->getFillType());
723
            }
724
        }
725 19
        $this->xmlContent->writeAttribute('draw:fill-color', '#'.$series->getFill()->getStartColor()->getRGB());
726
        // > style:graphic-properties
727 19
        $this->xmlContent->endElement();
728
        // style:text-properties
729 19
        $this->xmlContent->startElement('style:text-properties');
730 19
        $this->xmlContent->writeAttribute('fo:color', '#'.$series->getFont()->getColor()->getRGB());
731 19
        $this->xmlContent->writeAttribute('fo:font-family', $series->getFont()->getName());
732 19
        $this->xmlContent->writeAttribute('fo:font-size', $series->getFont()->getSize().'pt');
733
        // > style:text-properties
734 19
        $this->xmlContent->endElement();
735
736
        // > style:style
737 19
        $this->xmlContent->endElement();
738
739 19
        foreach ($series->getDataPointFills() as $idx => $oFill) {
740
            // style:style
741 6
            $this->xmlContent->startElement('style:style');
742 6
            $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries.'_'.$idx);
743 6
            $this->xmlContent->writeAttribute('style:family', 'chart');
744
            // style:graphic-properties
745 6
            $this->xmlContent->startElement('style:graphic-properties');
746 6
            $this->xmlContent->writeAttribute('draw:fill', $oFill->getFillType());
747 6
            $this->xmlContent->writeAttribute('draw:fill-color', '#'.$oFill->getStartColor()->getRGB());
748
            // > style:graphic-properties
749 6
            $this->xmlContent->endElement();
750
            // > style:style
751 6
            $this->xmlContent->endElement();
752
        }
753 19
    }
754
755
    /**
756
     */
757 21
    private function writeTable()
758
    {
759
        // table:table
760 21
        $this->xmlContent->startElement('table:table');
761 21
        $this->xmlContent->writeAttribute('table:name', 'table-local');
762
763
        // table:table-header-columns
764 21
        $this->xmlContent->startElement('table:table-header-columns');
765
        // table:table-column
766 21
        $this->xmlContent->startElement('table:table-column');
767
        // > table:table-column
768 21
        $this->xmlContent->endElement();
769
        // > table:table-header-columns
770 21
        $this->xmlContent->endElement();
771
772
        // table:table-columns
773 21
        $this->xmlContent->startElement('table:table-columns');
774
        // table:table-column
775 21
        $this->xmlContent->startElement('table:table-column');
776 21
        if (!empty($this->arrayData)) {
777 19
            $rowFirst = reset($this->arrayData);
778 19
            $this->xmlContent->writeAttribute('table:number-columns-repeated', count($rowFirst) - 1);
779
        }
780
        // > table:table-column
781 21
        $this->xmlContent->endElement();
782
        // > table:table-columns
783 21
        $this->xmlContent->endElement();
784
785
        // table:table-header-rows
786 21
        $this->xmlContent->startElement('table:table-header-rows');
787
        // table:table-row
788 21
        $this->xmlContent->startElement('table:table-row');
789 21
        if (!empty($this->arrayData)) {
790 19
            $rowFirst = reset($this->arrayData);
791 19
            foreach ($rowFirst as $key => $cell) {
792
                // table:table-cell
793 19
                $this->xmlContent->startElement('table:table-cell');
794 19
                if (isset($this->arrayTitle[$key - 1])) {
795 19
                    $this->xmlContent->writeAttribute('office:value-type', 'string');
796
                }
797
                // text:p
798 19
                $this->xmlContent->startElement('text:p');
799 19
                if (isset($this->arrayTitle[$key - 1])) {
800 19
                    $this->xmlContent->text($this->arrayTitle[$key - 1]);
801
                }
802
                // > text:p
803 19
                $this->xmlContent->endElement();
804
                // > table:table-cell
805 19
                $this->xmlContent->endElement();
806
            }
807
        }
808
        // > table:table-row
809 21
        $this->xmlContent->endElement();
810
        // > table:table-header-rows
811 21
        $this->xmlContent->endElement();
812
813
        // table:table-rows
814 21
        $this->xmlContent->startElement('table:table-rows');
815
816 21
        foreach ($this->arrayData as $row) {
817
            // table:table-row
818 19
            $this->xmlContent->startElement('table:table-row');
819 19
            foreach ($row as $cell) {
820
                // table:table-cell
821 19
                $this->xmlContent->startElement('table:table-cell');
822 19
                if (is_numeric($cell)) {
823 19
                    $this->xmlContent->writeAttribute('office:value-type', 'float');
824 19
                    $this->xmlContent->writeAttribute('office:value', $cell);
825
                } else {
826 19
                    $this->xmlContent->writeAttribute('office:value-type', 'string');
827
                }
828
                // text:p
829 19
                $this->xmlContent->startElement('text:p');
830 19
                $this->xmlContent->text($cell);
831
                // > text:p
832 19
                $this->xmlContent->endElement();
833
                // > table:table-cell
834 19
                $this->xmlContent->endElement();
835
            }
836
            // > table:table-row
837 19
            $this->xmlContent->endElement();
838
        }
839
840
        // > table:table-rows
841 21
        $this->xmlContent->endElement();
842
        // > table:table
843 21
        $this->xmlContent->endElement();
844 21
    }
845
846
    /**
847
     * @param Title $oTitle
848
     */
849 21
    private function writeTitle(Title $oTitle)
850
    {
851 21
        if ($oTitle->isVisible()) {
852
            // chart:title
853 21
            $this->xmlContent->startElement('chart:title');
854 21
            $this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetX()), 3) . 'cm');
855 21
            $this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetY()), 3) . 'cm');
856 21
            $this->xmlContent->writeAttribute('chart:style-name', 'styleTitle');
857
            // > text:p
858 21
            $this->xmlContent->startElement('text:p');
859 21
            $this->xmlContent->text($oTitle->getText());
860
            // > text:p
861 21
            $this->xmlContent->endElement();
862
            // > chart:title
863 21
            $this->xmlContent->endElement();
864
        }
865 21
    }
866
867
    /**
868
     * @param Title $oTitle
869
     */
870 21
    private function writeTitleStyle(Title $oTitle)
871
    {
872 21
        if ($oTitle->isVisible()) {
873
            // style:style
874 21
            $this->xmlContent->startElement('style:style');
875 21
            $this->xmlContent->writeAttribute('style:name', 'styleTitle');
876 21
            $this->xmlContent->writeAttribute('style:family', 'chart');
877
            // style:text-properties
878 21
            $this->xmlContent->startElement('style:text-properties');
879 21
            $this->xmlContent->writeAttribute('fo:color', '#'.$oTitle->getFont()->getColor()->getRGB());
880 21
            $this->xmlContent->writeAttribute('fo:font-family', $oTitle->getFont()->getName());
881 21
            $this->xmlContent->writeAttribute('fo:font-size', $oTitle->getFont()->getSize().'pt');
882 21
            $this->xmlContent->writeAttribute('fo:font-style', $oTitle->getFont()->isItalic() ? 'italic' : 'normal');
883
            // > style:text-properties
884 21
            $this->xmlContent->endElement();
885
            // > style:style
886 21
            $this->xmlContent->endElement();
887
        }
888 21
    }
889
890 21
    private function writeWall()
891
    {
892
        // chart:wall
893 21
        $this->xmlContent->startElement('chart:wall');
894 21
        $this->xmlContent->writeAttribute('chart:style-name', 'styleWall');
895
        // > chart:wall
896 21
        $this->xmlContent->endElement();
897 21
    }
898
899
    /**
900
     * @param Chart $chart
901
     */
902 21
    private function writeWallStyle(Chart $chart)
903
    {
904 21
        $chartType = $chart->getPlotArea()->getType();
905
906
        // style:style
907 21
        $this->xmlContent->startElement('style:style');
908 21
        $this->xmlContent->writeAttribute('style:name', 'styleWall');
909 21
        $this->xmlContent->writeAttribute('style:family', 'chart');
910
        // style:chart-properties
911 21
        $this->xmlContent->startElement('style:graphic-properties');
912
        //@todo : Permit edit color and size border of wall
913 21
        if ($chartType instanceof Line || $chartType instanceof Scatter) {
914 9
            $this->xmlContent->writeAttribute('draw:fill', 'solid');
915 9
            $this->xmlContent->writeAttribute('draw:fill-color', '#FFFFFF');
916
        } else {
917 12
            $this->xmlContent->writeAttribute('draw:fill', 'none');
918 12
            $this->xmlContent->writeAttribute('draw:stroke', 'solid');
919 12
            $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm');
920 12
            $this->xmlContent->writeAttribute('svg:stroke-color', '#878787');
921
        }
922
        // > style:chart-properties
923 21
        $this->xmlContent->endElement();
924
        // > style:style
925 21
        $this->xmlContent->endElement();
926 21
    }
927
928
}
929