我们工作中经常需要将数据转化成柱状图,饼图等,以方便直观的分析数据, 这里给大家介绍一个asp中制作饼图、柱状图的组件:csdrawgraph,csdgt.zip,因为是组件,所以我们在使用之前需要用regsvr32.exe 注册一下,csdrawgraph,可以在asp中创建饼图,柱状图以及线图,其支持的格式有gif, png, jpg and bmp.
看看如下的柱状图,饼图如何生成的例子:
[align=center][/align]
chartdemo.asp
<%@ language=vbscript %>
csdrawgraph demonstrationthis simple demonstration shows two graphs using the same data. the first is
a bar chart:
the second is a pie chart. the background colour is set to light
grey to show the overall size of the image.
chartimages.asp
<%@ language=vbscript %>
<%
response.expires = 0
response.buffer = true
response.clear
response.contenttype = "image/gif"
set chart = server.createobject("csdrawgraphtrial.draw")
chart.adddata "no> 1", 17, "ff0000"
chart.adddata "no> 2", 28, "00ff00"
chart.adddata "no> 3", 5, "0000ff"
if request.querystring("type") = "pie" then
chart.title = "sample pie chart"
chart.bgcolor = "eeeeee"
chart.labelbgcolor = "eeeeee"
chart.titlebgcolor = "eeeeee"
response.binarywrite chart.gifpie
else
chart.title = "sample bar chart"
response.binarywrite chart.gifbar
end if
response.end
%>
程序很简单,再些不详细说明,下面看一个将
数据库中的数据转换到图表的例子:
lines.asp:
line graph showing all the results
links to the other result pages:
bar chart showing all results for any one day.
bar charts showing results for each colour separately.
gif_lines.asp:
<%@ language=vbscript %>
<%
'利用数据库中的数据生成线图。
'根据4个不同的值分别生成4条线。
'在x轴上显示星期的名称。
response.expires = 0
response.buffer = true
response.clear
'利用下面的语句创建chart对象,版本不同会有所差异。
'set chart = server.createobject("csdrawgraph.draw")
set chart = server.createobject("csdrawgraphtrial.draw")
connectionstring = "provider=microsoft.jet.oledb.4.0;data source=" & _
server.mappath("data.mdb")
set dbconn = server.createobject("adodb.connection")
dbconn.open connectionstring
set rs = server.createobject("adodb.recordset")
sql = "select * from table1 order by day"
rs.open sql, dbconn
while not rs.eof
chart.addpoint cint(rs("day")), cint(rs("red")), "ff0000", "red"
chart.addpoint cint(rs("day")), cint(rs("blue")), "0000ff", "blue"
chart.addpoint cint(rs("day")), cint(rs("green")), "00ff00", "green"
chart.addpoint cint(rs("day")), cint(rs("yellow")), "ffff00", "yellow"
chart.addxvalue cint(rs("day")), rs("dayname")
rs.movenext
wend
'关闭数据库连接
rs.close
dbconn.close
'下面设置组件属性
'x轴坐标从1开始而不是0。(xoffset = 1)
chart.title = "all the combined results"
chart.titlex = 100
chart.yaxistext = "total for each day"
chart.originy = 220
chart.xoffset = 1
chart.xtop = 7
chart.xgrad = 1
chart.usexaxislabels = true
chart.linewidth = 2
chart.pointsize = 3
chart.pointstyle = 1
'最后图片以gif格式发送到浏览器
response.contenttype = "image/gif"
response.binarywrite chart.gifline
response.end
%>
结果如图所示:
[align=center][/align]
csdrawgraph的下载地址:http://chestysoft.com/download/csdgt.zip
该文章在 2010/7/3 14:23:21 编辑过