Showing posts with label how to implement a highcharts in laravel 9 using highchart js. Show all posts
Showing posts with label how to implement a highcharts in laravel 9 using highchart js. Show all posts

Laravel Highcharts Example with Tutorial

Laravel Highcharts Example with Tutorial
Hi dev, Today,In this example, I am explain how to use highcharts in laravel 9. In this article i will show you highchart in laravel 9. We will learn how to add highchart in laravel 9. This tutorial demonstrates how to create charts in Laravel 9 with Highcharts. you will learn how to implement a highcharts in laravel 9 using highchart js.using highcharts you can create interactive charts easily for your web projects. so, now we will see basic line chart using highcharts in laravel 9. Highcharts is a modern SVG-based, multi-platform charting library. It makes it easy to add interactive charts to web and mobile projects. If you work with any web application or e-commerce application or any dating application etc, And need to show analytics on these application dashboards. So this laravel 9 highcharts example tutorial helps you, how to fetch month wise data and how to display month wise data in highcharts for analytics on laravel application. Step 1: Install Laravel 9 This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel laravel-highcharts
Step 2: Create Route first of all we will create simple route for creating simple line chart. so let's add simple routes as like 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 Here, we will create new controller as HighchartController. so let's add 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: here, we need to create blade file and in this blade file we use highchart js and use their code. resources/views/chart.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Laravel Highcharts Example with Tutorial - Itwebtuts</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
   
<body>
<div class="col-md-12 text-center my-4">
    <h2>Laravel Highcharts Example with Tutorial - Itwebtuts</h2>
</div>
<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: itsolutionstuff.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>
Step 5: Create Dummy Records: Here, we need to add some dummy records on users table as monthly wise. you can create dummy records using laravel tinker command as bellow:
php artisan tinker
User::factory()->count(30)->create()
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/chart
I hope it can help you...