Added HasLegend trait

- Refactored component to use BaseChartModel and traits
This commit is contained in:
Andrés Santibáñez
2020-10-25 00:40:06 -05:00
parent 8126b95593
commit 9df03c36a8
9 changed files with 123 additions and 72 deletions

View File

@@ -9,12 +9,15 @@ namespace Asantibanez\LivewireCharts\Models;
*/
class BaseChartModel
{
use HasAxisConfiguration;
use HasStrokeConfiguration;
use HasAxis;
use HasStroke;
use HasLegend;
public function __construct()
{
$this->setupAxis();
$this->initAxis();
$this->initStroke();
$this->initLegend();
}
public function reactiveKey()
@@ -24,16 +27,17 @@ class BaseChartModel
public function toArray()
{
return [
'xAxis' => $this->xAxis,
'yAxis' => $this->yAxis,
'stroke' => $this->stroke,
];
return array_merge(
$this->axisToArray(),
$this->strokeToArray(),
$this->legendToArray()
);
}
public function fromArray($array)
{
$this->setupAxis($array);
$this->setupStroke($array);
$this->axisFromArray($array);
$this->strokeFromArray($array);
$this->legendFromArray($array);
}
}

View File

@@ -3,13 +3,11 @@
namespace Asantibanez\LivewireCharts\Models;
use Ramsey\Uuid\Uuid;
/**
* Class ColumnChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class ColumnChartModel
class ColumnChartModel extends BaseChartModel
{
public $title;
@@ -23,6 +21,8 @@ class ColumnChartModel
public function __construct()
{
parent::__construct();
$this->title = '';
$this->animated = false;
@@ -76,17 +76,19 @@ class ColumnChartModel
public function toArray()
{
return [
return array_merge(parent::toArray(), [
'title' => $this->title,
'animated' => $this->animated,
'onColumnClickEventName' => $this->onColumnClickEventName,
'opacity' => $this->opacity,
'data' => $this->data->toArray(),
];
]);
}
public function fromArray($array)
{
parent::fromArray($array);
$this->title = data_get($array, 'title', '');
$this->animated = data_get($array, 'animated', false);
@@ -97,10 +99,4 @@ class ColumnChartModel
$this->data = collect(data_get($array, 'data', []));
}
public function reactiveKey()
{
return md5(json_encode($this->toArray()));
}
}

View File

@@ -4,39 +4,18 @@
namespace Asantibanez\LivewireCharts\Models;
trait HasAxisConfiguration
trait HasAxis
{
private $xAxis;
private $yAxis;
public function __construct()
public function initAxis()
{
$this->xAxis = $this->defaultXAxis();
$this->yAxis = $this->defaultYAxis();
}
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 [
@@ -52,4 +31,33 @@ trait HasAxisConfiguration
'show' => true,
];
}
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 axisFromArray($array)
{
$this->xAxis = data_get($array, 'xAxis', $this->defaultXAxis());
$this->yAxis = data_get($array, 'yAxis', $this->defaultYAxis());
}
protected function axisToArray()
{
return [
'xAxis' => $this->xAxis,
'yAxis' => $this->yAxis,
];
}
}

43
src/Models/HasLegend.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasLegend
{
private $legend;
public function initLegend()
{
$this->legend = $this->defaultLegend();
}
private function defaultLegend()
{
return [
'show' => true,
'position' => 'bottom',
'horizontalAlign' => 'center',
];
}
public function setLegendVisibility($visible)
{
data_set($this->legend, 'show', $visible);
return $this;
}
protected function legendToArray()
{
return [
'legend' => $this->legend,
];
}
protected function legendFromArray($array)
{
$this->legend = data_get($array, 'legend', $this->defaultLegend());
}
}

View File

@@ -4,15 +4,22 @@
namespace Asantibanez\LivewireCharts\Models;
trait HasStrokeConfiguration
trait HasStroke
{
private $stroke;
public function __construct()
public function initStroke()
{
$this->stroke = $this->defaultStroke();
}
private function defaultStroke()
{
return [
'curve' => 'straight',
];
}
public function setSmoothCurve()
{
data_set($this->stroke, 'curve', 'smooth');
@@ -27,15 +34,15 @@ trait HasStrokeConfiguration
return $this;
}
protected function setupStroke($array = [])
protected function strokeToArray()
{
return [
'stroke' => $this->stroke,
];
}
protected function strokeFromArray($array)
{
$this->stroke = data_get($array, 'stroke', $this->defaultStroke());
}
private function defaultStroke()
{
return [
'curve' => 'straight',
];
}
}

View File

@@ -110,9 +110,4 @@ class LineChartModel extends BaseChartModel
$this->markers = collect(data_get($array, 'markers', []));
}
public function reactiveKey()
{
return md5(json_encode($this->toArray()));
}
}

View File

@@ -7,7 +7,7 @@ namespace Asantibanez\LivewireCharts\Models;
* Class PieChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class PieChartModel
class PieChartModel extends BaseChartModel
{
public $title;
@@ -21,6 +21,8 @@ class PieChartModel
public function __construct()
{
parent::__construct();
$this->title = '';
$this->animated = false;
@@ -74,17 +76,19 @@ class PieChartModel
public function toArray()
{
return [
return array_merge(parent::toArray(), [
'title' => $this->title,
'animated' => $this->animated,
'onSliceClickEventName' => $this->onSliceClickEventName,
'opacity' => $this->opacity,
'data' => $this->data->toArray(),
];
]);
}
public function fromArray($array)
{
parent::fromArray($array);
$this->title = data_get($array, 'title', '');
$this->animated = data_get($array, 'animated', false);
@@ -95,9 +99,4 @@ class PieChartModel
$this->data = collect(data_get($array, 'data', []));
}
public function reactiveKey()
{
return md5(json_encode($this->toArray()));
}
}