chart.php 7.0 KB

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