/* 豆腐制作,都是精品 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 中傳遞要顯示的數據的數組就可以了。
|