The way I did it was roughly as follows:
1. Query your mysql data as you normally would
2. Determine whether your result set fields need to be strings or numbers for
visualization api purposes
3. Dynamically generate the javascript code
a. Define the fields as string or number based on step 2
a. Loop through the mysql result set and for each row generate an addRow
statement for your data object
Here's a code snippet from what I did to build the function:
<?php
echo "function drawViz() {\n";
echo "var data = new google.visualization.DataTable();\n";
$resultset = mysql_query( /* your SQL goes here */ ) or
die(mysql_error());
$cnt = mysql_num_fields($resultset);
for ($colnum=0; $colnum < $cnt; $colnum++)
{
$sqlFieldType = mysql_field_type($resultset,$colnum);
$fieldvar = getChartFieldType($sqlFieldType);
echo "data.addColumn('" .$fieldvar . "', '" .
mysql_field_name($resultset,$colnum) . "');\n";
}
while ($rowval = mysql_fetch_array($resultset)) {
$rowContents = "[";
for ($colnum=0; $colnum < $cnt; $colnum++)
{
if (mysql_field_type($resultset,$colnum) == "string")
$rowContents = $rowContents . "'" . $rowval[$colnum] . "'";
else $rowContents = $rowContents . $rowval[$colnum];
if ($colnum == $cnt - 1) $rowContents = $rowContents . "]";
else $rowContents = $rowContents . ",";
}
echo "data.addRow($rowContents);\n";
}
?>
Of course you need to issue the onCall() and include a div to contain your
chart.
HTH,
Bruce
________________________________
From: dror h <[email protected]>
To: Google Visualization API <[email protected]>
Sent: Wednesday, August 10, 2011 1:44 PM
Subject: [visualization-api] create chart by getting data from mysql databasee
Hi all,
I'm trying to create a line chart through connection to my mysql
database via php code.
cant seem to do it....
any help?
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-visualization-api?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-visualization-api?hl=en.