-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 93cdabf
Showing
12 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Èrik Campobadal Forés <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "chartisan/php", | ||
"description": "Chartisan's PHP backend", | ||
"license": "MIT", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Èrik Campobadal Forés", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": { | ||
"php": ">=7.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Chartisan\\PHP\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
php -S 127.0.0.1:9000 -t public/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
use Chartisan\PHP\Chartisan; | ||
|
||
/** | ||
* Outputing JSON encoded data and | ||
* the CORS headers. | ||
*/ | ||
header('Content-Type: application/json'); | ||
header('Access-Control-Allow-Origin: *'); | ||
|
||
/** | ||
* Main application entry point. | ||
*/ | ||
return fn (): string => Chartisan::build() | ||
->labels(['a', 'b', 'c']) | ||
->dataset('Sample 1', [1, 2 ,3]) | ||
->dataset('Sample 2', [3, 2 ,1]) | ||
->toJSON(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"type": "project", | ||
"repositories": [ | ||
{ | ||
"type": "path", | ||
"url": "../", | ||
"options": { | ||
"symlink": true | ||
} | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Chartisan\\PHP\\": "../src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* Register the auto loader. | ||
*/ | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
/** | ||
* Get the application response. | ||
*/ | ||
$response = require_once __DIR__ . '/../app.php'; | ||
|
||
/** | ||
* Respond to the request. | ||
*/ | ||
echo $response(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Chartisan\PHP; | ||
|
||
/** | ||
* Represents the chart information. | ||
*/ | ||
class ChartData | ||
{ | ||
/** | ||
* Stores the chart labels. | ||
* | ||
* @var string[] | ||
*/ | ||
public array $labels = []; | ||
|
||
/** | ||
* Stores the extra information of the chart if needed. | ||
* | ||
* @var array|null | ||
*/ | ||
public ?array $extra = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Chartisan\PHP; | ||
|
||
/** | ||
* Represents a chartisan chart instance. | ||
*/ | ||
class Chartisan | ||
{ | ||
/** | ||
* Stores the server data of the chart. | ||
* | ||
* @var ServerData | ||
*/ | ||
protected ServerData $serverData; | ||
|
||
/** | ||
* Creates a new instance of a chartisan chart. | ||
* | ||
* @param ServerData $serverData | ||
*/ | ||
public function __construct(ServerData $serverData) | ||
{ | ||
$this->serverData = $serverData; | ||
} | ||
|
||
/** | ||
* Creates a new instance of a chartisan chart. | ||
* | ||
* @return Chartisan | ||
*/ | ||
public static function build(): Chartisan | ||
{ | ||
return new Chartisan(new ServerData); | ||
} | ||
|
||
/** | ||
* Sets the chart labels. | ||
* | ||
* @param string[] $labels | ||
* @return Chartisan | ||
*/ | ||
public function labels(array $labels): Chartisan | ||
{ | ||
$this->serverData->chart->labels = $labels; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Adds extra information to the chart. | ||
* | ||
* @param array $value | ||
* @return Chartisan | ||
*/ | ||
public function extra(array $value): Chartisan | ||
{ | ||
$this->serverData->chart->extra = $value; | ||
return $this; | ||
} | ||
|
||
/** | ||
* AdvancedDataset appends a new dataset to the chart or modifies an existing one. | ||
* If the ID has already been used, the dataset will be replaced with this one. | ||
* | ||
* @param string $name | ||
* @param array $values | ||
* @param array|null $extra | ||
* @return Chartisan | ||
*/ | ||
public function advancedDataset(string $name, array $values, ?array $extra): Chartisan | ||
{ | ||
$dataset = $this->getDataset($name); | ||
if ($dataset) { | ||
$dataset->name = $name; | ||
$dataset->values = $values; | ||
$dataset->extra = $extra; | ||
} else { | ||
$this->serverData->datasets[] = new DatasetData($name, $values, $extra); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Dataset adds a new simple dataset to the chart. If more advanced control is | ||
* needed, consider using `AdvancedDataset` instead. | ||
* | ||
* @param string $name | ||
* @param array $values | ||
* @return Chartisan | ||
*/ | ||
public function dataset(string $name, array $values): Chartisan | ||
{ | ||
return $this->advancedDataset($name, $values, null); | ||
} | ||
|
||
/** | ||
* Returns the string representation JSON encoded. | ||
* | ||
* @return string | ||
*/ | ||
public function toJSON(): string | ||
{ | ||
return json_encode($this->toObject()); | ||
} | ||
|
||
/** | ||
* Transforms it to an object. | ||
* | ||
* @return ServerData | ||
*/ | ||
public function toObject(): ServerData | ||
{ | ||
return $this->serverData; | ||
} | ||
|
||
/** | ||
* Gets the dataset with the given name. | ||
* | ||
* @param string $name | ||
* @return ServerData|null | ||
*/ | ||
protected function getDataset(string $name): ?DatasetData | ||
{ | ||
foreach ($this->serverData->datasets as $dataset) { | ||
if ($dataset->name == $name) { | ||
return $dataset; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Chartisan\PHP; | ||
|
||
/** | ||
* Represents the dataset information. | ||
*/ | ||
class DatasetData | ||
{ | ||
/** | ||
* Stores the dataset name. | ||
* | ||
* @var string | ||
*/ | ||
public string $name; | ||
|
||
/** | ||
* Stores the dataset values. | ||
* | ||
* @var array[float] | ||
*/ | ||
public array $values; | ||
|
||
/** | ||
* Stores the dataset extra information if needed. | ||
* | ||
* @var ?array | ||
*/ | ||
public ?array $extra; | ||
|
||
/** | ||
* Creates a new instance of DatasetData. | ||
* | ||
* @param string $name | ||
* @param array $values | ||
* @param array|null $extra | ||
*/ | ||
public function __construct(string $name, array $values, ?array $extra) | ||
{ | ||
$this->name = $name; | ||
$this->values = $values; | ||
$this->extra = $extra; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Chartisan\PHP; | ||
|
||
/** | ||
* ServerData represents how the server is expected | ||
* to send the data to the chartisan client. | ||
*/ | ||
class ServerData | ||
{ | ||
/** | ||
* Stores the chart information. | ||
* | ||
* @var ChartData | ||
*/ | ||
public ChartData $chart; | ||
|
||
/** | ||
* Stores the datasets of the chart. | ||
* | ||
* @var DatasetData[] | ||
*/ | ||
public array $datasets = []; | ||
|
||
/** | ||
* Creates a new instance of a server data. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->chart = new ChartData; | ||
} | ||
} |