Basically, I am making a graph which updates every minute or so. I want to keep my object with values only containing a set number of properties (60 for an hour, 120 for 2 hours etc). Everything is working using canvas.js, but if I try to delete the first item from the object, once everything re-initializes, the object from key [0] is now missing, which breaks everything. Trying to figure out the best way to delete and object from inside my object and then get everything keyed correctly starting at 0,1,2,3 etc.
Some code...
This is a snippet of the object...
dataPoints = {
0:{x: Fri Apr 20 2018 14:14:00 GMT-0500 (Central Daylight Time), y: 150.64}
1:{x: Fri Apr 20 2018 14:15:00 GMT-0500 (Central Daylight Time), y: 150.68}
2:{x: Fri Apr 20 2018 14:16:00 GMT-0500 (Central Daylight Time), y: 150.7}
3:{x: Fri Apr 20 2018 14:17:00 GMT-0500 (Central Daylight Time), y: 150.71}
4:{x: Fri Apr 20 2018 14:18:00 GMT-0500 (Central Daylight Time), y: 150.75}
5:{x: Fri Apr 20 2018 14:19:00 GMT-0500 (Central Daylight Time), y: 150.91}
}
dataPoints.push({x: getTime, y: data.Data[1].close}); // pushing new datapoint...
if (dataPoints.length > 60) {
delete dataPoints[0]; // remove oldest datapoint
}
Everything is working as intended, but once a new point is added and an old point is deleted, it comes out like this:
dataPoints = {
1:{x: Fri Apr 20 2018 14:15:00 GMT-0500 (Central Daylight Time), y: 150.68}
2:{x: Fri Apr 20 2018 14:16:00 GMT-0500 (Central Daylight Time), y: 150.7}
3:{x: Fri Apr 20 2018 14:17:00 GMT-0500 (Central Daylight Time), y: 150.71}
4:{x: Fri Apr 20 2018 14:18:00 GMT-0500 (Central Daylight Time), y: 150.75}
5:{x: Fri Apr 20 2018 14:19:00 GMT-0500 (Central Daylight Time), y: 150.91}
}
By not having anything at index [0] canvasJS errors out. Unfortunately this is already an object through canvasJS and changing it to an array is not really an ideal solution, so looking for some inspiration of how to do this and essentially shift the object block.
JS Fiddle https://jsfiddle.net/0s4t9eox/14/