10 Commits
1.0.0 ... 1.2.0

Author SHA1 Message Date
Andrés Santibáñez
1b7f928f94 Updated CHANGELOG 2020-10-25 00:57:26 -05:00
Andrés Santibáñez
28fa9421ad Added axis configuration to Line Chart 2020-10-25 00:55:08 -05:00
Andrés Santibáñez
09f8b354f7 Added more expressive api for legend config 2020-10-25 00:49:10 -05:00
Andrés Santibáñez
8586357077 Updated gitignore 2020-10-25 00:41:11 -05:00
Andrés Santibáñez
9df03c36a8 Added HasLegend trait
- Refactored component to use BaseChartModel and traits
2020-10-25 00:40:06 -05:00
Andrés Santibáñez
8126b95593 Added Stroke configuration 2020-10-25 00:18:20 -05:00
Andrés Santibáñez
472dfd8a4a Removed unused function 2020-10-25 00:17:34 -05:00
Andrés Santibáñez
865c1fe13f Fixed layout 2020-10-24 23:54:18 -05:00
asantibanez
e8003a33bd Updated docs 2020-10-20 10:46:59 -05:00
asantibanez
1d0755e249 Added Area Chart component and model 2020-10-20 10:43:04 -05:00
17 changed files with 543 additions and 41 deletions

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@ composer.lock
docs docs
vendor vendor
coverage coverage
.DS_STORE

View File

@@ -1,5 +1,16 @@
# Changelog # Changelog
## 1.2.0 - 2020-10-25
- Fixed column chart growing infinitely
- Improved customization API
- Added Legend configuration
- Refactored configuration traits
## 1.1.0 - 2020-10-20
- Added Area Chart
## 1.0.0 - 2020-10-19 ## 1.0.0 - 2020-10-19
- Initial Release - Initial Release

View File

@@ -31,11 +31,13 @@ Livewire Charts supports out of the box the following types of charts
- Line Chart (`LivewireLineChart` component) - Line Chart (`LivewireLineChart` component)
- Pie Chart (`LivewirePieChart` component) - Pie Chart (`LivewirePieChart` component)
- Column Chart (`LivewireColumnChart` component) - Column Chart (`LivewireColumnChart` component)
- Area Chart (`LivewireAreaChart` component)
Each one comes with its own "model" class that allows you to define the chart's data and render behavior. Each one comes with its own "model" class that allows you to define the chart's data and render behavior.
- `LivewireLineChart` uses `LineChartModel` to set up data points, markers, events and other ui customizations. - `LivewireLineChart` uses `LineChartModel` to set up data points, markers, events and other ui customizations.
- `LivewirePieChart` uses `PieChartModel` to set up data slices, events and other ui customizations. - `LivewirePieChart` uses `PieChartModel` to set up data slices, events and other ui customizations.
- `LivewireColumnChart` uses `ColumnChartModel` to set up data columns, events and other ui customizations. - `LivewireColumnChart` uses `ColumnChartModel` to set up data columns, events and other ui customizations.
- `LivewireAreaChart` uses `AreaChartModel` to set up data points, events and other ui customizations.
For example, to render a column chart, we can create an instance of `ColumnChartModel` and add some data to it For example, to render a column chart, we can create an instance of `ColumnChartModel` and add some data to it
```php ```php
@@ -153,6 +155,17 @@ for each type of chart.
| addSlice(string $title, double $value, string $color, array $extras = []) | Adds a slice to the chart with the specified color. `$extras` is forwarded on click event | | addSlice(string $title, double $value, string $color, array $extras = []) | Adds a slice to the chart with the specified color. `$extras` is forwarded on click event |
| withOnSliceClickEvent(string $eventName) | Event Name that will be fired when a column of the chart is clicked | | withOnSliceClickEvent(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
#### LivewireAreaChart
| Method | Description |
|--------|-------------|
| setTitle(string $title) | Sets chart title |
| setAnimated(boolean $animated) | Enables/disables animation |
| addPoint(string $title, double $value, array $extras = []) | Adds a point to the area chart. `$extras` is forwarded on click event |
| withOnPointClickEvent(string $eventName) | Event Name that will be fired when a point of the chart is clicked |
| setXAxisVisible(boolean $visible) | Shows/hides xAxis labels |
| setYAxisVisible(boolean $visible) | Shows/hides yAxis labels |
## Testing ## Testing
``` bash ``` bash

View File

@@ -0,0 +1,87 @@
<div
class="w-full h-full"
x-data="{ ...areaChart() }"
x-init="drawChart(@this)"
x-on:draw-column-chart.window="drawChart(@this)"
>
<div wire:ignore x-ref="container"></div>
<script>
function areaChart() {
return {
chart: null,
drawChart(component) {
if (this.chart) {
this.chart.destroy()
}
var options = {
series: [{
name: component.get('areaChartModel.title'),
data: component.get('areaChartModel.data').map(item => item.value)
}],
chart: {
type: 'area',
height: '100%',
zoom: { enabled: false },
toolbar: { show: false },
animations: {
enabled: component.get('areaChartModel.animated') || false,
},
events: {
markerClick: function(event, chartContext, { dataPointIndex }) {
const onPointClickEventName = component.get('areaChartModel.onPointClickEventName')
if (!onPointClickEventName) {
return
}
const point = component.get('areaChartModel.data')[dataPointIndex]
component.call('onPointClick', point)
}
}
},
dataLabels: {
enabled: false
},
colors: [component.get('areaChartModel.color') || '#2E93fA'],
stroke: {
curve: 'straight'
},
title: {
text: component.get('areaChartModel.title'),
align: 'center'
},
labels: component.get('areaChartModel.data').map(item => item.title),
xaxis: component.get('areaChartModel.xAxis') || {},
yaxis: component.get('areaChartModel.yAxis') || {},
grid: {
padding: {
left: 0,
top: 0,
right: 0,
bottom: 0,
}
},
};
this.chart = new ApexCharts(this.$refs.container, options);
this.chart.render();
}
}
}
</script>
</div>

View File

@@ -1,5 +1,5 @@
<div <div
class="w-full h-full relative" class="w-full h-full"
x-data="{ ...columnChart() }" x-data="{ ...columnChart() }"
x-init="drawChart(@this)" x-init="drawChart(@this)"
x-on:draw-column-chart.window="drawChart(@this)" x-on:draw-column-chart.window="drawChart(@this)"
@@ -58,6 +58,8 @@
}, },
}, },
legend: component.get('columnChartModel.legend') || {},
plotOptions: { plotOptions: {
bar: { bar: {
horizontal: false, horizontal: false,

View File

@@ -2,7 +2,6 @@
class="w-full h-full" class="w-full h-full"
x-data="{ ...lineChart() }" x-data="{ ...lineChart() }"
x-init="drawChart(@this)" x-init="drawChart(@this)"
x-on:draw-column-chart.window="drawChart(@this)"
> >
<div wire:ignore x-ref="container"></div> <div wire:ignore x-ref="container"></div>
@@ -12,10 +11,6 @@
chart: null, chart: null,
drawChart(component) { drawChart(component) {
this.renderApex(component)
},
renderApex(component) {
if (this.chart) { if (this.chart) {
this.chart.destroy() this.chart.destroy()
} }
@@ -25,6 +20,7 @@
name: component.get('lineChartModel.title'), name: component.get('lineChartModel.title'),
data: component.get('lineChartModel.data').map(item => item.value), data: component.get('lineChartModel.data').map(item => item.value),
}], }],
chart: { chart: {
type: 'line', type: 'line',
height: '100%', height: '100%',
@@ -49,19 +45,25 @@
} }
} }
}, },
dataLabels: { dataLabels: {
enabled: false enabled: false
}, },
stroke: {
curve: 'straight' stroke: component.get('lineChartModel.stroke') || {},
},
title: { title: {
text: component.get('lineChartModel.title'), text: component.get('lineChartModel.title'),
align: 'center' align: 'center'
}, },
xaxis: { xaxis: {
...component.get('lineChartModel.xAxis') || {},
categories: component.get('lineChartModel.data').map(item => item.title), categories: component.get('lineChartModel.data').map(item => item.title),
}, },
yaxis: component.get('lineChartModel.yAxis') || {},
annotations: { annotations: {
points: component.get('lineChartModel.markers').map(item => { points: component.get('lineChartModel.markers').map(item => {
return { return {

View File

@@ -2,7 +2,6 @@
class="w-full h-full" class="w-full h-full"
x-data="{ ...pieChart() }" x-data="{ ...pieChart() }"
x-init="drawChart(@this)" x-init="drawChart(@this)"
x-on:draw-column-chart.window="drawChart(@this)"
> >
<div wire:ignore x-ref="container"></div> <div wire:ignore x-ref="container"></div>
@@ -12,10 +11,6 @@
chart: null, chart: null,
drawChart(component) { drawChart(component) {
this.renderApex(component)
},
renderApex(component) {
if (this.chart) { if (this.chart) {
this.chart.destroy() this.chart.destroy()
} }
@@ -73,6 +68,8 @@
} }
} }
], ],
legend: component.get('pieChartModel.legend') || {},
}; };
this.chart = new ApexCharts(this.$refs.container, options); this.chart = new ApexCharts(this.$refs.container, options);

View File

@@ -0,0 +1,36 @@
<?php
namespace Asantibanez\LivewireCharts\Charts;
use Asantibanez\LivewireCharts\Models\AreaChartModel;
use Livewire\Component;
/**
* Class LivewireAreaChart
* @package Asantibanez\LivewireCharts\Charts
*/
class LivewireAreaChart extends Component
{
public $areaChartModel;
public function mount(AreaChartModel $areaChartModel)
{
$this->areaChartModel = $areaChartModel->toArray();
}
public function onPointClick($point)
{
$onPointClickEventName = data_get($this->areaChartModel, 'onPointClickEventName', null);
if ($onPointClickEventName === null) {
return;
}
$this->emit($onPointClickEventName, $point);
}
public function render()
{
return view('livewire-charts::livewire-area-chart');
}
}

View File

@@ -2,6 +2,7 @@
namespace Asantibanez\LivewireCharts; namespace Asantibanez\LivewireCharts;
use Asantibanez\LivewireCharts\Charts\LivewireAreaChart;
use Asantibanez\LivewireCharts\Charts\LivewireColumnChart; use Asantibanez\LivewireCharts\Charts\LivewireColumnChart;
use Asantibanez\LivewireCharts\Charts\LivewireLineChart; use Asantibanez\LivewireCharts\Charts\LivewireLineChart;
use Asantibanez\LivewireCharts\Charts\LivewirePieChart; use Asantibanez\LivewireCharts\Charts\LivewirePieChart;
@@ -26,6 +27,7 @@ class LivewireChartsServiceProvider extends ServiceProvider
Livewire::component('livewire-line-chart', LivewireLineChart::class); Livewire::component('livewire-line-chart', LivewireLineChart::class);
Livewire::component('livewire-column-chart', LivewireColumnChart::class); Livewire::component('livewire-column-chart', LivewireColumnChart::class);
Livewire::component('livewire-pie-chart', LivewirePieChart::class); Livewire::component('livewire-pie-chart', LivewirePieChart::class);
Livewire::component('livewire-area-chart', LivewireAreaChart::class);
} }
/** /**

View File

@@ -0,0 +1,101 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
/**
* Class AreaChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class AreaChartModel extends BaseChartModel
{
public $title;
public $animated;
public $color;
public $data;
public $onPointClickEventName;
public function __construct()
{
parent::__construct();
$this->title = '';
$this->animated = false;
$this->color = '#90cdf4';
$this->onPointClickEventName = null;
$this->data = collect();
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setAnimated($animated)
{
$this->animated = $animated;
return $this;
}
public function setColor($color)
{
$this->color = $color;
return $this;
}
public function addPoint($title, $value, $extras = [])
{
$this->data->push([
'title' => $title,
'value' => $value,
'extras' => $extras,
]);
return $this;
}
public function withOnPointClickEvent($onPointClickEventName)
{
$this->onPointClickEventName = $onPointClickEventName;
return $this;
}
public function toArray()
{
return array_merge(parent::toArray(), [
'title' => $this->title,
'animated' => $this->animated,
'color' => $this->color,
'onPointClickEventName' => $this->onPointClickEventName,
'data' => $this->data->toArray(),
]);
}
public function fromArray($array)
{
parent::fromArray($array);
$this->title = data_get($array, 'title', '');
$this->animated = data_get($array, 'animated', false);
$this->color = data_get($array, 'color', '#90cdf4');
$this->onPointClickEventName = data_get($array, 'onPointClickEventName', null);
$this->data = collect(data_get($array, 'data', []));
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
/**
* Class BaseChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class BaseChartModel
{
use HasAxis;
use HasStroke;
use HasLegend;
public function __construct()
{
$this->initAxis();
$this->initStroke();
$this->initLegend();
}
public function reactiveKey()
{
return md5(json_encode($this->toArray()));
}
public function toArray()
{
return array_merge(
$this->axisToArray(),
$this->strokeToArray(),
$this->legendToArray()
);
}
public function fromArray($array)
{
$this->axisFromArray($array);
$this->strokeFromArray($array);
$this->legendFromArray($array);
}
}

View File

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

63
src/Models/HasAxis.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasAxis
{
private $xAxis;
private $yAxis;
public function initAxis()
{
$this->xAxis = $this->defaultXAxis();
$this->yAxis = $this->defaultYAxis();
}
private function defaultXAxis()
{
return [
'labels' => [
'show' => true,
],
];
}
private function defaultYAxis()
{
return [
'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,
];
}
}

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

@@ -0,0 +1,102 @@
<?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;
}
public function setLegendPosition($position)
{
data_set($this->legend, 'position', $position);
return $this;
}
public function setLegendHorizontalAlign($horizontalAlign)
{
data_set($this->legend, 'horizontalAlign', $horizontalAlign);
return $this;
}
public function withoutLegend()
{
return $this->setLegendVisibility(false);
}
public function withLegend()
{
return $this->setLegendVisibility(true);
}
public function legendPositionTop()
{
return $this->setLegendPosition('top');
}
public function legendPositionLeft()
{
return $this->setLegendPosition('left');
}
public function legendPositionRight()
{
return $this->setLegendPosition('right');
}
public function legendPositionBottom()
{
return $this->setLegendPosition('bottom');
}
public function legendHorizontallyAlignedLeft()
{
return $this->setLegendHorizontalAlign('left');
}
public function legendHorizontallyAlignedCenter()
{
return $this->setLegendHorizontalAlign('center');
}
public function legendHorizontallyAlignedRight()
{
return $this->setLegendHorizontalAlign('right');
}
protected function legendToArray()
{
return [
'legend' => $this->legend,
];
}
protected function legendFromArray($array)
{
$this->legend = data_get($array, 'legend', $this->defaultLegend());
}
}

48
src/Models/HasStroke.php Normal file
View File

@@ -0,0 +1,48 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasStroke
{
private $stroke;
public function initStroke()
{
$this->stroke = $this->defaultStroke();
}
private function defaultStroke()
{
return [
'curve' => 'straight',
];
}
public function setSmoothCurve()
{
data_set($this->stroke, 'curve', 'smooth');
return $this;
}
public function setStraightCurve()
{
data_set($this->stroke, 'curve', 'straight');
return $this;
}
protected function strokeToArray()
{
return [
'stroke' => $this->stroke,
];
}
protected function strokeFromArray($array)
{
$this->stroke = data_get($array, 'stroke', $this->defaultStroke());
}
}

View File

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