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

Add support to print data tables

Alan Lam пре 8 година
родитељ
комит
a6696f1503
1 измењених фајлова са 45 додато и 1 уклоњено
  1. 45 1
      engine/chart.php

+ 45 - 1
engine/chart.php

@@ -10,6 +10,7 @@ class Chart {
 	private $yAxes = [];
 
 	private $datalabelSuffixes = [];
+	public $print = false;
 
 	public function __construct() {
 		$this->id = bin2hex(random_bytes(8));
@@ -71,7 +72,7 @@ class Chart {
 		if($type) $dataset["type"] = $type;
 		if($datalabels) $dataset["datalabels"] = $datalabels;
 
-		$this->datasets[] = $dataset;
+		$this->datasets[] = (object) $dataset;
 
 	}
 
@@ -124,6 +125,49 @@ class Chart {
 
 	}
 
+	public function printDataTable() {
+		?>
+		<table class="mt-3 w-100 table table-condensed table-bordered">
+			<thead>
+				<tr>
+					<th></th>
+					<?php
+					foreach($this->labels as $label) {
+						?>
+						<th><?=htmlspecialchars($label)?></th>
+						<?php
+					}
+					?>
+				</tr>
+			</thead>
+			<tbody>
+			<?php
+			foreach($this->datasets as $index => $dataset) {
+
+				$suffix = "";
+				if($this->datalabelSuffixes) {
+					$suffix = $this->datalabelSuffixes[$index];
+				}
+
+				?>
+				<tr>
+					<td><?=htmlspecialchars($dataset->label)?></td>
+					<?php
+					foreach($dataset->data as $value) {
+						?>
+						<td class="text-right"><?=htmlspecialchars($value)?><?=htmlspecialchars($suffix)?></td>
+						<?php
+					}
+					?>
+				</tr>
+				<?php
+			}
+			?>
+			</tbody>
+		</table>
+		<?php
+	}
+
 }
 
 ?>