Home » Php » How to generate a chart using Laravel?

How to generate a chart using Laravel?

Posted by: admin November 29, 2017 Leave a comment

Questions:

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?

Answers:

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 exampleAxis 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

Questions:
Answers:

Laravel doesn’t provides any charting library out of the box. You need to find a third party library written in PHP to generate charts from your Laravel app.

Some free library for charting are:

I strongly encourage you to search for php charting library here in StackOverflow to see the opinion from other experienced users.

When choosing a particular library add it to you composer.json file as any other dependency.