1
0

chart.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. use PhpOffice\PhpPresentation\Style\Color;
  3. use PhpOffice\PhpPresentation\Style\Alignment;
  4. use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
  5. class Chart {
  6. const DATALABELS_BOX = [
  7. "backgroundColor" => "rgba(0, 0, 0, 0.75)",
  8. "borderRadius" => "2",
  9. ];
  10. private $id = "";
  11. private $title = "Untitled";
  12. private $type = "bar";
  13. private $datasets = [];
  14. private $printDatasets = [];
  15. private $labels = [];
  16. private $printLabels = null;
  17. private $tooltipsMode = "nearest";
  18. private $tooltipsIntersect = true;
  19. private $xAxes = [];
  20. private $yAxes = [];
  21. private $datalabelSuffixes = [];
  22. private $datalabelFormatterCallback = false;
  23. private $printSuffixes = null;
  24. public $beforeHtml = null;
  25. public $afterHtml = null;
  26. public $print = false;
  27. public $printPercentages = false;
  28. public $printTotalColumn = false;
  29. public function __construct() {
  30. $this->id = bin2hex(random_bytes(8));
  31. }
  32. public function addXAxis($options = []) {
  33. $type = $options["type"] ?? "linear";
  34. $position = $options["position"] ?? "bottom";
  35. $id = $options["id"] ?? "x-axis";
  36. $grid_lines = $options["grid_lines"] ?? true;
  37. $tick_config = $options["tick_config"] ?? null;
  38. $scale_label_config = $options["scale_label_config"] ?? null;
  39. $stacked = $options["stacked"] ?? false;
  40. $axis = [
  41. "type" => $type,
  42. "display" => true,
  43. "position" => $position,
  44. "id" => $id,
  45. "gridLines" => [
  46. "drawOnChartArea" => $grid_lines
  47. ],
  48. "stacked" => $stacked,
  49. ];
  50. if($tick_config) {
  51. $axis["ticks"] = $tick_config;
  52. }
  53. if($scale_label_config) {
  54. $axis["scaleLabel"] = $scale_label_config;
  55. }
  56. $this->xAxes[] = $axis;
  57. }
  58. public function addYAxis($options = []) {
  59. $type = $options["type"] ?? "linear";
  60. $position = $options["position"] ?? "left";
  61. $id = $options["id"] ?? "y-axis-0";
  62. $grid_lines = $options["grid_lines"] ?? true;
  63. $tick_config = $options["tick_config"] ?? null;
  64. $scale_label_config = $options["scale_label_config"] ?? null;
  65. $stacked = $options["stacked"] ?? false;
  66. $axis = [
  67. "type" => $type,
  68. "display" => true,
  69. "position" => $position,
  70. "id" => $id,
  71. "gridLines" => [
  72. "drawOnChartArea" => $grid_lines
  73. ],
  74. "stacked" => $stacked,
  75. ];
  76. if($tick_config) {
  77. $axis["ticks"] = $tick_config;
  78. }
  79. $this->yAxes[] = $axis;
  80. }
  81. public function getID() {
  82. return $this->id;
  83. }
  84. public function getTitle() {
  85. return $this->title;
  86. }
  87. public function setTitle($title) {
  88. $this->title = $title;
  89. }
  90. public function setType($type) {
  91. $this->type = $type;
  92. }
  93. public function addDataset($label, $data, $options = []) {
  94. $background_colours = $options["background_colours"] ?? false;
  95. $border_colours = $options["border_colours"] ?? false;
  96. $border_width = $options["border_width"] ?? false;
  97. $y_axis_id = $options["y_axis_id"] ?? false;
  98. $type = $options["type"] ?? false;
  99. $datalabels = $options["datalabels"] ?? false;
  100. $line_tension = $options["line_tension"] ?? null;
  101. $dataset = [
  102. "label" => $label,
  103. "data" => $data,
  104. ];
  105. if($background_colours) $dataset["backgroundColor"] = $background_colours;
  106. if($border_colours) $dataset["borderColor"] = $border_colours;
  107. if($border_width) $dataset["borderWidth"] = (int) $border_width;
  108. if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
  109. if($type) $dataset["type"] = $type;
  110. if($datalabels) $dataset["datalabels"] = $datalabels;
  111. if(isset($line_tension)) $dataset["lineTension"] = $line_tension;
  112. $this->datasets[] = (object) $dataset;
  113. }
  114. public function addPrintDataset($label, $data) {
  115. $dataset = [
  116. "label" => $label,
  117. "data" => $data,
  118. ];
  119. $this->printDatasets[] = (object) $dataset;
  120. }
  121. public function getDatasets() {
  122. return $this->datasets;
  123. }
  124. public function getPrintDatasets() {
  125. return $this->printDatasets;
  126. }
  127. public function getLabels() {
  128. return $this->labels;
  129. }
  130. public function setLabels($labels) {
  131. $this->labels = $labels;
  132. }
  133. public function getPrintLabels() {
  134. return $this->printLabels;
  135. }
  136. public function setPrintLabels($labels) {
  137. $this->printLabels = $labels;
  138. }
  139. public function getDatalabelSuffixes() {
  140. return $this->datalabelSuffixes;
  141. }
  142. public function setDatalabelSuffixes(... $suffixes) {
  143. $this->datalabelSuffixes = $suffixes;
  144. }
  145. public function setDatalabelFormatterCallback($callback) {
  146. $this->datalabelFormatterCallback = $callback;
  147. }
  148. public function getPrintSuffixes() {
  149. return $this->printSuffixes;
  150. }
  151. public function setPrintSuffixes(... $suffixes) {
  152. $this->printSuffixes = $suffixes;
  153. }
  154. public function getJSON() {
  155. $object = (object) [
  156. "type" => $this->type,
  157. "data" => (object) [
  158. "labels" => $this->labels,
  159. "datasets" => $this->datasets
  160. ],
  161. "options" => (object) [
  162. "title" => (object) [
  163. "display" => true,
  164. "fontSize" => 32,
  165. "fontFamily" => "Roboto",
  166. "fontStyle" => "300",
  167. "text" => $this->title
  168. ],
  169. "legend" => (object) [
  170. "position" => "top",
  171. ],
  172. "tooltips" => (object) [
  173. "mode" => $this->tooltipsMode,
  174. "intersect" => $this->tooltipsIntersect,
  175. ],
  176. "plugins" => (object) [
  177. "datalabels" => [
  178. "color" => "white",
  179. ],
  180. ],
  181. ],
  182. ];
  183. if($this->xAxes || $this->yAxes) {
  184. $object->options->scales = new stdClass;
  185. if($this->xAxes) {
  186. $object->options->scales->xAxes = $this->xAxes;
  187. }
  188. if($this->yAxes) {
  189. $object->options->scales->yAxes = $this->yAxes;
  190. }
  191. }
  192. if($this->datalabelSuffixes) {
  193. $object->options->datalabelSuffixes = $this->datalabelSuffixes;
  194. }
  195. if($this->datalabelFormatterCallback) {
  196. $object->datalabelFormatterCallback = $this->datalabelFormatterCallback;
  197. }
  198. return json_encode($object);
  199. }
  200. public function printDataTable() {
  201. ?>
  202. <table class="mt-3 w-100 table table-sm small table-bordered">
  203. <thead>
  204. <tr>
  205. <th></th>
  206. <?php
  207. $labels = $this->printLabels ?? $this->labels;
  208. foreach($labels as $label) {
  209. ?>
  210. <th><?=htmlspecialchars($label)?></th>
  211. <?php
  212. }
  213. if($this->printTotalColumn) {
  214. ?>
  215. <th>Total</th>
  216. <?php
  217. }
  218. ?>
  219. </tr>
  220. </thead>
  221. <tbody>
  222. <?php
  223. $datasets = array_merge($this->datasets, $this->printDatasets);
  224. foreach($datasets as $index => $dataset) {
  225. $suffix = "";
  226. $suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
  227. if($suffixes) {
  228. $suffix = $suffixes[$index] ?? "";
  229. }
  230. $data = $dataset->data;
  231. ?>
  232. <tr>
  233. <td><?=htmlspecialchars($dataset->label)?></td>
  234. <?php
  235. foreach($data as $value) {
  236. ?>
  237. <td class="text-right"><?=number_format($value)?><?=htmlspecialchars($suffix)?></td>
  238. <?php
  239. }
  240. if($this->printTotalColumn) {
  241. $string = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
  242. ?>
  243. <td class="text-right"><?=htmlspecialchars($string)?></td>
  244. <?php
  245. }
  246. ?>
  247. </tr>
  248. <?php
  249. if($this->printPercentages) {
  250. $total = array_sum($data);
  251. $percentages = [];
  252. foreach($data as $value) {
  253. $percentages[] = ($total == 0) ? "0%" : (floor(($value * 10000) / $total) / 100) . "%";
  254. }
  255. ?>
  256. <tr>
  257. <td>Percentage</td>
  258. <?php
  259. foreach($percentages as $string) {
  260. ?>
  261. <td class="text-right"><?=$string?></td>
  262. <?php
  263. }
  264. ?>
  265. </tr>
  266. <?php
  267. }
  268. }
  269. ?>
  270. </tbody>
  271. </table>
  272. <?php
  273. }
  274. public function setBeforeHtml($html) {
  275. $this->beforeHtml = $html;
  276. }
  277. public function setAfterHtml($html) {
  278. $this->afterHtml = $html;
  279. }
  280. public function setTooltipsMode($mode) {
  281. $this->tooltipsMode = $mode;
  282. }
  283. public function setTooltipsIntersect($intersect) {
  284. $this->tooltipsIntersect = $intersect;
  285. }
  286. // Presentation methods
  287. public function addChart($slide, $path) {
  288. $chart_offset_y = 100;
  289. if($this->print) {
  290. $chart_offset_y = 10;
  291. }
  292. $shape = $slide->createDrawingShape();
  293. $shape->setName("Chart")
  294. ->setDescription("Chart")
  295. ->setPath($path)
  296. ->setHeight(520)
  297. ->setOffsetX(133)
  298. ->setOffsetY($chart_offset_y);
  299. return true;
  300. }
  301. public function addDataTable($slide, $offset_x = 30, $offset_y = 540) {
  302. $labels = $this->getLabels();
  303. $datasets = array_merge($this->getDatasets(), $this->getPrintDatasets());
  304. $column_count = 1 + count($labels);
  305. // Table
  306. if($this->printTotalColumn) ++$column_count;
  307. $shape = $slide->createTableShape($column_count)
  308. ->setWidth(900)
  309. ->setOffsetX($offset_x)
  310. ->setOffsetY($offset_y);
  311. // Create header row
  312. $row = $shape->createRow();
  313. $row->setHeight(20);
  314. $cell = $row->nextCell();
  315. $labels = $this->getPrintLabels() ?? $this->getLabels();
  316. if($this->printTotalColumn) $labels[] = "Total";
  317. foreach($labels as $label) {
  318. $cell = $row->nextCell();
  319. $cell->getActiveParagraph()->getAlignment()
  320. ->setMarginBottom(4)
  321. ->setMarginLeft(4)
  322. ->setMarginRight(4)
  323. ->setMarginTop(4);
  324. $cell->createTextRun($label)
  325. ->getFont()->setBold(true);
  326. }
  327. $suffixes = $this->getPrintSuffixes() ?? $this->getDatalabelSuffixes();
  328. // Datasets
  329. foreach($datasets as $index => $dataset) {
  330. $label = $dataset->label;
  331. $data = $dataset->data;
  332. $print_data = array_slice($data, 0);
  333. $total = array_sum($data);
  334. $suffix = $suffixes[$index] ?? "";
  335. if($this->printTotalColumn) {
  336. $print_data[] = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
  337. }
  338. $row = $shape->createRow();
  339. $row->setHeight(20);
  340. $cell = $row->nextCell();
  341. $cell->getActiveParagraph()->getAlignment()
  342. ->setMarginBottom(4)
  343. ->setMarginLeft(4)
  344. ->setMarginRight(4)
  345. ->setMarginTop(4);
  346. $cell->createTextRun($label)
  347. ->getFont()->setBold(true);
  348. foreach($print_data as $value) {
  349. $string = strlen($value) == 0 ? "" : $value . $suffix;
  350. $cell = $row->nextCell();
  351. $cell->getActiveParagraph()->getAlignment()
  352. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  353. ->setMarginBottom(4)
  354. ->setMarginLeft(4)
  355. ->setMarginRight(4)
  356. ->setMarginTop(4);
  357. $cell->createTextRun($string);
  358. }
  359. if($this->printPercentages) {
  360. $percentages = [];
  361. foreach($data as $value) {
  362. $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
  363. }
  364. $row = $shape->createRow();
  365. $row->setHeight(20);
  366. $cell = $row->nextCell();
  367. $cell->getActiveParagraph()->getAlignment()
  368. ->setMarginBottom(4)
  369. ->setMarginLeft(4)
  370. ->setMarginRight(4)
  371. ->setMarginTop(4);
  372. $cell->createTextRun("Percentage")
  373. ->getFont()->setBold(true);
  374. foreach($percentages as $string) {
  375. $cell = $row->nextCell();
  376. $cell->getActiveParagraph()->getAlignment()
  377. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  378. ->setMarginBottom(4)
  379. ->setMarginLeft(4)
  380. ->setMarginRight(4)
  381. ->setMarginTop(4);
  382. $cell->createTextRun($string);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. ?>