我想使用 ajax 或 json 将数据点添加到我的折线图中,现在我必须重新加载整个网页才能在图表上显示我的新数据。但我想通过添加如下链接的点来显示实时数据:
jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/dynamic-update/ http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/dynamic-update/
www.highcharts.com/studies/live-server.htm http://www.highcharts.com/studies/live-server.htm
我试图从 mysql 检索数据并通过 json 添加到图表上,但什么也没发生。这是我在 live-server-data.php 中的代码:
<?php
header("Content-type: text/json");
include_once 'include/connection.php';
$db = new DB_Class();
$query = "select distinct idchip from datatable ";
$result = mysql_query( $query );
$rows = array();
$count = 0;
while( $row = mysql_fetch_array( $result ) ) {
$SQL1 = "SELECT datetime,temperature FROM `datatable` WHERE idchip=".$row['0']." datetime DESC limit 0,1 ";
$result1 = mysql_query($SQL1);
while ($rows = mysql_fetch_array($result1)) {
$data[] = $rows['1'];
$datatime[] = 'moment('.$rows['0'].').valueOf()';
}
// The x value is the current JavaScript time, which is the Unix time multiplied
// by 1000.
$x = $datatime;
// The y value is a random number
$y = $data;
}
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
这就是我用来获取数据并在 index.php 页面的图表上显示的方法。
var chart; // global
/**
* Request data from the server, add it to the graph and set a timeout to request again
*/
function requestData() {
$.ajax({
url: 'live-server-data.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(eval(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: 150,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: [{
name: 'Random data',
data: []
}]
});
});
这是我的图表,我现在重新加载页面以获取新数据,但我想向图表添加新点以“实时”