Added DataLabels support for charts

This commit is contained in:
Andrés Santibáñez
2020-11-03 07:44:32 -05:00
parent 63fe7a1533
commit 05c9f5c605
7 changed files with 102 additions and 45 deletions

View File

@@ -14,6 +14,7 @@ class BaseChartModel
use HasAxis;
use HasStroke;
use HasLegend;
use HasDataLabels;
public function __construct()
{
@@ -22,6 +23,7 @@ class BaseChartModel
$this->initAxis();
$this->initStroke();
$this->initLegend();
$this->initDataLabels();
}
public function reactiveKey()
@@ -36,7 +38,8 @@ class BaseChartModel
$this->animationToArray(),
$this->axisToArray(),
$this->strokeToArray(),
$this->legendToArray()
$this->legendToArray(),
$this->dataLabelsToArray()
);
}
@@ -47,5 +50,6 @@ class BaseChartModel
$this->axisFromArray($array);
$this->strokeFromArray($array);
$this->legendFromArray($array);
$this->dataLabelsFromArray($array);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasDataLabels
{
private $dataLabels;
public function setDataLabelsEnabled($enabled)
{
data_set($this->dataLabels, 'enabled', $enabled);
return $this;
}
public function withDataLabels()
{
data_set($this->dataLabels, 'enabled', true);
return $this;
}
public function withoutDataLabels()
{
data_set($this->dataLabels, 'enabled', false);
return $this;
}
protected function initDataLabels()
{
$this->dataLabels = $this->defaultDataLabels();
}
private function defaultDataLabels()
{
return [
'enabled' => false,
];
}
protected function dataLabelsFromArray($array)
{
$this->dataLabels = data_get($array, 'dataLabels', $this->defaultDataLabels());
}
protected function dataLabelsToArray()
{
return [
'dataLabels' => $this->dataLabels,
];
}
}