服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一个关于java绘制仿真立体饼图的程序源码,供大家参考


  下面程序是一个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;
//定义标题xy轴信息
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文档要收费)
--------------------------------
希望对大家有所帮助,也希望大家指出程序中的不足,谢谢!

扫描关注微信公众号