0

I'm trying to create a sample bar chart or pie chart from a MySQL data that I have. I know how to use Google Charts and its basic functionality. The question is..How can I integrate my PHP/MySQL data to create a sample Bar or Pie Chart.

I have the simplest data to display: Count of Apples, Banana and Orange.

I can only display them using the basic coding from Google Charts( putting the values in the Google chart codes),but I need to query them from MySQL. Do I need json for this?

Thanks!

Tristan Jade Awat
  • 275
  • 2
  • 4
  • 16
  • the answer is not related to google but you can check out these charts on http://www.thetutlage.com/post=TUT103 ,it is a jquery canvas plugin and quite flexible – Aman Virk Feb 21 '12 at 08:39

1 Answers1

0

A simple example, get the data from your database and pass it into charts, like:


while($r = mysql_fetch_assoc($query)) {

     $google_JSON = "{cols: [";    
     $column = array_keys($r);
     foreach($column as $key=>$value){
         $google_JSON_cols[]="{id: '".$key."', label: '".$value."'}";
     }    
     $google_JSON .= implode(",",$google_JSON_cols)."],rows: [";       

   $google_JSON_rows[] = "{c:[{v: '".$r['id']."'}, {v: ".$r['count']."}]}";
}    
// you may need to change the above into a function that loops through rows, with $r['id'] etc, referring to the fields you want to inject..
//pass it into google charts data
echo $google_JSON.implode(",",$google_JSON_rows)."]}";

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • Using the code above (with proper modification) will show a DataTable recognizable by Google Charts? and use the Page URL where it is shown for its google.visualization.Query? Am I gettting it right? – Tristan Jade Awat Feb 21 '12 at 09:30
  • uh,., is still confused...this PHP code will show something like this format: {cols:[{id:'Col1',label:'',type:'number'}], rows:[{c:[{v:1.0,f:'1'}]}, {c:[{v:2.0,f:'2'}]}, {c:[{v:3.0,f:'3'}]}, {c:[{v:1.0,f:'1'}]} ] } Thenwhat will I do with this PHP file? how to I put it in my code ? – Tristan Jade Awat Feb 22 '12 at 01:59