chart_generator.php 38 KB

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