Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Graphing: Converting Values to Co-ordinates

Thread view: 
morc - 21 Mar 2006 19:35 GMT
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
Dave Mandelin - 22 Mar 2006 00:36 GMT
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)
morc - 22 Mar 2006 16:58 GMT
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
Dave Mandelin - 22 Mar 2006 22:49 GMT
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);
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.