chart.php 6.7 KB

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