Extracted title property to trait

This commit is contained in:
Andrés Santibáñez
2020-11-03 07:20:46 -05:00
parent 22b72c1980
commit 63fe7a1533
6 changed files with 43 additions and 57 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

39
src/Models/HasTitle.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasTitle
{
private $title;
public function setTitle($title)
{
$this->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,
];
}
}

View File

@@ -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);

View File

@@ -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);