Skip to content

Commit

Permalink
Merge pull request #65 from halfpastfouram/dev
Browse files Browse the repository at this point in the history
Release v1.2.2
  • Loading branch information
halfpastfouram authored Jan 11, 2020
2 parents 58175c6 + ffeccd0 commit 546d17f
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
6 changes: 5 additions & 1 deletion src/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/ChartOwned.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class ChartOwned
*
* @package Halfpastfour\PHPChartJS
*/
trait ChartOwned
Expand All @@ -20,7 +21,7 @@ trait ChartOwned
*/
public function setOwner(ChartInterface $chart)
{
$this->owner = $chart;
$this->owner = $chart;

return $this;
}
Expand Down
29 changes: 29 additions & 0 deletions src/DataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class DataSet implements ChartOwnedInterface, ArraySerializableInterface, JsonSe
*/
protected $hoverBorderWidth;

/**
* @var bool|null
*/
protected $hidden;

/**
* @return string
*/
Expand Down Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/Delegate/JsonSerializable.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function jsonSerialize()
return $value;
}, $this->getArrayCopy());
}

/**
* @return array
*/
abstract public function getArrayCopy();
}
41 changes: 41 additions & 0 deletions test/example/hidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

require_once '../../vendor/autoload.php';

use Halfpastfour\PHPChartJS\Chart\Bar;

$bar = new Bar();
$bar->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);

?>
<!doctype html>
<html lang="en">
<head>
<title>Bar</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
</head>
<body>
<?php
// Render the chart
echo $bar->render();
?>
</body>
</html>
25 changes: 17 additions & 8 deletions test/example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,25 @@ <h2>Pie graphs</h2>
</div>

<div class="row">
<div class="col-sm-4">
<div class="col-sm-12">
<h2>Other graphs</h2>
</div>
<div class="col-sm-4">
<div class="list-group list-group-flush">
<a href="bubble.php" class="list-group-item list-group-item-action">Bubble graph</a>
<a href="disableAspectRatio.php" class="list-group-item list-group-item-action">
Disable maintaining aspect ratio
</a>
<a href="polarArea.php" class="list-group-item list-group-item-action">Polar area graph</a>
<a href="radar.php" class="list-group-item list-group-item-action">Radar graph</a>
<a href="scatter.php" class="list-group-item list-group-item-action">Scatter graph</a>
<a href="onClick.php" class="list-group-item list-group-item-action">onClick</a>
</div>
</div>

<div class="col-sm-4">
<div class="list-group list-group-flush">
<a href="bubble.php" class="list-group-item list-group-item-action">Bubble graph</a> <a
href="disableAspectRatio.php" class="list-group-item list-group-item-action">Disable maintaining
aspect ratio</a> <a
href="polarArea.php" class="list-group-item list-group-item-action">Polar area graph</a> <a
href="radar.php" class="list-group-item list-group-item-action">Radar graph</a> <a
href="scatter.php" class="list-group-item list-group-item-action">Scatter graph</a> <a
href="onClick.php" class="list-group-item list-group-item-action">onClick</a>
<a href="hidden.php" class="list-group-item list-group-item-action">Hidden dataset</a>
</div>
</div>
</div>
Expand Down
41 changes: 41 additions & 0 deletions test/unit/ChartOwnedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Test;

use Halfpastfour\PHPChartJS\Chart\Bar;
use Halfpastfour\PHPChartJS\ChartOwnedInterface;
use Halfpastfour\PHPChartJS\Options;
use PHPUnit_Framework_TestCase;

/**
* Class ChartOwnedTest
*
* @package Test
*/
class ChartOwnedTest extends PHPUnit_Framework_TestCase
{
/**
*
*/
public function testSetOwner()
{
$options = new Options();
$this->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);
}
}
16 changes: 10 additions & 6 deletions test/unit/ChartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

namespace Test;

use DOMDocument;
use Halfpastfour\PHPChartJS\Chart;
use Halfpastfour\PHPChartJS\Chart\Bar;
use Halfpastfour\PHPChartJS\DataSet;
use Halfpastfour\PHPChartJS\DataSet\BarDataSet;
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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -212,7 +216,7 @@ public function testGetDataSet()
public function testRenderCanvas()
{
$chartHtml = "<div>" . $this->chart->render() . "</div>";
$htmlDoc = new \DOMDocument();
$htmlDoc = new DOMDocument();
$htmlDoc->loadXML($chartHtml);
$canvas = $htmlDoc->getElementsByTagName('canvas')->item(0);
$result = $canvas->getAttribute('id');
Expand All @@ -227,7 +231,7 @@ public function testRenderHeight()
$expected = '500';
$this->chart->setHeight($expected);
$chartHtml = "<div>" . $this->chart->render(true) . "</div>";
$htmlDoc = new \DOMDocument();
$htmlDoc = new DOMDocument();
$htmlDoc->loadXML($chartHtml);
$canvas = $htmlDoc->getElementsByTagName('canvas')->item(0);
$result = $canvas->getAttribute('height');
Expand All @@ -242,7 +246,7 @@ public function testRenderWidth()
$expected = '500';
$this->chart->setWidth($expected);
$chartHtml = "<div>" . $this->chart->render(true) . "</div>";
$htmlDoc = new \DOMDocument();
$htmlDoc = new DOMDocument();
$htmlDoc->loadXML($chartHtml);
$canvas = $htmlDoc->getElementsByTagName('canvas')->item(0);
$result = $canvas->getAttribute('width');
Expand All @@ -255,7 +259,7 @@ public function testRenderWidth()
public function testRenderScript()
{
$chartHtml = "<div>" . $this->chart->render(true) . "</div>";
$htmlDoc = new \DOMDocument();
$htmlDoc = new DOMDocument();
$htmlDoc->loadXML($chartHtml);
$script = $htmlDoc->getElementsByTagName('script')->item(0);
self::assertNotEmpty($script->nodeValue);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/DataSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 546d17f

Please sign in to comment.