ソースを参照

Add custom datalabel formatter for "Tests by Stage" chart

Alan Lam 8 年 前
親
コミット
bb9450f2c5
2 ファイル変更、31 行追加、9 行削除
  1. 12 8
      controllers/performance.php
  2. 19 1
      public/js/app-performance.js

+ 12 - 8
controllers/performance.php

@@ -349,6 +349,7 @@ if($submit) {
 
 			$stage_key = "stage" . $test->challenge_stage . "Count";
 			$data[$key]->$stage_key++;
+			$total_data->{$stage_key}++;
 
 			if($test->pass) {
 				$data[$key]->passCount++;
@@ -426,13 +427,13 @@ if($submit) {
 		$chart = new Chart;
 		$chart->setType("pie");
 		$chart->setTitle("Tests by Stage $date_range_label");
-		$chart->setLabels(["On Entry", "While Browsing", "During Play", "On Exit", "No Challenge"]);
+		$chart->setLabels(["1 - On Entry", "2 - While Browsing", "3 - During Play", "4 - On Exit", "5 - No Challenge"]);
 		$chart->addDataset("Counts", [
-			array_sum(array_column($data, "stage1Count")),
-			array_sum(array_column($data, "stage2Count")),
-			array_sum(array_column($data, "stage3Count")),
-			array_sum(array_column($data, "stage4Count")),
-			array_sum(array_column($data, "stage5Count")),
+			$total_data->stage1Count,
+			$total_data->stage2Count,
+			$total_data->stage3Count,
+			$total_data->stage4Count,
+			$total_data->stage5Count,
 		], [
 			"background_colours" => [
 				"#4CAF50",
@@ -440,9 +441,12 @@ if($submit) {
 				"#FBC02D",
 				"#FFAB91",
 				"#F44336",
-			]
+			],
+			"datalabels" => [
+				"textAlign" => "center"
+			],
 		]);
-		$chart->setDatalabelSuffixes(" Test");
+		$chart->setDatalabelFormatterCallback("testsByStageFormatter");
 		$charts[] = $chart;
 
 

+ 19 - 1
public/js/app-performance.js

@@ -42,4 +42,22 @@ $(function() {
 		}
 	});
 
-});
+});
+
+function testsByStageFormatter(value, context) {
+	let dataset = context.dataset;
+	let data = dataset.data;
+	let total = data.reduce((total, value) => { return total + value; });
+	let percentage = Math.round((value * 10000) / total) / 100;
+
+	let suffix = "Test";
+	if(value != 1) {
+		suffix += "s";
+	}
+
+	let label = "Stage " + (context.dataIndex + 1) + "\n";
+	label += value + " " + suffix + "\n";
+	label += percentage + "%";
+
+	return label;
+}