注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
Norton AntiVirus 2005测试版截图
让Ghostbusters为电脑保驾护航
安全小知识:Rundll.exe是病毒吗?
Htran把内网肉鸡做成SOCKS5代理
Windows Vista系统防火墙初探
微软官方解读Vista安全性: 全面的
Windows利器:Rundll.exe高级应用
DNS (domain Name System) 系统的
DNS 系统设定例--7.[Fwd] 特殊网
IMail Anti-Virus简易说明及快速
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Java >> 文章正文
struts开发实践—柱形图实例
作者:佚名  来源:不详  点击:  更新:2006-12-19
简介:

--文章源自一位网友


struts开发实践-柱形图、饼状图实例 本案主要功能是完成柱形图、饼状图的绘制,并将柱形图、饼状图生成jpg图形并显示。 1。调用的主要函数说明:请参考曲线图部分的说明 2。曲线图绘制文件 /*****************program CurveFrame begin************************/ package test; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.JFrame; import java.lang.Math; /**  * 绘图:柱形图;饼形图  */ public class StatRectFrame     extends JFrame {   private int[] result;   private String[] title;   private int statStyle;   private Color[] color = {       Color.blue, new Color(255, 92, 0), new Color(255, 0, 30),       new Color(0, 255, 30), new Color(255, 0, 240), Color.cyan};     /**入口参数:结果集,标题集,统计类型:柱状图 or 饼状图 */   public StatRectFrame(int[] result, String[] title, int statStyle) {     this.result = result;     this.title = title;     this.statStyle = statStyle;   }     public void paint(Graphics g) {     Graphics2D g2 = (Graphics2D) g;     Dimension dim = this.getSize();     if (statStyle == 1) {//饼状图       g2.setColor(Color.white);       g2.fillRect(0, 0, dim.width, dim.height);       int total = result[result.length - 1];       int begTangle = 0;       int endTangle = 0;       g2.setColor(Color.black);       int pointx=dim.width/2;//圆心x       int pointy=dim.height/2;//圆心y         int r=150;//半径       long[] pointsx=new long[result.length-1];       long[] pointsy=new long[result.length-1];       pointsx[0]=pointx+r;       pointsy[0]=pointy;       for (int i = 1; i < result.length - 1; i++) {         if (i >= 6) {           g2.setColor(color[i % 6]);         }         else {           g2.setColor(color[i]);         }           if (result[i] > 0) {           int percent=Math.round((result[i]*100f)/total);           endTangle = Math.round(percent*360f/100f);           title[i]=title[i]+"("+percent+"%)";           g2.fillArc( pointx-r,pointy-r, 2*r, 2*r,                      begTangle, endTangle);             //新中心点           pointsx[i]=Math.round(Math.cos(Math.toRadians(begTangle+endTangle*0.5))*r+pointx);           pointsy[i]=Math.round(pointy-Math.sin(Math.toRadians(begTangle+endTangle*0.5))*r);           //延长点           long pointsxEx=Math.round(Math.cos(Math.toRadians(begTangle+endTangle*0.5))*(r+50)+pointx);           long pointsyEx=Math.round(pointy-Math.sin(Math.toRadians(begTangle+endTangle*0.5))*(r+50));           g2.setColor(Color.black);           g2.draw(new Line2D.Double(pointsx[i], pointsy[i], pointsxEx, pointsyEx));           //画文本框           g2.setColor(new Color(251,249,206));           if(((int)pointsxEx)<pointx){             g2.fillRect( (int) pointsxEx-110, (int) pointsyEx - 10, 110, 20);             g2.setColor(Color.black);             g2.drawString(title[i],(int)pointsxEx-110,(int)pointsyEx+5);           }           else{             g2.fillRect( (int) pointsxEx, (int) pointsyEx - 10, 110, 20);             g2.setColor(Color.black);             g2.drawString(title[i],(int)pointsxEx,(int)pointsyEx+5);           }           begTangle = begTangle + endTangle;         }       }       }     else {//柱状图       // make sure to fill in the background color       g2.setColor(Color.white);       g2.fillRect(0, 0, dim.width, dim.height);         // draw the x and y axis       g2.setPaint(Color.black);       g2.setStroke(new BasicStroke(2.0f));       g2.draw(new Line2D.Double(10, 10, 10, dim.height - 120));       g2.draw(new Line2D.Double(10, dim.height - 120,                                 dim.width, dim.height - 120));         //画柱形图       Font font = g2.getFont().deriveFont(12.0f);       g2.setFont(font);       // font.layoutGlyphVector()       int height = Math.round(dim.height - 120 - 30) / result[0];       for (int i = 1; i < result.length - 1; i++) {         g2.setPaint(Color.black);         g2.drawString("" + result[i], 20 + (i - 1) * 30,                       dim.height - 120 - height * result[i] - 20);         //draw the title         //print one by one;         for (int j = 1; j <= Math.round(title[i].length() / 2); j++) {           g2.drawString(title[i].substring( (j - 1) * 2, j * 2),                         20 + (i - 1) * 30, dim.height - 120 + j * 20);         }         if (i >= 6) {           g2.setColor(color[i % 6]);         }         else {           g2.setColor(color[i]);         }         g2.fillRect(20 + (i - 1) * 30, dim.height - 120 - height * result[i],                     20, height * result[i]);       }     }     }   }/*****************program end************************/ 3。生成jpg图形并显示:与曲线图中介绍的部分类似,不再赘述。






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务