10

我认为这是因为调用var chart我的函数时我的全局尚未设置requestData

这是我里面的highcharts代码$(document).ready(function()

        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                defaultSeriesType: 'spline',
                marginRight: 130,
                marginBottom: 25,
                events: {
                load: requestData()
                }
            },
            title: {
                text: 'Reporte PMU',
                x: -20 //center
            },
            subtitle: {
                text: '',
                x: -20
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150,
                maxZoom : 20 * 1000,
                title: {
                  text: 'tiempo'
                },
                labels : { y : 0, rotation: -60 }
            },
            yAxis: {
                title: {
                  text: 'Amount'
                },
                plotLines: [{
                  value: 0,
                  width: 1,
                  color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                  return '<b>'+ this.series.name +'</b><br/>'+
                  this.x +': '+ this.y;
                }
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: [{
                name: 'serieTension',
                data: []
            }]
        });     
    });

这是我的requestData ()

        $.ajax({                        
            url: 'data2.php', 
            success: function(point) {
                var series =chart.series[0],
                shift = series.data.length > 20; //shift if the series is longer than 20

                //add point
                chart.series[0].addPoint(point, true, shift);

                setTimeout(requestData, 1000);
            },
            cache : false
        });
    }
4

1 回答 1

7

只需放置

var chart;

在所有功能之外,在您的文档就绪处理程序旁边使其成为全局。

编辑:

另外,在 load 调用中添加一个引用

load: function() {
    chart = this; // `this` is the reference to the chart
    requestData();
}
于 2013-07-25T19:56:11.623 回答