例子是饼图,画圆改画矩形我就偷懒了.
ShowImage.jsp
<img src="/Test/Image.jsp?ElementCount=<%=4%>&ShowType=<%=1%>&Data=">
Image.jsp
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="com.sun.image.codec.jpeg.*"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.awt.geom.*"%>
<%@ page import="java.awt.image.*"%>
<%
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
OutputStream outImage=response.getOutputStream();
Color color[]={Color.red,Color.black,Color.orange,Color.green};
BufferedImage bufImage=new BufferedImage(150,100,BufferedImage.TYPE_INT_RGB);
Graphics2D g=(Graphics2D)bufImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.white);
g.fillRect(0,0,150,100); 
int iBaseAng=30;
int iElementCount,iShowType;
String strElementCount=request.getParameter("ElementCount");
String strShowType=request.getParameter("ShowType");
String strData=request.getParameter("Data");
if(strElementCount!=null)
{iElementCount=4;}
else
{iElementCount=4;}
if(strShowType!=null)
{iShowType=1;}
else
{iShowType=1;}
int[] Array_Ang=new int[iElementCount];
Array_Ang[0]=(int)(.35*360);
Array_Ang[1]=(int)(.15*360);
Array_Ang[2]=(int)(.25*360);
Array_Ang[3]=360-Array_Ang[0]-Array_Ang[1]-Array_Ang[2];
AffineTransform iAt=null;
Arc2D m_Arc=null;
int iFromAng=iBaseAng;
iAt=AffineTransform.getRotateInstance((-20*java.lang.Math.PI)/180,45,37);
g.setTransform(iAt);
switch(iShowType)
{
case 1:
int iR=6;
int iX=(int)(iR*java.lang.Math.cos((iBaseAng+Array_Ang[0])/2.0*java.lang.Math.PI/180));
int iY=(int)(iR*java.lang.Math.sin((iBaseAng+Array_Ang[0])/2.0*java.lang.Math.PI/180));
m_Arc=new Arc2D.Double(10+iX,24-iY,80,50,iFromAng,Array_Ang[0],Arc2D.PIE);
g.setColor(color[0]);
g.fill(m_Arc);
iFromAng+=Array_Ang[0];
for(int iIndex=1;iIndex<iElementCount;iIndex++)
{
g.setColor(color[iIndex]);
m_Arc=new Arc2D.Double(10,24,80,50,iFromAng,Array_Ang[iIndex],Arc2D.PIE);
g.fill(m_Arc);
iFromAng+=Array_Ang[iIndex];
if(iFromAng>360)
{
iFromAng-=360;
}
}
break;
case 2:
break;
default:
break;
}
iAt=AffineTransform.getRotateInstance(0,m_Arc.getCenterX(),m_Arc.getCenterY());
g.setTransform(iAt);
for(int jIndex=0;jIndex<iElementCount;jIndex++)
{
g.setColor(color[jIndex]);
g.fillRect(100,15*jIndex+20,10,10);
g.drawString("No."+jIndex,120,15*jIndex+20+8);
}
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(outImage);
encoder.encode(bufImage);
outImage.close();
%>

解决方案 »

  1.   

    //生成图片的 Java Bean
    import java.io.*;
    import java.util.*;
    import com.sun.image.codec.jpeg.*;
    import java.awt.image.*;
    import java.awt.*;public class ChartGraphics {
      BufferedImage image;
      public void createImage(String fileLocation) {
        try {
          FileOutputStream fos = new FileOutputStream(fileLocation);
          BufferedOutputStream bos = new BufferedOutputStream(fos);
          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
          encoder.encode(image);
          bos.close(); 
        } catch(Exception e) {
          System.out.println(e);
        }
      }  
      
      public void graphicsGeneration(int[] height) {
      
        final int X=10;
        int imageWidth = 300;//图片的宽度
        int imageHeight = 300;//图片的高度   
        int columnWidth=30;//柱的宽度
        int columnHeight=210;//柱的最大高度 + 10
        
        ChartGraphics chartGraphics = new ChartGraphics();
        chartGraphics.image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); 
        Graphics graphics = chartGraphics.image.createGraphics(); //返回一个与该图像相关的 Graphics2D 对象
        graphics.setColor(Color.white);
        graphics.fillRect(0,0,imageWidth,imageHeight); 
    //画坐标轴
        graphics.setColor(Color.green);
    graphics.drawLine(0,200,0,0);
    graphics.drawArc(0,0,2,2,2,2);
    graphics.drawString("y",2,10);
    graphics.drawLine(0,200,300,200);
    graphics.drawString("x",290,210);
        //画折线
        int[] x = {10,40,80,100,150};
        int[] y = {columnHeight-20,columnHeight-110,columnHeight-80,columnHeight-200,columnHeight-150};
        graphics.setColor(Color.blue);
        graphics.drawPolyline(x,y,5);
    //画柱状图
    graphics.setColor(Color.red);
    for (int i = 0; i < height.length; i++) {
    graphics.drawRect((i+1)*(columnWidth+10), columnHeight-height[i]-10, columnWidth, height[i]);
    graphics.fillRect((i+1)*(columnWidth+10), columnHeight-height[i]-10, columnWidth, height[i]);
    graphics.drawString("String"+i,(i+1)*(columnWidth+10), columnHeight);
        }
    chartGraphics.createImage("%path%chart.jpg"); //chart.jpg应包含生成的jpg文件的路径
      } 
    }