1

複数のチャートがあり、1つの更新機能でこれらを更新する方法を理解できないようです。
私はHTMLファイルを持っています、そのファイルに私はこれを持っています:

<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」の値を取得できません:オブジェクトがnullまたは未定義
ですこれは、livedata.jsが風速変数を認識できないために何か関係があると思います別のスコープで。
誰かがこれを機能させる方法を教えてもらえますか?

4

1 に答える 1

3

$(function())の外部の変数にアクセスできません。これらはローカルです:

$(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({
        ...
    });
});

現在、これらはグローバルであるため、他のファイルでもアクセスできます。

于 2013-02-08T12:55:51.710 に答える