chart.php 12 KB

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