Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Graphing: Converting Values to Co-ordinates

Thread Tools

Graphing: Converting Values to Co-ordinates

Posts: n/a
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+"

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);


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

Reply With Quote
Dave Mandelin
Posts: n/a
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)

Reply With Quote
Posts: n/a
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

Reply With Quote
Dave Mandelin
Posts: n/a
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);

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: converting int values to other formats Sunil C Programming 4 04-27-2008 11:28 PM
converting XML values into String using Carbon kitty XML 3 01-07-2007 10:05 PM
converting to STRICT (href values and target attribute) HTML 7 10-01-2006 05:16 PM
converting sqlite return values bolly Python 3 03-20-2006 06:49 PM
converting cookie values mike parr ASP .Net 2 05-11-2004 02:18 PM