From ba496ebf4115304de398f7868d11d679df1b312e Mon Sep 17 00:00:00 2001 From: Danny Adegeest Date: Tue, 2 May 2023 16:09:30 +0200 Subject: [PATCH] Add distributed option --- src/LarapexChart.php | 45 +++++++++++++++++--- stubs/resources/views/chart/script.blade.php | 2 +- tests/Feature/ChartsTest.php | 31 +++++++++++++- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/LarapexChart.php b/src/LarapexChart.php index 8d13834..e376e8b 100644 --- a/src/LarapexChart.php +++ b/src/LarapexChart.php @@ -27,7 +27,9 @@ class LarapexChart protected $height = 500; protected $width; protected $colors; - protected $horizontal; + protected $horizontal = false; + protected $distributed = false; + protected $bar; protected $xAxis; protected $grid; protected $markers; @@ -47,7 +49,7 @@ class LarapexChart public function __construct() { $this->id = substr(str_shuffle(str_repeat($x = $this->chartLetters, ceil(25 / strlen($x)))), 1, 25); - $this->horizontal = json_encode(['horizontal' => false]); + $this->bar = json_encode(['horizontal' => $this->horizontal, 'distributed' => $this->distributed]); $this->colors = json_encode(config('larapex-charts.colors')); $this->setXAxis([]); $this->grid = json_encode(['show' => false]); @@ -165,12 +167,24 @@ public function setColors(array $colors) :LarapexChart return $this; } - public function setHorizontal(bool $horizontal) :LarapexChart + public function setBar() :LarapexChart { - $this->horizontal = json_encode(['horizontal' => $horizontal]); + $this->bar = json_encode(['horizontal' => $this->horizontal, 'distributed' => $this->distributed]); return $this; } + public function setHorizontal(bool $horizontal): LarapexChart + { + $this->horizontal = $horizontal; + return $this; + } + + public function setDistributed(bool $distributed): LarapexChart + { + $this->distributed = $distributed; + return $this; + } + public function setTitle(string $title) :LarapexChart { $this->title = $title; @@ -401,6 +415,23 @@ public function horizontal() return $this->horizontal; } + /** + * @return false|string + */ + public function bar() + { + $this->setBar(); + return $this->bar; + } + + /** + * @return false|string + */ + public function distributed() + { + return $this->distributed; + } + /** * @return mixed */ @@ -485,7 +516,7 @@ public function toJson() 'sparkline' => $this->sparkline(), ], 'plotOptions' => [ - 'bar' => json_decode($this->horizontal()), + 'bar' => json_decode($this->bar()), ], 'colors' => json_decode($this->colors()), 'series' => json_decode($this->dataset()), @@ -524,13 +555,13 @@ public function toVue() :array 'chart' => [ 'height' => $this->height(), 'toolbar' => json_decode($this->toolbar()), - 'zoom' => json_decode($this->zoom()), + 'zoom' => json_decode($this->zoowm()), 'fontFamily' => json_decode($this->fontFamily()), 'foreColor' => $this->foreColor(), 'sparkline' => json_decode($this->sparkline()), ], 'plotOptions' => [ - 'bar' => json_decode($this->horizontal()), + 'bar' => json_decode($this->bar()), ], 'colors' => json_decode($this->colors()), 'dataLabels' => json_decode($this->dataLabels()), diff --git a/stubs/resources/views/chart/script.blade.php b/stubs/resources/views/chart/script.blade.php index 1272423..9fd00d1 100644 --- a/stubs/resources/views/chart/script.blade.php +++ b/stubs/resources/views/chart/script.blade.php @@ -12,7 +12,7 @@ sparkline: {!! $chart->sparkline() !!} }, plotOptions: { - bar: {!! $chart->horizontal() !!} + bar: {!! $chart->bar() !!} }, colors: {!! $chart->colors() !!}, series: {!! $chart->dataset() !!}, diff --git a/tests/Feature/ChartsTest.php b/tests/Feature/ChartsTest.php index 51190d5..5a2d6ea 100644 --- a/tests/Feature/ChartsTest.php +++ b/tests/Feature/ChartsTest.php @@ -179,10 +179,39 @@ public function it_tests_larapex_charts_can_render_horizontal_bar_chart() $this->assertEquals($chart->id(), $chart->container()['id']); $this->assertEquals($chart, $chart->script()['chart']); $this->assertEquals('bar', $chart->type()); - $chartHorizontalOrientation = json_decode($chart->horizontal(), 1)['horizontal']; + $chartHorizontalOrientation = json_decode($chart->bar(), 1)['horizontal']; $this->assertTrue($chartHorizontalOrientation); } + /** @test */ + public function it_tests_larapex_charts_can_render_distributed_bar_chart() + { + $chart = (new LarapexChart)->barChart() + ->setTitle('Net Profit') + ->setdistributed(true) + ->setXAxis(['Jan', 'Feb', 'Mar']) + ->setDataset([ + [ + 'name' => 'Company A', + 'data' => [500, 1000, 1900] + ], + [ + 'name' => 'Company B', + 'data' => [300, 900, 1400] + ], + [ + 'name' => 'Company C', + 'data' => [430, 245, 500] + ] + ]); + + $this->assertEquals($chart->id(), $chart->container()['id']); + $this->assertEquals($chart, $chart->script()['chart']); + $this->assertEquals('bar', $chart->type()); + $chartDistributed = json_decode($chart->bar(), 1)['distributed']; + $this->assertTrue($chartDistributed); + } + /** @test */ public function it_tests_larapex_charts_can_render_heatmap_chart() {