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

Add min/max to y-axis for all cartesian charts

Alan Lam 8 лет назад
Родитель
Сommit
a36ace3e67
2 измененных файлов с 30 добавлено и 16 удалено
  1. 19 14
      controllers/performance.php
  2. 11 2
      engine/chart.php

+ 19 - 14
controllers/performance.php

@@ -78,6 +78,7 @@ if($submit) {
 			"project_id" => $project_ids,
 			"status" => Test::STATUS_COMPLETED
 		]);
+		$test_count = count($tests);
 
 		User::load(array_column($tests, "tester_id"));
 		$locations = Location::load(array_column($tests, "location_id"));
@@ -100,7 +101,6 @@ if($submit) {
 		$has_regions = !empty($region_data);
 		$has_area_managers = !empty($area_manager_data);
 
-
 		foreach($locations as $location) {
 			if($has_regions && !$location->region) $locations_missing_region[] = $location;
 			if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
@@ -496,6 +496,7 @@ if($submit) {
 		}, $data));
 
 		$chart = new Chart;
+		$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
 		$chart->setType("bar");
 		$chart->setTitle("Pass Rate by $resolution_name");
 		$chart->setLabels($x_data_labels);
@@ -514,9 +515,18 @@ if($submit) {
 		$chart->setType("bar");
 		$chart->setTitle("Totals by $resolution_name");
 		$chart->setLabels($x_data_labels);
-		$chart->addYAxis("linear", "left", "y-axis-1");
-		$chart->addYAxis("linear", "right", "y-axis-2", false);
+		$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => ceil($test_count / 10) * 10]);
+		$chart->addYAxis("linear", "right", "y-axis-2", false, ["min" => 0, "max" => 100]);
 
+		$chart->addDataset("Performance (%)", $performance, [
+			"border_colours" => "#FFEB3B",
+			"background_colours" => "transparent",
+			"type" => "line",
+			"y_axis_id" => "y-axis-2",
+			"datalabels" => [
+				"display" => false
+			],
+		]);
 		$chart->addDataset("Test Count", $test_counts, [
 			"background_colours" => "#2196F3",
 			"type" => "bar",
@@ -532,12 +542,6 @@ if($submit) {
 			"type" => "bar",
 			"y_axis_id" => "y-axis-1",
 		]);
-		$chart->addDataset("Performance (%)", $performance, [
-			"border_colours" => "#FFEB3B",
-			"background_colours" => "transparent",
-			"type" => "line",
-			"y_axis_id" => "y-axis-2",
-		]);
 		$charts[] = $chart;
 
 		// Point of Challenge (Each Period)
@@ -571,10 +575,6 @@ if($submit) {
 			$charts[] = $chart;
 		}
 
-
-
-
-
 		// Tester Genders by Period
 		$male_counts = array_values(array_map(function($d) {
 			return $d->maleTesterCount;
@@ -585,6 +585,7 @@ 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->setType("bar");
 		$chart->setTitle("Tester Gender by $resolution_name");
 		$chart->setLabels($x_data_labels);
@@ -602,6 +603,7 @@ if($submit) {
 		}, $data));
 
 		$chart = new Chart;
+		$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]);
 		$chart->setType("bar");
 		$chart->setTitle("Tests by $resolution_name");
 		$chart->setLabels($x_data_labels);
@@ -613,7 +615,6 @@ if($submit) {
 		$pass_counts = array_column($data, "passCount");
 		$fail_counts = array_column($data, "failCount");
 
-
 		if($has_regions) {
 
 			$region_keys = array_keys($region_data);
@@ -632,6 +633,7 @@ if($submit) {
 			}
 
 			$chart = new Chart;
+			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
 			$chart->setType("bar");
 			$chart->setTitle("Region Performance");
 			$chart->setLabels($x_data_labels);
@@ -642,6 +644,7 @@ if($submit) {
 
 			// Region Pass Rate Period
 			$chart = new Chart;
+			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
 			$chart->setType("bar");
 			$chart->setTitle("Region Pass Rate by $resolution_name");
 			$chart->setLabels($x_data_labels);
@@ -678,6 +681,7 @@ if($submit) {
 			}
 
 			$chart = new Chart;
+			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
 			$chart->setType("bar");
 			$chart->setTitle("Area Manager Performance");
 			$chart->setLabels($x_data_labels);
@@ -688,6 +692,7 @@ if($submit) {
 
 			// Area Pass Rate Period
 			$chart = new Chart;
+			$chart->addYAxis("linear", "left", "y-axis-1", true, ["min" => 0, "max" => 100]);
 			$chart->setType("bar");
 			$chart->setTitle("Area Manager Pass Rate by $resolution_name");
 			$chart->setLabels($x_data_labels);

+ 11 - 2
engine/chart.php

@@ -13,8 +13,9 @@ class Chart {
 		$this->id = bin2hex(random_bytes(8));
 	}
 
-	public function addYAxis($type, $position, $id, $grid_lines = true) {
-		$this->yAxes[] = [
+	public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) {
+
+		$axis = [
 			"type" => $type,
 			"display" => true,
 			"position" => $position,
@@ -23,6 +24,12 @@ class Chart {
 				"drawOnChartArea" => $grid_lines
 			],
 		];
+
+		if($tick_config) {
+			$axis["ticks"] = $tick_config;
+		}
+
+		$this->yAxes[] = $axis;
 	}
 
 	public function getID() {
@@ -48,6 +55,7 @@ class Chart {
 		$border_width = $options["border_width"] ?? false;
 		$y_axis_id = $options["y_axis_id"] ?? false;
 		$type = $options["type"] ?? false;
+		$datalabels = $options["datalabels"] ?? false;
 
 		$dataset = [
 			"label" => $label,
@@ -59,6 +67,7 @@ class Chart {
 		if($border_width) $dataset["borderWidth"] = (int) $border_width;
 		if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
 		if($type) $dataset["type"] = $type;
+		if($datalabels) $dataset["datalabels"] = $datalabels;
 
 		$this->datasets[] = $dataset;