# Calculate line graph x and y without knowing max/min of data set

This question pertains to programing but is really a math question.

I am building an application that draws a line graph, similar to a stock line graph. The problem is I am not starting with a known set of numbers. The values will come in based on an altitude value. So, when I start the graph I have no idea what the max and min values of my data set will be, because they max might not come along for some time.

Like I mentioned, these values will be added to the graph when I receive and elevation value but I don't know these until they are received from my device (iPhone).

Is there a formula used to calculate a line graph for unknown values? How can I know what x and y values to use for each new value received? And how can I calculate the min and max of the graph?

-
The usual approach is to choose some default scale for your graph and rescale if the data goes out of range. Maybe you start with 0-1mi on x and current altitude (rounded) +-200 ft on y. Then when somebody goes past 1 mi, change the horizontal (which requires updating all the points) to 0-2 mi. – Ross Millikan Oct 27 '11 at 17:17
@RossMillikan Yes, it is a good idea as you mentioned to set a default scale, this is something that I will implement. Right now I want to get y working because that is really the "unknown" at this point. So are you suggesting that I takes random samples of max and min then adjust my graph scale throughout the duration? – Nic Hubbard Oct 27 '11 at 17:23
sometimes people just cover the range of the current graph. So if somebody starts out on very flat ground, y could be only +-15 feet more or less. The bad news is it jumps around a lot. Otherwise you can just start with the idea that "everybody" will change in elevation by 100 feet and set that as a minimum scale, increasing it as required. Really x and y work the same way in this regard. – Ross Millikan Oct 27 '11 at 17:42
Do you want to post this as an answer? – Nic Hubbard Oct 27 '11 at 19:16

You are receiving a sequence $(x_k,y_k)_{k\geq1}$ of data points in real time. Initialize $$a_1:=b_1:=x_1,\quad c_1:=d_1:=y_1$$ and update these boundaries by means of \left.\eqalign{a_n&:=\min\{a_{n-1}, x_n\},\quad b_n:=\max\{b_{n-1}, x_n\},\cr c_n&:=\min\{c_{n-1}, y_n\},\quad d_n:=\max\{d_{n-1}, y_n\}\cr}\right\}\qquad(n\geq2)\ . Assuming $a_2\ne b_2$ and $c_2\ne d_2$ you can plot the $n\geq2$ data points received so far in the unit square $[0,1]^2$ as follows: \eqalign{\xi_k:={x_k-a_n\over b_n-a_n} \cr \eta_k:={y_k-c_n\over d_n-c_n}\cr}\qquad (1\leq k\leq n)\ .