views:

38

answers:

1

Now I currently have an array that contains 120 pairs. For example:

[[1, 12],
 [2, 15],
 [3, 10]]

Essentially 120 long. Now I am using this data in a Jquery Flot graph. "I have a live example on jsFiddle here"

Code:

var numbers = [];
var jsonString = "";
var PlotData;
var x = 0;

function EveryOneSec() {
  if (numbers.length == 120) {
      numbers.shift();
  }
  numbers.push([x++, Math.random()*10]);
  PlotData = numbers;
  $.plot($("#PlaceHolder"), [{ data: PlotData, points: { show: true},lines: { show: true }}]);
    console.log(PlotData);
  setTimeout(EveryOneSec, 1000);
}
EveryOneSec();

I am creating the numbers on the fly (see jsFiddle Example) just with a simple increment of 1 for the first number and math random for the 2nd now this all works well and fine and sits their counting up again see the jsfiddle example.

Now what I would like to try and do is instead of increasing the numbers by 1 is play with the array. So that the first pair of the array is always [1, ##] and the 120th is always [120, ##] for all within the array. The idea being that it updates every 1 sec so the final result closest to the axis on the left is 120 showing 120 secs ago or 2mins.

So the question is: What would be the most efficient way to do this?

Because the number added on the fly is going to become outdated as soon as it does the next update.

+1  A: 
Yanick Rochon
Very clear answer and fast response thankyou.
Thqr
Now yes I would like them going from right to left :) Buttt.. How would I reverse the axis. E.g 120, 119, 118. Making 0 the rightmost and 120 the leftmost. If you understand what i mean.
Thqr
No problem :) I also answered the below comments :)
Yanick Rochon
Sorry I removed that comment it was me being bad ^^
Thqr
Thankyou for the indepth response and awesome help :)I'll go play around with trying to get it to move the 120 to 0 axis in flot.
Thqr
then just play with the push, unshift, and the return value in the $.map function, and you'll eventually get what you really mean... because i'm not sure I understand what kind of end result you want :) but essentially, you have all the necessary information to make it work. Good luck!
Yanick Rochon