2014-04-05 36 views
0

我在找到一種方法將我從javascript文件中的另一個函數中獲取的數據插入我的線圖中。我只是想確保這是支持的,我不必切換到不同的圖形包。這裏是我當前的代碼,我猜你必須將for循環添加到數據系列中,但我不確定如何實現這一點。將數據從數組插入到線圖中的高圖表

function displayGraph(){ 
    //data to insert 
    var x =new Array(0, 1, 2, 3, 4); //array of x coordinates 
    var y =new Array(1, 1.8, 1.5, 2.5, 6.3);//array of y coordinates 

$(function() { 
     $('#container').highcharts({ 
      title: { 
       text: 'Glaicer Elevations', 
       x: -20 //center 
      }, 
      xAxis: { 
       title: { 
        text: 'xAxis' 
       }, 
       plotLines: [{ 
        value: 0, 
        width: 1, 
        color: '#808080' 
       }] 
      }, 
      yAxis: { 
       title: { 
        text: 'yAxis' 
       }, 
       plotLines: [{ 
        value: 0, 
        width: 1, 
        color: '#808080' 
       }] 
      }, 
      tooltip: { 
       valueSuffix: 'km' 
      }, 
      legend: { 
       layout: 'vertical', 
       align: 'right', 
       verticalAlign: 'middle', 
       borderWidth: 0 
      }, 
     // want to insert another data series using outside arrays 
      series: [{ 
       name: 'Line1', 
       data: [[5, 2], [6, 3], [8, 2]] 
      }] 

     }); 
    }); 
} 

回答

0

如果您可以改變功能,您應該以正確的格式準備數據。就像你的例子中的示例數據。 如果你不能,那麼做一個for循環來準備數據。這樣的事:

xy= []; 
    for(var i in x) { 
     xy[i] = [x[i],y[i]]; 
    } 
+0

一旦我準備好這樣的數據,我將如何調用它來在data:tag中進行圖形化呢? – user2461876

+0

我將它重命名爲xy,所以你沒有任何衝突。你簡單設置'data:xy' – Asped

+0

非常感謝!你幫了我很多。 – user2461876

相關問題