diff --git a/resources/views/livewire-area-chart.blade.php b/resources/views/livewire-area-chart.blade.php new file mode 100644 index 0000000..82f105a --- /dev/null +++ b/resources/views/livewire-area-chart.blade.php @@ -0,0 +1,87 @@ +
+
+ + +
+ diff --git a/src/Charts/LivewireAreaChart.php b/src/Charts/LivewireAreaChart.php new file mode 100755 index 0000000..c23a6a4 --- /dev/null +++ b/src/Charts/LivewireAreaChart.php @@ -0,0 +1,36 @@ +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'); + } +} diff --git a/src/LivewireChartsServiceProvider.php b/src/LivewireChartsServiceProvider.php index 724ee76..a2f3fbd 100755 --- a/src/LivewireChartsServiceProvider.php +++ b/src/LivewireChartsServiceProvider.php @@ -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); } /** diff --git a/src/Models/AreaChartModel.php b/src/Models/AreaChartModel.php new file mode 100644 index 0000000..0708c1f --- /dev/null +++ b/src/Models/AreaChartModel.php @@ -0,0 +1,101 @@ +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', [])); + } +} diff --git a/src/Models/BaseChartModel.php b/src/Models/BaseChartModel.php new file mode 100644 index 0000000..c77cd0b --- /dev/null +++ b/src/Models/BaseChartModel.php @@ -0,0 +1,36 @@ +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); + } +} diff --git a/src/Models/HasAxisConfiguration.php b/src/Models/HasAxisConfiguration.php new file mode 100644 index 0000000..be1671b --- /dev/null +++ b/src/Models/HasAxisConfiguration.php @@ -0,0 +1,49 @@ +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, + ]; + } +}