| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- class Chart {
- private $id = "";
- private $title = "Untitled";
- private $type = "bar";
- private $datasets = [];
- private $labels = [];
- private $printLabels = null;
- private $tooltipsMode = "nearest";
- private $tooltipsIntersect = true;
- private $xAxes = [];
- private $yAxes = [];
- private $datalabelSuffixes = [];
- private $datalabelFormatterCallback = false;
- private $printSuffixes = null;
- public $beforeHtml = null;
- public $afterHtml = null;
- public $print = false;
- public $printPercentages = false;
- public function __construct() {
- $this->id = bin2hex(random_bytes(8));
- }
- public function addXAxis($options = []) {
- $type = $options["type"] ?? "linear";
- $position = $options["position"] ?? "bottom";
- $id = $options["id"] ?? "x-axis";
- $grid_lines = $options["grid_lines"] ?? true;
- $tick_config = $options["tick_config"] ?? null;
- $scale_label_config = $options["scale_label_config"] ?? null;
- $stacked = $options["stacked"] ?? false;
- $axis = [
- "type" => $type,
- "display" => true,
- "position" => $position,
- "id" => $id,
- "gridLines" => [
- "drawOnChartArea" => $grid_lines
- ],
- "stacked" => $stacked,
- ];
- if($tick_config) {
- $axis["ticks"] = $tick_config;
- }
- if($scale_label_config) {
- $axis["scaleLabel"] = $scale_label_config;
- }
- $this->xAxes[] = $axis;
- }
- public function addYAxis($options = []) {
- $type = $options["type"] ?? "linear";
- $position = $options["position"] ?? "left";
- $id = $options["id"] ?? "y-axis-0";
- $grid_lines = $options["grid_lines"] ?? true;
- $tick_config = $options["tick_config"] ?? null;
- $scale_label_config = $options["scale_label_config"] ?? null;
- $stacked = $options["stacked"] ?? false;
- $axis = [
- "type" => $type,
- "display" => true,
- "position" => $position,
- "id" => $id,
- "gridLines" => [
- "drawOnChartArea" => $grid_lines
- ],
- "stacked" => $stacked,
- ];
- if($tick_config) {
- $axis["ticks"] = $tick_config;
- }
- $this->yAxes[] = $axis;
- }
- 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;
- $y_axis_id = $options["y_axis_id"] ?? false;
- $type = $options["type"] ?? false;
- $datalabels = $options["datalabels"] ?? 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;
- if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
- if($type) $dataset["type"] = $type;
- if($datalabels) $dataset["datalabels"] = $datalabels;
- $this->datasets[] = (object) $dataset;
- }
- public function getDatasets() {
- return $this->datasets;
- }
- public function getLabels() {
- return $this->labels;
- }
- public function setLabels($labels) {
- $this->labels = $labels;
- }
- public function getPrintLabels() {
- return $this->printLabels;
- }
- public function setPrintLabels($labels) {
- $this->printLabels = $labels;
- }
- public function getDatalabelSuffixes() {
- return $this->datalabelSuffixes;
- }
- public function setDatalabelSuffixes(... $suffixes) {
- $this->datalabelSuffixes = $suffixes;
- }
- public function setDatalabelFormatterCallback($callback) {
- $this->datalabelFormatterCallback = $callback;
- }
- public function getPrintSuffixes() {
- return $this->printSuffixes;
- }
- public function setPrintSuffixes(... $suffixes) {
- $this->printSuffixes = $suffixes;
- }
- public function getJSON() {
- $object = (object) [
- "type" => $this->type,
- "data" => (object) [
- "labels" => $this->labels,
- "datasets" => $this->datasets
- ],
- "options" => (object) [
- "title" => (object) [
- "display" => true,
- "fontSize" => 32,
- "fontFamily" => "Roboto",
- "fontStyle" => "300",
- "text" => $this->title
- ],
- "legend" => (object) [
- "position" => "top",
- ],
- "tooltips" => (object) [
- "mode" => $this->tooltipsMode,
- "intersect" => $this->tooltipsIntersect,
- ],
- "plugins" => (object) [
- "datalabels" => [
- "color" => "white",
- ],
- ],
- ],
- ];
- if($this->xAxes || $this->yAxes) {
- $object->options->scales = new stdClass;
- if($this->xAxes) {
- $object->options->scales->xAxes = $this->xAxes;
- }
- if($this->yAxes) {
- $object->options->scales->yAxes = $this->yAxes;
- }
- }
- if($this->datalabelSuffixes) {
- $object->options->datalabelSuffixes = $this->datalabelSuffixes;
- }
- if($this->datalabelFormatterCallback) {
- $object->datalabelFormatterCallback = $this->datalabelFormatterCallback;
- }
- return json_encode($object);
- }
- public function printDataTable() {
- ?>
- <table class="mt-3 w-100 table table-sm small table-bordered">
- <thead>
- <tr>
- <th></th>
- <?php
- $labels = $this->printLabels ?? $this->labels;
- foreach($labels as $label) {
- ?>
- <th><?=htmlspecialchars($label)?></th>
- <?php
- }
- ?>
- </tr>
- </thead>
- <tbody>
- <?php
- foreach($this->datasets as $index => $dataset) {
- $suffix = "";
- $suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
- if($suffixes) {
- $suffix = $suffixes[$index];
- }
- $data = $dataset->data;
- ?>
- <tr>
- <td><?=htmlspecialchars($dataset->label)?></td>
- <?php
- foreach($data as $value) {
- ?>
- <td class="text-right"><?=htmlspecialchars($value)?><?=htmlspecialchars($suffix)?></td>
- <?php
- }
- ?>
- </tr>
- <?php
- if($this->printPercentages) {
- $total = array_sum($data);
- $percentages = [];
- foreach($data as $value) {
- $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
- }
- ?>
- <tr>
- <td>Percentage</td>
- <?php
- foreach($percentages as $string) {
- ?>
- <td class="text-right"><?=$string?></td>
- <?php
- }
- ?>
- </tr>
- <?php
- }
- }
- ?>
- </tbody>
- </table>
- <?php
- }
- public function setBeforeHtml($html) {
- $this->beforeHtml = $html;
- }
- public function setAfterHtml($html) {
- $this->afterHtml = $html;
- }
- public function setTooltipsMode($mode) {
- $this->tooltipsMode = $mode;
- }
- public function setTooltipsIntersect($intersect) {
- $this->tooltipsIntersect = $intersect;
- }
- }
- ?>
|