googlecharts php postgresql timestamp format datetime axis cleaner

时间:2017-01-02 11:45:09

标签: postgresql format timestamp axis pygooglechart

我的应用程序准确启动并运行,但底部轴上的日期格式很糟糕。 https://s28.postimg.org/fts5h6fel/app.jpg

这是我的底轴格式的目标: https://s28.postimg.org/57qwu5y3x/app2.jpg

有没有办法使用postgresql的时间戳作为googlecharts的日期时间?

目前我的脚本将时间戳字段作为字符串导入,或者根本不起作用。

postgresql db表: CREATE TABLE stats(id SERIAL PRIMARY KEY, spo INTEGER NOT NULL, hr INTEGER NOT NULL, spt timestamp NOT NULL);

示例数据: patient=# SELECT * FROM stats LIMIT 100; id | spo | hr | spt -----+-----+----+---------------------------- 1 | 97 | 80 | 2017-01-01 22:39:48.606672 2 | 96 | 79 | 2017-01-01 22:39:49.60654 3 | 97 | 79 | 2017-01-01 22:39:50.606504 4 | 96 | 79 | 2017-01-01 22:39:51.60639 5 | 96 | 76 | 2017-01-01 22:39:52.606374 6 | 96 | 74 | 2017-01-01 22:39:53.606271 7 | 96 | 72 | 2017-01-01 22:39:54.606251 8 | 96 | 71 | 2017-01-01 22:39:55.606124 9 | 97 | 70 | 2017-01-01 22:39:56.606061 10 | 97 | 69 | 2017-01-01 22:39:57.606012

这是我当前的网页:

<?php
    $con = pg_connect("host=127.0.0.1 port=5432 dbname=patient user=spo password=secretpass") or die("db connection failed!");
    $sth = pg_query($con, "select spt,spo,hr from stats");
    $table = array();
    $table[] = ["spt", "spo", "hr"];
    while($r = pg_fetch_assoc($sth)) {
        $table[] = [(string) $r['spt'], (int) $r['spo'], (int) $r['hr']];
    }
    $jsonTable = json_encode($table);
    echo $jsonTable;
?>
<html>
    <head>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
        google.charts.load('current', {'packages':['corechart']});
        google.charts.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = new google.visualization.arrayToDataTable(<?=$jsonTable?>);
            var options = {
                title: 'spO2/hr monitor',
                legend: { position: 'bottom' }
            };
            var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
            chart.draw(data, options);
        }
        </script>
    </head>
    <body>
        <div id="curve_chart" style="width: 1200px; height: 720px"></div>
    </body>
</html>

<?php $con = pg_connect("host=127.0.0.1 port=5432 dbname=patient user=spo password=secretpass") or die("db connection failed!"); $sth = pg_query($con, "select spt,spo,hr from stats"); $table = array(); $table[] = ["spt", "spo", "hr"]; while($r = pg_fetch_assoc($sth)) { $table[] = [(string) $r['spt'], (int) $r['spo'], (int) $r['hr']]; } $jsonTable = json_encode($table); echo $jsonTable; ?> <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart); function drawChart() { var data = new google.visualization.arrayToDataTable(<?=$jsonTable?>); var options = { title: 'spO2/hr monitor', legend: { position: 'bottom' } }; var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); chart.draw(data, options); } </script> </head> <body> <div id="curve_chart" style="width: 1200px; height: 720px"></div> </body> </html>

1 个答案:

答案 0 :(得分:0)

如果此答案可以帮助您解决您的工作问题,请点击此帖子,我对此网站不熟悉。

让它运转起来:https://s28.postimg.org/6hwya5925/done.jpg

我最终在数据库中使用了unix时间戳,并将其用作主键,我需要修改时间戳的任何地方,我在必要时使用字符串操作。

新的postgresql数据库详细信息:

CREATE TABLE stats(utc INTEGER PRIMARY KEY, spo INTEGER NOT NULL, hr INTEGER NOT NULL);

patient=# SELECT * FROM stats LIMIT 10;
    utc     | spo | hr
------------+-----+----
 1464499543 |  89 | 63
 1464499544 |  89 | 64
 1464499545 |  89 | 65
 1464499546 |  89 | 65
 1464499547 |  89 | 66
 1464499548 |  89 | 65
 1464499549 |  89 | 65
 1464499550 |  89 | 65
 1464499551 |  89 | 65
 1464499552 |  89 | 65

CREATE TABLE stats(utc INTEGER PRIMARY KEY, spo INTEGER NOT NULL, hr INTEGER NOT NULL); patient=# SELECT * FROM stats LIMIT 10; utc | spo | hr ------------+-----+---- 1464499543 | 89 | 63 1464499544 | 89 | 64 1464499545 | 89 | 65 1464499546 | 89 | 65 1464499547 | 89 | 66 1464499548 | 89 | 65 1464499549 | 89 | 65 1464499550 | 89 | 65 1464499551 | 89 | 65 1464499552 | 89 | 65

的index.php:

<html>
    <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript" src="jquery.js"></script>
    <link rel="stylesheet" href="jquery-ui.min.css">
    <link rel="stylesheet" href="jquery-ui.structure.min.css">
    <link rel="stylesheet" href="jquery-ui.theme.min.css">
    <script type="text/javascript" src="jquery-ui.min.js"></script>
    <script type="text/javascript">
        google.charts.load('current', {'packages':['corechart']});
        $(function() {
            $("#datepicker").datepicker();
            $("#datepicker").datepicker("option", "dateFormat", "yy-mm-dd").datepicker("setDate", "-1");
            $("#datepicker").datepicker().on("change", function (e) {
                $.ajax({
                    url: 'getChart.php',
                    type: 'GET',
                    data: {dtpickerdate: $(this).val()},
                    contentType: 'application/json; charset=utf-8',
                    success: function (result) {
                        $("#chart").html(result);
                    }
                });
            });
        });
        $(document).ready(function() {
            $.ajax({
                url: 'getChart.php',
                type: 'GET',
                data: {dtpickerdate: $("#datepicker").val()},
                contentType: 'application/json; charset=utf-8',
                success: function (result) {
                    $("#chart").html(result);
                }
            });
        });
    </script>
    </head>
    <body>
        Date: <input type="text" id="datepicker"><br>
        <div id="chart" style="width: 100%; height: 97%"></div>
    </body>
</html>

getChart.php: <html> <head> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript" src="jquery.js"></script> <link rel="stylesheet" href="jquery-ui.min.css"> <link rel="stylesheet" href="jquery-ui.structure.min.css"> <link rel="stylesheet" href="jquery-ui.theme.min.css"> <script type="text/javascript" src="jquery-ui.min.js"></script> <script type="text/javascript"> google.charts.load('current', {'packages':['corechart']}); $(function() { $("#datepicker").datepicker(); $("#datepicker").datepicker("option", "dateFormat", "yy-mm-dd").datepicker("setDate", "-1"); $("#datepicker").datepicker().on("change", function (e) { $.ajax({ url: 'getChart.php', type: 'GET', data: {dtpickerdate: $(this).val()}, contentType: 'application/json; charset=utf-8', success: function (result) { $("#chart").html(result); } }); }); }); $(document).ready(function() { $.ajax({ url: 'getChart.php', type: 'GET', data: {dtpickerdate: $("#datepicker").val()}, contentType: 'application/json; charset=utf-8', success: function (result) { $("#chart").html(result); } }); }); </script> </head> <body> Date: <input type="text" id="datepicker"><br> <div id="chart" style="width: 100%; height: 97%"></div> </body> </html>