How to add charts in Laravel using Chart JS ?

How to add charts in Laravel using Chart JS ?

In this post we will give you information about How to add charts in Laravel using Chart JS ?. Hear we will give you detail about How to add charts in Laravel using Chart JS ?And how to use it also give you demo for it if it is necessary.

Laravel 5’s Blade template engine is awesome. you can easyly use PHP variable, js and js library in laravel view. i will create chart using Chart.js in laravel application. Chartjs is a js library, this library through we can use bar chart, line chart, area chart, column chart etc, C]chart.js also provide sevral theme and graph that way you can use more chart from here : Chartjs Docs.

whenever you need to add charts in laravel server side. then you can easily use following example you have to fetch data from database and then set in Chart JS function, In this post i will give you simple example to create bar chart using chart js that way you can use in your laravel application.


So, first add chartjs route in your routes.php file.


Route::get('chartjs', 'HomeController@chartjs');

Ok, now add bellow method like this way in Homecontroller :

HomeController Method

public function chartjs()


$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('chartjs')




And Put bellow code in chartjs view file.


Also see:Laravel – GEO Chart Example using lavacharts



<script src=""></script>


var year = ['2013','2014','2015', '2016'];

var data_click = <?php echo $click; ?>;

var data_viewer = <?php echo $viewer; ?>;

var barChartData = {

labels: year,

datasets: [{

label: 'Click',

backgroundColor: "rgba(220,220,220,0.5)",

data: data_click

}, {

label: 'View',

backgroundColor: "rgba(151,187,205,0.5)",

data: data_viewer



window.onload = function() {

var ctx = document.getElementById("canvas").getContext("2d");

window.myBar = new Chart(ctx, {

type: 'bar',

data: barChartData,

options: {

elements: {

rectangle: {

borderWidth: 2,

borderColor: 'rgb(0, 255, 0)',

borderSkipped: 'bottom'



responsive: true,

title: {

display: true,

text: 'Yearly Website Visitor'






<div >

<div >

<div >

<div >

<div >Dashboard</div>

<div >

<canvas id="canvas" height="280" width="600"></canvas>







Hope this code and post will helped you for implement How to add charts in Laravel using Chart JS ?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  3  =  4

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US