Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Google Chart with data from a csv

I have a csv file that looks like that:

week,value1,value2
1,2,3
2,7,9

I would like to plot a stacked graph of it using google chart (week being my x (horizontal) values and values1 and values2 being the two set of y's). Unfortunately, I didn't find any easy way to do so. That probably relates to me being a complete noob in js.

Is there any simple way to do that?

like image 801
cpa Avatar asked Jan 08 '13 09:01

cpa


People also ask

How do I integrate a chart in Google?

The most common way to use Google Charts is with simple JavaScript that you embed in your web page. You load some Google Chart libraries, list the data to be charted, select options to customize your chart, and finally create a chart object with an id that you choose.

Can CSV files have graphs?

Graph data from your CSV files. Excel is a versatile spreadsheet application made by Microsoft for the Windows and Mac OSX operating systems. It features graphing, calculation and pivot tools, and the Visual Basic macro programming language. Use the handy graphing feature to turn your CSV data into graphs.


1 Answers

I have been searching for a while, and found the solution on a Google group discussion. https://groups.google.com/forum/#!topic/google-visualization-api/cnXYDr411tQ

I have tried it, and it works!

In this case, we have to specify the header types of our csv file.

var queryOptions = {
    csvColumns: ['number', 'number', 'number' /* Or whatever the columns in the CSV file are */],
    csvHasHeader: true /* This should be false if your CSV file doesn't have a header */
}
/* csvUrl is the path to your csv */    
var query = new google.visualization.Query(csvUrl, queryOptions);
query.send(handleQueryResponse);

function handleQueryResponse(response) {

    if (response.isError()) {
        alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        return;
    }

    var data = response.getDataTable();

    var chart = new google.visualization.ColumnChart(document.getElementById('your div'));
    // Draw your chart with the data table here.
    // chart.draw(view, queryOptions);
}
like image 90
Gurpreet Bajwa Avatar answered Sep 21 '22 20:09

Gurpreet Bajwa