Not able to display ComboChart via MySql

48 views
Skip to first unread message

ankitkans...@gmail.com

unread,
Aug 31, 2015, 1:46:20 AM8/31/15
to Google Visualization API
Hello,
I'm new to Google Charts & want to make a combo chart. 
I have fired a query & want the output to be shown in ComboChart Format. 
Here is the code i have tried

<?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" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <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>

I'm getting error of  Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean on line 21


Please Help ! 
Thanks in advance 
Reply all
Reply to author
Forward
0 new messages