1

I would like to create a simple Bar chart using Google Charts. Data comes from MySQL. Using PHP What I have so far: 1. Index.php (No problem with this one) and GetData.php (The problem)

<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() {
      var jsonData = $.ajax({
          url: "Getdata.php",
          dataType:"json",
          async: false
          }).responseText; 
      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);
   var chart = new google.visualization.barChart(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});
    }
    </script>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>

....GetData.php

 <?php
 include('../conn.php');
 ?>
<?php
$result = mysql_query
("select (case when n = 1 then 'Quality' else 'Speed' end) as         Assessment,
   (case when n = 1 then avg_quality else avg_speed end) as Value
from (select AVG(r.quality) as avg_quality, AVG(r.speed) as avg_speed
  from reading_assestment r join
       people p
       on r.person_id =p.person_id
  where person_id = 3
 ) t cross join
 (select 1 as n union all select 2) n");

$rows = array();
while($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
}
print json_encode($rows);

Select query output is correct:

    Assestment Value
    Quality      77
    Speed        65

Following this I am no sure how to create correct json output to draw Barchart

kaulainais
  • 115
  • 1
  • 2
  • 12
  • Its shows error message : Table has no columns – kaulainais Jan 09 '14 at 17:35
  • Sorted. Used this brilliant example http://stackoverflow.com/questions/12994282/php-mysql-google-chart-json-complete-example?rq=1 – kaulainais Jan 09 '14 at 18:20
  • Possible duplicate of [PHP MySQL Google Chart JSON - Complete Example](https://stackoverflow.com/questions/12994282/php-mysql-google-chart-json-complete-example) – Dharman Aug 11 '19 at 20:03

0 Answers0