JFreeCharthttp://www.jfree.org/jfreechart/index.html

解决方案 »

  1.   

    只有饼状图的例子,代码如下。
    package chart;
    import java.awt.*;  //畫圖形
    import java.awt.image.*;
    import javax.imageio.*; //圖開輸出import java.util.*; //在未取得月份時,用來格式化月份
    import java.text.*;import java.io.*; //輸出到頁面
    import javax.servlet.*;
    import javax.servlet.http.*;import java.sql.*; //連接數據庫
    import cdsts.sql.db;
    import java.awt.geom.*;
    import com.sun.image.codec.jpeg.*;
    class PieColors{
    Color pieColorArray[] = {
    new Color(255,0,0), new Color(0,153,0), new Color(0,0,255), 
    new Color(0,255,0), new Color(255,0,255), new Color(255,204,0),
    new Color(204,153,255), new Color(51,204,204), new Color(255,153,0),
    new Color(255,102,0),
    };
    int curPieColor = 0; 
    public Color getPieColor(){
    return pieColorArray[curPieColor]; 
    }
    public void setNewColor(){
    curPieColor++;
    if(curPieColor >= pieColorArray.length) 
    {curPieColor = 0;}
    }
    }public class caky extends HttpServlet{
      public void doGet(HttpServletRequest request,HttpServletResponse response)
                throws ServletException,IOException{
              String szMonth=request.getParameter("month");
              if (szMonth==null || szMonth.equals("")){
                  java.util.Date grc=new java.util.Date();
                  SimpleDateFormat smpDateFormat=new SimpleDateFormat("yyyyMM");
                  szMonth=smpDateFormat.format(grc);
      String products[];
      float sales[];// = {70,100,30,70,100,30,70,100,30,70};          ResultSet rs=null;
         
            int MAX_NUM=0;
            String str = "select count(number) from test";
            try{
             db db_1=new db();
             rs=db_1.getDbResultSet(str);
             if(rs.next()) MAX_NUM=rs.getInt(1);
            }catch(Exception e){System.out.println(e);}
            
            if(MAX_NUM>0)
            {
             products = new String[MAX_NUM];
             sales = new float[MAX_NUM];
         
                String szSQL="select * from temp";           try
               {
             db db1=new db();
             rs=db1.getDbResultSet(szSQL);
             int i=0;
             while(rs.next())
             {
             products[i]=rs.getString("name");
             sales[i]=rs.getInt("number");
             i++;
             }
             rs.close();
                }
               catch (Exception e) 
               {
                System.err.println(e.getMessage());
               }
              
              
              
               float perc=0;
    PieColors pc = new PieColors();
    Color dropShadow = new Color(240,240,240); 
    int innerOffset = 20; 
    int WIDTH = 600;
    int HEIGHT = 300;
    int pieHeight = HEIGHT - (innerOffset * 2); 
    int pieWidth = pieHeight; 
    int halfWidth = WIDTH/2;
    int innerWIDTH = WIDTH - (innerOffset * 2); 
    Dimension graphDim = new Dimension(WIDTH,HEIGHT);
    Rectangle graphRect = new Rectangle(graphDim); 
    Dimension borderDim = new Dimension(halfWidth-2,HEIGHT-2);
    Rectangle borderRect = new Rectangle(borderDim); 

    /////////////////////////////////////////////////////////////
     //Set up the graph 
    //////////////////////////////////////////////////////////// 
    //Set content type
    // response.setContentType("image/gif");
    //Create BufferedImage & Graphics2D
    BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 
    Graphics2D g2d = bi.createGraphics();
    // Set Antialiasing RenderingHints 
    RenderingHints renderHints = new RenderingHints( RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 
    g2d.setRenderingHints(renderHints); 
    //Set graph background color to white: 
    g2d.setColor(Color.white);
    g2d.fill(graphRect); 
    //Draw black border
    g2d.setColor(Color.black);
    borderRect.setLocation(1,1);
    g2d.draw(borderRect); 
    //Now draw border for legend 
    borderRect.setLocation((WIDTH/2) + 1,1); 
    g2d.draw(borderRect); 


    //////////////////////////////////////////////////////////////////// 
    //Draw data onto the graph: 
    ////////////////////////////////////////////////////////////////////
    int x_pie = innerOffset; 
    int y_pie = innerOffset; 
    int border = 0; 
    //Main chart Ellipse
    //Ellipse2D.Double el = new Ellipse2D.Double(x_pie, y_pie, pieWidth, pieHeight); 
    Ellipse2D.Double elb = new Ellipse2D.Double(x_pie - border/2, y_pie - border/2, pieWidth + border, pieHeight + border);
    //Shadow 
    g2d.setColor(dropShadow);
    g2d.fill(elb);
    //Border
    g2d.setColor(Color.black); 
    g2d.draw(elb); 

    ///////////////////////////////////////////////////////////////// 
    //Calculate the total sales 
    /////////////////////////////////////////////////////////////////
    float salesTotal = 0.0f; 
    int lastElement = 0; 
    for(int i=0; i<products.length; i++)
    {
    if(sales[i] > 0.0f) 

    salesTotal += sales[i];
    lastElement = i; 
     }
    }


    //////////////////////////////////////////////////////////////
     //Draw the pie chart 
    ///////////////////////////////////////////////////////////// 
    //Chart variables 
    int startAngle = 0; 
    //Legend variables 
    int legendWidth = 20;
    int x_legendText = halfWidth + innerOffset/2 + legendWidth + 5;
    int x_legendBar = halfWidth + innerOffset/2;
    int textHeight = 20; 
    int curElement = 0; 
    int y_legend = 0;
    //Dimensions of the legend bar
    Dimension legendDim = new Dimension(legendWidth , textHeight/2); 
    Rectangle legendRect = new Rectangle(legendDim);
    for(int i=0; i<products.length; i++)
    {
    if(sales[i] > 0.0f) 
    {
    //Calculate percentage sales float
    perc = (sales[i]/salesTotal);
    //Calculate new angle 
    int sweepAngle = (int)(perc * 360);
    //Check that the last element goes back to 0 position
    if (i == lastElement) 
    {
    sweepAngle = 360-startAngle;
    }
    // Draw Arc 
    g2d.setColor(pc.getPieColor());
    g2d.fillArc(x_pie, y_pie, pieWidth, pieHeight, startAngle, sweepAngle); 
    //Increment startAngle with the sweepAngle 
    startAngle += sweepAngle; 
    /////////////
    //Draw Legend 
    ///////////// 
    //Set y position for bar 
    y_legend = curElement * textHeight + innerOffset; 
    //Display the current product 
    String display = products[i];
    g2d.setColor(Color.black); 
    g2d.drawString(display, x_legendText, y_legend);
    //Display the total sales 
    display = "" + (int)sales[i]; 
    g2d.setColor(Color.black);
    g2d.drawString(display, x_legendText + 80, y_legend);
    //Display the sales percentage
    display = " (" + (int)(perc*100) + "%)"; 
    g2d.setColor(Color.red); 
    g2d.drawString(display, x_legendText + 110, y_legend);
    //Draw the bar
    g2d.setColor(pc.getPieColor()); 
    legendRect.setLocation(x_legendBar,y_legend - textHeight/2); 
    g2d.fill(legendRect); 
    //Set new pie color 
    pc.setNewColor();
    //Increment 
    curElement++; 
    }
    }
               g2d.dispose();//生產圖像
               ImageIO.write(bi, "JPEG", response.getOutputStream());//輸出到頁面
      }
            }
    }
    }
      

  2.   

    不好意思,第二条SQL语句写错了~~
    String szSQL="select * from temp";
    应该改为:
    String szSQL="select * from test";
      

  3.   

    JFreeChart主页:http://www.jfree.org/jfreechart/index.html
    JFreeChart下载页面:http://sourceforge.net/projects/jfreechart/
    看看吧,一定有你感兴趣的,下载下来看看吧
      

  4.   

    jfreechart不错,去年根据它的算法实现过一个三维饼图,看看它的源码就行,自己需要修改的不多