chart.php 6.5 KB

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