doutuoshou8915 2015-06-22 11:52
浏览 87
已采纳

使用php和mysql的Highstock

i want to make chart with mysql database. I have two columns - timestamp and temperature. This is the timestamp format - 2015-06-11 22:45:59 and the temperature is integer. Im not sure that my conversion of timestamp to javascript time is correct i have or maybe i have other mistake. this is my code:

data.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'] ."]";

}
?>

this is the output:

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

html file:

<!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>
  • 写回答

1条回答 默认 最新

  • donglin6659 2015-06-22 12:01
    关注

    Correct way to make JavaScript understand your PHP array is json_encode()

    Change your code block near while to

    $res = [];
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
       $res[] = [ strtotime($row['timestamp'])*1000, $row['temperature'] ];
    }
    
    echo json_encode($res);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么