mysql highcharts php rtg


mysql highcharts php rtg

首先我想说的是,我已经看了stackoverflow和highcharts论坛,但还没有找到我的问题的答案,所以我希望有一个善良的灵魂能为我提供一个解决我问题的例子。

我正试图从rtg(rtg.sourceforge.net)创建的mysql数据库中的数据创建一个样条曲线图(而不是自动更新)

我不是程序员,所以请容忍我没有创建干净/正确的代码(这包括从其他几个来源复制/粘贴)。

有3个表id(INT)、dtime(DATETIME)和counter(BIGINT),示例如下:

1   2012-03-05 17:49:06 16991
2   2012-03-05 17:50:06 3774
3   2012-03-05 17:50:06 1272

(1,2,3为接口名称)

我正在尝试创建一个图表,显示最后一个小时的流量。

这是我的数据的内容。php

<?php
$con = mysql_connect("localhost","root","XXXXXXXX");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("rtg", $con);
$result = mysql_query("SELECT * FROM ifInOctets_1 ORDER BY dtime DESC LIMIT 0,60");
     while($row = mysql_fetch_array($result)) {
  echo $row['dtime'] . "'t" . $row['counter']. "'n";
}
mysql_close($con);
?>

data.php的输出格式如下:

2012-03-05 20:53:31 245891 2012-03-05 20:53:31  8530 2012-03-05 20:53:31    6424577

rtg轮询3次pr-min,因此从上面的sql查询中得到180个输出"字段"。

这是我的index.php 的内容

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 

<title>Chart 1 Hour</title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js" ></script>
<script type="text/javascript" src="js/highcharts.js" ></script>
<script type="text/javascript" src="js/themes/gray.js"></script>
<script type="text/javascript">
        var chart;
                        $(document).ready(function() {
                                var options = {
                                        chart: {
                                                renderTo: 'container',
                                                defaultSeriesType: 'spline',
                                                marginRight: 130,
                                                marginBottom: 25
                                        },
                                        credits: {
                                           enabled: false
                                        },
                                        title: {
                                                text: 'Bits',
                                                x: -20 //center
                                        },
                                        subtitle: {
                                                text: '',
                                                x: -20
                                        },
                                        xAxis: {
                                                type: 'datetime',
                                                tickInterval: 3600 * 1000, // one hour
                                                tickWidth: 0,
                                                gridLineWidth: 1,
                                                labels: {
                                                        align: 'center',
                                                        x: -3,
                                                        y: 20,
                                                        formatter: function() {
                                                                return Highcharts.dateFormat('%Y%m%d%H%M%S', this.value);
                                                        }
                                                }
                                        },
                                        yAxis: {
                                                title: {
                                                        text: 'Bits'
                                                },
                                                plotLines: [{
                                                        value: 0,
                                                        width: 1,
                                                        color: '#808080'
                                                }]
                                        },
                                        tooltip: {
                                                formatter: function() {
                                                return Highcharts.dateFormat('%Y%m%d%H%M%S', this.x-(1000*3600)) +'-'+ Highcharts.dateFormat('%l%p', this.x) +': <b>'+ this.y + '</b>';
                                                }
                                        },
                                        legend: {
                                                layout: 'vertical',
                                                align: 'right',
                                                verticalAlign: 'top',
                                                x: -10,
                                                y: 100,
                                                borderWidth: 0
                                        },
                                        series: [{
                                                name: 'BlaBla'
                                        }]
                                }
                                // Load data asynchronously using jQuery. On success, add the data
                                // to the options and initiate the chart.
                                // This data is obtained by exporting a GA custom report to TSV.
                                // http://api.jquery.com/jQuery.get/
                                jQuery.get('data.php', null, function(tsv) {
                                        var lines = [];
                                        traffic = [];
                                        try {
                                                // split the data return into lines and parse them
                                                tsv = tsv.split(/'n/g);
                                                jQuery.each(tsv, function(i, line) {
                                                        line = line.split(/'t/);
                                                        date = Date.parse(line[0] +' UTC');
                                                        traffic.push([
                                                                date,
                                                                parseInt(line[1].replace(',', ''), 10)
                                                        ]);
                                                });
                                        } catch (e) {  }
                                        options.series[0].data = traffic;
                                        chart = new Highcharts.Chart(options);
                                });
                        });
</script>
</head>
<body>
<div id="container" style="width: 960px; height: 250px; margin: 0 auto"></div>
</body>
</html>

当我运行代码时,看起来所有的数据都被限制在图表的左侧。(很抱歉,第一次用户无法发布屏幕截图。)

由于highcharts需要以毫秒为单位的日期+时间,我尝试过(在其他几个sql select语句中)更改highcharts.dateFormat,但没有成功。

提前谢谢。

您需要将返回日期转换为自epoch以来的毫秒数。

因此SELECT * FROM ifInOctets_1 ORDER BY dtime DESC LIMIT 0,60将变为:

`选择UNIX_TIMESTAMP(dtime)*1000,计数器FROM ifInOctets_1 ORDER BY dtime DESC LIMIT 0,60"

这样,您就可以获得highcharts所期望的以ms为单位的正确时间戳值。