Skip to content
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

configuration of the minimum, maximum, tick amount of the y-axis #78

Closed
Closed
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
40 changes: 40 additions & 0 deletions src/LarapexChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/*
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
81 changes: 41 additions & 40 deletions stubs/resources/views/chart/script.blade.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
<script>
var options =
{
chart: {
type: '{!! $chart->type() !!}',
height: {!! $chart->height() !!},
width: '{!! $chart->width() !!}',
toolbar: {!! $chart->toolbar() !!},
zoom: {!! $chart->zoom() !!},
fontFamily: '{!! $chart->fontFamily() !!}',
foreColor: '{!! $chart->foreColor() !!}',
sparkline: {!! $chart->sparkline() !!},
@if($chart->stacked())
stacked: {!! $chart->stacked() !!},
@endif
},
plotOptions: {
bar: {!! $chart->horizontal() !!}
},
colors: {!! $chart->colors() !!},
series: {!! $chart->dataset() !!},
dataLabels: {!! $chart->dataLabels() !!},
@if($chart->labels())
{
chart: {
type: '{!! $chart->type() !!}',
height: {!! $chart->height() !!},
width: '{!! $chart->width() !!}',
toolbar: {!! $chart->toolbar() !!},
zoom: {!! $chart->zoom() !!},
fontFamily: '{!! $chart->fontFamily() !!}',
foreColor: '{!! $chart->foreColor() !!}',
sparkline: {!! $chart->sparkline() !!},
@if($chart->stacked())
stacked: {!! $chart->stacked() !!},
@endif
},
plotOptions: {
bar: {!! $chart->horizontal() !!}
},
colors: {!! $chart->colors() !!},
series: {!! $chart->dataset() !!},
dataLabels: {!! $chart->dataLabels() !!},
@if($chart->labels())
labels: {!! json_encode($chart->labels(), true) !!},
@endif
title: {
text: "{!! $chart->title() !!}"
},
subtitle: {
text: '{!! $chart->subtitle() !!}',
align: '{!! $chart->subtitlePosition() !!}'
},
xaxis: {
categories: {!! $chart->xAxis() !!}
},
grid: {!! $chart->grid() !!},
markers: {!! $chart->markers() !!},
@if($chart->stroke())
stroke: {!! $chart->stroke() !!},
@endif
legend: {
show: {!! $chart->showLegend() !!}
}
@endif
title: {
text: "{!! $chart->title() !!}"
},
subtitle: {
text: '{!! $chart->subtitle() !!}',
align: '{!! $chart->subtitlePosition() !!}'
},
xaxis: {
categories: {!! $chart->xAxis() !!}
},
yaxis: {!! json_encode($chart->yAxis(), true) !!},
grid: {!! $chart->grid() !!},
markers: {!! $chart->markers() !!},
@if($chart->stroke())
stroke: {!! $chart->stroke() !!},
@endif
legend: {
show: {!! $chart->showLegend() !!}
}
}

var chart = new ApexCharts(document.querySelector("#{!! $chart->id() !!}"), options);
Expand Down
Loading