chart_generator.php 41 KB

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