What I am trying to do is that I have created a php file to create a dynamic array of no's which changes on every page load.
the php code that I created is :
header("Content-type: text/json");
$random = [] ;
for($i=0 ;$i<10;$i++)
{
$random[] = rand(0,10) ;
}
echo json_encode($random);
Now I am fetching that array of 10 no's in the Highcharts.
The chart is displaying points but not in an expected manner it shows very weird line plotting.
The Code that I used for Highchart's data Plotting is Given below.
var chart;
function requestData() {
$.ajax({
url: 'data.php',
success: function(data) {
var series = chart.series[0],
shift = series.data.length > 20; // shift if the series is
y = data;
console.log(y);
chart.series[0].addPoint(y, true, shift);
setTimeout(requestData, 1000);
},
cache: false
});
}
document.addEventListener('DOMContentLoaded', function() {
chart = Highcharts.chart('container', {
chart: {
type: 'line',
events: {
load: requestData
}
},
title: {
text: 'Live random data'
},
xAxis: {
crosshair: false
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: [{
name: 'Random data',
data: []
}]
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container"></div>
I don't understand where I am making mistake. the console shows no errors and also shows the array values but the chart doesn't render as expected.
I need to show data something like following(just an example of how the line should plot).
console.log(y)inrequestDatafunction?