Added Area Chart component and model

This commit is contained in:
asantibanez
2020-10-20 10:43:04 -05:00
parent 8752a7eff0
commit 1d0755e249
6 changed files with 311 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
<div
class="w-full h-full"
x-data="{ ...areaChart() }"
x-init="drawChart(@this)"
x-on:draw-column-chart.window="drawChart(@this)"
>
<div wire:ignore x-ref="container"></div>
<script>
function areaChart() {
return {
chart: null,
drawChart(component) {
if (this.chart) {
this.chart.destroy()
}
var options = {
series: [{
name: component.get('areaChartModel.title'),
data: component.get('areaChartModel.data').map(item => item.value)
}],
chart: {
type: 'area',
height: '100%',
zoom: { enabled: false },
toolbar: { show: false },
animations: {
enabled: component.get('areaChartModel.animated') || false,
},
events: {
markerClick: function(event, chartContext, { dataPointIndex }) {
const onPointClickEventName = component.get('areaChartModel.onPointClickEventName')
if (!onPointClickEventName) {
return
}
const point = component.get('areaChartModel.data')[dataPointIndex]
component.call('onPointClick', point)
}
}
},
dataLabels: {
enabled: false
},
colors: [component.get('areaChartModel.color') || '#2E93fA'],
stroke: {
curve: 'straight'
},
title: {
text: component.get('areaChartModel.title'),
align: 'center'
},
labels: component.get('areaChartModel.data').map(item => item.title),
xaxis: component.get('areaChartModel.xAxis') || {},
yaxis: component.get('areaChartModel.yAxis') || {},
grid: {
padding: {
left: 0,
top: 0,
right: 0,
bottom: 0,
}
},
};
this.chart = new ApexCharts(this.$refs.container, options);
this.chart.render();
}
}
}
</script>
</div>

View File

@@ -0,0 +1,36 @@
<?php
namespace Asantibanez\LivewireCharts\Charts;
use Asantibanez\LivewireCharts\Models\AreaChartModel;
use Livewire\Component;
/**
* Class LivewireAreaChart
* @package Asantibanez\LivewireCharts\Charts
*/
class LivewireAreaChart extends Component
{
public $areaChartModel;
public function mount(AreaChartModel $areaChartModel)
{
$this->areaChartModel = $areaChartModel->toArray();
}
public function onPointClick($point)
{
$onPointClickEventName = data_get($this->areaChartModel, 'onPointClickEventName', null);
if ($onPointClickEventName === null) {
return;
}
$this->emit($onPointClickEventName, $point);
}
public function render()
{
return view('livewire-charts::livewire-area-chart');
}
}

View File

@@ -2,6 +2,7 @@
namespace Asantibanez\LivewireCharts;
use Asantibanez\LivewireCharts\Charts\LivewireAreaChart;
use Asantibanez\LivewireCharts\Charts\LivewireColumnChart;
use Asantibanez\LivewireCharts\Charts\LivewireLineChart;
use Asantibanez\LivewireCharts\Charts\LivewirePieChart;
@@ -26,6 +27,7 @@ class LivewireChartsServiceProvider extends ServiceProvider
Livewire::component('livewire-line-chart', LivewireLineChart::class);
Livewire::component('livewire-column-chart', LivewireColumnChart::class);
Livewire::component('livewire-pie-chart', LivewirePieChart::class);
Livewire::component('livewire-area-chart', LivewireAreaChart::class);
}
/**

View File

@@ -0,0 +1,101 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
/**
* Class AreaChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class AreaChartModel extends BaseChartModel
{
public $title;
public $animated;
public $color;
public $data;
public $onPointClickEventName;
public function __construct()
{
parent::__construct();
$this->title = '';
$this->animated = false;
$this->color = '#90cdf4';
$this->onPointClickEventName = null;
$this->data = collect();
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setAnimated($animated)
{
$this->animated = $animated;
return $this;
}
public function setColor($color)
{
$this->color = $color;
return $this;
}
public function addPoint($title, $value, $extras = [])
{
$this->data->push([
'title' => $title,
'value' => $value,
'extras' => $extras,
]);
return $this;
}
public function withOnPointClickEvent($onPointClickEventName)
{
$this->onPointClickEventName = $onPointClickEventName;
return $this;
}
public function toArray()
{
return array_merge(parent::toArray(), [
'title' => $this->title,
'animated' => $this->animated,
'color' => $this->color,
'onPointClickEventName' => $this->onPointClickEventName,
'data' => $this->data->toArray(),
]);
}
public function fromArray($array)
{
parent::fromArray($array);
$this->title = data_get($array, 'title', '');
$this->animated = data_get($array, 'animated', false);
$this->color = data_get($array, 'color', '#90cdf4');
$this->onPointClickEventName = data_get($array, 'onPointClickEventName', null);
$this->data = collect(data_get($array, 'data', []));
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
/**
* Class BaseChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class BaseChartModel
{
use HasAxisConfiguration;
public function __construct()
{
$this->setupAxis();
}
public function reactiveKey()
{
return md5(json_encode($this->toArray()));
}
public function toArray()
{
return [
'xAxis' => $this->xAxis,
'yAxis' => $this->yAxis,
];
}
public function fromArray($array)
{
$this->setupAxis($array);
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasAxisConfiguration
{
private $xAxis;
private $yAxis;
public function setXAxisVisible($visible)
{
data_set($this->xAxis, 'labels.show', $visible);
return $this;
}
public function setYAxisVisible($visible)
{
data_set($this->yAxis, 'show', $visible);
return $this;
}
protected function setupAxis($array = [])
{
$this->xAxis = data_get($array, 'xAxis', $this->defaultXAxis());
$this->yAxis = data_get($array, 'yAxis', $this->defaultYAxis());
}
private function defaultXAxis()
{
return [
'labels' => [
'show' => true,
],
];
}
private function defaultYAxis()
{
return [
'show' => true,
];
}
}