chart_generator.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. <?php
  2. /**
  3. * Class ChartGenerator
  4. */
  5. class ChartGenerator {
  6. public const COLOUR_GREEN = "#4CAF50";
  7. public const COLOUR_RED = "#F44336";
  8. public const COLOUR_YELLOW = "#FFEB3B";
  9. public const COLOUR_LIGHT_GREY = "#CCCCCC";
  10. public const COLOUR_LIGHT_RED = "#FFAB91";
  11. public const COLOUR_ORANGE = "#FBC02D";
  12. public const COLOUR_BLUE = "#2196F3";
  13. public const COLOUR_MAGENTA = "#E91E63";
  14. public const COLOUR_DARK_BLUE = "#3F51B5";
  15. public const COLOUR_PURPLE = "#9C27B0";
  16. public const COLOUR_GREY = "#9E9E9E";
  17. public const COLOUR_CYAN = "#00BCD4";
  18. /** @var object $totalData */
  19. private $totalData;
  20. /** @var array $periodicData */
  21. private $periodicData;
  22. /** @var string $dateRangeLabel */
  23. private $dateRangeLabel;
  24. /** @var string $resolutionName */
  25. private $resolutionName;
  26. /** @var string[] $xDataLabels */
  27. private $xDataLabels;
  28. /** @var string[] $periodKeys */
  29. private $periodKeys;
  30. /** @var string[] $periodLabels */
  31. private $periodLabels;
  32. /** @var array $regionData */
  33. private $regionData;
  34. /** @var array $areaManagerData */
  35. private $areaManagerData;
  36. /** @var array */
  37. private $brandData;
  38. /** @var Template $template */
  39. private $template;
  40. public static function getVenueTypePassFailChart(array $venue_type_data): Chart {
  41. $chart = new Chart();
  42. $chart->addXAxis([
  43. "type" => "category",
  44. "stacked" => true,
  45. ]);
  46. $chart->addYAxis([
  47. "id" => "y-axis-1",
  48. "tick_config" => ["min" => 0, "max" => 100],
  49. "stacked" => true,
  50. ]);
  51. $get_rates = static function(string $column) use ($venue_type_data) {
  52. return array_values(array_map(static function($d) use ($column) {
  53. if($d["testCount"] === 0) return 0;
  54. return round(($d[$column] / $d["testCount"]) * 100);
  55. }, $venue_type_data));
  56. };
  57. $pass_rates = $get_rates("passCount");
  58. $fail_rates = $get_rates("failCount");
  59. $chart->setType("bar");
  60. $chart->setTitle("Pass / Fail by Venue Type");
  61. $chart->setLabels(array_keys($venue_type_data));
  62. $chart->addDataset("Pass Rate (%)", $pass_rates, ["background_colours" => self::COLOUR_GREEN]);
  63. $chart->addDataset("Fail Rate (%)", $fail_rates, ["background_colours" => self::COLOUR_RED]);
  64. $chart->setDatalabelSuffixes("%", "%");
  65. return $chart;
  66. }
  67. public function getVenueTypeChallengeStageStackedBarChart(array $venue_type_data): ?Chart {
  68. $field = $this->template->getChallengeStageField();
  69. if(!$field) {
  70. return null;
  71. }
  72. $x_labels = $field->displayStrings;
  73. array_shift($x_labels); // Remove placeholder string
  74. $chart = new Chart();
  75. $chart->addXAxis([
  76. "type" => "category",
  77. "stacked" => true,
  78. ]);
  79. $chart->addYAxis([
  80. "id" => "y-axis-1",
  81. "tick_config" => ["min" => 0, "max" => 100],
  82. "stacked" => true,
  83. ]);
  84. $get_rates = static function(string $stage_index) use ($venue_type_data) {
  85. return array_values(array_map(static function($d) use ($stage_index) {
  86. $total_count = array_sum($d["stageCounts"]);
  87. if($total_count === 0) return 0;
  88. return round(($d["stageCounts"][$stage_index] / $total_count) * 100, 1);
  89. }, $venue_type_data));
  90. };
  91. $bg_colours = [
  92. self::COLOUR_GREEN,
  93. self::COLOUR_YELLOW,
  94. self::COLOUR_ORANGE,
  95. self::COLOUR_LIGHT_RED,
  96. self::COLOUR_RED,
  97. ];
  98. $chart->setType("bar");
  99. $chart->setTitle("Stages by Venue Type");
  100. $chart->setLabels(array_keys($venue_type_data));
  101. foreach($x_labels as $stage_index => $label) {
  102. $chart->addDataset("$label (%)", $get_rates($stage_index), ["background_colours" => $bg_colours[$stage_index]]);
  103. }
  104. $suffixes = array_fill(0, count($x_labels), "%");
  105. $chart->setDatalabelSuffixes(...$suffixes);
  106. return $chart;
  107. }
  108. public function getVenueTypeChallengeStagePieCharts(array $venue_type_data): array {
  109. $field = $this->template->getChallengeStageField();
  110. if(!$field) {
  111. return [];
  112. }
  113. $x_labels = $field->displayStrings;
  114. array_shift($x_labels); // Remove placeholder string
  115. $charts = [];
  116. foreach($venue_type_data as $venue_type => $data) {
  117. $values = $data["stageCounts"];
  118. $chart = new Chart();
  119. $chart->setType("pie");
  120. $chart->setTitle("Tests by Stage - $venue_type ({$this->dateRangeLabel})");
  121. $chart->setLabels($x_labels);
  122. $chart->addDataset("Counts", $values, [
  123. "background_colours" => [
  124. self::COLOUR_GREEN,
  125. self::COLOUR_YELLOW,
  126. self::COLOUR_ORANGE,
  127. self::COLOUR_LIGHT_RED,
  128. self::COLOUR_RED,
  129. ],
  130. "datalabels" => Chart::DATALABELS_BOX,
  131. ]);
  132. $charts[] = $chart;
  133. }
  134. return $charts;
  135. }
  136. public function __construct(object $total_data, array $periodic_data) {
  137. $this->totalData = $total_data;
  138. $this->periodicData = $periodic_data;
  139. $this->xDataLabels = array_values(array_map(static function($d) {
  140. return $d->label;
  141. }, $this->periodicData));
  142. }
  143. public function setDateRangeLabel(string $label): void {
  144. $this->dateRangeLabel = $label;
  145. }
  146. public function setResolutionName(string $name): void {
  147. $this->resolutionName = $name;
  148. }
  149. public function setPeriodKeysAndLabels(array $keys, array $labels): void {
  150. $this->periodKeys = $keys;
  151. $this->periodLabels = $labels;
  152. }
  153. public function setRegionData(array $data): void {
  154. ksort($data, SORT_NATURAL);
  155. $this->regionData = $data;
  156. }
  157. public function setAreaManagerData(array $data): void {
  158. ksort($data, SORT_NATURAL);
  159. $this->areaManagerData = $data;
  160. }
  161. public function setBrandData(array $data): void {
  162. $this->brandData = $data;
  163. }
  164. // Charts
  165. public function getTestsByStageChart(): Chart {
  166. $chart = new Chart;
  167. $chart->setType("doughnut");
  168. $chart->setTitle("Tests by Stage ({$this->dateRangeLabel})");
  169. $chart->setLabels(["1 - On Entry", "2 - While Browsing", "3 - During Play", "4 - On Exit", "5 - No Challenge"]);
  170. $chart->addDataset("Tests", [
  171. $this->totalData->stage1Count,
  172. $this->totalData->stage2Count,
  173. $this->totalData->stage3Count,
  174. $this->totalData->stage4Count,
  175. $this->totalData->stage5Count,
  176. ], [
  177. "background_colours" => [
  178. self::COLOUR_GREEN,
  179. self::COLOUR_YELLOW,
  180. self::COLOUR_ORANGE,
  181. self::COLOUR_LIGHT_RED,
  182. self::COLOUR_RED,
  183. ],
  184. "datalabels" => Chart::DATALABELS_BOX,
  185. ]);
  186. $chart->setPrintSuffixes("");
  187. $chart->print = true;
  188. $chart->printPercentages = true;
  189. $chart->printTotalColumn = true;
  190. $chart->setPrintLabels(["Stage 1", "Stage 2", "Stage 3", "Stage 4", "Stage 5"]);
  191. return $chart;
  192. }
  193. public function getChallengerGenderChart(): Chart {
  194. $chart = new Chart;
  195. $chart->setType("pie");
  196. $chart->setTitle("Staff Gender ({$this->dateRangeLabel})");
  197. $chart->setLabels(["Male", "Female", "Unspecified"]);
  198. $chart->addDataset("Counts", [
  199. $this->totalData->maleChallengerCount,
  200. $this->totalData->femaleChallengerCount,
  201. $this->totalData->unspecifiedChallengerCount,
  202. ], [
  203. "background_colours" => [
  204. self::COLOUR_BLUE,
  205. self::COLOUR_MAGENTA,
  206. self::COLOUR_GREY,
  207. ],
  208. "datalabels" => Chart::DATALABELS_BOX,
  209. ]);
  210. return $chart;
  211. }
  212. public function getTesterGenderChart(): Chart {
  213. $chart = new Chart;
  214. $chart->setType("pie");
  215. $chart->setTitle("Tester Gender ({$this->dateRangeLabel})");
  216. $chart->setLabels(["Male", "Female"]);
  217. $chart->addDataset("Counts", [
  218. $this->totalData->maleTesterCount,
  219. $this->totalData->femaleTesterCount,
  220. ], [
  221. "background_colours" => [
  222. self::COLOUR_BLUE,
  223. self::COLOUR_MAGENTA,
  224. ],
  225. "datalabels" => Chart::DATALABELS_BOX,
  226. ]);
  227. return $chart;
  228. }
  229. public function getPassFailCountChart(): Chart {
  230. $data = $this->totalData;
  231. $dataset = [$data->passCount, $data->failCount];
  232. $labels = ["Pass", "Fail"];
  233. $colours = [self::COLOUR_GREEN, self::COLOUR_RED];
  234. if($data->voidCount > 0) {
  235. $dataset[] = $data->voidCount;
  236. $labels[] = "Void";
  237. $colours[] = self::COLOUR_GREY;
  238. }
  239. $chart = new Chart;
  240. $chart->setType("pie");
  241. $chart->setTitle("Pass / Fail ({$this->dateRangeLabel})");
  242. $chart->setLabels($labels);
  243. $chart->addDataset("Counts", $dataset, [
  244. "background_colours" => $colours,
  245. "datalabels" => Chart::DATALABELS_BOX,
  246. ]);
  247. return $chart;
  248. }
  249. public function getPeriodicPassFailRateChart(): Chart {
  250. $chart = new Chart;
  251. $chart->addXAxis([
  252. "type" => "category",
  253. "stacked" => true,
  254. ]);
  255. $chart->addYAxis([
  256. "id" => "y-axis-1",
  257. "tick_config" => ["min" => 0, "max" => 100],
  258. "stacked" => true,
  259. ]);
  260. $chart->setType("bar");
  261. $chart->setTitle("Pass / Fail by {$this->resolutionName}");
  262. $chart->setLabels($this->xDataLabels);
  263. $chart->addDataset("Pass Rate (%)", $this->getPeriodicPassRates(), [
  264. "background_colours" => self::COLOUR_GREEN
  265. ]);
  266. $chart->addDataset("Fail Rate (%)", $this->getPeriodicFailRates(), [
  267. "background_colours" => self::COLOUR_RED
  268. ]);
  269. $chart->setDatalabelSuffixes("%", "%");
  270. return $chart;
  271. }
  272. public function getPeriodicDoorSensorAlertPassRateChart(): Chart {
  273. return $this->getPeriodicPassRateChart(
  274. "Pass Rate by Door Sensor Alert",
  275. $this->getPeriodicPassRateByDoorSensorAlert(1),
  276. $this->getPeriodicPassRateByDoorSensorAlert(0),
  277. $this->getPeriodicPassRateByDoorSensorAlert(2)
  278. );
  279. }
  280. public function getPeriodicMachinePowerChart(): Chart {
  281. return $this->getPeriodicBooleanChart(
  282. "Machines Powered",
  283. $this->getMachinePowerRates()
  284. );
  285. }
  286. public function getPeriodicMachineSeatChart(): Chart {
  287. return $this->getPeriodicBooleanChart(
  288. "Machine Seating",
  289. $this->getMachineSeatRates()
  290. );
  291. }
  292. public function getPeriodicMachineTableChart(): Chart {
  293. return $this->getPeriodicBooleanChart(
  294. "Machine Table/Shelf",
  295. $this->getMachineTableRates()
  296. );
  297. }
  298. public function getPeriodicMachineAccessibleChart(): Chart {
  299. return $this->getPeriodicBooleanChart(
  300. "Machines Accessible",
  301. $this->getMachineAccessibleRates()
  302. );
  303. }
  304. public function getPeriodicEyeContactPassRateChart(): Chart {
  305. return $this->getPeriodicPassRateChart(
  306. "Pass Rate by Eye Contact",
  307. $this->getPeriodicPassRateByEyeContact(1),
  308. $this->getPeriodicPassRateByEyeContact(0),
  309. $this->getPeriodicPassRateByEyeContact(2)
  310. );
  311. }
  312. public function getPeriodicLineOfSightPassRateChart(): Chart {
  313. return $this->getPeriodicPassRateChart(
  314. "Pass Rate by Line of Sight",
  315. $this->getPeriodicPassRateByLineOfSight(1),
  316. $this->getPeriodicPassRateByLineOfSight(0),
  317. $this->getPeriodicPassRateByLineOfSight(2)
  318. );
  319. }
  320. public function getPeriodicBarPurchasePassRateChart(): Chart {
  321. return $this->getPeriodicPassRateChart(
  322. "Pass Rate by Bar Purchase",
  323. $this->getPeriodicPassRateByBarPurchase(1),
  324. $this->getPeriodicPassRateByBarPurchase(0),
  325. $this->getPeriodicPassRateByBarPurchase(2)
  326. );
  327. }
  328. public function getPeriodicBooleanPassRateChart(array $rates, string $title): Chart {
  329. $chart = new Chart;
  330. $chart->addXAxis([
  331. "type" => "category",
  332. "stacked" => true,
  333. ]);
  334. $chart->addYAxis([
  335. "id" => "y-axis-1",
  336. "tick_config" => ["min" => 0, "max" => 100],
  337. "stacked" => true,
  338. ]);
  339. $chart->setType("bar");
  340. $chart->setTitle($title);
  341. $chart->setLabels($this->xDataLabels);
  342. $chart->addDataset("Yes (%)", array_column($rates, 0), [
  343. "background_colours" => self::COLOUR_GREEN
  344. ]);
  345. $chart->addDataset("No (%)", array_column($rates, 1), [
  346. "background_colours" => self::COLOUR_RED
  347. ]);
  348. $chart->setDatalabelSuffixes("%", "%");
  349. return $chart;
  350. }
  351. public function getPeriodicEyeContactChart(): Chart {
  352. return $this->getPeriodicBooleanChart(
  353. "Eye Contact by {$this->resolutionName}",
  354. $this->getEyeContactRates()
  355. );
  356. }
  357. public function getPeriodicTotalsChart(): Chart {
  358. $test_counts = $this->getPeriodicTestCounts();
  359. $pass_rates = $this->getPeriodicPassRates();
  360. $pass_counts = $this->getPeriodicPassCounts();
  361. $fail_counts = $this->getPeriodicFailCounts();
  362. $chart = new Chart;
  363. $chart->setType("bar");
  364. $chart->setTitle("Totals by {$this->resolutionName}");
  365. $chart->setLabels($this->xDataLabels);
  366. $chart->addYAxis([
  367. "id" => "y-axis-1",
  368. "tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
  369. ]);
  370. $chart->addYAxis([
  371. "position" => "right",
  372. "id" => "y-axis-2",
  373. "grid_lines" => false,
  374. "tick_config" => ["min" => 0, "max" => 100]
  375. ]);
  376. $chart->addDataset("Pass Rate (%)", $pass_rates, [
  377. "border_colours" => self::COLOUR_YELLOW,
  378. "background_colours" => "transparent",
  379. "type" => "line",
  380. "y_axis_id" => "y-axis-2",
  381. "datalabels" => [
  382. "display" => false
  383. ],
  384. "line_tension" => 0,
  385. ]);
  386. $chart->addDataset("Test Count", $test_counts, [
  387. "background_colours" => self::COLOUR_BLUE,
  388. "type" => "bar",
  389. "y_axis_id" => "y-axis-1",
  390. "datalabels" => [
  391. "display" => false
  392. ],
  393. ]);
  394. $chart->addDataset("Pass", $pass_counts, [
  395. "background_colours" => self::COLOUR_GREEN,
  396. "type" => "bar",
  397. "y_axis_id" => "y-axis-1",
  398. "datalabels" => [
  399. "display" => false
  400. ],
  401. ]);
  402. $chart->addDataset("Fail", $fail_counts, [
  403. "background_colours" => self::COLOUR_RED,
  404. "type" => "bar",
  405. "y_axis_id" => "y-axis-1",
  406. "datalabels" => [
  407. "display" => false
  408. ],
  409. ]);
  410. $chart->setDatalabelSuffixes("%", "", "", "");
  411. $chart->print = true;
  412. return $chart;
  413. }
  414. /**
  415. * @return Chart[]
  416. * @throws Exception
  417. */
  418. public function getPointOfChallengeCharts(string $title_format, array $labels = []): array {
  419. $charts = [];
  420. foreach($this->periodKeys as $index => $key) {
  421. $key_label = $this->periodLabels[$index];
  422. $period_data = $this->periodicData[$key];
  423. if($period_data->testCount === 0) continue;
  424. $chart = new Chart;
  425. $chart->setType("pie");
  426. $chart->setTitle(sprintf($title_format, $key_label));
  427. $chart->setLabels($labels);
  428. $chart->addDataset("Counts", [
  429. $period_data->stage1Count,
  430. $period_data->stage2Count,
  431. $period_data->stage3Count,
  432. $period_data->stage4Count,
  433. $period_data->stage5Count,
  434. ], [
  435. "background_colours" => [
  436. self::COLOUR_GREEN,
  437. self::COLOUR_YELLOW,
  438. self::COLOUR_ORANGE,
  439. self::COLOUR_LIGHT_RED,
  440. self::COLOUR_RED,
  441. ],
  442. "datalabels" => Chart::DATALABELS_BOX,
  443. ]);
  444. $charts[] = $chart;
  445. }
  446. return $charts;
  447. }
  448. public function getPeriodicTesterGenderChart(): Chart {
  449. $male_counts = $this->getPeriodicMaleTesterCount();
  450. $female_counts = $this->getPeriodicFemaleTesterCount();
  451. $counts = array_merge($male_counts, $female_counts);
  452. $chart = new Chart;
  453. $chart->addYAxis([
  454. "id" => "y-axis-1",
  455. "tick_config" => ["min" => 0, "max" => ceil(max($counts) / 10) * 10]
  456. ]);
  457. $chart->setType("bar");
  458. $chart->setTitle("Tester Gender by {$this->resolutionName}");
  459. $chart->setLabels($this->xDataLabels);
  460. $chart->addDataset("Male", $male_counts, [
  461. "background_colours" => self::COLOUR_BLUE
  462. ]);
  463. $chart->addDataset("Female", $female_counts, [
  464. "background_colours" => self::COLOUR_MAGENTA
  465. ]);
  466. return $chart;
  467. }
  468. public function getPeriodicTestCountChart(): Chart {
  469. $test_counts = $this->getPeriodicTestCounts();
  470. $chart = new Chart;
  471. $chart->addYAxis([
  472. "id" => "y-axis-1",
  473. "tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
  474. ]);
  475. $chart->setType("bar");
  476. $chart->setTitle("Tests by {$this->resolutionName}");
  477. $chart->setLabels($this->xDataLabels);
  478. $chart->addDataset("Tests", $test_counts, [
  479. "background_colours" => self::COLOUR_BLUE
  480. ]);
  481. return $chart;
  482. }
  483. public function getGenderComparisonCharts(): array {
  484. $charts = [];
  485. $gender_keys = [
  486. "maleChallengeMale" => [
  487. "title" => "Male Challenge Male",
  488. "label" => "M Cha. M",
  489. ],
  490. "maleChallengeFemale" => [
  491. "title" => "Male Challenge Female",
  492. "label" => "M Cha. F",
  493. ],
  494. "femaleChallengeMale" => [
  495. "title" => "Female Challenge Male",
  496. "label" => "F Cha. M",
  497. ],
  498. "femaleChallengeFemale" => [
  499. "title" => "Female Challenge Female",
  500. "label" => "F Cha. F",
  501. ],
  502. "unknownChallenge" => [
  503. "title" => "Gender Not Specified",
  504. "label" => "Gender Not Specified",
  505. ],
  506. ];
  507. $gender_test_counts = [];
  508. foreach($gender_keys as $gk => $gv) {
  509. $gender_test_counts[] = array_sum(array_column($this->periodicData, "{$gk}Count"));
  510. }
  511. // Gender Comparison by Date Range
  512. $chart = new Chart;
  513. $chart->addYAxis([
  514. "id" => "y-axis-1",
  515. "tick_config" => ["min" => 0, "max" => ceil(max($gender_test_counts) / 10) * 10]
  516. ]);
  517. $chart->setType("bar");
  518. $chart->setTitle("Gender Comparison ({$this->dateRangeLabel})");
  519. $chart->setLabels(array_column($gender_keys, "label"));
  520. $chart->addDataset("Tests", $gender_test_counts, [
  521. "background_colours" => self::COLOUR_BLUE
  522. ]);
  523. $charts[] = $chart;
  524. // Gender Comparison by Resolution
  525. $mcm_counts = array_column($this->periodicData, "maleChallengeMaleCount");
  526. $mcf_counts = array_column($this->periodicData, "maleChallengeFemaleCount");
  527. $fcm_counts = array_column($this->periodicData, "femaleChallengeMaleCount");
  528. $fcf_counts = array_column($this->periodicData, "femaleChallengeFemaleCount");
  529. $ns_counts = array_column($this->periodicData, "unknownChallengeCount");
  530. $counts = array_merge($mcm_counts, $mcf_counts, $fcm_counts, $fcf_counts);
  531. $chart = new Chart;
  532. $chart->addYAxis([
  533. "id" => "y-axis-1",
  534. "tick_config" => ["min" => 0, "max" => ceil(max($counts) / 10) * 10]
  535. ]);
  536. $chart->setType("bar");
  537. $chart->setTitle("Gender Comparison by {$this->resolutionName}");
  538. $chart->setLabels($this->xDataLabels);
  539. $chart->addDataset("M Cha. M", $mcm_counts, ["background_colours" => self::COLOUR_BLUE]);
  540. $chart->addDataset("M Cha. F", $mcf_counts, ["background_colours" => self::COLOUR_DARK_BLUE]);
  541. $chart->addDataset("F Cha. M", $fcm_counts, ["background_colours" => self::COLOUR_PURPLE]);
  542. $chart->addDataset("F Cha. F", $fcf_counts, ["background_colours" => self::COLOUR_MAGENTA]);
  543. $chart->addDataset("Not Specified", $ns_counts, ["background_colours" => self::COLOUR_GREY]);
  544. $chart->print = true;
  545. $charts[] = $chart;
  546. // Gender Comparison Pass Rate by Resolution
  547. /** @noinspection PhpUnusedLocalVariableInspection */
  548. {
  549. $mcm_pass_counts = array_column($this->periodicData, "maleChallengeMalePassCount");
  550. $mcf_pass_counts = array_column($this->periodicData, "maleChallengeFemalePassCount");
  551. $fcm_pass_counts = array_column($this->periodicData, "femaleChallengeMalePassCount");
  552. $fcf_pass_counts = array_column($this->periodicData, "femaleChallengeFemalePassCount");
  553. $ns_pass_counts = array_column($this->periodicData, "unknownChallengePassCount");
  554. }
  555. /**
  556. * @var float $mcm_pass_rates
  557. * @var float $mcf_pass_rates
  558. * @var float $fcm_pass_rates
  559. * @var float $fcf_pass_rates
  560. * @var float $ns_pass_rates
  561. */
  562. foreach(["mcm", "mcf", "fcm", "fcf", "ns"] as $prefix) {
  563. $pass_rates = [];
  564. $pass_counts = ${$prefix . "_pass_counts"};
  565. $test_counts = ${$prefix . "_counts"};
  566. foreach($test_counts as $index => $test_count) {
  567. if($test_count === 0) {
  568. $pass_rates[] = 0;
  569. continue;
  570. }
  571. $pass_rates[] = number_format(($pass_counts[$index] * 100) / $test_count, 2);
  572. }
  573. ${$prefix . "_pass_rates"} = $pass_rates;
  574. }
  575. $chart = new Chart;
  576. $chart->addYAxis([
  577. "id" => "y-axis-1",
  578. "tick_config" => ["min" => 0, "max" => 100],
  579. ]);
  580. $chart->setType("bar");
  581. $chart->setTitle("Gender Comparison Pass Rate by {$this->resolutionName}");
  582. $chart->setLabels($this->xDataLabels);
  583. $chart->addDataset("M Cha. M", $mcm_pass_rates, ["background_colours" => self::COLOUR_BLUE, "datalabels" => ["display" => false], "line_tension" => 0]);
  584. $chart->addDataset("M Cha. F", $mcf_pass_rates, ["background_colours" => self::COLOUR_DARK_BLUE, "datalabels" => ["display" => false], "line_tension" => 0]);
  585. $chart->addDataset("F Cha. M", $fcm_pass_rates, ["background_colours" => self::COLOUR_PURPLE, "datalabels" => ["display" => false], "line_tension" => 0]);
  586. $chart->addDataset("F Cha. F", $fcf_pass_rates, ["background_colours" => self::COLOUR_MAGENTA, "datalabels" => ["display" => false], "line_tension" => 0]);
  587. $chart->addDataset("Not Specified", $ns_pass_rates, ["background_colours" => self::COLOUR_GREY, "datalabels" => ["display" => false], "line_tension" => 0]);
  588. $chart->print = true;
  589. $chart->setDatalabelSuffixes("%", "%", "%", "%", "%");
  590. $charts[] = $chart;
  591. // Gender Comparison Point of Challenge
  592. foreach($this->periodKeys as $index => $key) {
  593. $key_label = $this->periodLabels[$index];
  594. $period_data = $this->periodicData[$key];
  595. foreach($gender_keys as $prefix => $gv) {
  596. $title = $gv["label"];
  597. $test_count = $period_data->{$prefix . "Count"};
  598. $stage_data = $period_data->{$prefix . "Stages"};
  599. if($test_count === 0) continue;
  600. $chart = new Chart;
  601. $chart->setType("pie");
  602. $chart->setTitle("$key_label $title Point of Challenge");
  603. $chart->setLabels(["On Entry", "While Browsing", "During Play", "On Exit", "No Challenge"]);
  604. $chart->addDataset("Counts", $stage_data, [
  605. "background_colours" => [
  606. self::COLOUR_GREEN,
  607. self::COLOUR_YELLOW,
  608. self::COLOUR_ORANGE,
  609. self::COLOUR_LIGHT_RED,
  610. self::COLOUR_RED,
  611. ],
  612. "datalabels" => Chart::DATALABELS_BOX,
  613. ]);
  614. $charts[] = $chart;
  615. }
  616. }
  617. return $charts;
  618. }
  619. private function getDataPassRateChart(array $data, string $entity_name): Chart {
  620. $data_values = [];
  621. $test_counts = [];
  622. $pass_counts = [];
  623. $fail_counts = [];
  624. foreach($data as $datum) {
  625. $total_test_count = array_sum(array_column($datum, "testCount"));
  626. $total_pass_count = array_sum(array_column($datum, "passCount"));
  627. $test_counts[] = $total_test_count;
  628. $pass_counts[] = $total_pass_count;
  629. $fail_counts[] = $total_test_count - $total_pass_count;
  630. $data_values[] = ($total_test_count === 0) ? 0 : round(($total_pass_count * 100) / $total_test_count);
  631. }
  632. $chart = new Chart;
  633. $chart->addXAxis([
  634. "type" => "category",
  635. "scale_label_config" => [
  636. "display" => true,
  637. "labelString" => $entity_name,
  638. ]
  639. ]);
  640. $chart->addYAxis([
  641. "id" => "y-axis-1",
  642. "tick_config" => ["min" => 0, "max" => 100]
  643. ]);
  644. $chart->setType("bar");
  645. $chart->setTitle("{$entity_name} Pass Rate ({$this->dateRangeLabel})");
  646. $chart->setLabels(array_keys($data));
  647. $chart->addDataset("Pass %", $data_values , [
  648. "background_colours" => self::COLOUR_BLUE
  649. ]);
  650. $chart->addPrintDataset("Tests", $test_counts);
  651. $chart->addPrintDataset("Pass", $pass_counts);
  652. $chart->addPrintDataset("Fail", $fail_counts);
  653. $chart->setDatalabelSuffixes("%");
  654. $chart->print = true;
  655. $chart->printTotalColumn = true;
  656. return $chart;
  657. }
  658. public function getPeriodicDataPassRateChart(string $entity): Chart {
  659. switch($entity) {
  660. case "region":
  661. $data = $this->regionData;
  662. $entity_name = "Region";
  663. $entity_keys = $this->getRegionKeys();
  664. $x_labels = $this->getRegionXDataLabels();
  665. break;
  666. case "areaManager":
  667. $data = $this->areaManagerData;
  668. $entity_name = "Area Manager";
  669. $entity_keys = $this->getAreaManagerKeys();
  670. $x_labels = $this->getAreaManagerKeys();
  671. break;
  672. case "brand":
  673. $data = $this->brandData;
  674. $entity_name = "Brand";
  675. $entity_keys = $this->getBrandKeys();
  676. $x_labels = $this->getBrandKeys();
  677. break;
  678. default:
  679. throw new RuntimeException("Invalid entity passed to getPeriodicDataPassRateChart: $entity");
  680. }
  681. $chart = new Chart();
  682. $chart->addXAxis([
  683. "type" => "category",
  684. "scale_label_config" => [
  685. "display" => true,
  686. "labelString" => $entity_name,
  687. ]
  688. ]);
  689. $chart->addYAxis([
  690. "id" => "y-axis-1",
  691. "tick_config" => ["min" => 0, "max" => 100]
  692. ]);
  693. $chart->setType("bar");
  694. $chart->setTitle("{$entity_name} Pass Rate by {$this->resolutionName}");
  695. $chart->setLabels($x_labels);
  696. foreach($this->periodKeys as $index => $key) {
  697. $data_values = [];
  698. foreach($entity_keys as $entity_key) {
  699. $datum = $data[$entity_key][$key];
  700. $data_values[] = ($datum->testCount === 0) ? 0 : round(($datum->passCount * 100) / $datum->testCount);
  701. }
  702. $chart->addDataset($this->periodLabels[$index], $data_values, [
  703. "background_colours" => Chart::COLOURS[$index],
  704. "datalabels" => [
  705. "display" => false
  706. ]
  707. ]);
  708. }
  709. $chart->setDatalabelSuffixes(...array_fill(0, count($this->periodKeys), "%"));
  710. return $chart;
  711. }
  712. public function hasRegionData(): bool {
  713. return $this->regionData !== null;
  714. }
  715. public function getRegionPassRateChart(): Chart {
  716. return $this->getDataPassRateChart($this->regionData, "Region");
  717. }
  718. public function getPeriodicRegionPassRateChart(): Chart {
  719. return $this->getPeriodicDataPassRateChart("region");
  720. }
  721. public function hasAreaManagerData(): bool {
  722. return $this->areaManagerData !== null;
  723. }
  724. public function getAreaManagerPassRateChart(): Chart {
  725. return $this->getDataPassRateChart($this->areaManagerData, "Area Manager");
  726. }
  727. public function getPeriodicAreaManagerPassRateChart(): Chart {
  728. return $this->getPeriodicDataPassRateChart("areaManager");
  729. }
  730. public function hasBrandData(): bool {
  731. return $this->brandData !== null;
  732. }
  733. public function getBrandPassRateChart(): Chart {
  734. return $this->getDataPassRateChart($this->brandData, "Brand");
  735. }
  736. public function getPeriodicBrandPassRateChart(): Chart {
  737. return $this->getPeriodicDataPassRateChart("brand");
  738. }
  739. public function getCleanlinessChart(): Chart {
  740. $chart = new Chart;
  741. $chart->setType("doughnut");
  742. $chart->setTitle("Tests by Cleanliness ({$this->dateRangeLabel})");
  743. $chart->setLabels(["Very dirty", "Dirty", "Adequate", "Clean", "Very Clean"]);
  744. $chart->addDataset("Tests", [
  745. $this->totalData->cleanlinessCounts[0],
  746. $this->totalData->cleanlinessCounts[1],
  747. $this->totalData->cleanlinessCounts[2],
  748. $this->totalData->cleanlinessCounts[3],
  749. $this->totalData->cleanlinessCounts[4],
  750. ], [
  751. "background_colours" => [
  752. self::COLOUR_RED,
  753. self::COLOUR_LIGHT_RED,
  754. self::COLOUR_ORANGE,
  755. self::COLOUR_YELLOW,
  756. self::COLOUR_GREEN,
  757. ],
  758. "datalabels" => Chart::DATALABELS_BOX,
  759. ]);
  760. $chart->setPrintSuffixes("");
  761. $chart->print = true;
  762. $chart->printTotalColumn = true;
  763. $chart->printPercentages = true;
  764. return $chart;
  765. }
  766. /**
  767. * @return Chart[]
  768. * @throws Exception
  769. */
  770. public function getPeriodicCleanlinessCharts(): array {
  771. $charts = [];
  772. foreach($this->periodKeys as $index => $key) {
  773. $key_label = $this->periodLabels[$index];
  774. $period_data = $this->periodicData[$key];
  775. if($period_data->testCount === 0) continue;
  776. $chart = new Chart;
  777. $chart->setType("pie");
  778. $chart->setTitle("$key_label Cleanliness");
  779. $chart->setLabels(["Very dirty", "Dirty", "Adequate", "Clean", "Very Clean"]);
  780. $chart->addDataset("Counts", [
  781. $period_data->cleanlinessCounts[0],
  782. $period_data->cleanlinessCounts[1],
  783. $period_data->cleanlinessCounts[2],
  784. $period_data->cleanlinessCounts[3],
  785. $period_data->cleanlinessCounts[4],
  786. ], [
  787. "background_colours" => [
  788. self::COLOUR_RED,
  789. self::COLOUR_LIGHT_RED,
  790. self::COLOUR_ORANGE,
  791. self::COLOUR_YELLOW,
  792. self::COLOUR_GREEN,
  793. ],
  794. "datalabels" => Chart::DATALABELS_BOX,
  795. ]);
  796. $charts[] = $chart;
  797. }
  798. return $charts;
  799. }
  800. public function getPeriodicStaffBadgeWornChart(): Chart {
  801. $staff_badge_worn_counts = array_column($this->periodicData, "staffBadgeWornCounts");
  802. $unknown_counts = array_column($staff_badge_worn_counts, 0);
  803. $no_counts = array_column($staff_badge_worn_counts, 1);
  804. $yes_counts = array_column($staff_badge_worn_counts, 2);
  805. $chart = new Chart;
  806. $chart->setType("bar");
  807. $chart->setTitle("Staff Badge Worn by {$this->resolutionName}");
  808. $chart->setLabels($this->xDataLabels);
  809. $chart->addDataset("No challenge", $unknown_counts, [
  810. "background_colours" => self::COLOUR_LIGHT_GREY
  811. ]);
  812. $chart->addDataset("No", $no_counts, [
  813. "background_colours" => self::COLOUR_RED
  814. ]);
  815. $chart->addDataset("Yes", $yes_counts, [
  816. "background_colours" => self::COLOUR_GREEN
  817. ]);
  818. return $chart;
  819. }
  820. public function getPeriodicStaffOfferedDrinkChart(): Chart {
  821. $counts = array_column($this->periodicData, "staffOfferedDrinkCounts");
  822. return $this->getPeriodicBooleanChart("Staff Offered Drink", $counts);
  823. }
  824. public function getPeriodicStaffOfferedPromotionChart(): Chart {
  825. $counts = array_column($this->periodicData, "staffOfferedPromotionCounts");
  826. return $this->getPeriodicBooleanChart("Staff Offered Promotion", $counts);
  827. }
  828. public function getPeriodicStaffOfferedHelpChart(): Chart {
  829. $counts = array_column($this->periodicData, "staffOfferedHelpCounts");
  830. return $this->getPeriodicBooleanChart("Staff Offered Help", $counts);
  831. }
  832. public function getPeriodicStaffBehaviourCharts(): array {
  833. $charts = [];
  834. foreach($this->periodKeys as $index => $key) {
  835. $key_label = $this->periodLabels[$index];
  836. $period_data = $this->periodicData[$key];
  837. if($period_data->testCount === 0) continue;
  838. $chart = new Chart;
  839. $chart->setType("pie");
  840. $chart->setTitle("$key_label Staff Behaviour");
  841. $chart->setLabels(["N/A", "Rude", "Slightly rude", "Neither rude or polite", "Polite", "Very polite"]);
  842. $chart->addDataset("Counts", [
  843. $period_data->staffBehaviourCounts[0],
  844. $period_data->staffBehaviourCounts[1],
  845. $period_data->staffBehaviourCounts[2],
  846. $period_data->staffBehaviourCounts[3],
  847. $period_data->staffBehaviourCounts[4],
  848. $period_data->staffBehaviourCounts[5],
  849. ], [
  850. "background_colours" => [
  851. self::COLOUR_LIGHT_GREY,
  852. self::COLOUR_RED,
  853. self::COLOUR_LIGHT_RED,
  854. self::COLOUR_ORANGE,
  855. self::COLOUR_YELLOW,
  856. self::COLOUR_GREEN,
  857. ],
  858. "datalabels" => Chart::DATALABELS_BOX,
  859. ]);
  860. $charts[] = $chart;
  861. }
  862. return $charts;
  863. }
  864. public function getPeriodicStaffUniformChart(): Chart {
  865. $counts = array_column($this->periodicData, "staffUniformCounts");
  866. return $this->getPeriodicBooleanChart("Staff Uniform", $counts);
  867. }
  868. // Private methods
  869. private function getPeriodicPassRates(): array {
  870. return array_values(array_map(static function($d) {
  871. if($d->testCount === 0) return 0;
  872. return round(($d->passCount / $d->testCount) * 100);
  873. }, $this->periodicData));
  874. }
  875. private function getPeriodicFailRates(): array {
  876. return array_values(array_map(static function($d) {
  877. if($d->testCount === 0) return 0;
  878. $pass_rate = round(($d->passCount / $d->testCount) * 100);
  879. return 100 - $pass_rate;
  880. }, $this->periodicData));
  881. }
  882. private function getPeriodicPassRate(string $prefix, int $index): array {
  883. $pass_field = $prefix . "PassCounts";
  884. $fail_field = $prefix . "FailCounts";
  885. return array_values(array_map(static function($data) use ($pass_field, $fail_field, $index) {
  886. if(!$data) return null;
  887. if(!isset($data->$pass_field)) return null;
  888. if(!isset($data->$fail_field)) return null;
  889. $test_count = $data->$pass_field[$index] + $data->$fail_field[$index];
  890. if($test_count === 0) {
  891. return null;
  892. }
  893. return round(($data->$pass_field[$index] / $test_count) * 100);
  894. }, $this->periodicData));
  895. }
  896. private function getPeriodicPassRateByDoorSensorAlert(int $index): array {
  897. return $this->getPeriodicPassRate("doorSensorAlert", $index);
  898. }
  899. private function getPeriodicPassRateByEyeContact(int $index): array {
  900. return $this->getPeriodicPassRate("eyeContact", $index);
  901. }
  902. private function getPeriodicPassRateByLineOfSight(int $index): array {
  903. return $this->getPeriodicPassRate("lineOfSight", $index);
  904. }
  905. private function getPeriodicPassRateByBarPurchase(int $index): array {
  906. return $this->getPeriodicPassRate("barPurchase", $index);
  907. }
  908. private function getMachinePowerRates(): array {
  909. return $this->getBooleanRates("machinePowerCounts");
  910. }
  911. private function getMachineSeatRates(): array {
  912. return $this->getBooleanRates("machineSeatCounts");
  913. }
  914. private function getMachineTableRates(): array {
  915. return $this->getBooleanRates("machineTableCounts");
  916. }
  917. private function getMachineAccessibleRates(): array {
  918. return $this->getBooleanRates("machineAccessibleCounts");
  919. }
  920. private function getBooleanRates(string $field): array {
  921. return array_values(array_map(static function($period) use ($field) {
  922. if($period->testCount === 0) {
  923. return [0, 0, 0];
  924. }
  925. $yes = round(($period->$field[0] / $period->testCount) * 100);
  926. $no = 100 - $yes;
  927. $na = 0;
  928. return [$yes, $no, $na];
  929. }, $this->periodicData));
  930. }
  931. private function getEyeContactRates(): array {
  932. return $this->getBooleanRates("eyeContactCounts");
  933. }
  934. private function getPeriodicTestCounts(): array {
  935. return array_column($this->periodicData, "testCount");
  936. }
  937. private function getPeriodicPassCounts(): array {
  938. return array_column($this->periodicData, "passCount");
  939. }
  940. private function getPeriodicFailCounts(): array {
  941. return array_column($this->periodicData, "failCount");
  942. }
  943. private function getPeriodicMaleTesterCount(): array {
  944. return array_values(array_map(static function($d) {
  945. return $d->maleTesterCount;
  946. }, $this->periodicData));
  947. }
  948. private function getPeriodicFemaleTesterCount(): array {
  949. return array_values(array_map(static function($d) {
  950. return $d->femaleTesterCount;
  951. }, $this->periodicData));
  952. }
  953. private function getRegionKeys(): array {
  954. if(!$this->regionData) return [];
  955. return array_keys($this->regionData);
  956. }
  957. private function getRegionXDataLabels(): array {
  958. return array_map(static function($key) {
  959. return "Region $key";
  960. }, $this->getRegionKeys());
  961. }
  962. private function getAreaManagerKeys(): array {
  963. if(!$this->areaManagerData) return [];
  964. return array_keys($this->areaManagerData);
  965. }
  966. private function getBrandKeys(): array {
  967. if(!$this->brandData) return [];
  968. return array_keys($this->brandData);
  969. }
  970. private function getAreaManagerXDataLabels(): array {
  971. return $this->getAreaManagerKeys();
  972. }
  973. private function getPeriodicBooleanChart(string $title, array $values): Chart {
  974. $no_counts = array_column($values, 0);
  975. $yes_counts = array_column($values, 1);
  976. $chart = new Chart;
  977. $chart->setType("bar");
  978. $chart->setTitle("{$title} by {$this->resolutionName}");
  979. $chart->setLabels($this->xDataLabels);
  980. $chart->addDataset("Yes", $yes_counts, [
  981. "background_colours" => self::COLOUR_GREEN
  982. ]);
  983. $chart->addDataset("No", $no_counts, [
  984. "background_colours" => self::COLOUR_RED
  985. ]);
  986. $chart->setDatalabelSuffixes("%", "%");
  987. return $chart;
  988. }
  989. private function getPeriodicPassRateChart(string $title, array $yes_rates, array $no_rates, ?array $null_rates = null): Chart {
  990. $chart = new Chart;
  991. $chart->addYAxis([
  992. "tick_config" => ["min" => 0, "max" => 100],
  993. ]);
  994. $chart->setType("bar");
  995. $chart->setTitle($title);
  996. $chart->setLabels($this->xDataLabels);
  997. $chart->setDatalabelSuffixes("%", "%");
  998. $chart->addDataset("Yes", $yes_rates, [
  999. "background_colours" => self::COLOUR_GREEN,
  1000. "border_colours" => self::COLOUR_GREEN,
  1001. "line_tension" => 0,
  1002. "datalabels" => ["display" => false],
  1003. ]);
  1004. $chart->addDataset("No", $no_rates, [
  1005. "background_colours" => self::COLOUR_RED,
  1006. "border_colours" => self::COLOUR_RED,
  1007. "line_tension" => 0,
  1008. "datalabels" => ["display" => false],
  1009. ]);
  1010. if($null_rates !== null) {
  1011. $chart->addDataset("No challenge", $null_rates, [
  1012. "background_colours" => self::COLOUR_CYAN,
  1013. "border_colours" => self::COLOUR_CYAN,
  1014. "line_tension" => 0,
  1015. "datalabels" => ["display" => false],
  1016. ]);
  1017. $chart->setDatalabelSuffixes("%", "%", "%");
  1018. }
  1019. $chart->print = true;
  1020. return $chart;
  1021. }
  1022. /**
  1023. * @param Template $template
  1024. */
  1025. public function setTemplate(Template $template): void {
  1026. $this->template = $template;
  1027. }
  1028. }