chart_generator.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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" => Chart::DATALABELS_BOX,
  498. "line_tension" => 0,
  499. ]);
  500. $chart->addDataset("Test Count", $test_counts, [
  501. "background_colours" => self::COLOUR_BLUE,
  502. "type" => "bar",
  503. "y_axis_id" => "y-axis-1",
  504. "datalabels" => Chart::DATALABELS_STROKE,
  505. ]);
  506. $chart->addDataset("Pass", $pass_counts, [
  507. "background_colours" => self::COLOUR_GREEN,
  508. "type" => "bar",
  509. "y_axis_id" => "y-axis-1",
  510. "datalabels" => Chart::DATALABELS_STROKE
  511. ]);
  512. $chart->addDataset("Fail", $fail_counts, [
  513. "background_colours" => self::COLOUR_RED,
  514. "type" => "bar",
  515. "y_axis_id" => "y-axis-1",
  516. "datalabels" => Chart::DATALABELS_STROKE
  517. ]);
  518. $chart->setDatalabelSuffixes("%", "", "", "");
  519. $chart->print = true;
  520. return $chart;
  521. }
  522. /**
  523. * @return Chart[]
  524. * @throws Exception
  525. */
  526. public function getPointOfChallengeCharts(string $title_format, array $labels = []): array {
  527. $charts = [];
  528. foreach($this->periodKeys as $index => $key) {
  529. $key_label = $this->periodLabels[$index];
  530. $period_data = $this->periodicData[$key];
  531. if($period_data->testCount === 0) continue;
  532. $chart = new Chart();
  533. $chart->setType("pie");
  534. $chart->setTitle(sprintf($title_format, $key_label));
  535. $chart->setLabels($labels);
  536. $chart->addDataset("Counts", [
  537. $period_data->stage1Count,
  538. $period_data->stage2Count,
  539. $period_data->stage3Count,
  540. $period_data->stage4Count,
  541. $period_data->stage5Count,
  542. ], [
  543. "background_colours" => [
  544. self::COLOUR_GREEN,
  545. self::COLOUR_YELLOW,
  546. self::COLOUR_ORANGE,
  547. self::COLOUR_LIGHT_RED,
  548. self::COLOUR_RED,
  549. ],
  550. "datalabels" => Chart::DATALABELS_BOX,
  551. ]);
  552. $charts[] = $chart;
  553. }
  554. return $charts;
  555. }
  556. public function getPeriodicTesterGenderChart(): Chart {
  557. $male_counts = $this->getPeriodicMaleTesterCount();
  558. $female_counts = $this->getPeriodicFemaleTesterCount();
  559. $counts = array_merge($male_counts, $female_counts);
  560. $chart = new Chart();
  561. $chart->setIsPeriodic(true);
  562. $chart->addYAxis([
  563. "id" => "y-axis-1",
  564. "tick_config" => ["min" => 0, "max" => ceil(max($counts) / 10) * 10]
  565. ]);
  566. $chart->setType("bar");
  567. $chart->setTitle("Tester Gender by {$this->resolutionName}");
  568. $chart->setLabels($this->xDataLabels);
  569. $chart->addDataset("Male", $male_counts, [
  570. "background_colours" => self::COLOUR_BLUE
  571. ]);
  572. $chart->addDataset("Female", $female_counts, [
  573. "background_colours" => self::COLOUR_MAGENTA
  574. ]);
  575. return $chart;
  576. }
  577. public function getPeriodicTestCountChart(string $title = "Tests"): Chart {
  578. $test_counts = $this->getPeriodicTestCounts();
  579. $pass_rates = $this->getPeriodicPassRates();
  580. $pass_rate_max = ceil(max($pass_rates) / 10) * 10;
  581. $chart = new Chart();
  582. $chart->setIsPeriodic(true);
  583. $chart->addYAxis([
  584. "id" => "y-axis-1",
  585. "tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
  586. ]);
  587. $chart->addYAxis([
  588. "id" => "y-axis-2",
  589. "tick_config" => ["min" => 0, "max" => $pass_rate_max],
  590. "position" => "right"
  591. ]);
  592. $chart->setType("bar");
  593. $chart->setTitle("{$title} by {$this->resolutionName}");
  594. $chart->setLabels($this->xDataLabels);
  595. $chart->addDataset("Pass Rate", $pass_rates, [
  596. "type" => "line",
  597. "y_axis_id" => "y-axis-2",
  598. "border_colours" => self::COLOUR_RED,
  599. "background_colours" => "transparent",
  600. "line_tension" => 0,
  601. "datalabels" => [
  602. "align" => "top",
  603. "color" => self::COLOUR_DARK_GREY,
  604. "borderColor" => self::COLOUR_GREY,
  605. "borderWidth" => 1,
  606. "borderRadius" => 4,
  607. "backgroundColor" => "white",
  608. "font" => [
  609. "weight" => "bold"
  610. ],
  611. ],
  612. "point_background_colour" => self::COLOUR_RED,
  613. ]);
  614. $chart->addDataset("Tests", $test_counts, [
  615. "background_colours" => self::COLOUR_BLUE
  616. ]);
  617. $chart->setDatalabelSuffixes("%", "");
  618. return $chart;
  619. }
  620. public function getGenderComparisonCharts(): array {
  621. $charts = [];
  622. $gender_keys = [
  623. "maleChallengeMale" => [
  624. "title" => "Male Challenge Male",
  625. "label" => "M Cha. M",
  626. ],
  627. "maleChallengeFemale" => [
  628. "title" => "Male Challenge Female",
  629. "label" => "M Cha. F",
  630. ],
  631. "femaleChallengeMale" => [
  632. "title" => "Female Challenge Male",
  633. "label" => "F Cha. M",
  634. ],
  635. "femaleChallengeFemale" => [
  636. "title" => "Female Challenge Female",
  637. "label" => "F Cha. F",
  638. ],
  639. "unknownChallenge" => [
  640. "title" => "Gender Not Specified",
  641. "label" => "Gender Not Specified",
  642. ],
  643. ];
  644. $gender_test_counts = [];
  645. foreach($gender_keys as $gk => $gv) {
  646. $gender_test_counts[] = array_sum(array_column($this->periodicData, "{$gk}Count"));
  647. }
  648. // Gender Comparison by Date Range
  649. $chart = new Chart();
  650. $chart->addYAxis([
  651. "id" => "y-axis-1",
  652. "tick_config" => ["min" => 0, "max" => ceil(max($gender_test_counts) / 10) * 10]
  653. ]);
  654. $chart->setType("bar");
  655. $chart->setTitle("Gender Comparison ({$this->dateRangeLabel})");
  656. $chart->setLabels(array_column($gender_keys, "label"));
  657. $chart->addDataset("Tests", $gender_test_counts, [
  658. "background_colours" => self::COLOUR_BLUE
  659. ]);
  660. $charts[] = $chart;
  661. // Gender Comparison by Resolution
  662. $mcm_counts = array_column($this->periodicData, "maleChallengeMaleCount");
  663. $mcf_counts = array_column($this->periodicData, "maleChallengeFemaleCount");
  664. $fcm_counts = array_column($this->periodicData, "femaleChallengeMaleCount");
  665. $fcf_counts = array_column($this->periodicData, "femaleChallengeFemaleCount");
  666. $ns_counts = array_column($this->periodicData, "unknownChallengeCount");
  667. $counts = array_merge($mcm_counts, $mcf_counts, $fcm_counts, $fcf_counts);
  668. $chart = new Chart();
  669. $chart->setIsPeriodic(true);
  670. $chart->addYAxis([
  671. "id" => "y-axis-1",
  672. "tick_config" => ["min" => 0, "max" => ceil(max($counts) / 10) * 10]
  673. ]);
  674. $chart->setType("bar");
  675. $chart->setTitle("Gender Comparison by {$this->resolutionName}");
  676. $chart->setLabels($this->xDataLabels);
  677. $chart->addDataset("M Cha. M", $mcm_counts, ["background_colours" => self::COLOUR_BLUE]);
  678. $chart->addDataset("M Cha. F", $mcf_counts, ["background_colours" => self::COLOUR_DARK_BLUE]);
  679. $chart->addDataset("F Cha. M", $fcm_counts, ["background_colours" => self::COLOUR_PURPLE]);
  680. $chart->addDataset("F Cha. F", $fcf_counts, ["background_colours" => self::COLOUR_MAGENTA]);
  681. $chart->addDataset("Not Specified", $ns_counts, ["background_colours" => self::COLOUR_GREY]);
  682. $chart->print = true;
  683. $charts[] = $chart;
  684. // Gender Comparison Pass Rate by Resolution
  685. /** @noinspection PhpUnusedLocalVariableInspection */
  686. {
  687. $mcm_pass_counts = array_column($this->periodicData, "maleChallengeMalePassCount");
  688. $mcf_pass_counts = array_column($this->periodicData, "maleChallengeFemalePassCount");
  689. $fcm_pass_counts = array_column($this->periodicData, "femaleChallengeMalePassCount");
  690. $fcf_pass_counts = array_column($this->periodicData, "femaleChallengeFemalePassCount");
  691. $ns_pass_counts = array_column($this->periodicData, "unknownChallengePassCount");
  692. }
  693. /**
  694. * @var float $mcm_pass_rates
  695. * @var float $mcf_pass_rates
  696. * @var float $fcm_pass_rates
  697. * @var float $fcf_pass_rates
  698. * @var float $ns_pass_rates
  699. */
  700. foreach(["mcm", "mcf", "fcm", "fcf", "ns"] as $prefix) {
  701. $pass_rates = [];
  702. $pass_counts = ${$prefix . "_pass_counts"};
  703. $test_counts = ${$prefix . "_counts"};
  704. foreach($test_counts as $index => $test_count) {
  705. if($test_count === 0) {
  706. $pass_rates[] = 0;
  707. continue;
  708. }
  709. $pass_rates[] = number_format(($pass_counts[$index] * 100) / $test_count, 2);
  710. }
  711. ${$prefix . "_pass_rates"} = $pass_rates;
  712. }
  713. $chart = new Chart();
  714. $chart->setIsPeriodic(true);
  715. $chart->addYAxis([
  716. "id" => "y-axis-1",
  717. "tick_config" => ["min" => 0, "max" => 100],
  718. ]);
  719. $chart->setType("bar");
  720. $chart->setTitle("Gender Comparison Pass Rate by {$this->resolutionName}");
  721. $chart->setLabels($this->xDataLabels);
  722. $chart->addDataset("M Cha. M", $mcm_pass_rates, ["background_colours" => self::COLOUR_BLUE, "datalabels" => ["display" => false], "line_tension" => 0]);
  723. $chart->addDataset("M Cha. F", $mcf_pass_rates, ["background_colours" => self::COLOUR_DARK_BLUE, "datalabels" => ["display" => false], "line_tension" => 0]);
  724. $chart->addDataset("F Cha. M", $fcm_pass_rates, ["background_colours" => self::COLOUR_PURPLE, "datalabels" => ["display" => false], "line_tension" => 0]);
  725. $chart->addDataset("F Cha. F", $fcf_pass_rates, ["background_colours" => self::COLOUR_MAGENTA, "datalabels" => ["display" => false], "line_tension" => 0]);
  726. $chart->addDataset("Not Specified", $ns_pass_rates, ["background_colours" => self::COLOUR_GREY, "datalabels" => ["display" => false], "line_tension" => 0]);
  727. $chart->print = true;
  728. $chart->setDatalabelSuffixes("%", "%", "%", "%", "%");
  729. $charts[] = $chart;
  730. // Gender Comparison Point of Challenge
  731. foreach($this->periodKeys as $index => $key) {
  732. $key_label = $this->periodLabels[$index];
  733. $period_data = $this->periodicData[$key];
  734. foreach($gender_keys as $prefix => $gv) {
  735. $title = $gv["label"];
  736. $test_count = $period_data->{$prefix . "Count"};
  737. $stage_data = $period_data->{$prefix . "Stages"};
  738. if($test_count === 0) continue;
  739. $chart = new Chart();
  740. $chart->setType("pie");
  741. $chart->setTitle("$key_label $title Point of Challenge");
  742. $chart->setLabels(["On Entry", "While Browsing", "During Play", "On Exit", "No Challenge"]);
  743. $chart->addDataset("Counts", $stage_data, [
  744. "background_colours" => [
  745. self::COLOUR_GREEN,
  746. self::COLOUR_YELLOW,
  747. self::COLOUR_ORANGE,
  748. self::COLOUR_LIGHT_RED,
  749. self::COLOUR_RED,
  750. ],
  751. "datalabels" => Chart::DATALABELS_BOX,
  752. ]);
  753. $charts[] = $chart;
  754. }
  755. }
  756. return $charts;
  757. }
  758. private function getDataPassRateChart(array $data, string $entity_name): Chart {
  759. $data_values = [];
  760. $test_counts = [];
  761. $pass_counts = [];
  762. $fail_counts = [];
  763. uasort($data, static function(array $a, array $b) {
  764. $a_test_count = array_sum(array_column($a, "testCount"));
  765. $a_pass_count = array_sum(array_column($a, "passCount"));
  766. $a_pass_rate = ($a_test_count === 0) ? 0 : round(($a_pass_count * 100) / $a_test_count);
  767. $b_test_count = array_sum(array_column($b, "testCount"));
  768. $b_pass_count = array_sum(array_column($b, "passCount"));
  769. $b_pass_rate = ($b_test_count === 0) ? 0 : round(($b_pass_count * 100) / $b_test_count);
  770. return $b_pass_rate <=> $a_pass_rate;
  771. });
  772. foreach($data as $datum) {
  773. $total_test_count = array_sum(array_column($datum, "testCount"));
  774. $total_pass_count = array_sum(array_column($datum, "passCount"));
  775. $test_counts[] = $total_test_count;
  776. $pass_counts[] = $total_pass_count;
  777. $fail_counts[] = $total_test_count - $total_pass_count;
  778. $data_values[] = ($total_test_count === 0) ? 0 : round(($total_pass_count * 100) / $total_test_count);
  779. }
  780. $chart = new Chart();
  781. $chart->addXAxis([
  782. "type" => "category",
  783. "scale_label_config" => [
  784. "display" => true,
  785. "labelString" => $entity_name,
  786. ]
  787. ]);
  788. $chart->addYAxis([
  789. "id" => "y-axis-1",
  790. "tick_config" => ["min" => 0, "max" => 100]
  791. ]);
  792. $chart->setType("bar");
  793. $chart->setTitle("{$entity_name} Pass Rate ({$this->dateRangeLabel})");
  794. $chart->setLabels(array_keys($data));
  795. $chart->addDataset("Pass %", $data_values , [
  796. "background_colours" => self::COLOUR_BLUE
  797. ]);
  798. $chart->addPrintDataset("Tests", $test_counts);
  799. $chart->addPrintDataset("Pass", $pass_counts);
  800. $chart->addPrintDataset("Fail", $fail_counts);
  801. $chart->setDatalabelSuffixes("%");
  802. $chart->print = true;
  803. $chart->printTotalColumn = true;
  804. $chart->numeratorColumnIndex = 2;
  805. $chart->denominatorColumnIndex = 1;
  806. return $chart;
  807. }
  808. public function getPeriodicDataPassRateChart(string $entity): Chart {
  809. switch($entity) {
  810. case "region":
  811. $data = $this->regionData;
  812. $entity_name = $this->customer->getRegionLabel(1);
  813. $entity_keys = $this->getRegionKeys();
  814. $x_labels = $this->getRegionXDataLabels();
  815. break;
  816. case "areaManager":
  817. $data = $this->areaManagerData;
  818. $entity_name = $this->customer->getAreaManagerLabel(1);
  819. $entity_keys = $this->getAreaManagerKeys();
  820. $x_labels = $this->getAreaManagerKeys();
  821. break;
  822. case "brand":
  823. $data = $this->brandData;
  824. $entity_name = $this->customer->getBrandLabel(1);
  825. $entity_keys = $this->getBrandKeys();
  826. $x_labels = $this->getBrandKeys();
  827. break;
  828. case "operationsManager":
  829. $data = $this->operationsManagerData;
  830. $entity_name = $this->customer->getOperationsManagerLabel(1);
  831. $entity_keys = $x_labels = $this->getOperationsManagerKeys();
  832. break;
  833. default:
  834. throw new RuntimeException("Invalid entity passed to getPeriodicDataPassRateChart: $entity");
  835. }
  836. $chart = new Chart();
  837. $chart->setIsPeriodic(true);
  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, $this->customer->getRegionLabel(1));
  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, $this->customer->getAreaManagerLabel(1));
  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, $this->customer->getBrandLabel(1));
  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, $this->customer->getOperationsManagerLabel(1));
  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->setIsPeriodic(true);
  1149. $chart->setType("bar");
  1150. $chart->setTitle("{$title} by {$this->resolutionName}");
  1151. $chart->setLabels($this->xDataLabels);
  1152. $chart->addDataset("Yes", $yes_counts, [
  1153. "background_colours" => self::COLOUR_GREEN
  1154. ]);
  1155. $chart->addDataset("No", $no_counts, [
  1156. "background_colours" => self::COLOUR_RED
  1157. ]);
  1158. $chart->setDatalabelSuffixes("%", "%");
  1159. return $chart;
  1160. }
  1161. private function getPeriodicPassRateChart(string $title, array $yes_rates, array $no_rates, ?array $null_rates = null): Chart {
  1162. $chart = new Chart();
  1163. $chart->setIsPeriodic(true);
  1164. $chart->addYAxis([
  1165. "tick_config" => ["min" => 0, "max" => 100],
  1166. ]);
  1167. $chart->setType("bar");
  1168. $chart->setTitle($title);
  1169. $chart->setLabels($this->xDataLabels);
  1170. $chart->setDatalabelSuffixes("%", "%");
  1171. $chart->addDataset("Yes (%)", $yes_rates, [
  1172. "background_colours" => self::COLOUR_GREEN,
  1173. "border_colours" => self::COLOUR_GREEN,
  1174. "line_tension" => 0,
  1175. "datalabels" => ["display" => false],
  1176. ]);
  1177. $chart->addDataset("No (%)", $no_rates, [
  1178. "background_colours" => self::COLOUR_RED,
  1179. "border_colours" => self::COLOUR_RED,
  1180. "line_tension" => 0,
  1181. "datalabels" => ["display" => false],
  1182. ]);
  1183. if($null_rates !== null) {
  1184. $chart->addDataset("No challenge", $null_rates, [
  1185. "background_colours" => self::COLOUR_CYAN,
  1186. "border_colours" => self::COLOUR_CYAN,
  1187. "line_tension" => 0,
  1188. "datalabels" => ["display" => false],
  1189. ]);
  1190. $chart->setDatalabelSuffixes("%", "%", "%");
  1191. }
  1192. $chart->print = true;
  1193. return $chart;
  1194. }
  1195. /**
  1196. * @param Template $template
  1197. */
  1198. public function setTemplate(Template $template): void {
  1199. $this->template = $template;
  1200. }
  1201. public function setCustomer(?Customer $customer): void {
  1202. $this->customer = $customer;
  1203. }
  1204. }