Alan Lam 2 лет назад
Родитель
Сommit
4ab3e05e57
2 измененных файлов с 67 добавлено и 58 удалено
  1. 66 58
      engine/chart.php
  2. 1 0
      engine/chart_generator.php

+ 66 - 58
engine/chart.php

@@ -289,102 +289,110 @@ class Chart {
 		<?php
 	}
 
-	public function printDataTable() {
+	public function printDataTable(): void {
+		$row_labels = $this->printLabels ?? $this->labels;
+		$print_total_row = $this->printTotalColumn;
+		$print_percentage_column = $this->printPercentages;
+
+		$suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
+
+		$datasets = array_merge($this->datasets, $this->printDatasets);
+
+		$total_count = array_sum($datasets[0]->data);
+
 		?>
 		<table class="mt-3 w-100 table table-sm small table-bordered">
 			<thead>
 				<tr>
-					<th></th>
+					<th>Name</th>
 					<?php
-
-					$labels = $this->printLabels ?? $this->labels;
-
-					foreach($labels as $label) {
+					foreach($datasets as $dataset) {
 						?>
-						<th><?=htmlspecialchars($label)?></th>
+						<th><?=htmlspecialchars($dataset->label)?></th>
 						<?php
 					}
-
-					if($this->printTotalColumn) {
+					if($print_percentage_column) {
 						?>
-						<th>Total</th>
+						<th><?=htmlspecialchars(__("Percentage"))?></th>
 						<?php
 					}
-
 					?>
 				</tr>
 			</thead>
 			<tbody>
 			<?php
 
-			$datasets = array_merge($this->datasets, $this->printDatasets);
-
-			foreach($datasets as $index => $dataset) {
-
-				$suffix = "";
-
-				$suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
+			$data = [];
 
-				if($suffixes) {
-					$suffix = $suffixes[$index] ?? "";
+			foreach($datasets as $column_index => $dataset) {
+				foreach($dataset->data as $row_index => $value) {
+					$data[$row_index][$column_index] = $value;
 				}
+			}
 
-				$data = $dataset->data;
-
+			foreach($data as $row_index => $columns) {
 				?>
 				<tr>
-					<td><?=htmlspecialchars($dataset->label)?></td>
+					<td><?=htmlspecialchars($row_labels[$row_index])?></td>
 					<?php
-					foreach($data as $value) {
+					foreach($columns as $column_index => $value) {
+						$suffix = $suffixes[$column_index] ?? "";
 						?>
 						<td class="text-right">
-                            <?php
-                            if($value === null) {
-                                ?>
-                                <small class="text-muted"><?=htmlspecialchars(__("N/A"))?></small>
-                                <?php
-                                continue;
-                            }
-                            ?>
-                            <?=number_format($value)?><?=htmlspecialchars($suffix)?>
-                        </td>
+							<?php
+							if($value === null) {
+								?>
+								<small class="text-muted"><?=htmlspecialchars(__("N/A"))?></small>
+								<?php
+								continue;
+							}
+							?>
+							<?=number_format($value)?><?=htmlspecialchars($suffix)?>
+						</td>
 						<?php
 					}
 
-					if($this->printTotalColumn) {
-
-						$string = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
-
+					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"><?=htmlspecialchars($string)?></td>
+						<td class="text-right">
+							<?=number_format($percentage, 2)?>%
+						</td>
 						<?php
 					}
+
 					?>
 				</tr>
 				<?php
-
-				if($this->printPercentages) {
-					$total = array_sum($data);
-					$percentages = [];
-					foreach($data as $value) {
-						$percentages[] = ($total == 0) ? "0%" : (floor(($value * 10000) / $total) / 100) . "%";
+			}
+			?>
+			</tbody>
+			<?php
+			if($print_total_row) {
+				?>
+				<tfoot>
+				<tr>
+					<th><?=htmlspecialchars(__("Total"))?></th>
+					<?php
+					foreach($datasets as $dataset) {
+						?>
+						<td class="text-right">
+							<?=number_format(array_sum($dataset->data))?>
+						</td>
+						<?php
 					}
 					?>
-					<tr>
-						<td>Percentage</td>
-						<?php
-						foreach($percentages as $string) {
-							?>
-							<td class="text-right"><?=$string?></td>
-							<?php
-						}
-						?>
-					</tr>
-					<?php
-				}
+				</tr>
+				</tfoot>
+				<?php
 			}
 			?>
-			</tbody>
 		</table>
 		<?php
 	}

+ 1 - 0
engine/chart_generator.php

@@ -823,6 +823,7 @@ class ChartGenerator {
 			]);
 		}
 		$chart->setDatalabelSuffixes(...array_fill(0, count($this->periodKeys), "%"));
+		$chart->print = true;
 		return $chart;
 	}