我正在开发一个界面,该界面显示了Google数据线图表从我的数据库中检索到的数据。但是,新数据每10秒存储在我的数据库中,我无法自动刷新图表。
我需要一些非常基本的东西,而且我已经在互联网上看过了。我读了一些关于Javascript / AJAX / JQuery的内容......但我对硬件感觉更舒服:D
这是我的档案 编辑:Chart_get和主文件已根据@Michel答案进行了修改。
fetch.php - 获取数据并回显
<?php // Connection and Request stuff
$host = blablabla
(...)
$req = $bdd->query('SELECT id, battery FROM Station');
while ($data = $req->fetch()){
$id = addslashes($data['id']);
$charge_batt = intval($data['charge_batt']);
$result .= "['".$id."' , ".$charge_batt."],";
}
$result = substr($result, 0, -1); // Erase the last ","
echo $result;
?>
输出:
['1' , 90],['2' , 89],['3' , 80],['4' , 100],['5' , 90],['6' , 50],['7' , 67]
chart_get.php - 初始化图表并使用&#34; echo $ result&#34;数据
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
<?php
echo ("['Date', 'Battery'],");
include('fetch.php');
?>
]);
var options = {
title: 'Battery health',
animation:{
duration: 1000,
easing: 'out',
},
curveType: 'function'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
刷新&#34; chart_div&#34;我试过了:
main.html - 带有加载功能的jQuery脚本
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>Project - Chart</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// first load it once, so it display's the chart
$('#tableHolder').load('chart_get.php');
// then execute the interval
setInterval(function(){$('#tableHolder').load('chart_get.php');}, 5000);
});
</script>
</head>
<body>
<p>Hello</p>
<div id="tableHolder"> </div>
</body>
</html>
但图表根本没有显示。 我完全不知道我做错了什么。我正在快速阅读一些关于javascript的教程,但如果你知道如何解决我的问题,那将很高兴帮助我:) 谢谢!
答案 0 :(得分:1)
解决方案。
我找到了答案!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<title>Projet GreenFeed - Station de recharge a energie positive</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
//AJAX Call is compulsory !
var jsonData = $.ajax({
url: "chart_fetch.php",
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
var options = {
title: 'Battery',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// First load the chart once
drawChart();
// Set interval to call the drawChart again
setInterval(drawChart, 5000);
});
</script>
</head>
<body>
<div id="chart_div"> </div>
</body>
</html>
答案 1 :(得分:0)
首先删除chart_get.php中的<!DOCTYPE>
,<html>
,<head>
,<meta>
,<title>
标记。
chart_get.php
中的数据与<div id="tableHolder"></div>
中的数据相同,如果您不想刷新它。
其次,将main.html
中的脚本重写为:
<script type="text/javascript">
$(document).ready(function(){
// first load it once, so it display's the chart
// (otherwise you have to wait 5 seconds)
$('#tableHolder').load('chart_get.php');
// then execute the interval
setInterval(function(){$('#tableHolder').load('chart_get.php');}, 5000);
});
</script>