下面程序是一個APPLET,有興趣的朋友可以將其改為BEAN,
/程序名稱:餅形圖
//程序類型:JAVA APPLET
//文件類型:模板文件,由HTML驅動,數據為模擬數據
//原文件名:CakeChart.java
//程序版本:1.5
//程序設計人:楊勇
//更新日期:2003-3-13
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.*;
import java.util.*;
import java.awt.geom.Arc2D;
import java.awt.geom.*;
import java.awt.Polygon;
import java.util.Date;
import java.applet.*;
public class CakeChart extends Applet
{
//初始數據
private double data[]={100.,50,113.,127.,299.234,120.,93.,123.,127.,199.,157.,189.};
private String percent[]=new String[data.length]; //每個數據所占百分比
private int radian[]=new int[data.length]; //每個數據所對應的弧度數
private double max,min;
//初始化每塊餅的顏色
private int dia=240;//設置餅圖透明度
private Color c1=new Color(0,255,0,dia);
private Color c2=new Color(255,255,0,dia);
private Color c3=new Color(255,0,0,dia);
private Color c4=new Color(255,128,64,dia);
private Color c5=new Color(255,128,255,dia);
private Color c6=new Color(255,0,128,dia);
private Color c7=new Color(233,124,24,dia);
private Color c8=new Color(204,119,115,dia);
private Color c9=new Color(89,159,230,dia);
private Color c10=new Color(148,140,179,dia);
private Color c11=new Color(128,0,64,dia);
private Color c12=new Color(174,197,208,dia);
private Color colors[]={c1,c2,c3,c12,c11,c6,c4,c8,c9,c10,c5,c7};
//統計圖的寬度和高度
private int width=600;
private int height=400;
//定義標題起始坐標變量
private int titleStart_x;
private int titleStart_y;
//定義圓心坐標
private int oval_x=60;
private int oval_y=90;
//定義橢圓的長軸和短軸
private int long_axes=280;
private int short_axes=200;
//定義圖例區域起點坐標
private int cutlineRect_x=70;
private int cutlineRect_y=450;
//定義圖倒區域矩形的寬度和高度
private int cutlineRect_width=100;
private int cutlineRect_heigth=235;
//定義日期變量
private Date date=new Date();
//定義統計圖其他顯示信息的超始坐標
private int otherInfo_x1;
private int otherInfo_y1;
private int otherInfo_x2;
private int otherInfo_y2;
private int otherInfo_x3;
private int otherInfo_y3;
//定義標題\X\Y軸信息
private String title="服務器流量統計 (餅形統計圖) ";
private String otherInfo1="西南師范大學網絡教育學院";
private String otherInfo2="www.eduwest.com";
private String otherInfo3="繪圖日期:";
//雙緩沖設置
private Image offScreenImage =null;
private Graphics offScreenBuffer =null;
public CakeChart()
{
}
public void init()
{
offScreenImage=this.createImage(width,height);
offScreenBuffer=offScreenImage.getGraphics();
NumberBudget();
CoorBudget();
}
public void NumberBudget()
{
//求數據中的最大值和最小值
max=data[0];
min=data[0];
for(int mm=0;mm<data.length;mm++)
{
if(data[mm]>max)
max=data[mm];
if(data[mm]<min)
min=data[mm];
}
//對數據進行求和運算
float allData_sum=0;
for(int s=0;s<data.length;s++)
{
allData_sum+=data[s];
}
//計算每個數據占總數的百分比
for(int p=0;p<data.length;p++)
{
percent[p]=String.valueOf(Math.round(data[p]/allData_sum*100))+"%";
}
//計算每個數據所對應的弧度數
for(int r=0;r<data.length;r++)
{
radian[r]=Math.round((float)data[r]/allData_sum*360);
}
}
public void CoorBudget()
{
//預算標題信息的起始坐標
titleStart_x=22;
titleStart_y=(width/2)-(title.length()*15/2);
//初始化統計圖其他顯示信息的起始坐標(位置固定)
otherInfo_x1=390;
otherInfo_y1=450;
otherInfo_x2=400;
otherInfo_y2=480;
otherInfo_x3=390;
otherInfo_y3=5;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
PaintBackground(offScreenBuffer);
PaintChart(offScreenBuffer);
g.drawImage(offScreenImage,0,0,this);
}
public void PaintBackground(Graphics g)
{
//漸變背景初始顏色
Color BackStartColor=Color.white;
Color BackLastColor=new Color(162,189,230);
Color titleColor=Color.black;
Color otherInfoColor=new Color(41,78,118);
//標題背景顏色
Color titleBackColor=new Color(147,179,225);
//統計圖中心區域顏色
Color cutlineColor=new Color(0,128,255,50);
//圖例數據顏色
Color cutDataColor=Color.white;
Font titleFont=new Font("黑體",Font.BOLD,18);
Font otherFont=new Font("宋體",Font.PLAIN,12);
String year="";
String month="";
String day="";
Graphics2D g2=(Graphics2D)g;
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(hints);
GradientPaint gradient=new GradientPaint(0,0,BackStartColor,0,400,BackLastColor,false);
g2.setPaint(gradient);
Rectangle2D rect=new Rectangle2D.Double(0,0,width,height);
g2.fill(rect);
//繪制標題背景
g2.setColor(titleBackColor);
g2.fill3DRect(0,0,width,30,true);
//繪制圖例區域背景
g2.setColor(cutlineColor);
g2.fillRect(cutlineRect_y,cutlineRect_x,cutlineRect_width,cutlineRect_heigth);
g2.setColor(Color.white);
g2.drawRect(cutlineRect_y,cutlineRect_x,cutlineRect_width,cutlineRect_heigth);
//繪制統計圖標題
g2.setFont(titleFont);
g2.setColor(titleColor);
g2.drawString(title,titleStart_y,titleStart_x);
//顯示統計圖其它信息
g2.setFont(otherFont);
g2.setColor(otherInfoColor);
g2.drawString(otherInfo1,otherInfo_y1,otherInfo_x1);
g2.drawString(otherInfo2,otherInfo_y2,otherInfo_x2);
g2.drawString(otherInfo3,otherInfo_y3,otherInfo_x3);
//顯示繪制日期
year=Integer.toString(1900+date.getYear());
month=Integer.toString(date.getMonth()+1);
day=Integer.toString(date.getDate());
g2.drawString(year+"年"+month+"月"+day+"日",otherInfo_y3+60,otherInfo_x3);
//顯示數據百分比
int colorRectWH=15;
int space=5; //圖例中小色塊之間的間隔距離
int addData=cutlineRect_x;
for(int i=0;i<data.length;i++)
{
g2.setColor(colors[i]);
g2.fill3DRect(cutlineRect_y,addData,colorRectWH,colorRectWH,true);
if(data[i]==max || data[i]==min)
g2.setColor(Color.red);
else
g2.setColor(cutDataColor);
g2.drawString(String.valueOf(data[i])+" ("+percent[i]+")",cutlineRect_y+20,addData+colorRectWH-space);
addData+=colorRectWH+space;
}
}
public void PaintChart(Graphics g)
{
int start=0;
int rVal=192;
int gVal=192;
int bVal=192;
int frameCount=60;
Graphics2D g2=(Graphics2D)g;
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHints(hints);
for(int t=20;t>=0;t--)
{
rVal=t*255/frameCount;
gVal=t*255/frameCount;
bVal=t*255/frameCount;
g2.setColor(new Color(rVal,gVal,bVal,50));
g2.drawOval(oval_x,oval_y+t,long_axes,short_axes);
}
for(int a=0;a<data.length;a++)
{
Arc2D arc = new Arc2D.Float(Arc2D.PIE);
g2.setColor(colors[a]);
arc.setFrame(oval_x,oval_y,long_axes,short_axes);
arc.setAngleStart(start);
arc.setAngleExtent(radian[a]);
g2.fill(arc);
if(data[a]==max || data[a]==min)
g2.setColor(Color.white);
else
g2.setColor(new Color(223,223,223,150));
g2.draw(arc);
start+=radian[a];
}
}
HTML程序:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<APPLET CODE="CakeChart.class" WIDTH="600" HEIGHT="400">
</APPLET>
</BODY>
</HTML>
==============================
說明:
必須將HTML和CLASS放在一個目錄裡,必須安裝JRE(1.4及以上版本).
==============================
國外也有類似API包,有免費的也有收費的,
比如:http://www.visualmining.com(商業的,不過可以學習其系統架構,很不錯)
http://www.jfree.org/(免費的,不過API文檔要收費)
--------------------------------
希望對大家有所幫助,也希望大家指出程序中的不足,謝謝!