Home Chartjs Laravel – Chartjs with Dynamic Data Working Example

Laravel – Chartjs with Dynamic Data Working Example

by therichpost
Published: Updated: 7 comments
laravel_chartjs

Hello to all, welcome to therichpost.com. In this post, I will do, Laravel – Chartjs with Dynamic Data Working Example.

In this post, I will show laravel controller data to laravel blade template file with the help of laravel web routes.

 

Laravel - Chartjs with Dynamic Data Working Example

 

Here is the working code steps, you need to follow:

 

1. Very first, here is the code for laravel controller file HomeController.php with custom data:

public function Chartjs(){
  $month = array('Jan', 'Feb', 'Mar', 'Apr', 'May');
  $data  = array(1, 2, 3, 4, 5);
  return view('chartjs',['Months' => $month, 'Data' => $data]);
}

2. Here is the code for laravel routes/web.php file:

Route::get("/chartjs", "HomeController@Chartjs");

3. Finally, here is the code for laravel blade template file chartjs.blade.php:

I made laravel controller array data into json format for chartjs data:

<!doctype html>
<html>

  <head>
  <title>Bar Chart</title>
  <script src="http://www.chartjs.org/dist/2.7.3/Chart.bundle.js"></script>
  <script src="http://www.chartjs.org/samples/latest/utils.js"></script>
  <style>
  canvas {
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
  }
  </style>
  </head>

  <body>
    <div id="container" style="width: 75%;">
    <canvas id="canvas"></canvas>
    </div>
    <script>

    var chartdata = {
    type: 'bar',
    data: {
    labels: <?php echo json_encode($Months); ?>,
    // labels: month,
    datasets: [
    {
    label: 'this year',
    backgroundColor: '#26B99A',
    borderWidth: 1,
    data: <?php echo json_encode($Data); ?>
    }
    ]
    },
    options: {
    scales: {
    yAxes: [{
    ticks: {
    beginAtZero:true
    }
    }]
    }
    }
    }


    var ctx = document.getElementById('canvas').getContext('2d');
    new Chart(ctx, chartdata);
    </script>
  </body>

</html>

In the end run the laravel project:

http://localhost/laravelproject/public/chartjs

This is code. If you have any query related to this post, then do comment below or ask question. This will also work in laravel 8. Laravel 8 Chartjs with Dynamic Data Working Example

Thank you,

devil,

TheRichPost

 

Laravel Chartjs

You may also like

7 comments

Zaid May 4, 2020 - 12:16 pm

Simple, easy and great

Reply
Ajay Malhotra May 4, 2020 - 12:19 pm

Thank you

Reply
Austin Muvavi August 3, 2020 - 11:10 am

I’m getting this error $Months is undefined
Make the variable optional in the blade template. Replace {{ $Months }} with {{ $Months ?? ” }}

Reply
Ajay Malhotra August 4, 2020 - 4:02 pm

Did you follow my complete tutorial?

Reply
Ajay Malhotra August 19, 2020 - 11:04 am

It is working fine, please check the video link:
https://youtu.be/05WLjPjgox8

Reply
Neha kumari August 7, 2020 - 7:43 am

same error is facing

Reply
Ajay Malhotra August 19, 2020 - 11:04 am

It is working please check this video:
https://youtu.be/05WLjPjgox8

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.