2011-09-11 3 views
-2

나는 highchartsjQuery에서 두 번째 CSV를로드하려면 어떻게해야합니까?

$(function() { 
     $.get('trades.csv', function(csv, state, xhr) { 

      // inconsistency 
      if (typeof csv != 'string') { 
       csv = xhr.responseText; 
      } 

      // parse the CSV trades 
      var trades = [], header, comment = /^#/, x; 

      $.each(csv.split('\n'), function(i, line){ 
       if (!comment.test(line)) { 
        if (!header) { 
         header = line; 
        } 
        else { 
         var point = line.split(';'), 
          date = point[0].split('-'); 
          time = point[1].split(':'); 

         if (point.length > 1) { 
          x = Date.UTC(date[2], date[1] - 1, date[0], time[2], time[1], time[0]); 

          trades.push([ 
           x, // time 
           parseFloat(point[2]) // close 
          ]); 
         } 
        } 
       } 

      }); 

한다고 가정 내가 = VAR 스프레드에 두 번째 spreads.csv를로드 할에서 다음 코드 ...

나는 붙여 넣기 $ 적절한 다시 갔지 시도했다 페이지가 더 이상로드되지 않습니다. 두 번째 CSV를로드하는 올바른 구문은 무엇입니까?

+1

오류 콘솔에 어떤 오류가 있습니까? 어느 시점에서 두 번째 파일을로드 하시겠습니까? –

답변

1
function getCsv(file_name, array_holder){ 
    $.get(file_name, function(csv, state, xhr) { 

     // inconsistency 
     if (typeof csv != 'string') { 
      csv = xhr.responseText; 
     } 

     // parse the CSV trades 
     var header, comment = /^#/, x; 

     $.each(csv.split('\n'), function(i, line){ 
      if (!comment.test(line)) { 
       if (!header) { 
        header = line; 
       } 
       else { 
        var point = line.split(';'), 
         date = point[0].split('-'), 
         time = point[1].split(':'); 

        if (point.length > 1) { 
         x = Date.UTC(date[2], date[1] - 1, date[0], time[2], time[1], time[0]); 

         array_holder.push([ 
          x, // time 
          parseFloat(point[2]) // close 
         ]); 
        } 
       } 
      } 

     }); 
    }); 
} 

$(function() { 
    var trades = [], 
     spreads = []; 
    getCsv('trades.csv', trades); 
    getCsv('spreads.csv', spreads); 

}); 
관련 문제