chart_generator.php 37 KB

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