在JSP页面中轻松实现数据饼图(4)
[作者]:菩提树下的杨过 [来源]:互联网 [收录时间]:2007-8-1 20:18:00

  附:本文全部源代码

Listing E

< %@ page language="java" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.geom.*" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%!
////////////////////////////////////////////////////////////
// PieColors class manages the colors used in the pie chart
////////////////////////////////////////////////////////////
class PieColors
{
  Color pieColorArray[] = {
   new Color(210,60,60), new Color(60,210,60), new Color(60,60,210),
   new Color(120,60,120), new Color(60,120,210), new Color(210,120,60)
   };
  int curPieColor = 0;
  public Color getPieColor()
  {
   return pieColorArray[curPieColor];
  }
  public void setNewColor()
  {
   curPieColor++;
   if(curPieColor >= pieColorArray.length)
   {curPieColor = 0;}
  }
}
%>
<%! String driver = "com.mysql.jdbc.Driver"; String dburl = "jdbc:mysql://localhost/articles"; String dbuid = "myuid"; String dbpwd = "mypwd";

////////////////////////////////////////////////////////////
// Get the products from the database as a String array
////////////////////////////////////////////////////////////
public String[] getProducts()
{
  String[] arr = new String[0];
  Connection con;
  Statement stmt;
  ResultSet rs;
  int count = 0;
  String sql = "select * from p_products order by productID";
  try
  {
   //Load Driver:
   Class.forName(driver);
   //Connect to the database with the url
   con = DriverManager.getConnection(dburl , dbuid , dbpwd);
   stmt = con.createStatement();
   //Get ResultSet
   rs = stmt.executeQuery(sql);
   //Count the records
   while(rs.next()){count++;
  }
  //Create an array of the correct size
  arr = new String[count];
  //Get ResultSet (the most portable way of using rs a second time)
  rs = stmt.executeQuery(sql);
  while(rs.next())
  {
   arr[rs.getInt("productID")] = rs.getString("productname");
  }
  stmt.close();
  con.close();
}
  catch (java.lang.Exception ex)
  {arr[0] = ex.toString();}
  return arr;
}
////////////////////////////////////////////////////////////
//Get the sales totals from the database
////////////////////////////////////////////////////////////
public float[] getSales(int products)
{
  float[] arr = new float[products];
  Connection con;
  Statement stmt;
  ResultSet rs;
  String sql = "select productID, amount from p_sales";
  try {
   //Load Driver:
   Class.forName(driver);
   //Connect to the database with the url
   con = DriverManager.getConnection(dburl , dbuid , dbpwd);
   stmt = con.createStatement();
   //Get ResultSet
   rs = stmt.executeQuery(sql);
   while (rs.next()) { int product = rs.getInt("productID");
   //Check that the productID is valid
   if (product >= 0 && product < products)
   {
    //Add to product total
    arr[product] += rs.getFloat("amount");
   }
  }
  stmt.close();
  con.close();
} catch (java.lang.Exception ex) {arr[0] = -1.0f; }
  return arr; } %>
<%
  //get an array that contains the product names
  String products[] = getProducts();
  //read the data and store the totals in an array
  float sales[] = getSales(products.length);
  //Declare PieColors PieColors
  pc = new PieColors();
  //Colors Color
  dropShadow = new Color(240,240,240);
  //inner padding to make sure bars never touch the outer border
  int innerOffset = 20;
  //Set the graph's outer width & height
  int WIDTH = 400;
  int HEIGHT = 200;
  int pieHeight = HEIGHT - (innerOffset * 2);
  int pieWidth = pieHeight;
  //To make a square (circular) pie
  int halfWidth = WIDTH/2;
  //Width of the inner graphable area
  int innerWIDTH = WIDTH - (innerOffset * 2);
  //graph dimensions Dimension
  graphDim = new Dimension(WIDTH,HEIGHT);
  Rectangle graphRect = new Rectangle(graphDim);
  //border dimensions
  Dimension borderDim = new Dimension(halfWidth-2,HEIGHT-2);
  Rectangle borderRect = new Rectangle(borderDim);

  /////////////////////////////////////////////////////////////
  //Set up the graph
  ////////////////////////////////////////////////////////////
  //Set content type
  response.setContentType("image/jpeg");
  //Create BufferedImage & Graphics2D
  BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  Graphics2D g2d = bi.createGraphics();
  // Set Antialiasing 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 = 20;
  //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);