gpt4 book ai didi

javascript - Google 折线图 - 自动刷新数据库

转载 作者:行者123 更新时间:2023-11-28 01:15:19 26 4
gpt4 key购买 nike

我正在开发一个界面,通过 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 - 初始化图表并使用“echo $result”数据绘制它

    <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>

要刷新我尝试过的“chart_div”:

ma​​in.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 的教程,但如果您知道如何解决我的问题,那么对我的帮助将非常有用:)谢谢!

最佳答案

OP 的解决方案。

我找到了答案!

<!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>

关于javascript - Google 折线图 - 自动刷新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23905812/

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