import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.image.codec.jpeg.*;  //kuulub SUNi JDK-sse
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import java.sql.*;
 
public class statArv extends HttpServlet{
  public void doGet(HttpServletRequest kysimus, HttpServletResponse vastus)throws IOException, ServletException{
    BufferedImage pilt=new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB);
    Graphics2D  piltg=pilt.createGraphics();
    Connection cn=null;
    ResultSet rs;
    ResultSetMetaData rsmd=null;
    Statement st;
    int x1,x2,x3,x4;
    int[] x= new int[4];
    int[][] kordi=new int[4][3]; //grupp//->miinimum/maksimum/summa

    
    try{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		cn=DriverManager.getConnection("jdbc:odbc:andmed", "", "");
		st=cn.createStatement();
		String lause="SELECT * FROM vaatlusandmed WHERE suuruse_ID=2;";
		rs=st.executeQuery(lause);
		rsmd=rs.getMetaData();
		while(rs.next()){
			int a=rs.getInt("punkti_ID");
			int t=rs.getInt("Tulemus");
			if(a==1){
				++x[0];
				if(kordi[0][0]>t || x[0]==1){kordi[0][0]=t;}
				if(kordi[0][1]<t){kordi[0][1]=t;}
				kordi[0][2]=kordi[0][2]+t;
			}
			if(a==2){
				++x[1];
				if(kordi[1][0]>t || x[1]==1){kordi[1][0]=t;}
				if(kordi[1][1]<t){kordi[1][1]=t;}
				kordi[1][2]=kordi[1][2]+t;
			}
			if(a==3){
				++x[2];
				if(kordi[2][0]>t || x[2]==1){kordi[2][0]=t;}
				if(kordi[2][1]<t){kordi[2][1]=t;}
				kordi[2][2]=kordi[2][2]+t;
			}
			if(a==4){
				++x[3];
				if(kordi[3][0]>t || x[3]==1){kordi[3][0]=t;}
				if(kordi[3][1]<t){kordi[3][1]=t;}
				kordi[3][2]=kordi[3][2]+t;
			}
			System.out.println("kordus");
		}
    }catch(Exception e){System.out.println(e);}
    System.out.println(kordi[0][0]+";"+kordi[0][1]+";"+kordi[0][2]);
    System.out.println(kordi[1][0]+";"+kordi[1][1]+";"+kordi[1][2]);
    System.out.println(kordi[2][0]+";"+kordi[2][1]+";"+kordi[2][2]);
    System.out.println(kordi[3][0]+";"+kordi[3][1]+";"+kordi[3][2]);
    int v=0;
    for(int i=0; i<=3; i++){
    	piltg.setColor(Color.red);
    	piltg.fillRect(50+v , 400-kordi[i][0]*10 , 50 , kordi[i][0]*10);
    	piltg.setColor(Color.yellow);
    	piltg.fillRect(100+v , 400-kordi[i][1]*10 , 50 , kordi[i][1]*10);
    	piltg.setColor(Color.green);
    	if(x[i]!=0){piltg.fillRect(150+v , 400-kordi[i][2]*10/x[i] , 50 , kordi[i][2]*10/x[i]);}
    	piltg.setColor(Color.blue);
    	try{piltg.drawString("Grupp="+(i+1),75+v,380);}catch(Exception e){System.out.println(e+"");}
    	try{piltg.drawString("Min/Max/Kesk",75+v,400);}catch(Exception e){System.out.println(e+"");}
    	v=v+200;
    }
    vastus.setContentType("image/jpeg");
    JPEGCodec.createJPEGEncoder(vastus.getOutputStream()).encode(pilt);
    try{cn.close();}catch(Exception e){}
    }
}