\n
源代码:
\n
\n
\n
\n
//+————————+
\n
//| pie3dfun.PHP//公用函数 |
\n
//+————————+
\n
\n
define(“ANGLE_STEP”, 5); //定义画椭圆弧时的角度步长
\n
\n
function draw_getdarkcolor(img,clr) //求clr对应的暗色
\n
{
\n
rgb = imagecolorsforindex(img,clr);
\n
return array(rgb["red"]/2,rgb["green"]/2,rgb["blue"]/2);
\n
}
\n
\n
function draw_getexy(a, b, d) //求角度d对应的椭圆上的点坐标
\n
{
\n
d = deg2rad(d);
\n
return array(round(a*Cos(d)), round(b*Sin(d)));
\n
}
\n
\n
function draw_arc(img,ox,oy,a,b,sd,ed,clr) //椭圆弧函数
\n
{
\n
n = ceil((ed-sd)/ANGLE_STEP);
\n
d = sd;
\n
list(x0,y0) = draw_getexy(a,b,d);
\n
for(i=0; i<n; i++)
\n
{
\n
d = (d+ANGLE_STEP)>ed?ed:(d+ANGLE_STEP);
\n
list(x, y) = draw_getexy(a, b, d);
\n
imageline(img, x0+ox, y0+oy, x+ox, y+oy, clr);
\n
x0 = x;
\n
y0 = y;
\n
}
\n
}
\n
\n
function draw_sector(img, ox, oy, a, b, sd, ed, clr) //画扇面
\n
{
\n
n = ceil((ed-sd)/ANGLE_STEP);
\n
d = sd;
\n
list(x0,y0) = draw_getexy(a, b, d);
\n
imageline(img, x0+ox, y0+oy, ox, oy, clr);
\n
for(i=0; i<n; i++)
\n
{
\n
d = (d+ANGLE_STEP)>ed?ed:(d+ANGLE_STEP);
\n
list(x, y) = draw_getexy(a, b, d);
\n
imageline(img, x0+ox, y0+oy, x+ox, y+oy, clr);
\n
x0 = x;
\n
y0 = y;
\n
}
\n
imageline(img, x0+ox, y0+oy, ox, oy, clr);
\n
list(x, y) = draw_getexy(a/2, b/2, (d+sd)/2);
\n
imagefill(img, x+ox, y+oy, clr);
\n
}
\n
\n
function draw_sector3d(img, ox, oy, a, b, v, sd, ed, clr) //3d扇面
\n
{
\n
draw_sector(img, ox, oy, a, b, sd, ed, clr);
\n
if(sd<180)
\n
{
\n
list(R, G, B) = draw_getdarkcolor(img, clr);
\n
clr=imagecolorallocate(img, R, G, B);
\n
if(ed>180) ed = 180;
\n
list(sx, sy) = draw_getexy(a,b,sd);
\n
sx += ox;
\n
sy += oy;
\n
list(ex, ey) = draw_getexy(a, b, ed);
\n
ex += ox;
\n
ey += oy;
\n
imageline(img, sx, sy, sx, sy+v, clr);
\n
imageline(img, ex, ey, ex, ey+v, clr);
\n
draw_arc(img, ox, oy+v, a, b, sd, ed, clr);
\n
list(sx, sy) = draw_getexy(a, b, (sd+ed)/2);
\n
sy += oy+v/2;
\n
sx += ox;
\n
imagefill(img, sx, sy, clr);
\n
}
\n
}
\n
function draw_getindexcolor(img, clr) //RBG转索引色
\n
\n
{
\n
R = (clr>>16) & 0xff;
\n
G = (clr>>8)& 0xff;
\n
B = (clr) & 0xff;
\n
return imagecolorallocate(img, R, G, B);
\n
}
\n
\n
// 绘图主函数,并输出图片
\n
// datLst 为数据数组, datLst 为标签数组, datLst 为颜色数组
\n
// 以上三个数组的维数应该相等
\n
function draw_img(datLst,labLst,clrLst,a=250,b=120,v=20,font=10)
\n
{
\n
ox = 5+a;
\n
oy = 5+b;
\n
fw = imagefontwidth(font);
\n
fh = imagefontheight(font);
\n
\n
n = count(datLst);//数据项个数
\n
\n
w = 10+a*2;
\n
h = 10+b*2+v+(fh+2)*n;
\n
\n
img = imagecreate(w, h);
\n
\n
//转RGB为索引色
\n
for(i=0; i<n; i++)
\n
clrLst[i] = draw_getindexcolor(img,clrLst[i]);
\n
\n
clrbk = imagecolorallocate(img, 0xff, 0xff, 0xff);
\n
clrt = imagecolorallocate(img, 0×00, 0×00, 0×00);
\n
\n
//填充背景色
\n
imagefill(img, 0, 0, clrbk);
\n
\n
//求和
\n
tot = 0;
\n
for(i=0; i<n; i++)
\n
tot += datLst[i];
\n
sd = 0;
\n
ed = 0; 333
\n
ly = 10+b*2+v;
\n
for(i=0; i<n; i++)
\n
{
\n
sd = ed;
\n
ed += datLst[i]/tot*360;
\n
\n
//画圆饼
\n
draw_sector3d(img, ox, oy, a, b, v, sd, ed, clrLst[i]); //sd,ed,clrLst[i]);
\n
\n
//画标签
\n
imagefilledrectangle(img, 5, ly, 5+fw, ly+fh, clrLst[i]);
\n
imagerectangle(img, 5, ly, 5+fw, ly+fh, clrt);
\n
//imagestring(img, font, 5+2*fw, ly, labLst[i].”:”.datLst[i].”(“.(round(10000*(datLst[i]/tot))/100).”%)”, clrt);
\n
\n
str = iconv(“GB2312″, “UTF-8″, labLst[i]);
\n
ImageTTFText(img, font, 0, 5+2*fw, ly+13, clrt, “./simsun.ttf”, str.”:”.datLst[i].”(“.(round(10000*(datLst[i]/tot))/100).”%)”);
\n
ly += fh+2;
\n
}
\n
\n
//输出图形
\n
header(“Content-type: image/png”);
\n
\n
//输出生成的图片
\n
imgFileName = “../temp/”.time().”.png”;
\n
imagepng(img,imgFileName);
\n
echo ”””””””””””””””””””””””””””””””””””’
\n
}
\n
\n
datLst = array(30, 10, 20, 20, 10, 20, 10, 20); //数据
\n
labLst = array(“中国科技大学”, “安徽理工大学”, “清华大学”, “北京大学”, “南京大学”, “上海大学”, “河海大学”, “中山大学”); //标签
\n
clrLst = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0×009999);
\n
\n
//画图
\n
draw_img(datLst,labLst,clrLst);
\n
?>
\n