Lavacharts is a graphing / chart library for PHP5.4+ that wraps Google's Javascript Chart API
Upgrade guide: Migrating from 2.5.x to 3.0.x
- Lava.js module for interacting with charts client-side
- AJAX data reloading
- Fetching charts
- Events integration
- Column Formatters
- Column Roles
- Blade template extensions for laravel
- Twig template extensions for Symfony
- Carbon support for date/datetime/timeofday columns
- Now supporting 12 Charts!
- Area, Bar, Calendar, Column, Combo, Donut, Gauge, Geo, Line, Pie, Scatter, Table
- DataTablePlus package can be added to parse CSV files or Eloquent collections into DataTables.
For complete documentation, please visit lavacharts.com
In your project's main composer.json
file, add this line to the requirements:
"khill/lavacharts": "~3.0"
Run Composer to install Lavacharts:
$ composer update
If you are using Lavacharts with Silex, Lumen or your own Composer project, that's no problem! Just make sure to:
require 'vendor/autoload.php';
within you project and create an instance of Lavacharts: $lava = new Khill\Lavacharts\Lavacharts;
To integrate lavacharts into Laravel, a ServiceProvider and Facade have been included.
Register Lavacharts in your app by adding this line to the end of the providers array in config/app.php
:
<?php
// config/app.php
// ...
'providers' => [
...
Khill\Lavacharts\Laravel\LavachartsServiceProvider::class,
],
'aliases' => [
...
'Lava' => Khill\Lavacharts\Laravel\LavachartsFacade::class,
]
Register Lavacharts in your app by adding this line to the end of the providers array in app/config/app.php
:
<?php
// app/config/app.php
// ...
'providers' => array(
// ...
'Khill\Lavacharts\Laravel\LavachartsServiceProvider',
),
'aliases' => array(
...
'Lava' => 'Khill\Lavacharts\Laravel\LavachartsFacade',
)
Also included is a Bundle for Symfony to create a service that can be pulled from the Container.
Add the bundle to the AppKernel:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Khill\Lavacharts\Symfony\Bundle\LavachartsBundle(),
);
// ...
}
// ...
}
Add the service definition to the app/config/config.yml
file
imports:
# ...
- { resource: @LavachartsBundle/Resources/config/services.yml
The creation of charts is separated into two parts: First, within a route or controller, you define the chart, the data table, and the customization of the output.
Second, within a view, you use one line and the library will output all the necessary javascript code for you.
Here is an example of the simplest chart you can create: A line chart with one dataset and a title, no configuration.
$stocksTable = $lava->DataTable(); // Lava::DataTable() if using Laravel
$stocksTable->addDateColumn('Day of Month')
->addNumberColumn('Projected')
->addNumberColumn('Official');
// Random Data For Example
for ($a = 1; $a < 30; $a++)
{
$rowData = [
"2014-8-$a", rand(800,1000), rand(800,1000)
];
$stocksTable->addRow($rowData);
}
Arrays work for datatables as well...
$stocksTable->addColumns([
['date', 'Day of Month'],
['number', 'Projected'],
['number', 'Official']
]];
...and for setting chart options!
$lava->LineChart('Stocks', $stocksTable, ['title' => 'Stock Market Trends']);
If you are using Laravel and the Blade templating engine, there are some nifty extensions thrown in for a cleaner view
@linechart('Stocks', 'stocks-div');
// Behind the scenes this just calls Lava::renderLineChart('Stocks', 'stocks-div')
// which is an alias for the render method, seen below
Or you can use the new render method, passing in the chart type, label, and element id.
echo Lava::render('LineChart', 'Stocks', 'stocks-div');
This is all assuming you already have a div in your page with the id "stocks-div":
<div id="stocks-div"></div>
If you don't have a div ready to accept the charts, add one more parameter to @linechart()
or render()
and it will be created for you.
Add true
to for the library to create a plain div, or an array with keys width & height
Example:
@linechart('Stocks', 'stocks-div', true)
// Or
echo Lava::render('LineChart', 'Stocks', 'stocks-div', ['width'=>1024, 'height'=>768]);
The complete changelog can be found here