chart.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. class Chart {
  3. private $id = "";
  4. private $title = "Untitled";
  5. private $type = "bar";
  6. private $datasets = [];
  7. private $labels = [];
  8. private $xAxes = [];
  9. private $yAxes = [];
  10. private $datalabelSuffixes = [];
  11. public $print = false;
  12. public function __construct() {
  13. $this->id = bin2hex(random_bytes(8));
  14. }
  15. public function addXAxis($options = []) {
  16. $type = $options["type"] ?? "linear";
  17. $position = $options["position"] ?? "bottom";
  18. $id = $options["id"] ?? "x-axis";
  19. $grid_lines = $options["grid_lines"] ?? true;
  20. $tick_config = $options["tick_config"] ?? null;
  21. $scale_label_config = $options["scale_label_config"] ?? null;
  22. $axis = [
  23. "type" => $type,
  24. "display" => true,
  25. "position" => $position,
  26. "id" => $id,
  27. "gridLines" => [
  28. "drawOnChartArea" => $grid_lines
  29. ],
  30. ];
  31. if($tick_config) {
  32. $axis["ticks"] = $tick_config;
  33. }
  34. if($scale_label_config) {
  35. $axis["scaleLabel"] = $scale_label_config;
  36. }
  37. $this->xAxes[] = $axis;
  38. }
  39. public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) {
  40. $axis = [
  41. "type" => $type,
  42. "display" => true,
  43. "position" => $position,
  44. "id" => $id,
  45. "gridLines" => [
  46. "drawOnChartArea" => $grid_lines
  47. ],
  48. ];
  49. if($tick_config) {
  50. $axis["ticks"] = $tick_config;
  51. }
  52. $this->yAxes[] = $axis;
  53. }
  54. public function getID() {
  55. return $this->id;
  56. }
  57. public function getTitle() {
  58. return $this->title;
  59. }
  60. public function setTitle($title) {
  61. $this->title = $title;
  62. }
  63. public function setType($type) {
  64. $this->type = $type;
  65. }
  66. public function addDataset($label, $data, $options = []) {
  67. $background_colours = $options["background_colours"] ?? false;
  68. $border_colours = $options["border_colours"] ?? false;
  69. $border_width = $options["border_width"] ?? false;
  70. $y_axis_id = $options["y_axis_id"] ?? false;
  71. $type = $options["type"] ?? false;
  72. $datalabels = $options["datalabels"] ?? false;
  73. $dataset = [
  74. "label" => $label,
  75. "data" => $data,
  76. ];
  77. if($background_colours) $dataset["backgroundColor"] = $background_colours;
  78. if($border_colours) $dataset["borderColor"] = $border_colours;
  79. if($border_width) $dataset["borderWidth"] = (int) $border_width;
  80. if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
  81. if($type) $dataset["type"] = $type;
  82. if($datalabels) $dataset["datalabels"] = $datalabels;
  83. $this->datasets[] = (object) $dataset;
  84. }
  85. public function setLabels($labels) {
  86. $this->labels = $labels;
  87. }
  88. public function setDatalabelSuffixes(... $suffixes) {
  89. $this->datalabelSuffixes = $suffixes;
  90. }
  91. public function getJSON() {
  92. $object = (object) [
  93. "type" => $this->type,
  94. "data" => (object) [
  95. "labels" => $this->labels,
  96. "datasets" => $this->datasets
  97. ],
  98. "options" => (object) [
  99. "title" => (object) [
  100. "display" => true,
  101. "fontSize" => 32,
  102. "fontFamily" => "Roboto",
  103. "fontStyle" => "300",
  104. "text" => $this->title
  105. ],
  106. "legend" => (object) [
  107. "position" => "top",
  108. ],
  109. "plugins" => (object) [
  110. "datalabels" => [
  111. "color" => "white",
  112. ],
  113. ],
  114. ],
  115. ];
  116. if($this->xAxes || $this->yAxes) {
  117. $object->options->scales = new stdClass;
  118. if($this->xAxes) {
  119. $object->options->scales->xAxes = $this->xAxes;
  120. }
  121. if($this->yAxes) {
  122. $object->options->scales->yAxes = $this->yAxes;
  123. }
  124. }
  125. if($this->datalabelSuffixes) {
  126. $object->options->datalabelSuffixes = $this->datalabelSuffixes;
  127. }
  128. return json_encode($object);
  129. }
  130. public function printDataTable() {
  131. ?>
  132. <table class="mt-3 w-100 table table-condensed table-bordered">
  133. <thead>
  134. <tr>
  135. <th></th>
  136. <?php
  137. foreach($this->labels as $label) {
  138. ?>
  139. <th><?=htmlspecialchars($label)?></th>
  140. <?php
  141. }
  142. ?>
  143. </tr>
  144. </thead>
  145. <tbody>
  146. <?php
  147. foreach($this->datasets as $index => $dataset) {
  148. $suffix = "";
  149. if($this->datalabelSuffixes) {
  150. $suffix = $this->datalabelSuffixes[$index];
  151. }
  152. ?>
  153. <tr>
  154. <td><?=htmlspecialchars($dataset->label)?></td>
  155. <?php
  156. foreach($dataset->data as $value) {
  157. ?>
  158. <td class="text-right"><?=htmlspecialchars($value)?><?=htmlspecialchars($suffix)?></td>
  159. <?php
  160. }
  161. ?>
  162. </tr>
  163. <?php
  164. }
  165. ?>
  166. </tbody>
  167. </table>
  168. <?php
  169. }
  170. }
  171. ?>