chart_generator.php 39 KB

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