chart.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. $line_tension = $options["line_tension"] ?? null;
  92. $dataset = [
  93. "label" => $label,
  94. "data" => $data,
  95. ];
  96. if($background_colours) $dataset["backgroundColor"] = $background_colours;
  97. if($border_colours) $dataset["borderColor"] = $border_colours;
  98. if($border_width) $dataset["borderWidth"] = (int) $border_width;
  99. if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
  100. if($type) $dataset["type"] = $type;
  101. if($datalabels) $dataset["datalabels"] = $datalabels;
  102. if(isset($line_tension)) $dataset["lineTension"] = $line_tension;
  103. $this->datasets[] = (object) $dataset;
  104. }
  105. public function getDatasets() {
  106. return $this->datasets;
  107. }
  108. public function getLabels() {
  109. return $this->labels;
  110. }
  111. public function setLabels($labels) {
  112. $this->labels = $labels;
  113. }
  114. public function getPrintLabels() {
  115. return $this->printLabels;
  116. }
  117. public function setPrintLabels($labels) {
  118. $this->printLabels = $labels;
  119. }
  120. public function getDatalabelSuffixes() {
  121. return $this->datalabelSuffixes;
  122. }
  123. public function setDatalabelSuffixes(... $suffixes) {
  124. $this->datalabelSuffixes = $suffixes;
  125. }
  126. public function setDatalabelFormatterCallback($callback) {
  127. $this->datalabelFormatterCallback = $callback;
  128. }
  129. public function getPrintSuffixes() {
  130. return $this->printSuffixes;
  131. }
  132. public function setPrintSuffixes(... $suffixes) {
  133. $this->printSuffixes = $suffixes;
  134. }
  135. public function getJSON() {
  136. $object = (object) [
  137. "type" => $this->type,
  138. "data" => (object) [
  139. "labels" => $this->labels,
  140. "datasets" => $this->datasets
  141. ],
  142. "options" => (object) [
  143. "title" => (object) [
  144. "display" => true,
  145. "fontSize" => 32,
  146. "fontFamily" => "Roboto",
  147. "fontStyle" => "300",
  148. "text" => $this->title
  149. ],
  150. "legend" => (object) [
  151. "position" => "top",
  152. ],
  153. "tooltips" => (object) [
  154. "mode" => $this->tooltipsMode,
  155. "intersect" => $this->tooltipsIntersect,
  156. ],
  157. "plugins" => (object) [
  158. "datalabels" => [
  159. "color" => "white",
  160. ],
  161. ],
  162. ],
  163. ];
  164. if($this->xAxes || $this->yAxes) {
  165. $object->options->scales = new stdClass;
  166. if($this->xAxes) {
  167. $object->options->scales->xAxes = $this->xAxes;
  168. }
  169. if($this->yAxes) {
  170. $object->options->scales->yAxes = $this->yAxes;
  171. }
  172. }
  173. if($this->datalabelSuffixes) {
  174. $object->options->datalabelSuffixes = $this->datalabelSuffixes;
  175. }
  176. if($this->datalabelFormatterCallback) {
  177. $object->datalabelFormatterCallback = $this->datalabelFormatterCallback;
  178. }
  179. return json_encode($object);
  180. }
  181. public function printDataTable() {
  182. ?>
  183. <table class="mt-3 w-100 table table-sm small table-bordered">
  184. <thead>
  185. <tr>
  186. <th></th>
  187. <?php
  188. $labels = $this->printLabels ?? $this->labels;
  189. foreach($labels as $label) {
  190. ?>
  191. <th><?=htmlspecialchars($label)?></th>
  192. <?php
  193. }
  194. ?>
  195. </tr>
  196. </thead>
  197. <tbody>
  198. <?php
  199. foreach($this->datasets as $index => $dataset) {
  200. $suffix = "";
  201. $suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
  202. if($suffixes) {
  203. $suffix = $suffixes[$index];
  204. }
  205. $data = $dataset->data;
  206. ?>
  207. <tr>
  208. <td><?=htmlspecialchars($dataset->label)?></td>
  209. <?php
  210. foreach($data as $value) {
  211. ?>
  212. <td class="text-right"><?=htmlspecialchars($value)?><?=htmlspecialchars($suffix)?></td>
  213. <?php
  214. }
  215. ?>
  216. </tr>
  217. <?php
  218. if($this->printPercentages) {
  219. $total = array_sum($data);
  220. $percentages = [];
  221. foreach($data as $value) {
  222. $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
  223. }
  224. ?>
  225. <tr>
  226. <td>Percentage</td>
  227. <?php
  228. foreach($percentages as $string) {
  229. ?>
  230. <td class="text-right"><?=$string?></td>
  231. <?php
  232. }
  233. ?>
  234. </tr>
  235. <?php
  236. }
  237. }
  238. ?>
  239. </tbody>
  240. </table>
  241. <?php
  242. }
  243. public function setBeforeHtml($html) {
  244. $this->beforeHtml = $html;
  245. }
  246. public function setAfterHtml($html) {
  247. $this->afterHtml = $html;
  248. }
  249. public function setTooltipsMode($mode) {
  250. $this->tooltipsMode = $mode;
  251. }
  252. public function setTooltipsIntersect($intersect) {
  253. $this->tooltipsIntersect = $intersect;
  254. }
  255. }
  256. ?>