How to Create Highcharts in Laravel 9 ?

Today now in this tutorial i will focused on the laravel 9 highcharts. Here you’ll learn about the laravel 9 highcharts ajax example. So in this post i will give you a very simple example of how we can create chart in laravel 9 application. Now in this post i will give you a very simple example of how to use highcharts in laravel 9 application.

We can simply use the Line Charts, Bar Charts, Pie Charts, Area Charts, etc by using the highcharts js.

We know Highcharts is a js library, on this library we can use at bar chart, line chart, area chart, column chart, etc. Highcharts is a popular open-source chart library. We know highcharts also provide the several theme and graph on that way we can use more chart from the here : HighCharts Site.

Now in this example, i will create a line chart with all months of current years. so let’s start and follow the below step and add you can add chart in your own laravel 9 application.

Step 1: Install Laravel 9

This is first step but this step is optional; however, if you do not have then need to created the laravel app. then you may go ahead and then execute the below command:

composer create-project laravel/laravel example-app
Step 2: Create Route

Then at first of all i will create the simple route for creating the simple line chart. So let’s need to add simple routes as like as bellow:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\HighchartController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('chart', [HighchartController::class, 'index']);

Step 3: Create Controller

Now here, i will create a new controller as GoogleChartController. So let’s add the bellow code on that controller file.

app/Http/Controllers/HighchartController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use DB;
  
class HighchartController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $users = User::select(DB::raw("COUNT(*) as count"))
                    ->whereYear('created_at', date('Y'))
                    ->groupBy(DB::raw("Month(created_at)"))
                    ->pluck('count');
            
        return view('chart', compact('users'));
    }
}

Step 4: Create Blade File:

Now in the last step, i have to create the blade file and in this blade file i will use highcharts js and also use their code.

resources/views/chart.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Highcharts Example - CodingsPoint.com</title>
</head>
   
<body>
<h1>Laravel 9 Highcharts Example - CodingsPoint.com</h1>
<div id="container"></div>
</body>
  
<script src="https://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript">
    var users =  {{ Js::from($users) }};
   
    Highcharts.chart('container', {
        title: {
            text: 'New User Growth, 2022'
        },
        subtitle: {
            text: 'Source: codingspoint.com.com'
        },
         xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
            title: {
                text: 'Number of New Users'
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle'
        },
        plotOptions: {
            series: {
                allowPointSelect: true
            }
        },
        series: [{
            name: 'New Users',
            data: users
        }],
        responsive: {
            rules: [{
                condition: {
                    maxWidth: 500
                },
                chartOptions: {
                    legend: {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom'
                    }
                }
            }]
        }
});
</script>
</html>

Run Laravel App:

That’s good all the required steps have been complete, now we have to type the given below command and hit the enter to run the Laravel application:

php artisan serve

Read Also: How To Implement Laravel 9 form validation ?

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

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →