我有一个小网站,显示谷歌折线图。当显示的值是浮点数时,一切正常工作,谷歌图表将其转换为整数,尽管使用console.log我可以看到该值实际上是浮点数。
37,00实际上应该是37,50,因为它打印在控制台上。
代码:
function VOLTAGE_chart() {
var tempo = document.getElementById("janela").value;
var mysqlData = $.ajax({
url: "php/voltage_chart.php",
dataType: "JSON",
async: false,
data:{},
success: function(x){
return x;
}
}).responseJSON;
if (mysqlData == null){
alert("Não há dados a apresentar!");
return;
}
var phpDate = mysqlData[0]["time"].split(/[^0-9]/);
var date = new Date (phpDate[0],phpDate[1]-1,phpDate[2],phpDate[3],phpDate[4],phpDate[5] );
var data = google.visualization.arrayToDataTable([
['Time', 'Voltage'], //{label: 'Time', type: 'date'}, {label: 'Voltage', type: 'number'}
[date, parseFloat(mysqlData[0]["Voltage"])]
]);
for (i = 1; i < Object.keys(mysqlData).length; i++) {
phpDate = mysqlData[i]["time"].split(/[^0-9]/);
date = new Date (phpDate[0],phpDate[1]-1,phpDate[2],phpDate[3],phpDate[4],phpDate[5] );
data.addRows([[date,parseInt(mysqlData[i]["Voltage"])]]);
}
var formatter = new google.visualization.NumberFormat({
fractionDigits: 2,
decimalSymbol: ','
});
formatter.format(data, 1);
console.log(mysqlData[3]["Voltage"]);
var startDate = new Date( date );
startDate.setMinutes(date.getMinutes() - tempo);
var options = {
title: 'Voltage',
legend: { position: 'none' },
vAxes:
{0: {title: 'Voltage (V)', viewWindow:{min:34, max:45}, gridlines: {count: 12}, ticks: [34,35,36,37,38,39,40,41,42,43,44,45] } },
hAxes: {0: {title: 'Time', format:'H:m:s', viewWindow: {min: startDate, max: date} } },
pointSize: 5
};
var chart = new google.visualization.LineChart(document.getElementById('VOLTAGE_chart'));
chart.draw(data, options);
}
使用php和Ajax从数据库中推送要显示的值,以读取javascript文件中的值。
答案 0 :(得分:1)
检查以下内容......
for (i = 1; i < Object.keys(mysqlData).length; i++) {
phpDate = mysqlData[i]["time"].split(/[^0-9]/);
date = new Date (phpDate[0],phpDate[1]-1,phpDate[2],phpDate[3],phpDate[4],phpDate[5] );
data.addRows([[date,parseInt(mysqlData[i]["Voltage"])]]); //<-- parseInt?
}
改为......
for (i = 1; i < Object.keys(mysqlData).length; i++) {
phpDate = mysqlData[i]["time"].split(/[^0-9]/);
date = new Date (phpDate[0],phpDate[1]-1,phpDate[2],phpDate[3],phpDate[4],phpDate[5] );
data.addRows([[date,parseFloat(mysqlData[i]["Voltage"])]]); //<-- use parseFloat
}