gpt4 book ai didi

php - How to use 'time'(来自数据库的数据,数据类型: timestamp ) for plotting graph in Chart JS

转载 作者:可可西里 更新时间:2023-10-31 23:00:39 25 4
gpt4 key购买 nike

传感器设备(温度传感器)每 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,"," );

//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
});

最佳答案

标签必须是字符串。以这种方式构建 $timeIntervals...

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

...为了将日期时间值括在双引号之间。

关于php - How to use 'time'(来自数据库的数据,数据类型: timestamp ) for plotting graph in Chart JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55594467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com