chart.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. class Chart {
  3. private $id = "";
  4. private $title = "Untitled";
  5. private $type = "bar";
  6. private $datasets = [];
  7. private $labels = [];
  8. private $xAxes = [];
  9. private $yAxes = [];
  10. private $datalabelSuffixes = [];
  11. private $datalabelFormatterCallback = false;
  12. public $print = false;
  13. public function __construct() {
  14. $this->id = bin2hex(random_bytes(8));
  15. }
  16. public function addXAxis($options = []) {
  17. $type = $options["type"] ?? "linear";
  18. $position = $options["position"] ?? "bottom";
  19. $id = $options["id"] ?? "x-axis";
  20. $grid_lines = $options["grid_lines"] ?? true;
  21. $tick_config = $options["tick_config"] ?? null;
  22. $scale_label_config = $options["scale_label_config"] ?? null;
  23. $axis = [
  24. "type" => $type,
  25. "display" => true,
  26. "position" => $position,
  27. "id" => $id,
  28. "gridLines" => [
  29. "drawOnChartArea" => $grid_lines
  30. ],
  31. ];
  32. if($tick_config) {
  33. $axis["ticks"] = $tick_config;
  34. }
  35. if($scale_label_config) {
  36. $axis["scaleLabel"] = $scale_label_config;
  37. }
  38. $this->xAxes[] = $axis;
  39. }
  40. public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) {
  41. $axis = [
  42. "type" => $type,
  43. "display" => true,
  44. "position" => $position,
  45. "id" => $id,
  46. "gridLines" => [
  47. "drawOnChartArea" => $grid_lines
  48. ],
  49. ];
  50. if($tick_config) {
  51. $axis["ticks"] = $tick_config;
  52. }
  53. $this->yAxes[] = $axis;
  54. }
  55. public function getID() {
  56. return $this->id;
  57. }
  58. public function getTitle() {
  59. return $this->title;
  60. }
  61. public function setTitle($title) {
  62. $this->title = $title;
  63. }
  64. public function setType($type) {
  65. $this->type = $type;
  66. }
  67. public function addDataset($label, $data, $options = []) {
  68. $background_colours = $options["background_colours"] ?? false;
  69. $border_colours = $options["border_colours"] ?? false;
  70. $border_width = $options["border_width"] ?? false;
  71. $y_axis_id = $options["y_axis_id"] ?? false;
  72. $type = $options["type"] ?? false;
  73. $datalabels = $options["datalabels"] ?? false;
  74. $dataset = [
  75. "label" => $label,
  76. "data" => $data,
  77. ];
  78. if($background_colours) $dataset["backgroundColor"] = $background_colours;
  79. if($border_colours) $dataset["borderColor"] = $border_colours;
  80. if($border_width) $dataset["borderWidth"] = (int) $border_width;
  81. if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
  82. if($type) $dataset["type"] = $type;
  83. if($datalabels) $dataset["datalabels"] = $datalabels;
  84. $this->datasets[] = (object) $dataset;
  85. }
  86. public function setLabels($labels) {
  87. $this->labels = $labels;
  88. }
  89. public function setDatalabelSuffixes(... $suffixes) {
  90. $this->datalabelSuffixes = $suffixes;
  91. }
  92. public function setDatalabelFormatterCallback($callback) {
  93. $this->datalabelFormatterCallback = $callback;
  94. }
  95. public function getJSON() {
  96. $object = (object) [
  97. "type" => $this->type,
  98. "data" => (object) [
  99. "labels" => $this->labels,
  100. "datasets" => $this->datasets
  101. ],
  102. "options" => (object) [
  103. "title" => (object) [
  104. "display" => true,
  105. "fontSize" => 32,
  106. "fontFamily" => "Roboto",
  107. "fontStyle" => "300",
  108. "text" => $this->title
  109. ],
  110. "legend" => (object) [
  111. "position" => "top",
  112. ],
  113. "plugins" => (object) [
  114. "datalabels" => [
  115. "color" => "white",
  116. ],
  117. ],
  118. ],
  119. ];
  120. if($this->xAxes || $this->yAxes) {
  121. $object->options->scales = new stdClass;
  122. if($this->xAxes) {
  123. $object->options->scales->xAxes = $this->xAxes;
  124. }
  125. if($this->yAxes) {
  126. $object->options->scales->yAxes = $this->yAxes;
  127. }
  128. }
  129. if($this->datalabelSuffixes) {
  130. $object->options->datalabelSuffixes = $this->datalabelSuffixes;
  131. }
  132. if($this->datalabelFormatterCallback) {
  133. $object->datalabelFormatterCallback = $this->datalabelFormatterCallback;
  134. }
  135. return json_encode($object);
  136. }
  137. public function printDataTable() {
  138. ?>
  139. <table class="mt-3 w-100 table-bordered">
  140. <thead>
  141. <tr>
  142. <th></th>
  143. <?php
  144. foreach($this->labels as $label) {
  145. ?>
  146. <th><?=htmlspecialchars($label)?></th>
  147. <?php
  148. }
  149. ?>
  150. </tr>
  151. </thead>
  152. <tbody>
  153. <?php
  154. foreach($this->datasets as $index => $dataset) {
  155. $suffix = "";
  156. if($this->datalabelSuffixes) {
  157. $suffix = $this->datalabelSuffixes[$index];
  158. }
  159. ?>
  160. <tr>
  161. <td><?=htmlspecialchars($dataset->label)?></td>
  162. <?php
  163. foreach($dataset->data as $value) {
  164. ?>
  165. <td class="text-right"><?=htmlspecialchars($value)?><?=htmlspecialchars($suffix)?></td>
  166. <?php
  167. }
  168. ?>
  169. </tr>
  170. <?php
  171. }
  172. ?>
  173. </tbody>
  174. </table>
  175. <?php
  176. }
  177. }
  178. ?>