Hi,

I'm trying out Google Charts for the first time. Basically, I want it to 
display information from a database. The database has the following fields:

Name | Age | Nationality

I wrote a PHP / SQL query that groups the Nationality and displays the 
amount. For example:

India - 10
Australia- 15
US - 5

I want the above info to be displayed in a pie chart. When I run my code, I 
get a blank screen. Please check it and let me know how I can fix it. 


 

<?php
  error_reporting(E_ALL ^ E_DEPRECATED);
  $con = mysql_connect("localhost","root","");

  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("test", $con);
?>


<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" 
src="https://www.google.com/jsapi";></script>
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      
      <?php
        $query = "SELECT Nationality, COUNT(Nationality) AS Total FROM 
event1 GROUP BY Nationality";
        $result= mysql_query($query);

        $pieData = array();
        while($rows = mysql_fetch_array($result)){
          $pieData[] = array($row['Nationality'], $row['Total']);
        }
      ?>


      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Nationality');
        data.addColumn('number', 'Total');
        data.addRows(<?php echo json_encode($pieData, JSON_NUMERIC_CHECK); 
?>);

        // Set chart options
        var options = {'title':'Nationalities',
                       'width':800,
                       'height':700};

        // Instantiate and draw our chart, passing in some options.
        var chart = new 
google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>

  <body>

    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-visualization-api+unsubscr...@googlegroups.com.
To post to this group, send email to google-visualization-api@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.

Reply via email to