فهرست منبع

Add datalabel suffixes

Alan Lam 8 سال پیش
والد
کامیت
a5105fde52
3فایلهای تغییر یافته به همراه36 افزوده شده و 0 حذف شده
  1. 13 0
      controllers/performance.php
  2. 10 0
      engine/chart.php
  3. 13 0
      public/js/app-performance.js

+ 13 - 0
controllers/performance.php

@@ -438,6 +438,7 @@ if($submit) {
 				"#F44336",
 			]
 		]);
+		$chart->setDatalabelSuffixes(" Test");
 		$charts[] = $chart;
 
 
@@ -455,6 +456,7 @@ if($submit) {
 				"#E91E63",
 			]
 		]);
+		$chart->setDatalabelSuffixes(" Test");
 		$charts[] = $chart;
 
 		// Tester Gender
@@ -471,6 +473,7 @@ if($submit) {
 				"#E91E63",
 			]
 		]);
+		$chart->setDatalabelSuffixes(" Test");
 		$charts[] = $chart;
 
 		// Pass / Fail
@@ -487,6 +490,7 @@ if($submit) {
 				"#F44336",
 			]
 		]);
+		$chart->setDatalabelSuffixes(" Test");
 		$charts[] = $chart;
 
 		// Pass Rate Period
@@ -503,6 +507,7 @@ if($submit) {
 		$chart->addDataset("Pass Rate (%)", $performance, [
 			"background_colours" => "#2196F3"
 		]);
+		$chart->setDatalabelSuffixes("%");
 		$charts[] = $chart;
 
 		// Monthly Totals by Period
@@ -542,6 +547,7 @@ if($submit) {
 			"type" => "bar",
 			"y_axis_id" => "y-axis-1",
 		]);
+		$chart->setDatalabelSuffixes(" %", " T", " P", " F");
 		$charts[] = $chart;
 
 		// Point of Challenge (Each Period)
@@ -572,6 +578,7 @@ if($submit) {
 					"#F44336",
 				]
 			]);
+			$chart->setDatalabelSuffixes(" Test");
 			$charts[] = $chart;
 		}
 
@@ -595,6 +602,7 @@ if($submit) {
 		$chart->addDataset("Female", $female_counts, [
 			"background_colours" => "#E91E63"
 		]);
+		$chart->setDatalabelSuffixes(" M", " F");
 		$charts[] = $chart;
 
 		// Test Count by Period
@@ -610,6 +618,7 @@ if($submit) {
 		$chart->addDataset("Tests", $test_counts, [
 			"background_colours" => "#2196F3"
 		]);
+		$chart->setDatalabelSuffixes(" Test");
 		$charts[] = $chart;
 
 		$pass_counts = array_column($data, "passCount");
@@ -642,6 +651,7 @@ if($submit) {
 			$chart->addDataset("Pass Rate (%)", $data_values , [
 				"background_colours" => "#2196F3"
 			]);
+			$chart->setDatalabelSuffixes("%");
 			$charts[] = $chart;
 
 			// Region Pass Rate Period
@@ -659,6 +669,7 @@ if($submit) {
 				}
 				$chart->addDataset($key_labels[$index], $data_values, ["background_colours" => $colours[$index]]);
 			}
+			$chart->setDatalabelSuffixes(...array_fill(0, count($keys), "%"));
 
 			$charts[] = $chart;
 
@@ -692,6 +703,7 @@ if($submit) {
 			$chart->addDataset("Pass Rate (%)", $data_values , [
 				"background_colours" => "#2196F3"
 			]);
+			$chart->setDatalabelSuffixes("%");
 			$charts[] = $chart;
 
 			// Area Pass Rate Period
@@ -709,6 +721,7 @@ if($submit) {
 				}
 				$chart->addDataset($key_labels[$index], $data_values, ["background_colours" => $colours[$index]]);
 			}
+			$chart->setDatalabelSuffixes(...array_fill(0, count($keys), "%"));
 
 			$charts[] = $chart;
 

+ 10 - 0
engine/chart.php

@@ -9,6 +9,8 @@ class Chart {
 	private $labels = [];
 	private $yAxes = [];
 
+	private $datalabelSuffixes = [];
+
 	public function __construct() {
 		$this->id = bin2hex(random_bytes(8));
 	}
@@ -77,6 +79,10 @@ class Chart {
 		$this->labels = $labels;
 	}
 
+	public function setDatalabelSuffixes(... $suffixes) {
+		$this->datalabelSuffixes = $suffixes;
+	}
+
 	public function getJSON() {
 
 		$object = (object) [
@@ -109,6 +115,10 @@ class Chart {
 			$object->options->scales->yAxes = $this->yAxes;
 		}
 
+		if($this->datalabelSuffixes) {
+			$object->options->datalabelSuffixes = $this->datalabelSuffixes;
+		}
+
 
 		return json_encode($object);
 

+ 13 - 0
public/js/app-performance.js

@@ -9,6 +9,19 @@ $(function() {
 			var value = dataset.data[context.dataIndex];
 			return value;
 		};
+			console.log(object);
+
+		if(object.options.datalabelSuffixes) {
+			object.options.plugins.datalabels.formatter = function(value, context) {
+				let suffix = object.options.datalabelSuffixes[context.datasetIndex];
+
+				if(suffix != "%" && value != 1) {
+					suffix += "s";
+				}
+
+				return value + suffix;
+			}
+		}
 
 		var chart = new Chart(this, object);
 	});