【问题标题】:mysql highcharts php rtgmysql highcharts php rtg
【发布时间】:2012-03-05 22:17:26
【问题描述】:

首先我想说的是,我查看了 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是接口名称)

我正在尝试创建一个显示过去一小时流量的图表。

这是我的data.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 轮询 pr min 3 次,因此这会从上述 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 但没有任何运气。

提前致谢。

【问题讨论】:

    标签: php mysql sql highcharts


    【解决方案1】:

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

    所以SELECT * FROM ifInOctets_1 ORDER BY dtime DESC LIMIT 0,60 会变成:

    `SELECT UNIX_TIMESTAMP(dtime)*1000,counter FROM ifInOctets_1 ORDER BY dtime DESC LIMIT 0,60"

    这样您就可以得到 highcharts 期望的正确时间戳(以 ms 为单位)。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 2012-05-19
      • 2018-03-03
      • 2018-03-23
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 2018-08-19
      • 2012-10-12
      相关资源
      最近更新 更多