1
0

chart.php 11 KB

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