Home Javascript How to add charts in Laravel using Highcharts ?

How to add charts in Laravel using Highcharts ?

by Shahriar Sagor

Today now in this post i will show you how to add chart in laravel application by using Highcharts. Laravel Blade template engine is a very awesome. we can easyly use PHP variable, js and also js library in laravel view.Now here i will create a chart by using Highcharts in my laravel application. Highcharts is a js library, this library through we can use the bar chart, line chart, area chart, column chart etc, Highcharts is also provide several theme and graph that way we can use more chart from here : HighCharts Site.

Now whenever we need to add charts in laravel server side. then we can easily use by following example we need to fetch data from database and then set it in Highcharts function, so let’s see how to use Highcharts in laravel application with proper example.


charts in Laravel using Highcharts

So, at first need to add bellow method like this way in your controller :

Controller Method

public function highchart()
    $viewer = View::select(DB::raw("SUM(numberofview) as count"))
    $viewer = array_column($viewer, 'count');
    $click = Click::select(DB::raw("SUM(numberofclick) as count"))
    $click = array_column($click, 'count');
    return view('highchart')

And now need to Put bellow code in highchart view file.


<script src="https://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
    $(function () { 
        var data_click = <?php echo $click; ?>;
        var data_viewer = <?php echo $viewer; ?>;
        chart: {
            type: 'column'
        title: {
            text: 'Yearly Website Ratio'
        xAxis: {
            categories: ['2013','2014','2015', '2016']
        yAxis: {
            title: {
                text: 'Rate'
        series: [{
            name: 'Click',
            data: data_click
        }, {
            name: 'View',
            data: data_viewer
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>
                <div class="panel-body">
                    <div id="container"></div>

Read Also : Sql function remove numeric characters from column example

Thanks for read. I hope it help you. For more you can follow us on facebook.


You may also like