用于更新图表的getJSON数据

时间:2012-12-04 18:14:54

标签: javascript json javascript-events jsonp getjson

  

工作聊天示例:   http://www.highcharts.com/demo/dynamic-update/gray

我正在尝试上述工作,除了修改,其中上面的图表填充了随机数据;我希望使用$ .getJSON命令从非域数据库中提取数据并将其插入聊天中。

到目前为止,我可以加载初始数据,以便创建静态图表。为了使它静态,我必须注释掉setInterval()的内容。

当我将setInterval()包含一些静态数据时,我在Google Chrome中的JavaScript控制台上收到错误(Ctrl + Shift + J)。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript" src="date.js"></script>
        <script type="text/javascript">





$(function () {
    $(document).ready(function() {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });



                    var jsonUrl = "http://www.somewebsite.co.uk/jsonusv.php?callback=?";                    
                    $.getJSON(jsonUrl,{lastdatetime: "",},function(zippy){                  
                        for (i=0; i<zippy.cpmdata.length; i++){
                                console.log("TIMESTAMP: "+zippy.cpmdata[i].timestamp+" AFTER: ");
                                zippy.cpmdata[i].timestamp =  Date.parse(zippy.cpmdata[i].timestamp).getTime(); 

                                //var unixtime  Date.parse(temptime).getTime()/1000
                                console.log(" TESST "+zippy.cpmdata[i].timestamp+" \r\n");                              
                                console.log(" TESST zippy "+zippy.cpmdata[i].timestamp+" \r\n");







        var chart;
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'spline',
                marginRight: 10,
                events: {
                    load: function() {

                        // set up the updating of the chart each second

                        var series = this.series[0];
                        var tttt = "2012-11-13 17:17:00";
                        setInterval(function() { var x = Date.parse(tttt).getTime(),y = 8;
                        series.addPoint([x, y], true, true);
                        }, 5000);
                    }
                }
            },
            title: {
                text: 'Online Counter'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'units/h'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function() {
                        return '<b>'+ this.series.name +'</b><br/>'+
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
                        Highcharts.numberFormat(this.y, 2)+" uSv/h";
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'units/h ',
                data: (function() {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;


                    console.log(" =============== \r\n");
                    console.log(" FINAL "+zippy.cpmdata[5].timestamp+" \r\n");

                    for (i=0; i<zippy.cpmdata.length; i++){
                            data.push({
                            x: zippy.cpmdata[i].timestamp,
                            y: parseInt(zippy.cpmdata[i].cpm,10)
                            });
                        }

                    return data;
                })()
            }]
        });                             


                            }  });
                    console.log("==END==");                     
    });

});

        </script>



    </head>
    <body>
<script src="./js/highcharts.js"></script>
<script src="./js/modules/exporting.js"></script>
<script src="gray.js"></script> 

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

    </body>
</html>
  

我不断收到并且错误未捕获错误:NOT_FOUND_ERR:DOM   例外8 highcharts.js:167 我不知道为什么?

     

第二个问题是由于$ .getJSON的异步性质可以   使用相同的方法获取原始$ .getJSON的数据,但调用它   setInterval()调用来更新动态数据。

0 个答案:

没有答案