传感器设备(温度传感器)每 10 秒向数据库发送一次数据。我必须绘制一天的时间和温度之间的图表(最新数据)。我设法从数据库中获取数据。问题是因为我有大量的时间数据以及如何使用它来标记 Y 轴。日期和时间格式为2019-04-09 12:28:36。但我可以提取时间。但仍然如何使用一个(Y)轴上的时间。我的代码显示错误 Uncaught SyntaxError: Unexpected token :
//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,"," );
Run Code Online (Sandbox Code Playgroud)
//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
});
Run Code Online (Sandbox Code Playgroud)
标签必须是字符串。$timeIntervals就这样建立起来...
$timeIntervals = $timeIntervals . '"' . $timeInterval . '",';
Run Code Online (Sandbox Code Playgroud)
...为了将日期时间值括在双引号之间。
| 归档时间: |
|
| 查看次数: |
364 次 |
| 最近记录: |