Lavacharts is a wrapper for Google's powerful Javascript Chart API
Get up and running within minutes with Composer.
If you don't know about Composer, I would highly recommend visiting their site and learning how to use it in your projects.
First, copy this line into your project's composer.json file, in the "require": {}
section
Next, run composer to download and install
Finally, require('vendor/autoload.php');
in your project and you are good to go!
First step, register the service provider by adding the class namespace to the providers array in app/config/app.php
If you are using PHP 5.5+, then you can use the new ::class
syntax to reference the Service Provider.
The functionality is based upon the single instance of the library, so it is strongly recommended to add the Lava
alias for ease of use throughout your application.
Same as the service provider, if you are using PHP 5.5+, you can use the ::class
syntax.
Remember, many examples show $lava->
but while using Laravel, enable the alias and use \Lava::
instead.
First, grab the most recent copy of the source from Github using this link:
You will also need to get a copy of the Carbon Library
since it the DataTables depend on Carbon.
Then, include Carbon and Lavacharts and create a new instance of the library.
Getting up and running with Lavacharts is a breeze and if you follow along, you will learn how Lavacharts works. If you want some real working examples just jump down and take a look. Following these steps will show you how to create a DataTable, add columns and rows, add data, and finally render the chart into the page.
Creating a new DataTable for your chart.
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.
addDateColumn($description)
for datesaddTimeOfDayColumn($description)
for times of the dayaddNumberColumn($description)
for numbersaddStringColumn($description)
for stringsaddBooleanColumn($description)
for boolean valuesaddRoleColumn($description)
for rolesaddRow()
method signature, follows the order in which the columns were added.
Creating and customizing your chart.
Select which type of chart you will be creating. In this example, we will use LineChart, but Lavacharts currently supports:
Rendering the chart
All you need to do in your view, is use the render method and the library will output all of the neccessary javascript into the page.
Syntax: render(chartType, chartLabel, elementId)
Upgrading from 2.5.x -> 3.0.x
In an effort to keep chart creation terse, the chart constructor has been extended. Charts now set the DataTable with the second parameter, and options if needed with the third.
The constructor now takes two more arguments, the DataTable and optional configuration options.
Following the effort to reduce typing for the developer, ConfigObjects are no longer required to be instantiated manually. Just pass in the same config array you would have passed to the constructor, as a value to the key. Internally, a ConfigObject will be created and options will be checked/verified/assigned and applied to the chart, same as before.
These methods are available from the main Lavacharts class.
Creates new instance of a DataTable.
Creates new instance of a dashboard.
Output the necessary javascript tags for getting the chart onto the page.
Checks if the Chart exists in the Volcano (The library's internal storage class)
This method with output the script tag for the google jsapi, along with lava.js; A custom module for lavacharts.
Used with Angular so the script tag is not placed inside an ng-view and can be manually placed in the header / footer.
What's a chart without data?
For convenience, the common column types have their own dedicated methods. They are alias methods, which will internally call the addColumn()
method, with some values automatically filled; Type and Label.
If you want to explicitly add columns, then there are methods for you to do so. You can using strings, arrays, or multi-dimensional arrays.
Type
Defines the type of data that will be in the column.Description
The description of the column. (optional)Label
A label used to access specific columns. (optional)Format
The desired format for the column. (optional)Other column related methods
Add rows either by chaining the single row command, or batch adding with the addRows() method.
Other row related methods
Customize the appearance of your data
Columns can have formats applied so the data/labels will be shown according to the format definition.
Currently supported configuration options:
arrow is missingCurrently supported configuration options:
bar is missingCurrently supported configuration options:
Explanations of each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Explanations of each of the configuration options can be found in Google's Documentation
Set the timezone to be used for Date, DateTime, and TimeOfDay 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 loading chart data via AJAX. (See Example)
Select a chart from the dropdown or sidebar menu to see the different configuration methods / options available.
Create a new chart by calling from the top level Lava object, $lava->
if not using Laravel, or Lava::
when using the alias via service provider.
All of the methods below are also available to all of the charts, as all charts extend the parent Chart
All of the different charts extend from the parent chart, to provide common options and methods for all charts.
These methods are available to all charts:
setOptions()
getDataTableJson()
These options are available to all charts:
Jump to the available options for the different configuration options.
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
This is a special version of a PieChart, so it shares all the same options.
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Currently supported configuration options:
Learn more about:
Explanations for each of the configuration options can be found in Google's Documentation
Create dynamic data displays.
Dashboards are charts that have controls and filters attached to them to enable altering the view of the data through the controls.
For example, You want to view a sub-set of your data, you can attach a number filter, and change the display of the chart through number sliders.
To break it down into parts, a dashboard is created by:
ControlWrappers()
ChartWrappers()
Same as charts, dashboards need a label. Pass this as the only parameter to the constructor.
The one method you will need from the Dashboard is bind()
to attach a ControlWrapper
to a ChartWrapper
and add it to the dashboard.
OneToOne
, OneToMany
, ManyToOne
, and ManyToMany
. This is why the bind()
method accepts arrays as well as objects.
Filters are used alter the appearance of a Chart
by filtering the DataTable. Used within a ControlWrapper()
, filters work with the controls to alter the chart's display.
All filters need a previously created Chart
with a DataTable.
The first parameter is either the DataTable's column index or label, and the second parameter is an optional array of customization options.
For example:
After creating a filter, you wrap it with a control that will allow the view of the chart to be changed.
Pass in one of the filters and the HTML element id where the dashboard's control is to be rendered.
Next step is to add a chart to the dashboard. (This should have already been created with your options and data)
Pass in your created chart and the HTML element id where the dashboard's chart is to be rendered.
Lastly, bind the control to the chart into the dashboard.
It helps to remember the syntax that you bind a control to a chart, not a chart to a control.
The render method from the lava object will render dashboards too.
Instead of a chart type, put Dashboard
for the first parameter, then the dashboard label, and an element id.
Here is a working example of the a simple dashboard.
Here are a few examples of the types of charts you can create.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $areachart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $barchart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $calendarchart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $columnchart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $combochart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $donutchart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $gaugechart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $geochart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $piechart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $linechart or $lava to your view to render.
For Laravel, omit "$lava = new Lavacharts" and replace "$lava->" with "Lava::"
Otherwise, pass $Scatterchart or $lava to your view to render.
Extra functionality for your charts and dashboards.
Enable interaction or add custom functions to charts with javascript events.
Adding an event your chart is as simple as defining a javascript callback / function in your page, then attaching the event to the chart.
To use events with your charts, follow these two easy steps:
Acceptable events include:
This is a module that will be added to the page, which is used by the library. It also provides some extra functionality to interact with your charts via javascript.
lava.getChart(chartLabel, callback)
Pass in a chart label and a callback, to get access to the google chart, and the lavachart.
lava.getDashboard(dashboardLabel, callback)
Pass in a dashboard label and a callback, to get access to the google dashboard object, and the lavachart.
lava.loadData(chartLabel, dataTableJson, callback)
This method will allow you to update charts via ajax, loading data using a datatable's toJson() method.
Here is an example of how to load charts with ajax, using jQuery.
Here are some solutions to real world problems.
If you don't want to apply a format to an entire column, you can manually create a DataCell object with the value and format string.