/* 豆腐制作,都是精品 http://www.asp888.net 豆腐技术站 如转载,请保留完整版权信息 */
前面我们讲了如何利用AspImage来制作柱图,柱图还好办,起码有关于长方形的函数,我们可以来 借用,但是如果是,饼图怎么办? 有的朋友不是说了,AspImage上不是有Pie的函数吗?是呀,它是有,但是它的那个也太难用了。 豆腐没有办法,经过同事的帮助,利用我们高中学习的三角形公式(三角形公式?有没有搞错?) 终于做出了这样的函数,而且使用起来非常的方便。大家请看 Function DrawPie(ArrNum,arrText) '函数功能:根据指定的 数值和显示,他们均是 数组 '**********以下是变量定义************************** dim intTotal '当前 dim i dim intSettledAngle dim arrColor(6) '----------以下是代码开始-------------------------- '设置颜色 i=0 arrColor(i)=RGB(255,255,255) i=i+1 arrColor(i)=RGB(255,255,0) i=i+1 arrColor(i)=RGB(255,0,255) i=i+1 arrColor(i)=RGB(0,255,255) i=i+1 arrColor(i)=RGB(255,0,0) i=i+1 arrColor(i)=RGB(0,255,0) i=i+1 arrColor(i)=RGB(0,0,255) i=i+1 '以下开始 对数据进行处理 '首先得到 数量的总数
intTotal=0 for i=0 to UBOUND(ArrNum) intTotal=intTotal + ArrNum(i) Next
Set Image = Server.CreateObject("AspImage.Image")
'设定 图象的 区域大小 Image.MaxX=300 Image.MaxY=300 '生成渐进色 Image.GradientTwoWay rgb(41,137,204),vbWhite ,0,1
'处理角度 intSettledAngle=0 intRectStart=0 for i=0 to ubound(ArrNum) intAngle=(arrNum(i)/intTotal)*360 '一个一个的画 扇区,最终合成一个完整的 圆 set Image=DrawSinglePie(Image, 360-intSettledAngle,arrColor(i))
'在图象的最下方 对图象内容进行描述 Image.BackgroundColor =arrColor(i) Image.Rectangle intRectStart+10*(i+1),250,intRectStart+10*(i+1)+10,260 Image.TextOut arrText(i), intRectStart+10*(i+1)+10,245,false
'在图象的最下方 对图象内容进行描述 Image.BackgroundColor =arrColor(i) Image.Rectangle intRectStart+10*(i+1),270,intRectStart+10*(i+1)+10,280 Image.TextOut cstr(intAngle) & "%", intRectStart+10*(i+1)+10, 265,false intRectStart=intRectStart+50 intSettledAngle=intSettledAngle + intAngle next Image.FileName=server.MapPath("http://www.okasp.com/techinfo/test.jpg") Image.SaveImage set Image=nothing Response.Write "<img src=http://www.okasp.com/techinfo/test.jpg>" End Function
function DrawSinglePie(Image,intAngle,intColor) '函数功能: 根据指定的 角度和颜色 画一个矩形 '**************以下是变量定义******************** const pi=3.1415926 '圆周率 dim pii '经过180角转换后的圆周,弧度 dim x1,x2,x3,x4 '4个X坐标 dim y1,y2,y3,y4 '4个Y坐标 dim intR '圆的半径,这个半径不是真正的圆的半径,但是可以用来固定圆心的位置 '--------------以下是代码开始-------------------- '********************************** '* '*(x1,y1),(x2,y2) 和 圆心必须在 通过(0,0) 的 斜角45 的直线上 '*********************************** pii=pi/180 if intAngle > 360 then intAngle=intAngle-360 end if x1=10 y2=10 x2=250 y2=250
intR=(x1+x2)/2 '************************************ '* '*以下利用 三角形 公式 得到相应 Point 的坐标 '************************************ if intAngle<135 then '角度不足 135 angle=intAngle*pii x3=tan(angle-45*pii)*intR+intR ' y3=0 x4=0 y4=0 elseif intAngle=135 then x3=(intR)*2 y3=intR x4=0 y4=0 elseif intAngle<315 then angle=intAngle*pii intTemp=(intR/tan(angle-135*pii))-intR x3=250+intTemp y3=250 x4=0 y4=0 elseif intAngle=315 then x3=(-intR)*2 y3=intR x4=0 y4=0 else angle=intAngle*pii x3=tan(angle-45*pii)*intR+intR y3=0 x4=0 y4=0 end if
Image.BackgroundColor =intColor Image.Pie x1,y1,x2,y2,x3,y3,x4,y4
'Image.TextOut 360-intAngle,(125+x3)/2+20,(125+y3)/2+20,false 'Image.TextOut intRatio ,intPosX,intPosY,false
set DrawSinglePie=Image end function
最后我们在利用 drawPie 中传递要显示的数据的数组就可以了。
|