0

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/

Jon
  • 13
  • 6
  • https://stackoverflow.com/a/500617/965834 – Jeto Apr 20 '18 at 20:41
  • There is no [`push`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) method on objects, so it can't be one (or you're using `push` on something else, in which case you need to reformulate). – Jeto Apr 20 '18 at 20:44
  • The examples that I am seeing from canvasJS show datapoints in an array. Are you certain this is an object you're working with, and not an array? If you're initializing canvasJS data, make sure you're using an array initially. –  Apr 20 '18 at 20:47
  • added jsfiddle with this working...just if you uncomment the part that deletes the first datapoint it breaks – Jon Apr 20 '18 at 20:55
  • @Jon `var dataPoints = [];`. This is an array. See the answer I mentioned earlier. – Jeto Apr 20 '18 at 20:56
  • I bet 100$ that a library like canvasJS doesn't use arrays as objects. Please link to the line of code that leads you to this statement. _and changing it to an array is not really an ideal solution_ <-- no no no – baao Apr 20 '18 at 20:57
  • 1
    ugg, I am probably an idiot...I have been looking at this too long...thank you guys for shoving it through my head lol – Jon Apr 20 '18 at 21:03

0 Answers0