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