1
0

chart.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 function __construct() {
  11. $this->id = bin2hex(random_bytes(8));
  12. }
  13. public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) {
  14. $axis = [
  15. "type" => $type,
  16. "display" => true,
  17. "position" => $position,
  18. "id" => $id,
  19. "gridLines" => [
  20. "drawOnChartArea" => $grid_lines
  21. ],
  22. ];
  23. if($tick_config) {
  24. $axis["ticks"] = $tick_config;
  25. }
  26. $this->yAxes[] = $axis;
  27. }
  28. public function getID() {
  29. return $this->id;
  30. }
  31. public function getTitle() {
  32. return $this->title;
  33. }
  34. public function setTitle($title) {
  35. $this->title = $title;
  36. }
  37. public function setType($type) {
  38. $this->type = $type;
  39. }
  40. public function addDataset($label, $data, $options = []) {
  41. $background_colours = $options["background_colours"] ?? false;
  42. $border_colours = $options["border_colours"] ?? false;
  43. $border_width = $options["border_width"] ?? false;
  44. $y_axis_id = $options["y_axis_id"] ?? false;
  45. $type = $options["type"] ?? false;
  46. $datalabels = $options["datalabels"] ?? false;
  47. $dataset = [
  48. "label" => $label,
  49. "data" => $data,
  50. ];
  51. if($background_colours) $dataset["backgroundColor"] = $background_colours;
  52. if($border_colours) $dataset["borderColor"] = $border_colours;
  53. if($border_width) $dataset["borderWidth"] = (int) $border_width;
  54. if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
  55. if($type) $dataset["type"] = $type;
  56. if($datalabels) $dataset["datalabels"] = $datalabels;
  57. $this->datasets[] = $dataset;
  58. }
  59. public function setLabels($labels) {
  60. $this->labels = $labels;
  61. }
  62. public function setDatalabelSuffixes(... $suffixes) {
  63. $this->datalabelSuffixes = $suffixes;
  64. }
  65. public function getJSON() {
  66. $object = (object) [
  67. "type" => $this->type,
  68. "data" => (object) [
  69. "labels" => $this->labels,
  70. "datasets" => $this->datasets
  71. ],
  72. "options" => (object) [
  73. "title" => (object) [
  74. "display" => true,
  75. "fontSize" => 32,
  76. "fontFamily" => "Roboto",
  77. "fontStyle" => "300",
  78. "text" => $this->title
  79. ],
  80. "legend" => (object) [
  81. "position" => "top",
  82. ],
  83. "plugins" => (object) [
  84. "datalabels" => [
  85. "color" => "white",
  86. ],
  87. ],
  88. ],
  89. ];
  90. if($this->yAxes) {
  91. $object->options->scales = new stdClass;
  92. $object->options->scales->yAxes = $this->yAxes;
  93. }
  94. if($this->datalabelSuffixes) {
  95. $object->options->datalabelSuffixes = $this->datalabelSuffixes;
  96. }
  97. return json_encode($object);
  98. }
  99. }
  100. ?>