【发布时间】:2019-09-03 07:11:02
【问题描述】:
我使用以下 Highchart 的演示创建了一个 Highchart:
https://www.highcharts.com/demo/dynamic-update
现在我做了什么我创建了自己的函数来向图表添加动态值。
我创建了一个函数来从特定的 php 文件中获取动态数据,该文件的数据在每次页面加载事件时都会发生变化。
我在getData 函数console.log 中获取数据值
这是我正在使用的脚本。
<script type="text/javascript">
$(document).ready(function(){
function getData(){
$.ajax({
type: 'GET',
url: 'data.php',
success: function(data){
// var number = document.write(data) ;
console.log(data);
return data ;
}
}) ;
}
Highcharts.chart('chart', {
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = getData();
console.log(y);
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
time: {
useUTC: false
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
headerFormat: '<b>{series.name}</b><br/>',
pointFormat: '{point.x:%Y-%m-%d %H:%M:%S}<br/>{point.y:.2f}'
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: getData()
});
}
return data;
}())
}]
});
});
</script>
现在您可以看到,我创建了一个 getData 函数并获取数据值作为回报。
在 getData 函数下的控制台日志中,我每隔一秒就会得到一个整数值作为回报。
问题是在 Highchart 的函数下,我无法使用 getData 函数获取数据值,它在控制台中返回 undefined。
Highchart 正在运行,但未显示任何数据点。它正在移动,但没有显示任何数据点。
请在我做错的地方纠正我。
感谢任何帮助。谢谢
【问题讨论】:
标签: javascript function highcharts