1
0

chart_generator.php 40 KB

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