热门文章
 
其它的ASP常用组件
常用的 ASP Active
ActiveX 组件
Jmail的主要参数列表
ASP用JMail、CDO发
SA-FileUP组件
用 SA FileUp 上传
AspUpload组件上传例
ASPJPEG组件制作图片的
aspjpeg组件使用方法
 推荐文章
 
Aspjpeg添加水印完整方
组件的使用 asp学习网系列
Adodb.Stream 的
aspjpeg组件使用方法
其它的ASP常用组件
常用的 ASP Active
ActiveX 组件
会员系统“找回密码”的制作方
ASP调用dll
ASP用JMail、CDO发
SA-FileUP组件
用 SA FileUp 上传
检测已经安装的常用服务器组件
Jmail的主要参数列表
组件的使用 asp学习网系列
wsImage组件使用方法
 
你现在的位置:您现在的位置是: 中国ASP>>ASP组件>>常用组件
AspImage使用的例子

http://www.asp888.net

豆腐技术站

aspImage是serverobjects站点上非常好的一个组件,它可以使我们利用Asp实现很多对于图形的处理功能。
比如,豆腐在最近的项目中就需要对 费用进行统计,要画 柱图,饼图,等等,都需要用到这个组件。
这个组件本身没有内置柱图的功能,我们需要自己做这样的函数,还是老规矩,我们先来看看代码吧。
Function DrawPillar(arrNum)
'这个函数的功能是 生成 一个 柱图
'**************以下是变量定义********************
dim arrColor(13)
dim arrNum1()
dim i
dim intX
dim intY
'--------------以下是代码开始--------------------
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
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

intMax=GetMaxY(arrNum)

Set Image = Server.CreateObject("AspImage.Image")
Image.MaxX=620
Image.MaxY=300

intRatio=(Image.MaxY-40)/intMax

Image.GradientTwoWay rgb(41,137,204),vbWhite ,0,1


'Image.BackgroundColor =vbBlue
'Image.FrameRect 15,15,Image.MaxX -15,Image.MaxY -15

'Y 轴
Image.X =40+2
Image.Y =15
Image.LineTo 40+2,Image.MaxY-20
Image.TextOut "↑",40-5,14,false
Image.TextOut "单位:元",15,0,false
for i=1 to 10
Image.TextOut "-" ,40,((Image.MaxY-40)/10)*i,false
'Image.TextOut cDBL((intMax/10))*(i),0,((Image.MaxY-40)/10)*(11-i),false
next

'X轴
Image.X =Image.MaxX-15
Image.Y =Image.MaxY-20
Image.LineTo 40,Image.MaxY-20

Image.TextOut ">",Image.MaxX-15-4 ,Image.MaxY-20-9,false
for i=1 to 12
Image.TextOut i & "月" ,40+ i * (30+15) -20 ,Image.MaxY-20,false
next

if intMax=0 then
intMax=1
end if

'一年有12个月,产生 12个 颜色
intX=40

for i=0 to 11
Image.BackgroundColor = arrColor(i)
Image.Rectangle intX+20,Image.MaxY -20 , intX+50, Image.MaxY -20 - arrNum(i)*intRatio
Image.TextOut arrNum(i),intX+20, Image.MaxY -32 - arrNum(i)*intRatio , false
intX=intX+ 30 +15
next
Image.FileName=server.MapPath("test.jpg")
Image.SaveImage

set Image=nothing
Response.Write "<img src=test.jpg>"
End Function

function GetMaxY(arrNum)
'这个函数的功能是 得到数组中最大 的数字
'***************以下是变量定义************
dim i
dim intMax '寄存器
'---------------以下是代码开始------------
intMax=0
for i=0 to UBOUND(arrNum)
if cDbl(intMax)< cDBL(arrNum(i)) then
intMax=arrNum(i)
end if
Next

GetMaxY=intMax
End Function

我们通过代码Set Image = Server.CreateObject("AspImage.Image")得到一个AspImage的实例,现在
我们就可以利用AspImage提供给我们的动人功能了。
Image.MaxX=620
Image.MaxY=300
我们可以设定我们需要画的图形的区域。
Image.GradientTwoWay rgb(41,137,204),vbWhite ,0,1
这是一个非常非常好的方法,我们可以给这个图片的背静添加一个渐进填充的颜色。

'Y 轴
Image.X =40+2
Image.Y =15
Image.LineTo 40+2,Image.MaxY-20
Image.TextOut "↑",40-5,14,false
Image.TextOut "单位:元",15,0,false
for i=1 to 10
Image.TextOut "-" ,40,((Image.MaxY-40)/10)*i,false
next

这段代码是一个画 Y 轴的代码,不要小看这个"↑",豆腐花了很长的时间才搞定的。:)
'X轴
Image.X =Image.MaxX-15
Image.Y =Image.MaxY-20
Image.LineTo 40,Image.MaxY-20

Image.TextOut ">",Image.MaxX-15-4 ,Image.MaxY-20-9,false
X轴相对来说就简单多了。

'一年有12个月,产生 12个 颜色
intX=40

for i=0 to 11
Image.BackgroundColor = arrColor(i)
Image.Rectangle intX+20,Image.MaxY -20 , intX+50, Image.MaxY -20 - arrNum(i)*intRatio
Image.TextOut arrNum(i),intX+20, Image.MaxY -32 - arrNum(i)*intRatio , false
intX=intX+ 30 +15
next
这端代码是最终生成柱图的代码,具体我就不解释了。

 

 

之二:

前面我们讲了如何利用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("test.jpg")
Image.SaveImage
set Image=nothing
Response.Write "<img src=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 中传递要显示的数据的数组就可以了,生成的图形如下:


相关信息:

其它的ASP常用组件
常用的 ASP ActiveX 组件
ActiveX 组件
Jmail的主要参数列表
ASP用JMail、CDO发送邮件
SA-FileUP组件
用 SA FileUp 上传多文件
AspUpload组件上传例子代码

 

中国ASP技术 ASP.ORG.CN 版权所有 2004-2008