Graphing: Converting Values to Co-ordinates

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

  1. morc

    morc Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. morc

    morc Guest

    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
    #3
  4. 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
    #4
    1. Advertising

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.
Similar Threads
  1. Roubles

    GIS co-ordinates

    Roubles, Nov 6, 2004, in forum: Java
    Replies:
    1
    Views:
    449
    Oscar kind
    Nov 6, 2004
  2. Abhishek
    Replies:
    0
    Views:
    390
    Abhishek
    Mar 5, 2006
  3. Domestos
    Replies:
    5
    Views:
    531
    de_doos
    Oct 6, 2005
  4. Replies:
    4
    Views:
    579
  5. Vikas Kumar

    how to get co-ordinates to write on image

    Vikas Kumar, Jun 20, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    346
    Jeffrey Tan[MSFT]
    Jun 21, 2006
Loading...

Share This Page