1
0

chart_generator.php 36 KB

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