Lavacharts abstracts Google's Javascript Chart API to PHP
Found a bug? Have an issue? Come visit the project on Github and let me know.
Get up and running within minutes, using the PHP package manager, Composer.
Click Here to check out Composer, if you haven't heard of it
First, copy this line into your project's composer.json file, in the "require": {} section
"khill/lavacharts" : "2.5.*"
composer update
Finally, require 'vendor/autoload.php'; in your project and you are good to go!
If you are using Laravel...
Do not require the vendor/autoload.php file, since Laravel takes care of that.
And also, register the service provider by adding this line to the providers array in app/config/app.php
"Khill\Lavacharts\Laravel\LavachartsServiceProvider"
Don't worry about adding the class alias, it is loaded automatically.
Remember, while using Laravel, replace $lava-> with Lava::
First, grab the most recent copy of the source from Github using the this link:
You will also need to get a copy of the Carbon Library since it is a dependency of DataTables.
Then, include Carbon and Lavacharts and create a new instance.
include(/* path to unzipped carbon archive */ . '/src/Carbon/Carbon.php')
include(/* path to unzipped lavacharts archive */ . '/src/Lavacharts.php');
use \Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts;
Creating a new DataTable
The following is a simple example, just to get you up and running.
For an in-depth look at DataTables, Click Here or use the link from the menu.
$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 = array(
"2014-8-$a", rand(800,1000), rand(800,1000)
);
$stocksTable->addRow($rowData);
}
$chart = $lava->LineChart('myFancyChart'); // Lava::LineChart() if using Laravel
$chart->datatable($stocksTable);
// You could also pass an associative array to setOptions() method
// $chart->setOptions(array(
// 'datatable' => $stocksTable
// ));
Rendering the chart
There are two ways to get your chart to display on your page.
Syntax: render(chartType, chartLabel, elementId [, bool | array(height, width)])
// Example #1, output into a div you already created
// <div id="myStocks"></div>
echo $lava->render('LineChart', 'myFancyChart', 'myStocks');
// Example #2, have the library create the div
echo $lava->render('LineChart', 'myFancyChart', 'myStocks', true);
// Example #3, have the library create the div with a fixed size
echo $lava->render('LineChart', 'myFancyChart', 'myStocks', array('height'=>300, 'width'=>600));
Alternatively, the render method is available from the Chart object as well, bypassing the need for inputing the type and title of the chart.
// After creating the chart with $linechart = $lava->LineChart() or any other type,
// pass the Chart object to your view and call the render method with a div's elementId
echo $linechart->render('myStocks');
These are the methods available via the main Lavacharts class.
This method with output the script tag to the google jsapi, along with some core javascript for lavacharts.
Use with Angular so the script tag is not placed inside an ng-view and can be manually placed in the header.
If you do not need manual placement, ignore this method and the render methods will automatically add the script tags.
Any valid chart object, capitalized: LineChart, BarChart, PieChart, etc...
Any valid config object, capitalized: TextStyle, HorizontalAxis, Annotation, etc...
Any valid format object, capitalized: DateFormat, NumberFormat
Any valid event object, capitalized: Ready, MouseOver, etc...
There are many ways to add columns to the DataTable, using strings, arrays, or an array of arrays.
Also provided are simplified, alias methods, which will automatically fill the type and colLabel.
Other column related methods
Add rows either by chaining the single row command, or batch adding with the addRows() method.
Other row related methods
Columns can have formats applied so the data/labels will be shown according to the format definition. The two available are:
All of the methods return the Format object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Format's constructor
Formats are created the same way as ConfigObjects
DateFormat
Explanations of the methods can be found in Google's Documentation
NumberFormat
Explanations of the methods can be found in Google's Documentation
Set the timezone to be used for Date columns, defaults to America/Los_Angeles
If using string dates, maybe to represent only years such as "2000" and they are not being interpreted correctly, then use this method to set the format string for the whole DataTable.
Use a format string following the DateTime format
Get the JSON representation of a DataTable, useful for reloading charts via AJAX. (See Example)
Select a chart from the dropdown menu to see the different configuration methods / options available.
All of the methods below are also available to all of the charts, as the each extend the parent Chart
All of the methods (except for setOptions) return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
The donut chart extends the pie chart with one extra method
All the configuration options are the same as the pie chart.
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
All of the methods return the Chart to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the chart's setOptions() method
Explanations for each configuration option can be found in Google's Config Docs
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$temperatures = $lava->DataTable();
$temperatures->addDateColumn('Date')
->addNumberColumn('Max Temp')
->addNumberColumn('Mean Temp')
->addNumberColumn('Min Temp')
->addRow(array('2014-10-1', 67, 65, 62))
->addRow(array('2014-10-2', 68, 65, 61))
->addRow(array('2014-10-3', 68, 62, 55))
->addRow(array('2014-10-4', 72, 62, 52))
->addRow(array('2014-10-5', 61, 54, 47))
->addRow(array('2014-10-6', 70, 58, 45))
->addRow(array('2014-10-7', 74, 70, 65))
->addRow(array('2014-10-8', 75, 69, 62))
->addRow(array('2014-10-9', 69, 63, 56))
->addRow(array('2014-10-10', 64, 58, 52))
->addRow(array('2014-10-11', 59, 55, 50))
->addRow(array('2014-10-12', 65, 56, 46))
->addRow(array('2014-10-13', 66, 56, 46))
->addRow(array('2014-10-14', 75, 70, 64))
->addRow(array('2014-10-15', 76, 72, 68))
->addRow(array('2014-10-16', 71, 66, 60))
->addRow(array('2014-10-17', 72, 66, 60))
->addRow(array('2014-10-18', 63, 62, 62));
$linechart = $lava->LineChart('Temps')
->dataTable($temperatures)
->title('Weather in October');
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $linechart or $lava to your view to render.
<div id="temps_div"></div>
// With the chart object
<? echo $linechart->render('temps_div') ?>
// With the lava object
<? echo $lava->render('LineChart', 'Temps', 'temps_div') ?>
<div id="temps_div"></div>
// With Lava class alias
<? echo Lava::render('LineChart', 'Temps', 'temps_div') ?>
// With Blade Templates
@linechart('Temps', 'temps_div')
lava.charts.LineChart.Temps.draw = function() {
var $this = lava.charts.LineChart.Temps;
$this.data = new google.visualization.DataTable({"cols":[{"type":"date","label":"Date"},{"type":"number","label":"Max Temp"},{"type":"number","label":"Mean Temp"},{"type":"number","label":"Min Temp"}],"rows":[{"c":[{"v":new Date(2014,9,1,0,0,0)},{"v":67},{"v":65},{"v":62}]},{"c":[{"v":new Date(2014,9,2,0,0,0)},{"v":68},{"v":65},{"v":61}]},{"c":[{"v":new Date(2014,9,3,0,0,0)},{"v":68},{"v":62},{"v":55}]},{"c":[{"v":new Date(2014,9,4,0,0,0)},{"v":72},{"v":62},{"v":52}]},{"c":[{"v":new Date(2014,9,5,0,0,0)},{"v":61},{"v":54},{"v":47}]},{"c":[{"v":new Date(2014,9,6,0,0,0)},{"v":70},{"v":58},{"v":45}]},{"c":[{"v":new Date(2014,9,7,0,0,0)},{"v":74},{"v":70},{"v":65}]},{"c":[{"v":new Date(2014,9,8,0,0,0)},{"v":75},{"v":69},{"v":62}]},{"c":[{"v":new Date(2014,9,9,0,0,0)},{"v":69},{"v":63},{"v":56}]},{"c":[{"v":new Date(2014,9,10,0,0,0)},{"v":64},{"v":58},{"v":52}]},{"c":[{"v":new Date(2014,9,11,0,0,0)},{"v":59},{"v":55},{"v":50}]},{"c":[{"v":new Date(2014,9,12,0,0,0)},{"v":65},{"v":56},{"v":46}]},{"c":[{"v":new Date(2014,9,13,0,0,0)},{"v":66},{"v":56},{"v":46}]},{"c":[{"v":new Date(2014,9,14,0,0,0)},{"v":75},{"v":70},{"v":64}]},{"c":[{"v":new Date(2014,9,15,0,0,0)},{"v":76},{"v":72},{"v":68}]},{"c":[{"v":new Date(2014,9,16,0,0,0)},{"v":71},{"v":66},{"v":60}]},{"c":[{"v":new Date(2014,9,17,0,0,0)},{"v":72},{"v":66},{"v":60}]},{"c":[{"v":new Date(2014,9,18,0,0,0)},{"v":63},{"v":62},{"v":62}]}]}, 0.6);
$this.options = {"title":"Weather in October"};
$this.chart = new google.visualization.LineChart(document.getElementById('chart'));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(lava.charts.LineChart.Temps.draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$population = $lava->DataTable();
$population->addDateColumn('Year')
->addNumberColumn('Number of People')
->addRow(array('2006', 623452))
->addRow(array('2007', 685034))
->addRow(array('2008', 716845))
->addRow(array('2009', 757254))
->addRow(array('2010', 778034))
->addRow(array('2011', 792353))
->addRow(array('2012', 839657))
->addRow(array('2013', 842367))
->addRow(array('2014', 873490));
$areachart = $lava->AreaChart('Population')
->setOptions(array(
'datatable' => $population,
'title' => 'Population Growth',
'legend' => $lava->Legend(array(
'position' => 'in'
))
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $areachart or $lava to your view to render.
<div id="pop_div"></div>
// With the chart object
<? echo $areachart->render('pop_div') ?>
// With the lava object
<? echo $lava->render('AreaChart', 'Population', 'pop_div') ?>
<div id="pop_div"></div>
// With Lava class alias
<? echo Lava::render('AreaChart', 'Population', 'pop_div') ?>
// With Blade Templates
@areachart('Population', 'pop_div')
lava.charts.AreaChart = {"Population":{chart:null,draw:null,data:null,options:null,formats:[]}};
if (!document.getElementById('chart')){console.error('[Lavaharts] No matching element was found with ID "chart"');}
lava.charts.AreaChart.Population.draw = function() {
var $this = lava.charts.AreaChart.Population;
$this.data = new google.visualization.DataTable({"cols":[{"type":"date","label":"Year","id":"date"},{"type":"number","label":"Number of People","id":"pop"}],"rows":[{"c":[{"v":new Date(2014,11,19,20,6,0)},{"v":623452}]},{"c":[{"v":new Date(2014,11,19,20,7,0)},{"v":685034}]},{"c":[{"v":new Date(2014,11,19,20,8,0)},{"v":716845}]},{"c":[{"v":new Date(2014,11,19,20,9,0)},{"v":757254}]},{"c":[{"v":new Date(2014,11,19,20,10,0)},{"v":778034}]},{"c":[{"v":new Date(2014,11,19,20,11,0)},{"v":792353}]},{"c":[{"v":new Date(2014,11,19,20,12,0)},{"v":839657}]},{"c":[{"v":new Date(2014,11,19,20,13,0)},{"v":842367}]},{"c":[{"v":new Date(2014,11,19,20,14,0)},{"v":873490}]}]}, 0.6);
$this.options = {"title":"Population Growth","legend":{"position":"in"}};
$this.chart = new google.visualization.AreaChart(document.getElementById('chart'));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(lava.charts.AreaChart.Population.draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$votes = $lava->DataTable();
$votes->addStringColumn('Food Poll')
->addNumberColumn('Votes')
->addRow(array('Tacos', rand(1000,5000)))
->addRow(array('Salad', rand(1000,5000)))
->addRow(array('Pizza', rand(1000,5000)))
->addRow(array('Apples', rand(1000,5000)))
->addRow(array('Fish', rand(1000,5000)));
$lava->BarChart('Votes')
->setOptions(array(
'datatable' => $votes
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $barchart or $lava to your view to render.
<div id="poll_div"></div>
// With the chart object
<? echo $barchart->render('poll_div') ?>
// With the lava object
<? echo $lava->render('BarChart', 'Food Poll', 'poll_div') ?>
<div id="poll_div"></div>
// With Lava class alias
<? echo Lava::render('BarChart', 'Food Poll', 'poll_div') ?>
// With Blade Templates
@barchart('Food Poll', 'poll_div')
lava.charts.BarChart["Votes"] = {chart:null,draw:null,data:null,options:null,formats:[]};
if (!document.getElementById("chart")){console.error("[Lavacharts] No matching element was found with ID \"chart\"");}
lava.charts.BarChart["Votes"].draw = function() {
var $this = lava.charts.BarChart["Votes"];
$this.data = new google.visualization.DataTable({"cols":[{"type":"string","label":"Food"},{"type":"number","label":"Votes"}],"rows":[{"c":[{"v":"Tacos"},{"v":1448}]},{"c":[{"v":"Salad"},{"v":2690}]},{"c":[{"v":"Pizza"},{"v":3353}]},{"c":[{"v":"Apples"},{"v":4465}]},{"c":[{"v":"Fish"},{"v":3231}]}]}, 0.6);
$this.options = [];
$this.chart = new google.visualization.BarChart(document.getElementById("chart"));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(lava.charts.BarChart["Votes"].draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$finances = $lava->DataTable();
$finances->addDateColumn('Year')
->addNumberColumn('Sales')
->addNumberColumn('Expenses')
->setDateTimeFormat('Y')
->addRow(array('2004', 1000, 400))
->addRow(array('2005', 1170, 460))
->addRow(array('2006', 660, 1120))
->addRow(array('2007', 1030, 54));
$columnchart = $lava->ColumnChart('Finances')
->setOptions(array(
'datatable' => $finances,
'title' => 'Company Performance',
'titleTextStyle' => $lava->TextStyle(array(
'color' => '#eb6b2c',
'fontSize' => 14
))
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $columnchart or $lava to your view to render.
<div id="perf_div"></div>
// With the chart object
<? echo $columnchart->render('perf_div') ?>
// With the lava object
<? echo $lava->render('ColumnChart', 'Finances', 'perf_div') ?>
<div id="perf_div"></div>
// With Lava class alias
<? echo Lava::render('ColumnChart', 'Finances', 'perf_div') ?>
// With Blade Templates
@columnchart('Finances', 'perf_div')
lava.charts.AreaChart = {"Population":{chart:null,draw:null,data:null,options:null,formats:[]}};
if (!document.getElementById('chart')){console.error('[Lavaharts] No matching element was found with ID "chart"');}
lava.charts.AreaChart.Population.draw = function() {
var $this = lava.charts.AreaChart.Population;
$this.data = new google.visualization.DataTable({"cols":[{"type":"date","label":"Year","id":"date"},{"type":"number","label":"Number of People","id":"pop"}],"rows":[{"c":[{"v":new Date(2014,11,19,20,6,0)},{"v":623452}]},{"c":[{"v":new Date(2014,11,19,20,7,0)},{"v":685034}]},{"c":[{"v":new Date(2014,11,19,20,8,0)},{"v":716845}]},{"c":[{"v":new Date(2014,11,19,20,9,0)},{"v":757254}]},{"c":[{"v":new Date(2014,11,19,20,10,0)},{"v":778034}]},{"c":[{"v":new Date(2014,11,19,20,11,0)},{"v":792353}]},{"c":[{"v":new Date(2014,11,19,20,12,0)},{"v":839657}]},{"c":[{"v":new Date(2014,11,19,20,13,0)},{"v":842367}]},{"c":[{"v":new Date(2014,11,19,20,14,0)},{"v":873490}]}]}, 0.6);
$this.options = {"title":"Population Growth","legend":{"position":"in"}};
$this.chart = new google.visualization.AreaChart(document.getElementById('chart'));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(lava.charts.AreaChart.Population.draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$finances = $lava->DataTable();
$finances->addDateColumn('Year')
->addNumberColumn('Sales')
->addNumberColumn('Expenses')
->addNumberColumn('Net Worth')
->addRow(array('2009-1-1', 1100, 490, 1324))
->addRow(array('2010-1-1', 1000, 400, 1524))
->addRow(array('2011-1-1', 1400, 450, 1351))
->addRow(array('2012-1-1', 1250, 600, 1243))
->addRow(array('2013-1-1', 1100, 550, 1462));
$combochart = $lava->ComboChart('Finances')
->setOptions(array(
'datatable' => $finances,
'title' => 'Company Performance',
'titleTextStyle' => $lava->TextStyle(array(
'color' => 'rgb(123, 65, 89)',
'fontSize' => 16
)),
'legend' => $lava->Legend(array(
'position' => 'in'
)),
'seriesType' => 'bars',
'series' => array(
2 => $lava->Series(array(
'type' => 'line'
))
)
));
// Note: In the series config, '2' correlates to the 3rd horizontal axis dataset.
// The DateColumn is the VerticalAxis, so 'Net Worth' is the 3rd, key 2, in the series.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $combochart or $lava to your view to render.
<div id="finances-div"></div>
// With the chart object
<? echo $combochart->render('finances-div') ?>
// With the lava object
<? echo $lava->render('ComboChart', 'Finances', 'finances-div') ?>
<div id="finances-div"></div>
// With Lava class alias
<? echo Lava::render('ComboChart', 'Finances', 'finances-div') ?>
// With Blade Templates
@combochart('Finances', 'finances-div')
lava.charts.ColumnChart = {"Finances":{chart:null,draw:null,data:null,options:null,formats:[]}};
if (!document.getElementById('chart')){console.error('[Lavaharts] No matching element was found with ID "chart"');}
lava.charts.ColumnChart.Finances.draw = function() {
var $this = lava.charts.ColumnChart.Finances;
$this.data = new google.visualization.DataTable({"cols":[{"type":"date","label":"Year"},{"type":"number","label":"Sales"},{"type":"number","label":"Expenses"}],"rows":[{"c":[{"v":new Date(2004,11,19,22,34,51)},{"v":1000},{"v":400}]},{"c":[{"v":new Date(2005,11,19,22,34,51)},{"v":1170},{"v":460}]},{"c":[{"v":new Date(2006,11,19,22,34,51)},{"v":660},{"v":1120}]},{"c":[{"v":new Date(2007,11,19,22,34,51)},{"v":1030},{"v":54}]}]}, 0.6);
$this.options = {"title":"Company Performance","titleTextStyle":{"color":"#eb6b2c","fontSize":14}};
$this.chart = new google.visualization.ColumnChart(document.getElementById('chart'));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(lava.charts.ColumnChart.Finances.draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$reasons = $lava->DataTable();
$reasons->addStringColumn('Reasons')
->addNumberColumn('Percent')
->addRow(array('Check Reviews', 5))
->addRow(array('Watch Trailers', 2))
->addRow(array('See Actors Other Work', 4))
->addRow(array('Settle Argument', 89));
$piechart = $lava->PieChart('IMDB')
->setOptions(array(
'datatable' => $reasons,
'title' => 'Reasons I visit IMDB',
'is3D' => true,
'slices' => array(
$lava->Slice(array(
'offset' => 0.2
)),
$lava->Slice(array(
'offset' => 0.25
)),
$lava->Slice(array(
'offset' => 0.3
))
)
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $piechart or $lava to your view to render.
<div id="chart-div"></div>
// With the chart object
<? echo $piechart->render('chart-div') ?>
// With the lava object
<? echo $lava->render('PieChart', 'IMDB', 'chart-div') ?>
<div id="chart-div"></div>
// With Lava class alias
<? echo Lava::render('PieChart', 'IMDB', 'chart-div') ?>
// With Blade Templates
@piechart('IMDB', 'chart-div')
lava.charts.PieChart = {"IMDB":{chart:null,draw:null,data:null,options:null,formats:[]}};
if (!document.getElementById('chart')){console.error('[Lavaharts] No matching element was found with ID "chart"');}
lava.charts.PieChart.IMDB.draw = function() {
var $this = lava.charts.PieChart.IMDB;
$this.data = new google.visualization.DataTable({"cols":[{"type":"string","label":"Reasons"},{"type":"number","label":"Percent"}],"rows":[{"c":[{"v":"Check Reviews"},{"v":5}]},{"c":[{"v":"Watch Trailers"},{"v":2}]},{"c":[{"v":"See Actors Other Work"},{"v":4}]},{"c":[{"v":"Settle Argument"},{"v":89}]}]}, 0.6);
$this.options = {"title":"Reasons I visit IMDB","is3D":true,"slices":[{"offset":0.2},{"offset":0.25},{"offset":0.3}]};
$this.chart = new google.visualization.PieChart(document.getElementById('chart'));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(lava.charts.PieChart.IMDB.draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$reasons = $lava->DataTable();
$reasons->addStringColumn('Reasons')
->addNumberColumn('Percent')
->addRow(array('Check Reviews', 5))
->addRow(array('Watch Trailers', 2))
->addRow(array('See Actors Other Work', 4))
->addRow(array('Settle Argument', 89));
$donutchart = $lava->DonutChart('IMDB')
->setOptions(array(
'datatable' => $reasons,
'title' => 'Reasons I visit IMDB'
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $donutchart or $lava to your view to render.
<div id="chart-div"></div>
// With the chart object
<? echo $donutchart->render('IMDB', 'chart-div') ?>
// With the lava object
<? echo $lava->render('DonutChart', 'IMDB', 'chart-div') ?>
<div id="chart-div"></div>
// With Lava class alias
<? echo Lava::render('DonutChart', 'IMDB', 'chart-div') ?>
// With Blade Templates
@donutchart('IMDB', 'chart-div')
lava.charts.DonutChart = {"IMDB":{chart:null,draw:null,data:null,options:null,formats:[]}};
if (!document.getElementById('chart')){console.error('[Lavaharts] No matching element was found with ID "chart"');}
lava.charts.DonutChart.IMDB.draw = function() {
var $this = lava.charts.DonutChart.IMDB;
$this.data = new google.visualization.DataTable({"cols":[{"type":"string","label":"Reasons"},{"type":"number","label":"Percent"}],"rows":[{"c":[{"v":"Check Reviews"},{"v":5}]},{"c":[{"v":"Watch Trailers"},{"v":2}]},{"c":[{"v":"See Actors Other Work"},{"v":4}]},{"c":[{"v":"Settle Argument"},{"v":89}]}]}, 0.6);
$this.options = {"pieHole":0.5,"title":"Reasons I visit IMDB"};
$this.chart = new google.visualization.PieChart(document.getElementById('chart'));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(lava.charts.DonutChart.IMDB.draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$popularity = $lava->DataTable();
$popularity->addStringColumn('Country')
->addNumberColumn('Popularity')
->addRow(array('Germany', 200))
->addRow(array('United States', 300))
->addRow(array('Brazil', 400))
->addRow(array('Canada', 500))
->addRow(array('France', 600))
->addRow(array('RU', 700));
$geochart = $lava->GeoChart('Popularity')
->setOptions(array(
'datatable' => $popularity
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $geochart or $lava to your view to render.
<div id="pop-div"></div>
// With the chart object
<? echo $geochart->render('pop-div') ?>
// With the lava object
<? echo $lava->render('GeoChart', 'Popularity', 'pop-div') ?>
<div id="pop-div"></div>
// With Lava class alias
<? echo Lava::render('GeoChart', 'Popularity', 'pop-div') ?>
// With Blade Templates
@geochart('Popularity', 'pop-div')
lava.charts.GeoChart = {"Popularity":{chart:null,draw:null,data:null,options:null,formats:[]}};
if (!document.getElementById('chart')){console.error('[Lavaharts] No matching element was found with ID "chart"');}
lava.charts.GeoChart.Popularity.draw = function() {
var $this = lava.charts.GeoChart.Popularity;
$this.data = new google.visualization.DataTable({"cols":[{"type":"string","label":"Country"},{"type":"number","label":"Popularity"}],"rows":[{"c":[{"v":"Germany"},{"v":200}]},{"c":[{"v":"United States"},{"v":300}]},{"c":[{"v":"Brazil"},{"v":400}]},{"c":[{"v":"Canada"},{"v":500}]},{"c":[{"v":"France"},{"v":600}]},{"c":[{"v":"RU"},{"v":700}]}]}, 0.6);
$this.options = [];
$this.chart = new google.visualization.GeoChart(document.getElementById('chart'));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['geochart']});
google.setOnLoadCallback(lava.charts.GeoChart.Popularity.draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$sales = $lava->DataTable();
$sales->addDateColumn('Date')
->addNumberColumn('Orders');
foreach (range(2, 5) as $month) {
for ($a=0; $a < 20; $a++) {
$day = rand(1, 30);
$sales->addRow(array("2014-${month}-${day}", rand(0,100)));
}
}
$calendarchart = $lava->CalendarChart('Sales')
->setOptions(array(
'datatable' => $sales,
'title' => 'Cars Sold',
'unusedMonthOutlineColor' => $lava->Stroke(array(
'stroke' => '#ECECEC',
'strokeOpacity' => 0.75,
'strokeWidth' => 1
)),
'dayOfWeekLabel' => $lava->TextStyle(array(
'color' => '#4f5b0d',
'fontSize' => 16,
'italic' => true
)),
'noDataPattern' => $lava->Color(array(
'color' => '#DDD',
'backgroundColor' => '#11FFFF'
)),
'colorAxis' => $lava->ColorAxis(array(
'values' => array(0, 100),
'colors' => array('black', 'green')
))
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $calendarchart or $lava to your view to render.
<div id="sales_div"></div>
// With the chart object
<? echo $calendarchart->render('sales_div') ?>
// With the lava object
<? echo $lava->render('CalendarChart', 'Sales', 'sales_div') ?>
<div id="sales_div"></div>
// With Lava class alias
<? echo Lava::render('CalendarChart', 'Sales', 'sales_div') ?>
// With Blade Templates
@calendarchart('Sales', 'sales_div')
lava.charts.CalendarChart["Sales"] = {chart:null,draw:null,data:null,options:null,formats:[]};
if (!document.getElementById("chart")){console.error("[Lavacharts] No matching element was found with ID \"chart\"");}
lava.charts.CalendarChart["Sales"].draw = function() {
var $this = lava.charts.CalendarChart["Sales"];
$this.data = new google.visualization.DataTable({"cols":[{"type":"date","label":"Date"},{"type":"number","label":"Orders"}],"rows":[{"c":[{"v":new Date(2014,1,28,0,0,0)},{"v":33}]},{"c":[{"v":new Date(2014,1,11,0,0,0)},{"v":27}]},{"c":[{"v":new Date(2014,1,7,0,0,0)},{"v":96}]},{"c":[{"v":new Date(2014,1,18,0,0,0)},{"v":96}]},{"c":[{"v":new Date(2014,1,14,0,0,0)},{"v":76}]},{"c":[{"v":new Date(2014,1,21,0,0,0)},{"v":25}]},{"c":[{"v":new Date(2014,1,24,0,0,0)},{"v":57}]},{"c":[{"v":new Date(2014,1,13,0,0,0)},{"v":79}]},{"c":[{"v":new Date(2014,1,17,0,0,0)},{"v":20}]},
/* Some data omitted for brevity */
{"c":[{"v":new Date(2014,1,20,0,0,0)},{"v":17}]},{"c":[{"v":new Date(2014,1,5,0,0,0)},{"v":59}]},{"c":[{"v":new Date(2014,1,11,0,0,0)},{"v":83}]},{"c":[{"v":new Date(2014,1,26,0,0,0)},{"v":6}]},{"c":[{"v":new Date(2014,1,7,0,0,0)},{"v":37}]},{"c":[{"v":new Date(2014,1,23,0,0,0)},{"v":4}]},{"c":[{"v":new Date(2014,2,1,0,0,0)},{"v":69}]},{"c":[{"v":new Date(2014,4,3,0,0,0)},{"v":67}]},{"c":[{"v":new Date(2014,4,21,0,0,0)},{"v":53}]},{"c":[{"v":new Date(2014,4,28,0,0,0)},{"v":9}]},{"c":[{"v":new Date(2014,4,10,0,0,0)},{"v":21}]},{"c":[{"v":new Date(2014,4,9,0,0,0)},{"v":24}]},{"c":[{"v":new Date(2014,4,27,0,0,0)},{"v":55}]},{"c":[{"v":new Date(2014,4,15,0,0,0)},{"v":44}]},{"c":[{"v":new Date(2014,4,9,0,0,0)},{"v":78}]}]}, 0.6);
$this.options = {"calendar":{"unusedMonthOutlineColor":{"stroke":"#ECECEC","strokeOpacity":0.75,"strokeWidth":1},"dayOfWeekLabel":{"color":"#4f5b0d","fontSize":16,"italic":true}},"title":"Cars Sold","noDataPattern":{"color":"#DDD","backgroundColor":"#11FFFF"},"colorAxis":{"values":[0,100],"colors":["black","green"]}};
$this.chart = new google.visualization.Calendar(document.getElementById("chart"));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1.1', {'packages':['calendar']});
google.setOnLoadCallback(lava.charts.CalendarChart["Sales"].draw);
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts; // See note below for Laravel
$temps = $lava->DataTable();
$temps->addStringColumn('Type')
->addNumberColumn('Value')
->addRow(array('CPU', rand(0,100)))
->addRow(array('Case', rand(0,100)))
->addRow(array('Graphics', rand(0,100)));
$lava->GaugeChart('Temps')
->setOptions(array(
'datatable' => $temps,
'width' => 400,
'greenFrom' => 0,
'greenTo' => 69,
'yellowFrom' => 70,
'yellowTo' => 89,
'redFrom' => 90,
'redTo' => 100,
'majorTicks' => array(
'Safe',
'Critical'
)
));
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $gaugechart or $lava to your view to render.
<div id="temps_div"></div>
// With the chart object
<? echo $gaugechart->render('temps_div') ?>
// With the lava object
<? echo $lava->render('GaugeChart', 'Temps', 'temps_div') ?>
<div id="temps_div"></div>
// With Lava class alias
<? echo Lava::render('GaugeChart', 'Temps', 'temps_div') ?>
// With Blade Templates
@gaugechart('Temps', 'temps_div')
lava.charts.GaugeChart["Temps"] = {chart:null,draw:null,data:null,options:null,formats:[]};
if (!document.getElementById("chart")){console.error("[Lavacharts] No matching element was found with ID \"chart\"");}
lava.charts.GaugeChart["Temps"].draw = function() {
var $this = lava.charts.GaugeChart["Temps"];
$this.data = new google.visualization.DataTable({"cols":[{"type":"string","label":"Type"},{"type":"number","label":"Value"}],"rows":[{"c":[{"v":"CPU"},{"v":65}]},{"c":[{"v":"Case"},{"v":86}]},{"c":[{"v":"Graphics"},{"v":11}]}]}, 0.6);
$this.options = {"width":400,"greenFrom":0,"greenTo":69,"yellowFrom":70,"yellowTo":89,"redFrom":90,"redTo":100,"majorTicks":["Safe","Critical"]};
$this.chart = new google.visualization.Gauge(document.getElementById("chart"));
$this.chart.draw($this.data, $this.options);
};
google.load('visualization', '1', {'packages':['gauge']});
google.setOnLoadCallback(lava.charts.GaugeChart["Temps"].draw);
All of the config methods return themselves to enable the chaining of options.
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Config's constructor
$lava->TextStyle([
'color' => '#F4C6B2',
'fontName' => 'Arial',
'fontSize' => 16
]);
$lava->TextStyle()
->color('#F4C6B2')
->fontName('Arial')
->fontSize(16);
Lava::TextStyle([
'color' => '#F4C6B2',
'fontName' => 'Arial',
'fontSize' => 16
]);
All of the methods return the Annotation object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Annotation's constructor
All of the methods return the BackgroundColor object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the BackgroundColor's constructor
All of the methods return the BoxStyle object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the BoxStyle's constructor
All of the methods return the ChartArea object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the ChartArea's constructor
All of the methods return the Color object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Color's constructor
All of the methods return the ColorAxis object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the ColorAxis's constructor
All of the methods return the Gradient object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Gradient's constructor
All of the methods return the HorizontalAxis object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the HorizontalAxis's constructor
All of the methods return the Legend object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Legend's constructor
The MagnifyingGlass object is created by passing an int to the constructor to set the ZoomFactor
The default zoomFactor is 5, and can also be set with the named method.
All of the methods return the Series object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Series's constructor
All of the methods return the SizeAxis object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the SizeAxis's constructor
All of the methods return the Slice object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Slice's constructor
All of the methods return the Stroke object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Stroke's constructor
All of the methods return the TextStyle object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the TextStyle's constructor
All of the methods return the Tooltip object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the Tooltip's constructor
All of the methods return the VerticalAxis object to enable the chaining of configuration options
Alternatively, you can build an associative array, using the method names as keys, and pass the array into the VerticalAxis's constructor
Enable interaction with your charts through events
Adding an event your chart is as simple as defining a javascript function in your page, then attaching the Lava event object to the chart.
Pass the event's constructor the function name, and when the chart event is fired, the function will be called.
As with any chart customization, you can use the named method, events(), and pass in an array of events.
Or you can add the key 'events' to a config array with and pass an array of events there.
$ready = $lava->Ready('readyCallback'); // Lava::Ready() if using Laravel
More information can be found on Google's Website
$mouseOut = $lava->MouseOut('mouseOutCallback'); // Lava::MouseOut() if using Laravel
$mouseOver = $lava->MouseOver('mouseOverCallback'); // Lava::MouseOver() if using Laravel
$select = $lava->Select('selectCallback'); // Lava::Select() if using Laravel
More information can be found on Google's Website
To use events with your charts, follow these two easy steps:
When the chart is rendered, your defined function will be wrapped with lava.event() which gives your function access to the event and chart objects.
function selectHandler (event, chart) {
// Useful for using chart methods such as chart.getSelection();
console.log(chart.getSelection());
}
The get function takes a chart label (the label you used when creating the chart) and returns the chart object.
lava.get('myFancyChart', function (chart) {
console.log(chart);
});
This will let you access the chart and its methods outside of a chart event.
lava.loadData('myFancyChart', {dataTableJsonObject}, function (chart) {
console.log(chart);
});
This will allow you to update charts via ajax, loading data using a datatable's toJson() method.
Here is an example of how to reload charts with ajax, using jQuery.
$temps = $lava->DataTable('America/Los_Angeles');
$temps->addDateColumn('Date')
->addNumberColumn('Max Temp')
->addNumberColumn('Mean Temp')
->addNumberColumn('Min Temp');
foreach(range(1, 30) as $day) {
$temps->addRow(array(
'2014-10-'.$day,
rand(50,90),
rand(50,90),
rand(50,90)
));
}
return $temps->toJson();
$.getJSON('http://my.cool.site.com/api/whatever/getDataTableJson', function (dataTableJson) {
lava.loadData('Chart1', dataTableJson, function (chart) {
console.log(chart);
});
});