From 28f86599678fab92ec6c975e3485b71b4fcb6ec9 Mon Sep 17 00:00:00 2001 From: halfpastfouram Date: Fri, 10 Jan 2020 01:33:12 +0100 Subject: [PATCH 1/5] Bumped up version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 020db09..1da65cc 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "readme": "README.md", "homepage": "http://halfpastfouram.github.io/PHPChartJS", "type": "package", - "version": "v1.2.1", + "version": "v1.3.0-dev", "license": "AGPL-3.0-or-later", "authors": [ { From bdab4c9b577ea17fd59fd6bffa72b7c7e3d73b03 Mon Sep 17 00:00:00 2001 From: halfpastfouram Date: Sat, 11 Jan 2020 21:46:39 +0100 Subject: [PATCH 2/5] Fixes #64: Add method to hide dataSet by default. --- src/DataSet.php | 29 +++++++++++++++++++++++++++ test/example/hidden.php | 41 +++++++++++++++++++++++++++++++++++++++ test/example/index.html | 25 ++++++++++++++++-------- test/unit/DataSetTest.php | 16 +++++++++++++++ 4 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 test/example/hidden.php diff --git a/src/DataSet.php b/src/DataSet.php index 88ac32f..ed08658 100644 --- a/src/DataSet.php +++ b/src/DataSet.php @@ -77,6 +77,11 @@ class DataSet implements ChartOwnedInterface, ArraySerializableInterface, JsonSe */ protected $hoverBorderWidth; + /** + * @var bool|null + */ + protected $hidden; + /** * @return string */ @@ -350,4 +355,28 @@ public function setHoverBorderWidth($hoverBorderWidth) return $this; } + + /** + * @return bool|null + */ + public function isHidden() + { + if (is_null($this->hidden)) { + $this->hidden = false; + } + + return $this->hidden; + } + + /** + * @param bool|null $hidden + * + * @return $this + */ + public function setHidden($hidden) + { + $this->hidden = is_null($hidden) ? null : boolval($hidden); + + return $this; + } } diff --git a/test/example/hidden.php b/test/example/hidden.php new file mode 100644 index 0000000..2985642 --- /dev/null +++ b/test/example/hidden.php @@ -0,0 +1,41 @@ +setId('myChart'); + +// Set labels +$bar->labels()->exchangeArray(["M", "T", "W", "T", "F", "S", "S"]); + +// Add Datasets +$apples = $bar->createDataSet(); + +$apples->setLabel("apples") + ->setBackgroundColor("rgba( 0, 150, 0, .5 )") + ->data()->exchangeArray([12, 19, 3, 17, 28, 24, 7]); +$bar->addDataSet($apples); + +$oranges = $bar->createDataSet(); +$oranges->setLabel("oranges") + ->setBackgroundColor('rgba( 255, 153, 0, .5 )') + ->data()->exchangeArray([30, 29, 5, 5, 20, 3, 10]); +$oranges->setHidden(true); +$bar->addDataSet($oranges); + +?> + + + + Bar + + + +render(); +?> + + diff --git a/test/example/index.html b/test/example/index.html index 8875997..b028518 100644 --- a/test/example/index.html +++ b/test/example/index.html @@ -55,16 +55,25 @@

Pie graphs

- diff --git a/test/unit/DataSetTest.php b/test/unit/DataSetTest.php index c8daea5..d114eeb 100644 --- a/test/unit/DataSetTest.php +++ b/test/unit/DataSetTest.php @@ -277,4 +277,20 @@ public function testHoverBorderWidth() $dataSet->setHoverBorderWidth([1, 10, '5a', 0]); $this->assertEquals([1, 10, 5, 0], $dataSet->getHoverBorderWidth(), 'The correct value is returned'); } + + /** + * + */ + public function testVisibility() + { + $dataSet = new DataSet(); + $this->assertArrayNotHasKey('hidden', $dataSet->jsonSerialize(), 'Value should not be present'); + $this->assertFalse($dataSet->isHidden(), 'Default value should be false'); + $this->assertArrayHasKey('hidden', $dataSet->jsonSerialize(), 'Value should be present'); + $this->assertInstanceOf(DataSet::class, $dataSet->setHidden(true)); + $this->assertTrue($dataSet->isHidden(), 'Value should be true'); + $this->assertTrue($dataSet->jsonSerialize()['hidden'], 'Value should be true'); + $this->assertInstanceOf(DataSet::class, $dataSet->setHidden(null)); + $this->assertArrayNotHasKey('hidden', $dataSet->jsonSerialize(), 'Value should not be present'); + } } From 6fe21618cd9931a2ab3f64a1fe478240f80751c4 Mon Sep 17 00:00:00 2001 From: halfpastfouram Date: Sat, 11 Jan 2020 21:52:35 +0100 Subject: [PATCH 3/5] Fixes #62: Add abstract method in trait --- src/Delegate/JsonSerializable.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Delegate/JsonSerializable.php b/src/Delegate/JsonSerializable.php index 486ed77..3fb0b55 100644 --- a/src/Delegate/JsonSerializable.php +++ b/src/Delegate/JsonSerializable.php @@ -27,4 +27,9 @@ public function jsonSerialize() return $value; }, $this->getArrayCopy()); } + + /** + * @return array + */ + abstract public function getArrayCopy(); } From 12daebb67e2671817c855dee2a0f13fa57fad201 Mon Sep 17 00:00:00 2001 From: halfpastfouram Date: Sat, 11 Jan 2020 22:18:15 +0100 Subject: [PATCH 4/5] Fixes #14: Auto assign owner and add unit tests. --- src/Chart.php | 6 +++++- src/ChartOwned.php | 3 ++- test/unit/ChartOwnedTest.php | 41 ++++++++++++++++++++++++++++++++++++ test/unit/ChartTest.php | 15 +++++++------ 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 test/unit/ChartOwnedTest.php diff --git a/src/Chart.php b/src/Chart.php index caa4b3e..fcced96 100644 --- a/src/Chart.php +++ b/src/Chart.php @@ -229,8 +229,11 @@ public function render($pretty = false) public function createDataSet() { $datasetClass = static::MODEL['dataset']; + /** @var \Halfpastfour\PHPChartJS\DataSet $dataSet */ + $dataSet = new $datasetClass(); + $dataSet->setOwner($this); - return new $datasetClass(); + return $dataSet; } /** @@ -241,6 +244,7 @@ public function options() if (is_null($this->options)) { $optionsClass = static::MODEL['options']; $this->options = new $optionsClass($this); + $this->options->setOwner($this); } return $this->options; diff --git a/src/ChartOwned.php b/src/ChartOwned.php index 42091ff..7ce32b0 100644 --- a/src/ChartOwned.php +++ b/src/ChartOwned.php @@ -4,6 +4,7 @@ /** * Class ChartOwned + * * @package Halfpastfour\PHPChartJS */ trait ChartOwned @@ -20,7 +21,7 @@ trait ChartOwned */ public function setOwner(ChartInterface $chart) { - $this->owner = $chart; + $this->owner = $chart; return $this; } diff --git a/test/unit/ChartOwnedTest.php b/test/unit/ChartOwnedTest.php new file mode 100644 index 0000000..7d7eb02 --- /dev/null +++ b/test/unit/ChartOwnedTest.php @@ -0,0 +1,41 @@ +assertNull($options->owner(), 'Owner should be empty'); + + $this->assertSame($options, $options->setOwner($bar = new Bar())); + $this->assertSame($bar, $options->owner()); + } + + /** + * + */ + public function testOwnerFromChart() + { + $bar = new Bar(); + $this->assertInstanceOf(ChartOwnedInterface::class, $bar->options()); + $this->assertSame($bar, $bar->options()->owner()); + + $this->assertInstanceOf(ChartOwnedInterface::class, $dataSet = $bar->createDataSet()); + $this->assertSame($dataSet->owner(), $bar); + } +} diff --git a/test/unit/ChartTest.php b/test/unit/ChartTest.php index 66214b5..3573c3d 100644 --- a/test/unit/ChartTest.php +++ b/test/unit/ChartTest.php @@ -2,6 +2,7 @@ namespace Test; +use DOMDocument; use Halfpastfour\PHPChartJS\Chart; use Halfpastfour\PHPChartJS\Chart\Bar; use Halfpastfour\PHPChartJS\DataSet; @@ -9,12 +10,14 @@ use Halfpastfour\PHPChartJS\DataSetCollection; use Halfpastfour\PHPChartJS\LabelsCollection; use Halfpastfour\PHPChartJS\Options\BarOptions; +use PHPUnit_Framework_TestCase; /** * Class ChartTest + * * @package Halfpastfour\PHPChartJS */ -class ChartTest extends \PHPUnit_Framework_TestCase +class ChartTest extends PHPUnit_Framework_TestCase { /** * @var Chart @@ -180,7 +183,7 @@ public function testAddDataSet() { $expected = new DataSetCollection(); $dataSet1 = new DataSet(); - $expected->append($dataSet1->setOwner($this->chart)); + $expected->append($dataSet1); $dataSet2 = new DataSet(); $this->chart->addDataSet($dataSet2); self::assertEquals($dataSet1, $dataSet2); @@ -212,7 +215,7 @@ public function testGetDataSet() public function testRenderCanvas() { $chartHtml = "
" . $this->chart->render() . "
"; - $htmlDoc = new \DOMDocument(); + $htmlDoc = new DOMDocument(); $htmlDoc->loadXML($chartHtml); $canvas = $htmlDoc->getElementsByTagName('canvas')->item(0); $result = $canvas->getAttribute('id'); @@ -227,7 +230,7 @@ public function testRenderHeight() $expected = '500'; $this->chart->setHeight($expected); $chartHtml = "
" . $this->chart->render(true) . "
"; - $htmlDoc = new \DOMDocument(); + $htmlDoc = new DOMDocument(); $htmlDoc->loadXML($chartHtml); $canvas = $htmlDoc->getElementsByTagName('canvas')->item(0); $result = $canvas->getAttribute('height'); @@ -242,7 +245,7 @@ public function testRenderWidth() $expected = '500'; $this->chart->setWidth($expected); $chartHtml = "
" . $this->chart->render(true) . "
"; - $htmlDoc = new \DOMDocument(); + $htmlDoc = new DOMDocument(); $htmlDoc->loadXML($chartHtml); $canvas = $htmlDoc->getElementsByTagName('canvas')->item(0); $result = $canvas->getAttribute('width'); @@ -255,7 +258,7 @@ public function testRenderWidth() public function testRenderScript() { $chartHtml = "
" . $this->chart->render(true) . "
"; - $htmlDoc = new \DOMDocument(); + $htmlDoc = new DOMDocument(); $htmlDoc->loadXML($chartHtml); $script = $htmlDoc->getElementsByTagName('script')->item(0); self::assertNotEmpty($script->nodeValue); From ffeccd01ca9a8a959359d19282c25c40029c4e13 Mon Sep 17 00:00:00 2001 From: halfpastfouram Date: Sat, 11 Jan 2020 22:30:54 +0100 Subject: [PATCH 5/5] Fixes #14: Update ChartTest --- test/unit/ChartTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/ChartTest.php b/test/unit/ChartTest.php index 3573c3d..6eeb7e0 100644 --- a/test/unit/ChartTest.php +++ b/test/unit/ChartTest.php @@ -184,6 +184,7 @@ public function testAddDataSet() $expected = new DataSetCollection(); $dataSet1 = new DataSet(); $expected->append($dataSet1); + $dataSet1->setOwner($this->chart); $dataSet2 = new DataSet(); $this->chart->addDataSet($dataSet2); self::assertEquals($dataSet1, $dataSet2);