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": [
{
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/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/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();
}
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/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..6eeb7e0 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,8 @@ public function testAddDataSet()
{
$expected = new DataSetCollection();
$dataSet1 = new DataSet();
- $expected->append($dataSet1->setOwner($this->chart));
+ $expected->append($dataSet1);
+ $dataSet1->setOwner($this->chart);
$dataSet2 = new DataSet();
$this->chart->addDataSet($dataSet2);
self::assertEquals($dataSet1, $dataSet2);
@@ -212,7 +216,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 +231,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 +246,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 +259,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);
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');
+ }
}