diff --git a/src/Models/AreaChartModel.php b/src/Models/AreaChartModel.php index eb25d5c..e78d184 100644 --- a/src/Models/AreaChartModel.php +++ b/src/Models/AreaChartModel.php @@ -9,8 +9,6 @@ namespace Asantibanez\LivewireCharts\Models; */ class AreaChartModel extends BaseChartModel { - public $title; - public $color; public $data; @@ -21,8 +19,6 @@ class AreaChartModel extends BaseChartModel { parent::__construct(); - $this->title = ''; - $this->color = '#90cdf4'; $this->onPointClickEventName = null; @@ -30,13 +26,6 @@ class AreaChartModel extends BaseChartModel $this->data = collect(); } - public function setTitle($title) - { - $this->title = $title; - - return $this; - } - public function setColor($color) { $this->color = $color; @@ -65,7 +54,6 @@ class AreaChartModel extends BaseChartModel public function toArray() { return array_merge(parent::toArray(), [ - 'title' => $this->title, 'color' => $this->color, 'onPointClickEventName' => $this->onPointClickEventName, 'data' => $this->data->toArray(), @@ -76,8 +64,6 @@ class AreaChartModel extends BaseChartModel { parent::fromArray($array); - $this->title = data_get($array, 'title', ''); - $this->color = data_get($array, 'color', '#90cdf4'); $this->onPointClickEventName = data_get($array, 'onPointClickEventName', null); diff --git a/src/Models/BaseChartModel.php b/src/Models/BaseChartModel.php index 761507c..72066f1 100644 --- a/src/Models/BaseChartModel.php +++ b/src/Models/BaseChartModel.php @@ -9,6 +9,7 @@ namespace Asantibanez\LivewireCharts\Models; */ class BaseChartModel { + use HasTitle; use HasAnimation; use HasAxis; use HasStroke; @@ -16,6 +17,7 @@ class BaseChartModel public function __construct() { + $this->initTitle(); $this->initAnimation(); $this->initAxis(); $this->initStroke(); @@ -30,6 +32,7 @@ class BaseChartModel public function toArray() { return array_merge( + $this->titleToArray(), $this->animationToArray(), $this->axisToArray(), $this->strokeToArray(), @@ -39,6 +42,7 @@ class BaseChartModel public function fromArray($array) { + $this->titleFromArray($array); $this->animationFromArray($array); $this->axisFromArray($array); $this->strokeFromArray($array); diff --git a/src/Models/ColumnChartModel.php b/src/Models/ColumnChartModel.php index 498c18f..1bbdfb9 100644 --- a/src/Models/ColumnChartModel.php +++ b/src/Models/ColumnChartModel.php @@ -9,8 +9,6 @@ namespace Asantibanez\LivewireCharts\Models; */ class ColumnChartModel extends BaseChartModel { - public $title; - public $opacity; public $onColumnClickEventName; @@ -21,8 +19,6 @@ class ColumnChartModel extends BaseChartModel { parent::__construct(); - $this->title = ''; - $this->onColumnClickEventName = null; $this->opacity = 0.5; @@ -30,13 +26,6 @@ class ColumnChartModel extends BaseChartModel $this->data = collect(); } - public function setTitle($title) - { - $this->title = $title; - - return $this; - } - public function setOpacity($opacity) { $this->opacity = $opacity; @@ -66,7 +55,6 @@ class ColumnChartModel extends BaseChartModel public function toArray() { return array_merge(parent::toArray(), [ - 'title' => $this->title, 'onColumnClickEventName' => $this->onColumnClickEventName, 'opacity' => $this->opacity, 'data' => $this->data->toArray(), @@ -77,8 +65,6 @@ class ColumnChartModel extends BaseChartModel { parent::fromArray($array); - $this->title = data_get($array, 'title', ''); - $this->onColumnClickEventName = data_get($array, 'onColumnClickEventName', null); $this->opacity = data_get($array, 'opacity', 0.5); diff --git a/src/Models/HasTitle.php b/src/Models/HasTitle.php new file mode 100644 index 0000000..e13fc9f --- /dev/null +++ b/src/Models/HasTitle.php @@ -0,0 +1,39 @@ +title = $title; + + return $this; + } + + protected function initTitle() + { + $this->title = $this->defaultTitle(); + } + + private function defaultTitle() + { + return ''; + } + + protected function titleFromArray($array) + { + $this->title = data_get($array, 'title', $this->defaultTitle()); + } + + protected function titleToArray() + { + return [ + 'title' => $this->title, + ]; + } +} diff --git a/src/Models/LineChartModel.php b/src/Models/LineChartModel.php index 6439321..1a4fa8c 100644 --- a/src/Models/LineChartModel.php +++ b/src/Models/LineChartModel.php @@ -8,13 +8,10 @@ use Illuminate\Support\Collection; /** * Class LineChartModel * @package Asantibanez\LivewireCharts\Models - * @property string $title * @property boolean $isMultiLine */ class LineChartModel extends BaseChartModel { - public $title; - public $isMultiLine; public $data; @@ -27,8 +24,6 @@ class LineChartModel extends BaseChartModel { parent::__construct(); - $this->title = ''; - $this->isMultiLine = false; $this->onPointClickEventName = null; @@ -56,13 +51,6 @@ class LineChartModel extends BaseChartModel return $this; } - public function setTitle($title) - { - $this->title = $title; - - return $this; - } - public function addPoint($title, $value, $extras = []) { $this->data->push([ @@ -119,7 +107,6 @@ class LineChartModel extends BaseChartModel public function toArray() { return array_merge(parent::toArray(), [ - 'title' => $this->title, 'isMultiLine' => $this->isMultiLine, 'onPointClickEventName' => $this->onPointClickEventName, 'data' => $this->data->toArray(), @@ -131,8 +118,6 @@ class LineChartModel extends BaseChartModel { parent::fromArray($array); - $this->title = data_get($array, 'title', ''); - $this->isMultiLine = data_get($array, 'isMultiLine', false); $this->onPointClickEventName = data_get($array, 'onPointClickEventName', null); diff --git a/src/Models/PieChartModel.php b/src/Models/PieChartModel.php index 858079d..a9a1d88 100644 --- a/src/Models/PieChartModel.php +++ b/src/Models/PieChartModel.php @@ -9,8 +9,6 @@ namespace Asantibanez\LivewireCharts\Models; */ class PieChartModel extends BaseChartModel { - public $title; - public $onSliceClickEventName; public $opacity; @@ -21,8 +19,6 @@ class PieChartModel extends BaseChartModel { parent::__construct(); - $this->title = ''; - $this->onSliceClickEventName = null; $this->opacity = 0.75; @@ -30,13 +26,6 @@ class PieChartModel extends BaseChartModel $this->data = collect(); } - public function setTitle($title) - { - $this->title = $title; - - return $this; - } - public function setOpacity($opacity) { $this->opacity = $opacity; @@ -66,7 +55,6 @@ class PieChartModel extends BaseChartModel public function toArray() { return array_merge(parent::toArray(), [ - 'title' => $this->title, 'onSliceClickEventName' => $this->onSliceClickEventName, 'opacity' => $this->opacity, 'data' => $this->data->toArray(), @@ -77,8 +65,6 @@ class PieChartModel extends BaseChartModel { parent::fromArray($array); - $this->title = data_get($array, 'title', ''); - $this->onSliceClickEventName = data_get($array, 'onSliceClickEventName', null); $this->opacity = data_get($array, 'opacity', 0.75);