Highcharts和php的动态图表

时间:2013-07-03 18:11:37

标签: php json dynamic highcharts

我有一段时间试图获取高价图表来绘制php返回的一些数据。我做了很多搜索,没有任何作用。我可以编写php来提供数据然而它需要但是如何让它动态地绘制它?????

我可以将其作为: [[ “1372875867”, “44.8782806”],[ “1372875885”, “46.2020226”]] 要么 [[1372876686,44.0655823],[1372876693,43.3360596]等]

但是如何从php输出中获取数据到他们显示的dyname示例?????

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

    <script type="text/javascript" 
     src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {

Highcharts.setOptions({
    global : {
        useUTC : false
    }
});

// Create the chart
window.chart = new Highcharts.StockChart({
    chart : {
        renderTo : 'container',
        events : {
            load : function() {

                // set up the updating of the chart eachsecond
                var series = this.series[0];
                setInterval(function() {
                    var x = (new Date()).getTime(), 
                    y = Math.round(Math.random() * 100);
                    series.addPoint([x, y], true, true);
                }, 1000);
            }
        }
    },

    rangeSelector: {
        buttons: [{
            count: 1,
            type: 'minute',
            text: '1M'
        }, {
            count: 5,
            type: 'minute',
            text: '5M'
        }, {
            type: 'all',
            text: 'All'
        }],
        inputEnabled: false,
        selected: 0
    },

    title : {
        text : 'Live random data'
    },

    exporting: {
        enabled: false
    },

    series : [{
        name : 'Random data',
        data : (function() {
            // generate an array of random data
            var data = [], time = (new Date()).getTime(), i;

            for( i = -999; i <= 0; i++) {
                data.push([
                    time + i * 1000,
                    Math.round(Math.random() * 100)
                ]);
            }
            return data;
        })()
    }]
        });

    });

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

    <div id="container" style="height: 500px; min-width: 500px"></div>
</body>
    </html>

我目前的php是:

    <?php
    //    include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
    include("adodb5/adodb.inc.php");
    $connection = new COM("ADODB.Connection") or die("Cannot start ADO"); 
    $result_set = $connection->Execute("
            SELECT tag, TIME, value 
            FROM picomp 
            WHERE TIME >= '*-3m' AND tag = 'xxx:xx_xxx.xxx'
            ");

    $result_count = 0;
    //    $labels = array();
    while (!$result_set->EOF) {
$pidate = date("U", strtotime($result_set->fields[1]) );
if ($result_count <> 0){
print ",";
}else{
print "[";
}
    print "[".$pidate.",".$result_set->fields[2]."]";
    //        array_push("{$result_set->fields[2]}");
    $result_count = $result_count +1;
    $result_set->MoveNext();
    //  echo "testing";
    }
    print "];";

2 个答案:

答案 0 :(得分:0)

你可以使用HighchartsPHP,它是Highcharts的包装器,它基本上允许你用PHP编写所有JS代码。它非常实用且非常简单。

HighchartsPHP on GitHub

答案 1 :(得分:0)

您的时间戳应乘以1000,两个值都应为数字。

请带着灵魂的家庭,如何准备JSON,因为你只打印“作为JSON”,但事实并非如此。

请看http://php.net/manual/en/function.json-encode.php,其中介绍了一些示例。