Skip to content

Add distributed option #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/LarapexChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion stubs/resources/views/chart/script.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
sparkline: {!! $chart->sparkline() !!}
},
plotOptions: {
bar: {!! $chart->horizontal() !!}
bar: {!! $chart->bar() !!}
},
colors: {!! $chart->colors() !!},
series: {!! $chart->dataset() !!},
Expand Down
31 changes: 30 additions & 1 deletion tests/Feature/ChartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down