chart_generator.php 40 KB

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