chart.php 7.4 KB

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