2017-07-27 140 views
1

如何轉換此陣列在谷歌添加圖表新的數據變量:谷歌圖用動態數組

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];

,這讓我這個錯誤在谷歌圖表:

enter image description here

當我使用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] 
      ]; 
+1

將數組拆分爲塊的邏輯可以在這裏找到(https://stackoverflow.com/questions/8495687/split-array-into-chunks) – James

+0

我剛更新了這個問題。 –

回答

1

我修改了你的代碼以便與你已經寫過的代碼保持一致,但是它可以被清理一下。

與初始解決方案的問題是,這種聲明,

var newData = [array_column,newarr]; 

被創建與列陣列作爲第一元件和包含與該行的數組中的第二陣列的陣列。爲了使圖表的工作,你需要有一個陣列列作爲第一要素和第一排的第二個元素,第二行的第三個元素,依此類推......

這就是爲什麼我修改了代碼推array_column到結果陣列的行被分割之前(感謝@WhiteHat)

newarr.push(array_column); 

下面是修改代碼:

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); 
 

 
newarr.push(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)) 
 
} 
 

 
newData = newarr;

+1

可能更容易,只是在切分行之前將'array_column'添加到'newarr',然後完成... – WhiteHat

+0

@WhiteHat是的,你說得對,我會將該更改作爲答案。 – rolspace