From cdb0b8bf90c2bb1185688bbea73dc064273b9365 Mon Sep 17 00:00:00 2001 From: macocci7 Date: Sat, 20 Apr 2024 21:46:24 +0900 Subject: [PATCH] Refactor codes --- composer.json | 10 +- src/Analyzer.php | 3 +- src/Boxplot.php | 343 +-------------------------------- src/Plotter.php | 95 +++------ src/Traits/AttributeTrait.php | 102 ++++++++++ src/Traits/StyleTrait.php | 185 ++++++++++++++++++ src/Traits/VisibilityTrait.php | 112 +++++++++++ 7 files changed, 433 insertions(+), 417 deletions(-) create mode 100644 src/Traits/AttributeTrait.php create mode 100644 src/Traits/StyleTrait.php create mode 100644 src/Traits/VisibilityTrait.php diff --git a/composer.json b/composer.json index 8275661..3aa9c99 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,13 @@ { "name": "macocci7/php-boxplot", - "version": "1.2.0", + "version": "1.2.1", "description": "it's easy to use for creating boxplots.", "type": "library", "require": { "php": ">=8.1", - "intervention/image": "^3.5", - "macocci7/php-frequency-table": "1.3.0", - "fakerphp/faker": "^1.22", + "intervention/image": "^3.6", + "macocci7/php-frequency-table": "^1.3", + "fakerphp/faker": "^1.23", "nette/neon": "^3.4" }, "require-dev": { @@ -15,7 +15,7 @@ "phpunit/phpunit": "^10.5", "phpmd/phpmd": "^2.15", "phpstan/phpstan": "^1.10", - "php-parallel-lint/php-parallel-lint": "^1.3" + "php-parallel-lint/php-parallel-lint": "^1.4" }, "license": "MIT", "autoload": { diff --git a/src/Analyzer.php b/src/Analyzer.php index 0a1098b..6b9298d 100644 --- a/src/Analyzer.php +++ b/src/Analyzer.php @@ -4,7 +4,6 @@ use Macocci7\PhpFrequencyTable\FrequencyTable; use Macocci7\PhpBoxplot\Helpers\Config; -use Macocci7\PhpBoxplot\Traits\JudgeTrait; /** * class for analysis @@ -13,7 +12,7 @@ */ class Analyzer { - use JudgeTrait; + use Traits\JudgeTrait; public FrequencyTable $ft; /** diff --git a/src/Boxplot.php b/src/Boxplot.php index 4b55778..cb67907 100644 --- a/src/Boxplot.php +++ b/src/Boxplot.php @@ -4,23 +4,16 @@ use Macocci7\PhpBoxplot\Plotter; use Macocci7\PhpBoxplot\Helpers\Config; -use Macocci7\PhpBoxplot\Traits\JudgeTrait; use Nette\Neon\Neon; /** * class for managing boxplot * @author macocci7 * @license MIT - * @SuppressWarnings(PHPMD.TooManyMethods) - * @SuppressWarnings(PHPMD.TooManyPublicMethods) - * @SuppressWarnings(PHPMD.CamelCasePropertyName) */ class Boxplot extends Plotter { - use JudgeTrait; - - private int $CANVAS_WIDTH_LIMIT_LOWER; - private int $CANVAS_HEIGHT_LIMIT_LOWER; + use Traits\JudgeTrait; /** * constructor @@ -62,338 +55,4 @@ public function config(string|array $configResource) } return $this; } - - /** - * sets canvas background color - * @param string $bgcolor - * @return self - */ - public function bgcolor(string $bgcolor) - { - if (!$this->isColorCode($bgcolor)) { - throw new \Exception("specify rgb color code."); - } - $this->canvasBackgroundColor = $bgcolor; - return $this; - } - - /** - * sets font color - * @param string $color - * @return self - */ - public function fontColor(string $color) - { - if (!$this->isColorCode($color)) { - throw new \Exception("specify rgb color code."); - } - $this->fontColor = $color; - return $this; - } - - /** - * sets axis color - * @param string $color - * @return self - */ - public function axisColor(string $color) - { - if (!$this->isColorCode($color)) { - throw new \Exception("specify rgb color code."); - } - $this->axisColor = $color; - return $this; - } - - /** - * sets grid color - * @param string $color - * @return self - */ - public function gridColor(string $color) - { - if (!$this->isColorCode($color)) { - throw new \Exception("specify rgb color code."); - } - $this->gridColor = $color; - return $this; - } - - /** - * sets legend background color - * @param string $color - * @return self - */ - public function legendBgcolor(string $color) - { - if (!$this->isColorCode($color)) { - throw new \Exception("specify rgb color code."); - } - $this->legendBackgroundColor = $color; - return $this; - } - - /** - * sets attributes of border of boxes - * @param int $width - * @param string $color - * @return self - */ - public function boxBorder(int $width, string $color) - { - if ($width < 1) { - throw new \Exception("specify natural number as width."); - } - if (!$this->isColorCode($color)) { - throw new \Exception("specify rgb color code."); - } - $this->boxBorderWidth = $width; - $this->boxBorderColor = $color; - return $this; - } - - /** - * sets attributes of whisker - * @param int $width - * @param string $color - * @return self - */ - public function whisker(int $width, string $color) - { - if ($width < 1) { - throw new \Exception("specify natural number as width."); - } - if (!$this->isColorCode($color)) { - throw new \Exception("specify rgb color code."); - } - $this->whiskerWidth = $width; - $this->whiskerColor = $color; - return $this; - } - - /** - * sets height pitch of grid - * @param int|float $pitch - * @return self - */ - public function gridHeightPitch(int|float $pitch) - { - if ($pitch <= 0) { - throw new \Exception("specify positive number."); - } - $this->gridHeightPitch = $pitch; - return $this; - } - - /** - * resizes width and height - * @param int $width - * @param int $height - * @return self - * @thrown \Exception - */ - public function resize(int $width, int $height) - { - if ($width < $this->CANVAS_WIDTH_LIMIT_LOWER) { - throw new \Exception( - "width is below the lower limit " - . $this->CANVAS_WIDTH_LIMIT_LOWER - ); - } - if ($height < $this->CANVAS_HEIGHT_LIMIT_LOWER) { - throw new \Exception( - "height is below the lower limit " - . $this->CANVAS_HEIGHT_LIMIT_LOWER - ); - } - $this->canvasWidth = $width; - $this->canvasHeight = $height; - return $this; - } - - /** - * sets width of boxes - * @param int $width - * @return self - */ - public function boxWidth(int $width) - { - if ($width < $this->boxBorderWidth * 2 + 1) { - throw new \Exception("Box width must be greater than twice of box border width."); - } - $this->boxWidth = $width; - return $this; - } - - /** - * sets background colors of boxes - * @param string[] $colors - * @return self - */ - public function boxBackground(array $colors) - { - if (!$this->isColorCodeAll($colors)) { - throw new \Exception("only color codes are acceptable."); - } - $this->boxBackgroundColors = $colors; - return $this; - } - - /** - * sets labels - * @param array $labels - * @return self - */ - public function labels(array $labels) - { - $this->labels = []; - foreach ($labels as $label) { - $this->labels[] = (string) $label; - } - return $this; - } - - /** - * sets the label of X - * @param string $label - * @return self - */ - public function labelX(string $label) - { - $this->labelX = $label; - return $this; - } - - /** - * sets the label of Y - * @param string $label - * @return self - */ - public function labelY(string $label) - { - $this->labelY = $label; - return $this; - } - - /** - * sets the caption - * @param string $caption - * @return self - */ - public function caption(string $caption) - { - $this->caption = $caption; - return $this; - } - - /** - * sets legends - * @param string[] $legends - * @return self - */ - public function legends(array $legends) - { - $this->legends = $legends; - return $this; - } - - /** - * sets vertical grids on - * @return self - */ - public function gridVerticalOn() - { - $this->gridVertical = true; - return $this; - } - - /** - * sets vertical grids off - * @return self - */ - public function gridVerticalOff() - { - $this->gridVertical = false; - return $this; - } - - /** - * sets detecting outliers on - * @return self - */ - public function outlierOn() - { - $this->outlier = true; - return $this; - } - - /** - * sets detecting outliers off - * @return self - */ - public function outlierOff() - { - $this->outlier = false; - return $this; - } - - /** - * sets jitter plotting on - * @return self - */ - public function jitterOn() - { - $this->jitter = true; - return $this; - } - - /** - * sets jitter plotting off - * @return self - */ - public function jitterOff() - { - $this->jitter = false; - return $this; - } - - /** - * sets plotting means on - * @return self - */ - public function meanOn() - { - $this->mean = true; - return $this; - } - - /** - * sets plotting means off - * @return self - */ - public function meanOff() - { - $this->mean = false; - return $this; - } - - /** - * sets showing legends on - * @return self - */ - public function legendOn() - { - $this->legend = true; - return $this; - } - - /** - * sets showing legends off - * @return self - */ - public function legendOff() - { - $this->legend = false; - return $this; - } } diff --git a/src/Plotter.php b/src/Plotter.php index 575a319..d0bd11d 100644 --- a/src/Plotter.php +++ b/src/Plotter.php @@ -2,12 +2,12 @@ namespace Macocci7\PhpBoxplot; +use Intervention\Image\Geometry\Factories\CircleFactory; +use Intervention\Image\Geometry\Factories\LineFactory; +use Intervention\Image\Geometry\Factories\RectangleFactory; use Intervention\Image\ImageManager; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\Geometry\Factories\LineFactory; use Intervention\Image\Typography\FontFactory; -use Intervention\Image\Geometry\Factories\RectangleFactory; -use Intervention\Image\Geometry\Factories\CircleFactory; use Macocci7\PhpBoxplot\Analyzer; use Macocci7\PhpBoxplot\Helpers\Config; @@ -15,72 +15,31 @@ * class for analysis * @author macocci7 * @license MIT - * @SuppressWarnings(PHPMD.TooManyFields) - * @SuppressWarnings(PHPMD.TooManyPublicMethods) * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) - * @SuppressWarnings(PHPMD.ElseExpression) */ class Plotter extends Analyzer { + use Traits\StyleTrait; + use Traits\AttributeTrait; + use Traits\VisibilityTrait; + protected string $imageDriver; protected ImageManager $imageManager; protected ImageInterface $image; - protected int $canvasWidth; - protected int $canvasHeight; - protected string|null $canvasBackgroundColor; protected float $frameXRatio; protected float $frameYRatio; - protected string|null $axisColor; protected int $axisWidth; - protected string|null $gridColor; protected int $gridWidth; - protected int|null $gridHeightPitch; protected int|float $pixGridWidth; protected int $gridMax; protected int $gridMin; - protected bool $gridVertical; protected int $boxCount; - protected int $boxWidth; - /** - * @var string[] $boxBackgroundColors - */ - protected array $boxBackgroundColors; - protected string|null $boxBorderColor; - protected int $boxBorderWidth; protected int|float $pixHeightPitch; - protected string|null $whiskerColor; - protected int $whiskerWidth; - protected string $fontPath; - protected int|float $fontSize; - protected string|null $fontColor; protected int $baseX; protected int $baseY; - protected bool $outlier; - protected int $outlierDiameter; - protected string|null $outlierColor; - protected bool $jitter; - protected string|null $jitterColor; - protected int $jitterDiameter; - protected bool $mean; - protected string|null $meanColor; - /** - * @var string[] $labels - */ - protected array $labels; - protected string $labelX; - protected string $labelY; - protected string $caption; - protected bool $legend; protected int $legendCount; - protected string|null $legendBackgroundColor; - protected int $legendWidth; - protected int $legendFontSize; - /** - * @var string[] $colors - */ - protected array $colors; /** * constructor @@ -213,7 +172,7 @@ private function setProperties() } // Note: // - If $this->labels has values, those values takes precedence. - // - The values of $this->labels may be set by the function labels(). + // - The values of $this->labels may be set by the method labels(). if (empty($this->labels)) { $this->labels = array_keys($this->dataSet[array_keys($this->dataSet)[0]]); } @@ -224,7 +183,7 @@ private function setProperties() * plots axis * @return self */ - public function plotAxis() + private function plotAxis() { // Horizontal Axis $x1 = (int) $this->baseX; @@ -259,7 +218,7 @@ function (LineFactory $line) use ($x1, $y1, $x2, $y2) { * plots grids * @return self */ - public function plotGrids() + private function plotGrids() { $this->plotGridHorizontal(); $this->plotGridVertical(); @@ -270,7 +229,7 @@ public function plotGrids() * plots horizontal grids * @return self */ - public function plotGridHorizontal() + private function plotGridHorizontal() { for ($y = $this->gridMin; $y <= $this->gridMax; $y += $this->gridHeightPitch) { $x1 = (int) $this->baseX; @@ -293,7 +252,7 @@ function (LineFactory $line) use ($x1, $y1, $x2, $y2) { * plots vertical grid * @return self */ - public function plotGridVertical() + private function plotGridVertical() { if (!$this->gridVertical) { return $this; @@ -319,7 +278,7 @@ function (LineFactory $line) use ($x1, $y1, $x2, $y2) { * plots grid values * @return self */ - public function plotGridValues() + private function plotGridValues() { for ($y = $this->gridMin; $y <= $this->gridMax; $y += $this->gridHeightPitch) { $x1 = (int) ($this->baseX - $this->fontSize * 1.1); @@ -346,7 +305,7 @@ function (FontFactory $font) { * @param int $legend * @return self */ - public function plotBox(int $index, int $legend) + private function plotBox(int $index, int $legend) { $gridWidth = $this->pixGridWidth; $legends = $this->legendCount; @@ -373,7 +332,7 @@ function (RectangleFactory $rectangle) use ($legend, $x1, $y1, $x2, $y2) { * @param int $legend * @return self */ - public function plotMedian(int $index, int $legend) + private function plotMedian(int $index, int $legend) { $gridWidth = $this->pixGridWidth; $legends = $this->legendCount; @@ -399,7 +358,7 @@ function (LineFactory $line) use ($x1, $y1, $x2, $y2) { * @param int $legend * @return self */ - public function plotMean(int $index, int $legend) + private function plotMean(int $index, int $legend) { if (!$this->mean) { return $this; @@ -431,7 +390,7 @@ function (FontFactory $font) { * @param int $legend * @return self */ - public function plotWhiskerUpper(int $index, int $legend) + private function plotWhiskerUpper(int $index, int $legend) { // upper whisker $gridWidth = $this->pixGridWidth; @@ -477,7 +436,7 @@ function (LineFactory $line) use ($x1, $y1, $x2, $y2) { * @param int $legend * @return self */ - public function plotWhiskerLower(int $index, int $legend) + private function plotWhiskerLower(int $index, int $legend) { // lower whisker $gridWidth = $this->pixGridWidth; @@ -523,7 +482,7 @@ function (LineFactory $line) use ($x1, $y1, $x2, $y2) { * @param int $legend * @return self */ - public function plotWhisker(int $index, int $legend) + private function plotWhisker(int $index, int $legend) { $this->plotWhiskerUpper($index, $legend); $this->plotWhiskerLower($index, $legend); @@ -536,7 +495,7 @@ public function plotWhisker(int $index, int $legend) * @param int $legend * @return self */ - public function plotOutliers(int $index, int $legend) + private function plotOutliers(int $index, int $legend) { if (!$this->outlier) { return $this; @@ -566,7 +525,7 @@ function (CircleFactory $circle) { * @param int $legend * @return self */ - public function plotJitter(int $index, int $legend) + private function plotJitter(int $index, int $legend) { if (!$this->jitter) { return $this; @@ -602,7 +561,7 @@ function (CircleFactory $circle) { * plots labels * @return self */ - public function plotLabels() + private function plotLabels() { if (!is_array($this->labels)) { return $this; @@ -633,7 +592,7 @@ function (FontFactory $font) { * plots label of X * @return self */ - public function plotLabelX() + private function plotLabelX() { $x = (int) ($this->canvasWidth / 2); $y = (int) ($this->baseY + (1 - $this->frameYRatio) * $this->canvasHeight / 3); @@ -656,7 +615,7 @@ function (FontFactory $font) { * plots label of Y * @return self */ - public function plotLabelY() + private function plotLabelY() { $width = $this->canvasHeight; $height = (int) ($this->canvasWidth * (1 - $this->frameXRatio) / 3); @@ -684,7 +643,7 @@ function (FontFactory $font) { * plots caption * @return self */ - public function plotCaption() + private function plotCaption() { $x = (int) ($this->canvasWidth / 2); $y = (int) ($this->canvasHeight * (1 - $this->frameYRatio) / 3); @@ -707,7 +666,7 @@ function (FontFactory $font) { * plots a legend * @return self */ - public function plotLegend() + private function plotLegend() { if (!$this->legend) { return $this; @@ -770,7 +729,7 @@ function (FontFactory $font) { * @param int $legend * @return self */ - public function plot(int $index, int $legend) + private function plot(int $index, int $legend) { $this->plotBox($index, $legend); $this->plotMedian($index, $legend); diff --git a/src/Traits/AttributeTrait.php b/src/Traits/AttributeTrait.php new file mode 100644 index 0000000..d8c9a76 --- /dev/null +++ b/src/Traits/AttributeTrait.php @@ -0,0 +1,102 @@ +CANVAS_WIDTH_LIMIT_LOWER) { + throw new \Exception( + "width is below the lower limit " + . $this->CANVAS_WIDTH_LIMIT_LOWER + ); + } + if ($height < $this->CANVAS_HEIGHT_LIMIT_LOWER) { + throw new \Exception( + "height is below the lower limit " + . $this->CANVAS_HEIGHT_LIMIT_LOWER + ); + } + $this->canvasWidth = $width; + $this->canvasHeight = $height; + return $this; + } + + /** + * sets labels + * @param array $labels + * @return self + */ + public function labels(array $labels) + { + $this->labels = []; + foreach ($labels as $label) { + $this->labels[] = (string) $label; + } + return $this; + } + + /** + * sets the label of X + * @param string $label + * @return self + */ + public function labelX(string $label) + { + $this->labelX = $label; + return $this; + } + + /** + * sets the label of Y + * @param string $label + * @return self + */ + public function labelY(string $label) + { + $this->labelY = $label; + return $this; + } + + /** + * sets the caption + * @param string $caption + * @return self + */ + public function caption(string $caption) + { + $this->caption = $caption; + return $this; + } + + /** + * sets legends + * @param string[] $legends + * @return self + */ + public function legends(array $legends) + { + $this->legends = $legends; + return $this; + } +} diff --git a/src/Traits/StyleTrait.php b/src/Traits/StyleTrait.php new file mode 100644 index 0000000..45880e8 --- /dev/null +++ b/src/Traits/StyleTrait.php @@ -0,0 +1,185 @@ +isColorCode($bgcolor)) { + throw new \Exception("specify rgb color code."); + } + $this->canvasBackgroundColor = $bgcolor; + return $this; + } + + /** + * sets font color + * @param string $color + * @return self + */ + public function fontColor(string $color) + { + if (!$this->isColorCode($color)) { + throw new \Exception("specify rgb color code."); + } + $this->fontColor = $color; + return $this; + } + + /** + * sets axis color + * @param string $color + * @return self + */ + public function axisColor(string $color) + { + if (!$this->isColorCode($color)) { + throw new \Exception("specify rgb color code."); + } + $this->axisColor = $color; + return $this; + } + + /** + * sets grid color + * @param string $color + * @return self + */ + public function gridColor(string $color) + { + if (!$this->isColorCode($color)) { + throw new \Exception("specify rgb color code."); + } + $this->gridColor = $color; + return $this; + } + + /** + * sets legend background color + * @param string $color + * @return self + */ + public function legendBgcolor(string $color) + { + if (!$this->isColorCode($color)) { + throw new \Exception("specify rgb color code."); + } + $this->legendBackgroundColor = $color; + return $this; + } + + /** + * sets attributes of border of boxes + * @param int $width + * @param string $color + * @return self + */ + public function boxBorder(int $width, string $color) + { + if ($width < 1) { + throw new \Exception("specify natural number as width."); + } + if (!$this->isColorCode($color)) { + throw new \Exception("specify rgb color code."); + } + $this->boxBorderWidth = $width; + $this->boxBorderColor = $color; + return $this; + } + + /** + * sets attributes of whisker + * @param int $width + * @param string $color + * @return self + */ + public function whisker(int $width, string $color) + { + if ($width < 1) { + throw new \Exception("specify natural number as width."); + } + if (!$this->isColorCode($color)) { + throw new \Exception("specify rgb color code."); + } + $this->whiskerWidth = $width; + $this->whiskerColor = $color; + return $this; + } + + /** + * sets height pitch of grid + * @param int|float $pitch + * @return self + */ + public function gridHeightPitch(int|float $pitch) + { + if ($pitch <= 0) { + throw new \Exception("specify positive number."); + } + $this->gridHeightPitch = $pitch; + return $this; + } + + /** + * sets width of boxes + * @param int $width + * @return self + */ + public function boxWidth(int $width) + { + if ($width < $this->boxBorderWidth * 2 + 1) { + throw new \Exception("Box width must be greater than twice of box border width."); + } + $this->boxWidth = $width; + return $this; + } + + /** + * sets background colors of boxes + * @param string[] $colors + * @return self + */ + public function boxBackground(array $colors) + { + if (!$this->isColorCodeAll($colors)) { + throw new \Exception("only color codes are acceptable."); + } + $this->boxBackgroundColors = $colors; + return $this; + } +} diff --git a/src/Traits/VisibilityTrait.php b/src/Traits/VisibilityTrait.php new file mode 100644 index 0000000..7089a4b --- /dev/null +++ b/src/Traits/VisibilityTrait.php @@ -0,0 +1,112 @@ +gridVertical = true; + return $this; + } + + /** + * sets vertical grids off + * @return self + */ + public function gridVerticalOff() + { + $this->gridVertical = false; + return $this; + } + + /** + * sets detecting outliers on + * @return self + */ + public function outlierOn() + { + $this->outlier = true; + return $this; + } + + /** + * sets detecting outliers off + * @return self + */ + public function outlierOff() + { + $this->outlier = false; + return $this; + } + + /** + * sets jitter plotting on + * @return self + */ + public function jitterOn() + { + $this->jitter = true; + return $this; + } + + /** + * sets jitter plotting off + * @return self + */ + public function jitterOff() + { + $this->jitter = false; + return $this; + } + + /** + * sets plotting means on + * @return self + */ + public function meanOn() + { + $this->mean = true; + return $this; + } + + /** + * sets plotting means off + * @return self + */ + public function meanOff() + { + $this->mean = false; + return $this; + } + + /** + * sets showing legends on + * @return self + */ + public function legendOn() + { + $this->legend = true; + return $this; + } + + /** + * sets showing legends off + * @return self + */ + public function legendOff() + { + $this->legend = false; + return $this; + } +}