| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- class Chart {
- private $id = "";
- private $title = "Untitled";
- private $type = "bar";
- private $datasets = [];
- private $labels = [];
- public function __construct() {
- $this->id = bin2hex(random_bytes(8));
- }
- public function getID() {
- return $this->id;
- }
- public function getTitle() {
- return $this->title;
- }
- public function setTitle($title) {
- $this->title = $title;
- }
- public function setType($type) {
- $this->type = $type;
- }
- public function addDataset($label, $data, $options = []) {
- $background_colours = $options["background_colours"] ?? false;
- $border_colours = $options["border_colours"] ?? false;
- $border_width = $options["border_width"] ?? false;
- $dataset = [
- "label" => $label,
- "data" => $data,
- ];
- if($background_colours) $dataset["backgroundColor"] = $background_colours;
- if($border_colours) $dataset["borderColor"] = $border_colours;
- if($border_width) $dataset["borderWidth"] = (int) $border_width;
- $this->datasets[] = $dataset;
- }
- public function setLabels($labels) {
- $this->labels = $labels;
- }
- public function getJSON() {
- return json_encode([
- "type" => $this->type,
- "data" => [
- "labels" => $this->labels,
- "datasets" => $this->datasets
- ],
- "options" => [
- "title" => [
- "display" => true,
- "fontSize" => 32,
- "fontFamily" => "Roboto",
- "fontStyle" => "300",
- "text" => $this->title
- ],
- "legend" => [
- "position" => "top",
- ],
- ],
- ]);
- }
- }
- ?>
|