chart_generator.php 38 KB

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