2013-07-01 62 views
0

我有一個更新當前比特幣價格的圖表,但是,x軸上的日期和時間不正確。它始終在12月31日19:00開始。在我的控制檯中,我收到了正確的日期和時間,但似乎無法讓它在圖表上正確顯示。我很確定我需要將一個數組粘貼到另一個數組中。任何幫助表示讚賞,謝謝。如何將數組放入另一個數組?

$.ajax({ 
     url: "/chart/ajax_get_chart", // the URL of the controller action method 
     dataType: "json", 
     type: "GET", 
     success: function (result) { 
      var result = JSON.parse(result); 
      series = []; 
      for (var i = 0; i < result.length; i++) { 
      tempDate = Date.parse(result[i]['date']); 
      tempArray = [parseFloat(result[i]['price'])]; 
      var date = new Date(); 
      tempDate = date.toString(); 
      series.push(tempArray); 
      var tempDate = tempDate.concat(tempArray); 
      } 
+0

從什麼時候開始數字或字符串有'concat'方法? – Ian

+0

您可以像添加任何其他值一樣添加數組。 –

+0

@Ian:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat。 –

回答

1

如果我理解正確的,對於每一個i

  • result[i]['date']給人以i
  • result[i]['price']位置的日期先給在i位置

價格現在所有的,讓我們來看看你的循環:

for (var i = 0; i < result.length; i++) { 

     //Here you grab the date from the result array 
     tempDate = Date.parse(result[i]['date']); 

     //Here you grab the price from the result array and parse it to a float 
     tempArray = [parseFloat(result[i]['price'])]; 

     //Here you grab the current date (of today) 
     var date = new Date(); 

     //Here you overwrite the 'grabbed date' with the current date (of today) 
     tempDate = date.toString(); 

     //Here you add the price at position i to the 'result array' called series 
     series.push(tempArray); 

     //Here you join tempArray with the tempDate 
     var tempDate = tempDate.concat(tempArray); 
     } 

那究竟出了什麼問題呢?密切關注的代碼,以及在提示框正是「顯示了」 http://jsfiddle.net/KzLFr/1/

看:

嗯,我已經準備了的jsfiddle你。您會看到tempDate的值被當前日期覆蓋。將這個信息應用到你的循環中:你會看到每次迭代都會覆蓋從result陣列中獲取的日期和當前日期。

這意味着'實際'日期(從result[i]['date'])總是被覆蓋,因此,如果您要在每次迭代時將所有tempDates添加到數組,則最終將得到一個包含當前日期的result.length倍數組。

另一個有趣的一點是你的聲明:var tempDate = tempDate.concat(tempArray); 正如你可以在的jsfiddle第二警報中看到,這將創造出具有後對方兩個數組的數組。但爲什麼在世界上你會在每一次迭代中做到這一點?

此外,你永遠不會東西與你的日期。你不要將它們添加到數組或任何東西:你只需創建它們,然後讓它們獨立。

這引出了一個問題:你究竟想做什麼?

因此:

注:這可能是我錯了,但如果我是正確的,你想與包含x-axis信息和y-axis信息的數組就結了。因爲它是而不是從你的問題和你的代碼中清楚你如何實現這一點,我只能猜測。

所以通過猜測你想結束了一下,我會改寫你的循環如下:

var dateData = []; 
var priceData = []; 

for(var i = 0; i < result.length; i++){ 
    dateData.push(result[i][0]); 
    priceData.push(result[i][1]); 
} 

你可以看到這是如何工作在這裏:http://jsfiddle.net/WT58s/1/

如果不解決您的問題,請解釋一下你想要的結果(一個二維數組還是兩個不同的數組?),所以我們可以幫助你更好。

此外,通常當您從互聯網下載信息時,信息會反向存儲;所以從當前日期開始,然後及時倒退。

試試看看您的數據是否也是這種情況。如果這是事實確實如此,你可以讀到這裏倒車:

Reverse order of elements in 2-dimensional array

+0

感謝您深入解釋您的答案。我想要做的是將當前時間顯示在圖表的x軸上。當我console.log,當前時間正確顯示在我的控制檯。但我不能讓它正確顯示在圖表上。 – evann

+0

@evann:你可以給你正在使用的插件的鏈接? –

相關問題