Advertisement
kansaramankit

Pie Chart Code

Jul 29th, 2015
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. <?php
  2. $con=mysqli_connect("localhost","root","") or die("Failed to connect with database!!!!");
  3. mysqli_select_db($con,"tcsdata");
  4. $sth = mysqli_query($con,"SELECT site_address,customer_name,SUM(item_discount) AS item_discount FROM `salesdata` WHERE site_address='BDD1' GROUP BY customer_name ");
  5.  
  6. $rows = array();
  7. //flag is not needed
  8. $flag = true;
  9. $table = array();
  10. $table['cols'] = array(
  11.  
  12. // Labels for your chart, these represent the column titles
  13. // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
  14. array('label' => 'customer_name', 'type' => 'string'),
  15. array('label' => 'item_discount', 'type' => 'number')
  16.  
  17. );
  18.  
  19. $rows = array();
  20. while($r = mysqli_fetch_assoc($sth)) {
  21. $temp = array();
  22. // the following line will be used to slice the Pie chart
  23. $temp[] = array('v' => (string) $r['customer_name']);
  24.  
  25. // Values of each slice
  26. $temp[] = array('v' => (float) $r['item_discount']);
  27. $rows[] = array('c' => $temp);
  28. }
  29.  
  30. $table['rows'] = $rows;
  31. $jsonTable = json_encode($table);
  32. //echo $jsonTable;
  33. ?>
  34.  
  35. <html>
  36. <head>
  37.  
  38. <!--Load the Ajax API-->
  39. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  40. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  41. <script type="text/javascript">
  42.  
  43. // Load the Visualization API and the piechart package.
  44. google.load('visualization', '1', {'packages':['corechart']});
  45.  
  46. // Set a callback to run when the Google Visualization API is loaded.
  47. google.setOnLoadCallback(drawChart);
  48.  
  49. function drawChart() {
  50.  
  51. // Create our data table out of JSON data loaded from server.
  52. var data = new google.visualization.DataTable(<?=$jsonTable?>);
  53. var options = {
  54. title: ' Report for CMPSL ( Baddi1 April 2015) ',
  55. is3D: 'true',
  56. width: 450,
  57. height: 450
  58. };
  59. // Instantiate and draw our chart, passing in some options.
  60. // Do not forget to check your div ID
  61. var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  62. chart.draw(data, options);
  63. }
  64. </script>
  65. </head>
  66. <body>
  67. <!--this is the div that will hold the pie chart-->
  68. <div id="chart_div" align="center"></div>
  69. </body>
  70. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement