Any way of simply read data from a CSV file?

2,225 views
Skip to first unread message

Ricardo Viana Barreto

unread,
Jan 23, 2012, 7:17:55 PM1/23/12
to Google Visualization API
My site generates CSV data from a PHP script, and I would like to
create a chart loading data from it. I've searched the whole gviz
website but I can't find a way. Does gviz offers that feature? If it
doesn't, is there any plan about implement this in a future update?

Viz Kid

unread,
Jan 24, 2012, 5:31:50 AM1/24/12
to google-visua...@googlegroups.com

There are some written libraries to handle data and our data protocol in PHP but I'm not sure this is what you are looking for.
There is nothing specific to parsing csv into our DataTable format in client side JS, as we typically assume that the data is sent to the client already using our data protocol.

  Viz Kid


--
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-visualizati...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.


asgallant

unread,
Jan 24, 2012, 9:01:07 AM1/24/12
to google-visua...@googlegroups.com
There is no support for directly loading CSV data into the API, but if you Google for "javascript csv to array" there are a bunch of examples for converting CSV's to js arrays, which you can then use to populate your charts' DataTable(s).

rio

unread,
Jan 25, 2012, 1:11:05 AM1/25/12
to Google Visualization API
Im not sure you actually need to make a csv file.. just pass the data
from the database directly into the google dataArray format (ie you
just build the javascript using PHP.)

I read data from a query and store data into an Array[].. Then
transposed the data out into the googleformat, also putting 0 in where
there was no data. But i think your suppose to use _ (not tested)

Ricardo Viana Barreto

unread,
Jan 25, 2012, 6:10:13 AM1/25/12
to Google Visualization API
Yes, you are right, I don't really need to load it using a CSV file. I
can use native array format.
The reason I was using CSV is because I intent to dynamically load
charts into the page. Because of this, the chart is not yet created
when you load the page. Only after the user select some specific data
file, the chart is created using that data. So I thought about loading
the desired data form a php file, in CSV (or array) format, like:
www.domain.com/generate_array_page.php?data_id=10
Then save it in a variable, and then load it.
After I read the answers to my post, I'm thinking about loading it to
a variable using Ajax, then converting using the
google.visualization.arrayToDataTable() mentioned before. Any thoughts
about that? Better idea?

Thanks all

asgallant

unread,
Jan 25, 2012, 9:37:26 AM1/25/12
to google-visua...@googlegroups.com
If you're feeling a bit more ambitious, the DataTable constructor can accept a JSON representation of the data (http://code.google.com/apis/chart/interactive/docs/reference.html#DataTable), which PHP can output rather easily via the json_encode() function.  This would be the first step in implementing compatibility with the API query functions, if you choose to do so.

Ricardo Viana Barreto

unread,
Jan 25, 2012, 12:16:29 PM1/25/12
to Google Visualization API
asgallant,
That suggestion was awesome! That feature is exactly what I was
looking for.
Very very nice.
I made an array in a PHP script, in the format documented at the link
you sent, then I used json_encode() and echoed the results.
Then I made an Ajax request to the PHP script, and loaded it when
creating the chart object. Work's perfectly.

Thank you all.

On Jan 25, 12:37 pm, asgallant <drew_gall...@abtassoc.com> wrote:
> If you're feeling a bit more ambitious, the DataTable constructor can
> accept a JSON representation of the data (http://code.google.com/apis/chart/interactive/docs/reference.html#Dat...),

asgallant

unread,
Jan 25, 2012, 12:23:44 PM1/25/12
to google-visua...@googlegroups.com
You're welcome.

asgallant

unread,
Jul 31, 2012, 10:58:35 AM7/31/12
to google-visua...@googlegroups.com
Here's a post with some example code that I used to generate a a JSON representation of a DataTable: https://groups.google.com/forum/#!msg/google-visualization-api/Gy18YdVSSfQ/tKdtzSfqKlcJ 

You can then draw the chart using something like this:

google.load('visualization''1'{packages['corechart']});
google.setOnLoadCallback(drawChart);

function drawChart ({
    $.ajax({
        dataType'json',
        url'/path/to/data/source.php',
        successfunction (json{
            // convert json into DataTable object
            var data new google.visualization.DataTable(json);
            // create and draw chart 
            var chart new google.visualization.LineChart(document.getElementById('chart_div'));
            chart.draw(data{/*options*/});
        }
    });
} 

On Tuesday, July 31, 2012 10:09:25 AM UTC-4, Shaowen Hu wrote:
Can anybody post an example code on how to do this? I am new to Ajax and PHP, and Google Chart.

Thanks a lot in advance,
Shaowen
Reply all
Reply to author
Forward
0 new messages