Преглед изворни кода

Flip axes for tables in PPTX export; factorise table data generation

Alan Lam пре 1 година
родитељ
комит
c40f7dbe32
2 измењених фајлова са 143 додато и 145 уклоњено
  1. 10 3
      controllers/reports_presentation.php
  2. 133 142
      engine/chart.php

+ 10 - 3
controllers/reports_presentation.php

@@ -156,6 +156,8 @@ $create_summary_slide = static function(array $options) use ($presentation, $cha
 	$run = $shape->createTextRun($count_text);
 	$run = $shape->createTextRun($count_text);
 	$run->getFont()->setBold(true)->setSize(16);
 	$run->getFont()->setBold(true)->setSize(16);
 
 
+	$y = 220;
+
 	if($has_stages) {
 	if($has_stages) {
 		// Tests by stage
 		// Tests by stage
 
 
@@ -164,9 +166,11 @@ $create_summary_slide = static function(array $options) use ($presentation, $cha
 			if(stripos($chart->getTitle(), "Tests by Stage") === false) {
 			if(stripos($chart->getTitle(), "Tests by Stage") === false) {
 				continue;
 				continue;
 			}
 			}
-			$chart->addDataTable($slide, 30, 260);
+			$chart->addDataTable($slide, 30, $y);
 			break;
 			break;
 		}
 		}
+
+		$y += 180;
 	}
 	}
 
 
 	// Totals by period
 	// Totals by period
@@ -174,7 +178,7 @@ $create_summary_slide = static function(array $options) use ($presentation, $cha
 	/** @var Chart $chart */
 	/** @var Chart $chart */
 	foreach($charts as $chart) {
 	foreach($charts as $chart) {
 		if(stripos($chart->getTitle(), $chart_prefix) !== false) {
 		if(stripos($chart->getTitle(), $chart_prefix) !== false) {
-			$chart->addDataTable($slide, 30, 360);
+			$chart->addDataTable($slide, 30, $y);
 			break;
 			break;
 		}
 		}
 	}
 	}
@@ -206,6 +210,7 @@ $names = [];
 $charts_b64 = http::value("charts_b64");
 $charts_b64 = http::value("charts_b64");
 foreach($charts_b64 as $index => $b64) {
 foreach($charts_b64 as $index => $b64) {
 
 
+	/** @var Chart $chart */
 	$chart = $charts[$index] ?? null;
 	$chart = $charts[$index] ?? null;
 	if(!$chart) {
 	if(!$chart) {
 		continue;
 		continue;
@@ -230,7 +235,9 @@ foreach($charts_b64 as $index => $b64) {
 
 
 	// Write data table
 	// Write data table
 	if($chart->print) {
 	if($chart->print) {
-		$chart->addDataTable($slide);
+		$slide = $presentation->createSlide();
+
+		$chart->addDataTable($slide, 30, 30);
 	}
 	}
 }
 }
 
 

+ 133 - 142
engine/chart.php

@@ -315,7 +315,7 @@ class Chart {
 		<?php
 		<?php
 	}
 	}
 
 
-	public function printDataTable(): void {
+	public function getDataTableData(): array {
 		$row_labels = $this->printLabels ?? $this->labels;
 		$row_labels = $this->printLabels ?? $this->labels;
 		$print_total_row = $this->printTotalColumn;
 		$print_total_row = $this->printTotalColumn;
 		$print_percentage_column = $this->printPercentages;
 		$print_percentage_column = $this->printPercentages;
@@ -326,20 +326,103 @@ class Chart {
 
 
 		$total_count = array_sum($datasets[0]->data);
 		$total_count = array_sum($datasets[0]->data);
 
 
+		$header_row = [__("Name")];
+		$footer_row = [];
+
+		foreach($datasets as $dataset) {
+			$header_row[] = $dataset->label;
+		}
+		if($print_percentage_column) {
+			$header_row[] = __("Percentage");
+		}
+
+		$rows = [];
+
+		$data = [];
+
+		foreach($datasets as $column_index => $dataset) {
+			foreach($dataset->data as $row_index => $value) {
+				$data[$row_index][$column_index] = $value;
+			}
+		}
+
+		if($this->tableSorter) {
+			usort($data, $this->tableSorter);
+		}
+
+		foreach($data as $row_index => $columns) {
+			$row = [];
+			$row[] = $row_labels[$row_index];
+
+			foreach($columns as $column_index => $value) {
+				$suffix = $suffixes[$column_index] ?? "";
+
+				if($value === null) {
+					$row[] = __("N/A");
+				}
+				else {
+					$row[] = number_format($value) . $suffix;
+				}
+			}
+
+			if($print_percentage_column) {
+				$value = $columns[0] ?? 0;
+				if($total_count > 0) {
+					$percentage = floor(($value / $total_count) * 10000) / 100;
+				}
+				else {
+					$percentage = 0;
+				}
+
+				$row[] =number_format($percentage, 2) . "%";
+			}
+
+			$rows[] = $row;
+		}
+
+		if($print_total_row) {
+			$footer_row[] = __("Total");
+
+			foreach($datasets as $dataset) {
+				$label = $dataset->label;
+
+				$is_percentage = strpos($label, "%");
+				if($is_percentage && $this->numeratorColumnIndex !== null && $this->denominatorColumnIndex !== null) {
+					$numerator_sum = array_sum($datasets[$this->numeratorColumnIndex]->data);
+					$denominator_sum = array_sum($datasets[$this->denominatorColumnIndex]->data);
+
+					$text = number_format(($numerator_sum * 100) / $denominator_sum, 2) . "%";
+				}
+				else {
+					$text = number_format(array_sum($dataset->data));
+				}
+
+				$footer_row[] = $text;
+			}
+		}
+
+		return [
+			"headerRow" => $header_row,
+			"rows" => $rows,
+			"footerRow" => $footer_row,
+		];
+
+	}
+
+	public function printDataTable(): void {
+		$data = $this->getDataTableData();
+
+		$header_row = $data["headerRow"];
+		$rows = $data["rows"];
+		$footer_row = $data["footerRow"];
 		?>
 		?>
 		<table class="mt-3 w-100 table table-sm small table-bordered">
 		<table class="mt-3 w-100 table table-sm small table-bordered">
 			<thead>
 			<thead>
 				<tr>
 				<tr>
-					<th>Name</th>
 					<?php
 					<?php
-					foreach($datasets as $dataset) {
-						?>
-						<th><?=htmlspecialchars($dataset->label)?></th>
-						<?php
-					}
-					if($print_percentage_column) {
+					foreach($header_row as $text) {
 						?>
 						?>
-						<th><?=htmlspecialchars(__("Percentage"))?></th>
+						<th><?=htmlspecialchars($text)?></th>
 						<?php
 						<?php
 					}
 					}
 					?>
 					?>
@@ -348,55 +431,20 @@ class Chart {
 			<tbody>
 			<tbody>
 			<?php
 			<?php
 
 
-			$data = [];
-
-			foreach($datasets as $column_index => $dataset) {
-				foreach($dataset->data as $row_index => $value) {
-					$data[$row_index][$column_index] = $value;
-				}
-			}
-
-			if($this->tableSorter) {
-				usort($data, $this->tableSorter);
-			}
-
-			foreach($data as $row_index => $columns) {
+			foreach($rows as $row) {
 				?>
 				?>
 				<tr>
 				<tr>
-					<td><?=htmlspecialchars($row_labels[$row_index])?></td>
 					<?php
 					<?php
-					foreach($columns as $column_index => $value) {
-						$suffix = $suffixes[$column_index] ?? "";
+					foreach($row as $index => $text) {
+						$classes = [];
+						$classes[] = $index === 0 ? "" : "text-right";
+						$classes[] = $text === __("N/A") ? "text-muted small" : ""
 						?>
 						?>
-						<td class="text-right">
-							<?php
-							if($value === null) {
-								?>
-								<small class="text-muted"><?=htmlspecialchars(__("N/A"))?></small>
-								<?php
-								continue;
-							}
-							?>
-							<?=number_format($value)?><?=htmlspecialchars($suffix)?>
+						<td class="<?=implode(" ", $classes)?>">
+							<?=htmlspecialchars($text)?>
 						</td>
 						</td>
 						<?php
 						<?php
 					}
 					}
-
-					if($print_percentage_column) {
-						$value = $columns[0] ?? 0;
-						if($total_count > 0) {
-							$percentage = floor(($value / $total_count) * 10000) / 100;
-						}
-						else {
-							$percentage = 0;
-						}
-						?>
-						<td class="text-right">
-							<?=number_format($percentage, 2)?>%
-						</td>
-						<?php
-					}
-
 					?>
 					?>
 				</tr>
 				</tr>
 				<?php
 				<?php
@@ -404,30 +452,15 @@ class Chart {
 			?>
 			?>
 			</tbody>
 			</tbody>
 			<?php
 			<?php
-			if($print_total_row) {
+			if($footer_row) {
 				?>
 				?>
 				<tfoot>
 				<tfoot>
 				<tr>
 				<tr>
-					<th><?=htmlspecialchars(__("Total"))?></th>
 					<?php
 					<?php
-					foreach($datasets as $dataset) {
-						$label = $dataset->label;
-
-						$is_percentage = strpos($label, "%");
-						if($is_percentage && $this->numeratorColumnIndex !== null && $this->denominatorColumnIndex !== null) {
-							$numerator_sum = array_sum($datasets[$this->numeratorColumnIndex]->data);
-							$denominator_sum = array_sum($datasets[$this->denominatorColumnIndex]->data);
-
-							$text = number_format(($numerator_sum * 100) / $denominator_sum, 2) . "%";
-						}
-						else {
-							$text = number_format(array_sum($dataset->data));
-						}
-
+					foreach($footer_row as $index => $text) {
+						$class = $index === 0 ? "" : "text-right";
 						?>
 						?>
-						<td class="text-right">
-							<?=$text?>
-						</td>
+						<th class="<?=$class?>"><?=htmlspecialchars($text)?></th>
 						<?php
 						<?php
 					}
 					}
 					?>
 					?>
@@ -458,11 +491,7 @@ class Chart {
 
 
 	// Presentation methods
 	// Presentation methods
 	public function addChart(Slide $slide, string $path) {
 	public function addChart(Slide $slide, string $path) {
-
 		$chart_offset_y = 100;
 		$chart_offset_y = 100;
-		if($this->print) {
-			$chart_offset_y = 10;
-		}
 
 
 		$shape = $slide->createDrawingShape();
 		$shape = $slide->createDrawingShape();
 		$shape->setName("Chart")
 		$shape->setName("Chart")
@@ -481,112 +510,74 @@ class Chart {
 	 * @param int $offset_y
 	 * @param int $offset_y
 	 * @throws Exception
 	 * @throws Exception
 	 */
 	 */
-	public function addDataTable(Slide $slide, int $offset_x = 30, int $offset_y = 540) {
+	public function addDataTable(Slide $slide, int $offset_x = 30, int $offset_y = 540): void {
+		$data = $this->getDataTableData();
 
 
-		$labels = $this->getLabels();
-		$datasets = array_merge($this->getDatasets(), $this->getPrintDatasets());
+		$header_row = $data["headerRow"];
+		$rows = $data["rows"];
+		$footer_row = $data["footerRow"];
 
 
-		$column_count = 1 + count($labels);
+		$column_count = count($header_row);
 
 
-		// Table
-		if($this->printTotalColumn) ++$column_count;
-
-		$shape = $slide->createTableShape($column_count)
+		$table = $slide->createTableShape($column_count)
 			->setWidth(900)
 			->setWidth(900)
 			->setOffsetX($offset_x)
 			->setOffsetX($offset_x)
 			->setOffsetY($offset_y);
 			->setOffsetY($offset_y);
+
 		// Create header row
 		// Create header row
-		$row = $shape->createRow();
+		$row = $table->createRow();
 		$row->setHeight(20);
 		$row->setHeight(20);
-		$cell = $row->nextCell();
 
 
-		$labels = $this->getPrintLabels() ?? $this->getLabels();
-
-		if($this->printTotalColumn) $labels[] = "Total";
-
-		foreach($labels as $label) {
+		foreach($header_row as $text) {
 			$cell = $row->nextCell();
 			$cell = $row->nextCell();
 			$cell->getActiveParagraph()->getAlignment()
 			$cell->getActiveParagraph()->getAlignment()
 				->setMarginBottom(4)
 				->setMarginBottom(4)
 				->setMarginLeft(4)
 				->setMarginLeft(4)
 				->setMarginRight(4)
 				->setMarginRight(4)
 				->setMarginTop(4);
 				->setMarginTop(4);
-			$cell->createTextRun($label)
+			$cell->createTextRun($text)
 				->getFont()->setBold(true);
 				->getFont()->setBold(true);
 		}
 		}
 
 
 		$suffixes = $this->getPrintSuffixes() ?? $this->getDatalabelSuffixes();
 		$suffixes = $this->getPrintSuffixes() ?? $this->getDatalabelSuffixes();
 
 
-		// Datasets
-		foreach($datasets as $index => $dataset) {
-			$label = $dataset->label;
-			$data = $dataset->data;
-			$print_data = array_slice($data, 0);
-
-			$total = array_sum($data);
-			$suffix = $suffixes[$index] ?? "";
+		// Rows
+		foreach($rows as $row) {
+			$table_row = $table->createRow();
+			$table_row->setHeight(20);
 
 
-			if($this->printTotalColumn) {
-				$print_data[] = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
-			}
+			foreach($row as $index => $text) {
+				$bold = $index === 0;
+				$alignment = ($index === 0) ? Alignment::HORIZONTAL_LEFT : Alignment::HORIZONTAL_RIGHT;
 
 
-			$row = $shape->createRow();
-			$row->setHeight(20);
-			$cell = $row->nextCell();
-			$cell->getActiveParagraph()->getAlignment()
-				->setMarginBottom(4)
-				->setMarginLeft(4)
-				->setMarginRight(4)
-				->setMarginTop(4);
-			$cell->createTextRun($label)
-				->getFont()->setBold(true);
-			foreach($print_data as $value) {
-				$string = strlen($value) == 0 ? "" : $value . $suffix;
-
-				$cell = $row->nextCell();
+				$cell = $table_row->nextCell();
 				$cell->getActiveParagraph()->getAlignment()
 				$cell->getActiveParagraph()->getAlignment()
-					->setHorizontal(Alignment::HORIZONTAL_RIGHT)
+					->setHorizontal($alignment)
 					->setMarginBottom(4)
 					->setMarginBottom(4)
 					->setMarginLeft(4)
 					->setMarginLeft(4)
 					->setMarginRight(4)
 					->setMarginRight(4)
 					->setMarginTop(4);
 					->setMarginTop(4);
-				$cell->createTextRun($string);
+				$cell->createTextRun($text)->getFont()->setBold($bold);
 			}
 			}
+		}
 
 
-			if($this->printPercentages) {
-
-				$percentages = [];
-				foreach($data as $value) {
-					if($total == 0) {
-						$percentages[] = 0;
-						continue;
-					}
+		// Footer
+		if($footer_row) {
+			$row = $table->createRow();
+			$row->setHeight(20);
 
 
-					$percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
-				}
+			foreach($footer_row as $index => $text) {
+				$alignment = ($index === 0) ? Alignment::HORIZONTAL_LEFT : Alignment::HORIZONTAL_RIGHT;
 
 
-				$row = $shape->createRow();
-				$row->setHeight(20);
 				$cell = $row->nextCell();
 				$cell = $row->nextCell();
 				$cell->getActiveParagraph()->getAlignment()
 				$cell->getActiveParagraph()->getAlignment()
+					->setHorizontal($alignment)
 					->setMarginBottom(4)
 					->setMarginBottom(4)
 					->setMarginLeft(4)
 					->setMarginLeft(4)
 					->setMarginRight(4)
 					->setMarginRight(4)
 					->setMarginTop(4);
 					->setMarginTop(4);
-				$cell->createTextRun("Percentage")
-					->getFont()->setBold(true);
-				foreach($percentages as $string) {
-					$cell = $row->nextCell();
-					$cell->getActiveParagraph()->getAlignment()
-						->setHorizontal(Alignment::HORIZONTAL_RIGHT)
-						->setMarginBottom(4)
-						->setMarginLeft(4)
-						->setMarginRight(4)
-						->setMarginTop(4);
-					$cell->createTextRun($string);
-				}
+				$cell->createTextRun($text)->getFont()->setBold(true);
 			}
 			}
-
 		}
 		}
 	}
 	}