I am trying to load data from the server-side and displaying them in Highcharts one by one.
But before displaying them on the Highchart, I am doing some processing on the data and then rendering it into the Highcharts.
I am using AJAX so that I can get data every single time without refreshing the page and reloading the chart.
The chart is working good for the first set of data and then it shows undefined and the chart not showing any next set of data.
Here I have random.php file that I use to get the data :
header("Content-type: text/json");
$data = [];
for($i=0; $i<500; $i++)
{
$data[] = rand(0,10);
}
$into_mysql = json_encode($data);
echo $into_mysql;
Now I am fetching the data in the chart in other page:
var chart;
// Prototype to Request Data using `AJAX`
function requestData() {
$.ajax({
url: 'random.php',
async: false,
success: function (data) {
y = data;
console.log(y);
return (y);
}
});
}
// Fetch the data from the `requestData` and process it.
function fetch() {
requestData();
var divisor = 2;
var count = 0;
for (var i = 0, length = y.length; i < length; i++) {
y[i] /= divisor;
}
console.log(y);
return (y);
setInterval(function () { requestData }, 1000);
}
var json_array = fetch();
var i = 0;
function next() {
return json_array[i++];
// i++;
}
Highcharts.chart('container', {
chart: {
type: 'line',
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],
chart = this;
var count = 0;
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = next();
console.log(y);
series.addPoint([x, y], false, true);
chart.redraw(false);
}, 1000 / 130);
}
}
},
time: {
useUTC: false
},
title: {
text: 'ECG Graph Plot From MySQl Data'
},
xAxis: {
type: 'datetime',
labels: {
enabled: false
},
tickPixelInterval: 150
},
yAxis: {
//max: 1.5,
//min: -1.5,
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: [{
animation: false,
name: 'ECG Graph Plot From MySQl Data',
dataGrouping: {
enabled: false
},
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -1000; i <= 0; i += 1) {
data.push([
time + i * 10,
null
]);
}
return data;
}())
}]
});
So what I require is to render the data one by one without breaking it. when the One dataset from the random.php gets over. the next data should start in continuation of that.
Update:
Consider a scenario where an array of 500 data is generated every few second. what I want is to show the array points in one highcharts one by one. but only first set of array data is being displayed and the rest of the array data is not showing.