[英]JS or JQuery - pushing data to a 2 dimensional array?
我正在使用jqPlot構建一些動態圖。
jqPlot將2D數組作為其數據,因此:
$.jqplot('chartdiv', [[[1, 2],[2,5.12],[3,13.1],[7,33.6],[9,85.9],[21,219.9]]],
{ title:'progress',
axes:{yaxis:{min:0, max:240}},
series:[{color:'#5FAB78'}]
});
第一個數字是x軸(在這種情況下是1,2,3,7,9和21),第二個數字是y軸。 chartdiv是顯示圖形的html元素,其余的不言而喻。
我想從localStorage中獲取數據並將其推送到一個數組,該數組將允許我將其顯示為圖形,但我不知道該怎么做。
如果我的數組聲明為
var graphdata = [];
如何將數據推入其中? 我正在使用像這樣的循環來獲取內容:
var dt = new Date();
var today = dt.getDateKey();
var df = getStartDate(period);
var datefrom = df.getDateKey();
var graphdata = [];
alert("Today is " + today + " and the graph will be calculated from " + datefrom);
while (df<=dt) {
dataObject = JSON.parse(localStorage.getItem(df.getDateKey()));
if(dataObject){
var dateposition = df.getDateKey();
console.log("Date is " + dateposition);
console.log("The Weight is " + dataObject.savedobs1);
graphdata.push([dateposition,dataObject.savedobs1]);
}
df.setDate(df.getDate() + 1);
};
console.log("Graph data " + graphdata);
我需要將日期位置和數據輸入圖表中,但我無法弄清楚如何將其轉換為正確的格式。 圖形數據的console.log僅給出:
Graph data ,26/04/2014,118,27/04/2014,116,28/04/2014,116,29/04/2014,105
您當前的設置是將數據追加到頂級數組中的第一個空數組之后。
如果問題確實是關於如何將數據推入該嵌套數組的問題,則只需在完成后將其包裝即可。
// create an empty array
var list = [];
// push data
list.push([1,2]);
list.push([3,4]);
list.push([5,6]);
// wrap the result before you return it
console.log([list]);
// returns [[[1,2],[3,4],[5,6]]]
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.