create chart by getting data from mysql databasee

170 views
Skip to first unread message

dror h

unread,
Aug 10, 2011, 1:44:12 PM8/10/11
to Google Visualization API
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?

NA

unread,
Aug 10, 2011, 2:13:32 PM8/10/11
to Google Visualization API
What's the problem you're having?

Bruce Smith

unread,
Aug 10, 2011, 2:42:40 PM8/10/11
to google-visua...@googlegroups.com
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 <dror...@gmail.com>
To: Google Visualization API <google-visua...@googlegroups.com>
Sent: Wednesday, August 10, 2011 1:44 PM
Subject: [visualization-api] create chart by getting data from mysql databasee
--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To post to this group, send email to google-visua...@googlegroups.com.
To unsubscribe from this group, send email to google-visualization-api+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.



asgallant

unread,
Aug 10, 2011, 3:53:29 PM8/10/11
to google-visua...@googlegroups.com, Bruce Smith
Post some of the generated JS, and we'll see what is going on with it.

dror h

unread,
Aug 26, 2011, 11:50:49 AM8/26/11
to Google Visualization API
Thanks ,
I'm a bit of a newbee and basically what I did is copy the code
exactly as shown on this page: http://code.google.com/apis/chart/interactive/docs/php_example.html
but am not sure where to put my data query and sql...

Thanks again,

Dror
Reply all
Reply to author
Forward
0 new messages