chart_generator.php 40 KB

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