I'm trying to get some array data into a particular format so I can use google linechart. But I can't quite get it right.
Right now I have the format
//format [date, id, count
var data = [
["2014-04-01", "1", "100"],
["2014-04-02", "1", "200"],
["2014-04-03", "1", "150"],
["2014-04-04", "1", "5"],
["2014-04-01", "2", "200"],
["2014-04-02", "2", "600"],
["2014-04-03", "2", "15"],
["2014-04-04", "2", "25"],
["2014-04-01", "3", "99"],
["2014-04-02", "3", "85"],
["2014-04-03", "3", "555"],
["2014-04-04", "3", "0"]
];
I need to get it into the format:
var reformatted = [
['Date', '1', '2', '3'],
['2014-04-01', 100, 200, 99],
['2014-04-02', 200, 600, 85],
['2014-04-03', 150, 15, 555],
['2014-04-04', 5, 25, 0]
]);
var graph = [["date"]];
//first element of array to be populated with array of ID's
//always second element of inner arrays
//these will be the lines of the graph
for (var i = 0; i < data.length; i++){
if (graph[0].indexOf(data[i][1]) < 0){
graph[0].push(data[i][1]);
}
}
This puts me in a pretty good place. I get:
Array[1]]
0: Array[4]
0: "date"
1: "1"
2: "2"
3: "3"
But I'm stumped on how to get the rest of the data in the appropriate format. Any ideas?
Tried this. No good result.
for (i = 0; i < data.length; i++){
graph[i + 1] = graph[i + 1] || [];
graph[i + 1].push(data[i][2]);
}