Google Chart Tools with PHP & MySQl

前端 未结 2 979
失恋的感觉
失恋的感觉 2020-12-15 00:21

I\'m trying to create a chart using the Google Visualization API, with PHP & MySQL in the background.

What I\'m doing is:

  • getting the data from

相关标签:
2条回答
  • 2020-12-15 01:06

    Per the docs, have you tried establishing the column references and data seperately?

    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Task');
    data.addColumn('number', 'Hours per Day');
    data.addRows([
      ['Work', 11],
      ['Eat', 2],
      ['Commute', 2],
      ['Watch TV', 2],
      ['Sleep', {v:7, f:'7.000'}]
    ]);
    

    To format into the correct JSON for the object, you can set it up as follows:

    while($r = mysql_fetch_assoc($sth)) {
       if(!isset($google_JSON)){    
         $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['quarters']."}, {v: ".$r['salary']."}]}";
    }    
    // 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..
    echo $google_JSON.implode(",",$google_JSON_rows)."]}";
    
    0 讨论(0)
  • 2020-12-15 01:23
    Complete Working Example: PHP/MYSQL/Google Chart/JSON
    

    PHP MySQL Google Chart JSON - Complete Example

    0 讨论(0)
提交回复
热议问题