How to generate a chart using Laravel?

syamimi picture syamimi · Aug 14, 2013 · Viewed 14.2k times · Source

I'm working with Laravel 3. I want to make a reporting page on the website. I have some view table as per below:

+---------+-----------------+-------+
| user_id | username        | total |
+---------+-----------------+-------+
|       1 | user            |    12 |
|       2 | admin           |     3 |
|       3 | user2           |     1 |
|       4 | user3           |     1 |
+---------+-----------------+-------+

I want to show the data in chart view. What is the best way to make it?

Answer

devXen picture devXen · Oct 31, 2013

I second phpChart. Used it in the past for an online report task. Very easy to create charts quickly.

Here's the solution to your scenario using phpChart based on their online example -- Axis Labels Rotated Text 2:

<?php 
$line = array(array('user', 12), array('admin', 3), array('user2', 1), array('user3', 1));

$pc = new C_PhpChartX(array($line),'user_chart');
$pc->add_plugins(array('canvasTextRenderer'));
//set series
$pc->add_series(array('renderer'=>'plugin::BarRenderer'));
//set axes
$pc->set_axes(array(
    'xaxis'  => array(
        'renderer'=>'plugin::CategoryAxisRenderer',
        'tickRenderer'=>'plugin::CanvasAxisTickRenderer'),
    'yaxis'  => array(
        'autoscale'=>true,
        'tickRenderer'=>'plugin::CanvasAxisTickRenderer')
));

$pc->draw(800,500);
?>

Result: enter image description here

Change the 6th line to PieRenderer you will get a pie chart.

<?php
$line = array(array('user', 12), array('admin', 3), array('user2', 1), array('user3', 1));

$pc = new C_PhpChartX(array($line),'chart_1');
$pc->add_plugins(array('canvasTextRenderer'));
//set series
$pc->add_series(array('renderer'=>'plugin::PieRenderer'));
//set axes
$pc->set_series_default(array(
    'renderer'=>'plugin::PieRenderer',
    'rendererOptions'=>array('showDataLabels'=>true)));
$pc->set_legend(array('show'=>true,
    'rendererOptions'=> array('numberRows'=> 1),
    'location'=> 's'));

$pc->draw(800,500);
?>

enter image description here

Here's a great intro on Codeproject I found: http://www.codeproject.com/Articles/604542/Creating-Interactive-HTML5-Graphs-in-PHP