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