• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

- Draw a pie chart

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 25次 | 进入软件测试论坛讨论

领测软件测试网  import java.util.*;
import java.awt.*;
import java.applet.Applet;

public class Graph extends Applet {
  int    depth, radius;

public void init() {
   float value;
   String at = getParameter("width");
   radius = (at != null) ?  Integer.valueOf(at).intValue() : 100;
   at = getParameter("depth");
   depth = (at != null) ? Integer.valueOf(at).intValue() : 20;
   at = getParameter("values");
   PieChartCanvas c = new PieChartCanvas(radius, depth);
   setLayout(new BorderLayout());

   // Create Hashtable to map color name (String) to Color type
   Hashtable colors = new Hashtable();
   colors.put("green", Color.green);
   colors.put("red", Color.red);
   colors.put("blue", Color.blue);
   colors.put("yellow", Color.yellow);
   colors.put("magenta", Color.magenta);
   colors.put("cyan", Color.cyan);
   colors.put("orange", Color.orange);
   colors.put("pink", Color.pink);
   colors.put("white", Color.white);
   colors.put("black", Color.black);

   // "value-color,value-color,..."
   StringTokenizer t = new StringTokenizer(at, ",");
   String s;
   int i;
   while (t.hasMoreTokens()) {
     s = t.nextToken();
     i = s.indexOf(@#-@#);
     value = Float.valueOf(s.substring(0, i)).floatValue();
     c.addSlice(value, (Color)colors.get(s.substring(i + 1)));
    }

   resize(c.getMinimumSize().width, c.getMinimumSize().height);
   add("Center", c);
   }
}

class PieChartCanvas extends Canvas {
   /*
   ** author     Ciaran Treanor  [email protected]
   */
   final double aspectFudge = 2.5;
   int radius, depth, called = 1, numSlices = 0;
   float total = 0, value[] = new float[10];
   Color color[] = new Color[10];
   Graphics offGraphics;
   Image gfxBuff;

   public PieChartCanvas(int radius, int depth) {
     this.value = value;
     this.color = color;
     this.radius = radius;
     this.depth = depth;
     }

   public void paint(Graphics g) {
     int startAngle;
     float angle;
     Dimension d = getSize();

     if(gfxBuff == null) {
       gfxBuff = createImage(d.width, d.height);
       offGraphics = gfxBuff.getGraphics();
       offGraphics.setColor(getBackground());
       offGraphics.fillRect(0, 0, d.width, d.height);
       }

     // do the 3d effect
     for(int x = depth; x >= 1; x--) {
       startAngle = -45;
       for(int i = 0; i < numSlices; i++) {
         offGraphics.setColor(color[i].darker());
         angle = Math.round(360 * (value[i] / total));
         offGraphics.fillArc(0, x, radius, (int)(radius / aspectFudge),
             startAngle, (int)angle);
             startAngle += angle;
         }
     }

     // draw the pie slice
     startAngle = -45;
     for(int i = 0; i < numSlices; i++) {
       offGraphics.setColor(color[i]);
       angle = Math.round(360 * (value[i] / total));
       offGraphics.fillArc(0, 0, radius, (int)(radius / aspectFudge),
          startAngle, (int)angle);
          startAngle += angle;
       }
     g.drawImage(gfxBuff, 0, 0, null);
     }

   public void addSlice(float value, Color color) {
     this.value[numSlices] = value;
     this.color[numSlices++] = color;
     total += value;
     }

   public Dimension getPreferredSize() {
     return getMinimumSize();
     }

   public Dimension getMinimumSize() {
     return new Dimension(radius, (int)((radius / aspectFudge) + depth));
     }
   }




[JavaPie.hmtl]
<HTML>
<APPLET CODE=Graph.class WIDTH=150 HEIGHT=150>
   <PARAM NAME="depth"  VALUE="30">
   <PARAM NAME="width"  VALUE="120">
   <PARAM NAME="values" VALUE="1-red,2-green,3-blue">
</APPLET>
</HMTL>

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网