我正忙于生成实时数据,为此我正在使用 CanvasJS 的服务,因为它们有非常棒的图表。
我有我的 JSON 数据获取代码,我有我的 html 代码,图表将在其中呈现。但它不会显示图表..所以我在我的网络浏览器中打开了 Inspector 以查看问题出在哪里或问题出在哪里,这就是我得到一个错误:
[错误] ReferenceError:找不到变量:$ 全局代码(myHeart_Chart.html,第 7 行)
我现在拥有的代码:
myHeart_Chart.html
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
$.getJSON("data.php", function (result) {
var dataPoints = [];
for (var i = 0; i <= result.length - 1; i++) {
dataPoints.push({ x: Number(result[i].x), y: Number(result[i].y) });
}
var chart = new CanvasJS.Chart("chartContainer", {
data: [
{
dataPoints: dataPoints
}
]
});
chart.render();
});
});
</script>
<script type="text/javascript" src="canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width:100%;">
</div>
</body>
</html>
PHP 返回 JSON 数据:
数据.php
<?php
//header('Content-Type: application/json');
$con = mysqli_connect("localhost","root","","WebApplication");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to DataBase: " . mysqli_connect_error();
}
else
{
$data_points = array();
$result = mysqli_query($con, "SELECT * FROM info");
while($row = mysqli_fetch_array($result))
{
$point = array("x" => $row['id'] , "y" => $row['acceleration']);
array_push($data_points, $point);
}
$json = json_encode($data_points, 32); //define('JSON_NUMERIC_CHECK',32); // Since PHP 5.3.3
$json = str_replace("\"", "", $json); //replace all the "" with nothing
echo $json;
}
mysqli_close($con);
?>
php首先生成了这个:
[{"x":"1","y":"5"},{"x":"2","y":"5"},{"x":"3","y": "4"},{"x":"4","y":"1"}, {"x":"5","y":"8"},{"x":"6", "y":"9"},{"x":"7","y":"5"},{"x":"8","y":"6"}, {"x": "9","y":"4"},{"x":"10","y":"7"},{"x":"14","y":"7"},{ "x":"15","y":"7"}]
然后我添加了这一行:
$json = json_encode($data_points, 32); //define('JSON_NUMERIC_CHECK',32); // Since PHP 5.3.3
$json = str_replace("\"", "", $json); //replace all the "" with nothing
echo $json;
这样输出将是数字:
[{x:1,y:5},{x:2,y:5},{x:3,y:4},{x:4,y:1},{x:5,y:8} ,{x:6,y:9},{x:7,y:5},{x:8,y:6}, {x:9,y:4},{x:10,y:7} ,{x:14,y:7},{x:15,y:7}]
此外:基本上第 7 行是它说的部分:
$(文件)
** 我已经忙于这个 canvasjs 大约 3 周了。我想我不知道用什么替换文档。**