gpt4 book ai didi

php - 如何使用 php 和 AJAX 绘制实时图表?

转载 作者:可可西里 更新时间:2023-11-01 07:02:10 25 4
gpt4 key购买 nike

我是网络开发的新手。我刚开始用 php 编程。我想开发一个连接到 MySQL 数据库(从服务器)的动态页面,并在图表(可以是散点图、直方图)中实时显示结果。到目前为止,我设法从数据库中获取数据并显示图表。但是,我无法实时做到这一点。

我一直在四处寻找。我发现的是使用 AJAX 进行实时绘图。很好,我做了一些关于它的教程并且能够运行示例。我的挑战是绘制图表。

如果有帮助,这正是我想要做的 http://jsxgraph.uni-bayreuth.de/wiki/index.php/Real-time_graphing

我曾尝试运行此代码,但未能成功。

谁能告诉我如何从简单开始?如果我的问题不够清楚,我会详细说明。提前致谢!

@Tim,这是我尝试做的。

我的 php 是

 <?php


$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
//echo "Database Connected!";
mysql_select_db("DB", $con);
$sql=mysql_query("SELECT Def_ID, Def_BH FROM BBB WHERE Ln_Def < 1200");

$Def_ID= array();
$Def_BH = array();

while($rs = mysql_fetch_array($sql))
{
$Def_ID[] = $rs['Def_ID'];
$Def_BH [] = $rs['Def_BH '];

}

mysql_close($con);

$json = array(
'Def_ID' => $Def_ID,
'Def_BH' => $Def_BH
);

echo json_encode($json);
?>

输出是

{"Df_ID":["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41"],"Df_BH":["1","1","1","5","5","2","1","1","1","1","2","1","1","1","1","1","1","1","1","1","1","1","2","1","1","2","1","3","10","1","2","1","1","1","2","2","2","1","1","1","1","1"]}

然后我的脚本如下

   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Flot Example: Real-time updates</title>
<link href="../examples.css" rel="stylesheet" type="text/css">
<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../../excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="../../jquery.js"></script>
<script language="javascript" type="text/javascript" src="../../jquery.flot.js"></script>
<script language = "javascript" type="text/javascript" src="Include/excanvas.js"></script>
</head>
<body>

<div id="placeholder" style="width:600px;height:300px"></div>


</body>

<script type="text/javascript">
function doRequest(e) {
var url = 'fakesensor.php'; // the PHP file
$.getJSON(url,data,requestCallback); // send request
}




function requestCallback(data, textStatus, xhr) {
// // you can do stuff with "value" here
$.each(data, function(index, value) {
console.log(value.Df_ID);
console.log(value.Df_BH);
});
}

</script>

</html>

我想绘制 Def_Id 与 Def_BH 的对比图。你看到哪里出了问题吗?

最佳答案

关于php - 如何使用 php 和 AJAX 绘制实时图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14459148/

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