chart_generator.php 37 KB

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