<?php
$con=mysqli_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysqli_select_db($con,"tdata");
$sth = mysqli_query($con,"SET @cust_rank := NULL;SET @current_cust:= NULL;
SELECT site,item_code,customer_name,item_value,cust_rank FROM (SELECT b.site,b.item_code,b.customer_name,b.item_value, @cust_rank :=
IF(@current_cust=a.customer_name ,@cust_rank+1,1) AS cust_rank,@current_cust:=a.customer_name FROM(SELECT site,customer_name,SUM
(item_amount) Total_value FROM `cmpslsalesdata` WHERE site='BDD1' GROUP BY site,customer_name ORDER BY Total_value DESC limit 5) a,
(SELECT site,item_code,customer_name,SUM(item_amount) AS item_value FROM `cmpslsalesdata` WHERE site='BDD1' GROUP BY
site,item_code,customer_name ORDER BY customer_name,item_code,item_value DESC) b WHERE a.customer_name=b.customer_name) c WHERE
c.cust_rank <=10 ORDER BY customer_name,item_code");
$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(
// Labels for your chart, these represent the column titles
// 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
array('label' => 'Customer_name', 'type' => 'string'),
array('label' => 'Total_Amount', 'type' => 'number')
);
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp[] = array('v' => (string) $r['Customer_name']);
// Values of each slice
$temp[] = array('v' => (float) $r['Total_Amount']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;
?>
<html>
<head>
<!--Load the Ajax API-->
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
title: ' Report (BDD1) ',
is3D: 'true',
hAxis: {
title: 'Top Customers'
},
vAxis: {
title: 'Sales in Rs.'
},
width: 600,
height: 500
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.ComboChart(document.getElementById('BDD1SALES'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--this is the div that will hold the pie chart-->
<div id="BDD1SALES" align="center"></div>
</body>
</html>