如何使用“时间”(数据库中的数据,数据类型:timestamp)在Chart JS中绘制图形

时间:2019-04-09 13:49:48

标签: php html chart.js

传感器设备(温度传感器)每10秒将数据发送到数据库。我必须在一天的时间和温度之间绘制一个图(最新数据)。我设法从数据库中获取数据。问题是因为我有大量的时间数据以及如何使用它来标记Y轴。日期和时间的格式为2019-04-09 12:28:36。但是我可以提取时间。但是仍然如何在一个(Y)轴上使用时间。我的代码显示错误Uncaught SyntaxError:意外令牌:

// php

$sql = "SELECT TIME(Date_Time) as TIME , Temperature FROM dataTable WHERE Date_Time>= (CURDATE() - INTERVAL 1 DAY )  ORDER BY Date_Time DESC ";

$result = $conn->query($sql);
  if ($result->num_rows > 0) {

   while($row = $result->fetch_assoc()) {
     $tempValue =$row['Temperature'];
     $timeInterval=$row['TIME'];

     $tempValues =$tempValues. $tempValue. ',';
     $timeIntervals =$timeIntervals. $timeInterval. ',';
    }
$tempValues =trim ($tempValues, ",");
$timeIntervals= trim ($timeIntervals,"," );

// Javascript

var myChart = document.getElementById('myChart').getContext('2d');




        var data = {
            datasets: [{

            data: [<?php  echo $tempValues ?>],
            backgroundColor: 'transparent',
            borderColor:'rgba(255,99,132)',
            borderWidth:5,
            label: 'Temperature in degree'
          }],

//error in the following line !
          labels: [<?php  echo $timeIntervals ?>]
        };

        var particleChart =new Chart(myChart, {
          type:'line',
          data: data
        });

1 个答案:

答案 0 :(得分:3)

标签必须是字符串。这样建立$timeIntervals ...

$timeIntervals = $timeIntervals . '"' . $timeInterval . '",';

...为了使日期时间值包含在双引号之间。