chart_generator.php 40 KB

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