Javascript 如何在highcharts中仅显示最新的民意测验数据
我正在尝试用highcharts创建CPU规格图。我有一个php脚本,我想每5秒调用一次,以获得输出并在仪表上显示数据: ajax功能如下所示:Javascript 如何在highcharts中仅显示最新的民意测验数据,javascript,highcharts,Javascript,Highcharts,我正在尝试用highcharts创建CPU规格图。我有一个php脚本,我想每5秒调用一次,以获得输出并在仪表上显示数据: ajax功能如下所示: function request_cpu_Data() { $.ajax({ url: 'get_cpu.php', success: function(point) { var myObj = JSON.parse(point);
function request_cpu_Data() {
$.ajax({
url: 'get_cpu.php',
success: function(point) {
var myObj = JSON.parse(point);
//This is the data part
var myDate=myObj[0]
//This is the CPU value
var myData=myObj[1];
cpu_chart.addSeries({ data: myData });
setTimeout(request_cpu_Data, 300000);
},
cache: false
});
}
get_cpu.php脚本的输出如下所示:
[1371048443000,46]
这是创建仪表高度表的函数:
$(document).ready(function() {
cpu_chart = new Highcharts.Chart({
chart: {
type: 'gauge',
plotBackgroundColor: null,
plotBackgroundImage: null,
plotBorderWidth: 0,
renderTo: 'container',
plotShadow: false,
events: {
load: request_cpu_Data
}
},
title: {
text: 'Profile DB'
},
credits: {
enabled: false
},
pane: {
startAngle: -150,
endAngle: 150,
background: [{
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#FFF'],
[1, '#333']
]
},
borderWidth: 0,
outerRadius: '109%'
}, {
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, '#333'],
[1, '#FFF']
]
},
borderWidth: 1,
outerRadius: '107%'
}, {
// default background
}, {
backgroundColor: '#DDD',
borderWidth: 0,
outerRadius: '105%',
innerRadius: '103%'
}]
},
// the value axis
yAxis: {
min: 0,
max: 100,
minorTickInterval: 'auto',
minorTickWidth: 1,
minorTickLength: 10,
minorTickPosition: 'inside',
minorTickColor: '#666',
tickPixelInterval: 30,
tickWidth: 2,
tickPosition: 'inside',
tickLength: 10,
tickColor: '#666',
labels: {
step: 2,
rotation: 'auto'
},
title: {
text: 'CPU'
},
plotBands: [{
from: 0,
to: 65,
color: '#55BF3B' // green
}, {
from: 65,
to: 75,
color: '#DDDF0D' // yellow
}, {
from: 75,
to: 100,
color: '#DF5353' // red
}]
},
series: [{
name: 'CPU',
data: [],
tooltip: {
valueSuffix: '%'
}
}]
});
});
当我这样做时,仪表图没有显示任何数据。有人能告诉我我在这里遗漏了什么吗?我也喜欢在gauage中显示myDate。类似的功能应该可以:
Chart.addSeries({ data: fromGetCpu[1] });
如果愿意,还有一个名称参数:
Chart.addSeries({ name: 'yourName', data: fromGetCpu[1] });
我解决了这个问题。这是我修改过的功能:
function request_cpu_Data() {
$.ajax({
url: 'get_cpu.php',
success: function(data) {
var myObj = JSON.parse(data);
var point = cpu_chart.series[0].points[0];
var newVal=myObj[1];
//alert(newVal);
point.update(newVal);
setTimeout(request_cpu_Data, 10000000);
},
cache: false
});
}
我还要将数据初始化为0:
series: [{
name: 'CPU',
data: [0],
},你已经设置好highcharts了吗?是的,我需要去掉46并将其分配给series。任何人,我都搞不懂。你正在使用fromGetCpu,也就是fromGetCpu?fromGetCpu是一个虚构的数组名称,它应该代表你的值,46如果你使用console.log(point),你会得到什么在您的成功处理程序中,我做了这个var myObj=JSON.parse(point);cpu_chart.addSeries({data:myObj[1]});不起作用