0

This is my code below to plot a chart on a blank image. The problem is that getY() returns points which go out of the range of image Height It's this line:

yy= mid + ((basePoint - val)*5);
evaluates to:
( 150 + ((90.0f - 92.0f)*5) );
( 150 + ((90.0f - 89.25f)*5) );
( 150 + ((90.0f - 89.0f)*5) );
and so on...
( 150 + ((90.0f - 127.0f)*5) );

When it reaches this high range 127.0f, the coordinates all go out of image bounds.

If I reduce the (*5), then points with very little difference look like as if they are in a straight line, like: 89.0f and 89.25f will form a straight line, but I want 89.25f to be a little higher than 89.0f, visible difference.

Can I change that calculation to something else so it always remains inside image bounds? Even if 127.0f goes beyond 150.0f?

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import javax.imageio.stream.*;


class plotChart{

int w= 1400;
int h= 300;
BufferedImage chart = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );

Integer mid = h/2;
Float basePoint = 0.0f;

public Integer getY( Float val ){
Integer y;
Float yy;

yy= mid + ((basePoint - val)*5);
y= yy.intValue();

return y;
}//method end getY

public static void main( String[] args ){
plotChart pp= new plotChart();


Float[] f={ 90.0f, 92.0f, 89.25f, 94.0f, 97.0f, 99.0f, 102.0f, 110.0f, 115.0f, 120.0f, 125.0f, 127.0f, 130.0f, 60.35f, 64.0f, 70.0f, 74.15f, 74.0f, 75.50f, 88.0f  };

Graphics2D gg= pp.chart.createGraphics();
BasicStroke bs = new BasicStroke(3);
gg.setStroke(bs);

Integer x=0; Integer y= pp.mid;
Integer xx, yy;

pp.basePoint = f[0];

for(int i=0; i<f.length; ++i){

xx= i * 20;
yy= pp.getY( f[i] );

gg.drawLine( x, y, xx, yy );

x=xx; y=yy;

//try{ ImageIO.write( pp.chart, "png", new FileOutputStream( "ppChart.png") ); }catch(Exception e){ e.printStackTrace(); }


}//for


gg.dispose();

try{
ImageIO.write( pp.chart, "png", new FileOutputStream( "ppChart.png") );
}catch(Exception e){ e.printStackTrace(); }

}//main ends
}//class ends
Sumit Kumar
  • 761
  • 1
  • 6
  • 17

1 Answers1

0

One way you could do it is make it so that the graph touches the top of the image. To do this you will need to get the farthest value from the basePoint, and add a float value to the class "plotChart" (I used "diff"). This code would go right after you set the basePoint:

float max = f[0];
for (float fl : f){
    if (fl>max){
        max = fl;
    }
}
float min = f[0];
for (float fl : f){
    if (fl<min){
        min = fl;
    }
}
float maxdiff = max-pp.basePoint;
float mindiff = pp.basePoint-min;
float diff;
if (maxdiff>mindiff){
    diff = maxdiff;
} else {
    diff = mindiff;
}
pp.diff = diff;

Then you need to change the code in getY():

yy= mid + (((basePoint - val)/diff)*mid);

This will make your graph size based on the size of the image because we know that the absolute value of basePoint-val will always be between 0 and diff (except for when it is diff) because diff is the max distance away from the basePoint. The end result is:

end result when run

Arthur
  • 1,246
  • 1
  • 15
  • 19
  • Thanks. I had this in mind as the last resort. I was hoping to use some kind of percentage etc, but looks like there isn't any other way. Thanks :) – Sumit Kumar Jul 14 '16 at 06:21