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 |
||
| 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 | 59 | public function render() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param Chart $chart |
||
| 67 | * @return string |
||
| 68 | * @throws \Exception |
||
| 69 | */ |
||
| 70 | 23 | protected function writeContentPart(Chart $chart) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param Chart $chart |
||
| 215 | */ |
||
| 216 | 23 | private function writeAxis(Chart $chart) |
|
| 258 | |||
| 259 | 23 | protected function writeGridline($oGridlines, $styleName, $chartClass) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param Chart $chart |
||
| 273 | * @todo Set function in \PhpPresentation\Shape\Chart\Axis for defining width and color of the axis |
||
| 274 | */ |
||
| 275 | 23 | protected function writeAxisStyle(Chart $chart) |
|
| 276 | { |
||
| 277 | 23 | $chartType = $chart->getPlotArea()->getType(); |
|
| 278 | |||
| 279 | // AxisX |
||
| 280 | // style:style |
||
| 281 | 23 | $this->xmlContent->startElement('style:style'); |
|
| 282 | 23 | $this->xmlContent->writeAttribute('style:name', 'styleAxisX'); |
|
| 283 | 23 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 284 | // style:style > style:chart-properties |
||
| 285 | 23 | $this->xmlContent->startElement('style:chart-properties'); |
|
| 286 | 23 | $this->xmlContent->writeAttribute('chart:display-label', 'true'); |
|
| 287 | 23 | $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false'); |
|
| 288 | 23 | $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false'); |
|
| 289 | 23 | if ($chartType instanceof AbstractTypePie) { |
|
| 290 | 4 | $this->xmlContent->writeAttribute('chart:reverse-direction', 'true'); |
|
| 291 | } |
||
| 292 | 23 | if ($chart->getPlotArea()->getAxisX()->getMinBounds() != null) { |
|
|
|
|||
| 293 | $this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisX()->getMinBounds()); |
||
| 294 | } |
||
| 295 | 23 | if ($chart->getPlotArea()->getAxisX()->getMaxBounds() != null) { |
|
| 296 | $this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisX()->getMaxBounds()); |
||
| 297 | } |
||
| 298 | 23 | $this->xmlContent->endElement(); |
|
| 299 | // style:style > style:text-properties |
||
| 300 | 23 | $oFont = $chart->getPlotArea()->getAxisX()->getFont(); |
|
| 301 | 23 | $this->xmlContent->startElement('style:text-properties'); |
|
| 302 | 23 | $this->xmlContent->writeAttribute('fo:color', '#'.$oFont->getColor()->getRGB()); |
|
| 303 | 23 | $this->xmlContent->writeAttribute('fo:font-family', $oFont->getName()); |
|
| 304 | 23 | $this->xmlContent->writeAttribute('fo:font-size', $oFont->getSize().'pt'); |
|
| 305 | 23 | $this->xmlContent->writeAttribute('fo:font-style', $oFont->isItalic() ? 'italic' : 'normal'); |
|
| 306 | 23 | $this->xmlContent->endElement(); |
|
| 307 | // style:style > style:graphic-properties |
||
| 308 | 23 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 309 | 23 | $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm'); |
|
| 310 | 23 | $this->xmlContent->writeAttribute('svg:stroke-color', '#878787'); |
|
| 311 | 23 | $this->xmlContent->endElement(); |
|
| 312 | // ##style:style |
||
| 313 | 23 | $this->xmlContent->endElement(); |
|
| 314 | |||
| 315 | // AxisX GridLines Major |
||
| 316 | 23 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMajorGridlines(), 'styleAxisXGridlinesMajor'); |
|
| 317 | |||
| 318 | // AxisX GridLines Minor |
||
| 319 | 23 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisX()->getMinorGridlines(), 'styleAxisXGridlinesMinor'); |
|
| 320 | |||
| 321 | // AxisY |
||
| 322 | // style:style |
||
| 323 | 23 | $this->xmlContent->startElement('style:style'); |
|
| 324 | 23 | $this->xmlContent->writeAttribute('style:name', 'styleAxisY'); |
|
| 325 | 23 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 326 | // style:style > style:chart-properties |
||
| 327 | 23 | $this->xmlContent->startElement('style:chart-properties'); |
|
| 328 | 23 | $this->xmlContent->writeAttribute('chart:display-label', 'true'); |
|
| 329 | 23 | $this->xmlContent->writeAttribute('chart:tick-marks-major-inner', 'false'); |
|
| 330 | 23 | $this->xmlContent->writeAttribute('chart:tick-marks-major-outer', 'false'); |
|
| 331 | 23 | if ($chartType instanceof AbstractTypePie) { |
|
| 332 | 4 | $this->xmlContent->writeAttribute('chart:reverse-direction', 'true'); |
|
| 333 | } |
||
| 334 | 23 | if ($chart->getPlotArea()->getAxisY()->getMinBounds() != null) { |
|
| 335 | $this->xmlContent->writeAttribute('chart:minimum', $chart->getPlotArea()->getAxisY()->getMinBounds()); |
||
| 336 | } |
||
| 337 | 23 | if ($chart->getPlotArea()->getAxisY()->getMaxBounds() != null) { |
|
| 338 | $this->xmlContent->writeAttribute('chart:maximum', $chart->getPlotArea()->getAxisY()->getMaxBounds()); |
||
| 339 | } |
||
| 340 | 23 | $this->xmlContent->endElement(); |
|
| 341 | // style:style > style:text-properties |
||
| 342 | 23 | $oFont = $chart->getPlotArea()->getAxisY()->getFont(); |
|
| 343 | 23 | $this->xmlContent->startElement('style:text-properties'); |
|
| 344 | 23 | $this->xmlContent->writeAttribute('fo:color', '#'.$oFont->getColor()->getRGB()); |
|
| 345 | 23 | $this->xmlContent->writeAttribute('fo:font-family', $oFont->getName()); |
|
| 346 | 23 | $this->xmlContent->writeAttribute('fo:font-size', $oFont->getSize().'pt'); |
|
| 347 | 23 | $this->xmlContent->writeAttribute('fo:font-style', $oFont->isItalic() ? 'italic' : 'normal'); |
|
| 348 | 23 | $this->xmlContent->endElement(); |
|
| 349 | // style:graphic-properties |
||
| 350 | 23 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 351 | 23 | $this->xmlContent->writeAttribute('svg:stroke-width', '0.026cm'); |
|
| 352 | 23 | $this->xmlContent->writeAttribute('svg:stroke-color', '#878787'); |
|
| 353 | 23 | $this->xmlContent->endElement(); |
|
| 354 | // ## style:style |
||
| 355 | 23 | $this->xmlContent->endElement(); |
|
| 356 | |||
| 357 | // AxisY GridLines Major |
||
| 358 | 23 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMajorGridlines(), 'styleAxisYGridlinesMajor'); |
|
| 359 | |||
| 360 | // AxisY GridLines Minor |
||
| 361 | 23 | $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor'); |
|
| 362 | 23 | } |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param Chart\Gridlines $oGridlines |
||
| 366 | * @param string $styleName |
||
| 367 | */ |
||
| 368 | 23 | protected function writeGridlineStyle($oGridlines, $styleName) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param Chart $chart |
||
| 388 | */ |
||
| 389 | 23 | private function writeChartStyle(Chart $chart) |
|
| 404 | |||
| 405 | 23 | private function writeFloor() |
|
| 413 | |||
| 414 | 23 | private function writeFloorStyle() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @param Chart $chart |
||
| 435 | */ |
||
| 436 | 23 | private function writeLegend(Chart $chart) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @param Chart $chart |
||
| 451 | */ |
||
| 452 | 23 | private function writeLegendStyle(Chart $chart) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @param Chart $chart |
||
| 472 | */ |
||
| 473 | 23 | private function writePlotArea(Chart $chart) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * @param Chart $chart |
||
| 527 | * @link : http://books.evc-cit.info/odbook/ch08.html#chart-plot-area-section |
||
| 528 | */ |
||
| 529 | 23 | private function writePlotAreaStyle(Chart $chart) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * @param Chart $chart |
||
| 580 | * @param Chart\Series $series |
||
| 581 | * @throws \Exception |
||
| 582 | */ |
||
| 583 | 21 | private function writeSeries(Chart $chart, Chart\Series $series) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * @param Chart $chart |
||
| 654 | * @param Chart\Series $series |
||
| 655 | */ |
||
| 656 | 21 | private function writeSeriesStyle(Chart $chart, Chart\Series $series) |
|
| 657 | { |
||
| 658 | 21 | $chartType = $chart->getPlotArea()->getType(); |
|
| 659 | |||
| 660 | // style:style |
||
| 661 | 21 | $this->xmlContent->startElement('style:style'); |
|
| 662 | 21 | $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries); |
|
| 663 | 21 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 664 | // style:chart-properties |
||
| 665 | 21 | $this->xmlContent->startElement('style:chart-properties'); |
|
| 666 | 21 | if ($series->hasShowValue()) { |
|
| 667 | 21 | if ($series->hasShowPercentage()) { |
|
| 668 | $this->xmlContent->writeAttribute('chart:data-label-number', 'value-and-percentage'); |
||
| 669 | } else { |
||
| 670 | 21 | $this->xmlContent->writeAttribute('chart:data-label-number', 'value'); |
|
| 671 | } |
||
| 672 | } else { |
||
| 673 | if ($series->hasShowPercentage()) { |
||
| 674 | $this->xmlContent->writeAttribute('chart:data-label-number', 'percentage'); |
||
| 675 | } |
||
| 676 | } |
||
| 677 | 21 | if ($series->hasShowCategoryName()) { |
|
| 678 | $this->xmlContent->writeAttribute('chart:data-label-text', 'true'); |
||
| 679 | } |
||
| 680 | 21 | $this->xmlContent->writeAttribute('chart:label-position', 'center'); |
|
| 681 | 21 | if ($chartType instanceof AbstractTypePie) { |
|
| 682 | 4 | $this->xmlContent->writeAttribute('chart:pie-offset', $chartType->getExplosion()); |
|
| 683 | } |
||
| 684 | 21 | if ($chartType instanceof Line || $chartType instanceof Scatter) { |
|
| 685 | 9 | $oMarker = $series->getMarker(); |
|
| 686 | /** |
||
| 687 | * @link : http://www.datypic.com/sc/odf/a-chart_symbol-type.html |
||
| 688 | */ |
||
| 689 | 9 | $this->xmlContent->writeAttributeIf($oMarker->getSymbol() == Chart\Marker::SYMBOL_NONE, 'chart:symbol-type', 'none'); |
|
| 690 | /** |
||
| 691 | * @link : http://www.datypic.com/sc/odf/a-chart_symbol-name.html |
||
| 692 | */ |
||
| 693 | 9 | $this->xmlContent->writeAttributeIf($oMarker->getSymbol() != Chart\Marker::SYMBOL_NONE, 'chart:symbol-type', 'named-symbol'); |
|
| 694 | 9 | if ($oMarker->getSymbol() != Chart\Marker::SYMBOL_NONE) { |
|
| 695 | 2 | switch ($oMarker->getSymbol()) { |
|
| 696 | 2 | case Chart\Marker::SYMBOL_DASH: |
|
| 697 | $symbolName = 'horizontal-bar'; |
||
| 698 | break; |
||
| 699 | 2 | case Chart\Marker::SYMBOL_DOT: |
|
| 700 | $symbolName = 'circle'; |
||
| 701 | break; |
||
| 702 | 2 | case Chart\Marker::SYMBOL_TRIANGLE: |
|
| 703 | $symbolName = 'arrow-up'; |
||
| 704 | break; |
||
| 705 | default: |
||
| 706 | 2 | $symbolName = $oMarker->getSymbol(); |
|
| 707 | 2 | break; |
|
| 708 | } |
||
| 709 | 2 | $this->xmlContent->writeAttribute('chart:symbol-name', $symbolName); |
|
| 710 | 2 | $symbolSize = number_format(CommonDrawing::pointsToCentimeters($oMarker->getSize()), 2, '.', ''); |
|
| 711 | 2 | $this->xmlContent->writeAttribute('chart:symbol-width', $symbolSize.'cm'); |
|
| 712 | 2 | $this->xmlContent->writeAttribute('chart:symbol-height', $symbolSize.'cm'); |
|
| 713 | } |
||
| 714 | } |
||
| 715 | // > style:chart-properties |
||
| 716 | 21 | $this->xmlContent->endElement(); |
|
| 717 | // style:graphic-properties |
||
| 718 | 21 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 719 | 21 | if ($chartType instanceof Line || $chartType instanceof Scatter) { |
|
| 720 | 9 | $outlineWidth = ''; |
|
| 721 | 9 | $outlineColor = ''; |
|
| 722 | |||
| 723 | 9 | $oOutline = $series->getOutline(); |
|
| 724 | 9 | if ($oOutline instanceof Outline) { |
|
| 725 | $outlineWidth = $oOutline->getWidth(); |
||
| 726 | if (!empty($outlineWidth)) { |
||
| 727 | $outlineWidth = number_format(CommonDrawing::pointsToCentimeters($outlineWidth), 3, '.', ''); |
||
| 728 | } |
||
| 729 | $outlineColor = $oOutline->getFill()->getStartColor()->getRGB(); |
||
| 730 | } |
||
| 731 | 9 | if (empty($outlineWidth)) { |
|
| 732 | 9 | $outlineWidth = '0.079'; |
|
| 733 | } |
||
| 734 | 9 | if (empty($outlineColor)) { |
|
| 735 | 9 | $outlineColor = '4a7ebb'; |
|
| 736 | } |
||
| 737 | 9 | $this->xmlContent->writeAttribute('svg:stroke-width', $outlineWidth.'cm'); |
|
| 738 | 9 | $this->xmlContent->writeAttribute('svg:stroke-color', '#'.$outlineColor); |
|
| 739 | } else { |
||
| 740 | 12 | $this->xmlContent->writeAttribute('draw:stroke', 'none'); |
|
| 741 | 12 | if (!($chartType instanceof Area)) { |
|
| 742 | 11 | $this->xmlContent->writeAttribute('draw:fill', $series->getFill()->getFillType()); |
|
| 743 | } |
||
| 744 | } |
||
| 745 | 21 | $this->xmlContent->writeAttribute('draw:fill-color', '#'.$series->getFill()->getStartColor()->getRGB()); |
|
| 746 | // > style:graphic-properties |
||
| 747 | 21 | $this->xmlContent->endElement(); |
|
| 748 | // style:text-properties |
||
| 749 | 21 | $this->xmlContent->startElement('style:text-properties'); |
|
| 750 | 21 | $this->xmlContent->writeAttribute('fo:color', '#'.$series->getFont()->getColor()->getRGB()); |
|
| 751 | 21 | $this->xmlContent->writeAttribute('fo:font-family', $series->getFont()->getName()); |
|
| 752 | 21 | $this->xmlContent->writeAttribute('fo:font-size', $series->getFont()->getSize().'pt'); |
|
| 753 | // > style:text-properties |
||
| 754 | 21 | $this->xmlContent->endElement(); |
|
| 755 | |||
| 756 | // > style:style |
||
| 757 | 21 | $this->xmlContent->endElement(); |
|
| 758 | |||
| 759 | 21 | foreach ($series->getDataPointFills() as $idx => $oFill) { |
|
| 760 | // style:style |
||
| 761 | 6 | $this->xmlContent->startElement('style:style'); |
|
| 762 | 6 | $this->xmlContent->writeAttribute('style:name', 'styleSeries'.$this->numSeries.'_'.$idx); |
|
| 763 | 6 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 764 | // style:graphic-properties |
||
| 765 | 6 | $this->xmlContent->startElement('style:graphic-properties'); |
|
| 766 | 6 | $this->xmlContent->writeAttribute('draw:fill', $oFill->getFillType()); |
|
| 767 | 6 | $this->xmlContent->writeAttribute('draw:fill-color', '#'.$oFill->getStartColor()->getRGB()); |
|
| 768 | // > style:graphic-properties |
||
| 769 | 6 | $this->xmlContent->endElement(); |
|
| 770 | // > style:style |
||
| 771 | 6 | $this->xmlContent->endElement(); |
|
| 772 | } |
||
| 773 | 21 | } |
|
| 774 | |||
| 775 | /** |
||
| 776 | */ |
||
| 777 | 23 | private function writeTable() |
|
| 864 | |||
| 865 | /** |
||
| 866 | * @param Title $oTitle |
||
| 867 | */ |
||
| 868 | 23 | private function writeTitle(Title $oTitle) |
|
| 869 | { |
||
| 870 | 23 | if (!$oTitle->isVisible()) { |
|
| 871 | return; |
||
| 872 | } |
||
| 873 | // chart:title |
||
| 874 | 23 | $this->xmlContent->startElement('chart:title'); |
|
| 875 | 23 | $this->xmlContent->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetX()), 3) . 'cm'); |
|
| 876 | 23 | $this->xmlContent->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($oTitle->getOffsetY()), 3) . 'cm'); |
|
| 877 | 23 | $this->xmlContent->writeAttribute('chart:style-name', 'styleTitle'); |
|
| 878 | // > text:p |
||
| 879 | 23 | $this->xmlContent->startElement('text:p'); |
|
| 880 | 23 | $this->xmlContent->text($oTitle->getText()); |
|
| 881 | // > text:p |
||
| 882 | 23 | $this->xmlContent->endElement(); |
|
| 883 | // > chart:title |
||
| 884 | 23 | $this->xmlContent->endElement(); |
|
| 885 | 23 | } |
|
| 886 | |||
| 887 | /** |
||
| 888 | * @param Title $oTitle |
||
| 889 | */ |
||
| 890 | 23 | private function writeTitleStyle(Title $oTitle) |
|
| 891 | { |
||
| 892 | 23 | if (!$oTitle->isVisible()) { |
|
| 893 | return; |
||
| 894 | } |
||
| 895 | // style:style |
||
| 896 | 23 | $this->xmlContent->startElement('style:style'); |
|
| 897 | 23 | $this->xmlContent->writeAttribute('style:name', 'styleTitle'); |
|
| 898 | 23 | $this->xmlContent->writeAttribute('style:family', 'chart'); |
|
| 899 | // style:text-properties |
||
| 900 | 23 | $this->xmlContent->startElement('style:text-properties'); |
|
| 901 | 23 | $this->xmlContent->writeAttribute('fo:color', '#'.$oTitle->getFont()->getColor()->getRGB()); |
|
| 902 | 23 | $this->xmlContent->writeAttribute('fo:font-family', $oTitle->getFont()->getName()); |
|
| 903 | 23 | $this->xmlContent->writeAttribute('fo:font-size', $oTitle->getFont()->getSize().'pt'); |
|
| 904 | 23 | $this->xmlContent->writeAttribute('fo:font-style', $oTitle->getFont()->isItalic() ? 'italic' : 'normal'); |
|
| 905 | // > style:text-properties |
||
| 906 | 23 | $this->xmlContent->endElement(); |
|
| 907 | // > style:style |
||
| 908 | 23 | $this->xmlContent->endElement(); |
|
| 909 | 23 | } |
|
| 910 | |||
| 911 | 23 | private function writeWall() |
|
| 919 | |||
| 920 | /** |
||
| 921 | * @param Chart $chart |
||
| 922 | */ |
||
| 923 | 23 | private function writeWallStyle(Chart $chart) |
|
| 948 | } |
||
| 949 |