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

Amend display of various charts

Alan Lam 8 лет назад
Родитель
Сommit
04431b9b6a
2 измененных файлов с 78 добавлено и 15 удалено
  1. 51 14
      controllers/reports.php
  2. 27 1
      engine/chart.php

+ 51 - 14
controllers/reports.php

@@ -514,7 +514,11 @@ if($submit) {
 		}, $data));
 
 		$chart = new Chart;
-		$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
+		$chart->addYAxis([
+			"id" => "y-axis-1",
+			"tick_config" => ["min" => 0, "max" => 100]
+		]);
+
 		$chart->setType("bar");
 		$chart->setTitle("Pass Rate by $resolution_name");
 		$chart->setLabels($x_data_labels);
@@ -534,8 +538,16 @@ if($submit) {
 		$chart->setType("bar");
 		$chart->setTitle("Totals by $resolution_name");
 		$chart->setLabels($x_data_labels);
-		$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]);
-		$chart->addYAxis("linear", "right", "y-axis-2", false, ["min" => 0, "max" => 100]);
+		$chart->addYAxis([
+			"id" => "y-axis-1",
+			"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
+		]);
+		$chart->addYAxis([
+			"position" => "right",
+			"id" => "y-axis-2",
+			"grid_lines" => false,
+			"tick_config" => ["min" => 0, "max" => 100]
+		]);
 
 		$chart->addDataset("Pass Rate (%)", $pass_rates, [
 			"border_colours" => "#FFEB3B",
@@ -600,9 +612,12 @@ if($submit) {
 					"#FBC02D",
 					"#FFAB91",
 					"#F44336",
-				]
+				],
+				"datalabels" => [
+					"backgroundColor" => "rgba(0, 0, 0, 0.75)",
+					"borderRadius" => "2",
+				],
 			]);
-			$chart->setDatalabelSuffixes(" Test");
 			$charts[] = $chart;
 		}
 
@@ -616,7 +631,11 @@ if($submit) {
 		}, $data));
 
 		$chart = new Chart;
-		$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => ceil(max(max($male_counts), max($female_counts)) / 10) * 10]);
+		$chart->addYAxis([
+			"id" => "y-axis-1",
+			"tick_config" => ["min" => 0, "max" => ceil(max(max($male_counts), max($female_counts)) / 10) * 10]
+		]);
+
 		$chart->setType("bar");
 		$chart->setTitle("Tester Gender by $resolution_name");
 		$chart->setLabels($x_data_labels);
@@ -626,7 +645,6 @@ if($submit) {
 		$chart->addDataset("Female", $female_counts, [
 			"background_colours" => "#E91E63"
 		]);
-		$chart->setDatalabelSuffixes(" M", " F");
 		$charts[] = $chart;
 
 		// Test Count by Period
@@ -635,14 +653,16 @@ if($submit) {
 		}, $data));
 
 		$chart = new Chart;
-		$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]);
+		$chart->addYAxis([
+			"id" => "y-axis-1",
+			"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
+		]);
 		$chart->setType("bar");
 		$chart->setTitle("Tests by $resolution_name");
 		$chart->setLabels($x_data_labels);
 		$chart->addDataset("Tests", $test_counts, [
 			"background_colours" => "#2196F3"
 		]);
-		$chart->setDatalabelSuffixes(" Test");
 		$charts[] = $chart;
 
 		$pass_counts = array_column($data, "passCount");
@@ -668,7 +688,10 @@ if($submit) {
 			}
 
 			$chart = new Chart;
-			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
+			$chart->addYAxis([
+				"id" => "y-axis-1",
+				"tick_config" => ["min" => 0, "max" => 100]
+			]);
 			$chart->setType("bar");
 			$chart->setTitle("Region Pass Rate $date_range_label");
 			$chart->setLabels($x_data_labels);
@@ -680,7 +703,10 @@ if($submit) {
 
 			// Region Pass Rate Period
 			$chart = new Chart;
-			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
+			$chart->addYAxis([
+				"id" => "y-axis-1",
+				"tick_config" => ["min" => 0, "max" => 100]
+			]);
 			$chart->setType("bar");
 			$chart->setTitle("Region Pass Rate by $resolution_name");
 			$chart->setLabels($x_data_labels);
@@ -727,7 +753,10 @@ if($submit) {
 					"labelString" => "Area Manager",
 				]
 			]);
-			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
+			$chart->addYAxis([
+				"id" => "y-axis-1",
+				"tick_config" => ["min" => 0, "max" => 100]
+			]);
 			$chart->setType("bar");
 			$chart->setTitle("Area Manager Pass Rate $date_range_label");
 			$chart->setLabels($x_data_labels);
@@ -746,7 +775,10 @@ if($submit) {
 					"labelString" => "Area Manager",
 				]
 			]);
-			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
+			$chart->addYAxis([
+				"id" => "y-axis-1",
+				"tick_config" => ["min" => 0, "max" => 100]
+			]);
 			$chart->setType("bar");
 			$chart->setTitle("Area Manager Pass Rate by $resolution_name");
 			$chart->setLabels($x_data_labels);
@@ -757,7 +789,12 @@ if($submit) {
 					$a_data = $area_manager_data[$area_manager][$key];
 					$data_values[] = ($a_data->testCount == 0) ? 0 : round(($a_data->passCount * 100) / $a_data->testCount);
 				}
-				$chart->addDataset($key_labels[$index], $data_values, ["background_colours" => $colours[$index]]);
+				$chart->addDataset($key_labels[$index], $data_values, [
+					"background_colours" => $colours[$index],
+					"datalabels" => [
+						"display" => false,
+					],
+				]);
 			}
 			$chart->setDatalabelSuffixes(...array_fill(0, count($keys), "%"));
 

+ 27 - 1
engine/chart.php

@@ -9,6 +9,9 @@ class Chart {
 	private $labels = [];
 	private $printLabels = null;
 
+	private $tooltipsMode = "nearest";
+	private $tooltipsIntersect = true;
+
 	private $xAxes = [];
 	private $yAxes = [];
 
@@ -34,6 +37,7 @@ class Chart {
 		$grid_lines = $options["grid_lines"] ?? true;
 		$tick_config = $options["tick_config"] ?? null;
 		$scale_label_config = $options["scale_label_config"] ?? null;
+		$stacked = $options["stacked"] ?? false;
 
 		$axis = [
 			"type" => $type,
@@ -43,6 +47,7 @@ class Chart {
 			"gridLines" => [
 				"drawOnChartArea" => $grid_lines
 			],
+			"stacked" => $stacked,
 		];
 
 		if($tick_config) {
@@ -56,7 +61,15 @@ class Chart {
 		$this->xAxes[] = $axis;
 	}
 
-	public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) {
+	public function addYAxis($options = []) {
+
+		$type = $options["type"] ?? "linear";
+		$position = $options["position"] ?? "left";
+		$id = $options["id"] ?? "y-axis-0";
+		$grid_lines = $options["grid_lines"] ?? true;
+		$tick_config = $options["tick_config"] ?? null;
+		$scale_label_config = $options["scale_label_config"] ?? null;
+		$stacked = $options["stacked"] ?? false;
 
 		$axis = [
 			"type" => $type,
@@ -66,6 +79,7 @@ class Chart {
 			"gridLines" => [
 				"drawOnChartArea" => $grid_lines
 			],
+			"stacked" => $stacked,
 		];
 
 		if($tick_config) {
@@ -175,6 +189,10 @@ class Chart {
 				"legend" => (object) [
 					"position" => "top",
 				],
+				"tooltips" => (object) [
+					"mode" => $this->tooltipsMode,
+					"intersect" => $this->tooltipsIntersect,
+				],
 				"plugins" => (object) [
 					"datalabels" => [
 						"color" => "white",
@@ -287,6 +305,14 @@ class Chart {
 		$this->afterHtml = $html;
 	}
 
+	public function setTooltipsMode($mode) {
+		$this->tooltipsMode = $mode;
+	}
+
+	public function setTooltipsIntersect($intersect) {
+		$this->tooltipsIntersect = $intersect;
+	}
+
 }
 
 ?>