20 Commits
1.0.0 ... 1.3.0

Author SHA1 Message Date
Andrés Santibáñez
0aedf541a9 Updated README and CHANGELOG 2020-11-03 08:02:36 -05:00
Andrés Santibáñez
316bb5a976 Removed markers support for multi line chart 2020-11-03 07:59:45 -05:00
Andrés Santibáñez
05c9f5c605 Added DataLabels support for charts 2020-11-03 07:44:32 -05:00
Andrés Santibáñez
63fe7a1533 Extracted title property to trait 2020-11-03 07:20:46 -05:00
Andrés Santibáñez
22b72c1980 Extracted multi line chart Blade view 2020-11-03 07:16:51 -05:00
Andrés Santibáñez
5e7cabfd16 Extracted animated property to trait 2020-11-03 07:10:30 -05:00
Andrés Santibáñez
71e2168b6c Added MultiLine support for Line Chart 2020-11-03 07:07:02 -05:00
Andrés Santibáñez
9b74d45a86 Updated README 2020-11-02 22:01:02 -05:00
Andrés Santibáñez
19f3ce70ae Updated README 2020-11-02 21:51:15 -05:00
Andrés Santibáñez
4c55e19486 Updated README 2020-10-25 01:01:39 -05:00
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
22 changed files with 863 additions and 158 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,22 @@
# Changelog # Changelog
## 1.3.0 - 2020-11-03
- Added Multi Line Chart
- Added Data Labels configuration for all charts
- Refactored title and animation to traits
## 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

@@ -1,11 +1,13 @@
# Livewire Charts ![Livewire Charts](https://banners.beyondco.de/Livewire%20Charts.png?theme=light&packageName=asantibanez%2Flivewire-charts&pattern=graphPaper&style=style_1&description=Neat+Livewire+Charts+for+your+Laravel+projects&md=1&fontSize=100px&images=chart-square-bar)
Neat Livewire Charts for your Laravel projects.
## Preview ## Preview
![preview](https://github.com/asantibanez/livewire-charts/raw/master/preview.gif) ![preview](https://github.com/asantibanez/livewire-charts/raw/master/preview.gif)
## Demo
https://github.com/asantibanez/livewire-charts-demo
## Installation ## Installation
You can install the package via composer: You can install the package via composer:
@@ -23,7 +25,7 @@ This package requires the following packages/libraries to work:
Please follow each package/library instructions on how to set them properly in your project. Please follow each package/library instructions on how to set them properly in your project.
> Note: At the moment, `Apex Charts` is only supported for drawing charts. >Note: At the moment, `Apex Charts` is only supported for drawing charts.
## Usage ## Usage
@@ -31,11 +33,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
@@ -48,7 +52,7 @@ $columnChartModel =
; ;
``` ```
> Note: Chart model methods are chainable 💪 >Note: Chart model methods are chainable 💪
With `$columnChartModel` at hand, we pass it to our `LivewireColumnChart` component in our Blade template. With `$columnChartModel` at hand, we pass it to our `LivewireColumnChart` component in our Blade template.
@@ -62,7 +66,7 @@ And that's it! You have a beautiful rendered chart in seconds. 👌
![column chart example](https://github.com/asantibanez/livewire-charts/raw/master/column-chart-example.png) ![column chart example](https://github.com/asantibanez/livewire-charts/raw/master/column-chart-example.png)
> Note: You can use these charts inside other Livewire components too. Just render them in your Blade template and you >Note: You can use these charts inside other Livewire components too. Just render them in your Blade template and you
are good to go. 👍 are good to go. 👍
## Enabling Interactions ## Enabling Interactions
@@ -123,22 +127,33 @@ The parent component renders the charts as follows
For each chart, a specific model class needs to be used. Here, it is detailed the api available For each chart, a specific model class needs to be used. Here, it is detailed the api available
for each type of chart. for each type of chart.
#### LivewireLineChart #### All
| Method | Description | | Method | Description |
|--------|-------------| |--------|-------------|
| setTitle(string $title) | Sets chart title | | setTitle(string $title) | Sets chart title |
| setAnimated(boolean $animated) | Enables/disables animation | | setAnimated(boolean $animated) | Enables/disables animation |
| addPoint(string $title, double $value, array $extras = []) | Adds a point to the line chart. `$extras` is forwarded on click event | | setDataLabelsEnabled(boolean $enabled) | Enables/disables data labels |
| addMarker(string $title, double $value) | Adds a marker point to the line chart. Markers are used to emphasize particular points in the chart | | withDataLabels() | Enables data labels |
| withoutDataLabels() | Disables data labels |
| withLegends() | Shows legends |
| withoutLegends() | Hides legends |
#### LivewireLineChart
| Method | Description |
|--------|-------------|
| multiLine() | Adds multiline support for line chart |
| singleLine() | Adds single support for line chart |
| addPoint(string $title, double $value, array $extras = []) | Adds a point to a single line chart. `$extras` is forwarded on click event |
| addSeriesPoint(string $seriesName, string $title, double $value, array $extras = []) | Adds a point to series to a multi line chart. `$extras` is forwarded on click event |
| addMarker(string $title, double $value) | Adds a marker point to the line chart. Markers are used to emphasize particular points in the chart (singleLine only) |
| withOnPointClickEvent(string $eventName) | Event Name that will be fired when a point/marker of the chart is clicked | | withOnPointClickEvent(string $eventName) | Event Name that will be fired when a point/marker of the chart is clicked |
#### LivewireColumnChart #### LivewireColumnChart
| Method | Description | | Method | Description |
|--------|-------------| |--------|-------------|
| setTitle(string $title) | Sets chart title |
| setAnimated(boolean $animated) | Enables/disables animation |
| setOpacity(int $opacity) | Sets columns' opacity | | setOpacity(int $opacity) | Sets columns' opacity |
| addColumn(string $title, double $value, string $color, array $extras = []) | Adds a column to the chart with the specified color. `$extras` is forwarded on click event | | addColumn(string $title, double $value, string $color, array $extras = []) | Adds a column to the chart with the specified color. `$extras` is forwarded on click event |
| onColumnClickEventName(string $eventName) | Event Name that will be fired when a column of the chart is clicked | | onColumnClickEventName(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
@@ -147,12 +162,32 @@ for each type of chart.
| Method | Description | | Method | Description |
|--------|-------------| |--------|-------------|
| setTitle(string $title) | Sets chart title |
| setAnimated(boolean $animated) | Enables/disables animation |
| setOpacity(int $opacity) | Sets slices' opacity | | setOpacity(int $opacity) | Sets slices' opacity |
| 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 |
|--------|-------------|
| 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 |
## Troubleshooting
Chart components must be placed inside a container with fixed height. This is because the chart will use all the given
vertical space. A fixed height is needed to render properly.
```blade
<div style="height: 32rem;">
<livewire:livewire-column-chart .../>
</div>
```
>Note: if a fixed height is not given, the chart will grow vertically infinitely. That's not what we want, right?
## Testing ## Testing
``` bash ``` bash

View File

@@ -0,0 +1,90 @@
<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()
}
const title = component.get('areaChartModel.title');
const animated = component.get('areaChartModel.animated') || false;
const dataLabels = component.get('areaChartModel.dataLabels') || {};
const onPointClickEventName = component.get('areaChartModel.onPointClickEventName')
const data = component.get('areaChartModel.data');
var options = {
series: [{
name: title,
data: data.map(item => item.value)
}],
chart: {
type: 'area',
height: '100%',
zoom: { enabled: false },
toolbar: { show: false },
animations: {
enabled: animated,
},
events: {
markerClick: function(event, chartContext, { dataPointIndex }) {
if (!onPointClickEventName) {
return
}
const point = data[dataPointIndex]
component.call('onPointClick', point)
}
}
},
dataLabels: dataLabels,
colors: [component.get('areaChartModel.color') || '#2E93fA'],
stroke: {
curve: 'straight'
},
title: {
text: title,
align: 'center'
},
labels: 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)"
@@ -12,16 +12,17 @@
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()
} }
const animated = component.get('columnChartModel.animated') || false;
const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName')
const dataLabels = component.get('columnChartModel.dataLabels') || {};
const data = component.get('columnChartModel.data');
const options = { const options = {
series: [{ data: component.get('columnChartModel.data').map(item => item.value)}], series: [{ data: data.map(item => item.value)}],
chart: { chart: {
type: 'bar', type: 'bar',
@@ -32,32 +33,32 @@
}, },
animations: { animations: {
enabled: component.get('columnChartModel.animated') || false, enabled: animated,
}, },
events: { events: {
dataPointSelection: function(event, chartContext, config) { dataPointSelection: function(event, chartContext, config) {
const onColumnClickEventName = component.get('columnChartModel.onColumnClickEventName')
if (!onColumnClickEventName) { if (!onColumnClickEventName) {
return return
} }
const { dataPointIndex } = config const { dataPointIndex } = config
const column = component.get('columnChartModel.data')[dataPointIndex] const column = data[dataPointIndex]
component.call('onColumnClick', column) component.call('onColumnClick', column)
}, },
} }
}, },
colors: component.get('columnChartModel.data').map(item => item.color), colors: data.map(item => item.color),
labels: { labels: {
style: { style: {
colors: component.get('columnChartModel.data').map(item => item.color), colors: data.map(item => item.color),
}, },
}, },
legend: component.get('columnChartModel.legend') || {},
plotOptions: { plotOptions: {
bar: { bar: {
horizontal: false, horizontal: false,
@@ -66,12 +67,10 @@
}, },
}, },
dataLabels: { dataLabels: dataLabels,
enabled: false,
},
xaxis: { xaxis: {
categories: component.get('columnChartModel.data').map(item => item.title), categories: data.map(item => item.title),
}, },
yaxis: { yaxis: {

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,19 +11,26 @@
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()
} }
const title = component.get('lineChartModel.title');
const animated = component.get('lineChartModel.animated') || false;
const dataLabels = component.get('lineChartModel.dataLabels') || {};
const data = component.get('lineChartModel.data');
const onPointClickEventName = component.get('lineChartModel.onPointClickEventName');
const series = [{
name: title,
data: data.map(item => item.value),
}]
const categories = data.map(item => item.title)
const options = { const options = {
series: [{ series: series,
name: component.get('lineChartModel.title'),
data: component.get('lineChartModel.data').map(item => item.value),
}],
chart: { chart: {
type: 'line', type: 'line',
height: '100%', height: '100%',
@@ -35,33 +41,36 @@
show: false show: false
}, },
animations: { animations: {
enabled: component.get('lineChartModel.animated') || false, enabled: animated,
}, },
events: { events: {
markerClick: function(event, chartContext, { dataPointIndex }) { markerClick: function(event, chartContext, { dataPointIndex }) {
const onPointClickEventName = component.get('lineChartModel.onPointClickEventName')
if (!onPointClickEventName) { if (!onPointClickEventName) {
return return
} }
const point = component.get('lineChartModel.data')[dataPointIndex] const point = data[dataPointIndex]
component.call('onPointClick', point) component.call('onPointClick', point)
} }
} }
}, },
dataLabels: {
enabled: false dataLabels: dataLabels,
},
stroke: { stroke: component.get('lineChartModel.stroke') || {},
curve: 'straight'
},
title: { title: {
text: component.get('lineChartModel.title'), text: title,
align: 'center' align: 'center'
}, },
xaxis: { xaxis: {
categories: component.get('lineChartModel.data').map(item => item.title), ...component.get('lineChartModel.xAxis') || {},
categories: categories,
}, },
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

@@ -0,0 +1,85 @@
<div
class="w-full h-full"
x-data="{ ...multiLineChart() }"
x-init="drawChart(@this)"
>
<div wire:ignore x-ref="container"></div>
<script>
function multiLineChart() {
return {
chart: null,
drawChart(component) {
if (this.chart) {
this.chart.destroy()
}
const title = component.get('lineChartModel.title');
const animated = component.get('lineChartModel.animated') || false;
const dataLabels = component.get('lineChartModel.dataLabels') || {};
const data = component.get('lineChartModel.data');
const onPointClickEventName = component.get('lineChartModel.onPointClickEventName');
const series = Object.keys(data).map(key => {
return {
name: key,
data: data[key].map(item => item.value),
}
})
const categories = []
const options = {
series: series,
chart: {
type: 'line',
height: '100%',
zoom: {
enabled: false
},
toolbar: {
show: false
},
animations: {
enabled: animated,
},
events: {
markerClick: function(event, chartContext, { seriesIndex, dataPointIndex }) {
if (!onPointClickEventName) {
return
}
const seriesName = Object.keys(data)[Object.keys(data).findIndex((el, index) => index === seriesIndex)]
const point = data[seriesName][dataPointIndex]
component.call('onPointClick', point)
}
}
},
dataLabels: dataLabels,
stroke: component.get('lineChartModel.stroke') || {},
title: {
text: title,
align: 'center'
},
xaxis: {
...component.get('lineChartModel.xAxis') || {},
categories: categories,
},
yaxis: component.get('lineChartModel.yAxis') || {},
};
this.chart = new ApexCharts(this.$refs.container, options);
this.chart.render();
}
}
}
</script>
</div>

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,54 +11,52 @@
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()
} }
const title = component.get('pieChartModel.title');
const animated = component.get('pieChartModel.animated') || false
const dataLabels = component.get('pieChartModel.dataLabels') || {}
const onSliceClickEventName = component.get('pieChartModel.onSliceClickEventName')
const data = component.get('pieChartModel.data')
const options = { const options = {
series: component.get('pieChartModel.data').map(item => item.value), series: data.map(item => item.value),
chart: { chart: {
height: '100%', height: '100%',
type: 'pie', type: 'pie',
animations: { animations: {
enabled: component.get('pieChartModel.animated') || false, enabled: animated,
}, },
events: { events: {
dataPointSelection: function(event, chartContext, config) { dataPointSelection: function(event, chartContext, config) {
const onSliceClickEventName = component.get('pieChartModel.onSliceClickEventName')
if (!onSliceClickEventName) { if (!onSliceClickEventName) {
return return
} }
const { dataPointIndex } = config const { dataPointIndex } = config
const slice = component.get('pieChartModel.data')[dataPointIndex] const slice = data[dataPointIndex]
component.call('onSliceClick', slice) component.call('onSliceClick', slice)
}, },
} }
}, },
labels: component.get('pieChartModel.data').map(item => item.title), labels: data.map(item => item.title),
dataLabels: { dataLabels: dataLabels,
enabled: false,
},
colors: component.get('pieChartModel.data').map(item => item.color), colors: data.map(item => item.color),
fill: { fill: {
opacity: component.get('pieChartModel.opacity'), opacity: component.get('pieChartModel.opacity'),
}, },
title: { title: {
text: component.get('pieChartModel.title'), text: title,
align: 'center', align: 'center',
}, },
@@ -73,6 +70,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

@@ -31,6 +31,10 @@ class LivewireLineChart extends Component
public function render() public function render()
{ {
if ($this->lineChartModel['isMultiLine']) {
return view('livewire-charts::livewire-multi-line-chart');
}
return view('livewire-charts::livewire-line-chart'); return view('livewire-charts::livewire-line-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,73 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
/**
* Class AreaChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class AreaChartModel extends BaseChartModel
{
public $color;
public $data;
public $onPointClickEventName;
public function __construct()
{
parent::__construct();
$this->color = '#90cdf4';
$this->onPointClickEventName = null;
$this->data = collect();
}
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(), [
'color' => $this->color,
'onPointClickEventName' => $this->onPointClickEventName,
'data' => $this->data->toArray(),
]);
}
public function fromArray($array)
{
parent::fromArray($array);
$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,55 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
/**
* Class BaseChartModel
* @package Asantibanez\LivewireCharts\Models
*/
class BaseChartModel
{
use HasTitle;
use HasAnimation;
use HasAxis;
use HasStroke;
use HasLegend;
use HasDataLabels;
public function __construct()
{
$this->initTitle();
$this->initAnimation();
$this->initAxis();
$this->initStroke();
$this->initLegend();
$this->initDataLabels();
}
public function reactiveKey()
{
return md5(json_encode($this->toArray()));
}
public function toArray()
{
return array_merge(
$this->titleToArray(),
$this->animationToArray(),
$this->axisToArray(),
$this->strokeToArray(),
$this->legendToArray(),
$this->dataLabelsToArray()
);
}
public function fromArray($array)
{
$this->titleFromArray($array);
$this->animationFromArray($array);
$this->axisFromArray($array);
$this->strokeFromArray($array);
$this->legendFromArray($array);
$this->dataLabelsFromArray($array);
}
}

View File

@@ -3,18 +3,12 @@
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 $animated;
public $opacity; public $opacity;
public $onColumnClickEventName; public $onColumnClickEventName;
@@ -23,9 +17,7 @@ class ColumnChartModel
public function __construct() public function __construct()
{ {
$this->title = ''; parent::__construct();
$this->animated = false;
$this->onColumnClickEventName = null; $this->onColumnClickEventName = null;
@@ -34,20 +26,6 @@ class ColumnChartModel
$this->data = collect(); $this->data = collect();
} }
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setAnimated($animated)
{
$this->animated = $animated;
return $this;
}
public function setOpacity($opacity) public function setOpacity($opacity)
{ {
$this->opacity = $opacity; $this->opacity = $opacity;
@@ -76,20 +54,16 @@ class ColumnChartModel
public function toArray() public function toArray()
{ {
return [ return array_merge(parent::toArray(), [
'title' => $this->title,
'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)
{ {
$this->title = data_get($array, 'title', ''); parent::fromArray($array);
$this->animated = data_get($array, 'animated', false);
$this->onColumnClickEventName = data_get($array, 'onColumnClickEventName', null); $this->onColumnClickEventName = data_get($array, 'onColumnClickEventName', null);
@@ -97,10 +71,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()));
}
} }

View File

@@ -0,0 +1,39 @@
<?php
namespace Asantibanez\LivewireCharts\Models;
trait HasAnimation
{
private $animated;
public function setAnimated($animated)
{
$this->animated = $animated;
return $this;
}
protected function initAnimation()
{
$this->animated = $this->defaultAnimated();
}
private function defaultAnimated()
{
return false;
}
protected function animationFromArray($array)
{
$this->animated = data_get($array, 'animated', $this->defaultAnimated());
}
protected function animationToArray()
{
return [
'animated' => $this->animated,
];
}
}

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,
];
}
}

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,
];
}
}

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());
}
}

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

@@ -3,15 +3,16 @@
namespace Asantibanez\LivewireCharts\Models; namespace Asantibanez\LivewireCharts\Models;
use Illuminate\Support\Collection;
/** /**
* Class LineChartModel * Class LineChartModel
* @package Asantibanez\LivewireCharts\Models * @package Asantibanez\LivewireCharts\Models
* @property boolean $isMultiLine
*/ */
class LineChartModel class LineChartModel extends BaseChartModel
{ {
public $title; public $isMultiLine;
public $animated;
public $data; public $data;
@@ -21,9 +22,9 @@ class LineChartModel
public function __construct() public function __construct()
{ {
$this->title = ''; parent::__construct();
$this->animated = false; $this->isMultiLine = false;
$this->onPointClickEventName = null; $this->onPointClickEventName = null;
@@ -32,16 +33,20 @@ class LineChartModel
$this->markers = collect(); $this->markers = collect();
} }
public function setTitle($title) public function multiLine()
{ {
$this->title = $title; $this->isMultiLine = true;
$this->data = collect();
return $this; return $this;
} }
public function setAnimated($animated) public function singleLine()
{ {
$this->animated = $animated; $this->isMultiLine = false;
$this->data = collect();
return $this; return $this;
} }
@@ -57,6 +62,22 @@ class LineChartModel
return $this; return $this;
} }
public function addSeriesPoint($seriesName, $title, $value, $extras = [])
{
$series = $this->data->get($seriesName, collect());
$series->push([
'seriesName' => $seriesName,
'title' => $title,
'value' => $value,
'extras' => $extras,
]);
$this->data->put($seriesName, $series);
return $this;
}
public function addMarker($title, public function addMarker($title,
$value, $value,
$strokeColor = 'green', $strokeColor = 'green',
@@ -85,20 +106,19 @@ class LineChartModel
public function toArray() public function toArray()
{ {
return [ return array_merge(parent::toArray(), [
'title' => $this->title, 'isMultiLine' => $this->isMultiLine,
'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)
{ {
$this->title = data_get($array, 'title', ''); parent::fromArray($array);
$this->animated = data_get($array, 'animated', false); $this->isMultiLine = data_get($array, 'isMultiLine', false);
$this->onPointClickEventName = data_get($array, 'onPointClickEventName', null); $this->onPointClickEventName = data_get($array, 'onPointClickEventName', null);
@@ -106,9 +126,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,12 +7,8 @@ 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 $animated;
public $onSliceClickEventName; public $onSliceClickEventName;
public $opacity; public $opacity;
@@ -21,9 +17,7 @@ class PieChartModel
public function __construct() public function __construct()
{ {
$this->title = ''; parent::__construct();
$this->animated = false;
$this->onSliceClickEventName = null; $this->onSliceClickEventName = null;
@@ -32,20 +26,6 @@ class PieChartModel
$this->data = collect(); $this->data = collect();
} }
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setAnimated($animated)
{
$this->animated = $animated;
return $this;
}
public function setOpacity($opacity) public function setOpacity($opacity)
{ {
$this->opacity = $opacity; $this->opacity = $opacity;
@@ -74,20 +54,16 @@ class PieChartModel
public function toArray() public function toArray()
{ {
return [ return array_merge(parent::toArray(), [
'title' => $this->title,
'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)
{ {
$this->title = data_get($array, 'title', ''); parent::fromArray($array);
$this->animated = data_get($array, 'animated', false);
$this->onSliceClickEventName = data_get($array, 'onSliceClickEventName', null); $this->onSliceClickEventName = data_get($array, 'onSliceClickEventName', null);
@@ -95,9 +71,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()));
}
} }