How to convert this array to add in google chart new data variable:
var array_column =["Year","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var array_rows = ["2014", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.67, 0, "2015", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.33, 0, "2016", 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, 4, 2.67, "2017", 2, 0, 0, 4.33, 2.67, 0, 2.5, 0, 0, 0, 0, 0, "2018", 0, 0, 2.17, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Basically the array_rows are dynamically added by 13. On array_column is fixed with the length of 13. Is there a way on how to add the array_rows above by 13 and make it like this?
var newData = [
['Year', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
["2014", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.67, 0],
["2015", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.33, 0],
["2016", 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, 4, 2.67],
["2017", 2, 0, 0, 4.33, 2.67, 0, 2.5, 0, 0, 0, 0, 0],
["2018", 0, 0, 2.17, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
// in this case the first column is of type 'string'.
dataTable.addColumn('string', newData[0][0]);
// all other columns are of type 'number'.
for (var i = 1; i < numCols; i++)
dataTable.addColumn('number', newData[0][i]);
// now add the rows.
for (var i = 1; i < numRows; i++)
dataTable.addRow(newData[i]);
// redraw the chart.
chart.draw(dataTable, options);
//--------------------updated-------------------------------
var array_column = ["Year","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var array_rows = ["2014", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.67, 0, "2015", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.33, 0, "2016", 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, 4, 2.67, "2017", 2, 0, 0, 4.33, 2.67, 0, 2.5, 0, 0, 0, 0, 0, "2018", 0, 0, 2.17, 0, 0, 0, 0, 0, 0, 0, 0, 0]
var splice=13;
var arr = array_rows;
var newarr = new Array();
console.log("newarr_column: "+array_column);
for (var i=0; i<arr.length; i=i+splice) {
newarr.push(arr.slice(i,i+splice));
console.log("slice: "+arr.slice(i,i+splice))
}
//Is this correct?
var newData = [array_column,newarr];
and it gives me this error in google chart:
When i used this variable it worked.
var newData = [
['Year', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
["2014", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.67, 0],
["2015", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.33, 0],
["2016", 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, 4, 2.67],
["2017", 2, 0, 0, 4.33, 2.67, 0, 2.5, 0, 0, 0, 0, 0],
["2018", 0, 0, 2.17, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];