<%@ page language="java" contentType="charset=gb2312"%> <%  response.setHeader("Cache-Control","no-store"); //HTTP 1.1      response.setHeader("Pragma","no-cache"); //HTTP 1.0      response.setDateHeader ("Expires", 0); //prevents caching at the proxy server  %> <%@ page import="java.io.OutputStream" %> <%@ page import="java.awt.image.*" %> <%@ page import="java.awt.*,java.awt.font.*,java.awt.geom.*,javax.imageio.*,java.io.*" %> 
<%@ page import="com.sun.image.codec.jpeg.*" %>  <%@ page import="acec.*,java.util.*" %>  
<!--%@ include file = "graphBG.jsp"%--> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head> 
<% 
//数据数组 String datanames[] = {"第一季度", "第二季度", "第三季度"}; int datavalues[] = {111, 620, 33}; 
 int WIDTH  = 600;  
 int HEIGHT =400;  
 int innerHEIGHT = HEIGHT-100;  int innerWIDTH = WIDTH-20; 
int maximum = 0; 
for(int i=0; i<datavalues.length; i++) {  if(datavalues[i] > maximum)  {   maximum = datavalues[i];  } } 
response.setContentType("image/jpeg"); BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 
Graphics2D biContext = bi.createGraphics(); 
//插入背景图案 try{  Image image=ImageIO.read(new File("bg.jpg"));  biContext.drawImage(image,0,0,WIDTH,HEIGHT,null); }catch(Exception ex){System.out.println(ex.toString());} 
  
//开始绘图 
int barWidth = innerWIDTH/(datavalues.length*4); 
 //System.out.println("barWidth="+barWidth); for(int i=0,ii=0; i<datavalues.length; i++,ii++) {    Color barColor=null;  Color barColor1=null;  if(ii==0)//蓝色  {   barColor=new Color(48,36,189);   barColor1 = new Color(201,201,245);  }  else if(ii==1)//绿色  {      barColor=new Color(62,145,68);   barColor1 = new Color(210,236,236);     }  else if(ii==2)//红色  {    barColor=Color.red;   barColor1 = new Color(253,181,199);   ii=-1;  } 
  int currentValue = datavalues[i];    //Set bar width  int barHeight = (innerHEIGHT * currentValue) / maximum; 
 biContext.setColor(barColor);  Rectangle barRect = new Rectangle(barWidth,barHeight); 
  
 //取字的宽度  Font f=biContext.getFont();  FontRenderContext context=biContext.getFontRenderContext();  Rectangle2D bounds_top=f.getStringBounds("人气值:"+currentValue,context);   Rectangle2D bounds=f.getStringBounds(datanames[i],context);   biContext.setColor(Color.black); 
  //柱形的定位  int locationY=HEIGHT-barHeight-20;   int locationX=innerWIDTH/datavalues.length*(i+1)-barWidth/2;  if(datavalues.length%2!=0)    locationX=innerWIDTH/(datavalues.length+1)*(i+1)-barWidth/2; 
 barRect.setLocation(locationX,locationY);  biContext.drawString(datanames[i],(int)((locationX+barWidth/2)-bounds.getWidth()/2),HEIGHT-5); 
 biContext.drawString("人气值:"+currentValue, (int)((locationX+barWidth/2)-bounds_top.getWidth()/2),locationY-10);  
  
  
  
 biContext.setPaint(new GradientPaint(locationX,locationY,barColor1,locationX+barWidth,locationY,barColor));  biContext.fill(barRect); } 
  
  
///////////////////////////////////////////////////////////////////////////////////// //保存图片 ///////////////////////////////////////////////////////////////////////////////////// 
 OutputStream bos = new FileOutputStream(application.getRealPath("tp.jpeg"));  JPEGImageEncoder encoder1 = JPEGCodec.createJPEGEncoder(bos);  encoder1.encode(bi);  bos.close(); 
%> <body  leftmargin="0" topmargin="0"> <img src="tp.jpeg" width="600" height="400">  </html>  
 
  |