php - Google Charts & MySQL Timestamp 处理问题

标签 php mysql google-visualization

我有一个 MySQL 数据库的日志记录设备,我想从中读取数百行,并用它创建一个 Google 图表。

我似乎找不到将 MySQL 时间戳数据转换为 Google 图表数据数组的时间列类型的好方法。

https://developers.google.com/chart/interactive/docs/datesandtimes

我开发了一个可以回显我想要的数据的 PHP,但是我受困于 MySQL 或 Unix 时间戳格式。 PHP 文件:

<?php 
            $mysqli = new mysqli("localhost", "readuser", "***", "***");
            /* check connection */
            if ($mysqli->connect_errno) {
                printf("Connect failed: %s\n", $mysqli->connect_error);
                exit();
            }
            /* Select queries return a resultset */
            if ($result = $mysqli->query("SELECT ID, Timestamp, temp FROM temperature ORDER BY ID DESC LIMIT 5")) {
                while($row = mysqli_fetch_assoc($result))
                {
                $dataset1[] = array($row['Timestamp'],$row['temp']);
                }

            /* encode array to JSON for JavaScript */
            echo json_encode($dataset1); 


            /* free result set */
            $result->close();
            }

        ?> 

上面php执行的例子:

[
["2018-08-03 17:01:33","80.60"],
["2018-08-03 17:01:23","81.05"],
["2018-08-03 17:01:13","80.60"],
["2018-08-03 17:01:03","81.05"],
["2018-08-03 17:00:53","81.05"]
]

我认为 Google 图表希望看到的更像是:

[
[[17, 01, 33],80.60],
[[17, 01, 23],81.05],
[[17, 01, 13],80.60],
[[17, 01, 03],81.50],
[[17, 00, 50],81.05]
]

这是数据将进入的主要 PHP 文件(ajax 访问 json 数据 php 文件,并在 setInterval 更新图表绘制):

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript"> 
      google.charts.load('current', {'packages':['line']});
      google.charts.setOnLoadCallback(drawChart);

      /* declare variable for json data */
    var chardata;
    /* ajax to pull mysql data via php file */
    function chartdataquery() {
        jQuery.ajax({
            url:'/mysql/chartdata.php',
            type:'POST',
            async: false,
            dataType: 'json',
            success:function (results) {
                chartdata = (results);
                } 
            });
         };
    /* Set the chartdata update frequency */
    updatequeryInterval = setInterval(chartdataquery,10000);

/* drawing the chart at the setInterval duration, at the end of the function */
    function drawChart() {

      var data = new google.visualization.DataTable();
      data.addColumn('timeofday', 'Time');
      data.addColumn('number', 'Temperature');

      data.addRows(chartdata);

      var options = {
        chart: {
          title: 'Temperature',
          subtitle: 'updated every 10 seconds'
        },
        width: 900,
        height: 500
      };

      var chart = new google.charts.Line(document.getElementById('linechart_material'));


      chart.draw(data, google.charts.Line.convertOptions(options));
    }

    </script>
  </head>
  <body>
    <div id="linechart_material" style="width: 900px; height: 500px"></div>
  </body>
</html>

这是正确的吗?我可以使用 MySQL 查询、PHP 或 Javascript 来完成吗?使数组 Google 图表符合时间的最佳途径是什么?

谢谢!

最佳答案

尝试像这样加载 php 数组...

while($row = mysqli_fetch_assoc($result))
{
  $dataset1[] = array(array((int) date_format(new DateTime($row['Timestamp']), 'H'), (int) date_format(new DateTime($row['Timestamp']), 'i'), (int) date_format(new DateTime($row['Timestamp']), 's')),$row['temp']);
}

要使上述工作正常,您需要将 google 列类型更改为...

data.addColumn('timeofday', 'Time');

关于php - Google Charts & MySQL Timestamp 处理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51680769/

相关文章:

mysql - 连接表以使用来自不同表的数据创建字段

javascript - 使用 JSON blob 的谷歌折线图可视化

jquery - Google 可视化 - 自定义工具提示点

javascript - 谷歌图表中特定数据的浏览器崩溃

javascript - 在不断重新加载的页面上保持选定的切换/选项卡打开

PHP-FPM 在执行繁重工作时用户过多时崩溃

php - 当我使用 jquery ajax 调用 php 文件时,wordpress 返回 404

java - 如何将 SQL 数据映射到 Java JSON 对象和 JSON 数组?

php - 使用ajax和json处理多个数据库行

php - 当左连接列不存在时如何选择总和