Visualization of data using highcharts in laravel

Visualization of data using highcharts in laravel

Here we ‘ll look at the visualization of data using highcharts. Generally, data visualization presents the data in a graphical or pictorial format so it helps the readers to easily understand the data.

Highcharts.com provides with a lot of different types of charts – line, area, bar, column, pie and many more. Here we ‘ll implement line chart with required data fetched from the database.

Working Demo      Project on Github

In this example we show yearly sales of 10 different companies, the database table can be found here.

The model file will look like,

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CompanyDetails extends Model {
    protected $table = 'companydetails';
    public $timestamps = false;
}
Double the learning and saving. Buy 1 Get 1 on premium e-degrees

First, we get all the data from the database and then convert the highcharts javascript code to PHP and pass the data to the view so the visualization of data can be seen.

We get data from each company separately,

        $y2006Details = CompanyDetails::where ( 'year', '2006' )->get ();
        $y2007Details = CompanyDetails::where ( 'year', '2007' )->get ();
        $y2008Details = CompanyDetails::where ( 'year', '2008' )->get ();
        $y2009Details = CompanyDetails::where ( 'year', '2009' )->get ();
        $y2010Details = CompanyDetails::where ( 'year', '2010' )->get ();
        $y2011Details = CompanyDetails::where ( 'year', '2011' )->get ();
        $y2012Details = CompanyDetails::where ( 'year', '2012' )->get ();
        $y2013Details = CompanyDetails::where ( 'year', '2013' )->get ();
        $y2014Details = CompanyDetails::where ( 'year', '2014' )->get ();
        $y2015Details = CompanyDetails::where ( 'year', '2015' )->get ();

We get all the company names,

$companyNames = CompanyDetails::select ( 'company' )->groupBy ( 'company' )->get ();

Now we push all the company names to an array so as to pass this array to highcharts

foreach ( $companyNames as $company )
    array_push ( $comapanyNamesArray, $company->company );

Now we push each of  the company sales details into array,

        foreach ( $y2006Details as $det )
            array_push ( $y2006, ( int ) $det->sales );
        foreach ( $y2007Details as $det )
            array_push ( $y2007, ( int ) $det->sales );
        foreach ( $y2008Details as $det )
            array_push ( $y2008, ( int ) $det->sales );
        foreach ( $y2009Details as $det )
            array_push ( $y2009, ( int ) $det->sales );
        foreach ( $y2010Details as $det )
            array_push ( $y2010, ( int ) $det->sales );
        foreach ( $y2011Details as $det )
            array_push ( $y2011, ( int ) $det->sales );
        foreach ( $y2012Details as $det )
            array_push ( $y2012, ( int ) $det->sales );
        foreach ( $y2013Details as $det )
            array_push ( $y2013, ( int ) $det->sales );
        foreach ( $y2014Details as $det )
            array_push ( $y2014, ( int ) $det->sales );
        foreach ( $y2015Details as $det )
            array_push ( $y2015, ( int ) $det->sales );

Now we push each  of this array into an array so as to pass this data to highcharts,

array_push ( $dataArray, $y2006, $y2007, $y2008,
        $y2009, $y2010, $y2011, $y2012, $y2013, 
        $y2014, $y2015 );

Working Demo      Project on Github

Passing name and data for the series array in highcharts.

for($i = 0; $i < count ( $dataArray ); $i ++)
    $chartArray ["series"] [] = array (
            "name" => $comapanyNamesArray [$i],
            "data" => $dataArray [$i] 
    );

Now we pass some data like chart title, X-axis name, y-axis name to highcharts.

$chartArray ["chart"] = array (
        "type" => "line" 
);
$chartArray ["title"] = array (
        "text" => "Yearly sales" 
);
$chartArray ["credits"] = array (
        "enabled" => false 
);
$chartArray ["xAxis"] = array (
        "categories" => array () 
);
$chartArray ["tooltip"] = array (
        "valueSuffix" => "$" 
);
$chartArray ["xAxis"] = array (
        "categories" => $categoryArray 
);
$chartArray ["yAxis"] = array (
        "title" => array (
        "text" => "Sales ( $ )" 
        ) 
);

We pass this chartArray to welcome view.

return view ( 'welcome' )->withChartarray ( $chartArray );

In the view, we simply pass this array to highcharts.

<script type="text/javascript">
$(function() {
  $('#container').highcharts( <?php echo json_encode($chartarray) ?>)
});
</script>

Working Demo      Project on Github

The output of our visualization

Data visualization using highcharts - justlaravel.com
Data visualization using highcharts – justlaravel.com



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.