[英]Displaying Google Chart from php script over JSON
I'm writing a program that retrieves data from an SQLite database on my server with PHP and should display the data on a Google chart using JavaScript. 我正在编写一个程序,该程序使用PHP从服务器上的SQLite数据库中检索数据,并应使用JavaScript在Google图表上显示数据。
When I run the program I get following errors: 当我运行程序时,出现以下错误:
1) Synchronous XMLHttpRequest on the main thread is deprecated
1)
Synchronous XMLHttpRequest on the main thread is deprecated
2) not an array => var data = google.visualization.arrayToDataTable(obj);
2)
not an array => var data = google.visualization.arrayToDataTable(obj);
I added all the code below and also the output generated by track.php: 我添加了以下所有代码以及track.php生成的输出:
track.php track.php
<?php
$dir = 'sqlite:mijn.db';
$dbh = new PDO($dir) or die("cannot open database");
$query = "SELECT * FROM DS18b20 ORDER BY date DESC , time DESC LIMIT 10";
$data[0] = array('date' , 'time' , 'value');
$index=1;
foreach ($dbh->query($query) as $row) {
$data[$index++] = array($row[0] , $row[1] , $row[2] );
// echo $data[$index-1][0] , " ";
}
echo json_encode($data);
unset($dbh); // Close Handle
?>
example output: 示例输出:
[["date","time","value"],["150122","21:38","20.9"],["150122","21:37","20.9"],["150122","21:37","20.9"],["150122","21:37","20.9"],["150120","22:33","20.6"],["150120","22:33","20.7"],["150120","22:31","20.6"],["150120","22:30","20.7"],["150120","20:00","19.7"],["150120","20:00","19.7"]]
html file : html文件:
<script>
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "track.php", dataType: "json", async: false
}).responseText;
var obj = window.JSON.stringify(jsonData);
var data = google.visualization.arrayToDataTable(obj);
var options = {
title: 'Trackerdata example'
};
var chart = new google.visualization.LineChart(
document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
Don't use a synchronous request. 不要使用同步请求。 You can put the rest of the chart-building code in a done() call.
您可以将其余的图表构建代码放入done()调用中。
google.load("visualization", "1", { packages: ["corechart"] }); google.setOnLoadCallback(drawChart); function drawChart() { $.ajax({ url: "track.php", dataType: "json" }).done(function(jsonData) { var obj = window.JSON.stringify(jsonData); var data = google.visualization.arrayToDataTable(obj); var options = { title: 'Trackerdata example' }; var chart = new google.visualization.LineChart( document.getElementById('chart_div')); chart.draw(data, options); } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.