Laravel PHP Google 3D pie chart add rows dynamically using visualization table Example

Laravel PHP Google 3D pie chart add rows dynamically using visualization table Example

Google 3D pie charts add rows dynamically using visualization table in PHP Laravel Framework

In this tutorial, i will tell you following thing that i have used in google pie charts :

  • Use of Google Pie Chart
  • Pass Eloquent data from Controller to view and how can i get php value in script.
  • How to add dynamically rows with two columns to a Google Pie Charts.
  • About Google Visualization Data Table
  • Use of event addListener in Google Pie Chart

Charts are mostly used to generate report to see the summary of details.

Here i have a user table with additional column source where user is registered from different source as facebook and google.

You can see some steps from here to Install Laravel, Create Table and Model

I display the total registered user of each source in google piechart.

I assume that you have install laravel in your system if don't have then install laravel first

Route and Controller

Here i define route google-piechart with get method that call ChartController with pieChart method.

routes.php

  1. Route::get('google-piechart',array('as'=>'chart.piechart','uses'=>'ChartController@pieChart'));
ChartController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\User;
  6. class ChartController extends Controller {
  7. public function pieChart(){
  8.         $social_users=User::selectRaw('count(source) as count,source')->groupBy('source')->get();
  9.     $user=array();
  10.     foreach ($social_users as $result) {
  11.         $user[$result->source]=(int)$result->count;
  12.     }
  13. return view('piechart',compact('user'));
  14. }
  15.     
  16. }

You will notice that here i use selectRaw method that are used to write raw query while working with Laravel Eloquent.

View File

Now create piechart.blade.php

resources/views/piechart.blade.php

  1. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  2. <script type="text/javascript">
  3. google.charts.load("current", {packages:["corechart"]});
  4. google.charts.setOnLoadCallback(drawChart);
  5. function drawChart() {
  6. var record={!! json_encode($user) !!};
  7. console.log(record);
  8. // Create our data table.
  9. var data = new google.visualization.DataTable();
  10. data.addColumn('string', 'Source');
  11. data.addColumn('number', 'Total_Signup');
  12. for(var k in record){
  13. var v = record[k];
  14. data.addRow([k,v]);
  15. console.log(v);
  16. }
  17. var options = {
  18. title: 'My Daily Activities',
  19. is3D: true,
  20. };
  21. var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
  22. chart.draw(data, options);
  23. }
  24. </script>
  25. <div id="piechart_3d" style="width: 900px; height: 500px;"></div>

Now you can use this demo code in your application for Google pie chart.

You can customize option value as per need and add dynamic title and set click event to see full report too.

To use event addListener follow the syntax :

  1. google.visualization.events.addListener(chart, 'select', function() {
  2.     var row = chart.getSelection()[0].row;
  3.     var selected_data=data.getValue(row, 0);
  4.     var url = "demo.expertphp/google-pie-chart?data="+selected_data ;
  5.      window.open(url, '_blank');
  6. });

Click here to see the demo how it is working

Phone: (+91) 8800417876
Noida, 201301