chart.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. class Chart {
  3. private $id = "";
  4. private $title = "Untitled";
  5. private $type = "bar";
  6. private $datasets = [];
  7. private $labels = [];
  8. private $printLabels = null;
  9. private $xAxes = [];
  10. private $yAxes = [];
  11. private $datalabelSuffixes = [];
  12. private $datalabelFormatterCallback = false;
  13. private $printSuffixes = null;
  14. public $beforeHtml = null;
  15. public $afterHtml = null;
  16. public $print = false;
  17. public $printPercentages = false;
  18. public function __construct() {
  19. $this->id = bin2hex(random_bytes(8));
  20. }
  21. public function addXAxis($options = []) {
  22. $type = $options["type"] ?? "linear";
  23. $position = $options["position"] ?? "bottom";
  24. $id = $options["id"] ?? "x-axis";
  25. $grid_lines = $options["grid_lines"] ?? true;
  26. $tick_config = $options["tick_config"] ?? null;
  27. $scale_label_config = $options["scale_label_config"] ?? null;
  28. $axis = [
  29. "type" => $type,
  30. "display" => true,
  31. "position" => $position,
  32. "id" => $id,
  33. "gridLines" => [
  34. "drawOnChartArea" => $grid_lines
  35. ],
  36. ];
  37. if($tick_config) {
  38. $axis["ticks"] = $tick_config;
  39. }
  40. if($scale_label_config) {
  41. $axis["scaleLabel"] = $scale_label_config;
  42. }
  43. $this->xAxes[] = $axis;
  44. }
  45. public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) {
  46. $axis = [
  47. "type" => $type,
  48. "display" => true,
  49. "position" => $position,
  50. "id" => $id,
  51. "gridLines" => [
  52. "drawOnChartArea" => $grid_lines
  53. ],
  54. ];
  55. if($tick_config) {
  56. $axis["ticks"] = $tick_config;
  57. }
  58. $this->yAxes[] = $axis;
  59. }
  60. public function getID() {
  61. return $this->id;
  62. }
  63. public function getTitle() {
  64. return $this->title;
  65. }
  66. public function setTitle($title) {
  67. $this->title = $title;
  68. }
  69. public function setType($type) {
  70. $this->type = $type;
  71. }
  72. public function addDataset($label, $data, $options = []) {
  73. $background_colours = $options["background_colours"] ?? false;
  74. $border_colours = $options["border_colours"] ?? false;
  75. $border_width = $options["border_width"] ?? false;
  76. $y_axis_id = $options["y_axis_id"] ?? false;
  77. $type = $options["type"] ?? false;
  78. $datalabels = $options["datalabels"] ?? false;
  79. $dataset = [
  80. "label" => $label,
  81. "data" => $data,
  82. ];
  83. if($background_colours) $dataset["backgroundColor"] = $background_colours;
  84. if($border_colours) $dataset["borderColor"] = $border_colours;
  85. if($border_width) $dataset["borderWidth"] = (int) $border_width;
  86. if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
  87. if($type) $dataset["type"] = $type;
  88. if($datalabels) $dataset["datalabels"] = $datalabels;
  89. $this->datasets[] = (object) $dataset;
  90. }
  91. public function getDatasets() {
  92. return $this->datasets;
  93. }
  94. public function getLabels() {
  95. return $this->labels;
  96. }
  97. public function setLabels($labels) {
  98. $this->labels = $labels;
  99. }
  100. public function getPrintLabels() {
  101. return $this->printLabels;
  102. }
  103. public function setPrintLabels($labels) {
  104. $this->printLabels = $labels;
  105. }
  106. public function getDatalabelSuffixes() {
  107. return $this->datalabelSuffixes;
  108. }
  109. public function setDatalabelSuffixes(... $suffixes) {
  110. $this->datalabelSuffixes = $suffixes;
  111. }
  112. public function setDatalabelFormatterCallback($callback) {
  113. $this->datalabelFormatterCallback = $callback;
  114. }
  115. public function getPrintSuffixes() {
  116. return $this->printSuffixes;
  117. }
  118. public function setPrintSuffixes(... $suffixes) {
  119. $this->printSuffixes = $suffixes;
  120. }
  121. public function getJSON() {
  122. $object = (object) [
  123. "type" => $this->type,
  124. "data" => (object) [
  125. "labels" => $this->labels,
  126. "datasets" => $this->datasets
  127. ],
  128. "options" => (object) [
  129. "title" => (object) [
  130. "display" => true,
  131. "fontSize" => 32,
  132. "fontFamily" => "Roboto",
  133. "fontStyle" => "300",
  134. "text" => $this->title
  135. ],
  136. "legend" => (object) [
  137. "position" => "top",
  138. ],
  139. "plugins" => (object) [
  140. "datalabels" => [
  141. "color" => "white",
  142. ],
  143. ],
  144. ],
  145. ];
  146. if($this->xAxes || $this->yAxes) {
  147. $object->options->scales = new stdClass;
  148. if($this->xAxes) {
  149. $object->options->scales->xAxes = $this->xAxes;
  150. }
  151. if($this->yAxes) {
  152. $object->options->scales->yAxes = $this->yAxes;
  153. }
  154. }
  155. if($this->datalabelSuffixes) {
  156. $object->options->datalabelSuffixes = $this->datalabelSuffixes;
  157. }
  158. if($this->datalabelFormatterCallback) {
  159. $object->datalabelFormatterCallback = $this->datalabelFormatterCallback;
  160. }
  161. return json_encode($object);
  162. }
  163. public function printDataTable() {
  164. ?>
  165. <table class="mt-3 w-100 table table-sm small table-bordered">
  166. <thead>
  167. <tr>
  168. <th></th>
  169. <?php
  170. $labels = $this->printLabels ?? $this->labels;
  171. foreach($labels as $label) {
  172. ?>
  173. <th><?=htmlspecialchars($label)?></th>
  174. <?php
  175. }
  176. ?>
  177. </tr>
  178. </thead>
  179. <tbody>
  180. <?php
  181. foreach($this->datasets as $index => $dataset) {
  182. $suffix = "";
  183. $suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
  184. if($suffixes) {
  185. $suffix = $suffixes[$index];
  186. }
  187. $data = $dataset->data;
  188. ?>
  189. <tr>
  190. <td><?=htmlspecialchars($dataset->label)?></td>
  191. <?php
  192. foreach($data as $value) {
  193. ?>
  194. <td class="text-right"><?=htmlspecialchars($value)?><?=htmlspecialchars($suffix)?></td>
  195. <?php
  196. }
  197. ?>
  198. </tr>
  199. <?php
  200. if($this->printPercentages) {
  201. $total = array_sum($data);
  202. $percentages = [];
  203. foreach($data as $value) {
  204. $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
  205. }
  206. ?>
  207. <tr>
  208. <td>Percentage</td>
  209. <?php
  210. foreach($percentages as $string) {
  211. ?>
  212. <td class="text-right"><?=$string?></td>
  213. <?php
  214. }
  215. ?>
  216. </tr>
  217. <?php
  218. }
  219. }
  220. ?>
  221. </tbody>
  222. </table>
  223. <?php
  224. }
  225. public function setBeforeHtml($html) {
  226. $this->beforeHtml = $html;
  227. }
  228. public function setAfterHtml($html) {
  229. $this->afterHtml = $html;
  230. }
  231. }
  232. ?>