【问题标题】:Highstock with php and mysqlHighstock 与 php 和 mysql
【发布时间】:2015-06-22 11:52:36
【问题描述】:

我想用 mysql 数据库制作图表。我有两列 - 时间戳和温度。这是时间戳格式 - 2015-06-11 22:45:59,温度是整数。我不确定我将时间戳转换为 javascript 时间是否正确,或者我可能有其他错误。这是我的代码:

数据.php

<?
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','...');
define('DBNAME','diplomna2');

try {

    //create PDO connection 
    $db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $e) {
    //show error
    echo '<p class="bg-danger">'.$e->getMessage().'</p>';
    exit;
}
$stmt = $db->query('SELECT * FROM temperature');

$res=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
   $row['timestamp'] = strtotime($row['timestamp']);
   $row['timestamp'] *=1000;

    echo $res="[".$row['timestamp'] . ',' . $row['temperature'] ."]";

}
?>

这是输出:

[1434051959000,25][1434051969000,26][1434051979000,26][1434051990000,28][1434052000000,27][1434052024000,25][1434052034000,24][1434052044000,23]

html 文件:

<!DOCTYPE HTML>
<html>
<head>
    <script type="text/javascript" src="js/jquery-1.7.1.min.js" ></script>
    <script src="http://code.highcharts.com/stock/highstock.js"></script>
    <script src="http://code.highcharts.com/stock/modules/exporting.js"></script>


    <script type="text/javascript">
        $(function() {
            $.getJSON('data.php', function(data) {

                // Create the chart
                $('#container').highcharts('StockChart', {


                    rangeSelector : {
                        inputEnabled: $('#container').width() > 480,
                        selected : 1
                    },

                    title : {
                        text : 'Temperature'
                    },

                    series : [{
                        name : 'temperature',
                        data : data,
                        type : 'areaspline',
                        threshold : null,
                        tooltip : {
                            valueDecimals : 2
                        },
                        fillColor : {
                            linearGradient : {
                                x1: 0,
                                y1: 0,
                                x2: 0,
                                y2: 1
                            },
                            stops : [
                                [0, Highcharts.getOptions().colors[0]],
                                [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                            ]
                        }
                    }]
                });
            });
        });
    </script>
</head>
<body>

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

【问题讨论】:

    标签: javascript php mysql highstock


    【解决方案1】:

    让 JavaScript 理解你的 PHP 数组的正确方法是 json_encode()

    while附近的代码块更改为

    $res = [];
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
       $res[] = [ strtotime($row['timestamp'])*1000, $row['temperature'] ];
    }
    
    echo json_encode($res);
    

    【讨论】:

    • 感谢您的快速回复。我更改了代码,但它不再起作用了。
    • 您遇到了什么错误,请检查您的浏览器控制台。
    • > 未捕获的类型错误:无法读取未定义的属性“addEvent”> 未捕获的类型错误:无法读取未定义的属性“fireEvent”> test_temperature.php:25 未捕获的语法错误:意外的标识符
    • omg .. :D 好的。我包括它,但有一个错误列表。当我打开它们时,出现以下错误:未捕获的类型错误:无法读取未定义的属性“addEvent”(匿名函数)@ highstock.js:308(匿名函数)@ highstock。 js:311(匿名函数)@ highstock.js:413 未捕获类型错误:无法读取未定义(匿名函数)的属性“fireEvent”@exporting.js:9(匿名函数)@exporting.js:23 未捕获语法错误:意外标识符。我会一一检查错误,看看问题出在哪里。感谢您的帮助:)
    • 一切正常。我添加 jquery 并将温度值更改为 int :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多