Просмотр исходного кода

Fix index out-of-bounds warning

Alan Lam 2 лет назад
Родитель
Сommit
e630797e53
2 измененных файлов с 13 добавлено и 1 удалено
  1. 11 0
      engine/chart.php
  2. 2 1
      engine/chart_generator.php

+ 11 - 0
engine/chart.php

@@ -61,6 +61,15 @@ class Chart {
 	public $printPercentages = false;
 	public $printTotalColumn = false;
 
+	// Static methods
+	public static function getColourByIndex(int $index): string {
+		$count = count(self::COLOURS);
+		$colour_index = $index % $count;
+		return self::COLOURS[$colour_index];
+	}
+
+	// Constructor
+
 	/**
 	 * Chart constructor.
 	 * @throws Exception
@@ -69,6 +78,8 @@ class Chart {
 		$this->id = bin2hex(random_bytes(8));
 	}
 
+	// Object methods
+
 	public function addXAxis($options = []) {
 
 		$type = $options["type"] ?? "linear";

+ 2 - 1
engine/chart_generator.php

@@ -850,6 +850,7 @@ class ChartGenerator {
 		$chart->setTitle("{$entity_name} Pass Rate by {$this->resolutionName}");
 		$chart->setLabels($x_labels);
 
+
 		foreach($this->periodKeys as $index => $key) {
 			$data_values = [];
 			foreach($entity_keys as $entity_key) {
@@ -857,7 +858,7 @@ class ChartGenerator {
 				$data_values[] = ($datum->testCount === 0) ? 0 : round(($datum->passCount * 100) / $datum->testCount);
 			}
 			$chart->addDataset($this->periodLabels[$index], $data_values, [
-				"background_colours" => Chart::COLOURS[$index],
+				"background_colours" => Chart::getColourByIndex($index),
 				"datalabels" => [
 					"display" => false
 				]