来自数据库的动态样条高图
我尝试制作样条高图并实施How to load data from JSON to Highchart?的解决方案,这是来自Mina Gabriel的答案。代码看起来像这样。来自数据库的动态样条高图
在test.php
}
// Set the JSON header
header("Content-type: text/json");
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000.
$x = time() * 1000;
$y = rand(0,100) ;
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
而在highchart脚本:
<script>
/**
* Request data from the server, add it to the graph and set a timeout to request again
*/
var chart; // global
function requestData() {
$.ajax({
url: 'http://localhost:8080/test.php',
success: function(point) {
var series = chart.series[0],
shift = series.data.length > 20; // shift if the series is longer than 20
// add the point
chart.series[0].addPoint(point, true, shift);
// call it again after one second
setTimeout(requestData, 1000);
},
cache: false
});
}
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
events: {
load: requestData
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 100,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: [{
name: 'Random data',
data: []
}]
});
});
</script>
< /head>
<body>
而那些刚刚工作。但是,当我试图改变在test.php
的代码来设置y值从数据库这样一个数字:
<?php
header("Content-type: text/json");
$db = mysql_connect("localhost","myusername","mypassword");
mysql_select_db("mydatabase");
$day=date('Y-m-d'); //UTC standar time
$result = mysql_query("SELECT COUNT(*) FROM table WHERE time='{$day}';");
$count = mysql_fetch_array($result);
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000.
$x = time() * 1000;
$y = $count[0];
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
线图不起作用。我检查了SQL代码,它的工作原理正确。我错过了什么?
从给定的信息和this post,我最好的选择是$ count [0]是一个字符串,highcharts需要它是严格的数字。你可以尝试以下对我来说
$y = intval($count[0]); // OR floatval($count[0]);
是的,你是对的。 。感谢:) – user1397595 2012-08-09 15:56:42
很高兴。我想你现在可以标记答案 – 2012-08-09 16:40:13
老实说,我不知道该怎么做? :D怎么样? – user1397595 2012-08-09 16:51:03
你可以请求ajax调用后返回的JSON后? – 2012-08-04 05:27:43
是的,这里需要更多信息。你可以在ajax成功回调中添加一个断点“var series = chart.series [0]”,并分享你有的点的确切值 – 2012-08-04 05:47:35
如果有任何答案有帮助,请相应地投票/标记 – 2012-08-06 21:31:05