Neat Livewire Charts for your Laravel projects.
You can install the package via composer:
composer require asantibanez/livewire-charts
This package requires the following packages/libraries to work:
Laravel Livewire v2
(https://laravel-livewire.com/)Alpine Js
(https://github.com/alpinejs/alpine)Apex Charts
(https://apexcharts.com/)
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.
Livewire Charts supports out of the box the following types of charts
- Line Chart (
LivewireLineChart
component) - Pie Chart (
LivewirePieChart
component) - Column Chart (
LivewireColumnChart
component)
Each one comes with its own "model" class that allows you to define the chart's data and render behavior.
LivewireLineChart
usesLineChartModel
to set up data points, markers, events and other ui customizations.LivewirePieChart
usesPieChartModel
to set up data slices, events and other ui customizations.LivewireColumnChart
usesColumnChartModel
to set up data columns, 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
$columnChartModel =
(new ColumnChartModel())
->setTitle('Expenses by Type')
->addColumn('Food', 100, '#f6ad55')
->addColumn('Shopping', 200, '#fc8181')
->addColumn('Travel', 300, '#90cdf4')
;
Note: Chart model methods are chainable 💪
With $columnChartModel
at hand, we pass it to our LivewireColumnChart
component in our Blade template.
<livewire:livewire-column-chart
:column-chart-model="$columnChartModel"
/>
And that's it! You have a beautiful rendered chart in seconds. 👌
Note: You can use these charts inside other Livewire components too. Just render them in your Blade template and you are good to go. 👍
To enable click events, you must use the with[XXX]ClickEvent($eventName)
method present in every model class and
define a custom $eventName
that will be fired with the corresponding data when a column/marker/slice is clicked.
$columnChartModel =
(new ColumnChartModel())
->setTitle('Expenses by Type')
->withOnColumnClickEventName('onColumnClick')
;
Here we define an onColumnClick
event that will be fired when a column is clicked in our chart.
We can listen to the onClickEvent
registering a listener in any other Livewire component.
protected $listeners = [
'onColumnClick' => 'handleOnColumnClick',
];
You can use livewire-charts components as nested components in you Livewire components. Once rendered, charts will
not automatically react to changes in the $model
passed in. This is just how Livewire works.
However, to enable "reactivity" when data passed in changes, you can define a special $key
to your components so they are fully re-rendered each time the chart data changes.
Each model class comes with a reactiveKey()
method that returns a string based on its data. If any of the properties
are changed, this key will update accordingly and re-render the chart again.
In the following example, a parent component houses both column chart and pie chart and defines a $model
for each one.
The parent component renders the charts as follows
<livewire:livewire-column-chart
key="{{ $columnChartModel->reactiveKey() }}"
:column-chart-model="$columnChartModel"
/>
<livewire:livewire-pie-chart
key="{{ $pieChartModel->reactiveKey() }}"
:pie-chart-model="$pieChartModel"
/>
When the parent component changes their respective models, charts will automatically re-render itself.
For each chart, a specific model class needs to be used. Here, it is detailed the api available for each type of chart.
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 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 |
withOnPointClickEvent(string $eventName) | Event Name that will be fired when a point/marker of the chart is clicked |
Method | Description |
---|---|
setTitle(string $title) | Sets chart title |
setAnimated(boolean $animated) | Enables/disables animation |
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 |
onColumnClickEventName(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
Method | Description |
---|---|
setTitle(string $title) | Sets chart title |
setAnimated(boolean $animated) | Enables/disables animation |
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 |
withOnSliceClickEvent(string $eventName) | Event Name that will be fired when a column of the chart is clicked |
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email santibanez.andres@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.