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 | * @throws \Exception |
||
| 53 | */ |
||
| 54 | 62 | public function render() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param Chart $chart |
||
| 69 | * @return string |
||
| 70 | * @throws \Exception |
||
| 71 | */ |
||
| 72 | 24 | protected function writeContentPart(Chart $chart) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param Chart $chart |
||
| 198 | * @throws \Exception |
||
| 199 | */ |
||
| 200 | 24 | private function writeAxis(Chart $chart) |
|
| 241 | |||
| 242 | 24 | protected function writeGridline($oGridlines, $styleName, $chartClass) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param Chart $chart |
||
| 256 | * @throws \Exception |
||
| 257 | * @todo Set function in \PhpPresentation\Shape\Chart\Axis for defining width and color of the axis |
||
| 258 | */ |
||
| 259 | 24 | protected function writeAxisStyle(Chart $chart) |
|
| 260 | { |
||
| 261 | 24 | $chartType = $chart->getPlotArea()->getType(); |
|
| 262 | |||
| 263 | // AxisX |
||
| 264 | // style:style |
||
| 265 | 24 | $this->xmlContent->startElement('style:style'); |
|
| 266 | 24 | $this->xmlContent->writeAttribute('style:name', 'styleAxisX'); |
|
| 267 | 24 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 268 | // style:style > style:chart-properties |
||
| 269 | 24 | $this->xmlContent->startElement('style:chart-properties'); |
|
| 270 | 24 | $this->xmlContent->writeAttribute('chart:display-label', 'true'); |
|
| 271 | 24 | $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false'); |
|
| 272 | 24 | $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false'); |
|
| 273 | 24 | if ($chartType instanceof AbstractTypePie) { |
|
| 274 | 5 | $this->xmlContent->writeAttribute('chart:reverse-direction', 'true'); |
|
| 275 | } |
||
| 276 | 24 | if ($chart->getPlotArea()->getAxisX()->getMinBounds() != null) { |
|
|
|
|||
| 277 | 1 | $this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisX()->getMinBounds()); |
|
| 278 | } |
||
| 279 | 24 | if ($chart->getPlotArea()->getAxisX()->getMaxBounds() != null) { |
|
| 280 | 1 | $this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisX()->getMaxBounds()); |
|
| 281 | } |
||
| 282 | 24 | $this->xmlContent->endElement(); |
|
| 283 | // style:style > style:graphic-properties |
||
| 284 | 24 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 285 | 24 | $this->xmlContent->writeAttribute('draw:stroke', 'solid'); |
|
| 286 | 24 | $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm'); |
|
| 287 | 24 | $this->xmlContent->writeAttribute('svg:stroke-color', '#878787'); |
|
| 288 | 24 | $this->xmlContent->endElement(); |
|
| 289 | // style:style > style:text-properties |
||
| 290 | 24 | $oFont = $chart->getPlotArea()->getAxisX()->getFont(); |
|
| 291 | 24 | $this->xmlContent->startElement('style:text-properties'); |
|
| 292 | 24 | $this->xmlContent->writeAttribute('fo:color', '#'.$oFont->getColor()->getRGB()); |
|
| 293 | 24 | $this->xmlContent->writeAttribute('fo:font-family', $oFont->getName()); |
|
| 294 | 24 | $this->xmlContent->writeAttribute('fo:font-size', $oFont->getSize().'pt'); |
|
| 295 | 24 | $this->xmlContent->writeAttribute('fo:font-style', $oFont->isItalic() ? 'italic' : 'normal'); |
|
| 296 | 24 | $this->xmlContent->endElement(); |
|
| 297 | // ##style:style |
||
| 298 | 24 | $this->xmlContent->endElement(); |
|
| 299 | |||
| 300 | // AxisX GridLines Major |
||
| 301 | 24 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor'); |
|
| 302 | |||
| 303 | // AxisX GridLines Minor |
||
| 304 | 24 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor'); |
|
| 305 | |||
| 306 | // AxisY |
||
| 307 | // style:style |
||
| 308 | 24 | $this->xmlContent->startElement('style:style'); |
|
| 309 | 24 | $this->xmlContent->writeAttribute('style:name', 'styleAxisY'); |
|
| 310 | 24 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 311 | // style:style > style:chart-properties |
||
| 312 | 24 | $this->xmlContent->startElement('style:chart-properties'); |
|
| 313 | 24 | $this->xmlContent->writeAttribute('chart:display-label', 'true'); |
|
| 314 | 24 | $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false'); |
|
| 315 | 24 | $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false'); |
|
| 316 | 24 | if ($chartType instanceof AbstractTypePie) { |
|
| 317 | 5 | $this->xmlContent->writeAttribute('chart:reverse-direction', 'true'); |
|
| 318 | } |
||
| 319 | 24 | if ($chart->getPlotArea()->getAxisY()->getMinBounds() !== null) { |
|
| 320 | $this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisY()->getMinBounds()); |
||
| 321 | } |
||
| 322 | 24 | if ($chart->getPlotArea()->getAxisY()->getMaxBounds() !== null) { |
|
| 323 | $this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisY()->getMaxBounds()); |
||
| 324 | } |
||
| 325 | 24 | $this->xmlContent->endElement(); |
|
| 326 | // style:graphic-properties |
||
| 327 | 24 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 328 | 24 | $this->xmlContent->writeAttribute('draw:stroke', 'solid'); |
|
| 329 | 24 | $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm'); |
|
| 330 | 24 | $this->xmlContent->writeAttribute('svg:stroke-color', '#878787'); |
|
| 331 | 24 | $this->xmlContent->endElement(); |
|
| 332 | // style:style > style:text-properties |
||
| 333 | 24 | $oFont = $chart->getPlotArea()->getAxisY()->getFont(); |
|
| 334 | 24 | $this->xmlContent->startElement('style:text-properties'); |
|
| 335 | 24 | $this->xmlContent->writeAttribute('fo:color', '#'.$oFont->getColor()->getRGB()); |
|
| 336 | 24 | $this->xmlContent->writeAttribute('fo:font-family', $oFont->getName()); |
|
| 337 | 24 | $this->xmlContent->writeAttribute('fo:font-size', $oFont->getSize().'pt'); |
|
| 338 | 24 | $this->xmlContent->writeAttribute('fo:font-style', $oFont->isItalic() ? 'italic' : 'normal'); |
|
| 339 | 24 | $this->xmlContent->endElement(); |
|
| 340 | // ## style:style |
||
| 341 | 24 | $this->xmlContent->endElement(); |
|
| 342 | |||
| 343 | // AxisY GridLines Major |
||
| 344 | 24 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor'); |
|
| 345 | |||
| 346 | // AxisY GridLines Minor |
||
| 347 | 24 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor'); |
|
| 348 | 24 | } |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param Chart\Gridlines $oGridlines |
||
| 352 | * @param string $styleName |
||
| 353 | */ |
||
| 354 | 24 | protected function writeGridlineStyle($oGridlines, $styleName) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param Chart $chart |
||
| 374 | */ |
||
| 375 | 24 | private function writeChartStyle(Chart $chart) |
|
| 390 | |||
| 391 | 24 | private function writeFloor() |
|
| 399 | |||
| 400 | 24 | private function writeFloorStyle() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param Chart $chart |
||
| 421 | */ |
||
| 422 | 24 | private function writeLegend(Chart $chart) |
|
| 423 | { |
||
| 424 | // chart:legend |
||
| 425 | 24 | $this->xmlContent->startElement('chart:legend'); |
|
| 426 | 24 | switch ($chart->getLegend()->getPosition()) { |
|
| 427 | case Chart\Legend::POSITION_BOTTOM: |
||
| 428 | 1 | $position = 'bottom'; |
|
| 429 | 1 | break; |
|
| 430 | case Chart\Legend::POSITION_LEFT: |
||
| 431 | 1 | $position = 'start'; |
|
| 432 | 1 | break; |
|
| 433 | case Chart\Legend::POSITION_TOP: |
||
| 434 | 1 | $position = 'top'; |
|
| 435 | 1 | break; |
|
| 436 | case Chart\Legend::POSITION_TOPRIGHT: |
||
| 437 | 1 | $position = 'top-end'; |
|
| 438 | 1 | break; |
|
| 439 | case Chart\Legend::POSITION_RIGHT: |
||
| 440 | default: |
||
| 441 | 24 | $position = 'end'; |
|
| 442 | 24 | break; |
|
| 443 | } |
||
| 444 | 24 | $this->xmlContent->writeAttribute('chart:legend-position', $position); |
|
| 445 | 24 | $this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetX()), 3) . 'cm'); |
|
| 446 | 24 | $this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($chart->getLegend()->getOffsetY()), 3) . 'cm'); |
|
| 447 | 24 | $this->xmlContent->writeAttribute('style:legend-expansion', 'high'); |
|
| 448 | 24 | $this->xmlContent->writeAttribute('chart:style-name', 'styleLegend'); |
|
| 449 | // > chart:legend |
||
| 450 | 24 | $this->xmlContent->endElement(); |
|
| 451 | 24 | } |
|
| 452 | |||
| 453 | /** |
||
| 454 | * @param Chart $chart |
||
| 455 | */ |
||
| 456 | 24 | private function writeLegendStyle(Chart $chart) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @param Chart $chart |
||
| 481 | * @throws \Exception |
||
| 482 | */ |
||
| 483 | 24 | private function writePlotArea(Chart $chart) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * @param Chart $chart |
||
| 537 | * @throws \Exception |
||
| 538 | * @link : http://books.evc-cit.info/odbook/ch08.html#chart-plot-area-section |
||
| 539 | */ |
||
| 540 | 24 | private function writePlotAreaStyle(Chart $chart) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * @param Chart $chart |
||
| 591 | * @param Chart\Series $series |
||
| 592 | * @throws \Exception |
||
| 593 | */ |
||
| 594 | 22 | private function writeSeries(Chart $chart, Chart\Series $series) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * @param Chart $chart |
||
| 665 | * @param Chart\Series $series |
||
| 666 | * @throws \Exception |
||
| 667 | */ |
||
| 668 | 22 | private function writeSeriesStyle(Chart $chart, Chart\Series $series) |
|
| 669 | { |
||
| 670 | 22 | $chartType = $chart->getPlotArea()->getType(); |
|
| 671 | |||
| 672 | // style:style |
||
| 673 | 22 | $this->xmlContent->startElement('style:style'); |
|
| 674 | 22 | $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries); |
|
| 675 | 22 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 676 | // style:chart-properties |
||
| 677 | 22 | $this->xmlContent->startElement('style:chart-properties'); |
|
| 678 | 22 | if ($series->hasShowValue()) { |
|
| 679 | 22 | if ($series->hasShowPercentage()) { |
|
| 680 | 1 | $this->xmlContent->writeAttribute('chart:data-label-number', 'value-and-percentage'); |
|
| 681 | } else { |
||
| 682 | 22 | $this->xmlContent->writeAttribute('chart:data-label-number', 'value'); |
|
| 683 | } |
||
| 684 | 1 | } elseif ($series->hasShowPercentage()) { |
|
| 685 | 1 | $this->xmlContent->writeAttribute('chart:data-label-number', 'percentage'); |
|
| 686 | } |
||
| 687 | 22 | if ($series->hasShowCategoryName()) { |
|
| 688 | 1 | $this->xmlContent->writeAttribute('chart:data-label-text', 'true'); |
|
| 689 | } |
||
| 690 | 22 | $this->xmlContent->writeAttribute('chart:label-position', 'center'); |
|
| 691 | 22 | if ($chartType instanceof AbstractTypePie) { |
|
| 692 | 5 | $this->xmlContent->writeAttribute('chart:pie-offset', $chartType->getExplosion()); |
|
| 693 | } |
||
| 694 | 22 | if ($chartType instanceof Line || $chartType instanceof Scatter) { |
|
| 695 | 9 | $oMarker = $series->getMarker(); |
|
| 696 | /** |
||
| 697 | * @link : http://www.datypic.com/sc/odf/a-chart_symbol-type.html |
||
| 698 | */ |
||
| 699 | 9 | $this->xmlContent->writeAttributeIf($oMarker->getSymbol() == Chart\Marker::SYMBOL_NONE, 'chart:symbol-type', 'none'); |
|
| 700 | /** |
||
| 701 | * @link : http://www.datypic.com/sc/odf/a-chart_symbol-name.html |
||
| 702 | */ |
||
| 703 | 9 | $this->xmlContent->writeAttributeIf($oMarker->getSymbol() != Chart\Marker::SYMBOL_NONE, 'chart:symbol-type', 'named-symbol'); |
|
| 704 | 9 | if ($oMarker->getSymbol() != Chart\Marker::SYMBOL_NONE) { |
|
| 705 | 2 | switch ($oMarker->getSymbol()) { |
|
| 706 | case Chart\Marker::SYMBOL_DASH: |
||
| 707 | 2 | $symbolName = 'horizontal-bar'; |
|
| 708 | 2 | break; |
|
| 709 | case Chart\Marker::SYMBOL_DOT: |
||
| 710 | 2 | $symbolName = 'circle'; |
|
| 711 | 2 | break; |
|
| 712 | case Chart\Marker::SYMBOL_TRIANGLE: |
||
| 713 | 2 | $symbolName = 'arrow-up'; |
|
| 714 | 2 | break; |
|
| 715 | default: |
||
| 716 | 2 | $symbolName = $oMarker->getSymbol(); |
|
| 717 | 2 | break; |
|
| 718 | } |
||
| 719 | 2 | $this->xmlContent->writeAttribute('chart:symbol-name', $symbolName); |
|
| 720 | 2 | $symbolSize = number_format(CommonDrawing::pointsToCentimeters($oMarker->getSize()), 2, '.', ''); |
|
| 721 | 2 | $this->xmlContent->writeAttribute('chart:symbol-width', $symbolSize.'cm'); |
|
| 722 | 2 | $this->xmlContent->writeAttribute('chart:symbol-height', $symbolSize.'cm'); |
|
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 726 | 22 | $separator = $series->getSeparator(); |
|
| 727 | 22 | if (!empty($separator)) { |
|
| 728 | // style:chart-properties/chart:label-separator |
||
| 729 | 1 | $this->xmlContent->startElement('chart:label-separator'); |
|
| 730 | 1 | if ($separator == PHP_EOL) { |
|
| 731 | $this->xmlContent->writeRaw('<text:p><text:line-break /></text:p>'); |
||
| 732 | } else { |
||
| 733 | 1 | $this->xmlContent->writeElement('text:p', $separator); |
|
| 734 | } |
||
| 735 | 1 | $this->xmlContent->endElement(); |
|
| 736 | } |
||
| 737 | |||
| 738 | // > style:chart-properties |
||
| 739 | 22 | $this->xmlContent->endElement(); |
|
| 740 | // style:graphic-properties |
||
| 741 | 22 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 742 | 22 | if ($chartType instanceof Line || $chartType instanceof Scatter) { |
|
| 743 | 9 | $outlineWidth = ''; |
|
| 744 | 9 | $outlineColor = ''; |
|
| 745 | |||
| 746 | 9 | $oOutline = $series->getOutline(); |
|
| 747 | 9 | if ($oOutline instanceof Outline) { |
|
| 748 | 2 | $outlineWidth = $oOutline->getWidth(); |
|
| 749 | 2 | if (!empty($outlineWidth)) { |
|
| 750 | 2 | $outlineWidth = number_format(CommonDrawing::pointsToCentimeters($outlineWidth), 3, '.', ''); |
|
| 751 | } |
||
| 752 | 2 | $outlineColor = $oOutline->getFill()->getStartColor()->getRGB(); |
|
| 753 | } |
||
| 754 | 9 | if (empty($outlineWidth)) { |
|
| 755 | 9 | $outlineWidth = '0.079'; |
|
| 756 | } |
||
| 757 | 9 | if (empty($outlineColor)) { |
|
| 758 | 9 | $outlineColor = '4a7ebb'; |
|
| 759 | } |
||
| 760 | 9 | $this->xmlContent->writeAttribute('svg:stroke-width', $outlineWidth.'cm'); |
|
| 761 | 9 | $this->xmlContent->writeAttribute('svg:stroke-color', '#'.$outlineColor); |
|
| 762 | } else { |
||
| 763 | 13 | $this->xmlContent->writeAttribute('draw:stroke', 'none'); |
|
| 764 | 13 | if (!($chartType instanceof Area)) { |
|
| 765 | 12 | $this->xmlContent->writeAttribute('draw:fill', $series->getFill()->getFillType()); |
|
| 766 | } |
||
| 767 | } |
||
| 768 | 22 | $this->xmlContent->writeAttribute('draw:fill-color', '#'.$series->getFill()->getStartColor()->getRGB()); |
|
| 769 | // > style:graphic-properties |
||
| 770 | 22 | $this->xmlContent->endElement(); |
|
| 771 | // style:text-properties |
||
| 772 | 22 | $this->xmlContent->startElement('style:text-properties'); |
|
| 773 | 22 | $this->xmlContent->writeAttribute('fo:color', '#'.$series->getFont()->getColor()->getRGB()); |
|
| 774 | 22 | $this->xmlContent->writeAttribute('fo:font-family', $series->getFont()->getName()); |
|
| 775 | 22 | $this->xmlContent->writeAttribute('fo:font-size', $series->getFont()->getSize().'pt'); |
|
| 776 | // > style:text-properties |
||
| 777 | 22 | $this->xmlContent->endElement(); |
|
| 778 | |||
| 779 | // > style:style |
||
| 780 | 22 | $this->xmlContent->endElement(); |
|
| 781 | |||
| 782 | 22 | foreach ($series->getDataPointFills() as $idx => $oFill) { |
|
| 783 | // style:style |
||
| 784 | 7 | $this->xmlContent->startElement('style:style'); |
|
| 785 | 7 | $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries.'_'.$idx); |
|
| 786 | 7 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 787 | // style:graphic-properties |
||
| 788 | 7 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 789 | 7 | $this->xmlContent->writeAttribute('draw:fill', $oFill->getFillType()); |
|
| 790 | 7 | $this->xmlContent->writeAttribute('draw:fill-color', '#'.$oFill->getStartColor()->getRGB()); |
|
| 791 | // > style:graphic-properties |
||
| 792 | 7 | $this->xmlContent->endElement(); |
|
| 793 | // > style:style |
||
| 794 | 7 | $this->xmlContent->endElement(); |
|
| 795 | } |
||
| 796 | 22 | } |
|
| 797 | |||
| 798 | /** |
||
| 799 | */ |
||
| 800 | 24 | private function writeTable() |
|
| 894 | |||
| 895 | /** |
||
| 896 | * @param Title $oTitle |
||
| 897 | */ |
||
| 898 | 24 | private function writeTitle(Title $oTitle) |
|
| 915 | |||
| 916 | /** |
||
| 917 | * @param Title $oTitle |
||
| 918 | */ |
||
| 919 | 24 | private function writeTitleStyle(Title $oTitle) |
|
| 939 | |||
| 940 | 24 | private function writeWall() |
|
| 947 | |||
| 948 | /** |
||
| 949 | * @param Chart $chart |
||
| 950 | * @throws \Exception |
||
| 951 | */ |
||
| 952 | 24 | private function writeWallStyle(Chart $chart) |
|
| 977 | } |
||
| 978 |