In this tutorial you will learn about the Laravel 7/6 Highcharts Example Tutorial and its application with practical example.
In this Laravel 7/6 Highcharts Example Tutorial I will show you how to implement highchart in laravel application. In this tutorial you will learn to implement highchart in laravel. In this example I will demonstrate the use of highchart in laravel application. In this example we will fetch month wise data and display month wise data in highcharts for analytics on laravel application.
Laravel 7/6 Highcharts Example Tutorial
1. Create web routes
In the first step we will define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 |
Route::get('highchart', 'ChartController@index'); |
2. Create Controller
Now, lets create a controller named ChartController using command given below –
1 |
php artisan make:controller ChartController |
Once the above command executed, it will create a controller file ChartController.php in app/Http/Controllers/ directory. Open the ChartController.php file and put the following code in it.
app/Http/Controllers/ChartController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use Redirect,Response; Use DB; class ChartController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $record['chart'] = User::select(\DB::raw("COUNT(*) as count")) ->whereYear('created_at', date('Y')) ->groupBy(\DB::raw("Month(created_at)")) ->pluck('count'); return view('highchart', $record); } } |
3. Create Blade File
Now, we will create a blade view file. Go to the resources/views/highchart.blade.php and put the below javascript and HTML code for displaying the highchart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<!DOCTYPE html> <html> <head> <title>Laravel 7/6 Highcharts Example - Tutsmake.com</title> <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/export-data.js"></script> <script src="https://code.highcharts.com/modules/accessibility.js"></script> </head> <body> <div id="container"></div> </body> <figure class="highcharts-figure"> <div id="container"></div> </figure> <script type="text/javascript"> let chart = JSON.parse(`<?php echo $chart ?>`); Highcharts.chart('container', { title: { text: 'New User Growth, 2019' }, subtitle: { text: 'Source: tutsmake.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: chart }], responsive: { rules: [{ condition: { maxWidth: 500 }, chartOptions: { legend: { layout: 'horizontal', align: 'center', verticalAlign: 'bottom' } } }] } }); </script> </html> |
Remember to include highchart js libraries on your blade file:
1 2 3 4 |
<script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <script src="https://code.highcharts.com/modules/export-data.js"></script> <script src="https://code.highcharts.com/modules/accessibility.js"></script> |
Use below javascript as per requirement :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<script type="text/javascript"> let chart = JSON.parse(`<?php echo $chart ?>`); Highcharts.chart('container', { title: { text: 'New User Growth, 2019' }, subtitle: { text: 'Source: tutsmake.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: chart }], responsive: { rules: [{ condition: { maxWidth: 500 }, chartOptions: { legend: { layout: 'horizontal', align: 'center', verticalAlign: 'bottom' } } }] } }); </script> |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/highchart |