diff --git a/resources/views/livewire-line-chart.blade.php b/resources/views/livewire-line-chart.blade.php index 826f518..d408c94 100644 --- a/resources/views/livewire-line-chart.blade.php +++ b/resources/views/livewire-line-chart.blade.php @@ -20,6 +20,7 @@ name: component.get('lineChartModel.title'), data: component.get('lineChartModel.data').map(item => item.value), }], + chart: { type: 'line', height: '100%', @@ -44,19 +45,24 @@ } } }, + dataLabels: { enabled: false }, + stroke: { - curve: 'straight' + curve: component.get('lineChartModel.stroke.curve') || 'straight', }, + title: { text: component.get('lineChartModel.title'), align: 'center' }, + xaxis: { categories: component.get('lineChartModel.data').map(item => item.title), }, + annotations: { points: component.get('lineChartModel.markers').map(item => { return { diff --git a/src/Models/BaseChartModel.php b/src/Models/BaseChartModel.php index c77cd0b..62aaec8 100644 --- a/src/Models/BaseChartModel.php +++ b/src/Models/BaseChartModel.php @@ -10,6 +10,7 @@ namespace Asantibanez\LivewireCharts\Models; class BaseChartModel { use HasAxisConfiguration; + use HasStrokeConfiguration; public function __construct() { @@ -26,11 +27,13 @@ class BaseChartModel return [ 'xAxis' => $this->xAxis, 'yAxis' => $this->yAxis, + 'stroke' => $this->stroke, ]; } public function fromArray($array) { $this->setupAxis($array); + $this->setupStroke($array); } } diff --git a/src/Models/HasAxisConfiguration.php b/src/Models/HasAxisConfiguration.php index be1671b..00fb40b 100644 --- a/src/Models/HasAxisConfiguration.php +++ b/src/Models/HasAxisConfiguration.php @@ -10,6 +10,12 @@ trait HasAxisConfiguration private $yAxis; + public function __construct() + { + $this->xAxis = $this->defaultXAxis(); + $this->yAxis = $this->defaultYAxis(); + } + public function setXAxisVisible($visible) { data_set($this->xAxis, 'labels.show', $visible); diff --git a/src/Models/HasStrokeConfiguration.php b/src/Models/HasStrokeConfiguration.php new file mode 100644 index 0000000..428c912 --- /dev/null +++ b/src/Models/HasStrokeConfiguration.php @@ -0,0 +1,41 @@ +stroke = $this->defaultStroke(); + } + + public function setSmoothCurve() + { + data_set($this->stroke, 'curve', 'smooth'); + + return $this; + } + + public function setStraightCurve() + { + data_set($this->stroke, 'curve', 'straight'); + + return $this; + } + + protected function setupStroke($array = []) + { + $this->stroke = data_get($array, 'stroke', $this->defaultStroke()); + } + + private function defaultStroke() + { + return [ + 'curve' => 'straight', + ]; + } +} diff --git a/src/Models/LineChartModel.php b/src/Models/LineChartModel.php index 7f82ad0..a1a217e 100644 --- a/src/Models/LineChartModel.php +++ b/src/Models/LineChartModel.php @@ -7,7 +7,7 @@ namespace Asantibanez\LivewireCharts\Models; * Class LineChartModel * @package Asantibanez\LivewireCharts\Models */ -class LineChartModel +class LineChartModel extends BaseChartModel { public $title; @@ -21,6 +21,8 @@ class LineChartModel public function __construct() { + parent::__construct(); + $this->title = ''; $this->animated = false; @@ -85,17 +87,19 @@ class LineChartModel public function toArray() { - return [ + return array_merge(parent::toArray(), [ 'title' => $this->title, 'animated' => $this->animated, 'onPointClickEventName' => $this->onPointClickEventName, 'data' => $this->data->toArray(), 'markers' => $this->markers->toArray(), - ]; + ]); } public function fromArray($array) { + parent::fromArray($array); + $this->title = data_get($array, 'title', ''); $this->animated = data_get($array, 'animated', false);