4

So I have the following file, data.csv. Which looks like this:

TestID,Cronbach,Percent Correct,Population
12416,0.866,0.17,26
12421,0.945,0.21,8
12385,0.777,0.40,258
12412,0.85,0.44,34
12407,0.831,0.45,48

And I want it to look like this:

[
["Test ID", "Cronbach", "Percent Correct", "Population"],
["12416", 0.866, 0.17, 26],
["12421", 0.945, 0.21, 8],
["12385", 0.777, 0.40, 258],
["12412", 0.85, 0.44, 34],
["12407", 0.831, 0.45, 48]
]

Is there a way I can make a conversion code in php to convert my CSV file to look like the above format. I need this because I want to put the code into a Google Bubble Chart.

Q: How might I go about making the code that can convert this to an acceptable format to fit Googles bubble chart?

programking
  • 1,376
  • 1
  • 17
  • 32

1 Answers1

2

The following example demonstrates how to parse csv file content:

function prepareChartData(data) {
    var items = [];
    var lines = data.split(/\r\n|\n/);

    lines.forEach(function(line,i){
         if(line.length > 0){
             var item =  line.split(','); 
             if(i > 0){
                 item[1] = parseFloat(item[1]);
                 item[2] = parseFloat(item[2]);
                 item[3] = parseInt(item[3]);   
             }
             items.push(item);
         }
    });
    return items;
}

Result

[
  [
    "TestID",
    "Cronbach",
    "Percent Correct",
    "Population"
  ],
  [
    "12416",
    0.866,
    0.17,
    26
  ],
  [
    "12421",
    0.945,
    0.21,
    8
  ],
  [
    "12385",
    0.777,
    0.4,
    258
  ],
  [
    "12412",
    0.85,
    0.44,
    34
  ],
  [
    "12407",
    0.831,
    0.45,
    48
  ]
]

Complete example

google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(function(){
    readChartData()
    .then(prepareChartData)
    .done(drawChart);
});

function drawChart(items) {

    var data = google.visualization.arrayToDataTable(items);
    var options = {
        title: 'Chart'
    };
    var chart = new google.visualization.BubbleChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}


function readChartData(complete)
{
    return $.ajax({
        type: "GET",
        url: "data.csv",
        dataType: "text"
    });  
}


function prepareChartData(data) {
    var items = [];
    var lines = data.split(/\r\n|\n/);

    lines.forEach(function(line,i){
         if(line.length > 0){
             var item =  line.split(','); 
             if(i > 0){
                 item[1] = parseFloat(item[1]);
                 item[2] = parseFloat(item[2]);
                 item[3] = parseInt(item[3]);   
             }
             items.push(item);
         }
    });
    return items;
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • 1
    Thank you so much! I am highly appreciative. – programking Jun 08 '15 at 12:44
  • This was answered two days ago and the post was closed instead of being edited. http://stackoverflow.com/questions/30688427/how-to-make-a-google-charts-bubble-chart-from-csv-file/30688627#30688627. Even a fully functional demo was provided. http://jsbin.com/wavacidupu/2/edit?html,js,output – Helmut Granda Jun 09 '15 at 16:51