Sfoglia il codice sorgente

Add support for x-axis options

Alan Lam 8 anni fa
parent
commit
dcd100b34e
1 ha cambiato i file con 41 aggiunte e 2 eliminazioni
  1. 41 2
      engine/chart.php

+ 41 - 2
engine/chart.php

@@ -7,6 +7,8 @@ class Chart {
 	private $type = "bar";
 	private $datasets = [];
 	private $labels = [];
+
+	private $xAxes = [];
 	private $yAxes = [];
 
 	private $datalabelSuffixes = [];
@@ -16,6 +18,36 @@ class Chart {
 		$this->id = bin2hex(random_bytes(8));
 	}
 
+	public function addXAxis($options = []) {
+
+		$type = $options["type"] ?? "linear";
+		$position = $options["position"] ?? "bottom";
+		$id = $options["id"] ?? "x-axis";
+		$grid_lines = $options["grid_lines"] ?? true;
+		$tick_config = $options["tick_config"] ?? null;
+		$scale_label_config = $options["scale_label_config"] ?? null;
+
+		$axis = [
+			"type" => $type,
+			"display" => true,
+			"position" => $position,
+			"id" => $id,
+			"gridLines" => [
+				"drawOnChartArea" => $grid_lines
+			],
+		];
+
+		if($tick_config) {
+			$axis["ticks"] = $tick_config;
+		}
+
+		if($scale_label_config) {
+			$axis["scaleLabel"] = $scale_label_config;
+		}
+
+		$this->xAxes[] = $axis;
+	}
+
 	public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) {
 
 		$axis = [
@@ -111,9 +143,16 @@ class Chart {
 			],
 		];
 
-		if($this->yAxes) {
+		if($this->xAxes || $this->yAxes) {
 			$object->options->scales = new stdClass;
-			$object->options->scales->yAxes = $this->yAxes;
+
+			if($this->xAxes) {
+				$object->options->scales->xAxes = $this->xAxes;
+			}
+
+			if($this->yAxes) {
+				$object->options->scales->yAxes = $this->yAxes;
+			}
 		}
 
 		if($this->datalabelSuffixes) {