2005-11-25

在JSP页面中轻松实现数据饼图

来源: 本站收集整理 作者:佚名 评论 0 条
 

arr[rs.getInt("productID")] = rs.getString("productname");

  一些数据库治理系统在缺省情况下就答应数据的自动累加或者自动排序。当你在设计数据库时,一定先查明你的数据库治理系统遵循哪些规则,比如自动累加,自动排序等。
  获取总销售量

  在多数情况下,销售列表中会有很多个记录,所以访问数据库的快捷性和高效性显得非常重要。现在我们只需要访问数据库中每一种产品的总额销售量。
  表C中的getSales()方法与数据库连接并返回一个数组,这个数组包含每一种产品的总额出售量。

  Listing C

////////////////////////////////////////////////////////////
//Get the sales totals from the database
////////////////////////////////////////////////////////////
public float[] getSales(int products)
{
 float[] arr = new float[products];
 Connection con;
 Statement stmt;
 ResultSet rs;
 int count = 0;
 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");
    count ;
   }
  }
  stmt.close();
  con.close();
 }
 catch (java.lang.Exception ex)
 {
  arr[0] = -1.0f;
 }
 return arr;
}

  当getSales()遍历所有的记录后,它只存储的是每一种产品新的出售量:

int product = rs.getInt("productID");
arr[product] = rs.getFloat("amount");

  pieColor对象

  饼状图形上的每一种产品应该以不同的颜色显示。为了达到这一目的,我们建立一个pieColor对象(如表D)所示,这一对象包含有关颜色的数组:

Color pieColorArray[] = {new Color(210,60,60), new Color(60,210,60)…}

  pieColor类定义了一个setNewColor()的方法,这一方法能够使curPieColor和索引递增,同时它可以检查索引不要超过边界范围,即采用的方法是:假如curPieColor过大即赋0值。

  更有效的是,setNewColor()循环每一种颜色后,并在第一种颜色下执行以下的代码:

curPieColor ;
if(curPieColor >= pieColorArray.length)
{curPieColor = 0;}

  RenderingHints和antialiasing类

  java.awt.RenderingHints类定义了很多方法以显示二维图形,包括alpha_interpolation, 抖动,以及antialiasing方法。RenderingHints有助于决定图形如何显示以及图形如何达到最佳处理。
共3页: 上一页 [1] 2 [3] 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:网页设计制作规范  
下一篇:ASP生成XBM图可用作验证码
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号