From 474c1fd94bc1b16af4373013cd60ae2d14dc116d Mon Sep 17 00:00:00 2001 From: sfinktah Date: Sat, 18 Mar 2023 16:28:24 +1100 Subject: [PATCH] added .setExtraOptions --- src/LarapexChart.php | 56 +++++++++++++++++------ src/Traits/ComplexChartDataAggregator.php | 11 +++-- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/LarapexChart.php b/src/LarapexChart.php index 8d13834..cd209e7 100644 --- a/src/LarapexChart.php +++ b/src/LarapexChart.php @@ -1,9 +1,11 @@ grid = json_encode([ - 'show' => true, - 'row' => [ - 'colors' => [$color, 'transparent'], - 'opacity' => $opacity, - ], - ]); + if (is_array($color)) { + $this->stroke = json_encode(array_replace_recursive($color)); + } + else { + $this->grid = json_encode([ + 'show' => true, + 'row' => [ + 'colors' => [$color, 'transparent'], + 'opacity' => $opacity, + ], + ]); + } return $this; } @@ -228,17 +236,22 @@ public function setMarkers($colors = [], $width = 4, $hoverSize = 7) :LarapexCha return $this; } - public function setStroke(int $width, array $colors = []) :LarapexChart + public function setStroke(mixed $width, array $colors = []) :LarapexChart { if(empty($colors)) { $colors = config('larapex-charts.colors'); } - $this->stroke = json_encode([ - 'show' => true, - 'width' => $width, - 'colors' => $colors, - ]); + if (is_array($width)) { + $this->stroke = json_encode(array_replace_recursive($width)); + } + else { + $this->stroke = json_encode([ + 'show' => true, + 'width' => $width, + 'colors' => $colors, + ]); + }; return $this; } @@ -261,6 +274,13 @@ public function setSparkline(bool $enabled = true): LarapexChart return $this; } + public function setExtraOptions(array $extraOptions): LarapexChart + { + $this->extraOptions = json_encode($extraOptions); + return $this; + } + + /* |-------------------------------------------------------------------------- | Getters @@ -268,11 +288,16 @@ public function setSparkline(bool $enabled = true): LarapexChart */ /** + * @deprecated unused * @param array $array * @return array|false|string */ public function transformLabels(array $array) { + if ($this->labelTransformer) { + $array = array_filter($array, $this->labelTransformer); + } + $stringArray = array_filter($array, function($string){ return "{$string}"; }); @@ -512,6 +537,8 @@ public function toJson() $options['stroke'] = json_decode($this->stroke()); } + $options = array_replace_recursive($options, json_decode($this->extraOptions, true)); + return response()->json([ 'id' => $this->id(), 'options' => $options, @@ -556,6 +583,9 @@ public function toVue() :array $options['stroke'] = json_decode($this->stroke()); } + if ($this->extraOptions) + $options = array_replace_recursive($options, json_decode($this->extraOptions, 1)); + return [ 'height' => $this->height(), 'width' => $this->width(), diff --git a/src/Traits/ComplexChartDataAggregator.php b/src/Traits/ComplexChartDataAggregator.php index 1c00c58..3fffd7f 100644 --- a/src/Traits/ComplexChartDataAggregator.php +++ b/src/Traits/ComplexChartDataAggregator.php @@ -4,17 +4,22 @@ trait ComplexChartDataAggregator { - public function addData(string $name, array $data) :self + public function addData(string $name, array $data, string $type = null) :self { $this->dataset = json_decode($this->dataset); - $this->dataset[] = [ + $newData = [ 'name' => $name, 'data' => $data ]; + if ($type) + $newData['type'] = $type; + + $this->dataset[] = $newData; + $this->dataset = json_encode($this->dataset); return $this; } -} \ No newline at end of file +}