Re: MySQL php JSON data format for using w/ Google Visualization API

367 views
Skip to first unread message

asgallant

unread,
Dec 19, 2012, 8:04:12 PM12/19/12
to google-visua...@googlegroups.com
You can't pass an arbitrary set of data to the DataTable constructor; you have to follow a set of rules.  There is a thread here that is filled with posts about setting up a MySQL data source in PHP.

On Wednesday, December 19, 2012 7:23:05 PM UTC-5, Kurt Braun wrote:
I am having trouble getting my MySQL data to work with the google visualization API.

My php outputs the following json data using echo json_encode($data);
{"type":["A","B","C","D","E"],"totals":[276.36,23.56,42.06,0.06,6254.49]}

I generate it with the following php:

<?php
/* I'm leaving out the mysql connection and select statements, but those parts work fine as you can see from the echo below
$data = array(
"Type" => array
(
"A",
"B",
"C",
"D",
"E",
), 
"Totals" => array
(
$ts0,
$ts1,
$ts2,
$ts3,
$ts4,
),
);
/*
Echo $data; 
array(2) { ["Type"]=> array(5) { [0]=> string(2) "A" [1]=> string(3) "B" [2]=> string(3) "C" [3]=> string(3) "D" [4]=> string(3) "E" } 
 ["Totals"]=> array(5) { [0]=> float(3450.25) [1]=> float(294.09) [2]=> float(525.16) [3]=> float(0.77) [4]=> float(78084.22) } }
*/
/*
echo json_encode($data);
{"type":["A","B","C","D","E"],"totals":[276.36,23.56,42.06,0.06,6254.49]}
*/
?>

Now, I am just trying to create a table with this data.  I copy/paste the example from google code play on using json data, but no luck for me.

  function drawVisualization() {
      // Create and populate the data table.
      var JSONObject = {<?php echo json_encode($data); ?>};
      var data = new google.visualization.DataTable(JSONObject, 0.5);
    
      // Create and draw the visualization.
      visualization = new google.visualization.Table(document.getElementById('table'));
      visualization.draw(data, {'allowHtml': true});

What am I missing? Do I need to setup the JSON object columns first?

Thanks!
Reply all
Reply to author
Forward
0 new messages