2013-02-08 29 views
1

我似乎無法弄清楚我有多個圖表並用一個更新函數更新這些圖表。
我有一個HTML文件,在該文件中,我有這樣的:Highcharts:多個圖表和一個更新功能

<script type="text/javascript" src="highchart/jquery-1.9.0.min.js"></script> 
<script type="text/javascript" src="highchart/highcharts.js"></script> 
<script type="text/javascript" src="highchart/highcharts-more.js"></script> 
<script type="text/javascript" src="windspeed.js"></script> 
<script type="text/javascript" src="livedata.js"></script> 

windspeed.js:

$(function() { 
    var windspeed = new Highcharts.Chart({ 
     chart: { 
      renderTo: 'windspeed', 
      type: 'gauge', 
      plotBackgroundColor: null, 
      plotBackgroundImage: null, 
      plotBorderWidth: 0, 
      plotShadow: false 
     }, 
etc. 
} 

livedata.js:

$(function (livedata) { 
     setInterval(function() { 
     $(function() { 
     $.getJSON('livedata.php', function(data) { 
      $.each(data, function(key,val) { 
      if (key == 'WindSpeed') 
      { 
      var point = windspeed.series[0].points[0]; 
       point.update(val); 
      } 
      }); 
     }); 
     }) 
    },2000) 
    } 
); 

的圖表彈出,但數據不更新,得到這個錯誤:無法獲得屬性'0'的值:對象爲空或未定義
我認爲這有som因爲它處於不同的範圍,所以livedata.js無法看到風速變量。
有人可以告訴我如何讓這個工作?

+0

我認爲它是與這個主題相關http://stackoverflow.com/questions/14716373/highchart-live-data-gauge – 2013-02-08 12:18:34

+0

不是真的,我想更新幾個現在,但變量似乎出範圍?更新一個單一的測量工作正常:) – HyperDevil 2013-02-08 12:28:10

回答

3

您沒有外界$訪問的變量(函數()),它們是本地:

$(function() { 
    var windspeed = new Highcharts.Chart({ 
     ... 
    }); 
    var windspeed2 = new Highcharts.Chart({ 
     ... 
    }); 
    var windspeed3 = new Highcharts.Chart({ 
     ... 
    }); 
}); 

您可以將它們移出該範圍:

var windspeed, windspeed2, windspeed3; 
$(function() { 
    windspeed = new Highcharts.Chart({ 
     ... 
    }); 
    windspeed2 = new Highcharts.Chart({ 
     ... 
    }); 
    windspeed3 = new Highcharts.Chart({ 
     ... 
    }); 
}); 

所以現在,它們是全局的,所以你也可以在其他文件中訪問它們。

+0

這就是我雖然變量是本地:)謝謝! – HyperDevil 2013-02-08 13:09:25