您好,我正在构建一个收集数据的系统。我将数据存储在 mysql 数据库中并使用 amCharts 显示它。我制作了显示数据随时间变化的存档图表。我在使用 php 和 josn 在 mysql 时间戳数组和 javascript 日期数组之间进行转换时遇到问题。
mysql查询是这样的:
SELECT
UNIX_TIMESTAMP( data ) AS data,ROUND(AVG(CurrentA),3),ROUND(AVG(CurrentB),3),ROUND(AVG(CurrentC),3)
FROM
Danni
WHERE
data
BETWEEN
'2013-07-13 00:00:00'
AND
'2013-07-16 00:00:00'
GROUP BY
FLOOR(UNIX_TIMESTAMP(data)/72)
LIMIT 40
返回的数据由以下 php 代码格式化:
$data = array();
while ($row = mysql_fetch_array($ustroistvo)){
$data['date'][] = $row['0'];
$data['CurrentA'][] = $row['1'];
$data['CurrentB'][] = $row['2'];
$data['CurrentC'][] = $row['3'];
}
echo json_encode($data);
exit();
所以最终的结果是
{"date":["1373748170","1373748192","1373748264","1373748336","1373748408","1373748480","1373748553","1373748624","1373748696","1373796506","1373796577","1373796648","1373796720","1373796792","1373796864","1373796936","1373797008"],"CurrentA":["45.667","37.794","37.508","35.815","31.238","33.061","32.937","35.385","36.020","31.852","34.047","34.508","30.672","37.292","32.554","39.262","32.314"],"CurrentB":["39.000","36.921","33.769","35.123","36.492","41.576","42.492","34.862","36.041","34.967","37.062","33.108","35.531","34.262","33.385","35.877","35.941"],"CurrentC":["35.000","32.429","35.862","36.785","34.873","36.894","31.921","36.938","33.714","40.541","28.688","34.308","33.266","39.846","35.708","34.908","32.118"]}
因此,要将日期对象转换为我使用的实际 javascript 日期:
success: function (data) {
console.log(data.date);
data.date=new Date(data.date*1000);
console.log(data.date);
chart.dataProvider=data;
chart.validateData();
}
问题出在这里data.date=new Date(data.date*1000)
给了我无效的日期,我认为这是因为data.date
是数组。我不知道如何正确格式化它以便data.date=new Date(data.date*1000)
返回日期数组。