JAva awt印刷柱状図、線図


とても乱れて、簡単に記録して、暇があって整理します




import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;

import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;



import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class Test{
	//       :  
	private String fileName = buildFileName("c:\");
	
	private String PillarNum="1";
	private int pillarnum=0;
	private String pillarflag="1";//   (1)    
	//     
	private String topTitle = "    ";
	// No set get Method.
	private Color topTitleColor = Color.gray;
	private Font topTitleFont = new Font("  ", Font.BOLD, 20);
	//    
	private int topTitleStart = 25;
	
	
	//     
	private String leftTitle = "    ";

	private Color leftTitleColor = Color.red;
	private Font leftTitleFont = new Font("  ", Font.PLAIN, 14);
	//    
	private int leftTitleStart = 25;
	//    
	private int leftlength=2;
	
	//     
	private String bottomTitle = "    ";
	private Color bottomTitleColor = Color.red;
	private Font bottomTitleFont = new Font("  ", Font.PLAIN, 14);
	//    
	private int bottomTitleStart = 10;
	//    
	private int bottomlength=2;
	
	//     
	private String[] PillarInfo={"","","","","","","","","","","","","","","","","","","","","","","","","","",};
	private Color rightTitleColor = Color.black;
	private Font rightTitleFont = new Font("  ", Font.PLAIN, 14);
	//    
	private int rightTitleStart = 10;
	//    
	private int rightlength=2;
	
	//         :  
	private Font drawLineFont = new Font("  ", Font.PLAIN, 2); //     
	private Font rectHeadFont = new Font("  ", Font.PLAIN, 12);//     
	private Font rowScaleFont = new Font("  ", Font.PLAIN, 12);//       
	private Font colScaleFont = new Font("  ", Font.PLAIN, 12);//      

	//           :  
	private Color drawLineFontColor = Color.lightGray;
	
	private Color rowScaleFontColor = Color.black;
	private Color colScaleFontColor = Color.darkGray;
	//private Color rectHeadFontColor = Color.black;
	private Color[] rectHeadFontColor ={Color.black,Color.decode("#FF0000"),Color.decode("#0000FF"),Color.decode("#77CC50"),Color.decode("#FF6600"),Color.decode("#00FF00"),Color.decode("#cc99FF"),Color.decode("#000080")};	
	//        =  :#99CC00:  
	//private Color fillRectColor = Color.decode("#99CC00");// new
	//       =  :#99CC00:  
	//#99cc00,#FF0000,#0000FF,#77CC50,#FF6600,#00FF00,#cc99FF,#000080
	private Color[] fillRectColor ={Color.decode("#99cc00"),Color.decode("#FF0000"),Color.decode("#0000FF"),Color.decode("#77CC50"),Color.decode("#FF6600"),Color.decode("#00FF00"),Color.decode("#cc99FF"),Color.decode("#000080")};
	// Color(153,204,0);//Color.decode("#00C600");
	// //Color.decode("#0099FF");
	private Color frameRectColor = Color.GRAY; //     

	//        =    :  (       )
	private Color canvasBackgroundColor = Color.decode("#efefef"); // Color.white;//

	//         :  :  
	
	private int topVerge = 20;//     
	private int addtoptitleVerge=0;//      ,        
	private int bottomVerge = 0; //     
	private int addbottomtitleVerge=0;//      ,        
	private int leftVerge = 0;//    
	private int addlefttitleVerge=0;//      ,        
	private int addrighttitleVerge=0;//      ,        
	private int rightVerge = 20;//    20
	private int leftmargin=10;//    
	private int bottommargin=10;//    
	//     :  
	private Hashtable hb = null;

	//     
	private String dataList = null;

	//       ,         :  
	private boolean bPaint = true;
	private String gradual="0";
	//     =  : :800; :300:  (        )
	private int canvasWidth = 800;
	private int canvasHeight = 300;

	//     =  : :11; :31:     (    )
	private int rowCount = 10;
	private int colCount = 200;


	//       ,     
	private int rectFootLenght = 0;

	//            :px:  
	private int rectHeadFontOffset = 2;

	//      :px[    ,     ]:  
	private int drawLineInterval = 4;

	//     :  100 [    ]:   
	private int zoomInRate = 100;

	//      =  :px:     
	private int rectWidth = 16;

	
	//      :  
	private int paintWidth = 0;
	private int paintHeight = 0;

	//     :   ;   :  ,         colinterval        
	private int colInterval = 20*pillarnum;
	private int rowInterval = 20;
	
	public void paint(BufferedImage bi) {
		if (bi == null){
			bPaint = false;
		}
		if (bPaint) {
			//       :    
			// BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight,
			// BufferedImage.TYPE_INT_BGR);
			Graphics g = bi.createGraphics();
			//       
			g.setColor(canvasBackgroundColor);
			g.fillRect(0, 0, canvasWidth, canvasHeight);
			//  top
			printTop(g);
			//  left
			printLeft(g);
			//      
			printMain(g);
			//  bottom
			printBottom(g);
			//  right
			if(pillarnum>1){
				printRight(g);
			}
			//     
			g.dispose();

		} else {//         
			if (bi == null){
				bi = new BufferedImage(canvasWidth, canvasHeight,BufferedImage.TYPE_INT_BGR);
			}
			Graphics g = bi.createGraphics();
			//       
			g.setColor(canvasBackgroundColor);
			g.fillRect(0,0,canvasWidth,canvasHeight);
			g.setColor(Color.RED);
			g.setFont(new Font("  ", Font.BOLD, 20));
			g.drawString("    ,     !",canvasWidth/2-11*10,canvasHeight/2+13-10);
			//     
			g.dispose();
		}
	}
	public void printTop(Graphics g) {
		//      
		int mainTitleLeftStart = canvasWidth / 2 - topTitle.length()* topTitleFont.getSize()* 10 / 23;
		g.setColor(topTitleColor);
		g.setFont(topTitleFont);
		g.drawString(topTitle, mainTitleLeftStart, topTitleStart);
	}
	public void printLeft(Graphics g) {
		//       ,                       ,    13
		if(canvasHeight<=(leftTitle.length()*20+13)){
			leftTitleStart=13;
		}else{
			leftTitleStart =canvasHeight/2 - leftTitle.length()*10+13;
		}
		//     
		g.setColor(leftTitleColor);
		g.setFont(leftTitleFont);
		
		drawString(g,leftTitle,(leftmargin+addlefttitleVerge)/2-10,leftTitleStart,20);
	}
	public void printRight(Graphics g) {
		
		
		rightTitleStart=canvasWidth-rightVerge-addrighttitleVerge+5;
		for(int i=0;i<pillarnum;i++){
			if(pillarflag.equalsIgnoreCase("1")){
				//    gradual 1   
				if(gradual.equalsIgnoreCase("1")){
					paintRect(g,rightTitleStart,canvasHeight/2+ (i-pillarnum)*17+13, rectWidth - 2,rectWidth - 2,fillRectColor[i]);
				}else{
					g.setColor(fillRectColor[i]);
					g.fillRect(rightTitleStart,canvasHeight/2+ (i-pillarnum)*17+13, rectWidth - 2,rectWidth - 2);
				}
			}else{
				g.setColor(fillRectColor[i]);
				g.drawOval(rightTitleStart-2,canvasHeight/2+ (i-pillarnum)*17+11, 5, 5);
				g.fillOval(rightTitleStart-2,canvasHeight/2+ (i-pillarnum)*17+11, 5, 5);
			}
			//     
			g.setColor(rightTitleColor);
			g.setFont(rightTitleFont);
			
			g.drawString(PillarInfo[i],rightTitleStart+rectWidth,canvasHeight/2+ (i-pillarnum)*17+11+rectWidth);
		}
	}
	public void printMain(Graphics g) {
		double val_max = toDbl(hb.get("val_max"));
		int rowScaleMax = round(val_max, 1);
		int rowScale = rowScaleMax / rowCount;
		boolean bCalc = (rowScale * rowCount == rowScaleMax);

		//     
		String[] col_arr = (String[]) hb.get("col_arr");

		double[][] val_arr = (double[][]) hb.get("val_arr");
		//     :     
		g.setFont(rowScaleFont);
		g.setColor(rowScaleFontColor);
		for (int i = 1; i < rowCount + 1; i++) {
			g.drawString(fmtNum("" + rowScale * i, leftlength, ' '), leftmargin+addlefttitleVerge,(topVerge+addtoptitleVerge + paintHeight - i * rowInterval ));
		}
		//      
		g.setColor(frameRectColor);
		g.drawRect(leftmargin+leftVerge+addlefttitleVerge, topVerge+addtoptitleVerge, paintWidth, paintHeight);
		//     
		g.setColor(Color.white);
		g.fillRect(leftmargin+leftVerge+addlefttitleVerge + 1, topVerge+addtoptitleVerge + 1, paintWidth - 1, paintHeight - 1);
		//     
		int offset = drawLineFont.getSize() / 2;
		g.setFont(drawLineFont);
		g.setColor(drawLineFontColor);
		for (int i = 1; i < rowCount + 1; i++) {
			for (int j = leftVerge+addlefttitleVerge + offset; j < (leftmargin+leftVerge+addlefttitleVerge + paintWidth); j += drawLineInterval) {
				g.drawString(".", j,(topVerge+addtoptitleVerge + paintHeight - i * rowInterval + 2));
			}
		}
		//     :   
		offset = colScaleFont.getSize() / 2;
		g.setFont(colScaleFont);
		g.setColor(colScaleFontColor);
		//      
		for (int i = 1; i < colCount + 1; i++) {
			drawString(g, col_arr[i - 1],leftmargin+ leftVerge+addlefttitleVerge + 20+(i-1) * colInterval+pillarnum*rectWidth / 2-rectWidth, canvasHeight - bottomVerge-addbottomtitleVerge-bottommargin+15, 12);
		}

		int rectHeight = (int) Math.round(Math.random() * 10);
		
	if(pillarflag.equals("1")){
		for (int i = 1; i < colCount + 1; i++) {
			for(int firsti=0;firsti<pillarnum;firsti++){
				if (bCalc){
					rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight - topVerge), 0);
				}else{
					rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight), 0);
				}
				//gradual 1   
				if(gradual.equalsIgnoreCase("1")){
					paintRect(g, leftmargin+leftVerge+addlefttitleVerge+  20+(i-1) * colInterval+firsti*rectWidth - rectWidth / 2, (topVerge+addtoptitleVerge+ paintHeight - rectHeight), rectWidth, rectHeight+ rectFootLenght,fillRectColor[firsti]);
				}else{
					g.setColor(fillRectColor[firsti]);
					g.fillRect(leftmargin+leftVerge+addlefttitleVerge+  20+(i-1) * colInterval+firsti*rectWidth - rectWidth / 2, (topVerge+addtoptitleVerge+ paintHeight - rectHeight), rectWidth, rectHeight+ rectFootLenght);
				}
			}
		}
	}else{
		for(int firsti=0;firsti<pillarnum;firsti++){
			int[] x=new int[colCount];
			int[] y=new int[colCount];
			for(int i=1;i<colCount+1;i++){
				if (bCalc){
					rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight - topVerge), 0);
				}else{
					rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight), 0);
				}
				x[i-1]=leftmargin+leftVerge+addlefttitleVerge+  20+(i-1) * colInterval+firsti*rectWidth+5- rectWidth / 2;
				y[i-1]=(topVerge+addtoptitleVerge+ paintHeight - rectHeight);
				g.setColor(fillRectColor[firsti]);
				g.drawOval(x[i-1]-3, y[i-1]-3, 6, 6);
				g.fillOval(x[i-1]-3, y[i-1]-3, 6, 6);
			}
			g.setColor(fillRectColor[firsti]);
			g.drawPolyline(x, y, colCount);
		}
	}
			for (int i = 1; i < colCount + 1; i++) {
				double d=0;
				for(int firsti=0;firsti<pillarnum;firsti++){
					if(val_arr[i-1][firsti]!=d){
						//      
						g.setFont(rectHeadFont);
						g.setColor(rectHeadFontColor[firsti]);
						offset = String.valueOf(val_arr[i - 1][firsti]).length() / 2;
						if (offset > 2){
							offset += 2;
						}
						offset += rectWidth / 2;
						rectHeight = round((val_arr[i - 1][firsti] / val_max)* (paintHeight - topVerge), 0);
						g.drawString(fmtOutStr(val_arr[i - 1][firsti]), leftmargin+leftVerge+addlefttitleVerge+20+ (i-1) * colInterval+firsti*rectWidth- offset,(topVerge+addtoptitleVerge + paintHeight - rectHeight - rectHeadFontOffset));
						d=val_arr[i-1][firsti];
					}
				}
			}
		
	}
	//      
	 public void paintRect(Graphics g,float x, float y,  int width, int height,Color color) {
		 /*
		 	int r=color.getRed();
			int gr=color.getGreen();
			int b=color.getBlue();
			int minusoffset=30;
			int addoffset=10;
			Color color1=new Color(RGBscope(r-minusoffset),RGBscope(gr-minusoffset),RGBscope(b-minusoffset));
			Color color2=new Color(RGBscope(r+addoffset),RGBscope(gr+addoffset),RGBscope(b+addoffset));
		*/
			Graphics2D g2D = (Graphics2D) g;
			Point2D.Float p1 = new Point2D.Float(x, y);
			Point2D.Float p2 = new Point2D.Float(x,y+height);
			   
			GradientPaint g1 = new GradientPaint(p1, color, p2, Color.decode("#FFFFFF"),false);
			Rectangle2D.Float rect1 = new Rectangle2D.Float(x,y,width,height);
			g2D.setPaint(g1);
			g2D.fill(rect1);
}
	 public int RGBscope(int value){
		 if(value<0){
			 value=0;
		 }else if(value>255){
			 value=255;
		 }else{
			 
		 }
		 return value;
	 }
	public void printBottom(Graphics g) {
		
		//      
		int bottomTitleLeftStart = canvasWidth / 2 - bottomTitle.length()* bottomTitleFont.getSize() / 2;
		g.setColor(bottomTitleColor);
		g.setFont(bottomTitleFont);
		g.drawString(bottomTitle, bottomTitleLeftStart + 10, canvasHeight-(addbottomtitleVerge+bottommargin)/2+5);
		
/*
		//         
		int bottomTitleLeftStart = canvasWidth / 2 - bottomTitle.length()
				* bottomTitleFont.getSize() / 2;
		g.setColor(fillRectColor);
		// g.fillRect(bottomTitleLeftStart-6, canvasHeight -
		// bottomTitleStart - rectWidth+4,
		// rectWidth+bottomTitleFont.getSize()*bottomTitle.length(),
		// rectWidth);
		g.fillRect(bottomTitleLeftStart - 6, canvasHeight
				- bottomTitleStart - rectWidth + 4, rectWidth - 2,
				rectWidth - 2);
*/
	}
	//         
	public void calculateScaleParameters() {
		//     
		// System.out.println("fdsafd"+dataList);
		hb = parseData();
		int data_len = toInt(hb.get("data_len"));
		leftlength=hb.get("val_max").toString().trim().length()-2;
		//         
		if (colCount < 2){
			colCount = 10;
		}
		if (rowCount < 2){
			rowCount = 31;
		}
		if (data_len < colCount){
			colCount = data_len;
		}
		
		//               bottomVerge
		String[] s=(String[]) hb.get("col_arr");
		int templength=0;
		for(int i=0;i<s.length;i++){
			if(templength<s[i].length()){
				templength=s[i].length();
			}
		}
		setBottomlength(templength);
		//      13
		bottomVerge=bottomlength*13;
		if(getBottomTitle().equals("")){
			addbottomtitleVerge=0;
		}else{
			//      20
			addbottomtitleVerge=20;
		}
		//       6
		leftVerge=leftlength*6;
		if(!getLeftTitle().equals("")){
			//      20+     <=addlefttitleVerge
			addlefttitleVerge=24;
		}
		if(!getTopTitle().equals("")){
			//        20   
			addtoptitleVerge=20;
		}
		if(pillarnum>1){
			rightVerge=10;
			addrighttitleVerge=rightlength*20;
		}
		canvasWidth = colCount * colInterval + leftmargin+leftVerge +addlefttitleVerge+ addrighttitleVerge+rightVerge+20;
		canvasHeight = rowCount * rowInterval + bottommargin+topVerge+addtoptitleVerge + bottomVerge+addbottomtitleVerge+20;

		paintWidth = canvasWidth - leftVerge-leftmargin -addlefttitleVerge- rightVerge-addrighttitleVerge;
		paintHeight = canvasHeight - topVerge-addtoptitleVerge-addbottomtitleVerge-bottommargin - bottomVerge;

		// System.err.print(paintWidth+" "+paintHeight);
		//            
		if (paintHeight < 20 || paintWidth < 20)
			bPaint = false;

		//       :     
		if (zoomInRate < 100)
			zoomInRate = 100;
		// colInterval = (paintWidth - rightMargin) / colCount;
		// rowInterval = (paintHeight - topMargin) / rowCount;

		//            ,      80%
		if (rectWidth >= colInterval)
			rectWidth = colInterval * 4 / 5;

		//     2px
		if (rectWidth < 2)
			rectWidth = 2;
		
		//             
		if(!bPaint){
			canvasWidth=300;
			canvasHeight=100;
		}
	}

	public Hashtable parseData() {
		// String data =
		// "'   ',121330.22,'2',92333.84,'3',31110.4677,'4',72950,'5',19620,'6',11000,'7',38300,'8',57870,'9',34720,'10',0,'11',66810,'12',76030,'13',67190,'14',66950,'15',34370,'16',25400,'17',0,'18',44780,'19',108820,'20',53830,'21',57730,'22',134190,'23',6000,'24',0,'25',55600,'26',67620,'27',11030,'28',81370,'29',16850,'30',159650,'31',7000,'32',2000,'33',6000,'34',12300";
		String data = "1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0";
		if (dataList != null && !dataList.trim().equals("")){
			data = dataList;
		}else{
			bPaint=false;
		}
		return parseData(data);
	}
	public Hashtable parseData(String data) {
		if (data == null || data.length() < 2){
			return null;
		}
		String[] arr_col = null;
		double[][] arr_dbl = null;
		String[] arr = replace(data, "'", "").split(",");
		int len = 0;
		if (arr != null){
			len = arr.length;
		}else{
			return null;
		}
		double max = 0.0d;
		Hashtable hb = new Hashtable(200);
		arr_col = new String[len / (pillarnum+1)];
		arr_dbl = new double[len / (pillarnum+1)][pillarnum];
		hb.put("data_len", String.valueOf(len / (pillarnum+1)));
		for (int i = 0; i < len; i++) {
			if (arr[i] == null || arr[i].trim().equals("")){
				arr[i] = "0";
			}
			if ((i % (pillarnum+1)) != 0  ){
				if(max < Double.valueOf(arr[i]).doubleValue()){
					max = Double.valueOf(arr[i]).doubleValue();
				}
			}
			if ((i % (pillarnum+1)) == 0){
				arr_col[i / (pillarnum+1)]=arr[i];
			}else{
				if((i % (pillarnum+1)) == 1){
					for(int ii=0;ii<pillarnum;ii++){
						arr_dbl[i / (pillarnum+1)][ii] = Double.valueOf(arr[i+ii]).doubleValue();
					}
				}
			}
		}
		if (max < 10) {
			max = 10;
		}
		hb.put("col_arr", arr_col);
		hb.put("val_arr", arr_dbl);
		hb.put("val_max", String.valueOf(max));

		return hb;
	}
	
	public void drawString(Graphics g, String msg, int x, int y, int offset) {
		if (g == null)
			return;
		if (x < 0 || y < 0)
			return;
		if (msg == null || msg.trim().equals(""))
			return;
		int len = msg.length();
		if (toInt(msg) > 0) {
			g.drawString(msg, x, y);
			return;
		}
		for (int i = 0; i < len; i++) {
			g.drawString(msg.substring(i, i + 1), x, y + i * offset);
		}

	}
	public String getFileName(String file) {
		String szRtn = ".";
		if (file == null || file.trim().equals(""))
			return File.separator + System.currentTimeMillis() + ".jpg";

		szRtn = file.trim();
		File f = new File(szRtn);
		String path = f.getParent();
		String name = f.getName();
		int iPos = name.lastIndexOf(".");
		if (iPos < 0)
			name += ".jpg";
		else
			name = name.substring(0, iPos) + ".jpg";
		f = new File(path);
		if (!f.exists() || !f.isDirectory())
			System.err.println("create dir ok! " + f.mkdirs());
		szRtn = path + File.separator + name;
		return szRtn;
	}
	public void paintToPic(String file) //     
	{
		fileName = getFileName(file);
		System.err.println(fileName);

		//       
		calculateScaleParameters();

		//         
		BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight,
				BufferedImage.TYPE_INT_BGR);

		//          
		paint(bi);

		try {
			FileOutputStream out = new FileOutputStream(fileName);
			JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(out);
			en.encode(bi);
			if (out != null) {
				out.close();
				out = null;
			}
		} catch (Exception e) {
			System.err.println("Create pic err:" + e);
		}
	}
	public void paintToStream(HttpServletResponse response) //     
	{
		//       
		calculateScaleParameters();

		//         
		BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight,
				BufferedImage.TYPE_INT_BGR);

		//          
		paint(bi);
		/*
		 * response.setContentType("image/jpeg");
		 * response.addHeader("pragma","NO-cache");
		 * response.addHeader("Cache-Control","no-cache");
		 * response.addDateHeader("Expries",0);
		 */
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(bi);
			if (false && out != null) {
				out.close();
				out = null;
			}

		} catch (Exception e) {
			System.err.println("Create pic err:" + e);
		}
	}
	/**
	 *               ,  w      
	 * */
	public String fmtNum(String src, int w, char ch) {
		StringBuffer sb = new StringBuffer();
		if (src == null || w < 0)
			return src;
		if (w <= src.length())
			return src;
		int len = w - src.length();
		for (int i = 0; i < len; i++)
			sb.append(ch);
		sb.append(src);
		return sb.toString();
	}
	public String fmtOutStr(double num) {
		return fmtNumStr(roundDbl(num, -2));
	}
	public String fmtNumStr(String str) {
		if (str == null || str.trim().equals(""))
			return str;
		int iPos = str.lastIndexOf(".");
		if (iPos < 0)
			return str;
		String head = str.substring(0, iPos);
		String tail = str.substring(iPos + 1);
		iPos = tail.length();
		while (head.startsWith("0"))
			head = head.substring(1);
		while (tail.endsWith("0"))
			tail = tail.substring(0, tail.length() - 1);

		if (tail.length() == iPos)
			return str;
		else if (tail.length() > 0)
			return head + "." + tail;
		else
			return head;
	}
	public int round(double num, int w) {
		if (num < 0.000001d || w < 1)
			return (int) Math.round(num);
		String szDbl = String.valueOf(num);
		int iPos = szDbl.indexOf(".");
		String szRate = "1";
		for (int i = 0; i < w; i++)
			szRate += "0";
		int tmp = (int) Math.round(num);
		int rate = Integer.valueOf(szRate).intValue();

		if (iPos > 0) {
			if (iPos <= w)
				return tmp;
			szDbl = szDbl.substring(iPos + 1);
			if (0 < Integer.valueOf(szDbl).intValue())
				return (int) (tmp / rate * rate + rate);
		}

		if (0 < (tmp % rate))
			return (int) (tmp / rate * rate + rate);
		return tmp;
	}
	public String buildFileName(String path) {
		String dir = ".";
		if (path != null || !path.trim().equals(""))
			dir = path.trim();

		File f = new File(dir);
		if (f.isFile() || !f.exists())
			System.err.println("create dir:" + f.mkdirs());
		dir = f.getAbsolutePath() + File.separator + System.currentTimeMillis()
				+ ".jpg";
		return dir;
	}
	public double toDbl(Object obj) {
		if (obj == null || obj.toString().trim().equals(""))
			return 0.0d;

		try {
			return Double.parseDouble(obj.toString().trim());
		} catch (Exception ex) {
			return 0;
		}
	}
	public int toInt(Object obj) {
		if (obj == null || obj.toString().trim().equals(""))
			return 0;

		try {
			return Integer.parseInt(obj.toString().trim());
		} catch (Exception ex) {
			return 0;
		}
	}
	public String roundDbl(double num, int w) {
		if(Double.valueOf(num).toString().contains("E")){
			return Double.valueOf(num).intValue()+"";
		}
		int width = 0;
		if (w > 0)
			return String.valueOf(round(num, w));

		width = Math.abs(w);
		String szDbl = String.valueOf(num);
		int iPos = szDbl.indexOf(".");
		if (width >= iPos)
			width = 2;
		if (iPos < 0) {
			szDbl += ".";
			for (int i = 0; i < width; i++)
				szDbl += "0";
			return szDbl;
		} else {
			int len = szDbl.length();
			if (width < (len - iPos - 1)) {
				iPos = iPos + 1 + width; //    +   +   
				double d1 = Double.valueOf(szDbl.substring(0, iPos))
						.doubleValue();
				int k = Integer.valueOf(szDbl.substring(iPos, iPos + 1))
						.intValue();
				if (k < 5)
					return String.valueOf(d1);
				else {
					String szTmp = "1";
					for (int i = 0; i < width; i++)
						szTmp += "0";
					// System.err.println("width="+width+" szTmp="+szTmp);
					int tmp = Integer.valueOf(szTmp).intValue();
					d1 = (d1 * tmp + 1) / tmp;
					szDbl = String.valueOf(d1);
				}
			}

			iPos = szDbl.indexOf(".");
			len = szDbl.length();
			if (width >= (len - iPos - 1)) {
				width = width - (len - iPos - 1);
				for (int i = 0; i < width; i++)
					szDbl += "0";
			}
			return szDbl;
		}
	}

	public String replace(String szSrc, String szOld, String szNew) {
		try {
			if (szSrc == null || szSrc.trim().equals(""))
				return "";

			String szTmp = szSrc.trim();
			int iOldLen = szOld.length();
			int iSrcLen = szSrc.length();
			int iStart = 0;
			int iPos = -1;
			StringBuffer sb = new StringBuffer(iSrcLen + iOldLen * 6);

			if (szNew == null) {
				return szTmp;
			}
			if (szOld == null || szOld.equals("")) {
				return szTmp;
			}

			if ((iPos = szTmp.indexOf(szOld)) == -1) //     
			{
				return szTmp;
			}

			while ((iPos = szTmp.indexOf(szOld, iStart)) > -1) {
				sb.append(szTmp.substring(iStart, iPos));
				sb.append(szNew);
				iStart = iPos + iOldLen;
			}

			if (iStart < iSrcLen) {
				sb.append(szTmp.substring(iStart));
			}

			return sb.toString();
		} catch (Exception ex) {
			return "";
		}
	}
	/**
	 *   (     )
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
	public String toGBKStr(String str) {
		try {
			String strTmp = "";
			if ((str == null) || str.trim().equals("")) {
				return "";
			} else {
				strTmp = str.trim();
			}
			
			
			
			byte[] bytes = strTmp.getBytes("GBK");// ISO8859_1
			return (new String(bytes, "GBK"));
		} catch (Exception e) {
			return "";
		}
	}
	
	
	
	
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getTopTitle() {
		return topTitle;
	}
	public void setTopTitle(String topTitle) {
		this.topTitle = topTitle;
	}
	public Color getTopTitleColor() {
		return topTitleColor;
	}
	public void setTopTitleColor(Color topTitleColor) {
		this.topTitleColor = topTitleColor;
	}
	public Font getTopTitleFont() {
		return topTitleFont;
	}
	public void setTopTitleFont(Font topTitleFont) {
		this.topTitleFont = topTitleFont;
	}
	public int getTopTitleStart() {
		return topTitleStart;
	}
	public void setTopTitleStart(int topTitleStart) {
		this.topTitleStart = topTitleStart;
	}
	public String getLeftTitle() {
		return leftTitle;
	}
	public void setLeftTitle(String leftTitle) {
		this.leftTitle = leftTitle;
	}
	public Color getLeftTitleColor() {
		return leftTitleColor;
	}
	public void setLeftTitleColor(Color leftTitleColor) {
		this.leftTitleColor = leftTitleColor;
	}
	public Font getLeftTitleFont() {
		return leftTitleFont;
	}
	public void setLeftTitleFont(Font leftTitleFont) {
		this.leftTitleFont = leftTitleFont;
	}
	public int getLeftTitleStart() {
		return leftTitleStart;
	}
	public void setLeftTitleStart(int leftTitleStart) {
		this.leftTitleStart = leftTitleStart;
	}
	public String getBottomTitle() {
		return bottomTitle;
	}
	public void setBottomTitle(String bottomTitle) {
		this.bottomTitle = bottomTitle;
	}
	public Color getBottomTitleColor() {
		return bottomTitleColor;
	}
	public void setBottomTitleColor(Color bottomTitleColor) {
		this.bottomTitleColor = bottomTitleColor;
	}
	public Font getBottomTitleFont() {
		return bottomTitleFont;
	}
	public void setBottomTitleFont(Font bottomTitleFont) {
		this.bottomTitleFont = bottomTitleFont;
	}
	public int getBottomTitleStart() {
		return bottomTitleStart;
	}
	public void setBottomTitleStart(int bottomTitleStart) {
		this.bottomTitleStart = bottomTitleStart;
	}
	public int getBottomlength() {
		return bottomlength;
	}
	public void setBottomlength(int bottomlength) {
		this.bottomlength = bottomlength;
	}
	public Font getDrawLineFont() {
		return drawLineFont;
	}
	public void setDrawLineFont(Font drawLineFont) {
		this.drawLineFont = drawLineFont;
	}
	public Font getRectHeadFont() {
		return rectHeadFont;
	}
	public void setRectHeadFont(Font rectHeadFont) {
		this.rectHeadFont = rectHeadFont;
	}
	public Font getRowScaleFont() {
		return rowScaleFont;
	}
	public void setRowScaleFont(Font rowScaleFont) {
		this.rowScaleFont = rowScaleFont;
	}
	public Font getColScaleFont() {
		return colScaleFont;
	}
	public void setColScaleFont(Font colScaleFont) {
		this.colScaleFont = colScaleFont;
	}
	public Color getDrawLineFontColor() {
		return drawLineFontColor;
	}
	public void setDrawLineFontColor(Color drawLineFontColor) {
		this.drawLineFontColor = drawLineFontColor;
	}
	public Color[] getRectHeadFontColor() {
		return rectHeadFontColor;
	}
	public void setRectHeadFontColor(String rectHeadFontColor) {
		if(!rectHeadFontColor.equals("")){
			String[] s=rectHeadFontColor.split(",");
			for(int i=0;i<s.length;i++){
				if(s[i].indexOf("#")==-1){
					s[i]="#"+s[i];
				}
				this.rectHeadFontColor[i] =Color.decode(s[i]);
			}
		}
	}
	public Color getRowScaleFontColor() {
		return rowScaleFontColor;
	}
	public void setRowScaleFontColor(Color rowScaleFontColor) {
		this.rowScaleFontColor = rowScaleFontColor;
	}
	public Color getColScaleFontColor() {
		return colScaleFontColor;
	}
	public void setColScaleFontColor(Color colScaleFontColor) {
		this.colScaleFontColor = colScaleFontColor;
	}
	public Color[] getFillRectColor() {
		return fillRectColor;
	}
	public void setFillRectColor(String fillRectColor) {
		if(!fillRectColor.equals("")){
			String[] s=fillRectColor.split(",");
			for(int i=0;i<s.length;i++){
				if(s[i].indexOf("#")==-1){
					s[i]="#"+s[i];
				}
				this.fillRectColor[i] =Color.decode(s[i]);
			}
		}
	}
	public Color getFrameRectColor() {
		return frameRectColor;
	}
	public void setFrameRectColor(Color frameRectColor) {
		this.frameRectColor = frameRectColor;
	}
	public Color getCanvasBackgroundColor() {
		return canvasBackgroundColor;
	}
	public void setCanvasBackgroundColor(Color canvasBackgroundColor) {
		this.canvasBackgroundColor = canvasBackgroundColor;
	}
	public Hashtable getHb() {
		return hb;
	}
	public void setHb(Hashtable hb) {
		this.hb = hb;
	}
	public String getDataList() {
		return dataList;
	}
	public void setDataList(String dataList) {
		this.dataList = dataList;
	}
	public boolean isBPaint() {
		return bPaint;
	}
	public void setBPaint(boolean paint) {
		bPaint = paint;
	}
	public int getCanvasWidth() {
		return canvasWidth;
	}
	public void setCanvasWidth(int canvasWidth) {
		this.canvasWidth = canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	public void setCanvasHeight(int canvasHeight) {
		this.canvasHeight = canvasHeight;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	public int getColCount() {
		return colCount;
	}
	public void setColCount(int colCount) {
		this.colCount = colCount;
	}
	public int getTopVerge() {
		return topVerge;
	}
	public void setTopVerge(int topVerge) {
		this.topVerge = topVerge;
	}
	public int getBottomVerge() {
		return bottomVerge;
	}
	public void setBottomVerge(int bottomVerge) {
		this.bottomVerge = bottomVerge;
	}
	public int getLeftVerge() {
		return leftVerge;
	}
	public void setLeftVerge(int leftVerge) {
		this.leftVerge = leftVerge;
	}
	public int getRightVerge() {
		return rightVerge;
	}
	public void setRightVerge(int rightVerge) {
		this.rightVerge = rightVerge;
	}
	public int getRectFootLenght() {
		return rectFootLenght;
	}
	public void setRectFootLenght(int rectFootLenght) {
		this.rectFootLenght = rectFootLenght;
	}
	public int getRectHeadFontOffset() {
		return rectHeadFontOffset;
	}
	public void setRectHeadFontOffset(int rectHeadFontOffset) {
		this.rectHeadFontOffset = rectHeadFontOffset;
	}
	public int getDrawLineInterval() {
		return drawLineInterval;
	}
	public void setDrawLineInterval(int drawLineInterval) {
		this.drawLineInterval = drawLineInterval;
	}
	public int getZoomInRate() {
		return zoomInRate;
	}
	public void setZoomInRate(int zoomInRate) {
		this.zoomInRate = zoomInRate;
	}
	public int getRectWidth() {
		return rectWidth;
	}
	public void setRectWidth(int rectWidth) {
		this.rectWidth = rectWidth;
	}
	
	public int getPaintWidth() {
		return paintWidth;
	}
	public void setPaintWidth(int paintWidth) {
		this.paintWidth = paintWidth;
	}
	public int getPaintHeight() {
		return paintHeight;
	}
	public void setPaintHeight(int paintHeight) {
		this.paintHeight = paintHeight;
	}
	public int getColInterval() {
		return colInterval;
	}
	public void setColInterval(int colInterval) {
		this.colInterval = colInterval;
	}
	public int getRowInterval() {
		return rowInterval;
	}
	public void setRowInterval(int rowInterval) {
		this.rowInterval = rowInterval;
	}
	public String[] getPillarInfo() {
		return PillarInfo;
	}
	public void setPillarInfo(String pillarInfo) {
		if(!pillarInfo.equals("")){
			String[] s=pillarInfo.split(",");
			for(int i=0;i<s.length;i++){
				PillarInfo[i] = s[i];
				if(this.rightlength<s[i].length()){
					this.rightlength=s[i].length();
				}
			}
		}
	}
	public String getPillarNum() {
		return PillarNum;
	}
	public void setPillarNum(String pillarNum) {
		PillarNum = pillarNum;
	}
	public int getPillarnum() {
		return pillarnum;
	}
	public void setPillarnum(int pillarnum) {
		this.pillarnum = pillarnum;
	}
	public int getLeftlength() {
		return leftlength;
	}
	public void setLeftlength(int leftlength) {
		this.leftlength = leftlength;
	}
	public int getAddtoptitleVerge() {
		return addtoptitleVerge;
	}
	public void setAddtoptitleVerge(int addtoptitleVerge) {
		this.addtoptitleVerge = addtoptitleVerge;
	}
	public int getAddbottomtitleVerge() {
		return addbottomtitleVerge;
	}
	public void setAddbottomtitleVerge(int addbottomtitleVerge) {
		this.addbottomtitleVerge = addbottomtitleVerge;
	}
	public int getAddlefttitleVerge() {
		return addlefttitleVerge;
	}
	public void setAddlefttitleVerge(int addlefttitleVerge) {
		this.addlefttitleVerge = addlefttitleVerge;
	}
	public int getLeftmargin() {
		return leftmargin;
	}
	public void setLeftmargin(int leftmargin) {
		this.leftmargin = leftmargin;
	}
	public int getBottommargin() {
		return bottommargin;
	}
	public void setBottommargin(int bottommargin) {
		this.bottommargin = bottommargin;
	}
	public void setRectHeadFontColor(Color[] rectHeadFontColor) {
		this.rectHeadFontColor = rectHeadFontColor;
	}
	public void setFillRectColor(Color[] fillRectColor) {
		this.fillRectColor = fillRectColor;
	}
	
	public String getGradual() {
		return gradual;
	}
	public void setGradual(String gradual) {
		this.gradual = gradual;
	}
	public String getPillarflag() {
		return pillarflag;
	}
	public void setPillarflag(String pillarflag) {
		this.pillarflag = pillarflag;
	}
	public Test() {
		super();
	}
	public Test(String PillarNum) {
		super();
		if(PillarNum.equals("")){
			PillarNum="1";
		}
		this.PillarNum=PillarNum;//      
		this.pillarnum=Integer.valueOf(PillarNum);
		this.colInterval=pillarnum*20;//         
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Color.blue);
		Color c=new Color(-16776961);
		System.out.println(c);
	}
	
	

}



<%@ page language="java"%>
<%@ page contentType="text/html;charset=gbk"%>
<%@page import="Test"%>
<%@page import="java.awt.Color" %>
<title>Test Create Pic</title>
 
<HTML>
 <HEAD>  
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 </HEAD>
 <body>
<%
		String strChartData=request.getParameter("chartData");
		String Pillarflag="1";//      1 
		//String strCoordColor="#99cc00,#0000FF,#FF0000,#FF99cc,#800000,#cc99FF,#000080,#808000";//    
		String strCoordColor="";//    
		//String RectHeadFontColor="#99cc00,#0000FF,#FF0000,#FF99cc,#800000,#cc99FF,#000080,#808000";//      
		String RectHeadFontColor="";//      
		String PillarNum="1";//    
		String PillarInfo="";//    
		String Gradual="1";//    
		String TopTitle="";
		String BottomTitle="";
		String LeftTitle=""; 
		
		
		if (request.getParameter("CoordColor")!=null)
		{
			strCoordColor=request.getParameter("CoordColor");
		}
		if (request.getParameter("Gradual")!=null)
		{
			Gradual=request.getParameter("Gradual");
		}
		if (request.getParameter("Pillarflag")!=null)
		{
			Pillarflag=request.getParameter("Pillarflag");
		}
		if (request.getParameter("RectHeadFontColor")!=null)
		{
			RectHeadFontColor=request.getParameter("RectHeadFontColor");
		}
		if (request.getParameter("PillarInfo")!=null)
		{
			PillarInfo=request.getParameter("PillarInfo");
		}
		if (request.getParameter("PillarNum")!=null)
		{
			PillarNum=request.getParameter("PillarNum");
		}
		if (request.getParameter("TopTitle")!=null)
		{
			TopTitle=request.getParameter("TopTitle");
		}
		if (request.getParameter("BottomTitle")!=null)
		{
			BottomTitle=request.getParameter("BottomTitle");
		}
		if (request.getParameter("LeftTitle")!=null)
		{
			LeftTitle=request.getParameter("LeftTitle");
		}
		out.clear();
		//77CC50
		//String list = "'     ',31330.22,'     ',22333.84,'     ',131110.4677,'     ',72950,'     ',19620,'6',11000,'7',38300,'8',57870,'9',34720,'10',0,'11',66810";
		//String list = "'     ',31330.22,30000.11,'     ',22333.84,20000.22,'     ',131110.4677,130000.33,'     ',72950,78000,'     ',19620,15000,'6',11000,11000,'7',38300,40000,'8',57870,56000,'9',34720,34720,'10',0,1,'11',66810,66000";
	   //String list = "'     ',31330.22,30000.11,30000.11,'     ',22333.84,20000.22,30000.11,'     ',131110.4677,130000.33,30000.11,'     ',72950,78000,30000.11,'     ',19620,15000,30000.11,'6',11000,11000,30000.11,'7',38300,40000,30000.11,'8',57870,56000,30000.11,'9',34720,34720,30000.11,'10',0,1,30000.11,'11',66810,66000,30000.11,";
		
		Test bp = new Test(PillarNum);
		bp.setPillarflag(Pillarflag);
		//bp.setDataList(list);
		bp.setDataList(strChartData);
		//bp.setTopTitle("      ");
		//bp.setBottomTitle("  ");
		bp.setTopTitle(TopTitle);
		bp.setBottomTitle(BottomTitle);
		bp.setLeftTitle(LeftTitle); 
		bp.setFillRectColor(strCoordColor);
		bp.setRectHeadFontColor(RectHeadFontColor);
		bp.setPillarInfo(PillarInfo);
		bp.setGradual(Gradual);
		bp.paintToStream(response);
		//bp.paintToPic(null);
%>
 </body>
</HTML>
<%
		out.clear();
%>