如何轉換此陣列在谷歌添加圖表新的數據變量:谷歌圖用動態數組
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]
基本上array_rows通過13. array_column動態添加被固定的13的長度 是有一種方法,如何將13上面的array_rows添加到像這樣?
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];
,這讓我這個錯誤在谷歌圖表:
當我使用d它工作的這個變量。
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]
];
將數組拆分爲塊的邏輯可以在這裏找到(https://stackoverflow.com/questions/8495687/split-array-into-chunks) – James
我剛更新了這個問題。 –