• Stars
    star
    792
  • Rank 57,492 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Neat Livewire Charts for your Laravel projects

GitHub release

Livewire Charts

Preview

preview preview

Demo

https://github.com/asantibanez/livewire-charts-demo

Installation

You can install the package via composer:

composer require asantibanez/livewire-charts

Next, you must export the package public scripts. To do this run

php artisan livewire-charts:install

This command will export a vendor/livewire-charts folder under the public directory of your app which is used by the @livewireChartsScripts directive. More on this topic later.

Requirements

This package requires the following packages/libraries to work:

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.

Usage

Livewire Charts supports out of the box the following types of charts

  • Line/Multi Line Chart (LivewireLineChart component)
  • Pie Chart (LivewirePieChart component)
  • Column/Multi Line Chart (LivewireColumnChart component)
  • Area Chart (LivewireAreaChart component)
  • Radar Chart (LivewireRadarChart component)
  • Tree Map Chart (LivewireTreeMapChart component)

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.
  • 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.
  • LivewireAreaChart uses AreaChartModel to set up data points, events and other ui customizations.
  • LivewireRadarChart uses RadarChartModel to set up data points, events and other ui customizations.
  • LivewireTreeMapChart uses TreeMapChartModel to set up data blocks, 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
    {{-- key="{{ $columnChartModel->reactiveKey() }}" --}}
    :column-chart-model="$columnChartModel"
/>

Note: Optionally add "key" to enable props reactivity if needed 💪

Next, include the @livewireChartsScripts directive next to your other app scripts

<livewire:scripts />
@livewireChartsScripts

And that's it! You have a beautiful rendered chart in seconds. 👌

column chart example

Note: You can use these charts inside other Livewire components too. Just render them in your Blade template and you are good to go. 👍

LivewireCharts facade

Chart models can also be created using the LivewireCharts facade.

LivewireCharts::lineChartModel();
LivewireCharts::multiLineChartModel();
LivewireCharts::columnChartModel();
LivewireCharts::multiColumnChartModel();
LivewireCharts::pieChartModel();
LivewireCharts::areaChartModel();
LivewireCharts::radarChartModel();
LivewireCharts::treeMapChartModel();

Enabling Interactions

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.

#[On('onColumnClick')]
public function handleOnColumnClick($event)
{
   // Handle event
} 

"Reactive" Charts

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.

reactive charts example

Charts API

For each chart, a specific model class needs to be used. Here, it is detailed the api available for each type of chart.

All

Method Description
setTitle(string $title) Sets chart title
setAnimated(boolean $animated) Enables/disables animation
setDataLabelsEnabled(boolean $enabled) Enables/disables data labels
withDataLabels() Enables data labels
withoutDataLabels() Disables data labels
withLegend() Shows legends
withoutLegend() Hides legends
setColors(array $colors) Set colors for chart
addColors(string $color) Append $color to $colors set
setXAxisCategories(array $categories) Enables custom categories for chart X Axis
sparklined() Enables Apex Charts sparkline feature
setSparklineEnabled(boolean $isEnabled) Enables/Disables Apex Charts sparkline feature

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

LivewireColumnChart

Method Description
setOpacity(int $opacity) Sets columns' opacity
setColumnWidth(int $columnWidth) Sets columns' width
setHorizontal() Displays columns horizontally
setVertical() Displays columns vertically
multiColumn() Sets chart to display multiple column series
singleColumn() Sets chart to display a single column series
stacked() Sets chart to display column series stacked
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
addSeriesColumn(string $seriesName, string $title, double $value, array $extras = []) Adds a column to a multicolumn chart. $extras is forwarded on click event
withOnColumnClickEventName(string $eventName) Event Name that will be fired when a column of the chart is clicked

LivewirePieChart

Method Description
setOpacity(int $opacity) Sets slices' opacity
asPie() Displays chart as pie
asDonut() Displays chart as donut
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

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

LivewireRadarChart

Method Description
addSeries(string $seriesName, string $title, double $value, array $extras = [])
withOnPointClickEvent(string $eventName) Event Name that will be fired when a point of the chart is clicked

LivewireTreeMapChart

Method Description
addBlock(string $title, double $value, array $extras = []) Adds a block to the default series
addSeriesBlock(string $seriesName, string $title, double $value, array $extras = []) Adds a block to the specified series
withOnBlockClickEvent(string $eventName) Event Name that will be fired when a block of the chart is clicked
setDistributed(bool $distributed) Set whether the chart uses distribution or not

Advanced Usage - Integrating Scripts

The directive @livewireChartsScripts adds a script tag that includes public/vendor/livewire-charts/app.js. If you want to include this script in your build system, you can export the package scripts with php artisan vendor:publish and selecting livewire-charts:scripts. This will export js/vendor/livewire-charts inside your resources folder. You can then adjust imports as you see fit in your project.

Note: You must remove @livewireChartsScripts directive so it doesn't clash with the scripts in your build system.

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.

<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

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

livewire-calendar

Laravel Livewire component to show Events in a good looking monthly calendar
PHP
883
star
2

laravel-eloquent-state-machines

State Machines for your Laravel Eloquent models
PHP
523
star
3

livewire-select

Livewire component for dependant and/or searchable select inputs
PHP
499
star
4

laravel-blade-sortable

Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.
PHP
408
star
5

livewire-status-board

Livewire component to show records according to their current status
PHP
338
star
6

livewire-resource-time-grid

Laravel Livewire component to show Events by time and resource in a good looking grid
PHP
220
star
7

laravel-subscribable-notifications

Laravel Subscribable Notifications
PHP
142
star
8

Patio

A minimalistic Android view widget for selecting multiple images
Java
106
star
9

livewire-charts-demo

Livewire Charts demo app
PHP
69
star
10

Ranger

Android horizontally scrolled DatePicker
Java
57
star
11

livewire-dependant-select-demo

Laravel Livewire demo of multiple selects depending on each other values
PHP
48
star
12

Quota

Quota widget for Android
Java
31
star
13

livewire-calendar-demo

livewire-calendar component demo
PHP
23
star
14

laravel-eloquent-state-machines-demo

Demo repository for Laravel Eloquent State Machines package
PHP
17
star
15

OAuthWebView

WebViews for OAuth Authentication
Java
12
star
16

laravel-blade-sortable-demo

Demos for asantibanez/laravel-blade-sortable
PHP
11
star
17

livewire-wire-model-file-demo

Livewire lifecycle hook example for wire:model in file inputs
PHP
11
star
18

livewire-select-demo

Laravel app showcasing asantibanez/livewire-select component
PHP
10
star
19

practical

Practical ActiveRecord for DynamoDB
JavaScript
10
star
20

livewire-status-board-demo

Livewire Status Board demo app
PHP
7
star
21

Miveo

Vimeo SUPER AWESOME Android App
Java
4
star
22

laravel-inertia-infinite-scroll-demo

Demo for Infinite Scroll Feed in Laravel and InertiaJs
PHP
4
star
23

udacity-spotify-streamer-stage-2

Java
3
star
24

udacity-build-it-bigger

Udacity Build It Bigger Project
Java
2
star
25

udacity-go-ubiquitous

Project 6 of Udacity's Android Nanodegree program
Java
1
star
26

udacity-make-your-app-material

Project 5 of Udacity's Android Nanodegree program.
Java
1
star
27

vue-router-example

Vue.js project that uses Vue-Router
JavaScript
1
star
28

lienzo

Lienzo is a Horizontal/Vertical RecyclerView gallery powered by Picasso
Java
1
star
29

MarkdownViewer

Android Markdown View
Java
1
star
30

android-google-maps-test

Google Maps instructions for Android project
Java
1
star
31

laravel-paypal-server-checkout-test

PHP
1
star
32

livewire-charts-demo-livewire-v3

Livewire Charts Demo in Livewire v3
PHP
1
star
33

golden-layout-vue

Example of Golden Layout + Vue and Vuex integration
Vue
1
star
34

laravel-ecuadorian-taxpayer-validation-rule

Ecuadorian Taxpayer Validation Rule for Laravel
PHP
1
star
35

whatsapp-clone

WhatsApp Web UI clone using TailwindCss
Vue
1
star