From 0b333185a66bcf010297140fcddbd7fa6139671e Mon Sep 17 00:00:00 2001 From: Henning Zimmermann Date: Wed, 12 Jul 2023 14:14:17 +0200 Subject: [PATCH] - configuration of the minimum, maximum, tick amount of the y-axis --- src/LarapexChart.php | 40 ++++++++++ stubs/resources/views/chart/script.blade.php | 81 ++++++++++---------- 2 files changed, 81 insertions(+), 40 deletions(-) diff --git a/src/LarapexChart.php b/src/LarapexChart.php index fa6dc36..7be768a 100644 --- a/src/LarapexChart.php +++ b/src/LarapexChart.php @@ -41,6 +41,10 @@ class LarapexChart protected string $dataLabels; protected string $theme = 'light'; protected string $sparkline; + protected bool $yAxisShow = false; + protected ?int $yAxisMin = null; + protected ?int $yAxisMax = null; + protected ?int $yAxisTickAmount = null; private string $chartLetters = 'abcdefghijklmnopqrstuvwxyz'; /* @@ -201,6 +205,16 @@ public function setXAxis(array $categories) :LarapexChart return $this; } + public function setYAxis(int $min, int $max, ?int $tickAmount = null, bool $show = true) :self + { + $this->yAxisMin = $min; + $this->yAxisMax = $max; + $this->yAxisTickAmount = $tickAmount ?? $max; + $this->yAxisShow = $show; + + return $this; + } + public function setGrid($color = '#e5e5e5', $opacity = 0.1) :LarapexChart { $this->grid = json_encode([ @@ -429,6 +443,24 @@ public function showLegend(): string return $this->showLegend ? 'true' : 'false'; } + public function yAxis(): ?array + { + if ( + $this->yAxisMin === null || + $this->yAxisMax === null || + $this->yAxisTickAmount === null + ) { + return null; + } + + return [ + 'show' => $this->yAxisShow ? 'true' : 'false', + 'min' => $this->yAxisMin, + 'max' => $this->yAxisMax, + 'tickAmount' => $this->yAxisTickAmount + ]; + } + /* |-------------------------------------------------------------------------- | JSON Options Builder @@ -483,6 +515,10 @@ public function toJson(): \Illuminate\Http\JsonResponse $options['stroke'] = json_decode($this->stroke()); } + if($this->yAxis() !== null) { + $options['yaxis'] = $this->yAxis(); + } + return response()->json([ 'id' => $this->id(), 'options' => $options, @@ -540,6 +576,10 @@ public function toVue() :array $options['stroke'] = json_decode($this->stroke()); } + if($this->yAxis() !== null) { + $options['yaxis'] = $this->yAxis(); + } + return [ 'height' => $this->height(), 'width' => $this->width(), diff --git a/stubs/resources/views/chart/script.blade.php b/stubs/resources/views/chart/script.blade.php index 3e101ac..5180552 100644 --- a/stubs/resources/views/chart/script.blade.php +++ b/stubs/resources/views/chart/script.blade.php @@ -1,46 +1,47 @@