diff --git a/README.md b/README.md index 7d674e3..6c61dbe 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Multiple datasets can be displayed side by side at the same time. Legends can also be displayed. - + Transparent background is also supported. @@ -35,8 +35,8 @@ Transparent background is also supported. - [5.1. Basic Usage](#51-basic-usage) - [5.2. Adjusting the Display By Methods](#52-adjusting-the-display-by-methods) - [5.3. Multiple Data Set](#53-multiple-data-set) - - [5.4. Changing Props By Neon File](#54-changing-props-by-neon-file) - - [5.5. Changing Props By Array](#55-changing-props-by-array) + - [5.4. Adjusting the Display By Neon File](#54-adjusting-the-display-by-neon-file) + - [5.5. Adjusting the Display By Array](#55-adjusting-the-display-by-array) - [5.6. Transparent Background](#56-transparent-background) - [6. Examples](#6-examples) - [7. LICENSE](#7-license) @@ -58,8 +58,8 @@ composer require macocci7/php-boxplot - [5.1. Basic Usage](#51-basic-usage) - [5.2. Adjusting the Display By Methods](#52-adjusting-the-display-by-methods) - [5.3. Multiple Data Set](#53-multiple-data-set) -- [5.4. Changing Props By Neon File](#54-changing-props-by-neon-file) -- [5.5. Changing Props By Array](#55-changing-props-by-array) +- [5.4. Adjusting the Display By Neon File](#54-adjusting-the-display-by-neon-file) +- [5.5. Adjusting the Display By Array](#55-adjusting-the-display-by-array) - [5.6. Transparent Background](#56-transparent-background) ### 5.1. Basic Usage @@ -135,16 +135,20 @@ And Setting Caption can be done as follows: ->limit(0, 100) ->gridHeightPitch(20) ->gridVerticalOn() + ->meanOn() + ->jitterOn() + ->legendOn() ->labels([ '#1', '#2', '#3', '#4', '#5', ]) ->labelX('Achievement Test') ->labelY('Score') ->caption('Achievement Test Results in 2023') - ->create('img/AdjustPlotArea.png'); + ->legends(['Donald Biden']) + ->create('img/AdjustDisplayByMethods.png'); ``` - Result: - + - Details: - Set Limits: `limit(int|float $lower, int|float $upper)` @@ -157,24 +161,27 @@ And Setting Caption can be done as follows: - Set Label for Y Axis: `labelY(string $labelY)` - Set Caption: `caption(string $caption)` - Available Methods: + + Note: `$colorCode` must be in `#RGB` or `#RRGGBB` format. + | Method | Detail | | :--- | :--- | |resize(int $width, int $height)|Resizes Canvas Size| - |bgcolor(string $bgcolor)|Sets Canvas Background Color| - |fontColor(string $color)|Sets Font Color| - |axisColor(string $color)|Sets Axis Color| - |gridColor(string $color)|Sets Grid Color| - |boxBorder(int $width, string $color)|Sets Box Border Color| - |whisker(int $width, string $color)|Sets Whisker Width and Color| + |bgcolor(string $colorCode)|Sets Canvas Background Color| + |fontColor(string $colorCode)|Sets Font Color| + |axisColor(string $colorCode)|Sets Axis Color| + |gridColor(string $colorCode)|Sets Grid Color| + |boxBorder(int $width, string $colorCode)|Sets Box Border Color| + |whisker(int $width, string $colorCode)|Sets Whisker Width and Color| |gridHeightPitch(int\|float $pitch)|Sets Grid Pitch| |boxWidth(int $width)|Sets Box Width| - |boxBackground(array $colors)|Sets Box Background Color| + |boxBackground(array $colorCodes)|Sets Box Background Color Set| |labels(array $labels)|Sets Labels for Each Box| |labelX(string $label)|Sets Label for X Axis| |labelY(string $label)|Sets Label for Y Axis| |caption(string $caption)|Sets Caption| |legends(array $legends)|Sets Legend Labels| - |legendBgcolor(string $color)|Sets Legends Area Background Color| + |legendBgcolor(string $colorCode)|Sets Legends Area Background Color| |gridVerticalOn()|Sets Visibility of Vertical Grid On| |gridVerticalOff()|Sets Visibility of Vertical Grid Off| |outlierOn()|Sets Outlier Detection On| @@ -185,7 +192,6 @@ And Setting Caption can be done as follows: |meanOff()|Sets Plotting Mean Off| |legendOn()|Sets Displaying Legends On| |legendOff()|Sets Displaying Legends Off| - - Note: Color Code must be in `#RGB` or `#RRGGBB` format. ### 5.3. Multiple Data Set @@ -251,14 +257,13 @@ Multiple datasets can be displayed side by side at the same time. - `legends(array $legends)` can overwrite legends. - `labels(array $labels)` can overwrite labels. -### 5.4. Changing Props By Neon File +### 5.4. Adjusting the Display By Neon File -You can change properties by using Neon file. +You can adjust the display by using Neon file. First, prepare a Neon file like this: - -- Neon: [examples/ChangePropsByNeon.neon](examples/ChangePropsByNeon.neon) +- Neon: [examples/AdjustDisplayByNeon.neon](examples/AdjustDisplayByNeon.neon) ```neon # Configuration for BoxplotExample.php @@ -338,7 +343,7 @@ First, prepare a Neon file like this: #labels: [ '1st', '2nd', '3rd', '4th', '5th' ] labelX: 'Achievement Test' labelY: 'Score' - caption: 'Changing Properties By Neon File' + caption: 'Adjusting the Display By Neon File' legend: true legendBackgroundColor: '#666666' @@ -360,7 +365,7 @@ First, prepare a Neon file like this: Second, Code PHP like this: -- PHP: [examples/ChangePropsByNeon.php](examples/ChangePropsByNeon.php) +- PHP: [examples/AdjustDisplayByNeon.php](examples/AdjustDisplayByNeon.php) ```php config('ChangePropsByNeon.neon') - ->create('img/ChangePropsByNeon.png'); + $bp->config('AdjustDisplayByNeon.neon') + ->create('img/AdjustDisplayByNeon.png'); ``` Then, run the PHP Code. -- Result: [examples/img/ChangPropsByNeon.png](examples/img/ChangePropsByNeon.png) +- Result: [examples/img/AdjustDisplyaByNeon.png](examples/img/AdjustDisplayByNeon.png) - + -### 5.5. Changing Props By Array +### 5.5. Adjusting the Display By Array -You can change properties by using Array. +You can Adjust the Display by using Array. -- PHP: [examples/ChangePropsByArray.php](examples/ChangePropsByArray.php) +- PHP: [examples/AdjustDisplayByArray.php](examples/AdjustDisplayByArray.php) ```php true, 'labelX' => 'Achievement Test', 'labelY' => 'Score', - 'caption' => 'Changing Properties By Array', + 'caption' => 'Adjusting the Display By Array', 'legend' => true, 'legendBackgroundColor' => '#666666', 'legendWidth' => 100, @@ -439,12 +444,12 @@ You can change properties by using Array. $bp = new Boxplot(); $bp->config($conf) - ->create('img/ChangePropsByArray.png'); + ->create('img/AdjustDisplayByArray.png'); ``` -- Result: [examples/img/ChangePropsByArray.png](examples/img/ChangePropsByArray.png) +- Result: [examples/img/AdjustDisplayByArray.png](examples/img/AdjustDisplayByArray.png) - + ### 5.6. Transparent Background @@ -487,15 +492,7 @@ You can set transparent background like this. - Result: [examples/img/TransparentBackground.png](examples/img/TransparentBackground.png) - - - + ## Examples @@ -503,23 +500,23 @@ You can set transparent background like this. -- [AdjustDisplay.php](examples/AdjustDisplay.php) >> results in: +- [AdjustDisplayByMethods.php](examples/AdjustDisplayByMethods.php) >> results in: - + -- [ChangePropsByNeon.php](examples/ChangePropsByNeon.php) >> results in: +- [AdjustDisplayByNeon.php](examples/AdjustDisplayByNeon.php) >> results in: - + -- [ChangePropsByArray.php](examples/ChangePropsByArray.php) +- [AdjustDisplayByArray.php](examples/AdjustDisplayByArray.php) >> results in: - + -- [MultipleDataSet.php](examples/MultipleDataSet.php) +- [MultipleDataSet.php](examples/MultipleDataSet.php) >> results in: -- [TransparentBackground.php](examples/TransparentBackground.php) +- [TransparentBackground.php](examples/TransparentBackground.php) >> results in: diff --git a/composer.lock b/composer.lock index 035f836..b6b2314 100644 --- a/composer.lock +++ b/composer.lock @@ -445,16 +445,16 @@ "packages-dev": [ { "name": "composer/pcre", - "version": "3.1.1", + "version": "3.1.2", "source": { "type": "git", "url": "https://github.com/composer/pcre.git", - "reference": "00104306927c7a0919b4ced2aaa6782c1e61a3c9" + "reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/pcre/zipball/00104306927c7a0919b4ced2aaa6782c1e61a3c9", - "reference": "00104306927c7a0919b4ced2aaa6782c1e61a3c9", + "url": "https://api.github.com/repos/composer/pcre/zipball/4775f35b2d70865807c89d32c8e7385b86eb0ace", + "reference": "4775f35b2d70865807c89d32c8e7385b86eb0ace", "shasum": "" }, "require": { @@ -496,7 +496,7 @@ ], "support": { "issues": "https://github.com/composer/pcre/issues", - "source": "https://github.com/composer/pcre/tree/3.1.1" + "source": "https://github.com/composer/pcre/tree/3.1.2" }, "funding": [ { @@ -512,7 +512,7 @@ "type": "tidelift" } ], - "time": "2023-10-11T07:11:09+00:00" + "time": "2024-03-07T15:38:35+00:00" }, { "name": "composer/xdebug-handler", @@ -1151,16 +1151,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.59", + "version": "1.10.60", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "e607609388d3a6d418a50a49f7940e8086798281" + "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e607609388d3a6d418a50a49f7940e8086798281", - "reference": "e607609388d3a6d418a50a49f7940e8086798281", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/95dcea7d6c628a3f2f56d091d8a0219485a86bbe", + "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe", "shasum": "" }, "require": { @@ -1209,7 +1209,7 @@ "type": "tidelift" } ], - "time": "2024-02-20T13:59:13+00:00" + "time": "2024-03-07T13:30:19+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/examples/ChangePropsByArray.php b/examples/AdjustDisplayByArray.php similarity index 94% rename from examples/ChangePropsByArray.php rename to examples/AdjustDisplayByArray.php index 0b05469..29e5fed 100644 --- a/examples/ChangePropsByArray.php +++ b/examples/AdjustDisplayByArray.php @@ -41,7 +41,7 @@ 'mean' => true, 'labelX' => 'Achievement Test', 'labelY' => 'Score', - 'caption' => 'Changing Properties By Array', + 'caption' => 'Adjusting the Display By Array', 'legend' => true, 'legendBackgroundColor' => '#666666', 'legendWidth' => 100, @@ -50,4 +50,4 @@ $bp = new Boxplot(); $bp->config($conf) - ->create('img/ChangePropsByArray.png'); + ->create('img/AdjustDisplayByArray.png'); diff --git a/examples/AdjustDisplay.php b/examples/AdjustDisplayByMethods.php similarity index 93% rename from examples/AdjustDisplay.php rename to examples/AdjustDisplayByMethods.php index 30d71b4..b0448e0 100644 --- a/examples/AdjustDisplay.php +++ b/examples/AdjustDisplayByMethods.php @@ -26,4 +26,4 @@ ->labelY('Score') ->caption('Achievement Test Results in 2023') ->legends(['Donald Biden']) - ->create('img/AdjustDisplay.png'); + ->create('img/AdjustDisplayByMethods.png'); diff --git a/examples/ChangePropsByNeon.neon b/examples/AdjustDisplayByNeon.neon similarity index 97% rename from examples/ChangePropsByNeon.neon rename to examples/AdjustDisplayByNeon.neon index b769d37..b7f8e3e 100644 --- a/examples/ChangePropsByNeon.neon +++ b/examples/AdjustDisplayByNeon.neon @@ -75,7 +75,7 @@ mean: true #labels: [ '1st', '2nd', '3rd', '4th', '5th' ] labelX: 'Achievement Test' labelY: 'Score' -caption: 'Changing Properties By Neon File' +caption: 'Adjusting the Display By Neon File' legend: true legendBackgroundColor: '#666666' diff --git a/examples/ChangePropsByNeon.php b/examples/AdjustDisplayByNeon.php similarity index 55% rename from examples/ChangePropsByNeon.php rename to examples/AdjustDisplayByNeon.php index 9d750ac..ac031c8 100644 --- a/examples/ChangePropsByNeon.php +++ b/examples/AdjustDisplayByNeon.php @@ -5,5 +5,5 @@ use Macocci7\PhpBoxplot\Boxplot; $bp = new Boxplot(); -$bp->config('ChangePropsByNeon.neon') - ->create('img/ChangePropsByNeon.png'); +$bp->config('AdjustDisplayByNeon.neon') + ->create('img/AdjustDisplayByNeon.png'); diff --git a/examples/img/AdjustDisplayByArray.png b/examples/img/AdjustDisplayByArray.png new file mode 100644 index 0000000..b50c078 Binary files /dev/null and b/examples/img/AdjustDisplayByArray.png differ diff --git a/examples/img/AdjustDisplay.png b/examples/img/AdjustDisplayByMethods.png similarity index 100% rename from examples/img/AdjustDisplay.png rename to examples/img/AdjustDisplayByMethods.png diff --git a/examples/img/AdjustDisplayByNeon.png b/examples/img/AdjustDisplayByNeon.png new file mode 100644 index 0000000..b26b898 Binary files /dev/null and b/examples/img/AdjustDisplayByNeon.png differ diff --git a/examples/img/ChangePropsByArray.png b/examples/img/ChangePropsByArray.png deleted file mode 100644 index 5047599..0000000 Binary files a/examples/img/ChangePropsByArray.png and /dev/null differ diff --git a/examples/img/ChangePropsByNeon.png b/examples/img/ChangePropsByNeon.png deleted file mode 100644 index f2710ac..0000000 Binary files a/examples/img/ChangePropsByNeon.png and /dev/null differ diff --git a/src/Traits/JudgeTrait.php b/src/Traits/JudgeTrait.php index 58e14a4..bba0dbe 100644 --- a/src/Traits/JudgeTrait.php +++ b/src/Traits/JudgeTrait.php @@ -84,6 +84,31 @@ public static function isColorCodeAll(array $colors) return true; } + /** + * judges if type of $input is valid or not + * @param mixed $input + * @param string $defs + * @return bool + */ + public static function isValidType(mixed $input, string $defs) + { + $r = false; + foreach (explode('|', $defs) as $def) { + $r = $r || match ($def) { + 'int' => is_int($input), + 'float' => is_float($input), + 'string' => is_string($input), + 'bool' => is_bool($input), + 'array' => is_array($input), + 'null' => is_null($input), + 'number' => self::isNumber($input), + 'colorCode' => self::isColorCode($input), + default => false, + }; + } + return $r; + } + /** * judges if the data is valid or not * @param mixed $data @@ -125,29 +150,4 @@ public static function isValidDataset(mixed $dataset) } return true; } - - /** - * judges if type of $input is valid or not - * @param mixed $input - * @param string $defs - * @return bool - */ - public static function isValidType(mixed $input, string $defs) - { - $r = false; - foreach (explode('|', $defs) as $def) { - $r = $r || match ($def) { - 'int' => is_int($input), - 'float' => is_float($input), - 'string' => is_string($input), - 'bool' => is_bool($input), - 'array' => is_array($input), - 'null' => is_null($input), - 'number' => self::isNumber($input), - 'colorCode' => self::isColorCode($input), - default => false, - }; - } - return $r; - } } diff --git a/tests/Helpers/ConfigTest.php b/tests/Helpers/ConfigTest.php index 638e125..5034e15 100644 --- a/tests/Helpers/ConfigTest.php +++ b/tests/Helpers/ConfigTest.php @@ -91,6 +91,14 @@ public static function provide_support_object_like_keys_correctly(): array ]; } + /** + * @dataProvider provide_support_object_like_keys_correctly + */ + public function get_can_support_object_like_keys_correctly(string $key, array|null $expect): void + { + $this->assertSame($expect, Config::get($key)); + } + public static function tearDownAfterClass(): void { $testConf = __DIR__ . '/../../conf/ConfigTest.neon'; diff --git a/tests/Traits/JudgeTraitTest.php b/tests/Traits/JudgeTraitTest.php index 3564838..6819e2b 100644 --- a/tests/Traits/JudgeTraitTest.php +++ b/tests/Traits/JudgeTraitTest.php @@ -79,6 +79,37 @@ public function test_isNumber_can_judge_correctly(mixed $item, bool $expect): vo $this->assertSame($expect, self::isNumber($item)); } + public static function provide_isNumbersAll_can_judge_correctly(): array + { + return [ + [ 'items' => null, 'expect' => false, ], + [ 'items' => true, 'expect' => false, ], + [ 'items' => false, 'expect' => false, ], + [ 'items' => '1', 'expect' => false, ], + [ 'items' => 1, 'expect' => false, ], + [ 'items' => 2.3, 'expect' => false, ], + [ 'items' => [], 'expect' => false, ], + [ 'items' => [null], 'expect' => false, ], + [ 'items' => [true], 'expect' => false, ], + [ 'items' => [false], 'expect' => false, ], + [ 'items' => ['1'], 'expect' => false, ], + [ 'items' => [1], 'expect' => true, ], + [ 'items' => [2.3], 'expect' => true, ], + [ 'items' => [[1]], 'expect' => false, ], + [ 'items' => [ 1, 2, ], 'expect' => true, ], + [ 'items' => [ 1, 2.3, ], 'expect' => true, ], + [ 'items' => [ 1, 2, '3', ], 'expect' => false, ], + ]; + } + + /** + * @dataProvider provide_isNumbersAll_can_judge_correctly + */ + public function test_isNumbersAll_can_judge_correctly(mixed $items, bool $expect): void + { + $this->assertSame($expect, self::isNumbersAll($items)); + } + public static function provide_isColorCode_can_judge_correctly(): array { return [ @@ -136,14 +167,6 @@ public function test_isColorCodeAll_can_judge_correctly(array $params, bool $exp $this->assertSame($expect, self::isColorCodeAll($params)); } - /** - * @dataProvider provide_support_object_like_keys_correctly - */ - public function get_can_support_object_like_keys_correctly(string $key, array|null $expect): void - { - $this->assertSame($expect, Config::get($key)); - } - public static function provide_isValidType_can_judge_correctly(): array { return [ @@ -232,4 +255,122 @@ public function test_isValidType_can_judge_correctly(mixed $input, string $def, { $this->assertSame($expect, self::isValidType($input, $def)); } + + public static function provide_isValidData_can_judge_correctly(): array + { + return [ + [ 'data' => null, 'expect' => false, ], + [ 'data' => true, 'expect' => false, ], + [ 'data' => false, 'expect' => false, ], + [ 'data' => 1, 'expect' => false, ], + [ 'data' => 2.3, 'expect' => false, ], + [ 'data' => '1', 'expect' => false, ], + [ 'data' => [], 'expect' => false, ], + [ 'data' => [null], 'expect' => false, ], + [ 'data' => [true], 'expect' => false, ], + [ 'data' => [false], 'expect' => false, ], + [ 'data' => [1], 'expect' => false, ], + [ 'data' => [2.3], 'expect' => false, ], + [ 'data' => ['1'], 'expect' => false, ], + [ 'data' => [[]], 'expect' => false, ], + [ 'data' => [[null]], 'expect' => false, ], + [ 'data' => [[true]], 'expect' => false, ], + [ 'data' => [[false]], 'expect' => false, ], + [ 'data' => [['1']], 'expect' => false, ], + [ 'data' => [[[1]]], 'expect' => false, ], + [ 'data' => [[1]], 'expect' => true, ], + [ 'data' => [[2.3]], 'expect' => true, ], + [ 'data' => [[ 1, 2, ]], 'expect' => true, ], + [ 'data' => [[ 1, 2.3, ]], 'expect' => true, ], + [ 'data' => [[ 1, 2.3, true ]], 'expect' => false, ], + ]; + } + + /** + * @dataProvider provide_isValidData_can_judge_correctly + */ + public function test_isValidData_can_judge_correctly(mixed $data, bool $expect): void + { + $this->assertSame($expect, self::isValidData($data)); + } + + public static function provide_isValidDataset_can_judge_correctly(): array + { + return [ + [ 'dataset' => null, 'expect' => false, ], + [ 'dataset' => true, 'expect' => false, ], + [ 'dataset' => false, 'expect' => false, ], + [ 'dataset' => 1, 'expect' => false, ], + [ 'dataset' => 2.3, 'expect' => false, ], + [ 'dataset' => '1', 'expect' => false, ], + [ 'dataset' => [], 'expect' => false, ], + [ 'dataset' => [null], 'expect' => false, ], + [ 'dataset' => [true], 'expect' => false, ], + [ 'dataset' => [false], 'expect' => false, ], + [ 'dataset' => [1], 'expect' => false, ], + [ 'dataset' => [2.3], 'expect' => false, ], + [ 'dataset' => ['1'], 'expect' => false, ], + [ 'dataset' => [[]], 'expect' => false, ], + [ 'dataset' => [[null]], 'expect' => false, ], + [ 'dataset' => [[true]], 'expect' => false, ], + [ 'dataset' => [[false]], 'expect' => false, ], + [ 'dataset' => [['1']], 'expect' => false, ], + [ 'dataset' => [[[1]]], 'expect' => true, ], + [ 'dataset' => [[1]], 'expect' => false, ], + [ 'dataset' => [[2.3]], 'expect' => false, ], + [ 'dataset' => [[ 1, 2, ]], 'expect' => false, ], + [ 'dataset' => [[ 1, 2.3, ]], 'expect' => false, ], + [ 'dataset' => [[ 1, 2.3, true ]], 'expect' => false, ], + [ + 'dataset' => [ + 'John' => [ + [ 1, 2.3, ], + ], + ], + 'expect' => true, + ], + [ + 'dataset' => [ + 'John' => [ + [ 1, 2.3, ], + [ 4, 5.6, ], + ], + 'Jake' => [ + [ 1, 2.3, ], + [ 4, 5.6, ], + [ 7, 8.9, ], + ], + ], + 'expect' => true, + ], + [ + 'dataset' => [ + 'John' => [ + [ 1, 2.3, ], + [ 4, 5.6, ], + ], + 'Jake' => [ + [ 1, 2.3, ], + [ 4, 5.6, ], + [ 7, 8.9, ], + ], + 'Hugo' => [ + [ 1, 2.3, ], + [ 4, 5.6, ], + [ 7, 8.9, ], + [ null ], + ], + ], + 'expect' => false, + ], + ]; + } + + /** + * @dataProvider provide_isValidDataset_can_judge_correctly + */ + public function test_isValidDataset_can_judge_correctly(mixed $dataset, bool $expect): void + { + $this->assertSame($expect, self::isValidDataset($dataset)); + } }