variables between PHP and the API

202 views
Skip to first unread message

maxweb59

unread,
May 6, 2012, 4:13:17 PM5/6/12
to Google Visualization API
Hello ! I would like to use the google charts API for my website, but
i've one enormous problem: my data (these who i need to make a chart)
are found in my DataBase (MySQL) by PHP, and are in PHP vars, but the
API want the vars directly in the javascript function. How can I put
my PHP vars into the javascript function of the API ?
Thanks for your help.

(and sorry for my english, i'm french :) )

Jérôme

unread,
May 7, 2012, 2:53:06 AM5/7/12
to google-visua...@googlegroups.com
you need to transform your SQL data into a json file, with a structure recognized by google API.
You have an example there :

asgallant

unread,
May 7, 2012, 10:17:18 AM5/7/12
to google-visua...@googlegroups.com
I posted a code snippet I use for transforming database output from PHP into JSON that the charts can understand: https://groups.google.com/d/msg/google-visualization-api/Gy18YdVSSfQ/tKdtzSfqKlcJ

You can either echo that output into the constructor of the DataTable - ie:

var data new google.visualization.DataTable(<?php echo $json_encoded_data?>); 

or you can use that code as a data source and make AJAX calls to it, putting the returned json into the DataTable constructor, ie:

google.load('visualization''1.0'{'packages'['corechart']});
google.setOnLoadCallback(init);

function init ({
    // get parameters to submit to data source
    var params;
    
    // get data w/ AJAX call
    $.ajax({
        url'/path/to/data_query.php',
        dataparams,
        typejson,
        successfunction (json{
            var data new google.visualization.DataTable(json);
            var chart new google.visualization.ChartType(document.getElementById('chart_div'));
            chart.draw(data{/*options*/});
        }
    });
} 
Reply all
Reply to author
Forward
0 new messages