Completed
Pull Request — develop (#209)
by Franck
47:59 queued 38:42
created

ObjectsChart::writeFloor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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