# Graphing: Converting Values to Co-ordinates

Discussion in 'Java' started by morc, Mar 21, 2006.

1. ### morcGuest

hey
I've made a dynamic graphing system using java.awt. The values on the
y-axis are dynmacilay generated from the database. Like so :

public void scaleYAxis(Graphics g,String ticker){
double high = getHigh(ticker);
double low = getLow(ticker);
high = Math.round(high*10000.0)/10000.0;
low = Math.round(low*10000.0)/10000.0;

double increment = (high-low)/4;
double tenPer = high*0.10;

// System.out.println("high:"+high+" low:"+low+" tenPer:"+tenPer+"
Increment:"+increment);

double zero = Math.round((low-increment)*10000.0)/10000.0;
double second = Math.round((low+increment)*10000.0)/10000.0;
double third = Math.round((second+increment)*10000.0)/10000.0;
double fourth = Math.round((third+increment)*10000.0)/10000.0;
double fifth = Math.round((fourth+increment)*10000.0)/10000.0;

//System.out.println("zero:"+zero+" first:"+low+" second:"+second+"
third:"+third+" fourth:"+fourth+" fifth:"+fifth);

g.drawString("(\$)",0,225);
g.drawLine(50,40,50,350);
g.drawString(Double.toString(low),10,300);
g.drawString(Double.toString(second),10,250);
g.drawString(Double.toString(third),10,200);
g.drawString(Double.toString(fourth),10,150);
g.drawString(Double.toString(fifth),10,100);
}

Now i need help devising a formula for a seperate function in which it
takes the value from the db and converts it to coordinates ont ehs cale
in the right spot.
I hope i made sense
Any suggestions are greatly appreciated.
Thanks in adavance
-morc

morc, Mar 21, 2006

2. ### Dave MandelinGuest

I assume 'high' and 'low' are the max and min numeric values, and you
want to know how to convert some other numeric value to a pixel value?
Let y0 be the pixel value for 'low' and y1 the pixel value for 'high'.
Then for value v, the proportional pixel value yv is given by:

(v-low) / (high-low) = (yv - y0) / (y1-y0)

Solving for yv,

yv = y0 + (v - low) * (y1-y0) / (high - low)

Dave Mandelin, Mar 21, 2006

3. ### morcGuest

thanks... sounds like u know what ur talking about
however im not sure how i would put that in a function

would you mind giving me an example?

thanks alot
-morc

morc, Mar 22, 2006
4. ### Dave MandelinGuest

I took 'high' and 'low' from your variable names, so I think those map
in directly. I think for you, y0 and y1, the pixel positions of your
axis, are 100 and 300, but I would recommend putting those in variables
for maintainability. So maybe something like this, which draws a small
circle at the right place:

int yp0 = 100; // Pixel value of bottom of y axis
int yp1 = 300; // Pixel value of top of y axis

int radius = 2; // Radius of little circle marking point

void plotValue(Graphics g, int xp, int value) {
int yp = yp0 + (value - low) * (yp1-yp0) / (high - low);
g.drawOval(xp-radius, yp-radius, radius*2+1, radius*2+1);
}

Dave Mandelin, Mar 22, 2006

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.