Paata
2 years ago
commit
fc08d903dc
10 changed files with 318 additions and 0 deletions
@ -0,0 +1,21 @@
|
||||
{ |
||||
"name": "at-lab/chartisan-port", |
||||
"description": "Chartisan's PHP backend", |
||||
"license": "MIT", |
||||
"type": "library", |
||||
"authors": [ |
||||
{ |
||||
"name": "Èrik Campobadal Forés", |
||||
"email": "soc@erik.cat" |
||||
} |
||||
], |
||||
"minimum-stability": "dev", |
||||
"require": { |
||||
"php": ">=7.4" |
||||
}, |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Chartisan\\PHP\\": "src/" |
||||
} |
||||
} |
||||
} |
@ -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(); |
@ -0,0 +1,17 @@
|
||||
{ |
||||
"type": "project", |
||||
"repositories": [ |
||||
{ |
||||
"type": "path", |
||||
"url": "../", |
||||
"options": { |
||||
"symlink": true |
||||
} |
||||
} |
||||
], |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Chartisan\\PHP\\": "../src" |
||||
} |
||||
} |
||||
} |
@ -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(); |
@ -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; |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
Loading…
Reference in new issue