chart.php 3.4 KB

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