chart.php 4.7 KB

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