chart_generator.php 35 KB

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