|
|
@@ -315,7 +315,7 @@ class Chart {
|
|
|
<?php
|
|
|
}
|
|
|
|
|
|
- public function printDataTable(): void {
|
|
|
+ public function getDataTableData(): array {
|
|
|
$row_labels = $this->printLabels ?? $this->labels;
|
|
|
$print_total_row = $this->printTotalColumn;
|
|
|
$print_percentage_column = $this->printPercentages;
|
|
|
@@ -326,20 +326,103 @@ class Chart {
|
|
|
|
|
|
$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">
|
|
|
<thead>
|
|
|
<tr>
|
|
|
- <th>Name</th>
|
|
|
<?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
|
|
|
}
|
|
|
?>
|
|
|
@@ -348,55 +431,20 @@ class Chart {
|
|
|
<tbody>
|
|
|
<?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>
|
|
|
- <td><?=htmlspecialchars($row_labels[$row_index])?></td>
|
|
|
<?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>
|
|
|
<?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>
|
|
|
<?php
|
|
|
@@ -404,30 +452,15 @@ class Chart {
|
|
|
?>
|
|
|
</tbody>
|
|
|
<?php
|
|
|
- if($print_total_row) {
|
|
|
+ if($footer_row) {
|
|
|
?>
|
|
|
<tfoot>
|
|
|
<tr>
|
|
|
- <th><?=htmlspecialchars(__("Total"))?></th>
|
|
|
<?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
|
|
|
}
|
|
|
?>
|
|
|
@@ -458,11 +491,7 @@ class Chart {
|
|
|
|
|
|
// Presentation methods
|
|
|
public function addChart(Slide $slide, string $path) {
|
|
|
-
|
|
|
$chart_offset_y = 100;
|
|
|
- if($this->print) {
|
|
|
- $chart_offset_y = 10;
|
|
|
- }
|
|
|
|
|
|
$shape = $slide->createDrawingShape();
|
|
|
$shape->setName("Chart")
|
|
|
@@ -481,112 +510,74 @@ class Chart {
|
|
|
* @param int $offset_y
|
|
|
* @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)
|
|
|
->setOffsetX($offset_x)
|
|
|
->setOffsetY($offset_y);
|
|
|
+
|
|
|
// Create header row
|
|
|
- $row = $shape->createRow();
|
|
|
+ $row = $table->createRow();
|
|
|
$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->getActiveParagraph()->getAlignment()
|
|
|
->setMarginBottom(4)
|
|
|
->setMarginLeft(4)
|
|
|
->setMarginRight(4)
|
|
|
->setMarginTop(4);
|
|
|
- $cell->createTextRun($label)
|
|
|
+ $cell->createTextRun($text)
|
|
|
->getFont()->setBold(true);
|
|
|
}
|
|
|
|
|
|
$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()
|
|
|
- ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
|
|
|
+ ->setHorizontal($alignment)
|
|
|
->setMarginBottom(4)
|
|
|
->setMarginLeft(4)
|
|
|
->setMarginRight(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->getActiveParagraph()->getAlignment()
|
|
|
+ ->setHorizontal($alignment)
|
|
|
->setMarginBottom(4)
|
|
|
->setMarginLeft(4)
|
|
|
->setMarginRight(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);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
|